admin
2025-08-20 f318c9c7c127b00f353bf45f273096d1dc4b424f
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
package com.taoke.autopay.controller.client.js2;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.taoke.autopay.dao.OrderTaskExecutionDetailMapper;
import com.taoke.autopay.entity.ClientAdditionalInfo;
import com.taoke.autopay.entity.js2.OrderTask;
import com.taoke.autopay.entity.js2.OrderTaskExecutionDetail;
import com.taoke.autopay.factory.js2.OrderTaskExecutionDetailFactory;
import com.taoke.autopay.service.ClientAdditionalInfoService;
import com.taoke.autopay.service.ClientInfoService;
import com.taoke.autopay.service.js2.OrderTaskExecutionDetailService;
import com.taoke.autopay.service.js2.OrderTaskService;
import com.taoke.autopay.utils.TimeUtil;
import com.taoke.autopay.vo.OrderTaskExecutionDetailVO;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * 手机客户端下单任务控制器
 */
@Controller
@RequestMapping("api/client/js2/task")
public class OrderTaskController {
 
    private Logger loggerDoOrder = LoggerFactory.getLogger("doOrderLogger");
 
    @Resource
    private ClientAdditionalInfoService clientAdditionalInfoService;
 
    private Logger loggerTask = LoggerFactory.getLogger("taskLogger");
 
