|
|
@@ -13,6 +13,7 @@ import com.ylx.common.config.WechatAccountConfig;
|
|
|
import com.ylx.common.constant.MassageConstants;
|
|
|
import com.ylx.common.core.domain.R;
|
|
|
import com.ylx.common.exception.ServiceException;
|
|
|
+import com.ylx.common.utils.SecurityUtils;
|
|
|
import com.ylx.massage.domain.*;
|
|
|
import com.ylx.massage.domain.vo.CouponReceiveVo;
|
|
|
import com.ylx.massage.domain.vo.HomeBlock;
|
|
|
@@ -94,6 +95,9 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
@Resource
|
|
|
private OrderNotificationService orderNotificationService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private OrderAllocationLogService allocationLogService;
|
|
|
+
|
|
|
/**
|
|
|
* 判断是否免车费
|
|
|
* 时间段判断:
|
|
|
@@ -135,14 +139,32 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public TOrder addOrder(TOrder order) {
|
|
|
+ String jsId = order.getcJsId();
|
|
|
// 1. 基础参数校验
|
|
|
- if (StringUtils.isBlank(order.getcJsId())) {
|
|
|
+ if (StringUtils.isBlank(jsId)) {
|
|
|
throw new ServiceException("请选择技师");
|
|
|
}
|
|
|
if (order.getcGoods().isEmpty()) {
|
|
|
throw new ServiceException("请选择项目");
|
|
|
}
|
|
|
|
|
|
+ TJs js = jsService.getById(jsId);
|
|
|
+ if (js == null) {
|
|
|
+ throw new ServiceException("技师不存在");
|
|
|
+ }
|
|
|
+ Integer techType = js.getTechType();
|
|
|
+ // 虚拟技师
|
|
|
+ if(techType.equals(1)){
|
|
|
+ //虚拟技师订单
|
|
|
+ order.setVirtualOrderFlag(1);
|
|
|
+ //虚拟技师订单未分配
|
|
|
+ order.setVirtualOrderAllocation(1);
|
|
|
+ }else{
|
|
|
+ //真实订单
|
|
|
+ order.setVirtualOrderFlag(0);
|
|
|
+ order.setVirtualOrderAllocation(0);
|
|
|
+ }
|
|
|
+
|
|
|
// 2. 【新增】订单状态锁校验 - 检查技师是否可以接单
|
|
|
// 确保技师没有进行中的订单,保证服务时间互斥
|
|
|
log.info("开始校验技师 {} 是否可以接单,订单号:{}", order.getcJsId(), order.getOrderNo());
|
|
|
@@ -153,7 +175,7 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
// List<CouponReceiveVo> coupons = couponReceiveService.getByOpenId(order.getcOpenId());
|
|
|
// BigDecimal preferential = this.setCoupon(coupons);
|
|
|
// order.setPreferential(preferential);
|
|
|
-
|
|
|
+ // 生成订单号
|
|
|
order.setOrderNo(generator.generateNextOrderNumber(OrderNumberGenerator.KEY_PREFIX_ORDER));
|
|
|
//订单价格
|
|
|
List<TXiangmu> list = JSONObject.parseArray(order.getcGoods().toJSONString(), TXiangmu.class);
|
|
|
@@ -166,7 +188,6 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
if (address == null) {
|
|
|
throw new ServiceException("请先添加地址");
|
|
|
}
|
|
|
- TJs js = jsService.getById(order.getcJsId());
|
|
|
|
|
|
//添加技师位置信息
|
|
|
locationUtil.geoAdd(LocationUtil.GEO_KEY_USER, js.getcOpenId() + order.getOrderNo(), Double.parseDouble(js.getLongitude().toString()), Double.parseDouble(js.getLatitude().toString()));
|
|
|
@@ -306,34 +327,177 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public TOrder transferOrder(TOrder order) {
|
|
|
+ // ========== 第1步:参数校验 ==========
|
|
|
if (StringUtils.isBlank(order.getcId())) {
|
|
|
throw new ServiceException("订单id不能为空");
|
|
|
}
|
|
|
if (StringUtils.isBlank(order.getcJsId())) {
|
|
|
throw new ServiceException("转单技师ID不能为空");
|
|
|
}
|
|
|
- TOrder oldOrder = this.getById(order.getcId());
|
|
|
-
|
|
|
- oldOrder.setOldJsId(oldOrder.getcJsId());
|
|
|
- oldOrder.setcJsId(order.getcJsId());
|
|
|
- log.info("新技师:{},老技师{}", oldOrder.getcJsId(), oldOrder.getOldJsId());
|
|
|
- if (!this.updateById(oldOrder)) {
|
|
|
- throw new ServiceException("转单失败");
|
|
|
- }
|
|
|
- //改变新技师服务状态
|
|
|
- TJs tJs = new TJs();
|
|
|
- tJs.setId(oldOrder.getcJsId());
|
|
|
- tJs.setnStatus(JsStatusEnum.JS_SERVICE.getCode());
|
|
|
- jsService.updateById(tJs);
|
|
|
- //改变旧技师服务状态
|
|
|
- TJs oldTJs = new TJs();
|
|
|
- oldTJs.setId(oldOrder.getOldJsId());
|
|
|
- oldTJs.setnStatus(JsStatusEnum.JS_SERVICEABLE.getCode());
|
|
|
- jsService.updateById(oldTJs);
|
|
|
-
|
|
|
- //通知技师转单
|
|
|
- this.newOrderNotification(oldOrder);
|
|
|
- return oldOrder;
|
|
|
+
|
|
|
+ // 定义操作结果(默认为失败)
|
|
|
+ Integer operationResult = 1; // 1-失败
|
|
|
+
|
|
|
+ // 定义操作记录所需的变量
|
|
|
+ String orderId = null;
|
|
|
+ String orderNo = null;
|
|
|
+ String oldTechnicianId = null;
|
|
|
+ String oldTechnicianName = null;
|
|
|
+ Integer oldTechnicianStatusBefore = null;
|
|
|
+ Integer oldTechnicianStatusAfter = null;
|
|
|
+ String newTechnicianId = null;
|
|
|
+ String newTechnicianName = null;
|
|
|
+ Integer newTechnicianStatusBefore = null;
|
|
|
+ Integer newTechnicianStatusAfter = null;
|
|
|
+ Integer orderStatusBefore = null;
|
|
|
+ Integer orderStatusAfter = null;
|
|
|
+ String operatorId = null;
|
|
|
+ String operatorName = null;
|
|
|
+ String operationReason = "虚拟订单分配";
|
|
|
+
|
|
|
+ try {
|
|
|
+ // ========== 第2步:查询原订单信息 ==========
|
|
|
+ TOrder oldOrder = this.getById(order.getcId());
|
|
|
+ if (oldOrder == null) {
|
|
|
+ throw new ServiceException("订单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ //原技师ID
|
|
|
+ oldTechnicianId = oldOrder.getcJsId();
|
|
|
+ //新技师ID
|
|
|
+ newTechnicianId = order.getcJsId();
|
|
|
+
|
|
|
+ // 记录订单操作前状态
|
|
|
+ orderStatusBefore = oldOrder.getnStatus();
|
|
|
+ orderId = oldOrder.getcId();
|
|
|
+ orderNo = oldOrder.getOrderNo();
|
|
|
+
|
|
|
+ log.info("开始转单操作 - 订单号:{}, 原技师ID:{}, 新技师ID:{}, 订单状态:{}", oldOrder.getOrderNo(), oldTechnicianId, newTechnicianId, orderStatusBefore);
|
|
|
+
|
|
|
+ // ========== 第3步:查询原技师信息 ==========
|
|
|
+ TJs oldTechnician = jsService.getById(oldTechnicianId);
|
|
|
+ if (oldTechnician == null) {
|
|
|
+ throw new ServiceException("原技师不存在");
|
|
|
+ }
|
|
|
+ oldTechnicianName = oldTechnician.getcName();
|
|
|
+ oldTechnicianStatusBefore = oldTechnician.getnStatus();
|
|
|
+
|
|
|
+ // ========== 第4步:查询新技师信息 ==========
|
|
|
+ TJs newTechnician = jsService.getById(newTechnicianId);
|
|
|
+ if (newTechnician == null) {
|
|
|
+ throw new ServiceException("新技师不存在");
|
|
|
+ }
|
|
|
+ newTechnicianName = newTechnician.getcName();
|
|
|
+ newTechnicianStatusBefore = newTechnician.getnStatus();
|
|
|
+
|
|
|
+ // ========== 第5步:更新订单技师信息 ==========
|
|
|
+ oldOrder.setOldJsId(oldTechnicianId); // 保存原技师ID
|
|
|
+ oldOrder.setcJsId(newTechnicianId); // 更新为新技师ID
|
|
|
+
|
|
|
+ log.info("更新订单技师 - 订单号:{}, 原技师:[ID:{}, 姓名:{}], 新技师:[ID:{}, 姓名:{}]", oldOrder.getOrderNo(), oldTechnicianId, oldTechnicianName, newTechnicianId, newTechnicianName);
|
|
|
+
|
|
|
+ if (!this.updateById(oldOrder)) {
|
|
|
+ throw new ServiceException("转单失败:更新订单技师信息失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录订单操作后状态(转单后状态通常保持不变)
|
|
|
+ orderStatusAfter = oldOrder.getnStatus();
|
|
|
+
|
|
|
+ // ========== 第6步:更新新技师状态(可服务 → 服务中)==========
|
|
|
+ TJs newJsUpdate = new TJs();
|
|
|
+ newJsUpdate.setId(newTechnicianId);
|
|
|
+ newJsUpdate.setnStatus(JsStatusEnum.JS_SERVICE.getCode());
|
|
|
+ if (!jsService.updateById(newJsUpdate)) {
|
|
|
+ throw new ServiceException("转单失败:更新新技师状态失败");
|
|
|
+ }
|
|
|
+ newTechnicianStatusAfter = JsStatusEnum.JS_SERVICE.getCode();
|
|
|
+
|
|
|
+ // ========== 第7步:更新原技师状态(服务中 → 可服务)==========
|
|
|
+ TJs oldJsUpdate = new TJs();
|
|
|
+ oldJsUpdate.setId(oldTechnicianId);
|
|
|
+ oldJsUpdate.setnStatus(JsStatusEnum.JS_SERVICEABLE.getCode());
|
|
|
+ if (!jsService.updateById(oldJsUpdate)) {
|
|
|
+ throw new ServiceException("转单失败:更新原技师状态失败");
|
|
|
+ }
|
|
|
+ oldTechnicianStatusAfter = JsStatusEnum.JS_SERVICEABLE.getCode();
|
|
|
+
|
|
|
+ log.info("更新技师状态完成 - 新技师:{} {}→{}, 原技师:{} {}→{}",
|
|
|
+ newTechnicianName, getStatusName(newTechnicianStatusBefore), getStatusName(newTechnicianStatusAfter),
|
|
|
+ oldTechnicianName, getStatusName(oldTechnicianStatusBefore), getStatusName(oldTechnicianStatusAfter));
|
|
|
+
|
|
|
+ // ========== 第8步:获取操作人信息 ==========
|
|
|
+ operatorId = SecurityUtils.getUserId() != null ? SecurityUtils.getUserId().toString() : "ADMIN";
|
|
|
+ operatorName = SecurityUtils.getUsername() != null ? SecurityUtils.getUsername() : "系统管理员";
|
|
|
+
|
|
|
+ // ========== 第9步:转单成功,设置操作结果为成功 ==========
|
|
|
+ operationResult = 0; // 0-成功
|
|
|
+ log.info("转单操作完成 - 订单号:{}", oldOrder.getOrderNo());
|
|
|
+ return oldOrder;
|
|
|
+ } catch (ServiceException e) {
|
|
|
+ // 业务异常,操作失败
|
|
|
+ log.error("转单操作失败 - 订单号:{}, 错误信息:{}", orderNo, e.getMessage());
|
|
|
+ operationResult = 1; // 1-失败
|
|
|
+ throw e;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 系统异常,操作失败
|
|
|
+ log.error("转单操作异常 - 订单号:{}, 异常信息:{}", orderNo, e.getMessage(), e);
|
|
|
+ operationResult = 1; // 1-失败
|
|
|
+ throw new ServiceException("转单操作异常:" + e.getMessage());
|
|
|
+ } finally {
|
|
|
+ // ========== 第10步:记录转单操作日志(无论成功或失败都记录)==========
|
|
|
+ try {
|
|
|
+ // 只有在获取到基本信息后才记录日志
|
|
|
+ if (orderId != null && orderNo != null && oldTechnicianId != null && newTechnicianId != null) {
|
|
|
+ allocationLogService.recordTransferOrder(
|
|
|
+ orderId, // orderId
|
|
|
+ orderNo, // orderNo
|
|
|
+ oldTechnicianId, // oldTechnicianId
|
|
|
+ oldTechnicianName, // oldTechnicianName
|
|
|
+ oldTechnicianStatusBefore, // oldTechnicianStatusBefore
|
|
|
+ oldTechnicianStatusAfter, // oldTechnicianStatusAfter
|
|
|
+ newTechnicianId, // newTechnicianId
|
|
|
+ newTechnicianName, // newTechnicianName
|
|
|
+ newTechnicianStatusBefore, // newTechnicianStatusBefore
|
|
|
+ newTechnicianStatusAfter, // newTechnicianStatusAfter
|
|
|
+ orderStatusBefore, // orderStatusBefore
|
|
|
+ orderStatusAfter, // orderStatusAfter
|
|
|
+ operatorId, // operatorId
|
|
|
+ operatorName, // operatorName
|
|
|
+ operationReason, // operationReason
|
|
|
+ operationResult // operationResult(0-成功,1-失败)
|
|
|
+ );
|
|
|
+
|
|
|
+ String resultDesc = operationResult == 0 ? "成功" : "失败";
|
|
|
+ log.info("转单操作记录已保存 - 订单号:{}, 操作结果:{}", orderNo, resultDesc);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 记录日志失败不影响转单操作
|
|
|
+ log.error("记录转单操作日志失败 - 订单号:{}, 错误信息:{}", orderNo, e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取技师状态名称
|
|
|
+ *
|
|
|
+ * @param status 状态码
|
|
|
+ * @return String 状态名称
|
|
|
+ */
|
|
|
+ private String getStatusName(Integer status) {
|
|
|
+ if (status == null) {
|
|
|
+ return "未知";
|
|
|
+ }
|
|
|
+ switch (status) {
|
|
|
+ case 0:
|
|
|
+ return "可服务";
|
|
|
+ case 1:
|
|
|
+ return "服务中";
|
|
|
+ case 2:
|
|
|
+ return "不可服务";
|
|
|
+ default:
|
|
|
+ return "未知(" + status + ")";
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|