xiaoyong931011
2021-11-17 88d248e32b0a40e81d0b6c3a841b2faee5ffcabd
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
193
194
195
196
197
198
199
200
201
202
203
204
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 com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.utils.ExceptionUtils;
import com.xzx.gc.common.utils.RedisUtil;
import com.xzx.gc.entity.AccountInfo;
import com.xzx.gc.entity.AccountLog;
import com.xzx.gc.entity.CoreUser;
import com.xzx.gc.model.admin.XzxAccountLogModel;
import com.xzx.gc.model.comon.account.AllAcountParamDto;
import com.xzx.gc.model.user.AccountVo;
import com.xzx.gc.order.service.LimitApplyService;
import com.xzx.gc.pay.mapper.AccountLogMapper;
import com.xzx.gc.pay.mapper.AccountMapper;
import com.xzx.gc.service.AccountContext;
import com.xzx.gc.util.SessionUtil;
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.HashMap;
import java.util.Map;
 
@Service
@Transactional
public class AccountService {
 
    @Autowired
    private AccountMapper accountMapper;
 
    @Autowired
    private AccountLogMapper accountLogMapper;
 
    @Autowired
    private SessionUtil sessionUtil;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private RestoreLimitService restoreLimitService;
 
 
    @Autowired
    private LimitApplyService limitApplyService;
 
    public void update(AccountInfo accountInfo){
        accountMapper.updateByPrimaryKeySelective(accountInfo);
    }
 
 
    public AccountInfo findByUserId(String userId){
        AccountInfo accountInfo=new AccountInfo();
        accountInfo.setDelFlag(Constants.DEL_NOT_FLAG);
        accountInfo.setIsProhibit("0");
        accountInfo.setUserId(userId);
        AccountInfo accountInfo1 = accountMapper.selectOne(accountInfo);
        return accountInfo1;
    }
 
    public AccountInfo findByUserIdForbidden(String userId){
        AccountInfo accountInfo=new AccountInfo();
        accountInfo.setDelFlag(Constants.DEL_NOT_FLAG);
        accountInfo.setUserId(userId);
        AccountInfo accountInfo1 = accountMapper.selectOne(accountInfo);
        return accountInfo1;
    }
 
 
 
 
 
