From a8ed6c8a55a3c594716a6ff02d5997c4bbbad09a Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Wed, 23 Feb 2022 11:36:50 +0800
Subject: [PATCH] 20222223

---
 src/main/java/com/xcong/excoin/modules/coin/controller/CoinController.java    |   15 ++-
 src/main/java/com/xcong/excoin/modules/coin/parameter/dto/ZhiYaGusdDto.java   |   17 ++++
 src/main/resources/mapper/walletCoinOrder/ZhiYaRecordDao.xml                  |   14 +++
 src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java |   95 ++++++++++++++++++++++-
 src/main/java/com/xcong/excoin/modules/coin/entity/ZhiYaRecordEntity.java     |   47 +++++++++++
 src/main/java/com/xcong/excoin/modules/coin/service/CoinService.java          |    2 
 src/main/java/com/xcong/excoin/modules/coin/dao/ZhiYaRecordDao.java           |   13 +++
 7 files changed, 193 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/coin/controller/CoinController.java b/src/main/java/com/xcong/excoin/modules/coin/controller/CoinController.java
index a1d9f64..12e1d8a 100644
--- a/src/main/java/com/xcong/excoin/modules/coin/controller/CoinController.java
+++ b/src/main/java/com/xcong/excoin/modules/coin/controller/CoinController.java
@@ -5,7 +5,7 @@
 import javax.annotation.Resource;
 import javax.validation.Valid;
 
-import com.xcong.excoin.modules.coin.parameter.dto.UsdtToGusdDto;
+import com.xcong.excoin.modules.coin.parameter.dto.*;
 import com.xcong.excoin.modules.coin.parameter.vo.AllWalletCoinVo;
 import com.xcong.excoin.modules.coin.parameter.vo.MemberAccountMoneyChangeInfoVo;
 import com.xcong.excoin.modules.coin.parameter.vo.MemberAgentIntoInfoVo;
@@ -22,9 +22,6 @@
 import org.springframework.web.bind.annotation.RestController;
 
 import com.xcong.excoin.common.response.Result;
-import com.xcong.excoin.modules.coin.parameter.dto.RecordsPageDto;
-import com.xcong.excoin.modules.coin.parameter.dto.TransferOfBalanceDto;
-import com.xcong.excoin.modules.coin.parameter.dto.TransferOfBalanceFromAgentDto;
 import com.xcong.excoin.modules.coin.service.CoinService;
 
 import lombok.extern.slf4j.Slf4j;
@@ -210,6 +207,16 @@
 		Integer transfertype = usdtToGusdDto.getTransfertype();
 		return coinService.usdtToGusd(balance,transfertype);
 	}
+
+	/**
+	 * 质押GUSD
+	 */
+	@ApiOperation(value="质押GUSD", notes="质押GUSD")
+	@PostMapping(value="/zhiYaGusd")
+	public Result  zhiYaGusd(@RequestBody @Valid ZhiYaGusdDto zhiYaGusdDto) {
+		BigDecimal balance = zhiYaGusdDto.getBalance();
+		return coinService.zhiYaGusd(balance);
+	}
 	
 
 }
