xiaoyong931011
2021-07-05 cf200a1f92c01ba22c326c49391f748ffb006910
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.xcong.excoin.modules.otc.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.entity.FebsResponse;
import com.xcong.excoin.common.entity.QueryRequest;
import com.xcong.excoin.modules.member.entity.MemberAccountMoneyChangeEntity;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.modules.member.mapper.MemberMapper;
import com.xcong.excoin.modules.member.mapper.MemberWalletCoinMapper;
import com.xcong.excoin.modules.otc.entity.*;
import com.xcong.excoin.modules.otc.mapper.*;
import com.xcong.excoin.modules.otc.service.OtcService;
import com.xcong.excoin.modules.otc.vo.OtcAppealInfoVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
@Service
@RequiredArgsConstructor
public class OtcServiceImpl extends ServiceImpl<OtcMarketBussinessMapper, OtcMarketBussinessEntity> implements OtcService {
 
    @Resource
    private OtcMarketBussinessMapper otcMarketBussinessMapper;
    @Resource
    private OtcOrderAppealMapper otcOrderAppealMapper;
    @Resource
    private OtcEntrustOrderMapper otcEntrustOrderMapper;
    @Resource
    private OtcOrderMapper otcOrderMapper;
    @Resource
    private OtcSettingMapper otcSettingMapper;
    @Resource
    private MemberWalletCoinMapper memberWalletCoinMapper;
    @Resource
    private MemberMapper memberMapper;
 
    @Override
    public IPage<OtcMarketBussinessEntity> otcShopList(OtcMarketBussinessEntity otcMarketBussinessEntity, QueryRequest request) {
        Page<OtcMarketBussinessEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<OtcMarketBussinessEntity> otcMarketBussinessEntitys = otcMarketBussinessMapper.otcShopList(page, otcMarketBussinessEntity);
        return otcMarketBussinessEntitys;
    }
 
    @Override
    @Transactional
    public FebsResponse agreeShop(Long id) {
        OtcMarketBussinessEntity otcMarketBussinessEntity = otcMarketBussinessMapper.selectById(id);
        Integer status = otcMarketBussinessEntity.getStatus();
        if(OtcMarketBussinessEntity.STATUS_ONE != status){
            return new FebsResponse().fail().message("当前状态不是待审核");
        }
        otcMarketBussinessEntity.setStatus(OtcMarketBussinessEntity.STATUS_TWO);
        otcMarketBussinessMapper.updateById(otcMarketBussinessEntity);
 
        long memberId = otcMarketBussinessEntity.getMemberId();
        MemberEntity memberEntity = memberMapper.selectById(memberId);
        memberEntity.setIsTrader(MemberEntity.ISTRADER_Y);
        memberMapper.updateById(memberEntity);
        return new FebsResponse().success();
    }
 
    @Override
    @Transactional
    public FebsResponse disagreeShop(Long id) {
        OtcMarketBussinessEntity otcMarketBussinessEntity = otcMarketBussinessMapper.selectById(id);
        Integer status = otcMarketBussinessEntity.getStatus();
        if(OtcMarketBussinessEntity.STATUS_ONE != status){
            return new FebsResponse().fail().message("当前状态不是待审核");
        }
        otcMarketBussinessEntity.setStatus(OtcMarketBussinessEntity.STATUS_THREE);
        otcMarketBussinessMapper.updateById(otcMarketBussinessEntity);
 
        long memberId = otcMarketBussinessEntity.getMemberId();
        MemberEntity memberEntity = memberMapper.selectById(memberId);
        memberEntity.setIsTrader(MemberEntity.ISTRADER_Y);
        memberMapper.updateById(memberEntity);
        return new FebsResponse().success();
    }
 
    @Override
    public IPage<OtcOrderAppealEntity> otcAppealList(OtcOrderAppealEntity otcOrderAppealEntity, QueryRequest request) {
        Page<OtcOrderAppealEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<OtcOrderAppealEntity> otcOrderAppealEntitys = otcOrderAppealMapper.otcAppealList(page, otcOrderAppealEntity);
        return otcOrderAppealEntitys;
    }
 
