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
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.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 {
 
    @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;
 
    /**
     * 获取设备可执行的任务列表
     *
     * @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不能为空");
            }
            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);
                }
            }
            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) {
            loggerTask.error("获取任务列表失败: {}", e.getMessage(), e);
            return JsonUtil.loadFalseResult("系统异常:" + e.getMessage());
        }
    }
 
    /**
     * 下单成功
     *
     * @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) {
        loggerTask.info("orderSuccess[{}]: {}-{}-{}-{}-{}", 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("订单号不能为空");
            }
 
            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) {
        loggerTask.info("orderFailure[{}]: {}-{}", 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) {
        loggerTask.info("confirmReceiptSuccess[{}]: {}-{}", 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) {
        loggerTask.info("confirmReceiptFailure[{}]: {}-{}", 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) {
        loggerTask.info("reviewSuccess[{}]: {}", 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) {
        loggerTask.info("reviewFailure[{}]: {}-{}", 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());
        }
    }
}