Helius
2020-05-31 7757c55dfdf79568836169bb46ea100ca32e2ec4
src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractEntrustOrderServiceImpl.java
@@ -2,12 +2,16 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.common.enumerates.MemberWalletCoinEnum;
import com.xcong.excoin.common.response.Result;
import com.xcong.excoin.common.system.service.CommonService;
import com.xcong.excoin.modules.contract.dao.ContractEntrustOrderDao;
import com.xcong.excoin.modules.contract.dao.ContractOrderDao;
import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity;
import com.xcong.excoin.modules.contract.entity.ContractOrderEntity;
import com.xcong.excoin.modules.contract.mapper.ContractEntrustOrderEntityMapper;
import com.xcong.excoin.modules.contract.parameter.dto.SubmitEntrustDto;
import com.xcong.excoin.modules.contract.parameter.vo.ContractEntrustVo;
import com.xcong.excoin.modules.contract.service.ContractEntrustOrderService;
import com.xcong.excoin.modules.member.dao.MemberWalletContractDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
@@ -21,6 +25,7 @@
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
/**
 * @author wzy
@@ -45,33 +50,36 @@
    @Resource
    private ContractOrderDao contractOrderDao;
    @Resource
    private CommonService commonService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addContractEntrustOrder(SubmitEntrustDto submitEntrustDto) {
        MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
        // 获取最新价
        BigDecimal newPirce = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(submitEntrustDto.getSymbol())));
        BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(submitEntrustDto.getSymbol())));
        // 委托开仓
        if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE || submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_LESS) {
            // 开多委托价不能大于当前价
            if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE) {
                if (submitEntrustDto.getEntrustPrice().compareTo(newPirce) > -1) {
                if (submitEntrustDto.getEntrustPrice().compareTo(newPrice) > -1) {
                    return Result.fail("委托价不能大于当前价");
                }
            }
            // 开空委托价不能小于当前价
            if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_LESS) {
                if (submitEntrustDto.getEntrustPrice().compareTo(newPirce) < 1) {
                if (submitEntrustDto.getEntrustPrice().compareTo(newPrice) < 1) {
                    return Result.fail("委托价不能小于当前价");
                }
            }
            MemberWalletContractEntity walletContract = memberWalletContractDao.selectById(memberEntity.getId());
            // 委托总额
            BigDecimal entrustTotalAmount = submitEntrustDto.getEntrustPrice().multiply(BigDecimal.valueOf(submitEntrustDto.getSymbolCnt()));
            BigDecimal entrustTotalAmount = submitEntrustDto.getEntrustPrice().multiply(BigDecimal.valueOf(submitEntrustDto.getSymbolCnt())).setScale(8, BigDecimal.ROUND_DOWN);
            BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(submitEntrustDto.getSymbol());
            // 保证金计算 -- 建仓价X规格X手数X(1/杠杆倍率)
@@ -83,9 +91,11 @@
            ContractEntrustOrderEntityMapper convert = ContractEntrustOrderEntityMapper.INSTANCE;
            ContractEntrustOrderEntity entrustOrderEntity = convert.submitEntrustDtoToEntity(submitEntrustDto);
            entrustOrderEntity.setOrderNo(commonService.generateOrderNo(memberEntity.getId()));
            entrustOrderEntity.setMemberId(memberEntity.getId());
            entrustOrderEntity.setBondAmount(bondAmount);
            entrustOrderEntity.setSymbolSku(lotNumber);
            entrustOrderEntity.setEntrustAmount(entrustTotalAmount);
            // 暂默认逐仓
            entrustOrderEntity.setPositionType(ContractEntrustOrderEntity.POSITION_TYPE_ADD);
@@ -112,10 +122,14 @@
    @Override
    public Result findEntrustOrderList() {
        return null;
        MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
        List<ContractEntrustOrderEntity> list = contractEntrustOrderDao.selectEntrustOrderListByMemberId(memberEntity.getId());
        List<ContractEntrustVo> resultList = ContractEntrustOrderEntityMapper.INSTANCE.entityListToVoList(list);
        return Result.ok(resultList);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result cancelEntrustOrder(Long id) {
        MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
@@ -125,8 +139,22 @@
            return Result.fail("该委托单不存在");
        }
        return null;
        MemberWalletContractEntity walletContractEntity = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue());
        BigDecimal total = entrustOrderEntity.getEntrustAmount().add(entrustOrderEntity.getBondAmount());
        walletContractEntity.setAvailableBalance(walletContractEntity.getAvailableBalance().add(total));
        walletContractEntity.setFrozenBalance(walletContractEntity.getFrozenBalance().subtract(total));
        memberWalletContractDao.updateById(walletContractEntity);
        ContractOrderEntity orderEntity = ContractEntrustOrderEntityMapper.INSTANCE.entrustOrderToOrder(entrustOrderEntity);
        orderEntity.setTradeType(ContractOrderEntity.TRADE_TYPE_MARK_PRICE);
        orderEntity.setOrderType(ContractOrderEntity.ORDER_TYPE_CANCEL);
        int i = contractOrderDao.insert(orderEntity);
        contractEntrustOrderDao.deleteById(entrustOrderEntity.getId());
        if (i > 0) {
            return Result.ok("撤销成功");
        }
        return Result.fail("撤销失败");
    }
}