AccountManager.java 9.8 KB
Newer Older
licc's avatar
licc committed
1
package cn.wisenergy.service.Manager;
licc's avatar
licc committed
2

licc's avatar
licc committed
3 4
import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.mapper.*;
licc's avatar
licc committed
5
import cn.wisenergy.model.app.*;
licc's avatar
licc committed
6
import cn.wisenergy.model.enums.RebateStatusEnum;
licc's avatar
licc committed
7
import cn.wisenergy.model.enums.TradeRecordEnum;
8
import cn.wisenergy.model.enums.TradeStatusEnum;
licc's avatar
licc committed
9
import cn.wisenergy.model.vo.TeamPerformanceSortVo;
licc's avatar
licc committed
10 11 12 13
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
licc's avatar
licc committed
14
import org.springframework.util.CollectionUtils;
licc's avatar
licc committed
15 16

import java.math.BigDecimal;
licc's avatar
licc committed
17
import java.math.RoundingMode;
licc's avatar
licc committed
18
import java.util.*;
licc's avatar
licc committed
19
import java.util.stream.Collectors;
licc's avatar
licc committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

/**
 * @author 86187
 * @ Description: 账户管理公共类
 * @ Author     : 86187
 * @ Date       : 2021/2/23 10:43
 */
@Component
@Slf4j
public class AccountManager {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private TradeRecordMapper recordMapper;

licc's avatar
licc committed
40 41 42
    @Autowired
    private TeamPerformanceMapper teamPerformanceMapper;

licc's avatar
licc committed
43 44 45
    @Autowired
    private ProgressPrizeMapper progressPrizeMapper;

licc's avatar
licc committed
46 47 48
    @Autowired
    private TradeRecordMapper tradeRecordMapper;

licc's avatar
licc committed
49 50 51
    @Autowired
    private MonthManureMapper monthManureMapper;

licc's avatar
licc committed
52 53 54 55

    private static final String PATTERN = "yyyy-MM";


licc's avatar
licc committed
56 57 58 59 60 61 62
    /**
     * 保存用户佣金
     *
     * @param orderInfo     订单信息
     * @param accountInfo   账户信息
     * @param memberPercent 会员等级百比分
     */
licc's avatar
licc committed
63
    @Transactional(rollbackFor = Exception.class)
licc's avatar
licc committed
64
    public void updateOrderAddMoney(OrderInfo orderInfo, AccountInfo accountInfo, MemberPercent memberPercent) {
licc's avatar
licc committed
65
        //1、计算返佣金额
licc's avatar
licc committed
66
        BigDecimal bigDecimal = orderInfo.getPayment().multiply(memberPercent.getPercent());
licc's avatar
licc committed
67
        BigDecimal extractMoney = accountInfo.getExtractMoney().add(bigDecimal);
licc's avatar
licc committed
68
        extractMoney.setScale(2, RoundingMode.HALF_UP);
licc's avatar
licc committed
69 70
        accountInfo.setExtractMoney(extractMoney);

licc's avatar
licc committed
71
        BigDecimal performanceMonth = accountInfo.getEarningsMonth().add(bigDecimal);
licc's avatar
licc committed
72
        performanceMonth.setScale(2, RoundingMode.HALF_UP);
licc's avatar
licc committed
73
        accountInfo.setEarningsMonth(performanceMonth);
licc's avatar
licc committed
74

licc's avatar
licc committed
75
        BigDecimal performanceTotal = accountInfo.getEarningsMonth().add(bigDecimal);
licc's avatar
licc committed
76 77
        performanceTotal.setScale(2, RoundingMode.HALF_UP);
        accountInfo.setEarningsTotal(performanceTotal);
78

licc's avatar
licc committed
79
        //2、修改订单返佣状态:已返佣 1
licc's avatar
licc committed
80
        orderInfo.setRebateStatus(RebateStatusEnum.ALREADY_REBATE.getCode());
licc's avatar
licc committed
81

licc's avatar
licc committed
82
        orderMapper.updateById(orderInfo);
licc's avatar
licc committed
83

84
        //3、更新账户可用金额
licc's avatar
licc committed
85
        accountMapper.edit(accountInfo);
licc's avatar
licc committed
86 87 88 89

        //4、添加交易流水记录
        TradeRecord tradeRecord = new TradeRecord();
        tradeRecord.setUserId(orderInfo.getBuyerId());
licc's avatar
licc committed
90
        tradeRecord.setTradeType(TradeRecordEnum.ORDER_REBATE.getCode());
licc's avatar
licc committed
91
        tradeRecord.setTradeNo(orderInfo.getTid());
92
        tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
licc's avatar
licc committed
93
        recordMapper.add(tradeRecord);
licc's avatar
licc committed
94
    }
licc's avatar
licc committed
95 96 97 98 99 100 101

