admin
2025-08-08 035edfa382d349ba66240fbfef68c14c7cfc95d1
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package com.taoke.autopay.service.impl.js2;
 
import com.taoke.autopay.dao.OrderTaskExecutionDetailMapper;
import com.taoke.autopay.dao.OrderTaskMapper;
import com.taoke.autopay.entity.js2.OrderTask;
import com.taoke.autopay.entity.js2.OrderTaskExecutionDetail;
import com.taoke.autopay.exception.OrderTaskExecutionDetailException;
import com.taoke.autopay.service.js2.OrderTaskExecutionDetailService;
import com.taoke.autopay.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
/**
 * @author 
 * @title: OrderTaskExecutionDetailServiceImpl
 * @description: 下单任务执行详情服务实现类
 * @date 2025/7/28
 */
@Service
public class OrderTaskExecutionDetailServiceImpl implements OrderTaskExecutionDetailService {
    
    @Resource
    private OrderTaskExecutionDetailMapper orderTaskExecutionDetailMapper;
 
    private static final Logger logger = LoggerFactory.getLogger(OrderTaskExecutionDetailServiceImpl.class);
    @Autowired
    private OrderTaskMapper orderTaskMapper;
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public OrderTaskExecutionDetail createOrderTaskExecutionDetail(OrderTaskExecutionDetail orderTaskExecutionDetail) throws OrderTaskExecutionDetailException {
        validateOrderTaskExecutionDetail(orderTaskExecutionDetail);
 
        // 设置创建时间
        if (orderTaskExecutionDetail.getCreateTime() == null) {
            orderTaskExecutionDetail.setCreateTime(new Date());
        }
        
        // 设置更新时间
        orderTaskExecutionDetail.setUpdateTime(new Date());
        
        // 插入数据库
        orderTaskExecutionDetailMapper.insertSelective(orderTaskExecutionDetail);
        
        return orderTaskExecutionDetail;
    }
    
