peternameyakj
2024-08-29 ca8e89d46a92b567aa9af2c32ea2951d1e60ca6c
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
package org.example.ssmico.demos.web.tesk;
 
import cn.hutool.core.date.DateUtil;
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.core.toolkit.CollectionUtils;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ThreadUtils;
import org.example.ssmico.demos.web.entity.*;
import org.example.ssmico.demos.web.mapper.ExchangeApplyOrderMapper;
import org.example.ssmico.demos.web.mapper.PartyMapper;
import org.example.ssmico.demos.web.mapper.WalletExtendMapper;
import org.example.ssmico.demos.web.service.IcoNewCurrencySerivce;
import org.example.ssmico.demos.web.service.IcoOrderService;
import org.example.ssmico.demos.web.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import project.wallet.WalletExtend;
 
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * @program: ssm-ico
 * @description:
 * @create: 2024-04-18 17:32
 **/
@Slf4j
@Component
public class NewCurrencyListingTask {
 
    @Autowired
    private IcoOrderService icoOrderService;
 
    @Autowired
    private IcoNewCurrencySerivce icoNewCurrencySerivce;
 
    @Autowired
    private ExchangeApplyOrderMapper exchangeApplyOrderMapper;
 
    @Autowired
    private WalletExtendMapper walletExtendMapper;
 
    private final Lock lock = new ReentrantLock();
 
    /**
     * 上市冻结转资产
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void icoListing() {
        if (lock.tryLock()) {
            log.info("开始执行上市冻结转资产任务");
            try {
                listingThaw();
            } catch (Exception e) {
                log.error("上市冻结转资产任务执行失败:" + e.getMessage(), e);
            } finally {
                lock.unlock();
            }
        } else {
            log.info("上一次上市冻结转资产任务尚未完成,本次任务取消");
        }
    }
 
    @Transactional
    public void listingThaw(){
        List<IcoNewCurrency> icoNewCurrencyList = findICOListings();
        if(CollectionUtils.isNotEmpty(icoNewCurrencyList)){
            List<Integer> ids = icoNewCurrencyList.stream().map(IcoNewCurrency::getId)
                    .collect(Collectors.toList());
            List<IcoOrder> icoOrderList = findOrdersByCurrencyIds(ids);
            if (!icoOrderList.isEmpty()) {
                icoOrderList.forEach(order -> {
                    WalletExtend walletExtend = walletExtendMapper.selectOne(
                            new LambdaQueryWrapper<WalletExtend>()
                            .eq(WalletExtend::getPartyId, order.getUserId())
                            .eq(WalletExtend::getWallettype, order.getTokenCode()));
                    if(null != walletExtend){
                        walletExtend.setAmount(walletExtend.getAmount()+order.getLotteryQuantity());
                        if(walletExtend.getFrozenAmount()<order.getLotteryQuantity()){
                            walletExtend.setFrozenAmount(0);
                        }else{
                            walletExtend.setFrozenAmount(walletExtend.getFrozenAmount()-order.getLotteryQuantity());
                        }
//
                        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);
                        order.setStatus(5); // 更新订单状态
                        icoOrderService.updateById(order);
                    }
                });
            }
        }
    }
 
    private List<IcoNewCurrency> findICOListings() {
        return icoNewCurrencySerivce.list(new LambdaQueryWrapper<IcoNewCurrency>()
                .lt(IcoNewCurrency::getListingTime, new Date()));
    }
 
    private List<IcoOrder> findOrdersByCurrencyIds(List<Integer> ids) {
        return icoOrderService.list(new LambdaQueryWrapper<IcoOrder>()
                .in(IcoOrder::getIcoNewCurrencyId, ids).and(a->
                        a.eq(IcoOrder::getStatus, 3).ne(IcoOrder::getStatus, 4)));
    }
 
}