无关风月
2024-09-14 3f481005be717250a2ea87ff9367aa84d6a3eb13
xinquan-modules/xinquan-user/src/main/java/com/xinquan/user/controller/client/ClientAppUserTreeController.java
@@ -1,9 +1,36 @@
package com.xinquan.user.controller.client;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.util.UUIDUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinquan.common.core.domain.R;
import com.xinquan.common.core.utils.page.CollUtils;
import com.xinquan.common.core.utils.page.PageDTO;
import com.xinquan.common.core.web.domain.BaseModel;
import com.xinquan.common.security.utils.SecurityUtils;
import com.xinquan.system.api.RemoteUserService;
import com.xinquan.system.api.domain.*;
import com.xinquan.system.api.domain.vo.AppUserEnergyRecordVO;
import com.xinquan.system.api.domain.vo.WateringVO;
import com.xinquan.user.domain.vo.EnergyVO;
import com.xinquan.user.domain.vo.ExchangeRecordVO;
import com.xinquan.user.service.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
/**
 * <p>
@@ -16,6 +43,392 @@
@RestController
@RequestMapping("/client/app-user-tree")
public class ClientAppUserTreeController {
    @Resource
    private AppUserTreeService appUserTreeService;
    @Resource
    private AppUserViewingHistoryService appUserViewingHistoryService;
    @Resource
    private AppUserService appUserService;
    @Resource
    private AppUserEnergyRecordService appUserEnergyRecordService;
    @Resource
    private PrizeService prizeService;
    @Resource
    private PrizeRedemptionRecordService prizeRedemptionRecordService;
    @PostMapping("/getUserTree")
    @ApiOperation(value = "获取用户树苗",tags = "树苗打卡站")
    public R<AppUserTree> getUserTree() {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        AppUser byId = appUserService.getById(userId);
        // 查询用户今日观看疗愈多少秒
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startOfDay = now.toLocalDate().atStartOfDay();
        LocalDateTime endOfDay = now.toLocalDate().atTime(LocalTime.MAX);
        List<AppUserViewingHistory> list = appUserViewingHistoryService
                .lambdaQuery().eq(AppUserViewingHistory::getAppUserId, userId)
                .eq(AppUserViewingHistory::getViewingType, 1)
                .eq(BaseModel::getDelFlag,0)
                .between(AppUserViewingHistory::getCreateTime, startOfDay,endOfDay).list();
        int temp = 0;
        for (AppUserViewingHistory appUserViewingHistory : list) {
            temp += appUserViewingHistory.getTimeLook();
        }
        AppUserTree one = appUserTreeService.lambdaQuery().eq(AppUserTree::getAppUserId, userId)
                .eq(AppUserTree::getSowAgain,2).one();
        if (one == null){
            AppUserTree appUserTree = new AppUserTree();
            appUserTree.setAppUserId(userId);
            appUserTree.setTreeLevelType(1);
            appUserTree.setGrowthValue(0);
            appUserTree.setSowAgain(2);
            appUserTree.setCreateTime(LocalDateTime.now());
            // 如果用户的签到时间是今天 那么修改为已签到
            if (byId.getSignTime().toLocalDate().equals(LocalDateTime.now().toLocalDate())){
                appUserTree.setIsSign(1);
            }else{
                appUserTree.setIsSign(2);
            }
            if (temp>=60){
                appUserTree.setTaskOne(1);
                appUserTree.setTaskTwo(1);
            }
            else if (temp>=30){
                appUserTree.setTaskOne(1);
                appUserTree.setTaskTwo(2);
            }else{
                appUserTree.setTaskOne(2);
                appUserTree.setTaskTwo(2);
            }
            appUserTree.setNextLevel(1000);
            appUserTreeService.save(appUserTree);
            return R.ok(appUserTree);
        }else{
            if (temp>=60){
                one.setTaskOne(1);
                one.setTaskTwo(1);
            }
            else if (temp>=30){
                one.setTaskOne(1);
                one.setTaskTwo(2);
            }else{
                one.setTaskOne(2);
                one.setTaskTwo(2);
            }
            // 如果用户的签到时间是今天 那么修改为已签到
            if (byId.getSignTime().toLocalDate().equals(LocalDateTime.now().toLocalDate())){
                one.setIsSign(1);
            }else{
                one.setIsSign(2);
            }
            switch (one.getTreeLevelType()){
                case 1:
                    one.setNextLevel(1000);
                    break;
                case 2:
                    one.setNextLevel(1000);
                    break;
                case 3:
                    one.setNextLevel(1000);
                    break;
                case 4:
                    one.setNextLevel(1000);
                    break;
                case 5:
                    one.setNextLevel(2000);
                    break;
                case 6:
                    one.setNextLevel(2000);
                    break;
                case 7:
                    one.setNextLevel(2000);
                    break;
                case 8:
                    one.setNextLevel(2000);
                    break;
                case 9:
                    one.setNextLevel(2400);
                    break;
                case 10:
                    one.setNextLevel(2400);
                    break;
            }
            return R.ok(one);
        }
    }
    @PostMapping("/restart")
    @ApiOperation(value = "重新播种",tags = "树苗打卡站")
    public R restart() {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        AppUserTree one = appUserTreeService.lambdaQuery().eq(AppUserTree::getAppUserId, userId)
                .eq(AppUserTree::getSowAgain,2).one();
        one.setSowAgain(1);
        appUserTreeService.updateById(one);
        AppUserTree appUserTree = new AppUserTree();
        appUserTree.setAppUserId(userId);
        appUserTree.setTreeLevelType(1);
        appUserTree.setGrowthValue(0);
        appUserTree.setSowAgain(2);
        appUserTree.setCreateTime(LocalDateTime.now());
        appUserTreeService.save(appUserTree);
        return R.ok();
    }
    @PostMapping("/watering")
    @ApiOperation(value = "浇水 返回值true证明升级了 false没升级",tags = "树苗打卡站")
    public R<WateringVO> watering() {
        WateringVO wateringVO = new WateringVO();
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        AppUser byId = appUserService.getById(userId);
        AppUserTree one = appUserTreeService.lambdaQuery().eq(AppUserTree::getAppUserId, userId)
                .eq(AppUserTree::getSowAgain,2).one();
        Integer currentEnergyValue = byId.getEnergyValue();
        byId.setEnergyValue(0);
        // 判断能否升级
        int temp = 0;
        switch (one.getTreeLevelType()){
            case 1:
                temp=1000;
                break;
            case 2:
                temp=1000;
                break;
            case 3:
                temp=1000;
                break;
            case 4:
                temp=1000;
                break;
            case 5:
                temp=2000;
                break;
            case 6:
                temp=2000;
                break;
            case 7:
                temp=2000;
                break;
            case 8:
                temp=2000;
                break;
            case 9:
                temp=2400;
                break;
            case 10:
                temp=2400;
                break;
        }
        wateringVO.setNextLevel(temp);
        // 升级阈值
        if (one.getGrowthValue()+currentEnergyValue>=temp){
            // 升级咯 如果当前等级已经为10级
            if (one.getTreeLevelType()==10){
                one.setGrowthValue(one.getGrowthValue()+currentEnergyValue);
                appUserTreeService.updateById(one);
                wateringVO.setIsNext(false);
                wateringVO.setNextLevel(2400);
            }else{
                // 升级
                one.setTreeLevelType(one.getTreeLevelType()+1);
                one.setGrowthValue(one.getGrowthValue()+currentEnergyValue-temp);
                appUserTreeService.updateById(one);
                switch (one.getTreeLevelType()+1){
                    case 2:
                        temp=1000;
                        break;
                    case 3:
                        temp=1000;
                        break;
                    case 4:
                        temp=1000;
                        break;
                    case 5:
                        temp=2000;
                        break;
                    case 6:
                        temp=2000;
                        break;
                    case 7:
                        temp=2000;
                        break;
                    case 8:
                        temp=2000;
                        break;
                    case 9:
                        temp=2400;
                        break;
                    case 10:
                        temp=2400;
                        break;
                }
                wateringVO.setNextLevel(temp);
                wateringVO.setIsNext(true);
            }
        }else{
            // 不能升级
            one.setGrowthValue(one.getGrowthValue()+currentEnergyValue-temp);
            appUserTreeService.updateById(one);
            wateringVO.setIsNext(false);
        }
        wateringVO.setGrowthValue(one.getGrowthValue());
        return R.ok(wateringVO);
    }
    @PostMapping("/sign")
    @ApiOperation(value = "签到",tags = "树苗打卡站")
    public R sign() {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        // 给用户加10能量
        AppUser byId = appUserService.getById(userId);
        byId.setTotalEnergyValue(byId.getTotalEnergyValue()+10);
        byId.setEnergyValue(byId.getEnergyValue()+10);
        appUserService.updateById(byId);
        return R.ok();
    }
    @PostMapping("/isFirst")
    @ApiOperation(value = "是否首次进入 ",tags = "树苗打卡站")
    public R isFirst() {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        AppUser byId = appUserService.getById(userId);
        if (byId.getIsFirst() == 1){
            byId.setIsFirst(2);
            appUserService.updateById(byId);
            return R.ok(true);
        }else{
            return R.ok(false);
        }
    }
    @PostMapping("/energyDetail")
    @ApiOperation(value = "能量值明细 ",tags = "树苗打卡站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "状态1全部 2增加 3扣除", dataType = "int", required = false),
            @ApiImplicitParam(name = "pageCurr", value = "分页参数,当前页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页参数,每页数量", dataType = "Integer", required = true)
    })
    public R<PageDTO<AppUserEnergyRecordVO>> energyDetail(Integer state,Integer pageCurr,Integer pageSize) {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        EnergyVO energyVO = new EnergyVO();
        energyVO.setEnergyTotal(appUserService.getById(userId).getTotalEnergyValue());
        LambdaQueryWrapper<AppUserEnergyRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (state!=null){
            switch (state){
                case 2:
                    lambdaQueryWrapper.eq(AppUserEnergyRecord::getChangeType, 1);
                    break;
                case 3:
                    lambdaQueryWrapper.eq(AppUserEnergyRecord::getChangeType, 2);
                    break;
            }
        }
        lambdaQueryWrapper.eq(AppUserEnergyRecord::getAppUserId, userId).orderByDesc(BaseModel::getCreateTime);
        Page<AppUserEnergyRecord> page = appUserEnergyRecordService.page(new Page<>(pageCurr, pageSize), lambdaQueryWrapper);
        if (CollUtils.isEmpty(page.getRecords())){
            PageDTO<AppUserEnergyRecordVO> empty = PageDTO.empty(page);
            energyVO.setList(new ArrayList<AppUserEnergyRecordVO>());
            return R.ok(empty);
        }
        PageDTO<AppUserEnergyRecordVO> appUserEnergyRecordVOPageDTO = PageDTO.of(page, AppUserEnergyRecordVO.class);
        energyVO.setList(appUserEnergyRecordVOPageDTO.getList());
        return R.ok(appUserEnergyRecordVOPageDTO);
    }
    @PostMapping("/goodsList")
    @ApiOperation(value = "能量兑换礼物列表 ",tags = "树苗打卡站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageCurr", value = "分页参数,当前页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页参数,每页数量", dataType = "Integer", required = true)
    })
    public R<PageDTO<Prize>> goodsList(Integer pageCurr,Integer pageSize) {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        Page<Prize> page = prizeService.lambdaQuery()
                .eq(Prize::getStatus, 1)
                .orderByDesc(BaseModel::getCreateTime).page(new Page<>(pageCurr, pageSize));
        if (CollUtils.isEmpty(page.getRecords())){
            PageDTO<Prize> empty = PageDTO.empty(page);
            return R.ok(empty);
        }
        for (Prize record : page.getRecords()) {
            PrizeRedemptionRecord one = prizeRedemptionRecordService.lambdaQuery()
                    .eq(PrizeRedemptionRecord::getPrizeId, record.getId())
                    .eq(PrizeRedemptionRecord::getAppUserId, userId).one();
            if (one == null){
                record.setReceiveStatus(2);
            }
        }
        PageDTO<Prize> res = PageDTO.of(page, Prize.class);
        return R.ok(res);
    }
    @PostMapping("/exchange")
    @ApiOperation(value = "兑换奖品 ",tags = "树苗打卡站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prizeId", value = "奖品id", dataType = "Long", required = true),
    })
    public R exchange(Long prizeId) {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        AppUser byId1 = appUserService.getById(userId);
        Prize byId = prizeService.getById(prizeId);
        if (byId1.getTotalEnergyValue()<=byId.getEnergyValue()){
            return R.fail("能量值不足");
        }
        PrizeRedemptionRecord one = prizeRedemptionRecordService.lambdaQuery()
                .eq(PrizeRedemptionRecord::getPrizeId, prizeId)
                .eq(PrizeRedemptionRecord::getAppUserId, userId).one();
        if (one!=null){
            return R.fail("奖品已兑换");
        }
        PrizeRedemptionRecord prizeRedemptionRecord = new PrizeRedemptionRecord();
        prizeRedemptionRecord.setStatus(1);
        prizeRedemptionRecord.setPrizeId(prizeId);
        prizeRedemptionRecord.setAppUserId(userId);
        prizeRedemptionRecord.setCreateTime(LocalDateTime.now());
        String code = RandomUtil.randomNumbers(6);
        prizeRedemptionRecord.setCode(code);
        prizeRedemptionRecordService.save(prizeRedemptionRecord);
        // 随机生成6位纯数字验证码
        return R.ok();
    }
    @PostMapping("/exchangeRecordList")
    @ApiOperation(value = "兑换记录 ",tags = "树苗打卡站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageCurr", value = "分页参数,当前页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页参数,每页数量", dataType = "Integer", required = true)
    })
    public R<PageDTO<ExchangeRecordVO>> exchangeRecordList(Integer pageCurr, Integer pageSize) {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        Page<PrizeRedemptionRecord> page = prizeRedemptionRecordService.lambdaQuery()
                .eq(PrizeRedemptionRecord::getAppUserId, userId)
                .orderByDesc(BaseModel::getCreateTime).page(new Page<>(pageCurr, pageSize));
        if (CollUtils.isEmpty(page.getRecords())){
            PageDTO<ExchangeRecordVO> empty = PageDTO.empty(page);
            return R.ok(empty);
        }
        PageDTO<ExchangeRecordVO> res = PageDTO.of(page, ExchangeRecordVO.class);
        for (ExchangeRecordVO record : res.getList()) {
            Prize temp = prizeService.lambdaQuery()
                    .eq(Prize::getId, record.getId()).one();
            if (temp != null){
                record.setName(temp.getName());
                record.setCoverUrl(temp.getCoverUrl());
                record.setWorth(temp.getWorth());
                record.setEnergyValue(temp.getEnergyValue());
            }
        }
        return R.ok(res);
    }
}