    private Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new TypeAdapter<Date>() {
        @Override
        public void write(JsonWriter out, Date value) throws IOException {
            String desc = "";
            if (value != null) {
                desc = TimeUtil.getGernalTime(value.getTime(), "yyyy-MM-dd HH:mm:ss");
                out.value(desc);
            } else {
                out.value("");
            }
        }
 
        @Override
        public Date read(JsonReader in) throws IOException {
            return new Date();
        }
    }).create();
 
    @Resource
    private OrderTaskExecutionDetailService orderTaskExecutionDetailService;
 
    @Resource
    private OrderTaskService orderTaskService;
 
    @Resource
    private ClientInfoService clientInfoService;
 
    /**
     * 获取设备可执行的任务列表
     *
     * @param uid      客户端ID
     * @param page     页码
     * @param pageSize 每页数量
     * @return 任务列表
     */
    @ResponseBody
    @RequestMapping("list")
    public String list(Long uid, int page, int pageSize) {
        try {
            if (uid == null) {
                return JsonUtil.loadFalseResult("客户端ID不能为空");
            }
            clientInfoService.setActiveTime(uid, new Date());
            List<OrderTaskExecutionDetailVO> voList = new ArrayList<>();
            List<OrderTaskExecutionDetail> list = orderTaskExecutionDetailService.listCanExcuteTaskDetail(uid, (page - 1) * pageSize, pageSize);
            if (!list.isEmpty()) {
                List<Long> taskIds = new ArrayList<>();
                for (OrderTaskExecutionDetail detail : list) {
                    if (taskIds.contains(detail.getTaskId())) {
                        continue;
                    }
                    taskIds.add(detail.getTaskId());
                }
                // 获取手机号与支付宝账号信息
                ClientAdditionalInfo clientAdditionalInfo = clientAdditionalInfoService.getClientAdditionalInfoByClientId(uid);
                List<OrderTask> orderTaskList = orderTaskService.getOrderTaskByIds(taskIds);
                Map<Long, OrderTask> orderTaskMap = new HashMap<>();
                for (OrderTask task : orderTaskList) {
                    orderTaskMap.put(task.getId(), task);
                }
                for (OrderTaskExecutionDetail detail : list) {
                    OrderTaskExecutionDetailVO vo = OrderTaskExecutionDetailFactory.createOrderTaskExecutionDetailVO(orderTaskMap.get(detail.getTaskId()), detail, clientAdditionalInfo);
                    voList.add(vo);
                }
            }
            voList.sort((o1, o2) -> (int) (o1.getCreateTime().getTime() - o2.getCreateTime().getTime()));
            long count = orderTaskExecutionDetailService.countCanExcuteTaskDetail(uid);
            JSONObject data = new JSONObject();
            data.put("list", gson.toJson(voList));
            data.put("count", count);
            return JsonUtil.loadTrueResult(data);
        } catch (Exception e) {
            e.printStackTrace();
            loggerTask.error("获取任务列表失败: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("系统异常:" + e.getMessage());
        }
    }
 
 
    @ResponseBody
    @RequestMapping("canOrder")
    public String canOrder(Long uid, String id) {
        if (uid == null) {
            return JsonUtil.loadFalseResult("客户端ID不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(id)) {
            return JsonUtil.loadFalseResult("任务ID不能为空");
        }
 
        OrderTaskExecutionDetail detail = orderTaskExecutionDetailService.getOrderTaskExecutionDetailByIdForUpdate(id);
        if (detail == null || detail.getExecutionStatus() != OrderTaskExecutionDetail.STATUS_NOT_ORDERED) {
            return JsonUtil.loadFalseResult("任务不存在或已执行");
        }
        return JsonUtil.loadTrueResult("");
    }
 
    /**
     * 下单成功
     *
     * @param uid          客户端ID
     * @param id
     * 任务执行详情ID
     * @param orderNo      订单号
     * @param productTitle 商品标题
     * @param shopName     店铺名称
     * @param orderTimeStr 下单时间字符串
     * @return 操作结果
     */
    @ResponseBody
    @RequestMapping("orderSuccess")
    public String orderSuccess(Long uid, String id, String orderNo, String productTitle, String shopName, String orderTimeStr) {
        loggerDoOrder.info("下单成功[{}]: {}-{}-{}-{}-{}", uid, id, orderNo, productTitle, shopName, orderTimeStr);
        try {
            if (uid == null) {
                return JsonUtil.loadFalseResult("客户端ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(id)) {
                return JsonUtil.loadFalseResult("任务ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(orderNo)) {
                return JsonUtil.loadFalseResult("订单号不能为空");
            }
 
 
            orderNo = orderNo.split("订单编号")[1];
 
            Date orderTime = null;
            if (!StringUtil.isNullOrEmpty(orderTimeStr)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                orderTime = sdf.parse(orderTimeStr);
            } else {
                orderTime = new Date();
            }
 
            orderTaskExecutionDetailService.orderSuccess(id, orderNo, productTitle, shopName, orderTime);
            return JsonUtil.loadTrueResult("");
        } catch (ParseException e) {
            loggerTask.error("下单成功处理失败,时间格式错误: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("时间格式错误");
        } catch (Exception e) {
            loggerTask.error("下单成功处理失败: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("系统异常:" + e.getMessage());
        }
    }
 
    /**
     * 下单失败,上传失败原因
     *
     * @param uid           客户端ID
     * @param id            任务执行详情ID
     * @param failureReason 失败原因
     * @return 操作结果
     */
    @ResponseBody
    @RequestMapping("orderFailure")
    public String orderFailure(Long uid, String id, String failureReason) {
        loggerDoOrder.info("下单失败[{}]: {}-{}", uid, id, failureReason);
        try {
            if (uid == null) {
                return JsonUtil.loadFalseResult("客户端ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(id)) {
                return JsonUtil.loadFalseResult("任务ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(failureReason)) {
                return JsonUtil.loadFalseResult("失败原因不能为空");
            }
 
            orderTaskExecutionDetailService.orderFailure(id, failureReason);
            return JsonUtil.loadTrueResult("");
        } catch (Exception e) {
            loggerTask.error("下单失败处理失败: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("系统异常:" + e.getMessage());
        }
    }
 
    /**
     * 确认收货成功,上传券码
     *
     * @param uid        客户端ID
     * @param id         任务执行详情ID
     * @param couponCode 券码
     * @return 操作结果
     */
    @ResponseBody
    @RequestMapping("confirmReceiptSuccess")
    public String confirmReceiptSuccess(Long uid, String id, String couponCode) {
        loggerDoOrder.info("确认收货成功[{}]: {}-{}", uid, id, couponCode);
        try {
            if (uid == null) {
                return JsonUtil.loadFalseResult("客户端ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(id)) {
                return JsonUtil.loadFalseResult("任务ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(couponCode)) {
                return JsonUtil.loadFalseResult("券码不能为空");
            }
 
            orderTaskExecutionDetailService.confirmReceiptSuccess(id, couponCode);
            return JsonUtil.loadTrueResult("");
        } catch (Exception e) {
            loggerTask.error("确认收货成功处理失败: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("系统异常:" + e.getMessage());
        }
    }
 
    /**
     * 确认收货失败,上传失败原因
     *
     * @param uid    客户端ID
     * @param id     任务执行详情ID
     * @param reason 失败原因
     * @return 操作结果
     */
    @ResponseBody
    @RequestMapping("confirmReceiptFailure")
    public String confirmReceiptFailure(Long uid, String id, String reason) {
        loggerDoOrder.info("确认收货失败[{}]: {}-{}", uid, id, reason);
        try {
            if (uid == null) {
                return JsonUtil.loadFalseResult("客户端ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(id)) {
                return JsonUtil.loadFalseResult("任务ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(reason)) {
                return JsonUtil.loadFalseResult("失败原因不能为空");
            }
 
            orderTaskExecutionDetailService.confirmReceiptFailure(id, reason);
            return JsonUtil.loadTrueResult("");
        } catch (Exception e) {
            loggerTask.error("确认收货失败处理失败: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("系统异常:" + e.getMessage());
        }
    }
 
    /**
     * 评价成功
     *
     * @param uid 客户端ID
     * @param id  任务执行详情ID
     * @return 操作结果
     */
    @ResponseBody
    @RequestMapping("reviewSuccess")
    public String reviewSuccess(Long uid, String id) {
        loggerDoOrder.info("评价成功[{}]: {}", uid, id);
        try {
            if (uid == null) {
                return JsonUtil.loadFalseResult("客户端ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(id)) {
                return JsonUtil.loadFalseResult("任务ID不能为空");
            }
 
            orderTaskExecutionDetailService.reviewSuccess(id);
            return JsonUtil.loadTrueResult("");
        } catch (Exception e) {
            loggerTask.error("评价成功处理失败: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("系统异常:" + e.getMessage());
        }
    }
 
    /**
     * 评价失败,上传失败原因
     *
     * @param uid    客户端ID
     * @param id     任务执行详情ID
     * @param reason 失败原因
     * @return 操作结果
     */
    @ResponseBody
    @RequestMapping("reviewFailure")
    public String reviewFailure(Long uid, String id, String reason) {
        loggerDoOrder.info("评价失败[{}]: {}-{}", uid, id, reason);
        try {
            if (uid == null) {
                return JsonUtil.loadFalseResult("客户端ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(id)) {
                return JsonUtil.loadFalseResult("任务ID不能为空");
            }
 
            if (StringUtil.isNullOrEmpty(reason)) {
                return JsonUtil.loadFalseResult("失败原因不能为空");
            }
 
            orderTaskExecutionDetailService.reviewFailure(id, reason);
            return JsonUtil.loadTrueResult("");
        } catch (Exception e) {
            loggerTask.error("评价失败处理失败: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("系统异常:" + e.getMessage());
        }
    }
}