puhanshu
2022-01-08 262cdc113a44bbd51f98a56ce91966dc7b2ea938
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsGameServiceImpl.java
@@ -2,11 +2,26 @@
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.panzhihua.common.model.vos.community.microCommercialStreet.TopStatisticsVO;
import com.panzhihua.common.utlis.DateUtils;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsCouponVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsEvaluateVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsMerchantVO;
import com.panzhihua.common.utlis.QRCodeUtils;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.service_community.dao.McsEvaluateDAO;
import com.panzhihua.service_community.entity.McsCoupon;
import com.panzhihua.service_community.entity.McsEvaluate;
import com.panzhihua.service_community.util.NearbyUtil;
import com.panzhihua.service_community.util.QRCodeUtil;
import com.spatial4j.core.shape.Rectangle;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
@@ -20,13 +35,15 @@
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.common.model.vos.community.microCommercialStreet.TopStatisticsVO;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.service_community.dao.McsCouponDAO;
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;
import org.springframework.transaction.annotation.Transactional;
/**
 * (McsGame)表服务实现类
@@ -39,6 +56,10 @@
    @Resource
    private McsMerchantDAO mcsMerchantDAO;
    @Resource
    private McsCouponDAO mcsCouponDAO;
    @Resource
    private McsEvaluateDAO mcsEvaluateDAO;
    /**
     * 分页查询戳戳游戏
@@ -60,6 +81,7 @@
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R setPopularForGame(SetPopularForGameDTO setPopularForGameDTO) {
        McsGame mcsGame = this.baseMapper.selectById(setPopularForGameDTO.getGameId());
        if (isNull(mcsGame)) {
@@ -68,6 +90,11 @@
        Integer type = setPopularForGameDTO.getType();
        if (type.equals(1)) {
            //设为热门
            List<McsGame> popularGames = this.baseMapper.selectList(new QueryWrapper<McsGame>().lambda().eq(McsGame::getIsPopular, true));
            if (nonNull(popularGames) && !popularGames.isEmpty()) {
                popularGames.forEach(e -> e.setIsPopular(false));
                this.updateBatchById(popularGames);
            }
            mcsGame.setIsPopular(true);
        } else if (type.equals(2)) {
            //取消热门
@@ -160,9 +187,11 @@
                return R.fail("缺少分配方式");
            }
            if (isNull(coins)) {
                return R.fail("未设置戳戳币总额");
                return R.fail("未设置戳戳币额度");
            }
            mcsGame.setSurplusCoins(mcsGameDTO.getCoins());
            if (allocation.equals(1)) {
                mcsGame.setSurplusCoins(mcsGameDTO.getCoins());
            }
        } else if (type.equals(2)) {
            //2.体验游戏
            Integer awardType = mcsGameDTO.getAwardType();
@@ -203,7 +232,7 @@
                return R.fail("缺少分配方式");
            }
            if (isNull(coins)) {
                return R.fail("未设置戳戳币总额");
                return R.fail("未设置戳戳币额度");
            }
            Integer beforeCoins = mcsGame.getCoins();
            Integer beforeSurplusCoins = mcsGame.getSurplusCoins();
@@ -316,4 +345,138 @@
        topStatisticsVO.setIdleTotal(idleTotal > 0 ? idleTotal : 0);
        return R.ok(topStatisticsVO);
    }
    /**
     * 戳戳卷领取
     * @param gameId
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R applyMcsCoupon(Long gameId, Long userId) {
        McsGame mcsGame = this.baseMapper.selectById(gameId);
        if (isNull(mcsGame) || mcsGame.getIsDel()) {
            return R.fail("游戏已下架");
        }
        Integer userAppliedCount = mcsCouponDAO.selectCount(new QueryWrapper<McsCoupon>().lambda().eq(McsCoupon::getGameId, gameId).eq(McsCoupon::getUserId, userId));
        if (userAppliedCount > 0) {
            return R.fail("请勿重复领取");
        }
        Integer surplusCoupons = mcsGame.getSurplusCoupons();
        if (surplusCoupons <= 0) {
            return R.fail("抢光了");
        }
        McsCoupon mcsCoupon = new McsCoupon();
        Long snowflakeId = Snowflake.getId();
        mcsCoupon.setId(snowflakeId);
        mcsCoupon.setGameId(gameId);
        mcsCoupon.setUserId(userId);
        mcsCoupon.setQrCode(QRCodeUtil.getBase64QRCode(snowflakeId.toString()));
        Integer type = mcsGame.getType();
        if (type.equals(1)) {
            //戳戳币游戏
            Integer allocation = mcsGame.getAllocation();
            if (allocation.equals(1)) {
                //随机分配
            } else {
                //固定值
                mcsCoupon.setCoin(mcsGame.getCoins());
                mcsCoupon.setAward("戳戳币:" + mcsGame.getCoins());
            }
        } else {
            //体验游戏
            Integer awardType = mcsGame.getAwardType();
            if (awardType.equals(1)) {
                mcsCoupon.setAward("免费产品");
            } else {
                mcsCoupon.setAward("免费服务");
            }
        }
        int num = mcsCouponDAO.insert(mcsCoupon);
        if (num > 0) {
            mcsGame.setSurplusCoupons(surplusCoupons - 1);
            this.baseMapper.updateById(mcsGame);
            McsCouponVO mcsCouponVO = new McsCouponVO();
            mcsCouponVO.setId(snowflakeId);
            mcsCouponVO.setIsVerified(false);
            mcsCouponVO.setQrCode(mcsCoupon.getQrCode());
            mcsCouponVO.setAward(mcsCoupon.getAward());
            return R.ok(mcsCouponVO);
        }
        return R.fail("领取失败,请重试");
    }
    /**
     * h5分页查询戳戳游戏
     * @param pageMcsGameDTO
     * @return
     */
    @Override
    public R pageH5McsGame(PageMcsGameDTO pageMcsGameDTO) {
        Page page = new Page<>();
        page.setSize(pageMcsGameDTO.getPageSize());
        page.setCurrent(pageMcsGameDTO.getPageNum());
        String lat = pageMcsGameDTO.getLat();
        String lon = pageMcsGameDTO.getLon();
        Integer distance = pageMcsGameDTO.getDistance();
        IPage<McsGameVO> mcsGames;
        if (isNotEmpty(lat) && isNotEmpty(lon) && nonNull(distance)) {
            Rectangle rectangle = new NearbyUtil().getRectangle(pageMcsGameDTO.getDistance(),
                    Double.parseDouble(pageMcsGameDTO.getLon()), Double.parseDouble(pageMcsGameDTO.getLat()));
            mcsGames = this.baseMapper.pageH5McsGame(page, rectangle.getMinX(), rectangle.getMaxX(),
                    rectangle.getMinY(), rectangle.getMaxY(), pageMcsGameDTO);
        } else {
            mcsGames = this.baseMapper.pageH5McsGame(page, null, null, null, null, pageMcsGameDTO);
        }
        return R.ok(mcsGames);
    }
    /**
     * 游戏详情
     * @param gameId
     * @return
     */
    @Override
    public R getMcsGame(Long gameId) {
        McsGameVO mcsGameVO = this.baseMapper.selectDetailById(gameId);
        Integer gameCount = this.baseMapper.selectCount(new QueryWrapper<McsGame>().lambda()
                .eq(McsGame::getMerchantId, mcsGameVO.getMerchantId()).eq(McsGame::getIsDel, false));
        mcsGameVO.setGameCount(gameCount);
        return R.ok(mcsGameVO);
    }
    public static void main(String[] args) {
        int total = 300;
        int people = 10;
        dispath(total, people, 30);
    }
    public static void dispath(int total, int people, int min) {
        for (int i = 0; i < people - 1; i++) {
            int leftPeople = people - i;
            double avg = Double.valueOf(total) / leftPeople;
            double ratio1 = RandomUtils.nextInt(0, 3);
            double ratio2 = RandomUtils.nextInt(0, 1);
            double ratio = ratio1 + ratio2;
            int cur = (int) Math.floor(ratio * avg) > min ? (int) Math.floor(ratio * avg) : min;
            // 扣减总额阅点数目
            total = total - cur;
            System.out.format("第 %d 个红包: %d 阅点,剩下: %d 阅点\n", i + 1, cur,total);
        }
        // 剩余的就是最后一个用户的阅点额度
        System.out.format("第 %d 个红包: %d 阅点,剩下: 0 阅点\n", people, total);
    }
}