huanghongfa
2021-07-06 236325458d40ce680bc681bc995e16d8cecce21d
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActUserWalletServiceImpl.java
@@ -1,12 +1,17 @@
package com.panzhihua.service_community.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.constants.Constants;
import com.panzhihua.common.model.dtos.community.wallet.ComActWalletDetailDTO;
import com.panzhihua.common.model.dtos.community.wallet.ComActWalletSettlementAdminDTO;
import com.panzhihua.common.model.dtos.community.wallet.PageComActWalletAdminDTO;
import com.panzhihua.common.model.dtos.community.wallet.PageComActWalletTradeDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.wallet.ComActWalletVO;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.service_community.dao.ComActEasyPhotoActivityMapper;
import com.panzhihua.service_community.dao.ComActEasyPhotoDAO;
import com.panzhihua.service_community.dao.ComActUserWalletMapper;
@@ -20,6 +25,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
@@ -73,7 +79,10 @@
        Map<String,String> resultMap = this.baseMapper.getCommunityName(walletDetailDTO.getCommunityId());
        if(!resultMap.isEmpty()){
            comActWalletVO.setCommunityName(resultMap.get("name"));
            comActWalletVO.setAgreement(resultMap.get("content"));
            if(StringUtils.isEmpty(resultMap.get("content"))){
                this.baseMapper.insertSysAgreement(Constants.PROFIT_EXPLAIN,walletDetailDTO.getCommunityId());
            }
            comActWalletVO.setAgreement(Constants.PROFIT_EXPLAIN);
        }
        return R.ok(comActWalletVO);
    }
