|
|
@@ -4,7 +4,10 @@ package com.ruoyi.app.order;
|
|
|
* 订单商家操作
|
|
|
*/
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.ruoyi.app.order.dto.OrderCreatItem;
|
|
|
+import com.ruoyi.app.order.dto.OrderCreateInput;
|
|
|
import com.ruoyi.app.order.dto.OrderPushBodyDto;
|
|
|
import com.ruoyi.app.utils.PayPush;
|
|
|
import com.ruoyi.app.utils.event.PushEventService;
|
|
|
@@ -12,21 +15,21 @@ import com.ruoyi.common.annotation.Anonymous;
|
|
|
import com.ruoyi.common.core.controller.BaseController;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.MessageUtils;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.framework.manager.AsyncManager;
|
|
|
-import com.ruoyi.system.domain.InfoUser;
|
|
|
-import com.ruoyi.system.domain.PosOrder;
|
|
|
-import com.ruoyi.system.service.IInfoUserService;
|
|
|
-import com.ruoyi.system.service.IPosOrderService;
|
|
|
+import com.ruoyi.system.domain.*;
|
|
|
+import com.ruoyi.system.service.*;
|
|
|
import com.ruoyi.system.utils.Auth;
|
|
|
import com.ruoyi.system.utils.JwtUtil;
|
|
|
+import com.ruoyi.system.utils.OrderLogHelper;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Optional;
|
|
|
-import java.util.TimerTask;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* posorderController
|
|
|
@@ -43,6 +46,159 @@ public class PosOrderShOprateController extends BaseController {
|
|
|
private IInfoUserService infoUserService;
|
|
|
@Autowired
|
|
|
private PushEventService pushEventService;
|
|
|
+ @Autowired
|
|
|
+ private OrderLogHelper orderLogHelper;
|
|
|
+ @Autowired
|
|
|
+ private IPosStoreService posStoreService;
|
|
|
+ @Autowired //经营时间
|
|
|
+ private IOperatingHoursService operatingHoursService;
|
|
|
+ @Autowired
|
|
|
+ private IOrderParentService orderParentService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商家端下单
|
|
|
+ */
|
|
|
+ @PostMapping("/createOrder")
|
|
|
+ @Auth
|
|
|
+ @Anonymous
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public AjaxResult createOrder(@RequestHeader String token, @RequestBody OrderCreateInput input)
|
|
|
+ {
|
|
|
+ JwtUtil jwtUtil = new JwtUtil();
|
|
|
+ String id = jwtUtil.getusid(token);
|
|
|
+ String shdz=null;
|
|
|
+ createOrderParent(input,Long.valueOf(id),shdz);
|
|
|
+ createOrderChild(input,Long.valueOf(id),Long.valueOf(id),shdz);
|
|
|
+
|
|
|
+ OrderParent result = orderParentService.getOne(new LambdaQueryWrapper<OrderParent>().eq(OrderParent::getDdId, input.getDdId().toString()));
|
|
|
+ System.out.println("创建返回信息:"+result);
|
|
|
+ return super.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建父订单
|
|
|
+ private void createOrderParent(OrderCreateInput input,Long UserId,String shdz){
|
|
|
+ OrderParent orderParent = new OrderParent();
|
|
|
+ orderParent.setUserId(UserId);
|
|
|
+ orderParent.setOrderStatus(0L);
|
|
|
+ orderParent.setTotalAmount(input.getTotalAmount());
|
|
|
+ orderParent.setActualPayAmount(input.getActualPayAmount());
|
|
|
+ orderParent.setPaymentMethod(input.getPaymentMethod());
|
|
|
+// orderParent.setPaymentTime(new Date());
|
|
|
+ orderParent.setCreateTime(new Date());
|
|
|
+// orderParent.setUpdateTime(new Date());
|
|
|
+ orderParent.setDdId(input.getDdId().toString());
|
|
|
+ orderParent.setTableId(input.getTableId());
|
|
|
+ orderParent.setShdzId(input.getShdzId());
|
|
|
+ orderParent.setShAddress(shdz);
|
|
|
+ orderParent.setType(input.getType());
|
|
|
+ orderParent.setRemarks(input.getRemarks());
|
|
|
+ orderParentService.save(orderParent);
|
|
|
+ }
|
|
|
+ //创建子订单
|
|
|
+ private void createOrderChild(OrderCreateInput input,Long orderParentId,Long userId,String shdz){
|
|
|
+ // 检查items是否为空
|
|
|
+ if (input.getItems() == null || input.getItems().isEmpty()) {
|
|
|
+ throw new ServiceException(MessageUtils.message("订单商品不能为空"));
|
|
|
+ }
|
|
|
+ String ddId = input.getDdId().toString();
|
|
|
+ List<Long> mdIdList = input.getItems().stream().map(OrderCreatItem::getMdId).collect(Collectors.toList());
|
|
|
+ // 将Long类型的门店ID转换为Integer类型用于查询(因为PosStore.getId()返回Integer)
|
|
|
+ List<Integer> mdIdIntegerList = mdIdList.stream().map(Long::intValue).collect(Collectors.toList());
|
|
|
+ List<PosStore> storeList = posStoreService.list(new LambdaQueryWrapper<PosStore>().in(PosStore::getId, mdIdIntegerList));
|
|
|
+ LambdaQueryWrapper<OperatingHours> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.in(OperatingHours::getMdId, mdIdList);
|
|
|
+ List<OperatingHours> hourslist = operatingHoursService.list(wrapper);
|
|
|
+ boolean isMultiStore = input.getItems().size() > 1;
|
|
|
+ // 循环items,为每个item创建一条PosOrder
|
|
|
+ int index = 0;
|
|
|
+ for (OrderCreatItem item : input.getItems()) {
|
|
|
+ index++;
|
|
|
+ // 单店铺:子订单号与父订单一致;多店铺:子订单号追加序号
|
|
|
+ String subddId = isMultiStore ? ddId + String.format("%03d", index) : ddId;
|
|
|
+ PosOrder posOrder = new PosOrder();
|
|
|
+ posOrder.setIsDisplay(true);
|
|
|
+ posOrder.setDisplayTime(new Date());
|
|
|
+ posOrder.setShdzId(input.getShdzId());
|
|
|
+ // 设置子订单的基本信息
|
|
|
+ posOrder.setDdId(subddId);
|
|
|
+ posOrder.setParentDdId(input.getDdId().toString()); // 设置父订单ID
|
|
|
+ posOrder.setCretim(new Date());
|
|
|
+ posOrder.setState(1L); // 初始状态
|
|
|
+ // 根据item设置订单信息
|
|
|
+ posOrder.setShId(item.getShId());
|
|
|
+ posOrder.setMdId(item.getMdId());
|
|
|
+ posOrder.setShdzId(input.getShdzId());
|
|
|
+ posOrder.setUserId(userId);
|
|
|
+ posOrder.setAmount(item.getAmount());
|
|
|
+ posOrder.setRemarks(item.getRemarks());
|
|
|
+ posOrder.setType(item.getType());
|
|
|
+ posOrder.setDelryTime(item.getDelryTime());
|
|
|
+ posOrder.setFood(JSON.toJSONString(item.getFood()));
|
|
|
+ // 设置支付相关信息(从父订单继承)
|
|
|
+ posOrder.setPayType(input.getPaymentMethod());
|
|
|
+ //设置桌号(从父订单继承)
|
|
|
+ posOrder.setTableId(input.getTableId());
|
|
|
+ posOrder.setTableNo(input.getTableNo());
|
|
|
+ posOrder.setLogo(item.getLogo());
|
|
|
+ posOrder.setPosName(item.getPosName());
|
|
|
+ posOrder.setJvli(item.getJvli());
|
|
|
+ posOrder.setFreight(item.getFreight());
|
|
|
+ posOrder.setShdzId(input.getShdzId());
|
|
|
+ posOrder.setShAddress(shdz);
|
|
|
+ posOrder.setFoodAmount(item.getFoodAmount());
|
|
|
+ Optional<PosStore> storeOptional = storeList.stream()
|
|
|
+ .filter(x -> item.getMdId().equals(Long.valueOf(x.getId())))
|
|
|
+ .findFirst();
|
|
|
+ if (storeOptional.isEmpty()) {
|
|
|
+ throw new ServiceException(MessageUtils.message("no.mendian.not.exist"));
|
|
|
+ }
|
|
|
+ PosStore store = storeOptional.get();
|
|
|
+ posOrder.setLongitude(store.getLongitude());
|
|
|
+ posOrder.setLatitude(store.getLatitude());
|
|
|
+ //设置订单分类
|
|
|
+ storeList.stream().filter(s -> Long.valueOf(s.getId()).equals(item.getMdId())).findFirst().ifPresent(st -> {
|
|
|
+ posOrder.setOrderCategory(String.valueOf(st.getType()));
|
|
|
+ });
|
|
|
+ setPickUpNum(posOrder);
|
|
|
+ posOrderService.save(posOrder);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成取餐码,根据门店id,当天取餐码从1开始累加
|
|
|
+ private void setPickUpNum(PosOrder order) {
|
|
|
+ if (order == null || order.getMdId() == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 获取当前日期,格式为 yyyy-MM-dd
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String today = dateFormat.format(new Date());
|
|
|
+
|
|
|
+ // 查询当天该门店的最大取餐码
|
|
|
+ LambdaQueryWrapper<PosOrder> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(PosOrder::getMdId, order.getMdId());
|
|
|
+ queryWrapper.like(PosOrder::getCretim, today);
|
|
|
+ queryWrapper.orderByDesc(PosOrder::getPickUpNum);
|
|
|
+ queryWrapper.last("LIMIT 1");
|
|
|
+
|
|
|
+ PosOrder lastOrder = posOrderService.getOne(queryWrapper);
|
|
|
+ long nextNum = 1L;
|
|
|
+ if (lastOrder != null && lastOrder.getPickUpNum() != null) {
|
|
|
+ // 直接使用Long类型的取餐码
|
|
|
+ nextNum = lastOrder.getPickUpNum() + 1;
|
|
|
+ }
|
|
|
+ // 生成新的取餐码:直接使用Long数字
|
|
|
+ order.setPickUpNum(nextNum);
|
|
|
+ logger.info("生成取餐码成功:门店ID={}, 取餐码={}", order.getMdId(), nextNum);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("生成取餐码失败:门店ID={}, 错误信息={}", order.getMdId(), e.getMessage(), e);
|
|
|
+ // 生成失败时使用时间戳作为备用方案
|
|
|
+ Long fallbackPickUpNum = System.currentTimeMillis() % 10000;
|
|
|
+ order.setPickUpNum(fallbackPickUpNum);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 商家接单:state 从 0 改为 1
|
|
|
@@ -59,6 +215,9 @@ public class PosOrderShOprateController extends BaseController {
|
|
|
update.setId(order.getId());
|
|
|
update.setState(1L);
|
|
|
posOrderService.saveOrUpdate(update);
|
|
|
+ InfoUser shUser = infoUserService.getOne(new LambdaQueryWrapper<InfoUser>().eq(InfoUser::getUserId, Long.valueOf(new JwtUtil().getusid(token))));
|
|
|
+ String shName = shUser != null ? shUser.getNickName() : "";
|
|
|
+ orderLogHelper.log(String.valueOf(order.getDdId()), 2, Long.valueOf(new JwtUtil().getusid(token)), shName, "商家" + shName + "已接单");
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
|
|
|
@@ -82,6 +241,9 @@ public class PosOrderShOprateController extends BaseController {
|
|
|
update.setDeliveryStatus(0L);
|
|
|
}
|
|
|
posOrderService.saveOrUpdate(update);
|
|
|
+ InfoUser shUser = infoUserService.getOne(new LambdaQueryWrapper<InfoUser>().eq(InfoUser::getUserId, Long.valueOf(new JwtUtil().getusid(token))));
|
|
|
+ String shName = shUser != null ? shUser.getNickName() : "";
|
|
|
+ orderLogHelper.log(String.valueOf(order.getDdId()), 2, Long.valueOf(new JwtUtil().getusid(token)), shName, "商家" + shName + "已出餐");
|
|
|
// 出餐推送
|
|
|
chuCan(order, update);
|
|
|
return AjaxResult.success();
|
|
|
@@ -107,6 +269,9 @@ public class PosOrderShOprateController extends BaseController {
|
|
|
update.setState(3L);
|
|
|
update.setPayStatus(1L);
|
|
|
posOrderService.saveOrUpdate(update);
|
|
|
+ InfoUser shUser = infoUserService.getOne(new LambdaQueryWrapper<InfoUser>().eq(InfoUser::getUserId, Long.valueOf(new JwtUtil().getusid(token))));
|
|
|
+ String shName = shUser != null ? shUser.getNickName() : "";
|
|
|
+ orderLogHelper.log(String.valueOf(order.getDdId()), 2, Long.valueOf(new JwtUtil().getusid(token)), shName, "商家" + shName + "已完成订单");
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
|
|
|
@@ -128,6 +293,9 @@ public class PosOrderShOprateController extends BaseController {
|
|
|
update.setId(order.getId());
|
|
|
update.setState(4L);
|
|
|
posOrderService.saveOrUpdate(update);
|
|
|
+ InfoUser shUser = infoUserService.getOne(new LambdaQueryWrapper<InfoUser>().eq(InfoUser::getUserId, Long.valueOf(new JwtUtil().getusid(token))));
|
|
|
+ String shName = shUser != null ? shUser.getNickName() : "";
|
|
|
+ orderLogHelper.log(String.valueOf(order.getDdId()), 2, Long.valueOf(new JwtUtil().getusid(token)), shName, "商家" + shName + "取消订单");
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
|