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)));
|
}
|
|
}
|