Commit a417d65de6b14d8f72b854bd5e965afb0e2a0eb4
1 parent
c90ac42a
新增城市校验逻辑,完善消息发送及查询接口
Showing
3 changed files
with
42 additions
and
8 deletions
src/main/java/com/diligrp/rider/controller/AdminMessageController.java
| 1 | 1 | package com.diligrp.rider.controller; |
| 2 | 2 | |
| 3 | +import com.diligrp.rider.common.exception.BizException; | |
| 3 | 4 | import com.diligrp.rider.common.result.Result; |
| 4 | 5 | import com.diligrp.rider.entity.RiderMessageTemplate; |
| 5 | 6 | import com.diligrp.rider.service.AdminMessageService; |
| ... | ... | @@ -23,6 +24,7 @@ public class AdminMessageController { |
| 23 | 24 | |
| 24 | 25 | @Data |
| 25 | 26 | static class MessageSendDTO { |
| 27 | + private Long cityId; | |
| 26 | 28 | private Long riderId; |
| 27 | 29 | private Integer type; |
| 28 | 30 | private String title; |
| ... | ... | @@ -33,6 +35,7 @@ public class AdminMessageController { |
| 33 | 35 | |
| 34 | 36 | @Data |
| 35 | 37 | static class MessageBroadcastDTO { |
| 38 | + private Long cityId; | |
| 36 | 39 | private Integer type; |
| 37 | 40 | private String title; |
| 38 | 41 | private String content; |
| ... | ... | @@ -43,7 +46,7 @@ public class AdminMessageController { |
| 43 | 46 | */ |
| 44 | 47 | @PostMapping("/send") |
| 45 | 48 | public Result<Void> send(@RequestBody MessageSendDTO dto, HttpServletRequest request) { |
| 46 | - Long cityId = resolveCityId(request); | |
| 49 | + Long cityId = resolveCityId(dto.getCityId(), request); | |
| 47 | 50 | messageService.sendToRider(cityId, dto.getRiderId(), dto.getType(), dto.getTitle(), dto.getContent(), dto.getBizType(), dto.getBizId()); |
| 48 | 51 | return Result.success(); |
| 49 | 52 | } |
| ... | ... | @@ -53,7 +56,7 @@ public class AdminMessageController { |
| 53 | 56 | */ |
| 54 | 57 | @PostMapping("/broadcast") |
| 55 | 58 | public Result<Void> broadcast(@RequestBody MessageBroadcastDTO dto, HttpServletRequest request) { |
| 56 | - Long cityId = resolveCityId(request); | |
| 59 | + Long cityId = resolveCityId(dto.getCityId(), request); | |
| 57 | 60 | messageService.broadcastToCity(cityId, dto.getType(), dto.getTitle(), dto.getContent()); |
| 58 | 61 | return Result.success(); |
| 59 | 62 | } |
| ... | ... | @@ -63,12 +66,13 @@ public class AdminMessageController { |
| 63 | 66 | */ |
| 64 | 67 | @GetMapping("/list") |
| 65 | 68 | public Result<Map<String, Object>> list( |
| 69 | + @RequestParam(required = false) Long cityId, | |
| 66 | 70 | @RequestParam(required = false) Long riderId, |
| 67 | 71 | @RequestParam(required = false) Integer type, |
| 68 | 72 | @RequestParam(defaultValue = "1") Integer page, |
| 69 | 73 | HttpServletRequest request) { |
| 70 | - Long cityId = resolveCityId(request); | |
| 71 | - return Result.success(messageService.listMessages(cityId, riderId, type, page)); | |
| 74 | + Long scopedCityId = resolveCityId(cityId, request); | |
| 75 | + return Result.success(messageService.listMessages(scopedCityId, riderId, type, page)); | |
| 72 | 76 | } |
| 73 | 77 | |
| 74 | 78 | /** |
| ... | ... | @@ -80,13 +84,15 @@ public class AdminMessageController { |
| 80 | 84 | } |
| 81 | 85 | |
| 82 | 86 | /** |
| 83 | - * 解析城市ID(分站管理员自动绑定本城市,超管需要传递) | |
| 87 | + * 解析城市ID:分站管理员固定使用账号绑定城市,平台管理员必须显式选择租户。 | |
| 84 | 88 | */ |
| 85 | - private Long resolveCityId(HttpServletRequest request) { | |
| 89 | + private Long resolveCityId(Long requestedCityId, HttpServletRequest request) { | |
| 86 | 90 | if ("substation".equals(request.getAttribute("role"))) { |
| 87 | 91 | return (Long) request.getAttribute("cityId"); |
| 88 | 92 | } |
| 89 | - // 超管需要从请求参数获取 cityId(这里简化处理,实际应该从请求参数获取) | |
| 90 | - return (Long) request.getAttribute("cityId"); | |
| 93 | + if (requestedCityId == null || requestedCityId < 1) { | |
| 94 | + throw new BizException("请选择租户"); | |
| 95 | + } | |
| 96 | + return requestedCityId; | |
| 91 | 97 | } |
| 92 | 98 | } | ... | ... |
src/main/java/com/diligrp/rider/service/impl/AdminMessageServiceImpl.java
| ... | ... | @@ -2,6 +2,7 @@ package com.diligrp.rider.service.impl; |
| 2 | 2 | |
| 3 | 3 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 4 | 4 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 5 | +import com.diligrp.rider.common.exception.BizException; | |
| 5 | 6 | import com.diligrp.rider.entity.Rider; |
| 6 | 7 | import com.diligrp.rider.entity.RiderMessage; |
| 7 | 8 | import com.diligrp.rider.entity.RiderMessageTemplate; |
| ... | ... | @@ -33,6 +34,9 @@ public class AdminMessageServiceImpl implements AdminMessageService { |
| 33 | 34 | @Override |
| 34 | 35 | @Transactional |
| 35 | 36 | public void sendToRider(Long cityId, Long riderId, Integer type, String title, String content, String bizType, Long bizId) { |
| 37 | + ensureCitySelected(cityId); | |
| 38 | + ensureRiderInCity(cityId, riderId); | |
| 39 | + | |
| 36 | 40 | RiderMessage message = new RiderMessage(); |
| 37 | 41 | message.setCityId(cityId); |
| 38 | 42 | message.setRiderId(riderId); |
| ... | ... | @@ -52,6 +56,8 @@ public class AdminMessageServiceImpl implements AdminMessageService { |
| 52 | 56 | @Override |
| 53 | 57 | @Transactional |
| 54 | 58 | public void broadcastToCity(Long cityId, Integer type, String title, String content) { |
| 59 | + ensureCitySelected(cityId); | |
| 60 | + | |
| 55 | 61 | // 查询城市内所有正常状态的骑手 |
| 56 | 62 | LambdaQueryWrapper<Rider> wrapper = new LambdaQueryWrapper<>(); |
| 57 | 63 | wrapper.eq(Rider::getCityId, cityId) |
| ... | ... | @@ -94,6 +100,8 @@ public class AdminMessageServiceImpl implements AdminMessageService { |
| 94 | 100 | |
| 95 | 101 | @Override |
| 96 | 102 | public Map<String, Object> listMessages(Long cityId, Long riderId, Integer type, Integer page) { |
| 103 | + ensureCitySelected(cityId); | |
| 104 | + | |
| 97 | 105 | Page<RiderMessage> pageObj = new Page<>(page, PAGE_SIZE); |
| 98 | 106 | LambdaQueryWrapper<RiderMessage> wrapper = new LambdaQueryWrapper<>(); |
| 99 | 107 | wrapper.eq(RiderMessage::getCityId, cityId) |
| ... | ... | @@ -111,6 +119,25 @@ public class AdminMessageServiceImpl implements AdminMessageService { |
| 111 | 119 | return data; |
| 112 | 120 | } |
| 113 | 121 | |
| 122 | + private void ensureCitySelected(Long cityId) { | |
| 123 | + if (cityId == null || cityId < 1) { | |
| 124 | + throw new BizException("请选择租户"); | |
| 125 | + } | |
| 126 | + } | |
| 127 | + | |
| 128 | + private void ensureRiderInCity(Long cityId, Long riderId) { | |
| 129 | + if (riderId == null) { | |
| 130 | + throw new BizException("骑手ID不能为空"); | |
| 131 | + } | |
| 132 | + Rider rider = riderMapper.selectById(riderId); | |
| 133 | + if (rider == null) { | |
| 134 | + throw new BizException("骑手不存在"); | |
| 135 | + } | |
| 136 | + if (!cityId.equals(rider.getCityId())) { | |
| 137 | + throw new BizException("只能给当前租户骑手发送消息"); | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 114 | 141 | @Override |
| 115 | 142 | public List<RiderMessageTemplate> listTemplates() { |
| 116 | 143 | LambdaQueryWrapper<RiderMessageTemplate> wrapper = new LambdaQueryWrapper<>(); | ... | ... |
src/main/java/com/diligrp/rider/service/impl/RiderOrderServiceImpl.java