    @Transactional(rollbackFor = Exception.class)
    public void updateAccountPerformanceMonth(List<TeamPerformance> list) {
        for (TeamPerformance teamPerformance : list) {
            teamPerformanceMapper.updateById(teamPerformance);
        }
    }
licc's avatar
licc committed
102

licc's avatar
licc committed
103 104 105 106 107 108 109
    /**
     * 修改 或新增  删除 最大进步奖实体类
     *
     * @param listVo          最新的进步奖 对象
     * @param accountInfoList 要更新的最大进步奖账户收益
     * @param prizes          上一次统计的进步奖 对象
     */
licc's avatar
licc committed
110
    @Transactional(rollbackFor = Exception.class)
licc's avatar
licc committed
111
    public Boolean updateOrSavePrize(List<TeamPerformanceSortVo> listVo, List<AccountInfo> accountInfoList, List<ProgressPrize> prizes) {
licc's avatar
licc committed
112 113 114

        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
licc's avatar
licc committed
115
        //构造一个map   key:userId  value progressPrize
licc's avatar
licc committed
116
        Map<String, ProgressPrize> map = new HashMap<>(32);
licc's avatar
licc committed
117 118 119 120 121
        for (ProgressPrize progressPrize : prizes) {
            map.put(progressPrize.getUserId(), progressPrize);
        }

        //1、判断 prizes 是否为空集合,是新增
licc's avatar
licc committed
122 123 124 125 126 127 128 129 130
        //新增
        if (CollectionUtils.isEmpty(prizes)) {
            for (TeamPerformanceSortVo sortVo : listVo) {
                String userId = sortVo.getTeamPerformance().getUserId();
                ProgressPrize progressPrize = new ProgressPrize();
                progressPrize.setUserId(userId);
                progressPrize.setGrowthRate(sortVo.getGrowthRate());
                progressPrize.setAwardMoney(sortVo.getMonthPerformance());
                progressPrize.setYearMonth(yearMonth);
licc's avatar
licc committed
131 132 133 134
                int count = progressPrizeMapper.add(progressPrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
135 136
            }
        } else {
licc's avatar
licc committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            //2、不为空。存在的修改,不存在的新增,多余的,删除
            List<ProgressPrize> updateList = new ArrayList<>();
            List<ProgressPrize> addList = new ArrayList<>();
            List<String> userIds = prizes.stream().map(ProgressPrize::getUserId).collect(Collectors.toList());

            //遍历  筛选出要修改、新增、删除 对象
            for (TeamPerformanceSortVo sortVo : listVo) {
                if (userIds.contains(sortVo.getUserId())) {
                    //修改
                    ProgressPrize prize = map.get(sortVo.getUserId());
                    prize.setGrowthRate(sortVo.getGrowthRate());
                    prize.setAwardMoney(sortVo.getMonthPerformance());
                    updateList.add(prize);
                    //移除要修改的
                    prizes.remove(prize);
                } else {
                    //新增
                    String userId = sortVo.getTeamPerformance().getUserId();
                    ProgressPrize progress = new ProgressPrize();
                    progress.setUserId(userId);
                    progress.setGrowthRate(sortVo.getGrowthRate());
                    progress.setAwardMoney(sortVo.getMonthPerformance());
                    progress.setYearMonth(yearMonth);
                    addList.add(progress);
                }
            }
licc's avatar
licc committed
163
            List<ProgressPrize> deleteList = new ArrayList<>(prizes);
licc's avatar
licc committed
164 165

            for (ProgressPrize updatePrize : updateList) {
licc's avatar
licc committed
166 167 168 169
                int count = progressPrizeMapper.edit(updatePrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
170 171 172
            }

            for (ProgressPrize addPrize : addList) {
licc's avatar
licc committed
173 174 175 176
                int count = progressPrizeMapper.add(addPrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
177 178 179
            }

            for (ProgressPrize deletePrize : deleteList) {
licc's avatar
licc committed
180 181 182 183
                int count = progressPrizeMapper.delById(deletePrize.getId());
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
184 185
            }
        }
licc's avatar
licc committed
186

licc's avatar
licc committed
187 188 189
        //3、添加账户获得的收益
        for (AccountInfo accountInfo : accountInfoList) {
            accountMapper.edit(accountInfo);
licc's avatar
licc committed
190 191 192 193 194 195

            //添加交易流水记录
            TradeRecord tradeRecord = new TradeRecord();
            tradeRecord.setUserId(accountInfo.getUserId());
            tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode());
            tradeRecord.setTradeNo(null);
196
            tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
licc's avatar
licc committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            int count = recordMapper.add(tradeRecord);
            if (count == 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 更新账户信息和保存交易流水记录
     *
     * @param accountInfoList 账户列表
     * @param recordList      交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAddRecord(List<AccountInfo> accountInfoList, List<TradeRecord> recordList) {

        //更新月收益
        if (!CollectionUtils.isEmpty(accountInfoList)) {
            for (AccountInfo accountInfo : accountInfoList) {
                int count = accountMapper.updateById(accountInfo);
                if (count == 0) {
                    return false;
                }
            }
        }

        //新增交易流水记录
        if (!CollectionUtils.isEmpty(recordList)) {
            for (TradeRecord tradeRecord : recordList) {
                int count = tradeRecordMapper.add(tradeRecord);
                if (count == 0) {
                    return false;
                }
            }
licc's avatar
licc committed
232
        }
licc's avatar
licc committed
233
        return true;
licc's avatar
licc committed
234
    }
licc's avatar
licc committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

    /**
     * 更新账户信息和保存交易流水记录、保存上月剩余月度肥料奖金
     *
     * @param accountInfoList 账户列表
     * @param recordList      交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAddRecordAndManure(List<AccountInfo> accountInfoList,
                                                   List<TradeRecord> recordList,
                                                   MonthManure manure) {

        //更新月收益
        if (!CollectionUtils.isEmpty(accountInfoList)) {
            for (AccountInfo accountInfo : accountInfoList) {
                int count = accountMapper.updateById(accountInfo);
                if (count == 0) {
                    return false;
                }
            }
        }

        //新增交易流水记录
        if (!CollectionUtils.isEmpty(recordList)) {
            for (TradeRecord tradeRecord : recordList) {
                int count = tradeRecordMapper.add(tradeRecord);
                if (count == 0) {
                    return false;
                }
            }
        }

        //新增上月剩余月度肥料奖金
        int count = monthManureMapper.add(manure);
        return count != 0;
    }
licc's avatar
licc committed
271
}