puhanshu
2022-01-05 eb80b83a3d4a0b59325e90405dc6c687c2904d3a
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsGameServiceImpl.java
@@ -2,19 +2,29 @@
import static java.util.Objects.isNull;
import javax.annotation.Resource;
import com.panzhihua.common.model.vos.community.microCommercialStreet.TopStatisticsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.McsGameDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.PageMcsGameDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.SetPopularForGameDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.SetShelfForGameDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsGameVO;
import com.panzhihua.service_community.dao.McsGameDAO;
import com.panzhihua.service_community.dao.McsMerchantDAO;
import com.panzhihua.service_community.entity.McsGame;
import com.panzhihua.service_community.entity.McsMerchant;
import com.panzhihua.service_community.service.McsGameService;
import java.util.Date;
/**
 * (McsGame)表服务实现类
@@ -24,6 +34,9 @@
 */
@Service("mcsGameService")
public class McsGameServiceImpl extends ServiceImpl<McsGameDAO, McsGame> implements McsGameService {
    @Resource
    private McsMerchantDAO mcsMerchantDAO;
    /**
     * 分页查询戳戳游戏
@@ -81,6 +94,12 @@
        Integer type = setShelfForGameDTO.getType();
        if (type.equals(1)) {
            //上架
            McsMerchant mcsMerchant = mcsMerchantDAO.selectById(mcsGame.getMerchantId());
            Integer publishLimit = mcsMerchant.getPublishLimit();
            Integer publishCount = this.baseMapper.selectPublishCount(mcsGame.getMerchantId());
            if (publishCount >= publishLimit) {
                return R.fail("空闲戳戳点不足");
            }
            mcsGame.setStatus(2);
        } else if (type.equals(2)) {
            //下架
@@ -108,6 +127,7 @@
            return R.fail("修改数据不存在");
        }
        mcsGame.setIsDel(true);
        mcsGame.setStatus(McsGame.Status.yxj);
        mcsGame.setUpdatedBy(userId);
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
@@ -115,4 +135,179 @@
        }
        return R.fail("删除失败,请重新尝试");
    }
    /**
     * 新增戳戳游戏
     * @param mcsGameDTO
     * @return
     */
    @Override
    public R addMcsGame(McsGameDTO mcsGameDTO) {
        Long userId = mcsGameDTO.getCreatedBy();
        McsMerchant mcsMerchant = mcsMerchantDAO.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getUserId, userId));
        if (isNull(mcsMerchant)) {
            return R.fail("未查询到商家信息");
        }
        Integer type = mcsGameDTO.getType();
        McsGame mcsGame = new McsGame();
        if (type.equals(1)) {
            //1.戳戳币游戏
            Integer allocation = mcsGameDTO.getAllocation();
            Integer coins = mcsGameDTO.getCoins();
            if (isNull(allocation)) {
                return R.fail("缺少分配方式");
            }
            if (isNull(coins)) {
                return R.fail("未设置戳戳币总额");
            }
            mcsGame.setSurplusCoins(mcsGameDTO.getCoins());
        } else if (type.equals(2)) {
            //2.体验游戏
            Integer awardType = mcsGameDTO.getAwardType();
            if (isNull(awardType)) {
                return R.fail("缺少奖励类型");
            }
        } else {
            return R.fail("未知错误");
        }
        BeanUtils.copyProperties(mcsGameDTO, mcsGame);
        mcsGame.setSurplusCoupons(mcsGameDTO.getCoupons());
        mcsGame.setStatus(McsGame.Status.wfb);
        mcsGame.setMerchantId(mcsMerchant.getId());
        int num = this.baseMapper.insert(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("新增失败,请重新尝试");
    }
    /**
     * 编辑戳戳游戏
     * @param mcsGameDTO
     * @return
     */
    @Override
    public R putMcsGame(McsGameDTO mcsGameDTO) {
        McsGame mcsGame = this.baseMapper.selectById(mcsGameDTO.getId());
        if (isNull(mcsGame)) {
            return R.fail("资源不存在");
        }
        Integer type = mcsGameDTO.getType();
        if (type.equals(1)) {
            //1.戳戳币游戏
            Integer allocation = mcsGameDTO.getAllocation();
            Integer coins = mcsGameDTO.getCoins();
            if (isNull(allocation)) {
                return R.fail("缺少分配方式");
            }
            if (isNull(coins)) {
                return R.fail("未设置戳戳币总额");
            }
            Integer beforeCoins = mcsGame.getCoins();
            Integer beforeSurplusCoins = mcsGame.getSurplusCoins();
            Integer beforeUsedCoins = beforeCoins - beforeSurplusCoins;
            Integer surplusCoins = coins - beforeUsedCoins;
            if (surplusCoins < 0) {
                return R.fail("戳戳币总额少于已发出的数量");
            }
            mcsGame.setSurplusCoins(surplusCoins);
        } else if (type.equals(2)) {
            //2.体验游戏
            Integer awardType = mcsGameDTO.getAwardType();
            if (isNull(awardType)) {
                return R.fail("缺少奖励类型");
            }
        } else {
            return R.fail("未知错误");
        }
        Integer beforeCoupons = mcsGame.getCoupons();
        Integer beforeSurplusCoupons = mcsGame.getSurplusCoupons();
        Integer beforeUsedCoupons = beforeCoupons - beforeSurplusCoupons;
        BeanUtils.copyProperties(mcsGameDTO, mcsGame);
        Integer coupons = mcsGame.getCoupons();
        Integer surplusCoupons = coupons - beforeUsedCoupons;
        if (surplusCoupons < 0) {
            return R.fail("卷总数少于已发出的数量");
        }
        mcsGame.setSurplusCoupons(surplusCoupons);
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("编辑失败,请重新尝试");
    }
    /**
     * 发布戳戳游戏
     * @param gameId
     * @param userId
     * @return
     */
    @Override
    public R publishMcsGame(Long gameId, Long userId) {
        McsGame mcsGame = this.baseMapper.selectById(gameId);
        if (isNull(mcsGame)) {
            return R.fail("资源不存在");
        }
        McsMerchant mcsMerchant = mcsMerchantDAO.selectById(mcsGame.getMerchantId());
        Integer publishLimit = mcsMerchant.getPublishLimit();
        Integer publishCount = this.baseMapper.selectPublishCount(mcsGame.getMerchantId());
        if (publishCount >= publishLimit) {
            return R.fail("空闲戳戳点不足");
        }
        mcsGame.setStatus(McsGame.Status.jxz);
        mcsGame.setPublishAt(new Date());
        mcsGame.setUpdatedBy(userId);
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("发布失败,请重新尝试");
    }
    /**
     * 结束戳戳游戏
     * @param gameId
     * @param userId
     * @return
     */
    @Override
    public R finishMcsGame(Long gameId, Long userId) {
        McsGame mcsGame = this.baseMapper.selectById(gameId);
        if (isNull(mcsGame)) {
            return R.fail("资源不存在");
        }
        mcsGame.setStatus(McsGame.Status.yjs);
        mcsGame.setUpdatedBy(userId);
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("结束失败,请重新尝试");
    }
    /**
     * 戳戳游戏顶部统计数据
     * @param userId
     * @return
     */
    @Override
    public R getTopStatistics(Long userId) {
        McsMerchant mcsMerchant = mcsMerchantDAO.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getUserId, userId));
        if (isNull(mcsMerchant)) {
            return R.fail("未查询到商家信息");
        }
        TopStatisticsVO topStatisticsVO = new TopStatisticsVO();
        topStatisticsVO.setExpireAt(mcsMerchant.getExpireAt());
        Integer appliedCount = this.baseMapper.selectAppliedCount(mcsMerchant.getId());
        topStatisticsVO.setAppliedTotal(appliedCount);
        Integer verifiedCount = this.baseMapper.selectVerifiedCount(mcsMerchant.getId());
        topStatisticsVO.setVerifiedTotal(verifiedCount);
        Integer publishLimit = mcsMerchant.getPublishLimit();
        Integer publishCount = this.baseMapper.selectPublishCount(mcsMerchant.getId());
        Integer idleTotal = publishLimit - publishCount;
        topStatisticsVO.setIdleTotal(idleTotal > 0 ? idleTotal : 0);
        return R.ok(topStatisticsVO);
    }
}