xiaoyong931011
2022-03-16 dca3e301410be4aee5a7895a1c2c1bbf90f3d03b
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
package com.xcong.excoin.quartz.job;
 
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.xcong.excoin.common.contants.AppContants;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.modules.otc.dao.OtcEntrustOrderDao;
import com.xcong.excoin.modules.otc.dao.OtcMarketBussinessDao;
import com.xcong.excoin.modules.otc.dao.OtcOrderDao;
import com.xcong.excoin.modules.otc.dao.OtcSettingDao;
import com.xcong.excoin.modules.otc.entity.OtcEntrustOrder;
import com.xcong.excoin.modules.otc.entity.OtcMarketBussiness;
import com.xcong.excoin.modules.otc.entity.OtcOrder;
import com.xcong.excoin.modules.otc.entity.OtcSetting;
import com.xcong.excoin.modules.otc.service.OtcOrderService;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "otc-job", havingValue = "true")
public class OtcOrderJob {
 
    @Autowired
    private OtcOrderDao otcOrderDao;
    @Autowired
    private OtcEntrustOrderDao otcEntrustOrderDao;
    @Autowired
    private OtcMarketBussinessDao otcMarketBussinessDao;
    @Autowired
    private MemberWalletCoinDao memberWalletCoinDao;
    @Autowired
    private OtcSettingDao otcSettingDao;
    @Autowired
    private RedisUtils redisUtils;
 
    @Scheduled(cron = "0/1 * * * * ? ")
    public void autoCancelOrder() {
        List<OtcOrder> otcOrders = otcOrderDao.selectOrderListWithStatusAndType(OtcEntrustOrder.ORDER_TYPE_B, OtcOrder.STATUS_SUBMIT);
        if (CollUtil.isNotEmpty(otcOrders)) {
            for (OtcOrder otcOrder : otcOrders) {
                long between = DateUtil.between(new Date(), DateUtil.offsetMinute(otcOrder.getCreateTime(), 30), DateUnit.SECOND, false);
 
                if (between <= 0) {
                    OtcOrder saleOrder = otcOrderDao.selectOrderByOrderNoAndType(otcOrder.getOrderNo(), OtcEntrustOrder.ORDER_TYPE_S);
                    if (!saleOrder.getMemberId().equals(saleOrder.getEntrustMemberId())) {
                        MemberWalletCoinEntity wallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(saleOrder.getMemberId(), "USDT");
                        memberWalletCoinDao.subFrozenBalance(saleOrder.getMemberId(), wallet.getId(), saleOrder.getCoinAmount());
                    }
 
                    otcEntrustOrderDao.updateRemainAmount(otcOrder.getEntrustOrderId(), otcOrder.getCoinAmount());
                    otcOrderDao.updateOrderStatusByOrderNo(OtcOrder.STATUS_CANCEL, null, otcOrder.getOrderNo());
 
                    Long memberId;
                    if (otcOrder.getMemberId().equals(otcOrder.getEntrustMemberId())) {
                        memberId = otcOrder.getOppositeMemberId();
                    } else {
                        memberId = otcOrder.getMemberId();
                    }
 
                    OtcSetting setting = otcSettingDao.selectById(1L);
                    String times = redisUtils.getString(AppContants.OTC_ORDER_CANCEL_TIMES + memberId);
                    if (StrUtil.isNotBlank(times)) {
                        int i = Integer.parseInt(times);
                        i++;
                        if (i >= setting.getCancellNum()) {
                            redisUtils.set(AppContants.OTC_ORDER_CANCEL_TIMES + memberId, i, 86400);
                        } else {
                            redisUtils.set(AppContants.OTC_ORDER_CANCEL_TIMES + memberId, i);
                        }
                    } else {
                        redisUtils.set(AppContants.OTC_ORDER_CANCEL_TIMES + memberId, 1, 86400);
                    }
                }
            }
        }
    }
 
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void marketBussinessJob() {
        List<OtcMarketBussiness> list = otcMarketBussinessDao.selectList(null);
        if (CollUtil.isNotEmpty(list)) {
            for (OtcMarketBussiness mb : list) {
                // 服务人数
                Integer buyCnt = otcOrderDao.selectMemberCntForEntrust(mb.getMemberId());
                // 买单数
                Integer buyTotalCnt = otcOrderDao.selectTotalOrderCount(mb.getMemberId(), null, OtcEntrustOrder.ORDER_TYPE_B);
                // 买单完成率
                Integer buyFinishCnt = otcOrderDao.selectTotalOrderCount(mb.getMemberId(), OtcOrder.STATUS_FINISH, OtcEntrustOrder.ORDER_TYPE_B);
                if (buyTotalCnt != null && buyTotalCnt != 0) {
                    BigDecimal finishRatio = BigDecimal.valueOf(buyFinishCnt).divide(BigDecimal.valueOf(buyTotalCnt), 4, BigDecimal.ROUND_DOWN);
                    mb.setFinishRatio(finishRatio);
                }
 
                // 买单数
                Integer saleTotalCnt = otcOrderDao.selectTotalOrderCount(mb.getMemberId(), null, OtcEntrustOrder.ORDER_TYPE_S);
                // 买单完成率
                Integer saleFinishCnt = otcOrderDao.selectTotalOrderCount(mb.getMemberId(), OtcOrder.STATUS_FINISH, OtcEntrustOrder.ORDER_TYPE_S);
                if (saleTotalCnt != null && saleTotalCnt != 0) {
                    BigDecimal finishRatio = BigDecimal.valueOf(saleFinishCnt).divide(BigDecimal.valueOf(saleTotalCnt), 4, BigDecimal.ROUND_DOWN);
                    mb.setSaleFinishRatio(finishRatio);
                }
                // 平均付款时间
                BigDecimal avgPayTime = otcOrderDao.selectMemberAvgPayTime(mb.getMemberId());
                // 平均放币时间
                BigDecimal avgCoinTime = otcOrderDao.selectMemberAvgCoinTime(mb.getMemberId());
 
                mb.setBuyCnt(buyCnt);
                mb.setTotalOrderCnt(buyTotalCnt);
                mb.setSaleOrderCnt(saleTotalCnt);
                mb.setAvgPayTime(avgPayTime.intValue());
                mb.setAvgCoinTime(avgCoinTime.intValue());
 
                otcMarketBussinessDao.updateById(mb);
            }
        }
    }
 
}