src/main/java/com/xcong/excoin/modules/coin/controller/CoinController.java
@@ -5,6 +5,7 @@ import javax.annotation.Resource; import javax.validation.Valid; import com.xcong.excoin.common.annotations.SubmitRepeat; import com.xcong.excoin.modules.coin.parameter.dto.*; import com.xcong.excoin.modules.coin.parameter.vo.*; @@ -206,6 +207,7 @@ */ @ApiOperation(value="USDT兌換成GUSD", notes="USDT兌換成GUSD") @PostMapping(value="/usdtToGusd") @SubmitRepeat public Result usdtToGusd(@RequestBody @Valid UsdtToGusdDto usdtToGusdDto) { BigDecimal balance = usdtToGusdDto.getBalance(); Integer type = usdtToGusdDto.getType(); @@ -228,6 +230,7 @@ */ @ApiOperation(value="质押GUSD", notes="质押GUSD") @PostMapping(value="/zhiYaGusd") @SubmitRepeat public Result zhiYaGusd(@RequestBody @Valid ZhiYaGusdDto zhiYaGusdDto) { BigDecimal balance = zhiYaGusdDto.getBalance(); return coinService.zhiYaGusd(balance); @@ -249,11 +252,23 @@ */ @ApiOperation(value="赎回GUSD", notes="赎回GUSD") @PostMapping(value="/shuhuiGusd") @SubmitRepeat public Result shuhuiGusd(@RequestBody @Valid ShuhuiGusdDto shuhuiGusdDto) { BigDecimal balance = shuhuiGusdDto.getBalance(); Long id = shuhuiGusdDto.getId(); return coinService.shuhuiGusd(balance,id); } /** * 获取质押GUSD奖励记录 * @return */ @ApiOperation(value="获取质押GUSD奖励记录", notes="获取质押GUSD奖励记录") @ApiResponses({@ApiResponse( code = 200, message = "success", response = ZhiyaRewardVo.class)}) @PostMapping(value="/getZhiyaReward") public Result getZhiyaReward(@RequestBody @Valid ZhiyaRewardRecordsPageDto recordsPageDto) { return coinService.getZhiyaReward(recordsPageDto); } } src/main/java/com/xcong/excoin/modules/coin/dao/ZhiYaRecordDao.java
@@ -6,9 +6,12 @@ import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity; import com.xcong.excoin.modules.coin.entity.ZhiYaEntity; import com.xcong.excoin.modules.coin.entity.ZhiYaRecordEntity; import com.xcong.excoin.modules.coin.entity.ZhiyaRewardEntity; import com.xcong.excoin.modules.coin.parameter.vo.ZhiyaInfoVo; import com.xcong.excoin.modules.coin.parameter.vo.ZhiyaRewardVo; import org.apache.ibatis.annotations.Param; import java.util.Date; import java.util.List; public interface ZhiYaRecordDao extends BaseMapper<ZhiYaRecordEntity> { @@ -22,4 +25,12 @@ List<ZhiyaInfoVo> selectByMemberId(@Param("memberId")Long memberId); IPage<ZhiyaInfoVo> selectByMemberIdInPage(Page<ZhiyaInfoVo> page, @Param("record") ZhiYaEntity zhiya); IPage<ZhiyaRewardVo> selectZhiyaRewardByMemberIdInPage(Page<ZhiyaRewardVo> page, @Param("record") ZhiyaRewardEntity zhiyaRewardEntity); List<ZhiYaRecordEntity> selectZhiYaRewardByStatueAndTime(@Param("orderStatus")int i, @Param("now")Date date); Long selectTotalGusdByStatueAndTime(@Param("orderStatus")int i, @Param("now")Date date); // void selectZhiYaRewardByStatueAndTime(@Param("orderStatus")int i, @Param("now")Date date); } src/main/java/com/xcong/excoin/modules/coin/dao/ZhiyaRewardDao.java
New file @@ -0,0 +1,7 @@ package com.xcong.excoin.modules.coin.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.xcong.excoin.modules.coin.entity.ZhiyaRewardEntity; public interface ZhiyaRewardDao extends BaseMapper<ZhiyaRewardEntity> { } src/main/java/com/xcong/excoin/modules/coin/entity/ZhiyaRewardEntity.java
New file @@ -0,0 +1,38 @@ package com.xcong.excoin.modules.coin.entity; import com.baomidou.mybatisplus.annotation.TableName; import com.xcong.excoin.common.system.base.BaseEntity; import lombok.Data; import java.math.BigDecimal; /** * 质押记录表 */ @Data @TableName("zhiya_reward") public class ZhiyaRewardEntity extends BaseEntity { /** * */ private static final long serialVersionUID = 1L; /** * 会员ID */ private Long memberId; /** * 质押ID */ private Long recordId; /** * 质押编号 */ private String recordNo; /** * 奖励金额 */ private BigDecimal rewardAmout; } src/main/java/com/xcong/excoin/modules/coin/parameter/dto/UsdtToGusdDto.java
@@ -15,8 +15,8 @@ @ApiModelProperty(value = "划转金额", example = "100") private BigDecimal balance; @NotNull(message = "转换类型 1:USDT转GUSD 2GUSD转USDT") @ApiModelProperty(value = "币种", example = "1") @NotNull(message = "转换类型不能为空") @ApiModelProperty(value = "转换类型1:USDT转GUSD 2GUSD转USDT", example = "1") private Integer type; } src/main/java/com/xcong/excoin/modules/coin/parameter/dto/ZhiyaRewardRecordsPageDto.java
New file @@ -0,0 +1,26 @@ 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.Min; import javax.validation.constraints.NotNull; @Data @ApiModel(value = "ZhiyaRewardRecordsPageDto", description = "参数接受类") public class ZhiyaRewardRecordsPageDto { @NotNull(message = "编号不能为空") @ApiModelProperty(value = "编号", example = "1") private String orderNo; @NotNull @Min(1) @ApiModelProperty(value = "第几页", example = "1") private int pageNum; @NotNull @ApiModelProperty(value = "每页数量", example = "10") private int pageSize; } src/main/java/com/xcong/excoin/modules/coin/parameter/vo/UsdtToGusdVo.java
@@ -1,9 +1,12 @@ package com.xcong.excoin.modules.coin.parameter.vo; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.math.BigDecimal; import java.util.Date; @Data @ApiModel(value = "UsdtToGusdVo", description = "返回参数类") @@ -23,4 +26,8 @@ * 状态【0:待审核 1:成功2:失败】 */ private int status; @ApiModelProperty(value = "时间") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; } src/main/java/com/xcong/excoin/modules/coin/parameter/vo/ZhiyaInfoVo.java
@@ -34,6 +34,12 @@ */ @ApiModelProperty(value = "数量") private BigDecimal zhiyaCnt; /** * 总数 */ @ApiModelProperty(value = "总数") private BigDecimal rewardCnt; /** * 状态 1:生效中 2:已赎回 */ src/main/java/com/xcong/excoin/modules/coin/parameter/vo/ZhiyaRewardVo.java
New file @@ -0,0 +1,30 @@ package com.xcong.excoin.modules.coin.parameter.vo; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.math.BigDecimal; import java.util.Date; @Data @ApiModel(value = "ZhiyaRewardVo", description = "返回参数类") public class ZhiyaRewardVo { /** * 质押编号 */ @ApiModelProperty(value = "质押编号") private String recordNo; /** * 奖励金额 */ @ApiModelProperty(value = "奖励金额") private BigDecimal rewardAmout; @ApiModelProperty(value = "时间") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; } src/main/java/com/xcong/excoin/modules/coin/service/CoinService.java
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.modules.coin.parameter.dto.RecordsPageDto; import com.xcong.excoin.modules.coin.parameter.dto.ZhiyaRewardRecordsPageDto; import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity; import org.apache.ibatis.annotations.Param; @@ -53,4 +54,8 @@ Result getZhiyaRecords(RecordsPageDto recordsPageDto); Result getusdtToGusdRecords(RecordsPageDto recordsPageDto); void grantZhiyaAmount(); Result getZhiyaReward(ZhiyaRewardRecordsPageDto recordsPageDto); } src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
@@ -9,8 +9,9 @@ import cn.hutool.core.date.DateUtil; import com.xcong.excoin.modules.coin.dao.ZhiYaDao; import com.xcong.excoin.modules.coin.dao.ZhiYaRecordDao; import com.xcong.excoin.modules.coin.entity.ZhiYaEntity; import com.xcong.excoin.modules.coin.entity.ZhiYaRecordEntity; import com.xcong.excoin.modules.coin.dao.ZhiyaRewardDao; import com.xcong.excoin.modules.coin.entity.*; import com.xcong.excoin.modules.coin.parameter.dto.ZhiyaRewardRecordsPageDto; import com.xcong.excoin.modules.coin.parameter.vo.*; import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity; import org.springframework.stereotype.Service; @@ -24,8 +25,6 @@ import com.xcong.excoin.common.enumerates.MemberWalletCoinEnum; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.modules.coin.dao.MemberAccountMoneyChangeDao; import com.xcong.excoin.modules.coin.entity.MemberAccountMoneyChange; import com.xcong.excoin.modules.coin.entity.OrderCoinsDealEntity; import com.xcong.excoin.modules.coin.mapper.MemberAccountMoneyChangeMapper; import com.xcong.excoin.modules.coin.parameter.dto.RecordsPageDto; import com.xcong.excoin.modules.coin.service.CoinService; @@ -61,6 +60,8 @@ MemberWalletAgentDao memberWalletAgentDao; @Resource ZhiYaRecordDao zhiYaRecordDao; @Resource ZhiyaRewardDao zhiyaRewardDao; @Resource ZhiYaDao zhiYaDao; @Resource @@ -612,6 +613,7 @@ } @Override @Transactional public Result usdtToGusd(BigDecimal balance,Integer type) { //获取用户ID Long memberId = LoginUserUtils.getAppLoginUser().getId(); @@ -722,6 +724,7 @@ } @Override @Transactional public Result zhiYaGusd(BigDecimal balance) { //获取用户ID Long memberId = LoginUserUtils.getAppLoginUser().getId(); @@ -751,8 +754,8 @@ return Result.fail(MessageSourceUtils.getString("member_service_0096")); } //产生质押单,当前有的话,累加质押数量 ZhiYaRecordEntity zhiYaRecord = zhiYaRecordDao.selectByMemberIdAndOrderStatus(memberId,1); if(ObjectUtil.isEmpty(zhiYaRecord)){ // ZhiYaRecordEntity zhiYaRecord = zhiYaRecordDao.selectByMemberIdAndOrderStatus(memberId,1); // if(ObjectUtil.isEmpty(zhiYaRecord)){ //产生一条质押记录 ZhiYaRecordEntity zhiYaRecordEntity = new ZhiYaRecordEntity(); zhiYaRecordEntity.setMemberId(memberId); @@ -764,18 +767,19 @@ 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); } // }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")); } @Override @Transactional public Result shuhuiGusd(BigDecimal balance,Long id) { //获取用户ID Long memberId = LoginUserUtils.getAppLoginUser().getId(); @@ -794,10 +798,11 @@ if(balance.compareTo(zhiyaCnt) > 0){ return Result.fail(MessageSourceUtils.getString("zhiya_service_0004")); } zhiYaRecordEntity.setZhiyaCnt(zhiyaCnt.subtract(balance)); if(zhiyaCnt.compareTo(balance) == 0){ zhiYaRecordEntity.setOrderStatus(2); // zhiYaRecordEntity.setZhiyaCnt(zhiyaCnt.subtract(balance)); if(zhiyaCnt.compareTo(balance) != 0){ return Result.fail(MessageSourceUtils.getString("zhiya_service_0001")); } zhiYaRecordEntity.setOrderStatus(2); zhiYaRecordDao.updateById(zhiYaRecordEntity); //赎回Gusd,质押数量进入可用余额, ZhiYaEntity zhiya = zhiYaDao.selectByMemberId(memberId); @@ -856,6 +861,48 @@ return Result.ok(lists); } @Override @Transactional public void grantZhiyaAmount() { //获取每日总奖励 BigDecimal gusdReward = new BigDecimal(redisUtils.getString("GUSDREWARD")); //获取每日质押总数 Long totalGusd = zhiYaRecordDao.selectTotalGusdByStatueAndTime(1,new Date()); BigDecimal totalGusdBigDecimal = new BigDecimal(totalGusd); if(totalGusdBigDecimal.compareTo(BigDecimal.ZERO) > 0){ //平均一个获取奖励数量 BigDecimal divide = gusdReward.divide(totalGusdBigDecimal,2, BigDecimal.ROUND_HALF_UP); //获取今天之前满足发放奖励的全部质押单 //生效时间小于当前时间 //状态为生效中 List<ZhiYaRecordEntity> lists = zhiYaRecordDao.selectZhiYaRewardByStatueAndTime(1,new Date()); if(CollUtil.isNotEmpty(lists)){ for(ZhiYaRecordEntity zhiYaRecordEntity : lists){ ZhiyaRewardEntity zhiyaRewardEntity = new ZhiyaRewardEntity(); zhiyaRewardEntity.setRecordId(zhiYaRecordEntity.getId()); zhiyaRewardEntity.setRecordNo(zhiYaRecordEntity.getOrderNo()); zhiyaRewardEntity.setMemberId(zhiYaRecordEntity.getMemberId()); zhiyaRewardEntity.setRewardAmout(divide.multiply(zhiYaRecordEntity.getZhiyaCnt())); zhiyaRewardDao.insert(zhiyaRewardEntity); } } } } @Override public Result getZhiyaReward(ZhiyaRewardRecordsPageDto recordsPageDto) { //获取用户ID Long memberId = LoginUserUtils.getAppLoginUser().getId(); Page<ZhiyaRewardVo> page = new Page<>(recordsPageDto.getPageNum(), recordsPageDto.getPageSize()); ZhiyaRewardEntity zhiyaRewardEntity = new ZhiyaRewardEntity(); zhiyaRewardEntity.setMemberId(memberId); zhiyaRewardEntity.setRecordNo(recordsPageDto.getOrderNo()); IPage<ZhiyaRewardVo> lists = zhiYaRecordDao.selectZhiyaRewardByMemberIdInPage(page, zhiyaRewardEntity); return Result.ok(lists); } public String generateSimpleSerialno(String userId) { StringBuilder sb = new StringBuilder(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); src/main/java/com/xcong/excoin/quartz/job/ZhiyarRewardJob.java
New file @@ -0,0 +1,33 @@ package com.xcong.excoin.quartz.job; import com.xcong.excoin.modules.coin.service.CoinService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; /** * 质押 */ @Slf4j @Component public class ZhiyarRewardJob { @Resource private CoinService coinService; /** * 质押奖励计算 */ @Scheduled(cron = "0 0/1 * * * ? ") public void updateDoingPrice() { log.info("#质押奖励计算#"); try { coinService.grantZhiyaAmount(); } catch (Exception e) { log.error("#质押奖励计算错误#", e); } } } src/main/resources/mapper/walletCoinOrder/ZhiYaRecordDao.xml
@@ -20,15 +20,39 @@ </select> <select id="selectByMemberIdInPage" resultType="com.xcong.excoin.modules.coin.parameter.vo.ZhiyaInfoVo"> select * from zhiya_record select a.*,(SELECT ifnull(sum(b.reward_amout),0) FROM zhiya_reward b where b.member_id = #{record.memberId} and b.record_no = a.order_no) rewardCnt from zhiya_record a <if test="record != null"> <where> <if test="record.memberId != null" > and a.member_id=#{record.memberId} </if> </where> </if> order by id desc </select> <select id="selectZhiyaRewardByMemberIdInPage" resultType="com.xcong.excoin.modules.coin.parameter.vo.ZhiyaRewardVo"> select * from zhiya_reward <if test="record != null"> <where> <if test="record.memberId != null" > and member_id=#{record.memberId} </if> <if test="record.recordNo != null" > and record_no=#{record.recordNo} </if> </where> </if> order by id desc </select> <select id="selectZhiYaRewardByStatueAndTime" resultType="com.xcong.excoin.modules.coin.entity.ZhiYaRecordEntity"> SELECT a.* FROM zhiya_record a where a.order_status= #{orderStatus} and #{now} > a.effect_date </select> <select id="selectTotalGusdByStatueAndTime" resultType="long"> SELECT ifnull(sum(a.zhiya_cnt),0) FROM zhiya_record a where a.order_status= #{orderStatus} and #{now} > a.effect_date </select> </mapper> src/test/java/com/xcong/excoin/GuijiTest.java
@@ -2,11 +2,18 @@ import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.ExecutionException; import javax.annotation.Resource; import com.xcong.excoin.modules.coin.dao.ZhiYaDao; import com.xcong.excoin.modules.coin.dao.ZhiYaRecordDao; import com.xcong.excoin.modules.coin.dao.ZhiyaRewardDao; import com.xcong.excoin.modules.coin.entity.ZhiYaRecordEntity; import com.xcong.excoin.modules.coin.entity.ZhiyaRewardEntity; import com.xcong.excoin.utils.RedisUtils; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -40,6 +47,14 @@ private MemberCoinAddressDao memberCoinAddressDao; @Resource private MemberWalletCoinDao memberWalletCoinDao; @Resource ZhiYaRecordDao zhiYaRecordDao; @Resource ZhiyaRewardDao zhiyaRewardDao; @Resource ZhiYaDao zhiYaDao; @Resource RedisUtils redisUtils; public void pool() throws ExecutionException, InterruptedException { //List<MemberCoinChargeEntity> list = memberCoinChargeDao.selectAllBySymbolAndTag(CoinTypeEnum.USDT.name(), "ERC20", 1); @@ -117,6 +132,33 @@ } } @Test public void reward(){ //获取每日总奖励 BigDecimal gusdReward = new BigDecimal(redisUtils.getString("GUSDREWARD")); //获取每日质押总数 Long totalGusd = zhiYaRecordDao.selectTotalGusdByStatueAndTime(1,new Date()); BigDecimal totalGusdBigDecimal = new BigDecimal(totalGusd); if(totalGusdBigDecimal.compareTo(BigDecimal.ZERO) > 0){ //平均一个获取奖励数量 BigDecimal divide = gusdReward.divide(totalGusdBigDecimal); //获取今天之前满足发放奖励的全部质押单 //生效时间小于当前时间 //状态为生效中 List<ZhiYaRecordEntity> lists = zhiYaRecordDao.selectZhiYaRewardByStatueAndTime(1,new Date()); if(CollUtil.isNotEmpty(lists)){ for(ZhiYaRecordEntity zhiYaRecordEntity : lists){ ZhiyaRewardEntity zhiyaRewardEntity = new ZhiyaRewardEntity(); zhiyaRewardEntity.setRecordId(zhiYaRecordEntity.getId()); zhiyaRewardEntity.setRecordNo(zhiYaRecordEntity.getOrderNo()); zhiyaRewardEntity.setMemberId(zhiYaRecordEntity.getMemberId()); zhiyaRewardEntity.setRewardAmout(divide.multiply(zhiYaRecordEntity.getZhiyaCnt())); zhiyaRewardDao.insert(zhiyaRewardEntity); } } } } }