AccountManager.java 12.2 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

        //4、添加交易流水记录
        TradeRecord tradeRecord = new TradeRecord();
codezwjava's avatar
codezwjava committed
89
        tradeRecord.setUserId(orderInfo.getUserId());
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

    @Transactional(rollbackFor = Exception.class)
licc's avatar
licc committed
97
    public Boolean updateAccountPerformanceMonth(List<TeamPerformance> addList, List<TeamPerformance> updateList) {
licc's avatar
licc committed
98 99 100 101

        //1、新增
        if (!CollectionUtils.isEmpty(addList)) {
            for (TeamPerformance teamPerformance : addList) {
licc's avatar
licc committed
102 103 104 105
                int count = teamPerformanceMapper.add(teamPerformance);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
106 107 108 109 110 111
            }
        }

        //2、更新
        if (!CollectionUtils.isEmpty(updateList)) {
            for (TeamPerformance teamPerformance : updateList) {
licc's avatar
licc committed
112 113 114 115
                int count = teamPerformanceMapper.edit(teamPerformance);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
116
            }
licc's avatar
licc committed
117
        }
licc's avatar
licc committed
118
        return true;
licc's avatar
licc committed
119
    }
licc's avatar
licc committed
120

licc's avatar
licc committed
121 122 123 124 125 126 127
    /**
     * 修改 或新增  删除 最大进步奖实体类
     *
     * @param listVo          最新的进步奖 对象
     * @param accountInfoList 要更新的最大进步奖账户收益
     * @param prizes          上一次统计的进步奖 对象
     */
licc's avatar
licc committed
128
    @Transactional(rollbackFor = Exception.class)
licc's avatar
licc committed
129 130
    public Boolean updateOrSavePrize(List<TeamPerformanceSortVo> listVo, List<AccountInfo> accountInfoList,
                                     List<ProgressPrize> prizes) {
licc's avatar
licc committed
131 132 133

        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
licc's avatar
licc committed
134
        //构造一个map   key:userId  value progressPrize
licc's avatar
licc committed
135
        Map<String, ProgressPrize> map = new HashMap<>(32);
licc's avatar
licc committed
136 137 138 139 140
        for (ProgressPrize progressPrize : prizes) {
            map.put(progressPrize.getUserId(), progressPrize);
        }

        //1、判断 prizes 是否为空集合,是新增
licc's avatar
licc committed
141 142 143 144 145 146 147 148 149
        //新增
        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
150 151 152 153
                int count = progressPrizeMapper.add(progressPrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
154 155
            }
        } else {
licc's avatar
licc committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            //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
182
            List<ProgressPrize> deleteList = new ArrayList<>(prizes);
licc's avatar
licc committed
183 184

            for (ProgressPrize updatePrize : updateList) {
licc's avatar
licc committed
185 186 187 188
                int count = progressPrizeMapper.edit(updatePrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
189 190 191
            }

            for (ProgressPrize addPrize : addList) {
licc's avatar
licc committed
192 193 194 195
                int count = progressPrizeMapper.add(addPrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
196 197 198
            }

            for (ProgressPrize deletePrize : deleteList) {
licc's avatar
licc committed
199 200 201 202
                int count = progressPrizeMapper.delById(deletePrize.getId());
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
203 204
            }
        }
licc's avatar
licc committed
205

licc's avatar
licc committed
206 207 208
        //3、添加账户获得的收益
        for (AccountInfo accountInfo : accountInfoList) {
            accountMapper.edit(accountInfo);
licc's avatar
licc committed
209 210 211 212 213 214

            //添加交易流水记录
            TradeRecord tradeRecord = new TradeRecord();
            tradeRecord.setUserId(accountInfo.getUserId());
            tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode());
            tradeRecord.setTradeNo(null);
215
            tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
licc's avatar
licc committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
            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) {
licc's avatar
licc committed
236
                int count = accountMapper.edit(accountInfo);
licc's avatar
licc committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250
                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
251
        }
licc's avatar
licc committed
252
        return true;
licc's avatar
licc committed
253
    }
licc's avatar
licc committed
254

licc's avatar
licc committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    /**
     * 更新账户信息和保存交易流水记录保存最大进步奖信息
     *
     * @param accountInfoList 账户列表
     * @param recordList      交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAddRecordAddPrize(List<AccountInfo> accountInfoList, List<TradeRecord> recordList,
                                          List<ProgressPrize> prizeList) {

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

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

        //新增最大进步奖信息
        if (!CollectionUtils.isEmpty(prizeList)) {
            for (ProgressPrize progressPrize : prizeList) {
                int count = progressPrizeMapper.add(progressPrize);
                if (count == 0) {
                    return false;
                }
            }
        }
        return true;
    }

licc's avatar
licc committed
297 298 299 300 301 302 303 304 305
    /**
     * 更新账户信息和保存交易流水记录、保存上月剩余月度肥料奖金
     *
     * @param accountInfoList 账户列表
     * @param recordList      交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAddRecordAndManure(List<AccountInfo> accountInfoList,
                                                   List<TradeRecord> recordList,
licc's avatar
licc committed
306
                                                   MonthManure manure, MonthManure monthManure) {
licc's avatar
licc committed
307 308 309 310

        //更新月收益
        if (!CollectionUtils.isEmpty(accountInfoList)) {
            for (AccountInfo accountInfo : accountInfoList) {
licc's avatar
licc committed
311
                int count = accountMapper.edit(accountInfo);
licc's avatar
licc committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
                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
328 329 330 331
        //判断是否存在月度肥料奖金信息
        if (null == monthManure) {
            //新增上月剩余月度肥料奖金
            int count = monthManureMapper.add(manure);
licc's avatar
licc committed
332
            return count != 0;
licc's avatar
licc committed
333 334 335 336 337
        } else {
            //编辑
            monthManure.setManureAward(manure.getManureAward());
            monthManure.setYearMonth(manure.getYearMonth());
            int count = monthManureMapper.edit(monthManure);
licc's avatar
licc committed
338
            return count != 0;
licc's avatar
licc committed
339
        }
licc's avatar
licc committed
340
    }
licc's avatar
licc committed
341
}