zj
2024-06-03 5668aa7dc743d930b4b09dce1e7ccbba371e93cf
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
package org.example.ssmico.demos.web.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.ssmico.demos.web.entity.*;
import org.example.ssmico.demos.web.mapper.IcoNewCurrencyMapper;
import org.example.ssmico.demos.web.mapper.IcoOrderMapper;
import org.example.ssmico.demos.web.mapper.WalletExtendMapper;
import org.example.ssmico.demos.web.service.IcoOrderService;
import org.example.ssmico.demos.web.service.TMoneyLogService;
import org.example.ssmico.demos.web.service.TWalletService;
import org.example.ssmico.demos.web.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import project.wallet.Wallet;
import project.wallet.WalletExtend;
 
import java.util.Date;
import java.util.UUID;
 
/**
 * @program: ssh
 * @description:
 * @create: 2024-04-11 13:02
 **/
@Service
public class IcoOrderServiceImpl  extends ServiceImpl<IcoOrderMapper, IcoOrder> implements IcoOrderService {
 
    @Autowired
    private IcoNewCurrencyMapper mapper;
 
    @Autowired
    private TWalletService tWalletService;
 
    @Autowired
    private TMoneyLogService tMoneyLogService;
 
    @Autowired
    private WalletExtendMapper walletExtendMapper;
 
    @Override
    @Transactional
    public ResultObject subscribe(Integer id, String lang) throws Exception {
        ResultObject resultObject = new ResultObject();
        GoogleTranslateUtil googleTranslateUtil = new GoogleTranslateUtil();
        IcoOrder icoOrder = getById(id);
        if(null == icoOrder){
            resultObject.setCode("1");
            resultObject.setMsg(googleTranslateUtil.translate("当前订单不存在",lang));
            return resultObject;
        }
 
        Wallet tWallet = tWalletService.getOne(new LambdaQueryWrapper<Wallet>().eq(Wallet::getPartyId, icoOrder.getUserId()).last(" limit 1"));
        if(null == tWallet){
            resultObject.setCode("1");
            resultObject.setMsg(googleTranslateUtil.translate("用户资金不存在",lang));
            return resultObject;
        }
 
        IcoNewCurrency icoNewCurrency = mapper.selectById(icoOrder.getIcoNewCurrencyId());
 
        //中签总价
        Double totalPlacingPrice = icoNewCurrency.getIssuePrice()*icoOrder.getLotteryQuantity();
        if(tWallet.getMoney() < totalPlacingPrice){
            resultObject.setCode("1");
            resultObject.setMsg(googleTranslateUtil.translate("余额不足",lang));
            return resultObject;
        }
 
        double money = tWallet.getMoney();
 
        tWallet.setMoney(tWallet.getMoney()-totalPlacingPrice);
        tWalletService.updateById(tWallet);
        String jsonString = JSON.toJSONString(tWallet, SerializerFeature.WriteClassName);
        RedisShardedPoolUtils.set( WalletRedisKeys.WALLET_PARTY_ID + tWallet.getPartyId().toString(),jsonString);
        TMoneyLog tMoneyLog = new TMoneyLog();
        String msg = "ICO认缴,订单id"+icoOrder.getId();
        tMoneyLog.setUuid(UUID.randomUUID().toString().replace("-", ""));
        tMoneyLog.setLog(msg);
        tMoneyLog.setWallettype("USDT");
        tMoneyLog.setPartyId(icoOrder.getUserId());
        tMoneyLog.setAmount(totalPlacingPrice);
        tMoneyLog.setAmountBefore(money);
        tMoneyLog.setAmountAfter(tWallet.getMoney());
        tMoneyLog.setContentType("ICO");
        tMoneyLog.setCategory("认缴");
        tMoneyLog.setCreateTime(new Date());
        tMoneyLogService.save(tMoneyLog);
 
        WalletExtend walletExtend = walletExtendMapper.selectOne(new LambdaQueryWrapper<WalletExtend>()
                .eq(WalletExtend::getPartyId, icoOrder.getUserId())
                .eq(WalletExtend::getWallettype, icoOrder.getTokenCode()));
        if(null != walletExtend){
            double mount = walletExtend.getFrozenAmount() + icoOrder.getLotteryQuantity();
            System.out.println("认缴叠加-----"+mount);
            walletExtend.setFrozenAmount(mount);
        }else{
            walletExtend = new WalletExtend();
            walletExtend.setUuid(ApplicationUtil.getCurrentTimeUUID());
            walletExtend.setPartyId(icoOrder.getUserId());
            walletExtend.setWallettype(icoOrder.getTokenCode());
            walletExtend.setFrozenAmount(icoOrder.getLotteryQuantity());
            walletExtendMapper.insert(walletExtend);
            String json = JSON.toJSONString(walletExtend, SerializerFeature.WriteClassName);
            RedisShardedPoolUtils.set(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + walletExtend.getPartyId() + walletExtend.getWallettype(), json);
        }
        String json = JSON.toJSONString(walletExtend, SerializerFeature.WriteClassName);
        walletExtendMapper.update(walletExtend,new LambdaUpdateWrapper<WalletExtend>().eq(WalletExtend::getUuid,walletExtend.getUuid()));
        RedisShardedPoolUtils.set(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + walletExtend.getPartyId() + walletExtend.getWallettype(),json);
 
        icoOrder.setStatus(3);
        icoOrder.setIsCompleted(1);
        updateById(icoOrder);
        return resultObject;
    }
 
