package com.taoke.autopay.controller.admin.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.dao.OrderTaskMapper; import com.taoke.autopay.entity.js2.OrderTask; import com.taoke.autopay.entity.js2.OrderTaskExecutionDetail; import com.taoke.autopay.exception.OrderTaskException; import com.taoke.autopay.factory.js2.OrderTaskFactory; 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.admin.js2.OrderTaskVO; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; 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.sql.Time; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Calendar; import java.util.ArrayList; /** * 下单任务管理 */ @Controller @RequestMapping("/admin/api/ordertask/js2") public class AdminOrderTaskController { @Resource private OrderTaskService orderTaskService; @Resource private OrderTaskExecutionDetailMapper orderTaskExecutionDetailMapper; 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(); @Autowired private OrderTaskExecutionDetailService orderTaskExecutionDetailService; /** * 任务列表查询(按照关键字,日期查询) */ @ResponseBody @RequestMapping("list") public String list(String keyword, String startDate, String endDate, Integer status, int page, int limit) { try { OrderTaskMapper.DaoQuery query = new OrderTaskMapper.DaoQuery(); if (!StringUtil.isNullOrEmpty(keyword)) { query.keywordContent = keyword; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if (!StringUtil.isNullOrEmpty(startDate)) { query.minCreateTime = sdf.parse(startDate); } if (!StringUtil.isNullOrEmpty(endDate)) { Date end = sdf.parse(endDate); // 设置为当天的最后时刻 Calendar cal = Calendar.getInstance(); cal.setTime(end); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); query.maxCreateTime = cal.getTime(); } if (status != null) { query.status = status; } query.sortList = Arrays.asList(new String[]{"create_time desc"}); query.start = (page - 1) * limit; query.count = limit; List list = orderTaskService.listOrderTasks(query, page, limit); long count = orderTaskService.countOrderTasks(query); // 转换为VO对象 List voList = new ArrayList<>(); for (OrderTask orderTask : list) { voList.add(OrderTaskFactory.createOrderTaskVO(orderTask)); } JSONObject data = new JSONObject(); data.put("list", gson.toJson(voList)); data.put("count", count); return JsonUtil.loadTrueResult(data); } catch (ParseException e) { return JsonUtil.loadFalseResult("日期格式错误"); } catch (Exception e) { return JsonUtil.loadFalseResult(e.getMessage()); } } /** * 获取单个任务信息 */ @ResponseBody @RequestMapping("get") public String get(Long id) { try { if (id == null) { return JsonUtil.loadFalseResult("任务ID不能为空"); } OrderTask orderTask = orderTaskService.getOrderTaskById(id); if (orderTask == null) { return JsonUtil.loadFalseResult("任务不存在"); } OrderTaskVO orderTaskVO = OrderTaskFactory.createOrderTaskVO(orderTask); return JsonUtil.loadTrueResult(gson.toJson(orderTaskVO)); } catch (Exception e) { return JsonUtil.loadFalseResult("系统异常:" + e.getMessage()); } } /** * 新增任务 */ @ResponseBody @RequestMapping("add") public String add(OrderTaskVO orderTaskVO) { try { if (orderTaskVO == null) { return JsonUtil.loadFalseResult("任务信息不能为空"); } // 将VO转换为实体 OrderTask orderTask = OrderTaskFactory.createOrderTask(orderTaskVO); OrderTask result = orderTaskService.createOrderTask(orderTask); OrderTaskVO resultVO = OrderTaskFactory.createOrderTaskVO(result); return JsonUtil.loadTrueResult(gson.toJson(resultVO)); } catch (ParseException e) { return JsonUtil.loadFalseResult("日期格式错误:" + e.getMessage()); } catch (OrderTaskException e) { return JsonUtil.loadFalseResult(e.getMessage()); } catch (Exception e) { return JsonUtil.loadFalseResult("系统异常:" + e.getMessage()); } } /** * 删除任务 */ @ResponseBody @RequestMapping("delete") public String delete(Long id) { try { if (id == null) { return JsonUtil.loadFalseResult("任务ID不能为空"); } orderTaskService.deleteOrderTask(id); return JsonUtil.loadTrueResult(""); } catch (OrderTaskException e) { return JsonUtil.loadFalseResult(e.getMessage()); } catch (Exception e) { return JsonUtil.loadFalseResult("系统异常:" + e.getMessage()); } } /** * 修改任务 */ @ResponseBody @RequestMapping("update") public String update(OrderTaskVO orderTaskVO) { try { if (orderTaskVO == null || orderTaskVO.getId() == null) { return JsonUtil.loadFalseResult("任务信息不完整"); } // 先从数据库获取原始任务信息 OrderTask orderTask = orderTaskService.getOrderTaskByIdForUpdate(orderTaskVO.getId()); if (orderTask == null) { return JsonUtil.loadFalseResult("任务不存在"); } OrderTask newOrderTask = OrderTaskFactory.createOrderTask(orderTaskVO); newOrderTask.setUpdateTime(new Date()); orderTaskService.updateOrderTask(newOrderTask); return JsonUtil.loadTrueResult(""); } catch (ParseException e) { return JsonUtil.loadFalseResult("日期格式错误:" + e.getMessage()); } catch (OrderTaskException e) { return JsonUtil.loadFalseResult(e.getMessage()); } catch (Exception e) { return JsonUtil.loadFalseResult("系统异常:" + e.getMessage()); } } /** * 任务客户端分配 */ @ResponseBody @RequestMapping("assign") public String assign(Long taskId) { try { if (taskId == null) { return JsonUtil.loadFalseResult("任务ID不能为空"); } int result = orderTaskService.assignTask(taskId); JSONObject data = new JSONObject(); data.put("result", result); return JsonUtil.loadTrueResult(data); } catch (OrderTaskException e) { return JsonUtil.loadFalseResult(e.getMessage()); } catch (Exception e) { return JsonUtil.loadFalseResult("系统异常:" + e.getMessage()); } } /** * 查询任务已分配的客户端列表 */ @ResponseBody @RequestMapping("assignedClients") public String assignedClients(Long taskId, int page, int limit) { try { if (taskId == null) { return JsonUtil.loadFalseResult("任务ID不能为空"); } OrderTaskExecutionDetailMapper.DaoQuery query = new OrderTaskExecutionDetailMapper.DaoQuery(); query.taskId = taskId; query.start = (page - 1) * limit; query.count = limit; query.sortList = Arrays.asList("create_time desc"); List list = orderTaskExecutionDetailMapper.list(query); long count = orderTaskExecutionDetailMapper.count(query); JSONObject data = new JSONObject(); data.put("list", gson.toJson(list)); data.put("count", count); return JsonUtil.loadTrueResult(data); } catch (Exception e) { return JsonUtil.loadFalseResult("系统异常:" + e.getMessage()); } } @ResponseBody @RequestMapping("updateExpectedReviewTime") public String updateExpectedReviewTime(String id, String expectedReviewTime) { try { if (StringUtil.isNullOrEmpty(id)) { return JsonUtil.loadFalseResult("ID不能为空"); } OrderTaskExecutionDetail update = OrderTaskExecutionDetail.builder() .id( id) .expectedReviewTime(new Date(TimeUtil.convertToTimeTemp(expectedReviewTime, "yyyy-MM-dd HH:mm:ss"))) .updateTime(new Date()) .build(); orderTaskExecutionDetailService.updateOrderTaskExecutionDetail(update); return JsonUtil.loadTrueResult(""); } catch (Exception e) { return JsonUtil.loadFalseResult("系统异常:" + e.getMessage()); } } }