package cn.pluss.platform.AppletStoreUser.impl;

import cn.pluss.platform.AppletStoreUser.AppletStoreUserService;
import cn.pluss.platform.appletInfo.AppletInfoService;
import cn.pluss.platform.entity.AppletInfo;
import cn.pluss.platform.entity.AppletStore;
import cn.pluss.platform.entity.AppletStoreUser;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.AppletInfoMapper;
import cn.pluss.platform.mapper.AppletStoreMapper;
import cn.pluss.platform.mapper.AppletStoreUserMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author lyf
 */
@Service
public class AppletStoreUserServiceImpl extends ServiceImpl<AppletStoreUserMapper, AppletStoreUser> implements AppletStoreUserService {
    @Resource
    private AppletStoreUserMapper appletStoreUserMapper;
    @Resource
    private AppletStoreMapper appletStoreMapper;
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Integer addAppletStoreUser(AppletStoreUser appletStoreUser) {
        AppletStoreUser appletByUserStore = appletStoreUserMapper.getAppletByUserStore(appletStoreUser.getUserId(), appletStoreUser.getAppleStoreId());
        if (appletByUserStore != null){
            throw new MsgException("请勿重复绑定");
        }
        QueryWrapper<AppletStore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", appletStoreUser.getAppleStoreId());
        AppletStore appletStore = appletStoreMapper.selectOne(queryWrapper);
        if (appletStore == null || appletStore.getType() == 0){
            throw new MsgException("未找到店铺号");
        }
        appletStoreUser.setCreateTime(new Date());
        return baseMapper.insert(appletStoreUser);
    }
}
