AccountManager.java 12.7 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
        //3、更新账户可用金额
licc's avatar
licc committed
82
        accountMapper.edit(accountInfo);
licc's avatar
licc committed
83 84 85

        //4、添加交易流水记录
        TradeRecord tradeRecord = new TradeRecord();
codezwjava's avatar
codezwjava committed
86
        tradeRecord.setUserId(orderInfo.getUserId());
licc's avatar
licc committed
87
        tradeRecord.setTradeType(TradeRecordEnum.ORDER_REBATE.getCode());
licc's avatar
licc committed
88
        tradeRecord.setTradeNo(orderInfo.getTid());
89
        tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
licc's avatar
licc committed
90
        recordMapper.add(tradeRecord);
licc's avatar
licc committed
91
    }
licc's avatar
licc committed
92 93

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

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

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

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

licc's avatar
licc committed
131
        //构造一个map   key:userId  value progressPrize
licc's avatar
licc committed
132
        Map<String, ProgressPrize> map = new HashMap<>(32);
licc's avatar
licc committed
133 134 135 136 137
        for (ProgressPrize progressPrize : prizes) {
            map.put(progressPrize.getUserId(), progressPrize);
        }

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

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

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

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

            //添加交易流水记录
            TradeRecord tradeRecord = new TradeRecord();
            tradeRecord.setUserId(accountInfo.getUserId());
            tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode());
            tradeRecord.setTradeNo(null);
licc's avatar
licc committed
213 214 215 216 217 218
            if (flag == 1) {
                tradeRecord.setStatus(TradeStatusEnum.NO_SETTLE_ACCOUNTS.getCode());
            } else {
                tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
            }

licc's avatar
licc committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            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
239
                int count = accountMapper.edit(accountInfo);
licc's avatar
licc committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253
                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
254
        }
licc's avatar
licc committed
255
        return true;
licc's avatar
licc committed
256
    }
licc's avatar
licc committed
257

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

        //更新月收益
        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;
                }
            }
        }

        //新增最大进步奖信息
licc's avatar
licc committed
289 290
        if (!CollectionUtils.isEmpty(addPrizeList)) {
            for (ProgressPrize progressPrize : addPrizeList) {
licc's avatar
licc committed
291 292 293 294 295 296
                int count = progressPrizeMapper.add(progressPrize);
                if (count == 0) {
                    return false;
                }
            }
        }
licc's avatar
licc committed
297 298 299 300 301 302 303 304 305 306

        //编辑最大进步奖信息
        if (!CollectionUtils.isEmpty(updatePrizeList)) {
            for (ProgressPrize progressPrize : updatePrizeList) {
                int count = progressPrizeMapper.edit(progressPrize);
                if (count == 0) {
                    return false;
                }
            }
        }
licc's avatar
licc committed
307 308 309
        return true;
    }

licc's avatar
licc committed
310 311 312 313 314 315 316 317 318
    /**
     * 更新账户信息和保存交易流水记录、保存上月剩余月度肥料奖金
     *
     * @param accountInfoList 账户列表
     * @param recordList      交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAddRecordAndManure(List<AccountInfo> accountInfoList,
                                                   List<TradeRecord> recordList,
licc's avatar
licc committed
319
                                                   MonthManure manure, MonthManure monthManure) {
licc's avatar
licc committed
320 321 322 323

        //更新月收益
        if (!CollectionUtils.isEmpty(accountInfoList)) {
            for (AccountInfo accountInfo : accountInfoList) {
licc's avatar
licc committed
324
                int count = accountMapper.edit(accountInfo);
licc's avatar
licc committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                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
341 342 343 344
        //判断是否存在月度肥料奖金信息
        if (null == monthManure) {
            //新增上月剩余月度肥料奖金
            int count = monthManureMapper.add(manure);
licc's avatar
licc committed
345
            return count != 0;
licc's avatar
licc committed
346 347 348 349 350
        } else {
            //编辑
            monthManure.setManureAward(manure.getManureAward());
            monthManure.setYearMonth(manure.getYearMonth());
            int count = monthManureMapper.edit(monthManure);
licc's avatar
licc committed
351
            return count != 0;
licc's avatar
licc committed
352
        }
licc's avatar
licc committed
353
    }
licc's avatar
licc committed
354
}