AccountManager.java 15.3 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
        accountInfo.setExtractMoney(extractMoney.setScale(2, RoundingMode.HALF_UP));
licc's avatar
licc committed
69

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

licc's avatar
licc committed
73
        BigDecimal performanceTotal = accountInfo.getEarningsTotal().add(bigDecimal);
licc's avatar
licc committed
74
        accountInfo.setEarningsTotal(performanceTotal.setScale(2, RoundingMode.HALF_UP));
75

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

licc's avatar
licc committed
79
        orderMapper.updateById(orderInfo);
licc's avatar
licc committed
80 81
        String time = DateUtil.convertDateToStr(new Date(), "yyyy-MM-dd hh:mm:ss");
        log.info("订单" + orderInfo.getTid() + "在" + time + "时返佣:" + bigDecimal);
licc's avatar
licc committed
82

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

        //4、添加交易流水记录
        TradeRecord tradeRecord = new TradeRecord();
codezwjava's avatar
codezwjava committed
88
        tradeRecord.setUserId(orderInfo.getUserId());
licc's avatar
licc committed
89
        tradeRecord.setTradeType(TradeRecordEnum.ORDER_REBATE.getCode());
licc's avatar
licc committed
90
        tradeRecord.setTradeNo(orderInfo.getTid());
91
        tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
licc's avatar
licc committed
92
        tradeRecord.setMoney(bigDecimal);
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
                log.info("月度肥料新增用户" + teamPerformance.getUserId() + "团队绩效:" + teamPerformance.getMonthTeamPerformance());
licc's avatar
licc committed
107 108 109 110 111 112
            }
        }

        //2、更新
        if (!CollectionUtils.isEmpty(updateList)) {
            for (TeamPerformance teamPerformance : updateList) {
licc's avatar
licc committed
113 114 115 116
                int count = teamPerformanceMapper.edit(teamPerformance);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
117
                log.info("月度肥料更新用户" + teamPerformance.getUserId() + "团队绩效为:" + teamPerformance.getMonthTeamPerformance());
licc's avatar
licc committed
118
            }
licc's avatar
licc committed
119
        }
licc's avatar
licc committed
120
        return true;
licc's avatar
licc committed
121
    }
licc's avatar
licc committed
122

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

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

        //1、判断 prizes 是否为空集合,是新增
licc's avatar
licc committed
143 144 145 146 147 148 149 150 151
        //新增
        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
152 153 154 155
                int count = progressPrizeMapper.add(progressPrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
156 157
            }
        } else {
licc's avatar
licc committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
            //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
185 186
            //剩余的是要删除的
            List<ProgressPrize> deleteList = new ArrayList<>(prizes);
licc's avatar
licc committed
187
            for (ProgressPrize updatePrize : updateList) {
licc's avatar
licc committed
188
                int count = progressPrizeMapper.edit(updatePrize);
licc's avatar
licc committed
189
                log.info("更新最大进步奖用户" + updatePrize.getUserId() + "奖金为:" + updatePrize.getAwardMoney());
licc's avatar
licc committed
190 191 192
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
193 194 195
            }

            for (ProgressPrize addPrize : addList) {
licc's avatar
licc committed
196
                int count = progressPrizeMapper.add(addPrize);
licc's avatar
licc committed
197
                log.info("新增最大进步奖用户" + addPrize.getUserId() + "奖金:" + addPrize.getAwardMoney());
licc's avatar
licc committed
198 199 200
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
201 202 203
            }

            for (ProgressPrize deletePrize : deleteList) {
licc's avatar
licc committed
204 205 206 207
                int count = progressPrizeMapper.delById(deletePrize.getId());
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
208 209
            }
        }
licc's avatar
licc committed
210

licc's avatar
licc committed
211 212 213
        //3、添加账户获得的收益
        for (AccountInfo accountInfo : accountInfoList) {
            accountMapper.edit(accountInfo);
licc's avatar
licc committed
214
            log.info("最大进步奖更新用户" + accountInfo.getUserId() + "月收益:" + accountInfo.getEarningsMonth());
licc's avatar
licc committed
215 216 217 218 219 220

            //添加交易流水记录
            TradeRecord tradeRecord = new TradeRecord();
            tradeRecord.setUserId(accountInfo.getUserId());
            tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode());
            tradeRecord.setTradeNo(null);
licc's avatar
licc committed
221 222 223 224 225 226
            if (flag == 1) {
                tradeRecord.setStatus(TradeStatusEnum.NO_SETTLE_ACCOUNTS.getCode());
            } else {
                tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
            }

licc's avatar
licc committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
            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
247
                int count = accountMapper.edit(accountInfo);
