feat(mcp): 新增站点列表与详情工具 (REQ-20260908-0006)

This commit is contained in:
xqzp2026
2026-09-08 19:25:12 +09:30
parent b8cc1992b7
commit 686ce2daf8
7 changed files with 776 additions and 1 deletions
@@ -0,0 +1,92 @@
package me.iiv.agent.entity.m0;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Date;
@Getter
@Setter
@ToString
@TableName("district_district_strategy")
public class DistrictDistrictStrategy implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private Long createId;
private Date createTime;
private Date modifyTime;
private Integer softDelete;
private String districtName;
private String address;
private Long propertyId;
private Long agentId;
private String province;
private String city;
private String countyseat;
private String memo;
private String contact;
private String mail;
private Integer buildCount;
private String districtNumber;
private String phone;
private Integer installType;
private Integer warning;
private Integer airdetectorFlag;
private Integer warning2;
private Integer warning3;
private Integer warningTime;
private Integer onlineState;
private Integer paperPercentWarn;
private Integer pointState;
private Integer assignmentWorkState;
private Integer airState;
private String businessFormat;
private String posDevicePaperNorm;
private String toiletDevicePaperNorm;
private Integer messageFlag;
private Integer reportFlag;
private Double fineMoney;
private Integer lockMachineFlag;
private Integer eventRepairFlag;
private Integer rfidUseFlag;
private Integer calibratedFlag;
private String salesName;
private Long salesId;
private Integer waitTimes;
private Integer noPaperState;
private Integer lowerPaperState;
private Integer districtState;
private Integer deviceSalesType;
private Integer paperSalesType;
private LocalDate weaningDate;
private LocalDate honourDate;
private LocalDate testDate;
private Integer powerWarning;
private Integer pointDeduction;
private Integer testDays;
private Integer illegalPaperDisable;
private Integer effectiveUseTime;
private Long attributeId;
private String attributeName;
private Integer hardwareCount;
private Double consumablesPrice;
private Integer billDate;
private Integer foamCountWarning;
private Integer foamPowerWarning;
private Long serviceCompanyId;
private String serviceCompany;
private String billType;
private Integer consumablesType;
private Integer maintainOrderWarning;
private Integer lnfluenceLevel;
private Integer isSmart;
}
@@ -0,0 +1,7 @@
package me.iiv.agent.mapper.m0;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import me.iiv.agent.entity.m0.DistrictDistrictStrategy;
public interface DistrictDistrictStrategyMapper extends BaseMapper<DistrictDistrictStrategy> {
}
@@ -0,0 +1,324 @@
package me.iiv.agent.mcp.tool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.iiv.agent.entity.m0.Agent;
import me.iiv.agent.entity.m0.DistrictDistrictStrategy;
import me.iiv.agent.entity.m0.Properyt;
import me.iiv.agent.entity.m0.User;
import me.iiv.agent.mapper.m0.AgentMapper;
import me.iiv.agent.mapper.m0.DistrictDistrictStrategyMapper;
import me.iiv.agent.mapper.m0.ProperytMapper;
import me.iiv.agent.mapper.m0.UserMapper;
import me.iiv.agent.mcp.ToolObject;
import me.iiv.agent.mcp.XqTool;
import me.iiv.agent.permission.PermissionService;
import me.iiv.agent.security.AuthContextHolder;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Component
public class DistrictTools implements ToolObject {
private static final int DEFAULT_ROWS = 20;
private static final int MAX_ROWS = 100;
final DistrictDistrictStrategyMapper districtMapper;
final ProperytMapper properytMapper;
final AgentMapper agentMapper;
final UserMapper userMapper;
final PermissionService permissionService;
final StringRedisTemplate redisTemplate;
public DistrictTools(DistrictDistrictStrategyMapper districtMapper, ProperytMapper properytMapper,
AgentMapper agentMapper, UserMapper userMapper,
PermissionService permissionService, StringRedisTemplate redisTemplate) {
this.districtMapper = districtMapper;
this.properytMapper = properytMapper;
this.agentMapper = agentMapper;
this.userMapper = userMapper;
this.permissionService = permissionService;
this.redisTemplate = redisTemplate;
}
@XqTool(category = "customer", kind = "read")
@Tool(name = "district.list.query", description = "分页查询当前用户可见的站点。"
+ "支持站点 id、名称、状态、省市、代理商和物业筛选;名称、省、市为模糊匹配,其余为精确匹配。"
+ "page 默认 1,rows 默认 20、上限 100,固定按 id 倒序。"
+ "返回轻量站点字段,物业名和代理商名使用批量查询富化。")
public Map<String, Object> districtListQuery(
@ToolParam(required = false, description = "站点 id,正整数,精确匹配") Long id,
@ToolParam(required = false, description = "站点名称,模糊匹配") String districtName,
@ToolParam(required = false, description = "站点状态:0=测试 1=履约 2=演示 3=撤机") Integer districtState,
@ToolParam(required = false, description = "省份,模糊匹配") String province,
@ToolParam(required = false, description = "城市,模糊匹配") String city,
@ToolParam(required = false, description = "代理商 id,正整数,精确匹配") Long agentId,
@ToolParam(required = false, description = "物业 id,正整数,精确匹配") Long propertyId,
@ToolParam(required = false, description = "页码,从 1 开始,默认 1") Integer page,
@ToolParam(required = false, description = "每页条数,默认 20,上限 100") Integer rows) {
validatePositive(id, "id");
validatePositive(agentId, "agentId");
validatePositive(propertyId, "propertyId");
if (districtState != null && (districtState < 0 || districtState > 3)) {
throw new IllegalArgumentException("districtState 仅支持 0、1、2、3");
}
User user = AuthContextHolder.get().getUser();
int pageNo = page == null || page < 1 ? 1 : page;
boolean truncated = rows != null && rows > MAX_ROWS;
int pageSize = rows == null || rows < 1 ? DEFAULT_ROWS : Math.min(rows, MAX_ROWS);
LambdaQueryWrapper<DistrictDistrictStrategy> wrapper = new LambdaQueryWrapper<DistrictDistrictStrategy>()
.eq(DistrictDistrictStrategy::getSoftDelete, 0);
if (id != null) {
wrapper.eq(DistrictDistrictStrategy::getId, id);
}
if (StringUtils.hasText(districtName)) {
wrapper.like(DistrictDistrictStrategy::getDistrictName, districtName);
}
if (districtState != null) {
wrapper.eq(DistrictDistrictStrategy::getDistrictState, districtState);
}
if (StringUtils.hasText(province)) {
wrapper.like(DistrictDistrictStrategy::getProvince, province);
}
if (StringUtils.hasText(city)) {
wrapper.like(DistrictDistrictStrategy::getCity, city);
}
if (agentId != null) {
wrapper.eq(DistrictDistrictStrategy::getAgentId, agentId);
}
if (propertyId != null) {
wrapper.eq(DistrictDistrictStrategy::getPropertyId, propertyId);
}
permissionService.applyDistrictRowFilter(wrapper, user);
Long total = districtMapper.selectCount(wrapper);
wrapper.select(DistrictDistrictStrategy::getId, DistrictDistrictStrategy::getDistrictName,
DistrictDistrictStrategy::getDistrictNumber, DistrictDistrictStrategy::getPropertyId,
DistrictDistrictStrategy::getAgentId, DistrictDistrictStrategy::getProvince,
DistrictDistrictStrategy::getCity, DistrictDistrictStrategy::getCountyseat,
DistrictDistrictStrategy::getAddress, DistrictDistrictStrategy::getDistrictState,
DistrictDistrictStrategy::getOnlineState, DistrictDistrictStrategy::getHardwareCount,
DistrictDistrictStrategy::getIsSmart)
.orderByDesc(DistrictDistrictStrategy::getId)
.last("LIMIT " + (long) (pageNo - 1) * pageSize + "," + pageSize);
List<DistrictDistrictStrategy> districts = districtMapper.selectList(wrapper);
Map<Long, String> propertyNames = propertyNames(districts);
Map<Long, String> agentNames = agentNames(districts);
List<Map<String, Object>> records = new ArrayList<>();
for (DistrictDistrictStrategy district : districts) {
Map<String, Object> record = new LinkedHashMap<>();
record.put("id", district.getId());
record.put("districtName", district.getDistrictName());
record.put("districtNumber", district.getDistrictNumber());
record.put("propertyId", district.getPropertyId());
record.put("propertyName", name(propertyNames, district.getPropertyId()));
record.put("agentId", district.getAgentId());
record.put("agentName", name(agentNames, district.getAgentId()));
record.put("province", district.getProvince());
record.put("city", district.getCity());
record.put("countyseat", district.getCountyseat());
record.put("address", district.getAddress());
record.put("districtState", district.getDistrictState());
record.put("onlineState", district.getOnlineState());
record.put("hardwareCount", district.getHardwareCount());
record.put("isSmart", district.getIsSmart());
records.add(record);
}
Map<String, Object> result = new LinkedHashMap<>();
result.put("total", total);
result.put("page", pageNo);
result.put("rows", pageSize);
if (truncated) {
result.put("notice", "rows 超过上限 100,已截断为 100");
}
result.put("records", records);
return result;
}
@XqTool(category = "customer", kind = "read")
@Tool(name = "district.detail.get", description = "按站点 id 查询当前用户可见的站点详情。"
+ "站点不存在或无权访问均返回 found=false 和相同提示,防止枚举越权站点。"
+ "返回固定业务字段白名单,不包含软删标记、内部服务地址和重复字段。")
public Map<String, Object> districtDetailGet(
@ToolParam(description = "站点 id,正整数") Long id) {
if (id == null) {
throw new IllegalArgumentException("id 必填");
}
validatePositive(id, "id");
User user = AuthContextHolder.get().getUser();
LambdaQueryWrapper<DistrictDistrictStrategy> wrapper = new LambdaQueryWrapper<DistrictDistrictStrategy>()
.eq(DistrictDistrictStrategy::getSoftDelete, 0)
.eq(DistrictDistrictStrategy::getId, id);
permissionService.applyDistrictRowFilter(wrapper, user);
DistrictDistrictStrategy district = districtMapper.selectOne(wrapper);
Map<String, Object> result = new LinkedHashMap<>();
if (district == null) {
result.put("found", false);
result.put("message", "站点不存在或无权访问");
return result;
}
result.put("found", true);
result.put("data", detail(district));
return result;
}
private Map<String, Object> detail(DistrictDistrictStrategy district) {
Properyt property = district.getPropertyId() == null ? null : properytMapper.selectById(district.getPropertyId());
Agent agent = district.getAgentId() == null ? null : agentMapper.selectById(district.getAgentId());
User creator = district.getCreateId() == null ? null : userMapper.selectById(district.getCreateId());
String billType = StringUtils.hasText(district.getBillType()) ? district.getBillType() : "1month";
Map<String, Object> data = new LinkedHashMap<>();
data.put("id", district.getId());
data.put("districtName", district.getDistrictName());
data.put("districtNumber", district.getDistrictNumber());
data.put("propertyId", district.getPropertyId());
data.put("propertyName", property == null ? "未查询到" : property.getName());
data.put("agentId", district.getAgentId());
data.put("agentName", agent == null ? "未查询到" : agent.getName());
data.put("salesId", district.getSalesId());
data.put("salesName", district.getSalesName());
data.put("serviceCompanyId", district.getServiceCompanyId());
data.put("serviceCompany", district.getServiceCompany());
data.put("province", district.getProvince());
data.put("city", district.getCity());
data.put("countyseat", district.getCountyseat());
data.put("address", district.getAddress());
data.put("contact", district.getContact());
data.put("phone", district.getPhone());
data.put("mail", district.getMail());
data.put("businessFormat", district.getBusinessFormat());
data.put("attributeId", district.getAttributeId());
data.put("attributeName", district.getAttributeName());
data.put("installType", district.getInstallType());
data.put("isSmart", district.getIsSmart());
data.put("lnfluenceLevel", district.getLnfluenceLevel());
data.put("buildCount", district.getBuildCount());
data.put("hardwareCount", district.getHardwareCount());
data.put("airdetectorFlag", district.getAirdetectorFlag());
data.put("districtState", district.getDistrictState());
data.put("onlineState", district.getOnlineState());
data.put("testDate", district.getTestDate());
data.put("honourDate", district.getHonourDate());
data.put("weaningDate", district.getWeaningDate());
data.put("testDays", district.getTestDays());
data.put("deviceSalesType", district.getDeviceSalesType());
data.put("paperSalesType", district.getPaperSalesType());
data.put("consumablesType", district.getConsumablesType());
data.put("posDevicePaperNorm", district.getPosDevicePaperNorm());
data.put("toiletDevicePaperNorm", district.getToiletDevicePaperNorm());
data.put("pointState", district.getPointState());
data.put("assignmentWorkState", district.getAssignmentWorkState());
data.put("airState", district.getAirState());
data.put("messageFlag", district.getMessageFlag());
data.put("reportFlag", district.getReportFlag());
data.put("lockMachineFlag", district.getLockMachineFlag());
data.put("eventRepairFlag", district.getEventRepairFlag());
data.put("rfidUseFlag", district.getRfidUseFlag());
data.put("calibratedFlag", district.getCalibratedFlag());
data.put("illegalPaperDisable", district.getIllegalPaperDisable());
data.put("lowerPaperState", district.getLowerPaperState());
data.put("noPaperState", district.getNoPaperState());
data.put("powerWarning", district.getPowerWarning());
data.put("waitTimes", district.getWaitTimes());
data.put("warning", district.getWarning());
data.put("warning2", district.getWarning2());
data.put("warning3", district.getWarning3());
data.put("warningTime", district.getWarningTime());
data.put("paperPercentWarn", district.getPaperPercentWarn());
data.put("maintainOrderWarning", district.getMaintainOrderWarning());
data.put("foamCountWarning", district.getFoamCountWarning());
data.put("foamPowerWarning", district.getFoamPowerWarning());
data.put("pointDeduction", district.getPointDeduction());
data.put("effectiveUseTime", district.getEffectiveUseTime());
data.put("consumablesPrice", district.getConsumablesPrice());
data.put("billDate", district.getBillDate());
data.put("billType", billType);
data.put("billTypeStr", billTypeName(billType));
data.put("fineMoney", district.getFineMoney());
data.put("districtRefresh", redisInteger("DistrictRefresh:" + district.getId(), 5));
data.put("forbidFunction", redisInteger("DistrictForbidFunction:" + district.getId(), 0));
data.put("createId", district.getCreateId());
data.put("createName", creator == null ? "未分配" : creator.getName());
data.put("createTime", format(district.getCreateTime()));
data.put("modifyTime", format(district.getModifyTime()));
data.put("memo", district.getMemo());
return data;
}
private Map<Long, String> propertyNames(List<DistrictDistrictStrategy> districts) {
List<Long> ids = districts.stream().map(DistrictDistrictStrategy::getPropertyId)
.filter(Objects::nonNull).distinct().collect(Collectors.toList());
if (ids.isEmpty()) {
return Map.of();
}
return properytMapper.selectBatchIds(ids).stream()
.collect(Collectors.toMap(Properyt::getId,
property -> property.getName() == null ? "未查询到" : property.getName(), (a, b) -> a));
}
private Map<Long, String> agentNames(List<DistrictDistrictStrategy> districts) {
List<Long> ids = districts.stream().map(DistrictDistrictStrategy::getAgentId)
.filter(Objects::nonNull).distinct().collect(Collectors.toList());
if (ids.isEmpty()) {
return Map.of();
}
return agentMapper.selectBatchIds(ids).stream()
.collect(Collectors.toMap(Agent::getId,
agent -> agent.getName() == null ? "未查询到" : agent.getName(), (a, b) -> a));
}
private static String name(Map<Long, String> names, Long id) {
return id == null ? "未查询到" : names.getOrDefault(id, "未查询到");
}
private int redisInteger(String key, int defaultValue) {
String value = redisTemplate.opsForValue().get(key);
return value == null ? defaultValue : Integer.parseInt(value);
}
private static String billTypeName(String billType) {
if (billType.contains("month")) {
return billType.replace("month", "个月账期");
}
if (billType.contains("quarter")) {
return billType.replace("quarter", "季度账期");
}
return billType;
}
private static String format(java.util.Date value) {
return value == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
}
private static void validatePositive(Long value, String name) {
if (value != null && value < 1) {
throw new IllegalArgumentException(name + " 必须为正整数");
}
}
}
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.iiv.agent.entity.m0.Agent;
import me.iiv.agent.entity.m0.AgentManageDistrictAuth;
import me.iiv.agent.entity.m0.Authority;
import me.iiv.agent.entity.m0.DistrictDistrictStrategy;
import me.iiv.agent.entity.m0.Properyt;
import me.iiv.agent.entity.m0.User;
import me.iiv.agent.entity.m2.WarehouseAgentAuth;
@@ -243,4 +244,50 @@ public class PermissionService {
wrapper.in(Properyt::getAgentId, ids);
}
}
/**
* 站点行权限:1:1 对齐老接口 districtlistByPage 使用的
* getByIsAgent(..., propertyFlag=true, className="property_id")。
*/
public void applyDistrictRowFilter(LambdaQueryWrapper<DistrictDistrictStrategy> wrapper, User user) {
if (!isAgentAuthority(user.getAuthorityId())) {
return;
}
if (user.getAgentId() == null) {
throw new IllegalStateException("代理商用户未关联代理商,拒绝访问业务数据");
}
List<Long> ids = new ArrayList<>(subordinateAndSelfIds(user));
List<WarehouseAgentAuth> agentlist = managedAuths(user.getId());
ids.addAll(agentlist.stream().map(WarehouseAgentAuth::getManagerAgentId).collect(Collectors.toList()));
if (isSalesNonLeader(user)) {
List<Long> propertyIds = properytMapper.selectList(new LambdaQueryWrapper<Properyt>()
.eq(Properyt::getSoftDelete, 0)
.eq(Properyt::getCreateId, user.getId())
.select(Properyt::getId))
.stream().map(Properyt::getId).collect(Collectors.toList());
if (!agentlist.isEmpty()) {
ids.remove(user.getAgentId());
if (!ids.isEmpty()) {
propertyIds.addAll(properytMapper.selectList(new LambdaQueryWrapper<Properyt>()
.eq(Properyt::getSoftDelete, 0)
.in(Properyt::getAgentId, ids)
.select(Properyt::getId))
.stream().map(Properyt::getId).collect(Collectors.toList()));
}
}
wrapper.in(DistrictDistrictStrategy::getPropertyId,
propertyIds.isEmpty() ? List.of(0L) : propertyIds);
return;
}
List<Long> managedPropertyIds = managedPropertyIds(ids);
if (!managedPropertyIds.isEmpty()) {
wrapper.and(w -> w.in(DistrictDistrictStrategy::getAgentId, ids)
.or().in(DistrictDistrictStrategy::getPropertyId, managedPropertyIds));
} else {
wrapper.in(DistrictDistrictStrategy::getAgentId, ids);
}
}
}