diff --git a/src/main/java/com/xcong/excoin/modules/coin/dao/ZhiYaRecordDao.java b/src/main/java/com/xcong/excoin/modules/coin/dao/ZhiYaRecordDao.java
new file mode 100644
index 0000000..45f45be
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/coin/dao/ZhiYaRecordDao.java
@@ -0,0 +1,13 @@
+package com.xcong.excoin.modules.coin.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity;
+import com.xcong.excoin.modules.coin.entity.ZhiYaRecordEntity;
+import org.apache.ibatis.annotations.Param;
+
+public interface ZhiYaRecordDao extends BaseMapper<ZhiYaRecordEntity> {
+
+    long getOrderCountByToday(@Param("now")String now, @Param("tomorrow") String tomorrow);
+
+    ZhiYaRecordEntity selectByMemberIdAndOrderStatus(@Param("memberId")Long memberId,@Param("orderStatus") Integer orderStatus);
+}
diff --git a/src/main/java/com/xcong/excoin/modules/coin/entity/ZhiYaRecordEntity.java b/src/main/java/com/xcong/excoin/modules/coin/entity/ZhiYaRecordEntity.java
new file mode 100644
index 0000000..5ceb658
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/coin/entity/ZhiYaRecordEntity.java
@@ -0,0 +1,47 @@
+package com.xcong.excoin.modules.coin.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.xcong.excoin.common.system.base.BaseEntity;
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+/**
+ * 质押记录表
+ */
+@Data
+@TableName("zhiya_record")
+public class ZhiYaRecordEntity extends BaseEntity {
+    /**
+     *
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 会员ID
+     */
+    private Long memberId;
+    /**
+     * 编号
+     */
+    private String orderNo;
+    /**
+     * 币种
+     */
+    private String symbol;
+    /**
+     * 委托量
+     */
+    private BigDecimal zhiyaCnt;
+    /**
+     * 状态 1:生效中 2:已赎回
+     */
+    private Integer orderStatus;
+    /**
+     * 生效日期
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private Date effectDate;
+}
diff --git a/src/main/java/com/xcong/excoin/modules/coin/parameter/dto/ZhiYaGusdDto.java b/src/main/java/com/xcong/excoin/modules/coin/parameter/dto/ZhiYaGusdDto.java
new file mode 100644
index 0000000..6fd22bb
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/coin/parameter/dto/ZhiYaGusdDto.java
@@ -0,0 +1,17 @@
+package com.xcong.excoin.modules.coin.parameter.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+import java.math.BigDecimal;
+
+@Data
+@ApiModel(value = "ZhiYaGusdDto", description = "参数接收类")
+public class ZhiYaGusdDto {
+
+    @NotNull(message = "划转金额不能为空")
+    @ApiModelProperty(value = "划转金额", example = "100")
+    private BigDecimal balance;
+}
diff --git a/src/main/java/com/xcong/excoin/modules/coin/service/CoinService.java b/src/main/java/com/xcong/excoin/modules/coin/service/CoinService.java
index 6bd555e..a2946bb 100644
--- a/src/main/java/com/xcong/excoin/modules/coin/service/CoinService.java
+++ b/src/main/java/com/xcong/excoin/modules/coin/service/CoinService.java
@@ -43,4 +43,6 @@
 	void updateWalletBalance(@Param("id") Long id, @Param("availableBalance")BigDecimal availableBalance,@Param("totalBalance")BigDecimal totalBalance, @Param("frozenBalance")BigDecimal frozenBalance);
 
 	Result usdtToGusd(BigDecimal balance, Integer transfertype);
+
+    Result zhiYaGusd(BigDecimal balance);
 }
diff --git a/src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java b/src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
index 0b76dfb..7692d4a 100644
--- a/src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
@@ -1,11 +1,14 @@
 package com.xcong.excoin.modules.coin.service.impl;
 
 import java.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.List;
+import java.text.SimpleDateFormat;
+import java.util.*;
 import javax.annotation.Resource;
-import javax.validation.Valid;
 
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.core.date.DateUtil;
+import com.xcong.excoin.modules.coin.dao.ZhiYaRecordDao;
+import com.xcong.excoin.modules.coin.entity.ZhiYaRecordEntity;
 import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -60,6 +63,8 @@
     MemberAccountMoneyChangeDao memberAccountMoneyChangeDao;
     @Resource
     MemberWalletAgentDao memberWalletAgentDao;
+    @Resource
+    ZhiYaRecordDao zhiYaRecordDao;
     @Resource
     RedisUtils redisUtils;
 
@@ -612,11 +617,11 @@
 
         BigDecimal available = availableBalance.subtract(balance);
         if (available.compareTo(BigDecimal.ZERO) < 0) {
-            return Result.fail(MessageSourceUtils.getString("member_service_0008"));
+            return Result.fail(MessageSourceUtils.getString("member_service_0005"));
         }
         BigDecimal total = totalBalance.subtract(balance);
         if (total.compareTo(BigDecimal.ZERO) < 0) {
-            return Result.fail(MessageSourceUtils.getString("member_service_0008"));
+            return Result.fail(MessageSourceUtils.getString("member_service_0005"));
         }
 
         memberWalletCoinEntity.setAvailableBalance(available);
@@ -649,7 +654,85 @@
         memberAccountRecord.setAmount(balance);
         memberAccountMoneyChangeDao.insert(memberAccountRecord);
 
-        return Result.ok(MessageSourceUtils.getString("member_service_0006"));
+        return Result.ok(MessageSourceUtils.getString("member_service_0024"));
+    }
+
+    @Override
+    public Result zhiYaGusd(BigDecimal balance) {
+        //获取用户ID
+        Long memberId = LoginUserUtils.getAppLoginUser().getId();
+        if (balance.compareTo(new BigDecimal(100)) < 0) {
+            return Result.fail(MessageSourceUtils.getString("member_service_0005"));
+        }
+        //质押Gusd,质押数量进入冻结余额,
+        String gusdName = CoinTypeEnum.GUSD.name();
+        MemberWalletCoinEntity gusdMemberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, gusdName);
+        BigDecimal gusdAvailableBalance = gusdMemberWalletCoinEntity.getAvailableBalance();
+        BigDecimal gusdTotalBalance = gusdMemberWalletCoinEntity.getTotalBalance();
+        BigDecimal gusdfrozenBalance = gusdMemberWalletCoinEntity.getFrozenBalance();
+
+        BigDecimal available = gusdAvailableBalance.subtract(balance);
+        if (available.compareTo(BigDecimal.ZERO) < 0) {
+            return Result.fail(MessageSourceUtils.getString("member_service_0005"));
+        }
+        BigDecimal total = gusdTotalBalance.subtract(balance);
+        if (total.compareTo(BigDecimal.ZERO) < 0) {
+            return Result.fail(MessageSourceUtils.getString("member_service_0005"));
+        }
+        gusdMemberWalletCoinEntity.setFrozenBalance(gusdfrozenBalance.add(gusdfrozenBalance));
+        gusdMemberWalletCoinEntity.setAvailableBalance(gusdAvailableBalance.subtract(balance));
+
+        int updateById = memberWalletCoinDao.updateById(gusdMemberWalletCoinEntity);
+        if (updateById < 1) {
+            return Result.fail(MessageSourceUtils.getString("member_service_0095"));
+        }
+        //产生质押单,当前有的话,累加质押数量
+        ZhiYaRecordEntity zhiYaRecord = zhiYaRecordDao.selectByMemberIdAndOrderStatus(memberId,1);
+        if(ObjectUtil.isEmpty(zhiYaRecord)){
+            //产生一条质押记录
+            ZhiYaRecordEntity zhiYaRecordEntity = new ZhiYaRecordEntity();
+            zhiYaRecordEntity.setMemberId(memberId);
+            zhiYaRecordEntity.setOrderNo(generateSimpleSerialno(memberId.toString()));
+            zhiYaRecordEntity.setSymbol(gusdName);
+            zhiYaRecordEntity.setZhiyaCnt(balance);
+            zhiYaRecordEntity.setOrderStatus(1);
+            Date date = new Date();
+            Date newDate = DateUtil.offsetDay(date, 1);
+            zhiYaRecordEntity.setEffectDate(newDate);
+            zhiYaRecordDao.insert(zhiYaRecordEntity);
+        }else{
+            zhiYaRecord.setZhiyaCnt(zhiYaRecord.getZhiyaCnt().add(balance));
+            Date date = new Date();
+            Date newDate = DateUtil.offsetDay(date, 1);
+            zhiYaRecord.setEffectDate(newDate);
+            zhiYaRecordDao.updateById(zhiYaRecord);
+        }
+
+        return Result.ok(MessageSourceUtils.getString("member_service_0024"));
+    }
+
+    public String generateSimpleSerialno(String userId) {
+        StringBuilder sb = new StringBuilder();
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
+        Date now = new Date();
+        sb.append(sd.format(now));
+        Calendar calendar = new GregorianCalendar();
+        calendar.setTime(now);
+        calendar.add(calendar.DATE, 1);
+        Date nextDate = calendar.getTime();
+        if (StrUtil.isNotEmpty(userId)) {
+            sb.append(userId);
+        }
+        sb.append(RandomUtil.randomInt(2));
+        long count = zhiYaRecordDao.getOrderCountByToday(sdf.format(now), sdf.format(nextDate));
+        count++;
+        int size = 4;
+        for (int i = 0; i < size - String.valueOf(count).length(); i++) {
+            sb.append("0");
+        }
+        sb.append(count);
+        return sb.toString();
     }
 
 }
diff --git a/src/main/resources/mapper/walletCoinOrder/ZhiYaRecordDao.xml b/src/main/resources/mapper/walletCoinOrder/ZhiYaRecordDao.xml
new file mode 100644
index 0000000..4075b09
--- /dev/null
+++ b/src/main/resources/mapper/walletCoinOrder/ZhiYaRecordDao.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.xcong.excoin.modules.coin.dao.ZhiYaRecordDao">
+	
+	<select id="getOrderCountByToday" resultType="long">
+		<![CDATA[  SELECT COUNT(*) from zhiya_record  WHERE DATE_FORMAT(create_time, '%Y-%m-%d') >= DATE_FORMAT(#{now}, '%Y-%m-%d')
+		and  DATE_FORMAT(create_time, '%Y-%m-%d') <  DATE_FORMAT(#{tomorrow}, '%Y-%m-%d') ]]>
+	</select>
+
+	<select id="selectByMemberIdAndOrderStatus" resultType="com.xcong.excoin.modules.coin.entity.ZhiYaRecordEntity">
+		SELECT *  FROM zhiya_record a where a.member_id= #{memberId} and a.order_status = #{orderStatus}
+	</select>
+
+</mapper>

--
Gitblit v1.9.1