xuhy
2023-08-11 3c1223bf3eb6df077b48637a7c93a93246a1a8ae
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.UserInfoMapper;
import com.stylefeng.guns.modular.system.dao.WithdrawalMapper;
import com.stylefeng.guns.modular.system.model.BankCard;
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.model.UserInfo;
import com.stylefeng.guns.modular.system.model.Withdrawal;
import com.stylefeng.guns.modular.system.service.IBankCardService;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.service.IWithdrawalService;
import com.stylefeng.guns.modular.system.util.ICBCPayUtil;
import com.stylefeng.guns.modular.system.util.LiuZhouBank.ProtocolSignUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
 
@Service
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class WithdrawalServiceImpl extends ServiceImpl<WithdrawalMapper, Withdrawal> implements IWithdrawalService {
 
    @Resource
    private WithdrawalMapper withdrawalMapper;
 
    @Resource
    private UserInfoMapper userInfoMapper;
 
    @Autowired
    private IDriverService driverService;
 
    @Autowired
    private ICBCPayUtil icbcPayUtil;
 
    @Autowired
    private RestTemplate internalRestTemplate;
 
    @Autowired
    private IBankCardService bankCardService;
 
 
 
    private Map<String, Timer> timerMap = new HashMap<>();
 
 
 
 
    /**
     * 提现操作
     * @param money
     * @param code
     * @param name
     * @param uid
     * @throws Exception
     */
    @Override
    public ResultUtil withdrawal(Double money, Integer bankCardId, String name, Integer uid, Integer type) throws Exception {
        if(money.compareTo(0D) <= 0){
            return ResultUtil.error("提现金额必须大于0");
        }
        Driver driver = driverService.selectById(uid);
        if(null == type){
            type = 1;
        }
 
        Withdrawal withdrawal = new Withdrawal();
        if(type == 1){
            if(null == driver.getLaveActivityMoney()){
                return ResultUtil.error("账户没有余额,不能提现");
            }
            if(driver.getLaveActivityMoney().compareTo(money) < 0){
                return ResultUtil.error("提现金额必须小于账户余额");
            }
            withdrawal.setBalance(driver.getLaveActivityMoney());
        }
        if(type == 2){
            if(null == driver.getLaveBusinessMoney()){
                return ResultUtil.error("账户没有余额,不能提现");
            }
            if(driver.getLaveBusinessMoney().compareTo(money) < 0){
                return ResultUtil.error("提现金额必须小于账户余额");
            }
            withdrawal.setBalance(driver.getLaveBusinessMoney());
        }
        BankCard bankCard = bankCardService.selectById(bankCardId);
        withdrawal.setCode(bankCard.getCode());
        withdrawal.setFlag(1);
        withdrawal.setInsertTime(new Date());
        withdrawal.setMoney(money);
        withdrawal.setName(name);
        withdrawal.setState(1);
        withdrawal.setUserId(uid);
        withdrawal.setUserType(2);
        withdrawal.setType(type);
        withdrawal.setWithdrawalType(1);//线上
 
        if(type == 1){
            double v = new BigDecimal(driver.getLaveActivityMoney()).subtract(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
            driver.setLaveActivityMoney(v);
            v = new BigDecimal(driver.getBalance()).subtract(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
            driver.setBalance(v);
        }
        if(type == 2){
            double v = new BigDecimal(driver.getLaveBusinessMoney()).subtract(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
            driver.setLaveBusinessMoney(v);
        }
        this.insert(withdrawal);
        driverService.updateById(driver);
        return ResultUtil.success();
    }
 
 
    /**
     * 获取历史提交数
     * @param uid
     * @param pageNum
     * @param size
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryWithdrawal(Integer uid, Integer type, Integer pageNum, Integer size) throws Exception {
        pageNum = (pageNum - 1) * size;
        return withdrawalMapper.queryWithdrawal(uid, 2, type, pageNum, size);
    }
 
 
    /**
     * 提交审核处理
     * @param id
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil withdrawalAudit(Integer id, Integer state) throws Exception {
        Withdrawal withdrawal = this.selectById(id);
        if(withdrawal.getState() == 2){
            return ResultUtil.error("申请已审核通过,不能重复提交");
        }
        if(withdrawal.getState() == 3){
            return ResultUtil.error("申请已审核拒绝,不能重复提交");
        }
 
        if(withdrawal.getUserType() == 1){//用户
            UserInfo userInfo = userInfoMapper.selectById(withdrawal.getUserId());
            if(state == 2){//审核拒绝
                userInfo.setBalance(new BigDecimal(userInfo.getBalance()).add(new BigDecimal(withdrawal.getMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                userInfoMapper.updateById(userInfo);
 
                withdrawal.setState(3);
                this.updateById(withdrawal);
            }else{
                String code = withdrawal.getCode();
                BankCard bankCard = bankCardService.selectOne(new EntityWrapper<BankCard>().eq("code", code).eq("userType", 1).eq("userId", userInfo.getId()));
                if(null == bankCard){
                    return ResultUtil.error("请先绑定该银行卡");
                }
                Map<String, String> map = ProtocolSignUtil.queryProtocolSign(bankCard.getSignNum(), bankCard.getCode());
                String code2 = map.get("code");
                if("01".equals(code2)){
                    String msg = map.get("msg");
                    return ResultUtil.error(msg);
                }
                String signStatus = map.get("SignStatus");
                if("02".equals(signStatus)){
                    return ResultUtil.error("该银行卡已解约,请先进行签约");
                }
                if("03".equals(signStatus)){
                    return ResultUtil.error("该银行卡客户黑名单");
                }
                if("04".equals(signStatus)){
                    return ResultUtil.error("垫资超限冻结");
                }
                Map<String, Object> map1 = ProtocolSignUtil.protocolAgentPay(bankCard.getSignNum(), withdrawal.getMoney(), "用户提现");
                String code1 = map1.get("code").toString();
                if("01".equals(code1)){
                    return ResultUtil.error(map1.get("msg").toString());
                }
                withdrawal.setSerialNo(map1.get("TransSeq").toString());//交易序列号
                withdrawal.setState(2);
                this.updateById(withdrawal);
            }
        }else{//司机
            Driver driver = driverService.selectById(withdrawal.getUserId());
            if(state == 2){//拒绝
                driver.setBalance(new BigDecimal(driver.getBalance()).add(new BigDecimal(withdrawal.getMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                driver.setLaveActivityMoney(new BigDecimal(driver.getLaveActivityMoney()).add(new BigDecimal(withdrawal.getMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                driverService.updateById(driver);
            }else{
                String code = withdrawal.getCode();
                BankCard bankCard = bankCardService.selectOne(new EntityWrapper<BankCard>().eq("code", code).eq("userType", 2).eq("userId", driver.getId()));
                if(null == bankCard){
                    return ResultUtil.error("请先绑定该银行卡");
                }
                Map<String, String> map = ProtocolSignUtil.queryProtocolSign(bankCard.getSignNum(), bankCard.getCode());
                String code2 = map.get("code");
                if("01".equals(code2)){
                    String msg = map.get("msg");
                    return ResultUtil.error(msg);
                }
                String signStatus = map.get("SignStatus");
                if("02".equals(signStatus)){
                    return ResultUtil.error("该银行卡已解约,请先进行签约");
                }
                if("03".equals(signStatus)){
                    return ResultUtil.error("该银行卡客户黑名单");
                }
                if("04".equals(signStatus)){
                    return ResultUtil.error("垫资超限冻结");
                }
                Map<String, Object> map1 = ProtocolSignUtil.protocolAgentPay(bankCard.getSignNum(), withdrawal.getMoney(), "司机提现");
                String code1 = map1.get("code").toString();
                if("01".equals(code1)){
                    return ResultUtil.error(map1.get("msg").toString());
                }
                withdrawal.setSerialNo(map1.get("TransSeq").toString());//交易序列号
                withdrawal.setState(2);
                this.updateById(withdrawal);
            }
        }
        return ResultUtil.success();
    }
}