| | |
| | | package com.xcong.excoin.modules.yunding.service.Impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.xcong.excoin.common.LoginUserUtils; |
| | | import com.xcong.excoin.common.response.Result; |
| | | import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao; |
| | | import com.xcong.excoin.modules.member.entity.MemberEntity; |
| | | import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity; |
| | | import com.xcong.excoin.modules.yunding.dao.YdBasicSettingDao; |
| | | import com.xcong.excoin.modules.yunding.dao.YdOrderDao; |
| | | import com.xcong.excoin.modules.yunding.dao.YdProductDao; |
| | | import com.xcong.excoin.modules.yunding.dto.InsureChangeUsdtDto; |
| | | import com.xcong.excoin.modules.yunding.dto.PayProductDto; |
| | | import com.xcong.excoin.modules.yunding.dto.YdOrderListDto; |
| | | import com.xcong.excoin.modules.yunding.dto.YdProductListDto; |
| | | import com.xcong.excoin.modules.yunding.entity.YdBasicSettingEntity; |
| | | import com.xcong.excoin.modules.yunding.entity.YdOrderEntity; |
| | | import com.xcong.excoin.modules.yunding.entity.YdProductEntity; |
| | | import com.xcong.excoin.modules.yunding.service.YunDingService; |
| | | import com.xcong.excoin.modules.yunding.vo.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.text.DecimalFormat; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class YunDingServiceImpl extends ServiceImpl<YdOrderDao, YdOrderEntity> implements YunDingService { |
| | | |
| | | @Resource |
| | | private YdProductDao ydProductDao; |
| | | @Resource |
| | | private YdBasicSettingDao ydBasicSettingDao; |
| | | @Resource |
| | | private YdOrderDao ydOrderDao; |
| | | @Resource |
| | | private MemberWalletCoinDao memberWalletCoinDao; |
| | | |
| | | @Override |
| | | public Result findAllInfo() { |
| | | log.info("全网数据"); |
| | | YdBasicSettingVo ydBasicSettingVo = new YdBasicSettingVo(); |
| | | Wrapper<YdBasicSettingEntity> queryWrapper = new QueryWrapper<>(); |
| | | List<YdBasicSettingEntity> ydBasicSettingEntities = ydBasicSettingDao.selectList(queryWrapper); |
| | | if(CollUtil.isNotEmpty(ydBasicSettingEntities)){ |
| | | // 不足四位小数补0 |
| | | DecimalFormat decimalFormat = new DecimalFormat("0.0000#"); |
| | | YdBasicSettingEntity ydBasicSettingEntity = ydBasicSettingEntities.get(0); |
| | | ydBasicSettingVo.setAllPower(ydBasicSettingEntity.getAllPower() == null ? decimalFormat.format(BigDecimal.ZERO) : decimalFormat.format(ydBasicSettingEntity.getAllPower())); |
| | | ydBasicSettingVo.setProfitDay(ydBasicSettingEntity.getProfitDay() == null ?decimalFormat.format(BigDecimal.ZERO) : decimalFormat.format(ydBasicSettingEntity.getProfitDay())); |
| | | ydBasicSettingVo.setPowerModel(ydBasicSettingEntity.getPowerModel() == null ? "" : ydBasicSettingEntity.getPowerModel()); |
| | | ydBasicSettingVo.setExplosiveBlock(ydBasicSettingEntity.getExplosiveBlock() == null ?decimalFormat.format(BigDecimal.ZERO) : decimalFormat.format(ydBasicSettingEntity.getExplosiveBlock())); |
| | | } |
| | | return Result.ok(ydBasicSettingVo); |
| | | } |
| | | |
| | | @Override |
| | | public Result getProductList(YdProductListDto ydProductListDto) { |
| | | log.info("获取产品列表"); |
| | | Integer typeCoin = ydProductListDto.getTypeCoin(); |
| | | //默认查询现货的产品 |
| | | if(ObjectUtil.isEmpty(typeCoin)){ |
| | | typeCoin = 2; |
| | | } |
| | | //MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | Page<YdProductVo> page = new Page<>(ydProductListDto.getPageNum(), ydProductListDto.getPageSize()); |
| | | IPage<YdProductVo> list = ydProductDao.getProductList(page, ydProductListDto); |
| | | List<YdProductVo> records = list.getRecords(); |
| | | if(CollUtil.isNotEmpty(records)){ |
| | | //预计收益 |
| | | BigDecimal prifitT = BigDecimal.ZERO; |
| | | Wrapper<YdBasicSettingEntity> queryWrapper = new QueryWrapper<>(); |
| | | List<YdBasicSettingEntity> ydBasicSettingEntities = ydBasicSettingDao.selectList(queryWrapper); |
| | | if(CollUtil.isNotEmpty(ydBasicSettingEntities)){ |
| | | prifitT = ydBasicSettingEntities.get(0).getPrifitT(); |
| | | } |
| | | for(YdProductVo ydProductVo : records){ |
| | | ydProductVo.setProfitT(prifitT); |
| | | |
| | | if(ydProductVo.getTotalT().compareTo(BigDecimal.ZERO) < 1) { |
| | | ydProductVo.setStatus(2); |
| | | } else { |
| | | ydProductVo.setStatus(1); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return Result.ok(list); |
| | | } |
| | | |
| | | @Override |
| | | public Result findProductInfoById(Long id) { |
| | | log.info("获取产品详情"); |
| | | YdProductVo ydProductVo = ydProductDao.findProductInfoById(id); |
| | | if (ObjectUtil.isEmpty(ydProductVo)) { |
| | | return Result.fail("产品不存在!"); |
| | | } |
| | | //预计收益 |
| | | Wrapper<YdBasicSettingEntity> queryWrapper = new QueryWrapper<>(); |
| | | List<YdBasicSettingEntity> ydBasicSettingEntities = ydBasicSettingDao.selectList(queryWrapper); |
| | | if(CollUtil.isNotEmpty(ydBasicSettingEntities)){ |
| | | BigDecimal prifitT = ydBasicSettingEntities.get(0).getPrifitT(); |
| | | if(ObjectUtil.isNotEmpty(prifitT)){ |
| | | ydProductVo.setProfitT(prifitT); |
| | | } |
| | | } |
| | | /** |
| | | *产出规则 |
| | | */ |
| | | Date date = DateUtil.date(); |
| | | ydProductVo.setNowTime(date); |
| | | //上架天数 |
| | | Integer shelvesDays = ydProductVo.getShelvesDays(); |
| | | Date workTime = DateUtil.offsetDay(date, shelvesDays); |
| | | ydProductVo.setWorkTime(workTime); |
| | | //服务周期 |
| | | Integer proCycle = ydProductVo.getProCycle(); |
| | | Date endTime = DateUtil.offsetDay(workTime, proCycle); |
| | | ydProductVo.setEndTime(endTime); |
| | | return Result.ok(ydProductVo); |
| | | } |
| | | |
| | | @Override |
| | | public Result getBalance() { |
| | | log.info("获取可用USDT"); |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | Long memberId = memberEntity.getId(); |
| | | // Long memberId = 28L; |
| | | MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, "USDT"); |
| | | BigDecimal availableBalance = memberWalletCoinEntity.getAvailableBalance(); |
| | | return Result.ok(availableBalance); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Result payProduct(PayProductDto payProductDto) { |
| | | log.info("购买,点击支付"); |
| | | /** |
| | | * 获取购买数量和当前价格,计算出支付总数 |
| | | * 去加减币币账户余额,减少产品的剩余数目 |
| | | * 成功后 |
| | | * 生成订单记录 |
| | | * 生成返利记录 |
| | | */ |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | Long memberId = memberEntity.getId(); |
| | | // Long memberId = 28L; |
| | | String tradePassword = payProductDto.getTradePassword(); |
| | | if(StrUtil.isEmpty(tradePassword)){ |
| | | return Result.fail("请输入交易密码"); |
| | | } |
| | | String tradePasswordOwn = memberEntity.getTradePassword(); |
| | | if(StrUtil.isEmpty(tradePasswordOwn)){ |
| | | return Result.fail("请设置交易密码"); |
| | | } |
| | | String tradePasswords = SecureUtil.md5(tradePassword); |
| | | if(!StrUtil.equals(tradePasswordOwn,tradePasswords)){ |
| | | return Result.fail("请输入正确的交易密码"); |
| | | } |
| | | BigDecimal quantity = payProductDto.getQuantity(); |
| | | if(ObjectUtil.isEmpty(quantity)){ |
| | | return Result.fail("请输入购买数量"); |
| | | } |
| | | if(quantity.compareTo(BigDecimal.ZERO) <= 0){ |
| | | return Result.fail("请输入正确的购买数量"); |
| | | } |
| | | Long id = payProductDto.getId(); |
| | | YdProductEntity ydProductEntity = ydProductDao.selectById(id); |
| | | if(ObjectUtil.isEmpty(ydProductEntity)){ |
| | | return Result.fail("请重新选择产品"); |
| | | } |
| | | BigDecimal salePrice = ydProductEntity.getSalePrice(); |
| | | //剩余产品数量 |
| | | BigDecimal surplusT = ydProductEntity.getSurplusT(); |
| | | if(quantity.compareTo(surplusT) > 0){ |
| | | return Result.fail("当前产品数量不足,请联系客服"); |
| | | } |
| | | //支付总数 |
| | | BigDecimal multiplyUsdt = quantity.multiply(salePrice); |
| | | if(multiplyUsdt.compareTo(BigDecimal.ZERO) <= 0){ |
| | | return Result.fail("支付失败"); |
| | | } |
| | | //去加减币币账户余额,减少产品的剩余数目 |
| | | boolean flag = updateWalletCoin(memberId, multiplyUsdt,id,quantity); |
| | | if(flag == false){ |
| | | return Result.fail("支付失败"); |
| | | } |
| | | //生成订单 |
| | | YdOrderEntity ydOrderEntity = new YdOrderEntity(); |
| | | ydOrderEntity.setMemberId(memberId); |
| | | ydOrderEntity.setProductId(id); |
| | | ydOrderEntity.setQuantity(Integer.parseInt(quantity.toString())); |
| | | ydOrderEntity.setAmount(multiplyUsdt); |
| | | ydOrderEntity.setTotalProfit(BigDecimal.ZERO); |
| | | ydOrderEntity.setTodayProfit(BigDecimal.ZERO); |
| | | ydOrderEntity.setState(YdOrderEntity.ORDER_STATE_READY); |
| | | Date date = DateUtil.date(); |
| | | ydOrderEntity.setBuyTime(date); |
| | | //上架天数 |
| | | Integer shelvesDays = ydProductEntity.getShelvesDays(); |
| | | Date workTime = DateUtil.offsetDay(date, shelvesDays); |
| | | ydOrderEntity.setWorkTime(workTime); |
| | | //服务周期 |
| | | Integer proCycle = ydProductEntity.getProCycle(); |
| | | Date endTime = DateUtil.offsetDay(workTime, proCycle); |
| | | ydOrderEntity.setEndTime(endTime); |
| | | ydOrderEntity.setReturnState(YdOrderEntity.RETURN_STATE_READY); |
| | | ydOrderDao.insert(ydOrderEntity); |
| | | return Result.ok("支付成功"); |
| | | } |
| | | |
| | | @Override |
| | | public Result getOrderList(YdOrderListDto ydOrderListDto) { |
| | | |
| | | log.info("获取订单列表"); |
| | | Integer state = ydOrderListDto.getState(); |
| | | //默认查询待生效 |
| | | if(ObjectUtil.isEmpty(state)){ |
| | | state = 1; |
| | | } |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | Long memberId = memberEntity.getId(); |
| | | //Long memberId = 28L; |
| | | Page<YdOrderVo> page = new Page<>(ydOrderListDto.getPageNum(), ydOrderListDto.getPageSize()); |
| | | YdOrderEntity ydOrderEntity = new YdOrderEntity(); |
| | | ydOrderEntity.setMemberId(memberId); |
| | | ydOrderEntity.setState(state); |
| | | IPage<YdOrderVo> list = ydOrderDao.getOrderList(page, ydOrderEntity); |
| | | return Result.ok(list); |
| | | } |
| | | |
| | | @Override |
| | | public Result findOrderAllInfo() { |
| | | |
| | | log.info("订单头部数据"); |
| | | |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | Long memberId = memberEntity.getId(); |
| | | // Long memberId = 28L; |
| | | OrderAllInfoVo orderAllInfoVo = new OrderAllInfoVo(); |
| | | DecimalFormat decimalFormat = new DecimalFormat("0.0000#"); |
| | | //历史总收益 |
| | | BigDecimal totalProfitAll = BigDecimal.ZERO; |
| | | QueryWrapper<YdOrderEntity> objectQueryWrapper = new QueryWrapper<>(); |
| | | objectQueryWrapper.eq("member_id",memberId); |
| | | List<YdOrderEntity> ydOrderEntities = ydOrderDao.selectList(objectQueryWrapper); |
| | | if(CollUtil.isNotEmpty(ydOrderEntities)){ |
| | | for(YdOrderEntity order : ydOrderEntities){ |
| | | BigDecimal totalProfit = order.getTotalProfit(); |
| | | totalProfitAll = totalProfitAll.add(totalProfit); |
| | | } |
| | | } |
| | | orderAllInfoVo.setAllProfit(decimalFormat.format(totalProfitAll)); |
| | | //当前已生效算力 |
| | | int powerNow = 0; |
| | | QueryWrapper<YdOrderEntity> powerNowQuery = new QueryWrapper<>(); |
| | | powerNowQuery.eq("member_id",memberId); |
| | | powerNowQuery.eq("state",YdOrderEntity.ORDER_STATE_WORK); |
| | | List<YdOrderEntity> PowerNowQuery = ydOrderDao.selectList(powerNowQuery); |
| | | if(CollUtil.isNotEmpty(PowerNowQuery)){ |
| | | for(YdOrderEntity order : PowerNowQuery){ |
| | | int quantity = order.getQuantity(); |
| | | powerNow = powerNow + quantity; |
| | | } |
| | | } |
| | | orderAllInfoVo.setPowerNow(powerNow); |
| | | //当前待生效算力 |
| | | int powerReady = 0; |
| | | QueryWrapper<YdOrderEntity> powerReradyQuery = new QueryWrapper<>(); |
| | | powerReradyQuery.eq("member_id",memberId); |
| | | powerReradyQuery.eq("state",YdOrderEntity.ORDER_STATE_READY); |
| | | List<YdOrderEntity> PowerReradyQuery = ydOrderDao.selectList(powerNowQuery); |
| | | if(CollUtil.isNotEmpty(PowerReradyQuery)){ |
| | | for(YdOrderEntity order : PowerReradyQuery){ |
| | | int quantity = order.getQuantity(); |
| | | powerReady = powerReady + quantity; |
| | | } |
| | | } |
| | | orderAllInfoVo.setPowerReady(powerReady); |
| | | return Result.ok(orderAllInfoVo); |
| | | } |
| | | |
| | | @Override |
| | | public Result getOrderInfo(Long id) { |
| | | |
| | | log.info("获取订单详情"); |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | Long memberId = memberEntity.getId(); |
| | | YdOrderVo ydOrderVo = ydOrderDao.selectOrderByMemberIdAndId(memberId,id); |
| | | return Result.ok(ydOrderVo); |
| | | } |
| | | |
| | | @Override |
| | | public Result changeUsdt() { |
| | | log.info("转换成USDT"); |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | Long memberId = memberEntity.getId(); |
| | | ChangeVo changeVo = new ChangeVo(); |
| | | BigDecimal currentPrice = BigDecimal.ZERO; |
| | | Wrapper<YdBasicSettingEntity> queryWrapper = new QueryWrapper<>(); |
| | | List<YdBasicSettingEntity> ydBasicSettingEntities = ydBasicSettingDao.selectList(queryWrapper); |
| | | if(CollUtil.isNotEmpty(ydBasicSettingEntities)){ |
| | | YdBasicSettingEntity ydBasicSettingEntity = ydBasicSettingEntities.get(0); |
| | | if(ObjectUtil.isNotEmpty(ydBasicSettingEntity)){ |
| | | //获取当前价 |
| | | currentPrice = ydBasicSettingEntity.getCurrentPrice(); |
| | | if(ObjectUtil.isNotEmpty(currentPrice)){ |
| | | changeVo.setCurrentPrice(currentPrice); |
| | | } |
| | | BigDecimal changeXch = ydBasicSettingEntity.getChangeXch(); |
| | | if(ObjectUtil.isNotEmpty(changeXch)){ |
| | | changeVo.setChangeXchRadio(changeXch); |
| | | } |
| | | } |
| | | } |
| | | //获取可用 |
| | | MemberWalletCoinEntity xch = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, "XCH"); |
| | | if(ObjectUtil.isNotEmpty(xch)){ |
| | | BigDecimal availableBalance = xch.getAvailableBalance(); |
| | | changeVo.setXchAvailableBalance(availableBalance.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO:availableBalance); |
| | | } |
| | | return Result.ok(changeVo); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Result insureChangeUsdt(InsureChangeUsdtDto insureChangeUsdtdto) { |
| | | log.info("转换成USDT"); |
| | | MemberEntity memberEntity = LoginUserUtils.getAppLoginUser(); |
| | | Long memberId = memberEntity.getId(); |
| | | /** |
| | | * 验证入参 |
| | | * |
| | | * USDT账户增加 |
| | | * XCH账户减少 |
| | | */ |
| | | BigDecimal xchNum = insureChangeUsdtdto.getXchNum(); |
| | | if(BigDecimal.ZERO.compareTo(xchNum) >= 0){ |
| | | return Result.fail("请输入正确的数量"); |
| | | } |
| | | BigDecimal currentPrice = insureChangeUsdtdto.getCurrentPrice(); |
| | | if(BigDecimal.ZERO.compareTo(currentPrice) >= 0){ |
| | | return Result.fail("XCH价格异常,请重试"); |
| | | } |
| | | MemberWalletCoinEntity xch = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, "XCH"); |
| | | BigDecimal totalBalance = xch.getTotalBalance(); |
| | | BigDecimal subtractTotalBalance = totalBalance.subtract(xchNum); |
| | | if(BigDecimal.ZERO.compareTo(subtractTotalBalance) >= 0){ |
| | | return Result.fail("XCH数量不足"); |
| | | } |
| | | |
| | | BigDecimal availableBalance = xch.getAvailableBalance(); |
| | | BigDecimal subtractAvailableBalance = availableBalance.subtract(xchNum); |
| | | if(BigDecimal.ZERO.compareTo(subtractAvailableBalance) >= 0){ |
| | | return Result.fail("XCH数量不足"); |
| | | } |
| | | //xch减少 |
| | | xch.setTotalBalance(subtractTotalBalance); |
| | | xch.setAvailableBalance(subtractAvailableBalance); |
| | | memberWalletCoinDao.updateById(xch); |
| | | |
| | | //USDT账户增加 |
| | | BigDecimal multiply = xchNum.multiply(currentPrice); |
| | | MemberWalletCoinEntity usdt = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, "USDT"); |
| | | usdt.setTotalBalance(usdt.getTotalBalance().add(multiply)); |
| | | usdt.setAvailableBalance(usdt.getAvailableBalance().add(multiply)); |
| | | memberWalletCoinDao.updateById(usdt); |
| | | return Result.ok("操作成功"); |
| | | } |
| | | |
| | | /** |
| | | * 去加减币币账户余额,减少产品的剩余数目 |
| | | * @param memberId |
| | | * @param multiplyUsdt |
| | | * @param productId |
| | | * @param quantity |
| | | * @return |
| | | */ |
| | | public synchronized boolean updateWalletCoin(Long memberId,BigDecimal multiplyUsdt,Long productId,BigDecimal quantity){ |
| | | MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, "USDT"); |
| | | BigDecimal availableBalance = memberWalletCoinEntity.getAvailableBalance(); |
| | | BigDecimal totalBalance = memberWalletCoinEntity.getTotalBalance(); |
| | | BigDecimal availableSubtract = availableBalance.subtract(multiplyUsdt); |
| | | BigDecimal totalSubtract = totalBalance.subtract(multiplyUsdt); |
| | | boolean flag = false; |
| | | if(availableSubtract.compareTo(BigDecimal.ZERO) >= 0){ |
| | | //更新余额 |
| | | memberWalletCoinEntity.setAvailableBalance(availableSubtract); |
| | | memberWalletCoinEntity.setTotalBalance(totalSubtract); |
| | | memberWalletCoinDao.updateById(memberWalletCoinEntity); |
| | | //减少产品的剩余数量 |
| | | YdProductEntity ydProductEntity = ydProductDao.selectById(productId); |
| | | BigDecimal surplusT = ydProductEntity.getSurplusT(); |
| | | BigDecimal subtract = surplusT.subtract(quantity); |
| | | ydProductEntity.setSurplusT(subtract); |
| | | ydProductDao.updateById(ydProductEntity); |
| | | flag = true; |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | } |