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() { @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 voList = new ArrayList<>(); List list = orderTaskExecutionDetailService.listCanExcuteTaskDetail(uid, (page - 1) * pageSize, pageSize); if (!list.isEmpty()) { List taskIds = new ArrayList<>(); for (OrderTaskExecutionDetail detail : list) { if (taskIds.contains(detail.getTaskId())) { continue; } taskIds.add(detail.getTaskId()); } // 获取手机号与支付宝账号信息 ClientAdditionalInfo clientAdditionalInfo = clientAdditionalInfoService.getClientAdditionalInfoByClientId(uid); List orderTaskList = orderTaskService.getOrderTaskByIds(taskIds); Map 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()); } } }