    /**
     *
     * @param receiverAccount 操作账户
     * @param channelType 操作渠道1:充值 2:提现 3:回收 4入库 5红包 6分享返利 7重量达标返利
     * @param channelId  操作ID 如充值 是充值流水号ID
     * @param channelMoney 操作金额
     */
    public void addAccount(AccountVo receiverAccount, String channelType, String channelId, String channelMoney){
 
        Map map = new HashMap();
        map.put("userId", receiverAccount.getUserId());
        String accountId = null;
 
        String now= DateUtil.now();
        AccountLog accountLog2 = new AccountLog();
        accountLog2.setCreateTime(now);
        accountLog2.setCreateUserId(receiverAccount.getUserId());
        accountLog2.setChannelType(Convert.toShort(channelType));
        accountLog2.setOrderId(channelId);
 
 
        //额度用完还用了环保金的情况
        String storageMoney = channelMoney;
        int limit = Convert.toInt(receiverAccount.getFixedLimit(),0);
        String receiverLimit = receiverAccount.getOverdraftLimit();
        accountId = receiverAccount.getAccountId();
        accountLog2.setAccountId(accountId);
        String money = receiverAccount.getMoney();
        BigDecimal v = NumberUtil.add(Convert.toBigDecimal(storageMoney), Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT));
        //查询账户信息
        if (v.compareTo(Convert.toBigDecimal(limit)) > 0) {
            //将额度填充完   再补money
            BigDecimal actualMoney = NumberUtil.sub(v, Convert.toBigDecimal(limit));
            map.put("money", actualMoney);
            map.put("limit", Convert.toBigDecimal(limit));
 
            accountLog2.setOldMoney(money);
            accountLog2.setNewMoney(Convert.toStr(NumberUtil.add(Convert.toBigDecimal(money), Convert.toBigDecimal(actualMoney))));
            accountLog2.setOldLimit(Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT).toString());
            accountLog2.setNewLimit(Convert.toStr(limit));
            accountLogMapper.insertSelective(accountLog2);
            accountMapper.updateMoneyAndLimit(map);
        } else {
            //直接补额度
            map.put("money", Convert.toBigDecimal(storageMoney));
            accountLog2.setOldLimit(Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT).toString());
            accountLog2.setNewLimit(Convert.toStr(NumberUtil.add(Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT), Convert.toBigDecimal(storageMoney))));
            accountLogMapper.insertSelective(accountLog2);
            accountMapper.updateLimitByOrder(map);
        }
 
    }
 
    private XzxAccountLogModel getAccountLog(AccountInfo accountInfo,String money){
        XzxAccountLogModel model = new XzxAccountLogModel();
        model.setOrderId("");
        model.setChannelType(9);
        model.setCreateTime(DateUtil.now());
        CoreUser user = sessionUtil.getCurrentUser();
        model.setCreateUserId(Long.toString(user.getId()));
        model.setAccountId(accountInfo.getAccountId());
        model.setOldMoney(accountInfo.getMoney());
        model.setNewMoney(accountInfo.getMoney());
        model.setOldLimit(accountInfo.getOverdraftLimit());
        model.setNewLimit(new BigDecimal(accountInfo.getOverdraftLimit()).add(new BigDecimal(money)).toString());
        model.setOldFixedLimit(accountInfo.getFixedLimit());
        model.setNewFixedLimit(new BigDecimal(accountInfo.getFixedLimit()).add(new BigDecimal(money)).toString());
        return model;
    }
 
 
    public AccountInfo queryById(String accountId) {
 
        return accountMapper.selectByPrimaryKey(accountId);
    }
 
    public void updateOverdrawPriceByUserId(String accountId,String money){
        //修改账户推送队列
        AccountInfo accountInfo = queryById(accountId);
        XzxAccountLogModel getAccountLog=getAccountLog(accountInfo,money);
        accountMapper.updateOverdrawPriceByUserId(accountId,money);
    }
 
    /**
     * 提高额度
     * @param userId
     * @param money
     */
    public void increaseOverdraftLimit(String userId,BigDecimal money,String payOrderId){
        if(redisUtil.setnx(Constants.REDIS_USER_KEY+"increaseOverdraftLimit:"+userId,"0")){
            try {
                AllAcountParamDto allAcountParamDto=AllAcountParamDto.builder().type(8).flowNo(payOrderId).userId(userId).
                        money(money).build();
//                accountFactory.getService(8).account(allAcountParamDto);
                new AccountContext(limitApplyService).account(allAcountParamDto);
            } catch (RestException e){
                ExceptionUtils.err(e.getMsg(),e);
            }catch (Exception e) {
                ExceptionUtils.err("提升额度失败",e);
            }finally {
                redisUtil.del(Constants.REDIS_USER_KEY+"increaseOverdraftLimit:"+userId);
            }
        }
    }
 
    /**
     * 恢复固定额度
     * @param userId
     */
    public void restoreFix(String userId,String payOrderId,String auditUserType){
        if(redisUtil.setnx(Constants.REDIS_USER_KEY+"restoreFix:"+userId,"0")){
            try {
                AllAcountParamDto allAcountParamDto=AllAcountParamDto.builder().type(1).flowNo(payOrderId)
                        .userId(userId).userRealType(auditUserType).build();
//                accountFactory.getService(1).account(allAcountParamDto);
                new AccountContext(restoreLimitService).account(allAcountParamDto);
            }catch (RestException e){
                ExceptionUtils.err(e.getMsg(),e);
            }catch (Exception e) {
                ExceptionUtils.err("恢复额度失败",e);
            }finally {
                redisUtil.del(Constants.REDIS_USER_KEY+"restoreFix:"+userId);
            }
        }
    }
 
}