Helius
2021-05-19 75f0bffc3d1aea9764406dc6be4b73f36c9ebb6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.xcong.excoin.modules.otc.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
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.exception.GlobalException;
import com.xcong.excoin.common.system.service.CommonService;
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.otc.dao.OtcEntrustOrderDao;
import com.xcong.excoin.modules.otc.dto.OrderAddDto;
import com.xcong.excoin.modules.otc.dto.OrderListDto;
import com.xcong.excoin.modules.otc.entity.OtcEntrustOrder;
import com.xcong.excoin.modules.otc.entity.OtcOrder;
import com.xcong.excoin.modules.otc.dao.OtcOrderDao;
import com.xcong.excoin.modules.otc.service.OtcOrderService;
import com.xcong.excoin.modules.otc.vo.OrderListVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.Date;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class OtcOrderServiceImpl extends ServiceImpl<OtcOrderDao, OtcOrder> implements OtcOrderService {
 
    private final OtcEntrustOrderDao otcEntrustOrderDao;
    private final CommonService commonService;
    private final MemberWalletCoinDao memberWalletCoinDao;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void buyOrder(OrderAddDto orderAddDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        OtcEntrustOrder entrustOrder = otcEntrustOrderDao.selectById(orderAddDto.getId());
        if (entrustOrder == null) {
            throw new GlobalException("委托单不存在");
        }
 
        if (!OtcEntrustOrder.ORDER_TYPE_S.equals(entrustOrder.getOrderType())) {
            throw new GlobalException("无法购买");
        }
 
        if (orderAddDto.getUsdtAmount().compareTo(entrustOrder.getRemainCoinAmount()) > 0) {
            throw new GlobalException("无足够的USDT");
        }
 
        BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice());
        log.info("--->{}", cny);
        if (cny.compareTo(orderAddDto.getCnyAmount()) != 0) {
            throw new GlobalException("数量与金额不符");
        }
 
        OtcOrder otcOrder = new OtcOrder();
        otcOrder.setOrderNo(commonService.generateOrderNo(member.getId()));
        otcOrder.setUnitPrice(entrustOrder.getUnitPrice());
        otcOrder.setEntrustOrderId(entrustOrder.getId());
        otcOrder.setCoinAmount(orderAddDto.getUsdtAmount());
        otcOrder.setTotalAmount(orderAddDto.getCnyAmount());
        otcOrder.setMemberId(member.getId());
        otcOrder.setStatus(OtcOrder.STATUS_SUBMIT);
        otcOrder.setPayTime(new Date());
        otcOrder.setEntrustMemberId(entrustOrder.getMemberId());
        otcOrder.setOrderType(OtcEntrustOrder.ORDER_TYPE_B);
 
        OtcOrder sale = new OtcOrder();
        BeanUtil.copyProperties(otcOrder, sale);
        sale.setMemberId(entrustOrder.getMemberId());
        sale.setOrderType(OtcEntrustOrder.ORDER_TYPE_S);
        otcEntrustOrderDao.updateRemainAmount(entrustOrder.getId(), orderAddDto.getUsdtAmount().negate());
        this.baseMapper.insert(otcOrder);
        this.baseMapper.insert(sale);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saleOrder(OrderAddDto orderAddDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        OtcEntrustOrder entrustOrder = otcEntrustOrderDao.selectById(orderAddDto.getId());
        if (entrustOrder == null) {
            throw new GlobalException("委托单不存在");
        }
 
        if (!OtcEntrustOrder.ORDER_TYPE_B.equals(entrustOrder.getOrderType())) {
            throw new GlobalException("无法出售");
        }
 
        BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice());
        if (cny.compareTo(orderAddDto.getCnyAmount()) != 0) {
            throw new GlobalException("数量与金额不符");
        }
 
        MemberWalletCoinEntity wallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
        if (wallet.getAvailableBalance().compareTo(orderAddDto.getUsdtAmount()) < 0) {
            throw new GlobalException("钱包余额不足");
        }
 
        OtcOrder otcOrder = new OtcOrder();
        otcOrder.setOrderNo(commonService.generateOrderNo(member.getId()));
        otcOrder.setUnitPrice(entrustOrder.getUnitPrice());
        otcOrder.setEntrustOrderId(entrustOrder.getId());
        otcOrder.setCoinAmount(orderAddDto.getUsdtAmount());
        otcOrder.setTotalAmount(orderAddDto.getCnyAmount());
        otcOrder.setMemberId(member.getId());
        otcOrder.setStatus(OtcOrder.STATUS_SUBMIT);
        otcOrder.setPayTime(new Date());
        otcOrder.setEntrustMemberId(entrustOrder.getMemberId());
        otcOrder.setOrderType(OtcEntrustOrder.ORDER_TYPE_S);
 
        OtcOrder buy = new OtcOrder();
        BeanUtil.copyProperties(otcOrder, buy);
        buy.setMemberId(entrustOrder.getMemberId());
        buy.setOrderType(OtcEntrustOrder.ORDER_TYPE_B);
 
        otcEntrustOrderDao.updateRemainAmount(entrustOrder.getId(), orderAddDto.getUsdtAmount().negate());
        this.baseMapper.insert(otcOrder);
        this.baseMapper.insert(buy);
 
        memberWalletCoinDao.updateFrozenBalance(member.getId(), wallet.getId(), orderAddDto.getUsdtAmount());
    }
 
    @Override
    public IPage<OrderListVo> findOrderListInPage(OrderListDto orderListDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        Page<OrderListVo> page = new Page<>(orderListDto.getPageNum(), orderListDto.getPageSize());
 
        OtcOrder order = new OtcOrder();
        order.setStatus(orderListDto.getStatus());
        order.setMemberId(member.getId());
        return this.baseMapper.selectOrdderListInPage(order, page);
    }
}