fix
Helius
2022-03-16 2e9a80416a0d2b049d8531d3666c45974ae54cc8
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.xcong.excoin.modules.otc.service.impl;
 
 
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
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.response.Result;
import com.xcong.excoin.common.system.service.CommonService;
import com.xcong.excoin.modules.member.dao.MemberAuthenticationDao;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberPaymentMethodDao;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberPaymentMethodEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.modules.otc.dao.OtcBlackListDao;
import com.xcong.excoin.modules.otc.dao.OtcMarketBussinessDao;
import com.xcong.excoin.modules.otc.dao.OtcOrderDao;
import com.xcong.excoin.modules.otc.dto.EntrustOrderAddDto;
import com.xcong.excoin.modules.otc.dto.EntrustOrderListDto;
import com.xcong.excoin.modules.otc.entity.OtcBlackList;
import com.xcong.excoin.modules.otc.entity.OtcEntrustOrder;
import com.xcong.excoin.modules.otc.dao.OtcEntrustOrderDao;
import com.xcong.excoin.modules.otc.entity.OtcMarketBussiness;
import com.xcong.excoin.modules.otc.entity.OtcOrder;
import com.xcong.excoin.modules.otc.mapper.OtcEntrustOrderMapper;
import com.xcong.excoin.modules.otc.service.OtcEntrustOrderService;
import com.xcong.excoin.modules.otc.vo.EntrustListInfoVo;
import com.xcong.excoin.modules.otc.vo.EntrustListVo;
import com.xcong.excoin.modules.otc.vo.EntrustOrderDetailVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.List;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class OtcEntrustOrderServiceImpl extends ServiceImpl<OtcEntrustOrderDao, OtcEntrustOrder> implements OtcEntrustOrderService {
 
    private final MemberDao memberDao;
    private final MemberPaymentMethodDao memberPaymentMethodDao;
    private final OtcMarketBussinessDao otcMarketBussinessDao;
    private final MemberWalletCoinDao memberWalletCoinDao;
    private final OtcOrderDao otcOrderDao;
    private final CommonService commonService;
    private final OtcBlackListDao otcBlackListDao;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(EntrustOrderAddDto addDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        OtcEntrustOrder otcEntrustOrder = OtcEntrustOrderMapper.INSTANCE.entrustOrderDtoToEntity(addDto);
        otcEntrustOrder.setMemberId(member.getId());
 
        member = memberDao.selectById(member.getId());
        if (!MemberEntity.CERTIFY_STATUS_Y.equals(member.getCertifyStatus())) {
            throw new GlobalException("未实名认证");
        }
 
        List<MemberPaymentMethodEntity> payments = memberPaymentMethodDao.selectByMemberId(member.getId());
        if (CollUtil.isEmpty(payments)) {
            throw new GlobalException("未绑定收款方式");
        }
 
        BigDecimal totalAmount = addDto.getUnitPrice().multiply(addDto.getAmount());
        otcEntrustOrder.setTotalAmount(totalAmount);
 
        if (totalAmount.compareTo(addDto.getMax()) < 0) {
            throw new GlobalException("最大限额应小于总金额");
        }
 
        if (OtcEntrustOrder.ORDER_TYPE_S.equals(addDto.getType())) {
            MemberWalletCoinEntity coinWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
//            BigDecimal multiply = addDto.getUnitPrice().multiply(coinWallet.getAvailableBalance());
            if(coinWallet.getAvailableBalance().compareTo(addDto.getAmount()) < 0) {
                throw new GlobalException("可用金额不足");
            }
 
            memberWalletCoinDao.updateFrozenBalance(member.getId(), coinWallet.getId(), addDto.getAmount());
        }
 
        if (!MemberEntity.IS_TRADER_Y.equals(member.getIsTrader())) {
            throw new GlobalException("不是市商");
        }
 
        if (member.getIsTrader() == 2) {
            otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_N);
        } else {
            otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_Y);
        }
 
        otcEntrustOrder.setRemainCoinAmount(addDto.getAmount());
        otcEntrustOrder.setStatus(OtcEntrustOrder.LINE_UP);
        otcEntrustOrder.setEntrustOrderNo(commonService.generateOrderNo(member.getId()));
        otcEntrustOrder.setId(null);
        this.baseMapper.insert(otcEntrustOrder);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(EntrustOrderAddDto modifyDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        OtcEntrustOrder otcEntrustOrder = OtcEntrustOrderMapper.INSTANCE.entrustOrderDtoToEntity(modifyDto);
        BigDecimal remainAmount = otcEntrustOrder.getRemainCoinAmount();
        otcEntrustOrder.setMemberId(member.getId());
 
        otcEntrustOrder.setRemainCoinAmount(modifyDto.getAmount());
        otcEntrustOrder.setStatus(OtcEntrustOrder.LINE_UP);
 
        List<OtcOrder> orders = otcOrderDao.selectOrderListUnFinish(member.getId(), otcEntrustOrder.getId());
        if (CollUtil.isNotEmpty(orders)) {
            throw new GlobalException("存在未完成的订单, 无法编辑");
        }
 
        OtcEntrustOrder prevEntity = this.baseMapper.selectById(modifyDto.getId());
        if (prevEntity == null) {
            throw new GlobalException("参数错误");
        }
 
        if (!prevEntity.getOrderType().equals(modifyDto.getType())) {
            throw new GlobalException("类型错误");
        }
 
        if (member.getIsTrader() == 2) {
            otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_N);
        } else {
            otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_Y);
        }
 
        BigDecimal totalAmount = modifyDto.getUnitPrice().multiply(modifyDto.getAmount());
        otcEntrustOrder.setTotalAmount(totalAmount);
        if (OtcEntrustOrder.ORDER_TYPE_S.equals(modifyDto.getType())) {
            MemberWalletCoinEntity coinWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
            coinWallet.setAvailableBalance(coinWallet.getAvailableBalance().add(remainAmount));
            if(coinWallet.getAvailableBalance().compareTo(modifyDto.getAmount()) < 0) {
                throw new GlobalException("可用金额不足");
            }
 
            BigDecimal frozen = coinWallet.getFrozenBalance().subtract(remainAmount);
            coinWallet.setAvailableBalance(coinWallet.getAvailableBalance().subtract(modifyDto.getAmount()));
            coinWallet.setFrozenBalance(frozen.add(modifyDto.getAmount()));
            memberWalletCoinDao.updateById(coinWallet);
        }
 
        this.baseMapper.updateById(otcEntrustOrder);
    }
 
    @Override
    public IPage<EntrustListVo> findEntrustListInPage(EntrustOrderListDto dto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        Page<EntrustListVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
//        List<OtcBlackList> blackLists = otcBlackListDao.selectBlackListByMemberId(member.getId());
//        if (CollUtil.isNotEmpty(blackLists)) {
            dto.setMemberId(member.getId());
//        }
        return this.baseMapper.selectEntrustListInPage(dto, page);
    }
 
    @Override
    public List<EntrustListInfoVo> findOwnEntrustOrder(EntrustOrderListDto orderListDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
 
        OtcEntrustOrder query = new OtcEntrustOrder();
        query.setStatus(3);
        query.setMemberId(member.getId());
 
        Page<OtcEntrustOrder> page = new Page<>(orderListDto.getPageNum(), orderListDto.getPageSize());
        IPage<OtcEntrustOrder> result = this.baseMapper.selectOwnEntrustListInPage(query, page);
        return OtcEntrustOrderMapper.INSTANCE.entrustToListInfoVoList(result.getRecords());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelEntrustOrder(Long id) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        OtcEntrustOrder otcEntrustOrder = this.baseMapper.selectById(id);
        if (otcEntrustOrder == null) {
            throw new GlobalException("参数错误");
        }
 
        if (OtcEntrustOrder.LINE_CANCEL.equals(otcEntrustOrder.getStatus())) {
            throw new GlobalException("请勿重复撤销");
        }
 
        List<OtcOrder> orders = otcOrderDao.selectOrderListUnFinish(member.getId(), otcEntrustOrder.getId());
        if (CollUtil.isNotEmpty(orders)) {
            throw new GlobalException("存在未完成的订单, 无法撤销");
        }
 
        if (OtcEntrustOrder.ORDER_TYPE_S.equals(otcEntrustOrder.getOrderType())) {
            MemberWalletCoinEntity wallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
            memberWalletCoinDao.subFrozenBalance(member.getId(), wallet.getId(), otcEntrustOrder.getRemainCoinAmount());
        }
 
        otcEntrustOrder = new OtcEntrustOrder();
        otcEntrustOrder.setId(id);
        otcEntrustOrder.setStatus(OtcEntrustOrder.LINE_CANCEL);
        this.baseMapper.updateById(otcEntrustOrder);
    }
 
    @Override
    public Result findEntrustOrderDetail(Long id) {
        OtcEntrustOrder otcEntrustOrder = this.baseMapper.selectById(id);
        if (otcEntrustOrder == null) {
            return Result.fail("参数错误");
        }
 
        MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(otcEntrustOrder.getMemberId(), null, "1");
        MemberEntity member = memberDao.selectById(otcEntrustOrder.getMemberId());
        OtcMarketBussiness mb = otcMarketBussinessDao.selectMarketBussinessByMemberId(member.getId());
        EntrustOrderDetailVo detail = OtcEntrustOrderMapper.INSTANCE.entityToOrderDetail(otcEntrustOrder);
        detail.setName(member.getName());
        if (OtcEntrustOrder.ORDER_TYPE_S.equals(otcEntrustOrder.getOrderType())) {
            detail.setOrderCnt(mb.getSaleOrderCnt());
            detail.setFinishRatio(mb.getSaleFinishRatio());
        } else {
            detail.setOrderCnt(mb.getBuyOrderCnt());
            detail.setFinishRatio(mb.getFinishRatio());
        }
        detail.setPayType(defualtMethod.getPaymentType());
        return Result.ok(detail);
    }
}