zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
243
244
245
246
247
248
249
250
package project.web.api;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import kernel.web.ApplicationUtil;
import com.mysql.cj.util.StringUtils;
import project.party.PartyService;
import project.party.model.Party;
import kernel.exception.BusinessException;
import kernel.web.BaseAction;
import kernel.web.ResultObject;
import project.user.idcode.IdentifyingCodeService;
import project.web.api.JsonUtils;
import project.web.api.SafeUtils;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;
import project.web.api.HttpsTransport;
import org.springframework.jdbc.core.JdbcTemplate;
import project.wallet.WalletService;
import project.wallet.WalletLog;
import project.wallet.WalletLogService;
import project.Constants;
import project.log.MoneyLog;
import project.log.MoneyLogService;
import kernel.util.Arith;
import project.wallet.Wallet;
import project.wallet.WalletExtend;
import project.blockchain.RechargeBlockchain;
import project.blockchain.RechargeBlockchainService;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import project.log.MoneyLogService;
 
/**
 * 邮箱或手机绑定
 * 发送验证码
 *
 */
@RestController
@CrossOrigin
public class IdentifyingCodeController extends BaseAction {
 
    @Autowired
    private IdentifyingCodeService identifyingCodeService;
    @Autowired
    private PartyService partyService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private WalletLogService walletLogService;
    @Autowired
    private RechargeBlockchainService rechargeBlockchainService;
    @Autowired
    private MoneyLogService moneyLogService;
 
    @RequestMapping("/api/idcode!execute.action")
    public Object execute(HttpServletRequest request) {
        ResultObject resultObject = new ResultObject();
        try {
            // 邮件或手机号
            String target = request.getParameter("target");
            if (StringUtils.isNullOrEmpty(target)) {
                throw new BusinessException("不能为空");
            }
            identifyingCodeService.send(target, this.getIp());
        } catch (BusinessException e) {
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
        }
        return resultObject;
    }
 
