diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/controller/MarketApplyController.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/controller/MarketApplyController.java index 5ae310f..8bd3dfb 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/controller/MarketApplyController.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/controller/MarketApplyController.java @@ -31,7 +31,7 @@ public class MarketApplyController { @PostMapping("/cancelApply") public ResultDTO cancelApply(HttpServletRequest request) { String userId = getUserId(request); - marketApplyService.applyMarket(userId, MarketApplyConstants.CANCEL); + marketApplyService.cancelMarket(userId); return ResultDTO.requstSuccess(); } @@ -40,7 +40,7 @@ public class MarketApplyController { @PostMapping("/marketApply") public ResultDTO marketApply(HttpServletRequest request) { String userId = getUserId(request); - marketApplyService.applyMarket(userId, MarketApplyConstants.MARKET); + marketApplyService.asMarket(userId); return ResultDTO.requstSuccess(); } diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/mapper/MarketFreezeMapper.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/mapper/MarketFreezeMapper.java index 34aa0b6..ed67113 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/mapper/MarketFreezeMapper.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/mapper/MarketFreezeMapper.java @@ -20,4 +20,11 @@ public interface MarketFreezeMapper extends Mapper { * @return */ MarketFreeze selectByUserId(@Param("userId") String userId); + + /*** + * 根据用户id删除 + * @param userId + * @return + */ + int deleteByUserId(@Param("userId") String userId); } \ No newline at end of file diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketApplyService.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketApplyService.java index 2bdae54..5f9266f 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketApplyService.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketApplyService.java @@ -11,6 +11,20 @@ public interface MarketApplyService { */ void applyMarket(String userId, String applyType); + /** + * 提交成为市商申请后直接成为市商 + * + * @param userId + */ + void asMarket(String userId); + + /** + * 提交取消申请后取消市商身份 + * + * @param userId + */ + void cancelMarket(String userId); + /*** * 根据用户id和申请类型查询 * @param userId diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketFreezeService.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketFreezeService.java index 3a6b0c6..72487e9 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketFreezeService.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketFreezeService.java @@ -2,6 +2,8 @@ package com.blockchain.server.otc.service; import com.blockchain.server.otc.entity.MarketFreeze; +import java.math.BigDecimal; + public interface MarketFreezeService { /*** @@ -10,4 +12,20 @@ public interface MarketFreezeService { * @return */ MarketFreeze getByUserId(String userId); + + /*** + * 新建用户保证金信息 + * @param userId + * @param amount + * @param coinName + * @return + */ + int insertMarketFreeze(String userId, BigDecimal amount, String coinName); + + /*** + * 删除保证金记录 + * @param userId + * @return + */ + int deleteMarketFreeze(String userId); } diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketUserService.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketUserService.java index 4dd6394..fe6d433 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketUserService.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/MarketUserService.java @@ -30,4 +30,11 @@ public interface MarketUserService { * @return */ int updateByPrimaryKeySelective(MarketUser marketUser); + + /*** + * 新建市商用户 + * @param userId 用户id + * @return + */ + int insertMarketUser(String userId); } diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketApplyServiceImpl.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketApplyServiceImpl.java index 4a84b3b..758b03d 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketApplyServiceImpl.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketApplyServiceImpl.java @@ -1,6 +1,7 @@ package com.blockchain.server.otc.service.impl; import com.blockchain.common.base.dto.ResultDTO; +import com.blockchain.server.otc.common.constant.BillConstants; import com.blockchain.server.otc.common.constant.MarketApplyConstants; import com.blockchain.server.otc.common.constant.MarketUserConstants; import com.blockchain.server.otc.common.enums.OtcEnums; @@ -10,10 +11,8 @@ import com.blockchain.server.otc.entity.MarketFreeze; import com.blockchain.server.otc.entity.MarketUser; import com.blockchain.server.otc.feign.UserFeign; import com.blockchain.server.otc.mapper.MarketApplyMapper; -import com.blockchain.server.otc.service.ConfigService; -import com.blockchain.server.otc.service.MarketApplyService; -import com.blockchain.server.otc.service.MarketFreezeService; -import com.blockchain.server.otc.service.MarketUserService; +import com.blockchain.server.otc.service.*; +import com.codingapi.tx.annotation.TxTransaction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -34,8 +33,15 @@ public class MarketApplyServiceImpl implements MarketApplyService { @Autowired private ConfigService configService; @Autowired + private WalletService walletService; + @Autowired + private BillService billService; + @Autowired private UserFeign userFeign; + //保证金固定为CNY + private static final String UNIT_NAME = "CNY"; + @Override @Transactional public void applyMarket(String userId, String applyType) { @@ -51,6 +57,88 @@ public class MarketApplyServiceImpl implements MarketApplyService { insertMarketApply(userId, applyType); } + @Override + @Transactional + @TxTransaction(isStart = true) + public void asMarket(String userId) { + //提交市商申请时,判断用户是否通过高级认证 + checkUserHighAuth(userId, MarketUserConstants.MARKET); + //查询用户市商状态 + MarketUser marketUser = marketUserService.getMarketUserByUserId(userId); + + //市商用户存在 + if (marketUser != null) { + //市商状态不等于未认证 + if (!marketUser.getStatus().equals(MarketUserConstants.NOTMARKET)) { + throw new OtcException(OtcEnums.USER_IS_MRAKET); + } + //更新市商用户状态为市商 + marketUser.setStatus(MarketUserConstants.MARKET); + marketUser.setModifyTime(new Date()); + marketUserService.updateByPrimaryKeySelective(marketUser); + } else {//市商用户不存在 + //新建市商 + marketUserService.insertMarketUser(userId); + } + + //新建市商申请 + //市商申请id + String applyId = insertAgreeMarketApply(userId, MarketApplyConstants.MARKET); + //市商保证金代币 + String coinName = configService.selectMarketFreezeCoin(); + //市商保证金数量 + BigDecimal amount = configService.selectMarketFreezeAmount(); + //防止保证金代币和数量为空 + if (coinName != null && amount.compareTo(BigDecimal.ZERO) > 0) { + //新建冻结记录 + marketFreezeService.insertMarketFreeze(userId, amount, coinName); + //冻结余额 + walletService.handleBalance(userId, applyId, coinName, UNIT_NAME, BigDecimal.ZERO, amount); + //增加余额变更记录 + billService.insertBill(userId, applyId, BigDecimal.ZERO, amount, BillConstants.MARKET_USER, coinName); + } + } + + @Override + @Transactional + @TxTransaction(isStart = true) + public void cancelMarket(String userId) { + //查询用户市商状态 + MarketUser marketUser = marketUserService.getMarketUserByUserId(userId); + //市商用户为空 + if (marketUser == null) { + throw new OtcException(OtcEnums.MARKET_USER_NULL); + } + //市商用户状态不等于市商 + if (!marketUser.getStatus().equals(MarketUserConstants.MARKET)) { + throw new OtcException(OtcEnums.CANCEL_MARKET_USER_STATUS_ERROR); + } + + //新建市商申请 + String applyId = insertAgreeMarketApply(userId, MarketApplyConstants.CANCEL); + + //更新 + marketUser.setStatus(MarketUserConstants.NOTMARKET); + marketUser.setModifyTime(new Date()); + marketUserService.updateByPrimaryKeySelective(marketUser); + + //查询保证金记录 + MarketFreeze marketFreeze = marketFreezeService.getByUserId(userId); + if (marketFreeze != null) { + //删除冻结记录 + marketFreezeService.deleteMarketFreeze(userId); + //解冻资金 + BigDecimal freeAmount = marketFreeze.getAmount(); + BigDecimal freezeAmount = freeAmount.multiply(new BigDecimal("-1")); + //解冻的币种 + String coinName = marketFreeze.getCoinName(); + //解冻余额 + walletService.handleBalance(userId, applyId, coinName, UNIT_NAME, freeAmount, freezeAmount); + //增加余额变更记录 + billService.insertBill(userId, applyId, freeAmount, freezeAmount, BillConstants.CANCEL_MARKET_USER, coinName); + } + } + @Override public MarketApply getByUserIdAndApplyTypeAndStatus(String userId, String applyType, String status) { return marketApplyMapper.selectByUserIdAndApplyTypeAndStatus(userId, applyType, status); @@ -101,6 +189,27 @@ public class MarketApplyServiceImpl implements MarketApplyService { } } + /*** + * 新建状态为'已同意'的申请记录 + * @param userId + * @param applyType + * @return + */ + private String insertAgreeMarketApply(String userId, String applyType) { + MarketApply marketApply = new MarketApply(); + Date now = new Date(); + String applyId = UUID.randomUUID().toString(); + marketApply.setId(applyId); + marketApply.setUserId(userId); + marketApply.setApplyType(applyType); + marketApply.setCreateTime(now); + marketApply.setModifyTime(now); + marketApply.setStatus(MarketApplyConstants.AGREE); + + marketApplyMapper.insertSelective(marketApply); + return applyId; + } + /*** * 新增市商申请 * @param userId diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketFreezeServiceImpl.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketFreezeServiceImpl.java index b755bb1..e54dea8 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketFreezeServiceImpl.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketFreezeServiceImpl.java @@ -5,6 +5,11 @@ import com.blockchain.server.otc.mapper.MarketFreezeMapper; import com.blockchain.server.otc.service.MarketFreezeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.UUID; @Service public class MarketFreezeServiceImpl implements MarketFreezeService { @@ -16,4 +21,24 @@ public class MarketFreezeServiceImpl implements MarketFreezeService { public MarketFreeze getByUserId(String userId) { return marketFreezeMapper.selectByUserId(userId); } + + @Override + @Transactional + public int insertMarketFreeze(String userId, BigDecimal amount, String coinName) { + MarketFreeze marketFreeze = new MarketFreeze(); + marketFreeze.setId(UUID.randomUUID().toString()); + marketFreeze.setUserId(userId); + marketFreeze.setCoinName(coinName); + marketFreeze.setAmount(amount); + marketFreeze.setMarketApplyId(null); + marketFreeze.setCreateTime(new Date()); + return marketFreezeMapper.insertSelective(marketFreeze); + } + + @Override + @Transactional + public int deleteMarketFreeze(String userId) { + return marketFreezeMapper.deleteByUserId(userId); + } } + diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketUserServiceImpl.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketUserServiceImpl.java index 3fd9e24..249371e 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketUserServiceImpl.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/MarketUserServiceImpl.java @@ -10,6 +10,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Date; +import java.util.UUID; + @Service public class MarketUserServiceImpl implements MarketUserService { @@ -44,4 +47,17 @@ public class MarketUserServiceImpl implements MarketUserService { public int updateByPrimaryKeySelective(MarketUser marketUser) { return marketUserMapper.updateByPrimaryKeySelective(marketUser); } + + @Override + @Transactional + public int insertMarketUser(String userId) { + MarketUser marketUser = new MarketUser(); + Date date = new Date(); + marketUser.setId(UUID.randomUUID().toString()); + marketUser.setUserId(userId); + marketUser.setStatus(MarketUserConstants.MARKET); + marketUser.setModifyTime(date); + marketUser.setCreateTime(date); + return marketUserMapper.insertSelective(marketUser); + } } diff --git a/blockchain-server/blockchain-server-otc/src/main/resources/mapper/MarketFreezeMapper.xml b/blockchain-server/blockchain-server-otc/src/main/resources/mapper/MarketFreezeMapper.xml index 6533f4d..5396ab0 100644 --- a/blockchain-server/blockchain-server-otc/src/main/resources/mapper/MarketFreezeMapper.xml +++ b/blockchain-server/blockchain-server-otc/src/main/resources/mapper/MarketFreezeMapper.xml @@ -22,4 +22,11 @@ user_id = #{userId} + + DELETE FROM + + WHERE + user_id = #{userId} + + \ No newline at end of file