    @Override
    @Transactional
    public FebsResponse dealDone(Long id) {
        OtcOrderAppealEntity otcOrderAppealEntity = otcOrderAppealMapper.selectById(id);
        Integer status = otcOrderAppealEntity.getStatus();
        if(OtcOrderAppealEntity.STATUS_TWO != status){
            return new FebsResponse().fail().message("当前状态不是处理中");
        }
 
        otcOrderAppealEntity.setStatus(OtcOrderAppealEntity.STATUS_THREE);
        otcOrderAppealMapper.updateById(otcOrderAppealEntity);
        return new FebsResponse().success();
    }
 
    @Override
    @Transactional
    public FebsResponse dealIng(Long id) {
        OtcOrderAppealEntity otcOrderAppealEntity = otcOrderAppealMapper.selectById(id);
        Integer status = otcOrderAppealEntity.getStatus();
        if(OtcOrderAppealEntity.STATUS_ONE != status){
            return new FebsResponse().fail().message("当前状态不是待处理");
        }
 
        otcOrderAppealEntity.setStatus(OtcOrderAppealEntity.STATUS_TWO);
        otcOrderAppealMapper.updateById(otcOrderAppealEntity);
        return new FebsResponse().success();
    }
 
    @Override
    public IPage<OtcEntrustOrderEntity> otcEntrustList(OtcEntrustOrderEntity otcEntrustOrderEntity, QueryRequest request) {
        Page<OtcEntrustOrderEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<OtcEntrustOrderEntity> otcEntrustOrderEntitys = otcEntrustOrderMapper.otcEntrustList(page, otcEntrustOrderEntity);
        return otcEntrustOrderEntitys;
    }
 
    @Override
    public IPage<OtcOrderEntity> otcOrderList(OtcOrderEntity otcOrderEntity, QueryRequest request) {
        Page<OtcOrderEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<OtcOrderEntity> otcOrderEntitys = otcOrderMapper.otcOrderList(page, otcOrderEntity);
        return otcOrderEntitys;
    }
 
    @Override
    public OtcAppealInfoVo otcAppealInfo(long id) {
        OtcAppealInfoVo otcAppealInfoVo = new OtcAppealInfoVo();
 
        OtcOrderAppealEntity otcOrderAppealEntity = otcOrderAppealMapper.selectById(id);
        otcAppealInfoVo.setId(id);
        if(ObjectUtil.isNotEmpty(otcOrderAppealEntity)){
            String reason = otcOrderAppealEntity.getReason();
            otcAppealInfoVo.setReason(reason);
            String content = otcOrderAppealEntity.getContent();
            List arr = Arrays.asList(content.split(","));
            otcAppealInfoVo.setContent(arr);
        }
        //获取对应的订单详情
        long orderId = otcOrderAppealEntity.getOrderId();
        OtcOrderEntity otcOrderEntity = otcOrderMapper.selectById(orderId);
        if(ObjectUtil.isNotEmpty(otcOrderEntity)){
            String orderNo = otcOrderEntity.getOrderNo();
            otcAppealInfoVo.setOrderNo(orderNo);
            BigDecimal unitPrice = otcOrderEntity.getUnitPrice();
            otcAppealInfoVo.setUnitPrice(unitPrice);
            BigDecimal coinAmount = otcOrderEntity.getCoinAmount();
            otcAppealInfoVo.setCoinAmount(coinAmount);
            BigDecimal totalAmount = otcOrderEntity.getTotalAmount();
            otcAppealInfoVo.setTotalAmount(totalAmount);
            Integer status = otcOrderEntity.getStatus();
            otcAppealInfoVo.setStatus(status);
            Date payTime = otcOrderEntity.getPayTime();
            otcAppealInfoVo.setPayTime(payTime);
            Date finishTime = otcOrderEntity.getFinishTime();
            otcAppealInfoVo.setFinishTime(finishTime);
        }
        //获取对应的商户信息
        long payMdId = otcOrderEntity.getPayMbId();
        OtcMarketBussinessEntity otcMarketBussinessEntity = otcMarketBussinessMapper.selectById(payMdId);
        if(ObjectUtil.isNotEmpty(otcMarketBussinessEntity)){
            String nikename = otcMarketBussinessEntity.getNikename();
            otcAppealInfoVo.setNikename(nikename);
        }
        return otcAppealInfoVo;
    }
 
    @Override
    public OtcOrderEntity otcOrderInfo(long id) {
        OtcOrderEntity otcOrderEntity = otcOrderMapper.selectById(id);
        return otcOrderEntity;
    }
 