    @Override
    public OrderTaskExecutionDetail getOrderTaskExecutionDetailById(String id) {
        if (id == null) {
            return null;
        }
        return orderTaskExecutionDetailMapper.selectByPrimaryKey(id);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public OrderTaskExecutionDetail getOrderTaskExecutionDetailByIdForUpdate(String id) {
        if (id == null) {
            return null;
        }
        return orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateOrderTaskExecutionDetail(OrderTaskExecutionDetail orderTaskExecutionDetail) throws OrderTaskExecutionDetailException {
        if (orderTaskExecutionDetail == null || orderTaskExecutionDetail.getId() == null) {
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "下单任务执行详情或详情ID不能为空");
        }
 
        // 设置更新时间
        orderTaskExecutionDetail.setUpdateTime(new Date());
 
        // 更新数据库
        orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(orderTaskExecutionDetail);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteOrderTaskExecutionDetail(String id) throws OrderTaskExecutionDetailException {
        if (id == null) {
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
        }
 
        // 删除数据库记录
        orderTaskExecutionDetailMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public List<OrderTaskExecutionDetail> listOrderTaskExecutionDetails(OrderTaskExecutionDetailMapper.DaoQuery query, int page, int pageSize) {
        if (query == null) {
            query = new OrderTaskExecutionDetailMapper.DaoQuery();
        }
        query.start = (long) (page - 1) * pageSize;
        query.count = pageSize;
        return orderTaskExecutionDetailMapper.list(query);
    }
 
    @Override
    public long countOrderTaskExecutionDetails(OrderTaskExecutionDetailMapper.DaoQuery query) {
        if (query == null) {
            query = new OrderTaskExecutionDetailMapper.DaoQuery();
        }
        return orderTaskExecutionDetailMapper.count(query);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void orderSuccess(String id, String orderNo, String productTitle, String shopName, Date orderTime) throws OrderTaskExecutionDetailException {
        if (id == null) {
            logger.error("任务执行详情ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
        }
 
        // 获取任务执行详情(带锁)
        OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
        if (detail == null) {
            logger.error("未找到ID为{}的任务执行详情", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
        }
 
        if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_NOT_ORDERED){
            logger.error("尚未处于未下单状态:{}", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于未下单状态");
        }
 
        OrderTaskExecutionDetail update=OrderTaskExecutionDetail.builder()
                .id(id)
                .orderNo(orderNo)
                .productName(productTitle)
                .shopName(shopName)
                .orderTime(orderTime)
                .executionStatus(OrderTaskExecutionDetail.STATUS_ORDERED)
                .statusDescription("下单成功")
                .updateTime(new Date())
                .build();
        // 更新数据库
        orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(update);
        // 更新任务下单成功数量
        OrderTask orderTask = orderTaskMapper.selectByPrimaryKeyForUpdate(detail.getTaskId());
        orderTaskMapper.updateByPrimaryKeySelective( OrderTask.builder()
                .id(orderTask.getId())
                .updateTime(new Date())
                .completedOrderCount(orderTask.getCompletedOrderCount() + 1)
                .build());
        logger.info("任务执行详情ID:{} 下单成功,订单号:{}", id, orderNo);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void orderFailure(String id, String failureReason) throws OrderTaskExecutionDetailException {
        if (id == null) {
            logger.error("任务执行详情ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(failureReason)) {
            logger.warn("下单失败原因为空,使用默认原因");
            failureReason = "未知错误";
        }
 
        // 获取任务执行详情(带锁)
        OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
        if (detail == null) {
            logger.error("未找到ID为{}的任务执行详情", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
        }
        if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_NOT_ORDERED){
            logger.error("尚未处于未下单状态:{}", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于未下单状态");
        }
 
        OrderTaskExecutionDetail update=OrderTaskExecutionDetail.builder()
                .id(id)
                .executionStatus(OrderTaskExecutionDetail.STATUS_ORDER_FAILED)
                .statusDescription("下单失败:" + failureReason)
                .updateTime(new Date())
                .build();
        // 更新数据库
        orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(update);
        logger.info("任务执行详情ID:{} 下单失败,原因:{}", id, failureReason);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void confirmReceiptSuccess(String id, String couponCode) throws OrderTaskExecutionDetailException {
        if (id == null) {
            logger.error("任务执行详情ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
        }
 
        // 获取任务执行详情(带锁)
        OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
        if (detail == null) {
            logger.error("未找到ID为{}的任务执行详情", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
        }
 
        if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_ORDERED){
            logger.error("尚未处于下单状态:{}", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于下单状态");
        }
 
        // 更新字段
        OrderTaskExecutionDetail update = OrderTaskExecutionDetail.builder()
                .id(id)
                .couponCode(couponCode)
                .receiveTime(new Date())
                .executionStatus(OrderTaskExecutionDetail.STATUS_RECEIVE_SUCCESS)
                .statusDescription("确认收货成功")
                .updateTime(new Date())
                .build();
        // 更新数据库
        orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(update);
 
        // 更新任务确认收货成功数量
        OrderTask orderTask = orderTaskMapper.selectByPrimaryKeyForUpdate(detail.getTaskId());
        orderTaskMapper.updateByPrimaryKeySelective( OrderTask.builder()
                .id(orderTask.getId())
                .updateTime(new Date())
                .receivedOrderCount(orderTask.getReceivedOrderCount() + 1)
                .build());
 
        logger.info("任务执行详情ID:{} 确认收货成功,券码:{}", id, couponCode);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void confirmReceiptFailure(String id, String reason) throws OrderTaskExecutionDetailException {
        if (id == null) {
            logger.error("任务执行详情ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(reason)) {
            logger.warn("确认收货失败原因为空,使用默认原因");
            reason = "未知错误";
        }
 
        // 获取任务执行详情(带锁)
        OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
        if (detail == null) {
            logger.error("未找到ID为{}的任务执行详情", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
        }
 
        if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_ORDERED){
            logger.error("尚未处于下单状态:{}", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于下单状态");
        }
 
 
        // 更新字段
        detail.setExecutionStatus(OrderTaskExecutionDetail.STATUS_RECEIVE_FAILED); // 设置为确认收货失败状态
        detail.setStatusDescription("确认收货失败:" + reason);
 
        // 更新时间
        detail.setUpdateTime(new Date());
 
        // 更新数据库
        orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(detail);
        logger.info("任务执行详情ID:{} 确认收货失败,原因:{}", id, reason);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void reviewSuccess(String id) throws OrderTaskExecutionDetailException {
        if (id == null) {
            logger.error("任务执行详情ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
        }
 
        // 获取任务执行详情(带锁)
        OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
        if (detail == null) {
            logger.error("未找到ID为{}的任务执行详情", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
        }
 
        if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_RECEIVE_SUCCESS){
            logger.error("尚未处于确认收货状态:{}", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于确认收货状态");
        }
 
 
        // 更新字段
        OrderTaskExecutionDetail update = OrderTaskExecutionDetail.builder()
                .id(id)
                .reviewTime(new Date())
                .executionStatus(OrderTaskExecutionDetail.STATUS_REVIEW_SUCCESS)
                .statusDescription("评价成功")
                .updateTime(new Date())
                .build();
        // 更新数据库
        orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(update);
        // 更新任务评价成功数量
        OrderTask orderTask = orderTaskMapper.selectByPrimaryKeyForUpdate(detail.getTaskId());
        orderTaskMapper.updateByPrimaryKeySelective( OrderTask.builder()
                .id(orderTask.getId())
                .updateTime(new Date())
                .reviewedOrderCount(orderTask.getReviewedOrderCount() + 1)
                .build());
 
        logger.info("任务执行详情ID:{} 评价成功", id);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void reviewFailure(String id, String reason) throws OrderTaskExecutionDetailException {
        if (id == null) {
            logger.error("任务执行详情ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(reason)) {
            logger.warn("评价失败原因为空,使用默认原因");
            reason = "未知错误";
        }
 
        // 获取任务执行详情(带锁)
        OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
        if (detail == null) {
            logger.error("未找到ID为{}的任务执行详情", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
        }
 
        if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_RECEIVE_SUCCESS){
            logger.error("尚未处于确认收货状态:{}", id);
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于确认收货状态");
        }
 
        // 更新字段
        detail.setExecutionStatus(OrderTaskExecutionDetail.STATUS_REVIEW_FAILED); // 设置为评价失败状态
        detail.setStatusDescription("评价失败:" + reason);
 
        // 更新时间
        detail.setUpdateTime(new Date());
 
        // 更新数据库
        orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(detail);
        logger.info("任务执行详情ID:{} 评价失败,原因:{}", id, reason);
    }
 
    @Override
    public List<OrderTaskExecutionDetail> listCanExcuteTaskDetail(Long clientId, int page, int pageSize) {
        // 获取30分钟以内的订单任务执行详情
        return orderTaskExecutionDetailMapper.listCanExcuteTaskDetail(clientId, new Date(System.currentTimeMillis() - 1000*60*30L));
    }
 
    @Override
    public long countCanExcuteTaskDetail(Long clientId) {
        return orderTaskExecutionDetailMapper.countCanExcuteTaskDetail(clientId, new Date(System.currentTimeMillis() - 1000*60*30L));
    }
 
    /**
     * 验证订单任务执行详情的必填字段
     *
     * @param orderTaskExecutionDetail 订单任务执行详情对象
     * @throws OrderTaskExecutionDetailException 如果必填字段为空则抛出异常
     */
    private void validateOrderTaskExecutionDetail(OrderTaskExecutionDetail orderTaskExecutionDetail) throws OrderTaskExecutionDetailException {
        if (orderTaskExecutionDetail == null) {
            logger.error("下单任务执行详情不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "下单任务执行详情不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(orderTaskExecutionDetail.getId())) {
            logger.error("下单任务执行详情ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "下单任务执行详情ID不能为空");
        }
 
        if (orderTaskExecutionDetail.getTaskId() == null) {
            logger.error("任务ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务ID不能为空");
        }
 
        if (orderTaskExecutionDetail.getClientId() == null) {
            logger.error("客户端ID不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "客户端ID不能为空");
        }
 
        if (orderTaskExecutionDetail.getExecutionStatus() == null) {
            logger.error("执行状态不能为空");
            throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "执行状态不能为空");
        }
    }
}