    @Override
    @Transactional
    public ResultObject add(IcoOrder icoOrder,String lang) throws Exception {
        GoogleTranslateUtil googleTranslateUtil = new GoogleTranslateUtil();
        ResultObject resultObject = new ResultObject();
 
        //判断是否在申购时间
        IcoNewCurrency icoNewCurrency = mapper.selectById( icoOrder.getIcoNewCurrencyId());
//        if(System.currentTimeMillis() < icoNewCurrency.getApplicationStartTime().getTime()){
//            resultObject.setCode("1");
//            resultObject.setMsg(googleTranslateUtil.translate("未到申购时间",lang));
//            return resultObject;
//        }else if(System.currentTimeMillis() > icoNewCurrency.getApplicationEndTime().getTime()){
//            resultObject.setCode("1");
//            resultObject.setMsg(googleTranslateUtil.translate("已过申购时间",lang));
//            return resultObject;
//        }
        icoOrder.setNewCoinName(icoNewCurrency.getNewCoinName());
        if(icoOrder.getOrderType() == 2){
            Wallet tWallet = tWalletService.getOne(new LambdaQueryWrapper<Wallet>().eq(Wallet::getPartyId, icoOrder.getUserId()).last(" limit 1 "));
            if(null == tWallet){
                resultObject.setCode("1");
                resultObject.setMsg(googleTranslateUtil.translate("资金账户不存在",lang));
                return resultObject;
            }
            //中签总价
            Double totalPlacingPrice = icoNewCurrency.getIssuePrice()*icoOrder.getLotteryQuantity();
            if(tWallet.getMoney() < totalPlacingPrice){
                resultObject.setCode("1");
                resultObject.setMsg(googleTranslateUtil.translate("余额不足",lang));
                return resultObject;
            }
            double amountafter = tWallet.getMoney();
 
            tWallet.setMoney(tWallet.getMoney()-totalPlacingPrice);
            tWalletService.update(tWallet,new LambdaUpdateWrapper<Wallet>().eq(Wallet::getUuid,tWallet.getUuid()));
            String jsonString = JSON.toJSONString(tWallet, SerializerFeature.WriteClassName);
            RedisShardedPoolUtils.set(WalletRedisKeys.WALLET_PARTY_ID + tWallet.getPartyId().toString(), jsonString);
 
            icoOrder.setStatus(1);
            icoOrder.setIsCompleted(1);
            save(icoOrder);
            TMoneyLog tMoneyLog = new TMoneyLog();
            String msg = "ICO配售,订单id"+icoOrder.getId();
            tMoneyLog.setUuid(UUID.randomUUID().toString().replace("-", ""));
            tMoneyLog.setLog(msg);
            tMoneyLog.setWallettype("USDT");
            tMoneyLog.setPartyId(icoOrder.getUserId());
            tMoneyLog.setAmount(totalPlacingPrice);
            tMoneyLog.setAmountBefore(amountafter);
            tMoneyLog.setAmountAfter(tWallet.getMoney());
            tMoneyLog.setContentType("ICO");
            tMoneyLog.setCategory("配售");
            tMoneyLog.setCreateTime(new Date());
            tMoneyLogService.save(tMoneyLog);
            return resultObject;
        }else{
            icoOrder.setStatus(1);
            icoOrder.setIsCompleted(0);
        }
 
        long count = count(new LambdaQueryWrapper<IcoOrder>()
                .eq(IcoOrder::getIcoNewCurrencyId, icoOrder.getIcoNewCurrencyId())
                .eq(IcoOrder::getUserId,icoOrder.getUserId()));
        if (count > 0) {
            resultObject.setCode("1");
            resultObject.setMsg(googleTranslateUtil.translate("申购单已存在,请勿重复申购",lang));
            return resultObject;
        }
        save(icoOrder);
        return resultObject;
    }
 
 
}