    @Override
    @Transactional
    public FebsResponse updateOrderInfo(OtcOrderEntity otcOrderEntity) {
        Long id = otcOrderEntity.getId();
        OtcOrderEntity otcOrder = otcOrderMapper.selectById(id);
        if(ObjectUtil.isEmpty(otcOrder)){
            return new FebsResponse().fail().message("连接超时,请刷新页面重试");
        }
        Integer status = otcOrder.getStatus();
        if(OtcOrderEntity.STATUS_ONE != status){
            return new FebsResponse().fail().message("当前订单不是待付款状态");
        }
        String payName = otcOrderEntity.getPayName();
        if(StrUtil.isEmpty(payName)){
            return new FebsResponse().fail().message("请输入付款人");
        }
        BigDecimal coinAmount = otcOrderEntity.getCoinAmount();
        /**
         * 获取订单信息--订单编号
         *      获取买单和买单
         *      更新payName字段
         *      更新订单状态
         */
        QueryWrapper<OtcOrderEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",otcOrder.getOrderNo());
        List<OtcOrderEntity> list = otcOrderMapper.selectList(queryWrapper);
        if(CollUtil.isNotEmpty(list)){
            for(OtcOrderEntity order : list){
                order.setStatus(OtcOrderEntity.STATUS_TWO);
                order.setPayName(payName);
                order.setPayTime(DateUtil.date());
                otcOrderMapper.updateById(order);
            }
        }
        return new FebsResponse().success();
    }
 
    @Override
    public OtcMarketBussinessEntity otcHuiKuan(long id) {
        return otcMarketBussinessMapper.selectById(id);
    }
 
    @Override
    @Transactional
    public FebsResponse otcHuiKuan(OtcMarketBussinessEntity otcMarketBussinessEntity) {
        Long id = otcMarketBussinessEntity.getId();
        OtcMarketBussinessEntity otcMarketBussiness = otcMarketBussinessMapper.selectById(id);
        if(ObjectUtil.isNotEmpty(otcMarketBussiness)){
            return new FebsResponse().fail().message("连接超时,请刷新页面重试");
        }
        BigDecimal coinAmount = otcMarketBussinessEntity.getCoinAmount();
        if(coinAmount.compareTo(BigDecimal.ZERO) <= 0){
            return new FebsResponse().fail().message("请输入正确的回款金额");
        }
        BigDecimal waitBackMoney = otcMarketBussiness.getWaitBackMoney();
        if(coinAmount.compareTo(waitBackMoney) > 0){
            return new FebsResponse().fail().message("请输入正确的回款金额");
        }
        /**
         * 增加已回款金额
         * 减少待回款金额
         * 增加汇款记录
         */
        BigDecimal hasBackMoney = otcMarketBussiness.getHasBackMoney();
        BigDecimal add = hasBackMoney.add(coinAmount);
        otcMarketBussiness.setHasBackMoney(add);
 
        BigDecimal subtract = waitBackMoney.subtract(coinAmount);
        otcMarketBussiness.setWaitBackMoney(subtract);
        otcMarketBussinessMapper.updateById(otcMarketBussiness);
 
        return new FebsResponse().success();
    }
 
    @Override
    public IPage<OtcSettingEntity> otcSettingList(OtcSettingEntity otcSettingEntity, QueryRequest request) {
        Page<OtcSettingEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<OtcSettingEntity> otcSettingEntitys = otcSettingMapper.otcSettingList(page, otcSettingEntity);
        return otcSettingEntitys;
    }
 
    @Override
    public OtcSettingEntity otcSettingUpdate(long id) {
        return otcSettingMapper.selectById(id);
    }
 
