Commit b9826003 by wangli

实现订单指派逻辑

1 parent 991f57bb
......@@ -2,6 +2,7 @@ package com.alibaba.cloud.integration.order.controller;
import com.alibaba.cloud.integration.common.BusinessException;
import com.alibaba.cloud.integration.common.Result;
import com.alibaba.cloud.integration.order.dto.OrderAssignReq;
import com.alibaba.cloud.integration.order.service.OrderAssign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
......@@ -15,7 +16,7 @@ public class OrderAssignController {
private OrderAssign orderAssign;
@GetMapping("/order/assign/recommend/engineers")
public Result<?> getOrderAssignRecommendEnginners(@RequestParam String orderId, @RequestParam String key, @RequestParam String distance, @RequestParam String recommend) {
public Result<?> getOrderAssignRecommendEngineers(@RequestParam String orderId, @RequestParam String key, @RequestParam String distance, @RequestParam String recommend) {
//服务单指派-推荐技术员列表
Result<?> res = null;
......@@ -28,11 +29,11 @@ public class OrderAssignController {
}
@PostMapping("/order/assign")
public Result<?> orderAssign(@RequestBody String orderId, @RequestBody String engineerCode) {
public Result<?> orderAssign(@RequestBody OrderAssignReq req) {
// 服务单指派-指派提交
Result<?> res = null;
try {
res = orderAssign.orderAssign(orderId, engineerCode);
res = orderAssign.orderAssign(req.getOrderId(), req.getEngineerCode());
}catch (BusinessException e) {
return Result.failed(e.getMessage());
}
......
package com.alibaba.cloud.integration.order.service.impl;
import com.alibaba.cloud.integration.common.BusinessException;
import com.alibaba.cloud.integration.common.Result;
import com.alibaba.cloud.integration.order.dao.EngineerInfoMPDao;
import com.alibaba.cloud.integration.order.dao.OrderAppointmentMPDao;
import com.alibaba.cloud.integration.order.dao.OrderRequestMPDao;
import com.alibaba.cloud.integration.order.entity.EngineerInfo;
import com.alibaba.cloud.integration.order.entity.OrderAppointment;
import com.alibaba.cloud.integration.order.entity.OrderRequest;
import com.alibaba.cloud.integration.order.service.OrderAssign;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@Service
public class OrderAssignImpl implements OrderAssign {
@Autowired
private OrderRequestMPDao orderRequestMPDao;
@Autowired
private OrderAppointmentMPDao orderAppointmentMPDao;
@Autowired
private EngineerInfoMPDao engineerInfoMPDao;
@Transactional
@Override
public Result<?> getOrderAssignRecommendEngineers(String orderId, String key, String distance, String recommend) {
// 服务单指派-推荐技术员列表
return null;
}
@Transactional
@Override
public Result<?> orderAssign(String orderId, String engineerCode) {
public Result<?> orderAssign(String orderId, String engineerCode) throws BusinessException {
// 服务单指派-指派提交
return null;
OrderRequest order = this.queryOrderRequest(orderId);
if (order == null) {
throw new BusinessException("订单不存在");
}
EngineerInfo engineer = this.queryEngineerInfo(engineerCode);
String date = "";
List<OrderAppointment> orderAppointments = this.queryOrderAppointments(engineerCode, date);
OrderRecommend recommend = new OrderRecommend();
RecommendResult rr = recommend.recommend(order, orderAppointments);
if (rr.getIndex() == -1) {
throw new BusinessException("指派失败, 未能找到合适的时间段, 请选择其他技术员");
}
LineSegment seg = rr.getSeg();
Timestamp expectStartTime = recommend.linePoint2Timestamp(seg.start, date);
Timestamp expectEndTime = recommend.linePoint2Timestamp(seg.end, date);
OrderAppointment op = new OrderAppointment();
op.setOrderId(orderId);
op.setSuborderId(Long.toString(System.currentTimeMillis()));
op.setMainSub(1);
op.setEngineerCode(engineerCode);
op.setEngineerName(engineer.getName());
op.setEngineerAge(0);
op.setEngineerPhone(engineer.getPhone());
op.setIsWorkshop(0);
op.setExpectStartTime(expectStartTime);
op.setExpectEndTime(expectEndTime);
op.setPreStatus("PRE");
op.setStatus("ASSIGNED");
orderAppointmentMPDao.insert(op);
// 更新order_request表状态
LambdaUpdateWrapper<OrderRequest> wrapper = new LambdaUpdateWrapper<>();
wrapper.set(OrderRequest::getAppointmentStatus, "ASSIGNED");
wrapper.eq(OrderRequest::getOrderId, orderId);
orderRequestMPDao.update(null, wrapper);
return Result.success(null);
}
private OrderRequest queryOrderRequest(String orderId) {
LambdaQueryWrapper<OrderRequest> lqw = new LambdaQueryWrapper<>();
lqw.eq(OrderRequest::getOrderId, orderId);
return orderRequestMPDao.selectOne(lqw);
}
private List<OrderAppointment> queryOrderAppointments(String engineerCode, String date) {
LambdaQueryWrapper<OrderAppointment> lqw = new LambdaQueryWrapper<>();
lqw.eq(OrderAppointment::getEngineerCode, engineerCode);
return orderAppointmentMPDao.selectList(lqw);
}
private EngineerInfo queryEngineerInfo(String engineerCode) {
LambdaQueryWrapper<EngineerInfo> lqw = new LambdaQueryWrapper<>();
lqw.eq(EngineerInfo::getEngineerCode, engineerCode);
return engineerInfoMPDao.selectOne(lqw);
}
}
@Data
class RecommendResult {
int index;
LineSegment seg;
}
class Recommend {
public RecommendResult recommend(List<LineSegment> used, Line task) {
used.sort(Comparator.comparingInt(a -> a.start));
String taskId = task.id;
int taskLength = task.length;
boolean scheduled = false;
int startRange = 480;
int endRange = 1440;
RecommendResult result = new RecommendResult();
if (!scheduled) {
for (int i = 0; i <= used.size(); i++) {
int start;
if (i == 0) {
start = startRange;
} else {
start = used.get(i - 1).end;
}
int end;
if (i == used.size() || used.get(i).start > endRange) {
end = endRange;
} else {
end = used.get(i).start;
}
if (end - start >= taskLength) {
int taskStart = start;
int taskEnd = start + taskLength;
LineSegment s = new LineSegment(taskId, taskStart, taskEnd);
result.setIndex(i);
result.setSeg(s);
return result;
}
}
}
result.setIndex(-1);
result.setSeg(null);
return result;
}
}
class OrderRecommend {
public RecommendResult recommend(OrderRequest orderRequest, List<OrderAppointment> orderAppointments) {
List<LineSegment> used = new ArrayList<>();
for (OrderAppointment o : orderAppointments) {
LineSegment s = new LineSegment(o.getOrderId(),
this.Timestamp2LinePoint(o.getExpectStartTime()),
this.Timestamp2LinePoint(o.getExpectEndTime()));
used.add(s);
}
Line task = new Line(orderRequest.getOrderId(), 30);
Recommend r = new Recommend();
return r.recommend(used, task);
}
public int Timestamp2LinePoint(Timestamp t) {
LocalDateTime datetime = t.toLocalDateTime();
return datetime.getHour() * 60 + datetime.getMinute();
}
public Timestamp linePoint2Timestamp(int x, String date) {
int base = 60;
int hour = x / base;
int minute = x % base;
String datetime = String.format("%s %d:%d:00", date, hour, minute);
return Timestamp.valueOf(datetime);
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!