xiaoyong931011
2021-07-23 4602ddaa6f8c408411aa6521b1f70ba3af629713
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
package com.xzx.gc.order.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.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 LimitApplyService 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;
 
 
 
    @Override
    public Object account(AllAcountParamDto allAcountParamDto) {
        AccountInfo byUserId = accountService.findByUserIdForbidden(allAcountParamDto.getUserId());
        if("1".equals(byUserId.getIsProhibit())){
            throw new RestException(-1,"账户被禁用");
        }
        String partnerId = otherUserService.findById(allAcountParamDto.getUserId()).getPartnerId();
        CityPartner byId1 = cityPartnerService.queryById(partnerId);
        //1:合伙人。2:打包站
        String partnerType = byId1.getPartnerType();
        String accountId = byUserId.getAccountId();
        allAcountParamDto.setAccountId(accountId);
        allAcountParamDto.setOverdraftLimit(byUserId.getOverdraftLimit());
        allAcountParamDto.setFixedLimit(byUserId.getFixedLimit());
        allAcountParamDto.setPartnerId(partnerId);
        allAcountParamDto.setPartnerType(partnerType);
        allAcountParamDto.setPartnerAccountId(byId1.getAccountNo());
 
        log.debug("准备进行额度申请操作:{}",JSONUtil.toJsonPrettyStr(allAcountParamDto));
        //合伙人扣钱或打包站先扣额度再扣钱 平台用户加钱
        accountId = allAcountParamDto.getAccountId();
        BigDecimal overdraftLimit = Convert.toBigDecimal(allAcountParamDto.getOverdraftLimit(),Constants.MONEY_INIT);
        BigDecimal fixLimit = Convert.toBigDecimal(allAcountParamDto.getFixedLimit(),Constants.MONEY_INIT);
        AccountInfo accountInfo=new AccountInfo();
        accountInfo.setAccountId(accountId);
        accountInfo.setOverdraftLimit(Convert.toStr(NumberUtil.add(overdraftLimit,allAcountParamDto.getMoney())));
        accountInfo.setFixedLimit(Convert.toStr(NumberUtil.add(fixLimit,allAcountParamDto.getMoney())));
        accountInfo.setUpdateTime(DateUtil.now());
        accountService.update(accountInfo);
 
        AccountLog accountLog=new AccountLog();
        accountLog.setAccountId(accountId);
        accountLog.setCreateUserId(allAcountParamDto.getUserId());
        accountLog.setChannelType(Convert.toShort(CommonEnum.额度申请.getValue()));
        accountLog.setOrderId(allAcountParamDto.getFlowNo());
        accountLog.setOldLimit(Convert.toStr(overdraftLimit));
        accountLog.setOldFixedLimit(Convert.toStr(fixLimit));
        accountLog.setNewLimit(Convert.toStr(NumberUtil.add(overdraftLimit,allAcountParamDto.getMoney())));
        accountLog.setNewFixedLimit(Convert.toStr(NumberUtil.add(fixLimit,allAcountParamDto.getMoney())));
        accountLogService.add(accountLog);
 
        //平台操作
        partnerType = allAcountParamDto.getPartnerType();
        //合伙人
        if("1".equals(partnerType)){
            PartnerAccountLog partnerAccountLog=new PartnerAccountLog();
            partnerAccountLog.setPartnerId(allAcountParamDto.getPartnerId());
            partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());
            partnerAccountLog.setType(OrderEnum.额度调整.getValue());
            partnerAccountLog.setMoney("-"+allAcountParamDto.getMoney().toString());
            partnerAccountLogService.add(partnerAccountLog);
            partnerAccountService.updateReduceMoneyByPartnerId(allAcountParamDto.getPartnerId(),allAcountParamDto.getMoney());
        }else if("2".equals(partnerType)){
            //打包站 先扣额度再扣钱
            PartnerAccountLog partnerAccountLog1=partnerAccountService.updateAll(allAcountParamDto.getPartnerAccountId(), Convert.toBigDecimal("-"+allAcountParamDto.getMoney().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);
        }
        log.debug("额度申请操作已完成:用户:{},额度:{}",allAcountParamDto.getUserId(),allAcountParamDto.getMoney().toString());
        return null;
    }
}