    @Override
    public FebsResponse updateOtcSetting(OtcSettingEntity otcSettingEntity) {
        Integer orderNum = otcSettingEntity.getOrderNum();
        if(ObjectUtil.isEmpty(orderNum) || orderNum < 0){
            return new FebsResponse().fail().message("请设置正确的总单数");
        }
        BigDecimal completionRate = otcSettingEntity.getCompletionRate();
        if(ObjectUtil.isEmpty(completionRate) || completionRate.compareTo(BigDecimal.ZERO) < 0){
            return new FebsResponse().fail().message("请设置正确的完成率");
        }
        BigDecimal totalAmount = otcSettingEntity.getTotalAmount();
        if(ObjectUtil.isEmpty(totalAmount) || totalAmount.compareTo(BigDecimal.ZERO) < 0){
            return new FebsResponse().fail().message("请设置正确的总金额");
        }
 
        Integer cancellNum = otcSettingEntity.getCancellNum();
        if(ObjectUtil.isEmpty(cancellNum) || cancellNum < 0){
            return new FebsResponse().fail().message("请设置正确的取消次数");
        }
        otcSettingMapper.updateById(otcSettingEntity);
        return new FebsResponse().success();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int reduceCoin(Long id) {
        OtcOrderEntity order = otcOrderMapper.selectById(id);
 
        MemberWalletCoinEntity saleWallet = memberWalletCoinMapper.findWalletCoinByMemberIdAndWalletCode(order.getMemberId(), "USDT");
        MemberWalletCoinEntity buyWallet = memberWalletCoinMapper.findWalletCoinByMemberIdAndWalletCode(order.getOppositeMemberId(), "USDT");
 
        memberWalletCoinMapper.updateBlockBalance(order.getCoinAmount(), buyWallet.getId());
        memberWalletCoinMapper.reduceFrozenBalance(order.getCoinAmount(), saleWallet.getId());
 
        otcOrderMapper.updateOrderStatusByOrderNo(OtcOrderEntity.STATUS_THREE, order.getOrderNo());
        return 1;
    }
 
    @Override
    public OtcEntrustOrderEntity otcEntrustListUpdate(long id) {
        return otcEntrustOrderMapper.selectById(id);
    }
 
    @Override
    public FebsResponse otcEntrustConfirm(OtcEntrustOrderEntity otcEntrustOrderEntity) {
        Long id = otcEntrustOrderEntity.getId();
        OtcEntrustOrderEntity otcEntrustOrder = otcEntrustOrderMapper.selectById(id);
        BigDecimal unitPrice = otcEntrustOrderEntity.getUnitPrice();
        if(ObjectUtil.isEmpty(unitPrice) || unitPrice.compareTo(BigDecimal.ZERO) < 0){
            return new FebsResponse().fail().message("请设置正确的单价");
        }
        otcEntrustOrder.setUnitPrice(unitPrice);
        BigDecimal coinAmount = otcEntrustOrderEntity.getCoinAmount();
        if(ObjectUtil.isEmpty(coinAmount) || coinAmount.compareTo(BigDecimal.ZERO) < 0){
            return new FebsResponse().fail().message("请设置正确的数量");
        }
        otcEntrustOrder.setCoinAmount(coinAmount);
        BigDecimal totalAmount = coinAmount.multiply(unitPrice);
        otcEntrustOrder.setTotalAmount(totalAmount);
 
        BigDecimal remainCoinAmount = otcEntrustOrderEntity.getRemainCoinAmount();
        if(ObjectUtil.isEmpty(remainCoinAmount) || remainCoinAmount.compareTo(BigDecimal.ZERO) < 0
                || totalAmount.compareTo(remainCoinAmount) < 0){
            return new FebsResponse().fail().message("请设置正确的剩余数量");
        }
        otcEntrustOrder.setRemainCoinAmount(remainCoinAmount);
        BigDecimal limitMinAmount = otcEntrustOrderEntity.getLimitMinAmount();
        if(ObjectUtil.isEmpty(limitMinAmount) || limitMinAmount.compareTo(BigDecimal.ZERO) < 0){
            return new FebsResponse().fail().message("请设置正确的最小限额");
        }
        otcEntrustOrder.setLimitMinAmount(limitMinAmount);
        BigDecimal limitMaxAmount = otcEntrustOrderEntity.getLimitMaxAmount();
        if(ObjectUtil.isEmpty(limitMaxAmount) || limitMaxAmount.compareTo(BigDecimal.ZERO) < 0
                || limitMaxAmount.compareTo(limitMinAmount) < 0 || totalAmount.compareTo(limitMaxAmount) < 0){
            return new FebsResponse().fail().message("请设置正确的最大限额");
        }
        otcEntrustOrder.setLimitMaxAmount(limitMaxAmount);
        otcEntrustOrder.setStatus(otcEntrustOrderEntity.getStatus());
        otcEntrustOrderMapper.updateById(otcEntrustOrder);
 
        return new FebsResponse().success();
    }
}