admin
2022-04-26 36681e15e12aaa9135f69260472de65303cdcba3
app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornMakeServiceImpl.java
@@ -1,10 +1,9 @@
package com.yeshi.makemoney.app.service.impl.goldcorn;
import com.ks.lib.common.exception.ParamsException;
import com.yeshi.makemoney.app.entity.config.SystemConfigKey;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetFrequencyConfig;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetPrice;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetRecord;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetType;
import com.yeshi.makemoney.app.entity.goldcorn.*;
import com.yeshi.makemoney.app.entity.money.UserMoneyRecord;
import com.yeshi.makemoney.app.entity.user.UserInfo;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetFrequencyConfigException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetPriceException;
@@ -16,11 +15,16 @@
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetPriceService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetRecordService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornMakeService;
import com.yeshi.makemoney.app.service.inter.money.UserMoneyRecordService;
import com.yeshi.makemoney.app.service.inter.team.TeamInviteRelationService;
import com.yeshi.makemoney.app.service.inter.user.UserInfoService;
import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornGetRecordQuery;
import com.yeshi.makemoney.app.utils.factory.UserMoneyRecordFactory;
import com.yeshi.makemoney.app.utils.factory.goldcorn.GoldCornConsumeRecordFactory;
import com.yeshi.makemoney.app.utils.goldcorn.GoldCornUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.TimeUtil;
import org.yeshi.utils.annotation.RequestSerializableByKey;
@@ -29,6 +33,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * @author hxh
@@ -46,7 +51,7 @@
    private GoldCornGetRecordService goldCornGetRecordService;
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private GoldCornGetPriceService goldCornGetPriceService;
@@ -59,6 +64,9 @@
    @Resource
    private SystemConfigService systemConfigService;
    @Resource
    private UserMoneyRecordService userMoneyRecordService;
    private void frequencyVerify(UserInfo user, GoldCornGetType type, Date time) throws GoldCornGetFrequencyConfigException, GoldCornMakeException {
@@ -115,7 +123,7 @@
    @RequestSerializableByKey(key = "'addgoldcorn-'+#uid+'-'+#type")
    @Override
    public void addGoldCorn(Long uid, GoldCornGetType type, Date currentTime) throws UserInfoException, GoldCornGetPriceException, GoldCornGetFrequencyConfigException, GoldCornMakeException {
    public GoldCornGetRecord addGoldCorn(Long uid, GoldCornGetType type, boolean isDouble, Date currentTime, Integer price) throws UserInfoException, GoldCornGetFrequencyConfigException, GoldCornMakeException {
        if (uid == null || type == null || currentTime == null) {
            throw new GoldCornMakeException(GoldCornMakeException.CODE_PARAMS_NOT_ENOUGH, "参数不完整");
        }
@@ -125,65 +133,221 @@
        }
        //判断是否超出了次数限制
        frequencyVerify(user, type, currentTime);
        //获取价格
        GoldCornGetPrice price = goldCornGetPriceService.getPrice(user.getSystem(), type, currentTime);
        if (price == null) {
            throw new GoldCornGetPriceException(GoldCornGetPriceException.CODE_NOT_EXIST, "价格信息缺失");
        }
        GoldCornGetPrice getPrice = goldCornGetPriceService.getPrice(user.getSystem(), GoldCornGetType.watchVideo, currentTime);
        //单价由外面传入
        int baseCornNum = price;
        GoldCornGetRecord record = new GoldCornGetRecord();
        record.setCornNum(price.getCornNum());
        record.setCornNum(baseCornNum);
        record.setDay(TimeUtil.getGernalTime(currentTime.getTime(), "yyyy-MM-dd"));
        record.setType(type);
        record.setUid(uid);
        record.setLevel(GoldCornGetRecord.LEVEL_OWN);
        record.setCreateTime(currentTime);
        GoldCornGetRecord recordD = null;
        if (isDouble) {
            recordD = new GoldCornGetRecord();
            recordD.setCornNum(baseCornNum);
            recordD.setDay(TimeUtil.getGernalTime(currentTime.getTime(), "yyyy-MM-dd"));
            recordD.setType(type);
            recordD.setUid(uid);
            recordD.setLevel(GoldCornGetRecord.LEVEL_OWN);
            recordD.setCreateTime(currentTime);
            recordD.setDubble(true);
        }
        //允许上级提成
        try {
            goldCornGetRecordService.add(record);
            if (recordD != null) {
                goldCornGetRecordService.add(recordD);
            }
            //是否有上级
            Long bossUid = teamInviteRelationService.getBossUid(uid);
            if (bossUid != null) {
                BigDecimal rate = new BigDecimal(systemConfigService.getValueCache(user.getSystem(), SystemConfigKey.teamFirstDivideRate));
                GoldCornGetRecord cRecord = new GoldCornGetRecord();
                cRecord.setCornNum(price.getCornNum());
                cRecord.setDay(record.getDay());
                cRecord.setType(type);
                cRecord.setUid(bossUid);
                cRecord.setCornNum(new BigDecimal(record.getCornNum()).multiply(rate).intValue());
                cRecord.setFromId(record.getId());
                cRecord.setFromUid(record.getUid());
                cRecord.setLevel(GoldCornGetRecord.LEVEL_FIRST);
                cRecord.setCreateTime(currentTime);
                if (cRecord.getCornNum() > 0) {
                    goldCornGetRecordService.add(cRecord);
                }
                //是否有上上级
                bossUid = teamInviteRelationService.getBossUid(bossUid);
            if (getPrice.getTeamDivide() != null && getPrice.getTeamDivide()) {
                Long bossUid = teamInviteRelationService.getBossUid(user.getId());
                if (bossUid != null) {
                    rate = new BigDecimal(systemConfigService.getValueCache(user.getSystem(), SystemConfigKey.teamSecondDivideRate));
                    GoldCornGetRecord ccRecord = new GoldCornGetRecord();
                    ccRecord.setCornNum(price.getCornNum());
                    ccRecord.setDay(record.getDay());
                    ccRecord.setType(type);
                    ccRecord.setUid(bossUid);
                    ccRecord.setFromId(record.getId());
                    ccRecord.setFromUid(record.getUid());
                    ccRecord.setCornNum(new BigDecimal(record.getCornNum()).multiply(rate).intValue());
                    ccRecord.setLevel(GoldCornGetRecord.LEVEL_SECOND);
                    ccRecord.setCreateTime(currentTime);
                    if (ccRecord.getCornNum() > 0) {
                        goldCornGetRecordService.add(ccRecord);
                    BigDecimal rate = new BigDecimal(systemConfigService.getValueCache(user.getSystem(), SystemConfigKey.teamFirstDivideRate));
                    GoldCornGetRecord cRecord = new GoldCornGetRecord();
                    cRecord.setDay(record.getDay());
                    cRecord.setType(type);
                    cRecord.setUid(bossUid);
                    cRecord.setCornNum(new BigDecimal(baseCornNum).multiply(rate).intValue());
                    cRecord.setFromId(record.getId());
                    cRecord.setFromUid(record.getUid());
                    cRecord.setLevel(GoldCornGetRecord.LEVEL_FIRST);
                    cRecord.setCreateTime(currentTime);
                    if (cRecord.getCornNum() > 0) {
                        goldCornGetRecordService.add(cRecord);
                    }
                    //是否有上上级
//                    bossUid = teamInviteRelationService.getBossUid(bossUid);
//                    if (bossUid != null) {
//                        rate = new BigDecimal(systemConfigService.getValueCache(user.getSystem(), SystemConfigKey.teamSecondDivideRate));
//                        GoldCornGetRecord ccRecord = new GoldCornGetRecord();
//                        ccRecord.setDay(record.getDay());
//                        ccRecord.setType(type);
//                        ccRecord.setUid(bossUid);
//                        ccRecord.setFromId(record.getId());
//                        ccRecord.setFromUid(record.getUid());
//                        ccRecord.setCornNum(new BigDecimal(baseCornNum).multiply(rate).intValue());
//                        ccRecord.setLevel(GoldCornGetRecord.LEVEL_SECOND);
//                        ccRecord.setCreateTime(currentTime);
//                        if (ccRecord.getCornNum() > 0) {
//                            goldCornGetRecordService.add(ccRecord);
//                        }
//                    }
                }
            }
        } catch (GoldCornGetRecordException e) {
            throw new GoldCornMakeException(GoldCornMakeException.CODE_ADD_RECORD_ERROR, e.getMsg());
        }
        return record;
    }
    @Override
    public Long getGoldCornByDay(Long uid, String day) {
    public Long getGoldCornByDay(Long uid, Date day) {
        //TODO redis缓存
        return goldCornGetRecordService.getGoldCornByDay(uid, day);
    }
    @Override
    public Long getExchangingGoldCorn(Long uid, int dayCount) {
        long goldCornNum = 0;
        long now = System.currentTimeMillis();
        //查询最近3天未兑换的金币数量,
        for (int i = 1; i <= dayCount; i++) {
            Date date = new Date(now - 1000 * 60 * 60 * 24L * i);
            long count = getGoldCornByDay(uid, date);
            if (count == 0L) {
                continue;
            }
            //获取兑换记录的主键ID
            GoldCornConsumeRecord record = GoldCornConsumeRecordFactory.createExchange(uid, GoldCornUtil.getFormatDay(date), 0, new BigDecimal(0.1));
            //查询资金记录是否有该记录
            try {
                UserMoneyRecord userMoneyRecord = UserMoneyRecordFactory.createGoldCornExchange(record);
                //查询记录
                userMoneyRecord = userMoneyRecordService.selectBySerialNo(userMoneyRecord.getSerialNo());
                if (userMoneyRecord == null) {
                    //未兑换
                    goldCornNum += count;
                }
            } catch (ParamsException e) {
                e.printStackTrace();
            }
        }
        return goldCornNum;
    }
    @Override
    public int getContinueSignDay(Long uid) {
        Date now = new Date();
        //查询今日是否已经签到
        boolean isSignIn = isSignIn(uid, now);
        Date deadDate = now;
        if (!isSignIn) {
            deadDate = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now.getTime(), "yyyyMMdd"), "yyyyMMdd"));
        }
        //今日未签到的,查询连续签到次数应该从前一天开始计算
        int continueDay = goldCornGetRecordService.getContinueDay(uid, GoldCornGetType.signIn, deadDate);
        return continueDay;
    }
    @Override
    public boolean isSignIn(Long uid, Date date) {
        GoldCornGetRecordQuery query = new GoldCornGetRecordQuery();
        query.setUid(uid);
        query.setType(GoldCornGetType.signIn);
        query.setDay(GoldCornUtil.getFormatDay(date));
        long count = goldCornGetRecordService.count(query);
        return count > 0L;
    }
    @Override
    public GoldCornGetRecord signIn(Long uid) throws GoldCornGetFrequencyConfigException, UserInfoException, GoldCornGetPriceException, GoldCornMakeException {
        UserInfo user = userInfoService.get(uid);
        //获取连续签到次数
        Integer price = goldCornGetPriceService.getSingInPrice(user.getSystem(), getContinueSignDay(uid) + 1);
        return addGoldCorn(uid, GoldCornGetType.signIn, false, new Date(), price);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer watchVideo(Long uid, boolean isDouble, Date time, int timeSeconds) throws GoldCornGetPriceException, UserInfoException, GoldCornMakeException, GoldCornGetFrequencyConfigException {
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            throw new UserInfoException(UserInfoException.CODE_NOT_EXIST, "用户不存在");
        }
        Integer price = goldCornGetPriceService.getWatchVideoPrice(user, time, timeSeconds);
        //价格为0时不加入记录
        if (price == 0) {
            return 0;
        }
        GoldCornGetRecord record = addGoldCorn(uid, GoldCornGetType.watchVideo, isDouble, time, price);
        return record.getCornNum() * (isDouble ? 2 : 1);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer readNovel(Long uid, boolean isDouble, Date time, int timeSeconds) throws GoldCornGetPriceException, UserInfoException, GoldCornMakeException, GoldCornGetFrequencyConfigException {
        String readNovelCacheKey = "task-n-" + uid + "-" + TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMdd");
        Integer readNovelNowSeconds = timeSeconds;
        Integer start = (Integer) redisTemplate.opsForValue().get(readNovelCacheKey);
        if (start == null) {
            start = 0;
        }
        timeSeconds = timeSeconds - start;
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            throw new UserInfoException(UserInfoException.CODE_NOT_EXIST, "用户不存在");
        }
        Integer price = goldCornGetPriceService.getReadNovelPrice(user, time, timeSeconds);
        //价格为0时不加入记录
        if (price == 0) {
            return 0;
        }
        GoldCornGetRecord record = addGoldCorn(uid, GoldCornGetType.readNovel, isDouble, time, price);
        //记录当天的实时阅读分钟数
        redisTemplate.opsForValue().set(readNovelCacheKey, readNovelNowSeconds, 60 * 60 * 24, TimeUnit.SECONDS);
        return record.getCornNum() * (isDouble ? 2 : 1);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer scanNews(Long uid, boolean isDouble, Date time, int num) throws GoldCornGetPriceException, UserInfoException, GoldCornMakeException, GoldCornGetFrequencyConfigException {
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            throw new UserInfoException(UserInfoException.CODE_NOT_EXIST, "用户不存在");
        }
        Integer price = goldCornGetPriceService.getScanNewsPrice(user, time, num);
        //价格为0时不加入记录
        if (price == 0) {
            return 0;
        }
        GoldCornGetRecord record = addGoldCorn(uid, GoldCornGetType.scanNews, isDouble, time, price);
        return record.getCornNum() * (isDouble ? 2 : 1);
    }
}