package org.example.ssmico.demos.web.controller; 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.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; import org.example.ssmico.demos.web.entity.*; import org.example.ssmico.demos.web.mapper.*; import org.example.ssmico.demos.web.service.SctUserSerivce; import org.example.ssmico.demos.web.service.impl.IcoOrderServiceImpl; import org.example.ssmico.demos.web.util.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import project.wallet.Wallet; import project.wallet.WalletExtend; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; /** * @program: dabao * @description: ico订单 * @create: 2024-04-10 15:48 **/ @Slf4j @Controller @RequestMapping({"/wap/api/icoOrder"}) @CrossOrigin public class IcoOrderController { @Autowired private IcoOrderServiceImpl service; @Autowired private IcoNewCurrencyMapper icoNewCurrencyMapper; @Autowired private TWalletMapper tWalletMapper; @Autowired private IcoOrderMapper mapper; @Autowired private TMoneyLogMapper tMoneyLogMapper; @Autowired private SctUserSerivce sctUserSerivce; @Autowired private WalletExtendMapper walletExtendMapper; /** * app 申购/配售 */ @ResponseBody @RequestMapping(value = "/appAdd.action", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public ResultObject appAdd(IcoOrder icoOrder, HttpServletRequest request) throws Exception { ResultObject resultObject = new ResultObject(); GoogleTranslateUtil googleTranslateUtil = new GoogleTranslateUtil(); String lang = request.getHeader("lang"); try { String token = request.getParameter("token"); String userId = RedisShardedPoolUtils.get("TOKEN_" + token); // String userId = "232a10b2485531e99713c66bd9ddb4e0"; userId = userId.replace("\"",""); icoOrder.setUserId(userId); resultObject = service.add(icoOrder,lang); } catch (Exception e) { e.printStackTrace(); resultObject.setCode("1"); resultObject.setMsg(googleTranslateUtil.translate("程序错误",lang)); } return resultObject; } /** * app 获取用户申购/配售订单 * * @return */ @ResponseBody @RequestMapping(value = "/getList.action", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public ResultObject getList(Integer orderType, HttpServletRequest request) throws Exception { String lang = request.getHeader("lang"); GoogleTranslateUtil googleTranslateUtil = new GoogleTranslateUtil(); ResultObject resultObject = new ResultObject(); try { String token = request.getParameter("token"); String userId = RedisShardedPoolUtils.get("TOKEN_" + token); // String userId = "232a10b2485531e99713c66bd9ddb4e0"; userId = userId.replace("\"",""); List list = service.list(new LambdaQueryWrapper().eq(IcoOrder::getOrderType, orderType) .eq(IcoOrder::getUserId, userId).orderByDesc(IcoOrder::getCreateTime)); resultObject.setData(list); } catch (Exception e) { resultObject.setCode("1"); resultObject.setMsg(googleTranslateUtil.translate("程序错误",lang)); } return resultObject; } /** * pc 获取用户申购/配售订单 * * @return */ @ResponseBody @RequestMapping(value = "/getPageList.action", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public ServerResponse getPageList(String tokenCode,Integer orderType, String userName, Integer pageNo, Integer size) { if (null == pageNo || null == size) { pageNo = 1; size = 10; } LambdaQueryWrapper userQueryWrapper = new LambdaQueryWrapper(); if(StringUtils.isNotBlank(userName)){ userQueryWrapper.like(SctUser::getUsername,userName); } List userList = sctUserSerivce.list(userQueryWrapper); List ids = userList.stream().map(SctUser::getPartyUuid).collect(Collectors.toList()); Map userMap = userList.stream().collect(Collectors.toMap( SctUser::getPartyUuid, obj -> obj, (key1 , key2) -> key1 )); if(CollectionUtils.isEmpty(ids)){ return ServerResponse.createBySuccess(); } // 创建分页对象 Page page = new Page<>(pageNo, size); // 构造查询条件 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if (StringUtils.isNotBlank(tokenCode)) { queryWrapper.eq(IcoOrder::getTokenCode, tokenCode); } if(null != orderType){ queryWrapper.eq(IcoOrder::getOrderType,orderType); } queryWrapper.in(IcoOrder::getUserId,ids); queryWrapper.orderByDesc(IcoOrder::getCreateTime); // 分页查询数据 Page pageList = service.page(page, queryWrapper); pageList.getRecords().forEach(f->{ SctUser one = userMap.get(f.getUserId()); if(null != one){ f.setUserName(one.getUsername()); } }); return ServerResponse.createBySuccess(pageList); } /** * pc ico认缴 * * @param id * @return */ @ResponseBody @RequestMapping(value = "/subscribe.action", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public ServerResponse subscribe(Integer id){ String lang = "zh-CN"; ResultObject result = new ResultObject(); try { result = service.subscribe(id,lang); if(result.getCode().equals("1")){ return ServerResponse.createByErrorMsg(result.getMsg()); } }catch (Exception e){ e.printStackTrace(); result.setCode("1"); result.setMsg("程序错误"); } return ServerResponse.createBySuccess(); } /** * app ico认缴 * * @param id * @return */ @ResponseBody @RequestMapping(value = "/appSubscribe.action", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public ResultObject appSubscribe(Integer id,HttpServletRequest request) throws Exception { String lang = request.getHeader("lang"); GoogleTranslateUtil googleTranslateUtil = new GoogleTranslateUtil(); ResultObject resultObject = new ResultObject(); try { resultObject = service.subscribe(id, lang); } catch (Exception e) { resultObject.setCode("1"); resultObject.setMsg(googleTranslateUtil.translate("程序错误",lang)); } return resultObject; } /** * pc ico客户中签 * * @return */ @ResponseBody @RequestMapping(value = "/winBallot.action", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") @Transactional public ServerResponse winBallot(IcoOrder icoOrder) { IcoOrder order = mapper.selectById(icoOrder.getId()); if (null == order) { return ServerResponse.createByErrorMsg("当前订单不存在"); } IcoNewCurrency icoNewCurrency = icoNewCurrencyMapper.selectById(order.getIcoNewCurrencyId()); if(new Date().getTime() >= icoNewCurrency.getListingTime().getTime()){ return ServerResponse.createByErrorMsg("该币已经上市不能操作中签"); } //判断是认购还是配售 if(order.getOrderType() == 2){ if(icoOrder.getLotteryQuantity() > order.getLotteryQuantity()){ return ServerResponse.createByErrorMsg("中签数量不能大于用户配售数量"); } Wallet tWallet = tWalletMapper.selectOne(new LambdaQueryWrapper().eq(Wallet::getPartyId, order.getUserId()).last(" limit 1")); if(null == tWallet){ return ServerResponse.createByErrorMsg("资金账户不存在"); } //配售总价 Double totalPlacingPrice = icoNewCurrency.getIssuePrice()*order.getLotteryQuantity(); //中签总价 Double winningTotalPrice = icoNewCurrency.getIssuePrice()*icoOrder.getLotteryQuantity(); Double subtract = totalPlacingPrice-winningTotalPrice;; tWallet.setMoney(tWallet.getMoney()+subtract); tWalletMapper.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(order.getUserId()); tMoneyLog.setAmount(totalPlacingPrice); tMoneyLog.setAmountBefore(totalPlacingPrice); tMoneyLog.setAmountAfter(tWallet.getMoney()); tMoneyLog.setContentType("ICO"); tMoneyLog.setCategory("中签"); tMoneyLog.setCreateTime(new Date()); tMoneyLogMapper.insert(tMoneyLog); order.setStatus(3); order.setLotteryQuantity(icoOrder.getLotteryQuantity()); WalletExtend walletExtend = walletExtendMapper.selectOne(new LambdaQueryWrapper() .eq(WalletExtend::getPartyId, order.getUserId()) .eq(WalletExtend::getWallettype, order.getTokenCode())); if(null != walletExtend){ System.out.println("=--------->"+walletExtend+"-----"+icoOrder.getLotteryQuantity()); double mount = walletExtend.getFrozenAmount() + icoOrder.getLotteryQuantity(); System.out.println("中签叠加---->"+mount); walletExtend.setFrozenAmount(mount); }else{ walletExtend = new WalletExtend(); walletExtend.setUuid(ApplicationUtil.getCurrentTimeUUID()); walletExtend.setPartyId(order.getUserId()); walletExtend.setWallettype(order.getTokenCode()); walletExtend.setFrozenAmount(order.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); }else{ order.setStatus(2); order.setLotteryQuantity(icoOrder.getLotteryQuantity()); } service.updateById(order); return ServerResponse.createBySuccess(); } }