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