Helius
2022-03-09 38179780b2f728abe6dd0e5aaa284c6ee6aa8380
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
package com.xcong.excoin.rabbit.consumer;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.configurations.RabbitMqConfig;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberSettingDao;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberSettingEntity;
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.OtcReturnMoneyDao;
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.OtcReturnMoney;
import com.xcong.excoin.utils.LogRecordUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "otc-job", havingValue = "true")
public class OtcConsumer {
 
    @Autowired
    private OtcMarketBussinessDao otcMarketBussinessDao;
 
    @Autowired
    private OtcEntrustOrderDao otcEntrustOrderDao;
 
    @Autowired
    private OtcOrderDao otcOrderDao;
 
    @Autowired
    private MemberDao memberDao;
 
    @Autowired
    private MemberSettingDao memberSettingDao;
 
    @Autowired
    private MemberWalletCoinDao memberWalletCoinDao;
 
    @Autowired
    private OtcReturnMoneyDao otcReturnMoneyDao;
 
    @RabbitListener(queues = RabbitMqConfig.QUEUE_MARKET_BUSSINESS)
    public void marketBussiness(String content) {
        log.info("收到市商消息:{}", content);
        JSONObject jsonObject = JSONObject.parseObject(content);
        Integer entrustOrderIdInt = jsonObject.getInteger("entrustOrderId");
        Long entrustOrderId = entrustOrderIdInt.longValue();
        Integer status = jsonObject.getInteger("status");
        OtcEntrustOrder entrustOrder = otcEntrustOrderDao.selectById(entrustOrderId);
 
        OtcMarketBussiness mb = otcMarketBussinessDao.selectMarketBussinessByMemberId(entrustOrder.getMemberId());
 
        if (!OtcOrder.STATUS_CANCEL.equals(status)) {
            if (OtcEntrustOrder.ORDER_TYPE_S.equals(entrustOrder.getOrderType())) {
                mb.setSaleOrderCnt(mb.getSaleOrderCnt() + 1);
                mb.setSaleTotalCnt(mb.getSaleTotalCnt() + 1);
            } else {
                mb.setBuyOrderCnt(mb.getBuyOrderCnt() + 1);
                mb.setTotalOrderCnt(mb.getTotalOrderCnt() + 1);
            }
        } else {
            if (OtcEntrustOrder.ORDER_TYPE_S.equals(entrustOrder.getOrderType())) {
                mb.setSaleTotalCnt(mb.getSaleTotalCnt() + 1);
            } else {
                mb.setTotalOrderCnt(mb.getTotalOrderCnt() + 1);
            }
        }
 
        if (mb.getTotalOrderCnt() != 0) {
            BigDecimal buyFinishRatio = BigDecimal.valueOf(mb.getBuyOrderCnt()).divide(BigDecimal.valueOf(mb.getTotalOrderCnt()), 8, BigDecimal.ROUND_DOWN);
            mb.setFinishRatio(buyFinishRatio);
        }
 
        if (mb.getSaleTotalCnt() != 0) {
            BigDecimal saleFinishRatio = BigDecimal.valueOf(mb.getSaleOrderCnt()).divide(BigDecimal.valueOf(mb.getSaleTotalCnt()), 8, BigDecimal.ROUND_DOWN);
            mb.setSaleFinishRatio(saleFinishRatio);
        }
 
 
        // 平均付款时间
//        BigDecimal avgPayTime = otcOrderDao.selectMemberAvgPayTime(mb.getMemberId());
//        // 平均放币时间
//        BigDecimal avgCoinTime = otcOrderDao.selectMemberAvgCoinTime(mb.getMemberId());
//
//        mb.setAvgPayTime(avgPayTime.intValue());
//        mb.setAvgCoinTime(avgCoinTime.intValue());
 
        otcMarketBussinessDao.updateById(mb);
    }
 
 
//    @RabbitListener(queues = RabbitMqConfig.QUEUE_MARKET_BUSSINESS)
    public void delayOrder(String content) {
        log.info("--{}-->{}", new Date(), content);
    }
 
 
//    @RabbitListener(queues = RabbitMqConfig.QUEUE_ORDER_RETURN)
//    @Transactional(rollbackFor = Exception.class)
//    public void orderReturn(String content) {
//        log.info("收到返佣消息:{}", content);
//        OtcOrder order = otcOrderDao.selectOrderUnEntrust(content);
//
//        MemberEntity member = memberDao.selectById(order.getMemberId());
//        List<String> refererIds = StrUtil.split(member.getRefererIds(), ',');
//
//        List<MemberEntity> parentMembers = memberDao.selectMemberListByReferer(refererIds);
//
//        if (CollUtil.isNotEmpty(parentMembers)) {
//            int isFirst = 1;
//            for (MemberEntity parent : parentMembers) {
//                MemberSettingEntity memberSetting = memberSettingDao.selectMemberSettingByMemberId(parent.getId());
//
//                BigDecimal returnRatio = BigDecimal.ZERO;
//                if (isFirst == 1) {
//                    returnRatio = memberSetting.getOtcReturnFirst();
//                } else {
//                    returnRatio = memberSetting.getOtcReturnSecond();
//                }
//
//                BigDecimal returnAmount = order.getCoinAmount().multiply(returnRatio);
//
//                MemberWalletCoinEntity wallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(parent.getId(), "USDT");
//                memberWalletCoinDao.updateBlockBalance(wallet.getId(), returnAmount, BigDecimal.ZERO, 0);
//
//                LogRecordUtils.insertMemberAccountMoneyChange(parent.getId(),"订单返利:" + content, returnAmount, "USDT", 1, 1);
//
//                OtcReturnMoney otcReturnMoney = new OtcReturnMoney();
//                otcReturnMoney.setToMemberId(parent.getId());
//                otcReturnMoney.setOrderNo(content);
//                otcReturnMoney.setFromMemberId(member.getId());
//                otcReturnMoney.setAmount(returnAmount);
//                otcReturnMoneyDao.insert(otcReturnMoney);
//
//                isFirst = 0;
//            }
//        }
//
//        order.setIsReturn(1);
//        otcOrderDao.updateIsReturnByOrderNo(1, content);
//    }
 
}