| | |
| | | import com.doumee.dao.business.DriverInfoMapper; |
| | | import com.doumee.dao.business.MemberMapper; |
| | | import com.doumee.dao.business.MultifileMapper; |
| | | import com.doumee.dao.business.OrderCommentMapper; |
| | | import com.doumee.dao.business.SmsrecordMapper; |
| | | import com.doumee.dao.business.CategoryMapper; |
| | | import com.doumee.dao.business.OrdersMapper; |
| | | import com.doumee.dao.business.ShopInfoMapper; |
| | | import com.doumee.dao.business.OrdersDetailMapper; |
| | | import com.doumee.dao.business.RevenueMapper; |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | |
| | | import com.doumee.dao.business.model.Category; |
| | | import com.doumee.dao.business.model.DriverInfo; |
| | | import com.doumee.dao.business.model.OrderLog; |
| | | import com.doumee.dao.business.model.OrderComment; |
| | | import com.doumee.dao.business.model.ShopInfo; |
| | | import com.doumee.dao.business.model.Member; |
| | | import com.doumee.dao.business.model.Multifile; |
| | | import com.doumee.dao.business.model.Smsrecord; |
| | |
| | | import com.doumee.service.business.NoticeService; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.RandomStringUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | * @author rk |
| | | * @date 2026/04/08 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class DriverInfoServiceImpl implements DriverInfoService { |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private OrdersMapper ordersMapper; |
| | | |
| | | @Autowired |
| | | private ShopInfoMapper shopInfoMapper; |
| | | |
| | | @Autowired |
| | | private OrderCommentMapper orderCommentMapper; |
| | | |
| | | @Autowired |
| | | private RevenueMapper revenueMapper; |
| | |
| | | Notice notice = new Notice(); |
| | | notice.setUserType(2); // 2=门店 |
| | | notice.setUserId(shopId); |
| | | notice.setTitle(notify.getTitle()); |
| | | notice.setContent(notify.format(params)); |
| | | notice.setObjId(orderId); |
| | | notice.setObjType(0); // 0=订单 |
| | | notice.setStatus(0); // 0=未读 |
| | | notice.setIsdeleted(Constants.ZERO); |
| | | notice.setCreateDate(new Date()); |
| | | noticeService.create(notice); |
| | | } |
| | | |
| | | /** |
| | | * 发送司机站内信通知 |
| | | */ |
| | | private void sendDriverNotice(Integer driverId, Constants.DriverOrderNotify notify, Integer orderId, String... params) { |
| | | DriverInfo driver = driverInfoMapper.selectById(driverId); |
| | | if (driver == null || driver.getMemberId() == null) { |
| | | return; |
| | | } |
| | | Notice notice = new Notice(); |
| | | notice.setUserType(1); // 1=司机 |
| | | notice.setUserId(driver.getMemberId()); |
| | | notice.setTitle(notify.getTitle()); |
| | | notice.setContent(notify.format(params)); |
| | | notice.setObjId(orderId); |
| | |
| | | } |
| | | } |
| | | |
| | | // 异地寄存实时经纬度(按状态返回) |
| | | if (Constants.ONE.equals(order.getType())) { |
| | | // 司机自身经纬度 |
| | | vo.setDriverLng(driverLng); |
| | | vo.setDriverLat(driverLat); |
| | | |
| | | // status=3(已接单):返回存件门店经纬度 |
| | | if (Constants.equalsInteger(order.getStatus(), Constants.OrderStatus.accepted.getStatus())) { |
| | | if (order.getDepositShopId() != null) { |
| | | ShopInfo depositShop = shopInfoMapper.selectById(order.getDepositShopId()); |
| | | if (depositShop != null) { |
| | | vo.setDepositShopLng(depositShop.getLongitude()); |
| | | vo.setDepositShopLat(depositShop.getLatitude()); |
| | | } |
| | | } |
| | | } |
| | | // status=4(配送中):返回取件点经纬度 |
| | | if (Constants.equalsInteger(order.getStatus(), Constants.OrderStatus.delivering.getStatus())) { |
| | | if (order.getTakeShopId() != null) { |
| | | ShopInfo takeShop = shopInfoMapper.selectById(order.getTakeShopId()); |
| | | if (takeShop != null) { |
| | | vo.setTakeLng(takeShop.getLongitude()); |
| | | vo.setTakeLat(takeShop.getLatitude()); |
| | | } |
| | | } else if (order.getTakeLgt() != null && order.getTakeLat() != null) { |
| | | vo.setTakeLng(order.getTakeLgt().doubleValue()); |
| | | vo.setTakeLat(order.getTakeLat().doubleValue()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 下单附件图片 |
| | | String imgPrefix = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode() |
| | | + systemDictDataBiz.queryByCode(Constants.OSS, Constants.MEMBER_FILES).getCode(); |
| | | vo.setOrderImages(getFileUrls(orderId, Constants.FileType.ORDER_FILE.getKey(), imgPrefix)); |
| | | |
| | | // 评价信息 |
| | | vo.setCommentStatus(order.getCommentStatus()); |
| | | if (Constants.equalsInteger(order.getCommentStatus(), Constants.ONE)) { |
| | | vo.setCommentTime(order.getCommentTime()); |
| | | // 查询司机评价记录 |
| | | OrderComment driverComment = orderCommentMapper.selectOne(new QueryWrapper<OrderComment>().lambda() |
| | | .eq(OrderComment::getOrderId, orderId) |
| | | .eq(OrderComment::getTargetType, Constants.THREE) |
| | | .eq(OrderComment::getDeleted, Constants.ZERO) |
| | | .last("limit 1")); |
| | | if (driverComment != null) { |
| | | vo.setDriverScore(driverComment.getScore()); |
| | | vo.setCommentContent(driverComment.getContent()); |
| | | } |
| | | // 评价附件图片 |
| | | vo.setCommentImages(getFileUrls(orderId, Constants.FileType.COMMENT_ATTACH.getKey(), imgPrefix)); |
| | | } |
| | | |
| | | return vo; |
| | | } |
| | |
| | | // 通知会员:司机变更 |
| | | sendOrderNotice(order.getMemberId(), Constants.MemberOrderNotify.DRIVER_CHANGED, orderId, |
| | | "orderNo", order.getCode()); |
| | | |
| | | // 通知司机:取消成功 |
| | | int remainLimit = limit - (todayCancelCount != null ? todayCancelCount.intValue() + 1 : 1); |
| | | sendDriverNotice(driverId, Constants.DriverOrderNotify.CANCELLED, orderId, |
| | | "orderNo", order.getCode(), |
| | | "cancelLimit", String.valueOf(Math.max(remainLimit, 0))); |
| | | } |
| | | |
| | | @Override |
| | |
| | | sendShopNotice(order.getDepositShopId(), Constants.ShopOrderNotify.WAIT_PICKUP, orderId, |
| | | "orderNo", order.getCode()); |
| | | } |
| | | |
| | | // 通知司机:抢单成功 |
| | | String shopName = order.getDepositShopName() != null ? order.getDepositShopName() : order.getDepositLocation(); |
| | | sendDriverNotice(driverId, Constants.DriverOrderNotify.WAIT_DELIVER, orderId, |
| | | "orderNo", order.getCode(), |
| | | "shopName", shopName != null ? shopName : ""); |
| | | |
| | | // 短信通知司机:抢单成功,待取件 |
| | | String pickupAddress = order.getDepositShopAddress() != null ? order.getDepositShopAddress() : order.getDepositLocation(); |
| | | sendSmsNotify(driver.getTelephone(), Constants.SmsNotify.DRIVER_WAIT_PICKUP, |
| | | "orderNo", order.getCode(), |
| | | "address", pickupAddress != null ? pickupAddress : ""); |
| | | } |
| | | |
| | | @Override |
| | |
| | | "orderNo", order.getCode(), |
| | | "driverName", driver.getName()); |
| | | } |
| | | |
| | | // 通知司机:已取件配送中 |
| | | sendDriverNotice(driverId, Constants.DriverOrderNotify.DELIVERING, orderId, |
| | | "orderNo", order.getCode()); |
| | | |
| | | // 短信通知会员:司机已取件,配送中 |
| | | Member deliveringMember = memberMapper.selectById(order.getMemberId()); |
| | | if (deliveringMember != null) { |
| | | sendSmsNotify(deliveringMember.getTelephone(), Constants.SmsNotify.MEMBER_DELIVERING, |
| | | "orderNo", order.getCode(), |
| | | "name", driver.getName()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void confirmDeliver(Integer driverId, DriverDeliverDTO dto) { |
| | | Integer orderId = dto.getOrderId(); |
| | | |
| | | // 1. 校验司机 |
| | | DriverInfo driver = driverInfoMapper.selectById(driverId); |
| | | if (driver == null) { |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "司机信息不存在"); |
| | | } |
| | | |
| | | // 2. 校验订单 |
| | | Orders order = ordersMapper.selectById(orderId); |
| | | if (order == null || Constants.ONE.equals(order.getDeleted())) { |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "订单不存在"); |
| | | } |
| | | if (!Constants.ONE.equals(order.getType())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "仅异地寄存订单支持此操作"); |
| | | } |
| | | if (order.getTakeShopId() != null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "有取件门店的订单请送达至门店核销"); |
| | | } |
| | | if (!Constants.equalsInteger(order.getStatus(), Constants.OrderStatus.delivering.getStatus())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "当前订单状态不允许确认送达"); |
| | | } |
| | | if (!driverId.equals(order.getAcceptDriver())) { |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "无权操作该订单"); |
| | | } |
| | | |
| | | // 3. 保存送达图片 |
| | | Date now = new Date(); |
| | | if (dto.getImages() != null && !dto.getImages().isEmpty()) { |
| | | int sortNum = 0; |
| | | for (String url : dto.getImages()) { |
| | | Multifile multifile = new Multifile(); |
| | | multifile.setObjId(orderId); |
| | | multifile.setObjType(Constants.FileType.DRIVER_DONE.getKey()); |
| | | multifile.setType(Constants.ZERO); |
| | | multifile.setFileurl(url); |
| | | multifile.setIsdeleted(Constants.ZERO); |
| | | multifile.setCreateDate(now); |
| | | multifile.setSortnum(sortNum++); |
| | | multifileMapper.insert(multifile); |
| | | } |
| | | } |
| | | |
| | | // 4. 更新订单状态为已送达(5) |
| | | ordersMapper.update(new UpdateWrapper<Orders>().lambda() |
| | | .set(Orders::getStatus, Constants.OrderStatus.arrived.getStatus()) |
| | | .set(Orders::getArriveTime, now) |
| | | .set(Orders::getUpdateTime, now) |
| | | .eq(Orders::getId, orderId)); |
| | | |
| | | // 5. 写入操作日志 |
| | | OrderLog log = new OrderLog(); |
| | | log.setOrderId(orderId); |
| | | log.setTitle("司机确认送达"); |
| | | log.setLogInfo(StringUtils.isNotBlank(dto.getRemark()) ? dto.getRemark() : "司机【" + driver.getName() + "】已送达"); |
| | | log.setObjType(Constants.ORDER_LOG_DRIVER_DELIVER); |
| | | log.setOptUserId(driver.getMemberId()); |
| | | log.setOptUserType(Constants.ONE); |
| | | log.setOrderStatus(Constants.OrderStatus.arrived.getStatus()); |
| | | log.setCreateTime(now); |
| | | log.setDeleted(Constants.ZERO); |
| | | orderLogMapper.insert(log); |
| | | |
| | | // 6. 通知会员:订单已送达(无取件门店) |
| | | String destination = StringUtils.isNotBlank(order.getTakeShopAddress()) ? order.getTakeShopAddress() : "目的地"; |
| | | sendOrderNotice(order.getMemberId(), Constants.MemberOrderNotify.ARRIVED_NO_SHOP, orderId, |
| | | "orderNo", order.getCode(), |
| | | "destination", destination); |
| | | |
| | | // 通知司机:已送达 |
| | | sendDriverNotice(driverId, Constants.DriverOrderNotify.ARRIVED, orderId, |
| | | "orderNo", order.getCode(), |
| | | "destination", destination); |
| | | } |
| | | |
| | | private List<String> getFileUrls(Integer orderId, int objType, String prefix) { |
| | |
| | | return vo; |
| | | } |
| | | |
| | | /** |
| | | * 发送短信通知(失败不影响主业务) |
| | | */ |
| | | private void sendSmsNotify(String phone, Constants.SmsNotify smsNotify, String... paramPairs) { |
| | | if (StringUtils.isBlank(phone)) { |
| | | return; |
| | | } |
| | | String content = smsNotify.format(paramPairs); |
| | | try { |
| | | JSONObject templateParam = new JSONObject(); |
| | | for (int i = 0; i < paramPairs.length - 1; i += 2) { |
| | | templateParam.put(paramPairs[i], paramPairs[i + 1]); |
| | | } |
| | | boolean result = AliSmsService.sendSms(phone, smsNotify.getTemplateCode(), |
| | | templateParam.toJSONString()); |
| | | if (result) { |
| | | log.info("短信发送成功: phone={}, template={}", phone, smsNotify.name()); |
| | | } else { |
| | | log.warn("短信发送失败: phone={}, template={}", phone, smsNotify.name()); |
| | | } |
| | | // 存储短信记录 |
| | | Smsrecord record = new Smsrecord(); |
| | | record.setPhone(phone); |
| | | record.setContent(content); |
| | | record.setType(Constants.ONE); // 1=订单通知 |
| | | record.setStatus(result ? Constants.ONE : Constants.ZERO); |
| | | record.setCreateTime(new Date()); |
| | | record.setDeleted(Constants.ZERO); |
| | | smsrecordMapper.insert(record); |
| | | } catch (Exception e) { |
| | | log.error("短信发送异常: phone={}, template={}, error={}", phone, smsNotify.name(), e.getMessage()); |
| | | try { |
| | | Smsrecord record = new Smsrecord(); |
| | | record.setPhone(phone); |
| | | record.setContent(content); |
| | | record.setType(Constants.ONE); |
| | | record.setStatus(Constants.ZERO); |
| | | record.setCreateTime(new Date()); |
| | | record.setDeleted(Constants.ZERO); |
| | | smsrecordMapper.insert(record); |
| | | } catch (Exception ignored) {} |
| | | } |
| | | } |
| | | |
| | | } |