    @RequestMapping("/api/rechargeCallback!execute.action")
    public void rechargeCallback(HttpServletRequest request) {
        try {
            System.out.println("支付回调start...");
//            Map<String, String> paramsMap = SafeUtils.convertRequestParamsToMap(request);
            //将异步通知中收到的待验证所有参数都存放到map中
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = request.getReader();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            String json = sb.toString();
            JSONObject jsonObject = JSONObject.parseObject(json);
            Map<String, String> paramsMap = new HashMap<String,String>();
            if(jsonObject.containsKey("recharge_hash")){
                paramsMap.put("recharge_hash",jsonObject.getString("recharge_hash"));
            }
            if(jsonObject.containsKey("user_id")){
                paramsMap.put("user_id",jsonObject.getString("user_id"));
            }
            if(jsonObject.containsKey("from_address")){
                paramsMap.put("from_address",jsonObject.getString("from_address"));
            }
            if(jsonObject.containsKey("address")){
                paramsMap.put("address",jsonObject.getString("address"));
            }
            if(jsonObject.containsKey("num")){
                paramsMap.put("num",jsonObject.getString("num"));
            }
 
            if(jsonObject.containsKey("confirmations")){
                paramsMap.put("confirmations",jsonObject.getString("confirmations"));
            }
            if(jsonObject.containsKey("currency")){
                paramsMap.put("currency",jsonObject.getString("currency"));
            }
            System.out.println("响应字符串为paramsMap:"+paramsMap);
 
            if(null != paramsMap && null != paramsMap.get("recharge_hash")){
                JdbcTemplate jdbcTemplate=ApplicationUtil.getBean(JdbcTemplate.class);
                Integer count = jdbcTemplate.queryForObject("SELECT count(ID) FROM PAT_HASH WHERE HASH=? AND status = 1", Integer.class,paramsMap.get("recharge_hash"));
                System.out.println("支付回调hashcount.."+count);
                if(count == 0){
                    //币种
                    String currency = (String) paramsMap.get("currency");
 
                    System.out.println("支付回调currency..."+currency);
                    //数量
                    String num = paramsMap.get("num").toString();
                    System.out.println("支付回调num..."+num);
                    //充值地址
                    String address = (String) paramsMap.get("address");
                    //来源地址
                    String from = (String) paramsMap.get("from_address");
 
                    Double doubleNum = Double.parseDouble(num);
                    System.out.println("支付回调doubleNum..."+doubleNum);
                    String usercode = paramsMap.get("user_id").toString();
                    System.out.println("支付回调usercode..."+usercode);
                    String partyId = jdbcTemplate.queryForObject("SELECT UUID FROM PAT_PARTY WHERE USERCODE=?", String.class,usercode);
                    System.out.println("支付回调partyId..."+partyId);
                    Party party = this.partyService.cachePartyBy(partyId, false);
 
                    double amount_before = 0;
                    double amount_after = 0;
                    String wallettype = "";
                    String symbol = "";
                    if("TRC20-USDT".equals(currency) || "ERC20-USDT".equals(currency)){
                        if("TRC20-USDT".equals(currency)){
                            symbol = "TRC20";
                        }else{
                            symbol = "ERC20";
                        }
                        // 交易所修改usdt
                        Wallet wallet = this.walletService.saveWalletByPartyId(partyId);
                        amount_before = wallet.getMoney();
                        System.out.println("交易所修改usdt  amount_before"+amount_before);
                        // 更新金额
                        this.walletService.update(wallet.getPartyId().toString(), doubleNum);
                        amount_after = Arith.add(wallet.getMoney(), doubleNum);
                        System.out.println("交易所修改usdt  amount_after"+amount_after);
                        System.out.println("symbol  symbol"+symbol);
                        wallettype = "USDT";
                    }else{
                        currency = currency.toLowerCase();
                        WalletExtend walletExtend = this.walletService.saveExtendByPara(partyId, currency);
                        amount_before = walletExtend.getAmount();
                        this.walletService.updateExtend(walletExtend.getPartyId().toString(), currency, doubleNum);
                        amount_after = Arith.add(walletExtend.getAmount(), doubleNum);
                        wallettype = currency;
                        symbol = currency;
                    }
                    String uuid = UUID.randomUUID().toString();
                    String uuidReplace = uuid.replace("-", "");
                    System.out.println("存储充值申请订单记录uuid" + uuidReplace);
                    String insertSql = "INSERT INTO T_RECHARGE_BLOCKCHAIN_ORDER(UUID,ORDER_NO,PARTY_ID,SUCCEEDED,CREATED,COIN,DESCRIPTION,BLOCKCHAIN_NAME,IMG,CHANNEL_AMOUNT,ADDRESS,CHANNEL_ADDRESS,REVIEWTIME,TX) VALUES(?,?,?,1,?,?,'',?,'',?,?,?,?,?)";
                    jdbcTemplate.update(insertSql
                            ,uuidReplace,"R"+System.currentTimeMillis(),
                            partyId,new Date(),wallettype,symbol,
                            doubleNum,from,address,new Date(),paramsMap.get("recharge_hash"));
 
                    //             recharge.setChannel_address(channel_address);
                    //             recharge.setTx(StringUtils.isEmptyString(tx) ? "" : tx);
                    // 账变日志
                    MoneyLog moneyLog = new MoneyLog();
                    moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_RECHARGE);
                    moneyLog.setAmount_before(amount_before);
                    moneyLog.setAmount(doubleNum);
                    moneyLog.setAmount_after(amount_after);
                    moneyLog.setPartyId(partyId);
                    moneyLog.setLog("充值到账");
                    moneyLog.setWallettype(Constants.WALLET);
                    moneyLog.setContent_type(Constants.MONEYLOG_CONTENT_RECHARGE);
                    this.moneyLogService.save(moneyLog);
                    System.out.println("支付回调doubleNum..."+doubleNum);
                    // 钱包日志
                    WalletLog walletLog = new WalletLog();
                    walletLog.setCategory(Constants.MONEYLOG_CATEGORY_RECHARGE);
                    walletLog.setPartyId(partyId);
                    walletLog.setOrder_no("R"+ System.currentTimeMillis());
                    walletLog.setStatus(1);
                    walletLog.setAmount(doubleNum);
                    walletLog.setWallettype(wallettype);
                    this.walletLogService.save(walletLog);
                    jdbcTemplate.update("INSERT INTO PAT_HASH(HASH,STATUS) VALUES(?,?)",paramsMap.get("recharge_hash"),1);
                }
            }
 
        } catch (BusinessException e) {
            System.out.println("出异常了");
            System.out.println(e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    @RequestMapping("/api/withdrawCallback!execute.action")
    public void withdrawCallback(HttpServletRequest request) {
        try {
            System.out.println("提现回调start...");
            //将异步通知中收到的待验证所有参数都存放到map中
            Map<String, String> paramsMap = SafeUtils.convertRequestParamsToMap(request);
            System.out.println("响应字符串为paramsMap:"+paramsMap);
            if(null != paramsMap && null != paramsMap.get("withdraw_hash")){
                JdbcTemplate jdbcTemplate = ApplicationUtil.getBean(JdbcTemplate.class);
                String withdrawHash = (String) paramsMap.get("withdraw_hash");
                Integer count = jdbcTemplate.queryForObject("SELECT count(ID) FROM PAT_HASH WHERE HASH=? AND status = 1", Integer.class,withdrawHash);
                System.out.println("提现回调hashcount.."+count);
                if(count == 0){
                    //2:提币成功 3:提币失败
                    String status = (String) paramsMap.get("status");
                    System.out.println("提现回调status.."+status);
                    System.out.println("2".equals(status));
                    
                    if("2".equals(status)){
                        String partyId = jdbcTemplate.queryForObject("SELECT PARTY_ID FROM T_WITHDRAW_ORDER WHERE WITHDRAW_HASH=?", String.class,withdrawHash);
                        System.out.println("提现回调partyId.."+partyId);
                        Party party = this.partyService.cachePartyBy(partyId, false);
                        
                        jdbcTemplate.update("INSERT INTO PAT_HASH(HASH,STATUS) VALUES(?,?)",withdrawHash,1);
                        
                        jdbcTemplate.update("UPDATE T_WITHDRAW_ORDER SET SUCCEEDED = 1 WHERE  WITHDRAW_HASH = ?",withdrawHash);
                    }
                }
            }
 
        } catch (BusinessException e) {
        }
    }
 
}