@@ -119,9 +128,14 @@
            return;
        }
        Date nowDate = new Date();
        //活动正在进行中,需要给用户计算收益
        if(nowDate.getTime() - photoActivityDO.getActivityStartAt().getTime() > 0 && nowDate.getTime() - photoActivityDO.getActivityEndAt().getTime() < 0){
            photoActivityDO.setCount(photoActivityDO.getCount() + 1);
            comActEasyPhotoActivityMapper.updateById(photoActivityDO);
            //查询用户钱包
            ComActUserWalletDO userWalletDO = comActUserWalletMapper.selectOne(new QueryWrapper<ComActUserWalletDO>()
                    .lambda().eq(ComActUserWalletDO::getUserId,easyPhotoDO.getSponsorId()).eq(ComActUserWalletDO::getCommunityId,easyPhotoDO.getCommunityId()));
@@ -147,31 +161,31 @@
                profitAmount = photoActivityDO.getOrdinaryReward();
            }
            if(profitAmount.equals(BigDecimal.ZERO)){
            if(profitAmount.compareTo(BigDecimal.ZERO) == 0){
                log.error("此次参加活动的随手拍收益为0,不记录本次交易");
                return;
            }else{
                //记录钱包变动前金额
                BigDecimal oldIncomeAmount = userWalletDO.getIncomeAmount();
                BigDecimal oldAvailableAmount = userWalletDO.getAvailableAmount();
                BigDecimal oldSettlementAmount = userWalletDO.getSettlementAmount();
                //更新钱包金额
                userWalletDO.setEasyCount(userWalletDO.getEasyCount() + 1);
                userWalletDO.setIncomeAmount(userWalletDO.getIncomeAmount().add(profitAmount));
                userWalletDO.setAvailableAmount(userWalletDO.getAvailableAmount().add(profitAmount));
                comActUserWalletMapper.updateById(userWalletDO);
                //新增钱包资金交易记录
                Long tradeId = comActUserWalletTradeService.addWalletTrade(easyPhotoDO.getSponsorId(),easyPhotoDO.getCommunityId()
                        ,easyPhotoDO.getId(),profitAmount, ComActUserWalletTradeDO.type.fb,"发布随手拍"
                        ,userId,userWalletDO.getId(),ComActUserWalletTradeDO.changeType.add);
                //新增钱包资金变动记录
                comActUserWalletChangeService.addWalletChange(easyPhotoDO.getSponsorId(),easyPhotoDO.getCommunityId(),userWalletDO.getId()
                        ,oldIncomeAmount,userWalletDO.getIncomeAmount(),oldAvailableAmount,userWalletDO.getAvailableAmount()
                        ,oldSettlementAmount,userWalletDO.getSettlementAmount(),tradeId);
            }
            //记录钱包变动前金额
            BigDecimal oldIncomeAmount = userWalletDO.getIncomeAmount();
            BigDecimal oldAvailableAmount = userWalletDO.getAvailableAmount();
            BigDecimal oldSettlementAmount = userWalletDO.getSettlementAmount();
            //更新钱包金额
            userWalletDO.setIncomeAmount(userWalletDO.getIncomeAmount().add(profitAmount));
            userWalletDO.setAvailableAmount(userWalletDO.getAvailableAmount().add(profitAmount));
            comActUserWalletMapper.updateById(userWalletDO);
            //新增钱包资金交易记录
            Long tradeId = comActUserWalletTradeService.addWalletTrade(easyPhotoDO.getSponsorId(),easyPhotoDO.getCommunityId()
                    ,easyPhotoDO.getId(),profitAmount, ComActUserWalletTradeDO.type.fb,"发布随手拍"
                    ,userId,userWalletDO.getId(),ComActUserWalletTradeDO.changeType.add);
            //新增钱包资金变动记录
            comActUserWalletChangeService.addWalletChange(easyPhotoDO.getSponsorId(),easyPhotoDO.getCommunityId(),userWalletDO.getId()
                    ,oldIncomeAmount,userWalletDO.getIncomeAmount(),oldAvailableAmount,userWalletDO.getAvailableAmount()
                    ,oldSettlementAmount,userWalletDO.getSettlementAmount(),tradeId);
            //更新随手拍活动信息
            easyPhotoDO.setActivityType(activityType);
            easyPhotoDO.setActivityAmount(profitAmount);
@@ -179,4 +193,54 @@
        }
    }
    /**
     * 分页查询收益结算列表
     * @param pageWalletAdminDTO    请求参数
     * @return  收益结算列表
     */
    @Override
    public R pageUserWalletAdmin(PageComActWalletAdminDTO pageWalletAdminDTO){
        return R.ok(this.baseMapper.getUserWalletList(new Page(pageWalletAdminDTO.getPageNum(),pageWalletAdminDTO.getPageSize()),pageWalletAdminDTO));
    }
    /**
     * 用户结算收益
     * @param settlementAdminDTO    请求参数
     * @return  结算结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R userWalletSettlementAdmin(ComActWalletSettlementAdminDTO settlementAdminDTO){
        //查询用户钱包
        ComActUserWalletDO userWalletDO = this.baseMapper.selectById(settlementAdminDTO.getId());
        if(userWalletDO == null){
            return R.fail("未查询到用户钱包");
        }
        if(userWalletDO.getAvailableAmount().compareTo(settlementAdminDTO.getSettlementAmount()) < 0){
            return R.fail("结算金额不能高于钱包可结算金额");
        }
        try {
            BigDecimal oldIncomeAmount = userWalletDO.getIncomeAmount();
            BigDecimal oldAvailableAmount = userWalletDO.getAvailableAmount();
            BigDecimal oldSettlementAmount = userWalletDO.getSettlementAmount();
            userWalletDO.setAvailableAmount(userWalletDO.getAvailableAmount().subtract(settlementAdminDTO.getSettlementAmount()));
            userWalletDO.setSettlementAmount(userWalletDO.getSettlementAmount().add(settlementAdminDTO.getSettlementAmount()));
            userWalletDO.setUpdateAt(new Date());
            this.baseMapper.updateById(userWalletDO);
            //添加钱包交易记录
            Long tradeServiceId = comActUserWalletTradeService.addWalletTrade(userWalletDO.getUserId(),userWalletDO.getCommunityId()
                    ,null,settlementAdminDTO.getSettlementAmount(),ComActUserWalletTradeDO.type.js
                    ,settlementAdminDTO.getRemark(),settlementAdminDTO.getUserId(),userWalletDO.getId(),ComActUserWalletTradeDO.changeType.reduce);
            //添加钱包变动记录
            comActUserWalletChangeService.addWalletChange(userWalletDO.getUserId(),userWalletDO.getCommunityId(),userWalletDO.getId()
                    ,oldIncomeAmount,userWalletDO.getIncomeAmount(),oldAvailableAmount,userWalletDO.getAvailableAmount()
                    ,oldSettlementAmount,userWalletDO.getSettlementAmount(),tradeServiceId);
            return R.ok();
        }catch (Exception e){
            log.error("给用户结算收益失败,原因:" + e.getMessage());
            return R.fail("结算失败");
        }
    }
}