xiaoyong931011
2021-11-09 d6778b4c38c3c9dd70da46715efdaaafc366ab5f
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
131
132
133
134
135
136
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.xzx.gc.pay.service;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
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.exception.RestException;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.comon.account.AllAcountParamDto;
import com.xzx.gc.pay.service.*;
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;
 
@Service
@Transactional
@Slf4j
public class RestoreLimitService extends BaseAccountService {
 
    @Autowired
    private CityPartnerService cityPartnerService;
 
    @Autowired
    private OtherUserService otherUserService;
 
 
    @Autowired
    private AccountService accountService;
 
 
    @Autowired
    private PartnerAccountService partnerAccountService;
 
 
    @Autowired
    private PartnerAccountLogService partnerAccountLogService;
 
 
    @Autowired
    private AccountLogService accountLogService;
 
 
    /**
     * 打包站标识
     */
    private static final String DBZ="1";
 
    @Override
    public Object account(AllAcountParamDto allAcountParamDto) {
        //额度恢复 恢复入库员的固定额度 同时要减去合伙人的金额 打包员则 需要 减去 打包站的额度(按额度优先原则 )
 
        //打包站恢复额度则需要减去合伙人的环保币
 
        String partnerId=null;
        if(StrUtil.isBlank(allAcountParamDto.getUserRealType())) {
            AccountInfo byUserId = accountService.findByUserId(allAcountParamDto.getUserId());
            if (byUserId == null) {
                throw new RestException(-1, "账户被禁用");
            }
            OtherUserInfo byId = otherUserService.findById(allAcountParamDto.getUserId());
            partnerId = byId.getPartnerId();
            allAcountParamDto.setFixedLimit(byUserId.getFixedLimit());
            allAcountParamDto.setAccountId(byUserId.getAccountId());
            allAcountParamDto.setOverdraftLimit(byUserId.getOverdraftLimit());
            CityPartner byId1 = cityPartnerService.queryById(partnerId);
            //1:合伙人。2:打包站
            String partnerType = byId1.getPartnerType();
            allAcountParamDto.setPartnerType(partnerType);
            allAcountParamDto.setPartnerId(partnerId);
            allAcountParamDto.setPartnerAccountId(byId1.getAccountNo());
 
        }else if(DBZ.equals(allAcountParamDto.getUserRealType())){
            String userId=allAcountParamDto.getUserId();
            //根据主打包员查询所属的打包站ID
            CityPartner byuserId = cityPartnerService.findByUserId(userId, OrderEnum.打包站.getValue());
            if(byuserId==null){
                throw new RestException("找不到对应的打包站");
            }
            partnerId=byuserId.getId()+"";
            //查询打包站的账户
            PartnerAccount accountInfo1 = partnerAccountService.findByPartnerId(partnerId);
            allAcountParamDto.setFixedLimit(accountInfo1.getFixedLimit());
 
            //打包站的账户
            allAcountParamDto.setAccountId(accountInfo1.getAccount());
            allAcountParamDto.setOverdraftLimit(accountInfo1.getOverdraftLimit());
 
            CityPartner byId1 = cityPartnerService.queryById(byuserId.getPackingStation());
            allAcountParamDto.setPartnerId(byId1.getId().toString());
            //合伙人的账户
            allAcountParamDto.setPartnerAccountId(byId1.getAccountNo());
        }
 
 
        log.debug("准备进行额度恢复操作:{}",JSONUtil.toJsonPrettyStr(allAcountParamDto));
        //额度恢复 入库员加钱 合伙人扣钱
        BigDecimal limit=Convert.toBigDecimal(allAcountParamDto.getOverdraftLimit(),Constants.MONEY_INIT);
        //实际恢复的额度
        BigDecimal realLimit=NumberUtil.sub(Convert.toBigDecimal(allAcountParamDto.getFixedLimit()),limit);
 
        if(StrUtil.isBlank(allAcountParamDto.getUserRealType())) {
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setAccountId(allAcountParamDto.getAccountId());
            accountInfo.setOverdraftLimit(allAcountParamDto.getFixedLimit());
            accountInfo.setUpdateTime(DateUtil.now());
            accountService.update(accountInfo);
 
            AccountLog accountLog = new AccountLog();
            accountLog.setAccountId(allAcountParamDto.getAccountId());
            accountLog.setCreateUserId(allAcountParamDto.getUserId());
            accountLog.setChannelType(Convert.toShort(CommonEnum.额度恢复.getValue()));
            accountLog.setOrderId(allAcountParamDto.getFlowNo());
            accountLog.setOldLimit(allAcountParamDto.getOverdraftLimit());
            accountLog.setNewLimit(allAcountParamDto.getFixedLimit());
            accountLog.setOldFixedLimit(accountLog.getNewLimit());
            accountLog.setNewFixedLimit(accountLog.getNewLimit());
            accountLogService.add(accountLog);
 
 
            //合伙人
            if("1".equals(allAcountParamDto.getPartnerType())){
                PartnerAccountLog partnerAccountLog=new PartnerAccountLog();
                partnerAccountLog.setPartnerId(allAcountParamDto.getPartnerId());
                partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());
                partnerAccountLog.setType(OrderEnum.额度调整.getValue());
                partnerAccountLog.setMoney("-"+realLimit.toString());
                partnerAccountLogService.add(partnerAccountLog);
                partnerAccountService.updateReduceMoneyByPartnerId(allAcountParamDto.getPartnerId(),realLimit);
            }else if("2".equals(allAcountParamDto.getPartnerType())){
                //打包站 先扣额度再扣钱
                PartnerAccountLog partnerAccountLog1=partnerAccountService.updateAll(allAcountParamDto.getPartnerAccountId(), Convert.toBigDecimal("-"+realLimit.toString()));
                PartnerAccountLog partnerAccountLog = new PartnerAccountLog();
                partnerAccountLog.setAccountId(allAcountParamDto.getPartnerAccountId());
                partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());
                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);
            }
 
 
        }else if(DBZ.equals(allAcountParamDto.getUserRealType())){
 
            //打包站可用额度恢复
            PartnerAccountLog partnerAccountLog = new PartnerAccountLog();
            partnerAccountLog.setAccountId(allAcountParamDto.getAccountId());
            partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());
            partnerAccountLog.setType(OrderEnum.恢复额度.getValue());
            partnerAccountLog.setManageLimit(realLimit.toString());
            PartnerAccount  partnerAccount=new PartnerAccount();
            partnerAccount.setOverdraftLimit(allAcountParamDto.getOverdraftLimit());
            partnerAccountLog.setPartnerAccount(partnerAccount);
            partnerAccountLogService.add(partnerAccountLog);
 
            PartnerAccount accountInfo = new PartnerAccount();
            accountInfo.setAccount(allAcountParamDto.getAccountId());
            accountInfo.setOverdraftLimit(allAcountParamDto.getFixedLimit());
            accountInfo.setUpdateTime(DateUtil.now());
            partnerAccountService.updateByAccount(accountInfo);
 
 
 
            //合伙人扣除环保币
            partnerAccountLog=new PartnerAccountLog();
            partnerAccountLog.setPartnerId(allAcountParamDto.getPartnerId());
            partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());
            partnerAccountLog.setType(OrderEnum.恢复额度.getValue());
            partnerAccountLog.setMoney("-"+realLimit.toString());
            partnerAccountLogService.add(partnerAccountLog);
            partnerAccountService.updateReduceMoneyByPartnerId(allAcountParamDto.getPartnerId(),realLimit);
        }
 
 
 
        log.debug("额度恢复操作已完成");
        return null;
    }
}