admin
2024-07-03 158b3be9024917bc5798486777dead4dd167abc7
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
package com.taoke.autopay.service.impl;
 
import com.taoke.autopay.dao.KeyOrderMapper;
import com.taoke.autopay.dao.WxUserSettingsMapper;
import com.taoke.autopay.entity.KeyOrder;
import com.taoke.autopay.entity.OrderCountTypeEnum;
import com.taoke.autopay.entity.OrderDistributeCountInfo;
import com.taoke.autopay.entity.WxUserSettings;
import com.taoke.autopay.exception.KeyOrderException;
import com.taoke.autopay.exception.WxOrderCountException;
import com.taoke.autopay.factory.OrderFactory;
import com.taoke.autopay.service.KeyOrderService;
import com.taoke.autopay.service.WxUserOrderCountService;
import com.taoke.autopay.service.WxUserSettingService;
import com.taoke.autopay.utils.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * @author hxh
 * @title: KeyOrderServiceImpl
 * @description: TODO
 * @date 2024/6/14 19:12
 */
@Service
public class KeyOrderServiceImpl implements KeyOrderService {
 
    @Resource
    private KeyOrderMapper keyOrderMapper;
 
    @Resource
    private WxUserSettingService wxUserSettingService;
 
    @Resource
    private WxUserOrderCountService wxUserOrderCountService;
 
 
    @Override
    public KeyOrder selectById(String id) {
        return keyOrderMapper.selectById(id);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public KeyOrder addKeyOrder(String key, Long uid, String day) throws KeyOrderException, WxOrderCountException {
        // 判断提交次数是否过量
        if (uid != null) {
            WxUserSettings settings = wxUserSettingService.selectByUid(uid);
            wxUserOrderCountService.addOrderCount(uid, OrderCountTypeEnum.SUBMIT_TOKEN_COUNT, day, 1, settings.getTotalOrderCountPerDay());
        }
        String id = OrderFactory.createId(key);
        KeyOrder order = keyOrderMapper.selectById(id);
        if (order != null) {
            throw new KeyOrderException("请勿重复提交口令");
        }
        order = new KeyOrder();
        order.setId(id);
        order.setKey(key);
        order.setUid(uid);
        order.setState(KeyOrder.STATE_NOT_PROCESS);
        order.setStateDesc("尚未处理");
        order.setCreateTime(new Date());
        keyOrderMapper.insertSelective(order);
 
 
        return order;
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(KeyOrder keyOrder) {
        if (keyOrder.getId() == null) {
            return;
        }
        KeyOrder old = keyOrderMapper.selectByPrimaryKeyForUpdate(keyOrder.getId());
        if (old == null) {
            return;
        }
        if (keyOrder.getUpdateTime() == null) {
            keyOrder.setUpdateTime(new Date());
        }
        keyOrderMapper.updateByPrimaryKeySelective(keyOrder);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void paySuccess(String id, String stateDesc,String day) throws WxOrderCountException {
        KeyOrder old = keyOrderMapper.selectByPrimaryKeyForUpdate(id);
        if(old==null){
           return;
        }
        if(old.getState() == KeyOrder.STATE_PAY){
            return;
        }
        if(old.getUid()!=null) {
            wxUserOrderCountService.addOrderCount(old.getUid(),OrderCountTypeEnum.DY_ORDER_PAY,day,1,null);
        }
        KeyOrder orderUpdate = new KeyOrder();
        orderUpdate.setId(id);
        orderUpdate.setState(KeyOrder.STATE_PAY);
        orderUpdate.setStateDesc(stateDesc);
        if(old.getPayTime()==null){
            orderUpdate.setPayTime(new Date());
        }
        update(orderUpdate);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setOrderInfo(String id, String orderNo, int orderState) throws KeyOrderException {
        KeyOrder old = keyOrderMapper.selectByPrimaryKeyForUpdate(id);
        if (old == null) {
            throw new KeyOrderException("口令不存在");
        }
        KeyOrder keyOrder = new KeyOrder();
        keyOrder.setId(id);
        // 默认1抖音
        keyOrder.setOrderType(1);
        keyOrder.setOrderNo(orderNo);
        if (keyOrder.getUpdateTime() == null) {
            keyOrder.setUpdateTime(new Date());
        }
        keyOrder.setOrderState(orderState);
        keyOrderMapper.updateByPrimaryKeySelective(keyOrder);
    }
 
    @Override
    public List<KeyOrder> list(KeyOrderMapper.DaoQuery query) {
        return keyOrderMapper.list(query);
    }
 
    @Override
    public long count(KeyOrderMapper.DaoQuery query) {
        return keyOrderMapper.count(query);
    }
 
    @Override
    public Long getCanDistributeUid() {
        // 最近1小时有活跃
        List<OrderDistributeCountInfo> list = keyOrderMapper.listDistributeUids(new Date(System.currentTimeMillis() - 1000 * 60 * 60L));
        if (list == null || list.size() == 0) {
            return null;
        }
        // count小于2直接视为0
        for (OrderDistributeCountInfo info : list) {
            if (info.getCount() < 2) {
                info.setCount(0);
            }
        }
 
        Comparator<OrderDistributeCountInfo> cm = new Comparator<OrderDistributeCountInfo>() {
            @Override
            public int compare(OrderDistributeCountInfo o1, OrderDistributeCountInfo o2) {
                return o1.getCount() - o2.getCount();
            }
        };
        list.sort(cm);
        if (list.get(0).getCount() == 0) {
            // 处理大多数设备都没有分配的情况
            // 将为0的数据随机分配
            List<OrderDistributeCountInfo> tempList = new ArrayList<>();
            for (OrderDistributeCountInfo info : list) {
                if (info.getCount() == 0) {
                    tempList.add(info);
                }
            }
            int index = new Random().nextInt(tempList.size());
            if (index < 0) {
                index = 0;
            }
            if (index >= tempList.size()) {
                index = tempList.size() - 1;
            }
            return tempList.get(index).getUid();
        }
        return list.get(0).getUid();
    }
 
    @Override
    public List<KeyOrder> listNotDistributed(int page, int pageSize) {
        return keyOrderMapper.listNotDistributed((page - 1) * pageSize, pageSize);
    }
 
    @Override
    public void deleteAll(Date maxCreateTime) {
        keyOrderMapper.deleteAll(maxCreateTime);
    }
}