licc's avatar
licc committed
248 249 250
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
251 252 253 254 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
                log.info("月度肥料更新用户" + accountInfo.getUserId() + "月收益" + accountInfo.getEarningsMonth());
            }
        }

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


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

        //更新月收益
        if (!CollectionUtils.isEmpty(accountInfoList)) {
            for (AccountInfo accountInfo : accountInfoList) {
                int count = accountMapper.edit(accountInfo);
                if (count == 0) {
                    return false;
                }
                log.info("运营中心更新用户" + accountInfo.getUserId() + "月收益" + accountInfo.getEarningsMonth());
licc's avatar
licc committed
285 286 287 288 289 290 291 292 293 294 295
            }
        }

        //新增交易流水记录
        if (!CollectionUtils.isEmpty(recordList)) {
            for (TradeRecord tradeRecord : recordList) {
                int count = tradeRecordMapper.add(tradeRecord);
                if (count == 0) {
                    return false;
                }
            }
licc's avatar
licc committed
296
        }
licc's avatar
licc committed
297
        return true;
licc's avatar
licc committed
298
    }
licc's avatar
licc committed
299

licc's avatar
licc committed
300 301 302 303 304 305 306 307
    /**
     * 更新账户信息和保存交易流水记录保存最大进步奖信息
     *
     * @param accountInfoList 账户列表
     * @param recordList      交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAddRecordAddPrize(List<AccountInfo> accountInfoList, List<TradeRecord> recordList,
licc's avatar
licc committed
308
                                                  List<ProgressPrize> addPrizeList, List<ProgressPrize> updatePrizeList) {
licc's avatar
licc committed
309 310 311 312 313 314 315 316

        //更新月收益
        if (!CollectionUtils.isEmpty(accountInfoList)) {
            for (AccountInfo accountInfo : accountInfoList) {
                int count = accountMapper.edit(accountInfo);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
317
                log.info("最大进步奖更新用户" + accountInfo.getUserId() + "月收益" + accountInfo.getEarningsMonth());
licc's avatar
licc committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331
            }
        }

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

        //新增最大进步奖信息
licc's avatar
licc committed
332 333
        if (!CollectionUtils.isEmpty(addPrizeList)) {
            for (ProgressPrize progressPrize : addPrizeList) {
licc's avatar
licc committed
334 335 336 337
                int count = progressPrizeMapper.add(progressPrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
338
                log.info("用户" + progressPrize.getUserId() + "新增最大进步奖奖金:" + progressPrize.getAwardMoney());
licc's avatar
licc committed
339 340
            }
        }
licc's avatar
licc committed
341 342 343 344 345 346 347 348

        //编辑最大进步奖信息
        if (!CollectionUtils.isEmpty(updatePrizeList)) {
            for (ProgressPrize progressPrize : updatePrizeList) {
                int count = progressPrizeMapper.edit(progressPrize);
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
349
                log.info("用户" + progressPrize.getUserId() + "更新最大进步奖奖金为:" + progressPrize.getAwardMoney());
licc's avatar
licc committed
350 351
            }
        }
licc's avatar
licc committed
352 353 354
        return true;
    }

licc's avatar
licc committed
355 356 357 358 359 360 361 362 363
    /**
     * 更新账户信息和保存交易流水记录、保存上月剩余月度肥料奖金
     *
     * @param accountInfoList 账户列表
     * @param recordList      交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAddRecordAndManure(List<AccountInfo> accountInfoList,
                                                   List<TradeRecord> recordList,
licc's avatar
licc committed
364
                                                   MonthManure manure, MonthManure monthManure) {
licc's avatar
licc committed
365 366 367 368

        //更新月收益
        if (!CollectionUtils.isEmpty(accountInfoList)) {
            for (AccountInfo accountInfo : accountInfoList) {
licc's avatar
licc committed
369
                int count = accountMapper.edit(accountInfo);
licc's avatar
licc committed
370 371 372
                if (count == 0) {
                    return false;
                }
licc's avatar
licc committed
373
                log.info("月定时任务月度肥料更新用户" + accountInfo.getUserId() + "月收益:" + accountInfo.getEarningsMonth());
licc's avatar
licc committed
374 375 376 377 378 379 380 381 382 383 384 385 386
            }
        }

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

licc's avatar
licc committed
387
        //判断是否存在月度肥料剩余奖金信息
licc's avatar
licc committed
388 389 390
        if (null == monthManure) {
            //新增上月剩余月度肥料奖金
            int count = monthManureMapper.add(manure);
licc's avatar
licc committed
391
            return count != 0;
licc's avatar
licc committed
392 393 394 395 396
        } else {
            //编辑
            monthManure.setManureAward(manure.getManureAward());
            monthManure.setYearMonth(manure.getYearMonth());
            int count = monthManureMapper.edit(monthManure);
licc's avatar
licc committed
397
            return count != 0;
licc's avatar
licc committed
398
        }
licc's avatar
licc committed
399
    }
licc's avatar
licc committed
400
}