zhibing.pu
2024-08-15 519aede1f3056cd87835ab9fa1c98328830fc462
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.dao.WithdrawalMapper;
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.IUserInfoService;
import com.stylefeng.guns.modular.system.service.IWithdrawalService;
import com.stylefeng.guns.modular.system.util.DateUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.Tingg.TinggPayoutUtil;
import com.stylefeng.guns.modular.system.util.Tingg.model.AuthStatus;
import com.stylefeng.guns.modular.system.util.Tingg.model.PayoutResponse;
import com.stylefeng.guns.modular.system.util.Tingg.model.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
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;
 
    @Autowired
    private IUserInfoService userInfoService;
 
    @Autowired
    private TinggPayoutUtil tinggPayoutUtil;
 
    private Map<String, Timer> timerMap = new HashMap<>();
 
 
 
 
    /**
     * 提现操作
     * @param money
     * @param uid
     * @throws Exception
     */
    @Override
    public synchronized ResultUtil withdrawal(Double money, Integer uid, String remark, Integer language) throws Exception {
        language = userInfoService.queryLanguage(uid, language);
        if(money.compareTo(0D) <= 0){
            return ResultUtil.error(language == 1 ? "提现金额必须大于0" : language == 2 ? "Withdrawal amount must be greater than zero." : "Le montant du retrait doit être supérieur à zéro.");
        }
        UserInfo userInfo = userInfoService.selectById(uid);
        if(null == userInfo.getBalance()){
            return ResultUtil.error(language == 1 ? "账户余额不足" : language == 2 ? "Insufficient balance" : "Solde insuffisant");
        }
        if(userInfo.getBalance().compareTo(money) < 0){
            return ResultUtil.error(language == 1 ? "账户余额不足" : language == 2 ? "Insufficient balance" : "Solde insuffisant");
        }
 
        Withdrawal withdrawal = new Withdrawal();
        withdrawal.setBalance(userInfo.getBalance());
        withdrawal.setCode(userInfo.getPhone());// TODO: 2023/7/5 第三方支支持手机号提现
        withdrawal.setBankName(userInfo.getPhoneOperator());
        withdrawal.setFlag(1);
        withdrawal.setInsertTime(new Date());
        withdrawal.setRemark(remark);
        withdrawal.setMoney(money);
        withdrawal.setName(userInfo.getFirstName() + " " + userInfo.getLastName());
        withdrawal.setState(1);
        withdrawal.setUserId(uid);
        withdrawal.setUserType(1);
        this.insert(withdrawal);
 
        double v = new BigDecimal(userInfo.getBalance()).subtract(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
        userInfo.setBalance(v);
        userInfoService.updateById(userInfo);
        return ResultUtil.success();
    }
 
 
    /**
     * 获取历史提交数
     * @param uid
     * @param pageNum
     * @param size
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryWithdrawal(Integer uid, Integer pageNum, Integer size, Integer state, Integer language) throws Exception {
        pageNum = (pageNum - 1) * size;
        String name = language == 1 ? "手机号" : language == 2 ? "Mobile Money" : "Argent mobile";
        List<Map<String, Object>> list = withdrawalMapper.queryWithdrawal(uid, 1, state, pageNum, size);
        for (Map<String, Object> map : list) {
            map.put("name", name + "(" + map.get("code") + ")");
            if(null != map.get("insertTime")){
                String insertTime = map.get("insertTime").toString();
                map.put("insertTime", DateUtil.conversionFormat(language, insertTime));
            }
        }
        return list;
    }
 
 
    /**
     * 提现审核处理
     * @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(state == 1){//审核通过
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    //Integer integer = icbcPayUtil.queryTransfer(withdrawal.getSerialNo());
                    //if(integer == 0){//交易成功
                        withdrawal.setState(2);
                        WithdrawalServiceImpl.this.updateById(withdrawal);
 
                        Timer timer = timerMap.get(withdrawal.getSerialNo());
                        timer.cancel();
                    //}
                    /*if(integer == 2){//交易拒绝/失败
                        //还原余额
                        if(withdrawal.getUserType() == 1){//用户
                            withdrawal.setState(3);
                            WithdrawalServiceImpl.this.updateById(withdrawal);
 
                            UserInfo userInfo = userInfoService.selectById(withdrawal.getUserId());
                            userInfo.setBalance(new BigDecimal(userInfo.getBalance()).add(new BigDecimal(withdrawal.getMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                            userInfoService.updateById(userInfo);
 
                            Timer timer = timerMap.get(withdrawal.getSerialNo());
                            timer.cancel();
                        }
 
                    }*/
                }
            };
            Timer timer = new Timer();
            timer.schedule(timerTask, 1000, 10000);//1秒钟后间隔10秒钟查询交易结果
            timerMap.put(withdrawal.getSerialNo(), timer);
        }
        if(state == 2){//审核拒绝
            UserInfo userInfo = userInfoService.selectById(withdrawal.getUserId());
            userInfo.setBalance(new BigDecimal(userInfo.getBalance()).add(new BigDecimal(withdrawal.getMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            userInfoService.updateById(userInfo);
 
            withdrawal.setState(3);
            this.updateById(withdrawal);
        }
        return ResultUtil.success();
    }
}