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
125
126
127
package com.xzx.gc.pay.service;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.entity.PartnerAccount;
import com.xzx.gc.entity.PartnerAccountLog;
import com.xzx.gc.pay.mapper.PartnerAccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import java.math.BigDecimal;
 
@Service
@Transactional
@Slf4j
public class PartnerAccountService {
 
    @Autowired
    private PartnerAccountMapper partnerAccountMapper;
 
 
    public PartnerAccount findByPartnerId(String partnerId){
        PartnerAccount partnerAccount=new PartnerAccount();
        partnerAccount.setPartnerId(partnerId);
        partnerAccount.setDelFlag(Constants.DEL_NOT_FLAG);
        PartnerAccount partnerAccount1 = partnerAccountMapper.selectOne(partnerAccount);
        return partnerAccount1;
    }
 
 
 
    public PartnerAccount findByAccount(String accountId){
        PartnerAccount partnerAccount=new PartnerAccount();
        partnerAccount.setAccount(accountId);
        partnerAccount.setDelFlag(0);
        return partnerAccountMapper.selectOne(partnerAccount);
    }
 
 
    /**
     * 根据money变化自动判断增长 及优先额度再是环保币的规则
     */
    public PartnerAccountLog updateAll(String accountId, BigDecimal money){
        PartnerAccountLog partnerAccountLog=new PartnerAccountLog();
        PartnerAccount byAccount = findByAccount(accountId);
        String hbb = byAccount.getHbb();
        String fixed=byAccount.getFixedLimit();
        String over=byAccount.getOverdraftLimit();
        if(money.compareTo(BigDecimal.ZERO)>0){
            money=money.abs();
            //增长
            BigDecimal limit = Convert.toBigDecimal(fixed);
            BigDecimal v = NumberUtil.add(money, Convert.toBigDecimal(over));
            //查询账户信息
            if (v.compareTo(limit) > 0) {
                //将额度填充完   再补money
                BigDecimal actualMoney = NumberUtil.sub(v, limit);
                updateAddMoneyResetFixLimitByAccountId(accountId,actualMoney,limit);
                partnerAccountLog.setAccountMoney(hbb);
                partnerAccountLog.setMoney(actualMoney.toString());
                partnerAccountLog.setOldLimit(over);
                partnerAccountLog.setManageLimit(NumberUtil.sub(limit,Convert.toBigDecimal(over)).toString());
            } else {
                //直接补额度
                updateAddMoneyLimitByAccountId(accountId,null,money);
                partnerAccountLog.setOldLimit(over);
                partnerAccountLog.setManageLimit(money.toString());
            }
 
        } else if(money.compareTo(BigDecimal.ZERO)<0){
            money=money.abs();
            if(NumberUtil.add(Convert.toBigDecimal(hbb,Constants.MONEY_INIT),Convert.toBigDecimal(over,Constants.MONEY_INIT)).compareTo(money)<0){
                throw new RestException(-1,"打包站账户余额不足");
            }
            //判断要操作金额是否是在可用额度 和 环保币的和之内
            if (NumberUtil.isLessOrEqual(money, Convert.toBigDecimal(over))) {//支付金额小于等于支付额度  优先扣除支付额度
                partnerAccountMapper.updateReduceMoneyLimitByAccountId(accountId,null,money);
                partnerAccountLog.setOldLimit(over);
                partnerAccountLog.setManageLimit("-"+money.toString());
            } else if (NumberUtil.isGreater(money, Convert.toBigDecimal(over))) {   //支付金额大于支付额度 也是优先扣除额度变为0  剩下的用余额扣除
                BigDecimal actualMoney = NumberUtil.sub(money, Convert.toBigDecimal(over));
                partnerAccountMapper.updateReduceMoneyResetLimitByAccountId(accountId,Convert.toBigDecimal(actualMoney));
                partnerAccountLog.setAccountMoney(hbb);
                partnerAccountLog.setMoney("-"+actualMoney);
                partnerAccountLog.setOldLimit(over);
                partnerAccountLog.setManageLimit("-"+over);
            }
        }
        return partnerAccountLog;
 
    }
 
 
    public void updateAddMoneyLimitByAccountId(String accountId,BigDecimal money,BigDecimal over){
        partnerAccountMapper.updateAddMoneyLimitByAccountId(accountId,money,over);
    }
 
    public void updateAddMoneyResetFixLimitByAccountId(String accountId,BigDecimal money,BigDecimal over){
        partnerAccountMapper.updateAddMoneyResetFixLimitByAccountId(accountId,money,over);
    }
 
 
    public void updateReduceMoneyByPartnerId(String partnerId,BigDecimal money){
        //判断合伙人账户的钱是否足够
        PartnerAccount byPartnerId = findByPartnerId(partnerId);
        BigDecimal bigDecimal = Convert.toBigDecimal(byPartnerId.getHbb(),Constants.MONEY_INIT);
        if(bigDecimal.compareTo(money)<0){
            throw new RestException(-1,"合伙人账户余额不足");
        }
        partnerAccountMapper.updateReduceMoneyByPartnerId(partnerId,money);
    }
 
 
 
    public void updateByAccount(PartnerAccount partnerAccount){
        Example example=new Example(PartnerAccount.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("account",partnerAccount.getAccount());
        criteria.andEqualTo("delFlag","0");
        partnerAccountMapper.updateByExampleSelective(partnerAccount,example);
    }
}