|
|
@@ -0,0 +1,210 @@
|
|
|
+package com.ruoyi.app.order;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.ruoyi.app.order.dto.OrderPushBodyDto;
|
|
|
+import com.ruoyi.app.service.UserService;
|
|
|
+import com.ruoyi.app.utils.PayPush;
|
|
|
+import com.ruoyi.app.utils.event.PushEventService;
|
|
|
+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.domain.UserBilling;
|
|
|
+import com.ruoyi.system.service.IInfoUserService;
|
|
|
+import com.ruoyi.system.service.IPosOrderService;
|
|
|
+import com.ruoyi.system.service.IUserBillingService;
|
|
|
+import com.ruoyi.system.utils.Auth;
|
|
|
+import com.ruoyi.system.utils.JwtUtil;
|
|
|
+import org.redisson.api.RLock;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.i18n.LocaleContextHolder;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.transaction.support.TransactionSynchronization;
|
|
|
+import org.springframework.transaction.support.TransactionSynchronizationManager;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.TimerTask;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/system/orderQsOprate")
|
|
|
+public class PosOrderQsOprateController extends BaseController {
|
|
|
+ @Autowired
|
|
|
+ private IPosOrderService posOrderService;
|
|
|
+ @Autowired
|
|
|
+ private IInfoUserService infoUserService;
|
|
|
+ @Autowired
|
|
|
+ private PushEventService pushEventService;
|
|
|
+ @Autowired
|
|
|
+ private RedissonClient redissonClient;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private IUserBillingService userBillingService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 骑手接单
|
|
|
+ * @param token
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Anonymous
|
|
|
+ @Auth
|
|
|
+ @PostMapping("/acceptOrder")
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public AjaxResult acceptOrder(@RequestHeader String token, @RequestParam Long id) {
|
|
|
+ JwtUtil jwtUtil = new JwtUtil();
|
|
|
+ PayPush push = new PayPush();
|
|
|
+ String qsId = jwtUtil.getusid(token);
|
|
|
+
|
|
|
+ PosOrder order = posOrderService.getOne(
|
|
|
+ new LambdaQueryWrapper<PosOrder>().eq(PosOrder::getId, id));
|
|
|
+ if (order == null) {
|
|
|
+ throw new ServiceException(MessageUtils.message("no.order.not.found"));
|
|
|
+ }
|
|
|
+ PosOrder posOrder = new PosOrder();
|
|
|
+ posOrder.setId(order.getId());
|
|
|
+ posOrder.setState(3L);
|
|
|
+ posOrder.setQsId(Long.valueOf(qsId));
|
|
|
+ return setOrderQsState(posOrder, qsId, push);
|
|
|
+ }
|
|
|
+
|
|
|
+ //骑手接单、取餐、送达
|
|
|
+ private AjaxResult setOrderQsState(PosOrder posOrder, String qsId, PayPush push) {
|
|
|
+ String lockKey = "order:qs:lock:" + posOrder.getId();
|
|
|
+ RLock lock = redissonClient.getLock(lockKey);
|
|
|
+ try {
|
|
|
+ var local = LocaleContextHolder.getLocale();
|
|
|
+ logger.info("骑手接单local信息:" + local.getLanguage());
|
|
|
+ logger.info(JSON.toJSONString(local));
|
|
|
+ // 尝试获取锁
|
|
|
+ if (lock.tryLock(20L, 10L, TimeUnit.SECONDS)) {
|
|
|
+ boolean releaseInfinally = true;
|
|
|
+ try {
|
|
|
+ userService.checkUserStatus(Long.valueOf(qsId));
|
|
|
+ PosOrder orst = posOrderService.getById(posOrder.getId());
|
|
|
+ if (orst.getState() == 3 && posOrder.getState() == 3) {
|
|
|
+ throw new ServiceException(MessageUtils.message("no.order.snatched"));
|
|
|
+ }
|
|
|
+ if (posOrder.getState() == 3) {
|
|
|
+ QueryWrapper<PosOrder> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("qs_id", qsId);
|
|
|
+ wrapper.in("state", 3, 4);
|
|
|
+ List<PosOrder> orsts = posOrderService.list(wrapper);
|
|
|
+ if (orsts.size() > 0) {
|
|
|
+ throw new ServiceException(MessageUtils.message("no.exist.undelivered.order"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String userMessage = MessageUtils.message("no.message.push.delivery.personnel.receiving.order");
|
|
|
+ if (posOrder.getState() == 4) {
|
|
|
+ userMessage = MessageUtils.message("no.message.push.delivery.personnel.qspsz.order");
|
|
|
+ } else if (posOrder.getState() == 12) {
|
|
|
+ userMessage = MessageUtils.message("no.message.push.delivery.personnel.qsysd.order");
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean org = posOrderService.saveOrUpdate(posOrder);
|
|
|
+ if (org) {
|
|
|
+ //到付订单,设置用户账单为完成
|
|
|
+ if (posOrder.getState() == 3 && "1".equals(orst.getCollectPayment())) {
|
|
|
+ updateUserBill(orst.getDdId(), orst.getUserId(), orst.getQsId());
|
|
|
+ }
|
|
|
+ InfoUser user = infoUserService.getById(orst.getUserId());
|
|
|
+ InfoUser shu = infoUserService.getById(orst.getShId());
|
|
|
+ String finalUserMessage = userMessage;
|
|
|
+ releaseInfinally = false;
|
|
|
+ TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
|
|
+ @Override
|
|
|
+ public void afterCommit() {
|
|
|
+ if (lock.isHeldByCurrentThread()) {
|
|
|
+ lock.unlock();
|
|
|
+ }
|
|
|
+ // 给用户推送(骑手已接单/配送中/已送达)
|
|
|
+ if (!StringUtils.isEmpty(user.getCid())) {
|
|
|
+ String userTitle = "no.message.push.message";
|
|
|
+ String userContent = "no.message.push.delivery.personnel.receiving.order";
|
|
|
+ if (posOrder.getState() == 4) {
|
|
|
+ userContent = "no.message.push.delivery.personnel.qspsz.order";
|
|
|
+ } else if (posOrder.getState() == 12) {
|
|
|
+ userContent = "no.message.push.delivery.personnel.qsysd.order";
|
|
|
+ }
|
|
|
+ final String finalUserContent = userContent;
|
|
|
+ String userBody = OrderPushBodyDto.getJson(String.valueOf(orst.getDdId()), String.valueOf(posOrder.getState()));
|
|
|
+ Long userUserId = user.getUserId();
|
|
|
+ String userCid = user.getCid();
|
|
|
+ String userCidType = "";
|
|
|
+ String userDdId = String.valueOf(orst.getDdId());
|
|
|
+ AsyncManager.me().execute(new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ PayPush.userPushHandleLocal(push, pushEventService, userUserId, userCid, userTitle, finalUserContent, userBody, userCidType, userDdId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 给商家推送(骑手已接单时)
|
|
|
+ if (!StringUtils.isEmpty(shu.getCid()) && posOrder.getState() == 3) {
|
|
|
+ String shTitle = "no.message.push.message";
|
|
|
+ String shContent = "no.message.push.delivery.personnel.receiving.order";
|
|
|
+ String shBody = OrderPushBodyDto.getJson(String.valueOf(orst.getDdId()), String.valueOf(posOrder.getState()));
|
|
|
+ Long shUserId = shu.getUserId();
|
|
|
+ String shCid = shu.getCid();
|
|
|
+ String shCidType = "";
|
|
|
+ String shDdId = String.valueOf(orst.getDdId());
|
|
|
+ AsyncManager.me().execute(new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ PayPush.shPushHandleLocal(push, pushEventService, shUserId, shCid, shTitle, shContent, shBody, shCidType, shDdId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return success(MessageUtils.message("no.modify.success"), posOrder.getId());
|
|
|
+ } else {
|
|
|
+ return error();
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ if (releaseInfinally && lock.isHeldByCurrentThread()) {
|
|
|
+ lock.unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new ServiceException(MessageUtils.message("no.system.busy.try.again"));
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ throw new ServiceException(MessageUtils.message("no.operation.interrupted.try.again"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户用户账单
|
|
|
+ *
|
|
|
+ * @param ddId
|
|
|
+ * @param userId
|
|
|
+ * @param qsId
|
|
|
+ */
|
|
|
+ private void updateUserBill(String ddId, Long userId, Long qsId) {
|
|
|
+ QueryWrapper<UserBilling> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("dd_id", ddId);
|
|
|
+ queryWrapper.eq("user_id", userId);
|
|
|
+ queryWrapper.eq("type", "3");
|
|
|
+ UserBilling userBill = userBillingService.getOne(queryWrapper);
|
|
|
+ if (userBill != null) {
|
|
|
+ userBill.setState("0");
|
|
|
+ userBill.setIllustrate("Rider Payment Order");
|
|
|
+ userBill.setBalancePay("1");
|
|
|
+ userBill.setPaymentId(String.valueOf(qsId));
|
|
|
+ userBillingService.saveOrUpdate(userBill);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|