fix
Helius
2021-08-05 8bbb027dc36b3f3c7f2d505fc75180261fb4d640
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.xzx.gc.user.service;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.OrderEnum;
import com.xzx.gc.common.utils.BusinessUtil;
import com.xzx.gc.entity.AccountInfo;
import com.xzx.gc.entity.AccountLog;
import com.xzx.gc.entity.PartnerAccount;
import com.xzx.gc.entity.PartnerAccountLog;
import com.xzx.gc.model.comon.account.AllAcountParamDto;
import com.xzx.gc.service.BaseAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
 
@Service
@Transactional
@Slf4j
public class LimitChangeService extends BaseAccountService {
 
    @Autowired
    private AccountService accountService;
    @Autowired
    private BusinessUtil businessUtil;
 
 
    @Autowired
    private PartnerAccountLogService partnerAccountLogService;
 
    @Autowired
    private PartnerAccountService partnerAccountService;
 
    @Autowired
    private AccountLogService accountLogService;
 
 
    @Override
    public Object account(AllAcountParamDto allAcountParamDto) {
 
        String now=DateUtil.now();
        //计算额度之间的差额
        BigDecimal overBalance=BigDecimal.ZERO;
        for (Iterator<AccountInfo> iterator = allAcountParamDto.getAccountInfoList().iterator(); iterator.hasNext(); ) {
            AccountInfo accountInfo1=iterator.next();
            AccountInfo accountInfo = accountService.findById(accountInfo1.getAccountId());
            BigDecimal overdraftLimit = Convert.toBigDecimal(accountInfo1.getOverdraftLimit(),Constants.MONEY_INIT);
            BigDecimal fixLimit = Convert.toBigDecimal(accountInfo1.getFixedLimit(),Constants.MONEY_INIT);
            BigDecimal oldOverdraftLimit = Convert.toBigDecimal(accountInfo.getOverdraftLimit(), Constants.MONEY_INIT);
            BigDecimal oldFixLimit = Convert.toBigDecimal(accountInfo.getFixedLimit(), Constants.MONEY_INIT);
            BigDecimal sub=NumberUtil.sub(oldOverdraftLimit,overdraftLimit);
            BigDecimal fix=NumberUtil.sub(oldFixLimit,fixLimit);
            //无任何额度改变
            if(sub.compareTo(BigDecimal.ZERO)==0){
                iterator.remove();
                continue;
            }
            accountInfo1.setOldFixLimit(oldFixLimit);
            accountInfo1.setOldOverdraftLimit(oldOverdraftLimit);
            accountInfo1.setOverBalance(sub);
            accountInfo1.setOverFix(fix);
            overBalance=NumberUtil.add(overBalance,sub);
 
        }
 
        //总的差额
        allAcountParamDto.setOverBlance(overBalance);
 
        log.debug("准备进行额度管理操作:{}",JSONUtil.toJsonPrettyStr(allAcountParamDto));
        //打包员额度管理  打包员账户增加或减少,打包站增加或减少,先增减额度再增减余额
        List<AccountInfo> accountInfoList = allAcountParamDto.getAccountInfoList();
        BigDecimal overBlance = allAcountParamDto.getOverBlance();
        String partnerAccountId = allAcountParamDto.getPartnerAccountId();
        for (AccountInfo info : accountInfoList) {
            accountService.updateOverLimitByAccountId( info.getAccountId(),Convert.toBigDecimal(info.getOverdraftLimit()));
            accountService.updateFixLimitByAccountId( info.getAccountId(),Convert.toBigDecimal(info.getFixedLimit()));
            AccountLog accountLog = new AccountLog();
            accountLog.setCreateTime(now);
            accountLog.setAccountId(info.getAccountId());
            accountLog.setCreateUserId(info.getUserId());
            overBalance = info.getOverBalance();
            BigDecimal overFix = info.getOverFix();
            if(overBalance.compareTo(BigDecimal.ZERO)>0) {
                accountLog.setChannelType(Convert.toShort(CommonEnum.额度降低.getValue()));
            }else if(overBalance.compareTo(BigDecimal.ZERO)<0){
                accountLog.setChannelType(Convert.toShort(CommonEnum.额度申请.getValue()));
            }
 
            if(overFix.compareTo(BigDecimal.ZERO)!=0){
                accountLog.setOldLimit(businessUtil.changeMoney(info.getOldOverdraftLimit()));
                accountLog.setNewLimit(businessUtil.changeMoney(info.getOverdraftLimit()));
                accountLog.setOldFixedLimit(businessUtil.changeMoney(info.getOldFixLimit()));
                accountLog.setNewFixedLimit(businessUtil.changeMoney(info.getFixedLimit()));
            }else if(overBalance.compareTo(BigDecimal.ZERO)!=0){
                accountLog.setOldLimit(businessUtil.changeMoney(info.getOldOverdraftLimit()));
                accountLog.setNewLimit(businessUtil.changeMoney(info.getOverdraftLimit()));
            }
            accountLog.setOrderId(partnerAccountId);
            accountLogService.add(accountLog);
        }
 
        //修改打包站账户 先扣额度再扣环保金
        PartnerAccountLog partnerAccountLog1=partnerAccountService.updateAll(partnerAccountId, overBlance);
        PartnerAccountLog partnerAccountLog = new PartnerAccountLog();
        partnerAccountLog.setAccountId(partnerAccountId);
        partnerAccountLog.setFlowNo(partnerAccountId);
        partnerAccountLog.setType(OrderEnum.额度调整.getValue());
        partnerAccountLog.setMoney(partnerAccountLog1.getMoney());
        partnerAccountLog.setManageLimit(partnerAccountLog1.getManageLimit());
        partnerAccountLog.setManageLimitFix(partnerAccountLog1.getManageLimitFix());
        PartnerAccount partnerAccount=new PartnerAccount();
        partnerAccount.setHbb(partnerAccountLog1.getAccountMoney());
        partnerAccount.setOverdraftLimit(partnerAccountLog1.getOldLimit());
        partnerAccount.setFixedLimit(partnerAccountLog1.getOldLimitFix());
        partnerAccountLog.setPartnerAccount(partnerAccount);
        partnerAccountLogService.add(partnerAccountLog);
 
        log.debug("额度管理操作已完成");
        return null;
    }
}