新增 district.paper.statistics.query 客户用纸统计工具(含 ES 7.17 客户端引入) (#23)
This commit was merged in pull request #23.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package me.iiv.agent.config;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
||||
import co.elastic.clients.transport.rest_client.RestClientTransport;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 双集群 ES 客户端:cloud=云集群、local=本地化集群(老系统 Dictionary.LocalEsDate 分界)。
|
||||
* 低阶 RestClient 也注册为 bean(destroyMethod=close 随容器关闭);
|
||||
* 高阶 ElasticsearchClient 包装同一 RestClient,关闭 client 即关闭底层连接。
|
||||
*/
|
||||
@Configuration
|
||||
public class EsClientConfig {
|
||||
|
||||
@Bean(name = "esCloudRestClient", destroyMethod = "close")
|
||||
public RestClient esCloudRestClient(@Value("${app.es.cloud.hosts}") String hosts,
|
||||
@Value("${app.es.cloud.port:9200}") int port,
|
||||
@Value("${app.es.cloud.username:}") String username,
|
||||
@Value("${app.es.cloud.password:}") String password) {
|
||||
return buildRestClient("cloud", hosts, port, username, password);
|
||||
}
|
||||
|
||||
@Bean(name = "esCloudClient")
|
||||
public ElasticsearchClient esCloudClient(@Qualifier("esCloudRestClient") RestClient restClient) {
|
||||
return new ElasticsearchClient(new RestClientTransport(restClient, new JacksonJsonpMapper()));
|
||||
}
|
||||
|
||||
@Bean(name = "esLocalRestClient", destroyMethod = "close")
|
||||
public RestClient esLocalRestClient(@Value("${app.es.local.hosts}") String hosts,
|
||||
@Value("${app.es.local.port:9200}") int port,
|
||||
@Value("${app.es.local.username:}") String username,
|
||||
@Value("${app.es.local.password:}") String password) {
|
||||
return buildRestClient("local", hosts, port, username, password);
|
||||
}
|
||||
|
||||
@Bean(name = "esLocalClient")
|
||||
public ElasticsearchClient esLocalClient(@Qualifier("esLocalRestClient") RestClient restClient) {
|
||||
return new ElasticsearchClient(new RestClientTransport(restClient, new JacksonJsonpMapper()));
|
||||
}
|
||||
|
||||
private static RestClient buildRestClient(String cluster, String hosts, int port,
|
||||
String username, String password) {
|
||||
HttpHost[] httpHosts = Arrays.stream(hosts.split(","))
|
||||
.map(String::trim)
|
||||
.filter(StringUtils::hasText)
|
||||
.map(host -> new HttpHost(host, port))
|
||||
.toArray(HttpHost[]::new);
|
||||
if (httpHosts.length == 0) {
|
||||
throw new IllegalStateException("app.es." + cluster + ".hosts 未配置有效节点");
|
||||
}
|
||||
RestClientBuilder builder = RestClient.builder(httpHosts)
|
||||
.setRequestConfigCallback(config ->
|
||||
config.setConnectTimeout(1000).setSocketTimeout(60000));
|
||||
if (StringUtils.hasText(username)) {
|
||||
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(AuthScope.ANY,
|
||||
new UsernamePasswordCredentials(username, password));
|
||||
builder.setHttpClientConfigCallback((HttpAsyncClientBuilder httpClientBuilder) ->
|
||||
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* district_strategy 站点策略(m0 库 xiaoqu_comples_d;老系统 beans/b0 走 dataSource0,
|
||||
* 行 id=站点 id,与 district_district_strategy 一一对应)。
|
||||
* 本服务只取 rfid_use_flag 判断换纸卷数口径。
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@TableName("district_strategy")
|
||||
public class DistrictStrategy implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/** 站点id */
|
||||
private Long districtId;
|
||||
|
||||
/** rfid使用标记(0:未使用;1:使用) */
|
||||
private Integer rfidUseFlag;
|
||||
|
||||
/** 数据状态 */
|
||||
private Integer softDelete;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
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.util.Date;
|
||||
|
||||
/**
|
||||
* monthly_statistics 站点用纸日统计(m0 库,每站点每厕位/厕每天一行,XXL-Job 预聚合)。
|
||||
* 字段照搬老项目 xiaoqu-publicbeanandmap beans/b0/MonthlyStatistics.java。
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@TableName("monthly_statistics")
|
||||
public class MonthlyStatistics implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/** 统计时间(当天 0 点) */
|
||||
private Date statisticsTime;
|
||||
|
||||
/** 数据状态 */
|
||||
private Integer softDelete;
|
||||
|
||||
/** 代理商 */
|
||||
private Long agentId;
|
||||
|
||||
/** 所属物业 */
|
||||
private Long propertyId;
|
||||
|
||||
/** 所属区域/站点 */
|
||||
private Long districtId;
|
||||
|
||||
/** 楼id */
|
||||
private Long buildingId;
|
||||
|
||||
/** 楼层 */
|
||||
private String floor;
|
||||
|
||||
/** 厕所id */
|
||||
private Long toiletId;
|
||||
|
||||
/** 位置id(>0=厕位,=0=厕墙) */
|
||||
private Long positionId;
|
||||
|
||||
/** 位置信息 */
|
||||
private String position;
|
||||
|
||||
/** 性别(0男1女2母婴3残卫4第三5其他,无厕所时99) */
|
||||
private Integer gender;
|
||||
|
||||
/** sn码 */
|
||||
private String snCode;
|
||||
|
||||
/** 用纸长度,单位厘米 */
|
||||
private Integer paperLength;
|
||||
|
||||
/** 有效使用人数(仅厕位模式有值,厕墙行恒 0) */
|
||||
private Integer useCount;
|
||||
|
||||
/** 总人次 */
|
||||
private Integer totalCount;
|
||||
|
||||
/** 出纸人次 */
|
||||
private Integer paperCount;
|
||||
|
||||
/** 运行模式(0厕位模式) */
|
||||
private Integer runlMode;
|
||||
|
||||
/** 设备类型(0出纸机1擦手纸机) */
|
||||
private Integer deviceType;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package me.iiv.agent.es;
|
||||
|
||||
/**
|
||||
* paperuselog(换纸记录)ES 查询网关。集群选择(云/本地)与索引选择(热/冷)
|
||||
* 由调用方通过参数传入,本接口只做单次查询,便于单测 mock。
|
||||
*/
|
||||
public interface PaperUseLogEsGateway {
|
||||
|
||||
/**
|
||||
* rfid 去重数:state ∈ {2上纸, 4撤纸} 合并后 rfid 去重(terms 聚合统计 bucket 数,
|
||||
* size 上限 10000,超出部分不计——见设计文档)。
|
||||
*
|
||||
* @param index paperuselog / paperuselog_all
|
||||
* @param localCluster true=本地集群,false=云集群
|
||||
*/
|
||||
long distinctRfidCount(String index, boolean localCluster, Long districtId, Integer gender,
|
||||
Long fromMillis, Long toMillis, Integer deviceType, String positionMode);
|
||||
|
||||
/**
|
||||
* state=2(上纸)记录数(精确 count,不含 state=4、不去重)。
|
||||
* 参数含义同 {@link #distinctRfidCount}。
|
||||
*/
|
||||
long countState2(String index, boolean localCluster, Long districtId, Integer gender,
|
||||
Long fromMillis, Long toMillis, Integer deviceType, String positionMode);
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package me.iiv.agent.es;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.elasticsearch._types.FieldValue;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
||||
import co.elastic.clients.json.JsonData;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* paperuselog ES 查询实现(elasticsearch-java 7.17 强类型 API)。
|
||||
* 查询口径对齐老 MonthlyStatisticsController.getMonthlyStatisticsMap:
|
||||
* term districtId/gender、range createTime [gte,lte](含边界)、deviceType 用 match(老代码字符串参数走 match),
|
||||
* positionMode 厕位=positionsId>0、厕墙=positionsId=0。
|
||||
* ES 异常不静默,抛 IllegalStateException(与老系统静默为 0 不同,见设计文档)。
|
||||
*/
|
||||
@Component
|
||||
public class PaperUseLogEsGatewayImpl implements PaperUseLogEsGateway {
|
||||
|
||||
/** rfid terms 聚合去重上限,超出部分不计入 */
|
||||
private static final int RFID_TERMS_SIZE = 10000;
|
||||
|
||||
private final ElasticsearchClient cloudClient;
|
||||
private final ElasticsearchClient localClient;
|
||||
|
||||
public PaperUseLogEsGatewayImpl(@Qualifier("esCloudClient") ElasticsearchClient cloudClient,
|
||||
@Qualifier("esLocalClient") ElasticsearchClient localClient) {
|
||||
this.cloudClient = cloudClient;
|
||||
this.localClient = localClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long distinctRfidCount(String index, boolean localCluster, Long districtId, Integer gender,
|
||||
Long fromMillis, Long toMillis, Integer deviceType, String positionMode) {
|
||||
List<Query> filters = baseFilters(districtId, gender, fromMillis, toMillis, deviceType, positionMode);
|
||||
// 上纸(2)+撤纸(4) 合并后 rfid 去重(老 :302-342)
|
||||
filters.add(Query.of(q -> q.terms(t -> t.field("state")
|
||||
.terms(tt -> tt.value(List.of(FieldValue.of(2), FieldValue.of(4)))))));
|
||||
try {
|
||||
return client(localCluster).search(s -> s
|
||||
.index(index)
|
||||
.size(0)
|
||||
.trackTotalHits(t -> t.enabled(true))
|
||||
.query(q -> q.bool(b -> b.filter(filters)))
|
||||
.aggregations("rfids", a -> a.terms(t -> t.field("rfid").size(RFID_TERMS_SIZE))),
|
||||
Void.class)
|
||||
.aggregations().get("rfids").sterms().buckets().array().size();
|
||||
} catch (IOException | RuntimeException e) {
|
||||
throw new IllegalStateException("ES 查询换纸 rfid 去重失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long countState2(String index, boolean localCluster, Long districtId, Integer gender,
|
||||
Long fromMillis, Long toMillis, Integer deviceType, String positionMode) {
|
||||
List<Query> filters = baseFilters(districtId, gender, fromMillis, toMillis, deviceType, positionMode);
|
||||
filters.add(Query.of(q -> q.term(t -> t.field("state").value(FieldValue.of(2)))));
|
||||
try {
|
||||
return client(localCluster).count(c -> c.index(index)
|
||||
.query(q -> q.bool(b -> b.filter(filters)))).count();
|
||||
} catch (IOException | RuntimeException e) {
|
||||
throw new IllegalStateException("ES 查询换纸上纸记录数失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Query> baseFilters(Long districtId, Integer gender, Long fromMillis, Long toMillis,
|
||||
Integer deviceType, String positionMode) {
|
||||
List<Query> filters = new ArrayList<>();
|
||||
filters.add(Query.of(q -> q.term(t -> t.field("districtId").value(FieldValue.of(districtId)))));
|
||||
filters.add(Query.of(q -> q.term(t -> t.field("gender").value(FieldValue.of(gender)))));
|
||||
// createTime 为 epoch 毫秒;含边界(老 rangeQuery from/to 均 inclusive)
|
||||
filters.add(Query.of(q -> q.range(r -> r.field("createTime")
|
||||
.gte(JsonData.of(fromMillis)).lte(JsonData.of(toMillis)))));
|
||||
if (deviceType != null) {
|
||||
// 老代码 deviceType 为字符串参数走 matchQuery(BaseSearchSupportImpl2 type=0 非数值分支)
|
||||
filters.add(Query.of(q -> q.match(m -> m.field("deviceType")
|
||||
.query(FieldValue.of(String.valueOf(deviceType))))));
|
||||
}
|
||||
if (StringUtils.hasText(positionMode)) {
|
||||
if ("position".equals(positionMode)) {
|
||||
filters.add(Query.of(q -> q.range(r -> r.field("positionsId").gt(JsonData.of(0)))));
|
||||
} else {
|
||||
filters.add(Query.of(q -> q.term(t -> t.field("positionsId").value(FieldValue.of(0)))));
|
||||
}
|
||||
}
|
||||
return filters;
|
||||
}
|
||||
|
||||
private ElasticsearchClient client(boolean localCluster) {
|
||||
return localCluster ? localClient : cloudClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package me.iiv.agent.es;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* paperuselog 查询的索引/集群选择纯函数(老 DateToConverter.isHotTime / MonthlyStatisticsController
|
||||
* 的 LocalEsDate 判断),抽成静态方法便于单测。常量与比较口径 1:1 对齐老系统。
|
||||
*/
|
||||
public final class PaperUseLogEsPlanner {
|
||||
|
||||
public static final String HOT_INDEX = "paperuselog";
|
||||
public static final String COLD_INDEX = "paperuselog_all";
|
||||
|
||||
/** 老 isHotTime 硬编码 90 天:startDate 距今 <90 天走热索引,否则冷索引 */
|
||||
private static final int HOT_DAYS = 90;
|
||||
|
||||
private PaperUseLogEsPlanner() {
|
||||
}
|
||||
|
||||
/** startDate >= 今天-90天 → 热索引 paperuselog,否则 paperuselog_all */
|
||||
public static String resolveIndex(LocalDate startDate, LocalDate today) {
|
||||
return startDate.isBefore(today.minusDays(HOT_DAYS)) ? COLD_INDEX : HOT_INDEX;
|
||||
}
|
||||
|
||||
/** localEsDate > endDate → 整个区间在本地集群 */
|
||||
public static boolean useLocalCluster(LocalDate localEsDate, LocalDate endDate) {
|
||||
return localEsDate.isAfter(endDate);
|
||||
}
|
||||
|
||||
/** startDate < localEsDate < endDate(与老代码同口径的严格小于)→ 跨界拒绝 */
|
||||
public static void checkNotCrossingBoundary(LocalDate startDate, LocalDate endDate, LocalDate localEsDate) {
|
||||
if (localEsDate.isBefore(endDate) && localEsDate.isAfter(startDate)) {
|
||||
throw new IllegalArgumentException("查询范围跨越本地化数据分界日 " + localEsDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package me.iiv.agent.mapper.m0;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import me.iiv.agent.entity.m0.DistrictStrategy;
|
||||
|
||||
public interface DistrictStrategyMapper extends BaseMapper<DistrictStrategy> {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package me.iiv.agent.mapper.m0;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import me.iiv.agent.entity.m0.MonthlyStatistics;
|
||||
|
||||
public interface MonthlyStatisticsMapper extends BaseMapper<MonthlyStatistics> {
|
||||
}
|
||||
+319
@@ -0,0 +1,319 @@
|
||||
package me.iiv.agent.mcp.tool;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import me.iiv.agent.entity.m0.DistrictDistrictStrategy;
|
||||
import me.iiv.agent.entity.m0.DistrictStrategy;
|
||||
import me.iiv.agent.entity.m0.MonthlyStatistics;
|
||||
import me.iiv.agent.entity.m0.User;
|
||||
import me.iiv.agent.es.PaperUseLogEsGateway;
|
||||
import me.iiv.agent.es.PaperUseLogEsPlanner;
|
||||
import me.iiv.agent.mapper.m0.DistrictDistrictStrategyMapper;
|
||||
import me.iiv.agent.mapper.m0.DistrictStrategyMapper;
|
||||
import me.iiv.agent.mapper.m0.MonthlyStatisticsMapper;
|
||||
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.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* 客户用纸统计(老 MonthlyStatisticsController.getMonthlyStatisticsMap 复刻):
|
||||
* DB 部分聚合 m0 monthly_statistics(用纸长度/人次/台日),ES 部分(paperuselog)
|
||||
* 按站点策略 rfid_use_flag 分口径统计换纸卷数。
|
||||
*/
|
||||
@Component
|
||||
public class DistrictPaperStatisticsTools implements ToolObject {
|
||||
|
||||
private static final String DICTIONARY_KEY = "Dictionary";
|
||||
private static final String DICT_LOCAL_ES_DATE = "LocalEsDate";
|
||||
private static final String DICT_STATISTICS_DAYS = "StatisticsDays";
|
||||
private static final ZoneId SHANGHAI = ZoneId.of("Asia/Shanghai");
|
||||
private static final DateTimeFormatter DAY_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
final DistrictDistrictStrategyMapper districtMapper;
|
||||
final MonthlyStatisticsMapper monthlyStatisticsMapper;
|
||||
final DistrictStrategyMapper districtStrategyMapper;
|
||||
final PermissionService permissionService;
|
||||
final StringRedisTemplate redisTemplate;
|
||||
final PaperUseLogEsGateway paperUseLogEsGateway;
|
||||
|
||||
public DistrictPaperStatisticsTools(DistrictDistrictStrategyMapper districtMapper,
|
||||
MonthlyStatisticsMapper monthlyStatisticsMapper,
|
||||
DistrictStrategyMapper districtStrategyMapper,
|
||||
PermissionService permissionService,
|
||||
StringRedisTemplate redisTemplate,
|
||||
PaperUseLogEsGateway paperUseLogEsGateway) {
|
||||
this.districtMapper = districtMapper;
|
||||
this.monthlyStatisticsMapper = monthlyStatisticsMapper;
|
||||
this.districtStrategyMapper = districtStrategyMapper;
|
||||
this.permissionService = permissionService;
|
||||
this.redisTemplate = redisTemplate;
|
||||
this.paperUseLogEsGateway = paperUseLogEsGateway;
|
||||
}
|
||||
|
||||
@XqTool(category = "hardware", kind = "read")
|
||||
@Tool(name = "district.paper.statistics.query", description = "客户用纸统计:按站点和日期范围统计各性别分组的用纸长度(米)、"
|
||||
+ "使用人次、台日合计与换纸卷数,并给出按日明细。主体数据来自 m0 monthly_statistics 日汇总表;"
|
||||
+ "换纸卷数来自 ES paperuselog(热索引近 90 天,更早走冷索引 paperuselog_all;"
|
||||
+ "按 Redis LocalEsDate 分界选择云/本地集群,跨界查询会被拒绝)。"
|
||||
+ "dateRange 最大天数受 Redis StatisticsDays 限制。所有长度字段单位均为米。")
|
||||
public Map<String, Object> districtPaperStatisticsQuery(
|
||||
@ToolParam(description = "站点 id,正整数") Long districtId,
|
||||
@ToolParam(description = "开始日期,yyyy-MM-dd,含当天") String startDate,
|
||||
@ToolParam(description = "结束日期,yyyy-MM-dd,含当天,不得晚于今天") String endDate,
|
||||
@ToolParam(required = false, description = "厕位/厕墙过滤:position=仅厕位、wall=仅厕墙,缺省不过滤") String positionMode,
|
||||
@ToolParam(required = false, description = "设备类型:0=出纸机、1=擦手纸机,缺省不过滤") Integer deviceType,
|
||||
@ToolParam(required = false, description = "楼 id,正整数,精确匹配") Long buildingId,
|
||||
@ToolParam(required = false, description = "楼层,精确匹配") String floor,
|
||||
@ToolParam(required = false, description = "厕所 id,正整数,精确匹配") Long toiletId) {
|
||||
|
||||
if (districtId == null) {
|
||||
throw new IllegalArgumentException("districtId 必填");
|
||||
}
|
||||
validatePositive(districtId, "districtId");
|
||||
validatePositive(buildingId, "buildingId");
|
||||
validatePositive(toiletId, "toiletId");
|
||||
LocalDate start = parseDay(startDate, "startDate");
|
||||
LocalDate end = parseDay(endDate, "endDate");
|
||||
LocalDate today = LocalDate.now();
|
||||
if (start.isAfter(end)) {
|
||||
throw new IllegalArgumentException("startDate 不能晚于 endDate");
|
||||
}
|
||||
if (end.isAfter(today)) {
|
||||
throw new IllegalArgumentException("endDate 不得晚于今天");
|
||||
}
|
||||
if (StringUtils.hasText(positionMode) && !"position".equals(positionMode) && !"wall".equals(positionMode)) {
|
||||
throw new IllegalArgumentException("positionMode 仅支持 position、wall");
|
||||
}
|
||||
if (deviceType != null && deviceType != 0 && deviceType != 1) {
|
||||
throw new IllegalArgumentException("deviceType 仅支持 0、1");
|
||||
}
|
||||
|
||||
User user = AuthContextHolder.get().getUser();
|
||||
LambdaQueryWrapper<DistrictDistrictStrategy> districtWrapper = new LambdaQueryWrapper<DistrictDistrictStrategy>()
|
||||
.eq(DistrictDistrictStrategy::getSoftDelete, 0)
|
||||
.eq(DistrictDistrictStrategy::getId, districtId);
|
||||
permissionService.applyDistrictRowFilter(districtWrapper, user);
|
||||
DistrictDistrictStrategy district = districtMapper.selectOne(districtWrapper);
|
||||
if (district == null) {
|
||||
throw new IllegalStateException("站点不存在或无权访问");
|
||||
}
|
||||
|
||||
// 老 :121-143:LocalEsDate 集群分界 + StatisticsDays 天数上限;字典缺失 fail fast(老系统 NPE 被吞)
|
||||
LocalDate localEsDate = parseDay(dictionaryValue(DICT_LOCAL_ES_DATE), DICT_LOCAL_ES_DATE);
|
||||
int maxDays = Integer.parseInt(dictionaryValue(DICT_STATISTICS_DAYS).trim());
|
||||
int queryDays = (int) ChronoUnit.DAYS.between(start, end) + 1;
|
||||
if (queryDays > maxDays) {
|
||||
throw new IllegalArgumentException("查询时间段最大" + maxDays + "天");
|
||||
}
|
||||
PaperUseLogEsPlanner.checkNotCrossingBoundary(start, end, localEsDate);
|
||||
String index = PaperUseLogEsPlanner.resolveIndex(start, today);
|
||||
boolean localCluster = PaperUseLogEsPlanner.useLocalCluster(localEsDate, end);
|
||||
|
||||
List<MonthlyStatistics> rows = queryMonthlyStatistics(districtId, start, end,
|
||||
buildingId, floor, toiletId, deviceType);
|
||||
Map<Integer, GenderAgg> byGender = aggregate(rows);
|
||||
|
||||
DistrictStrategy strategy = districtStrategyMapper.selectOne(new LambdaQueryWrapper<DistrictStrategy>()
|
||||
.eq(DistrictStrategy::getSoftDelete, 0)
|
||||
.eq(DistrictStrategy::getId, districtId));
|
||||
// 老 :324-325 对不存在/空 rfidUseFlag 会 NPE;这里按"未使用 rfid"口径走 state=2 count
|
||||
boolean rfidUsed = strategy != null && strategy.getRfidUseFlag() != null
|
||||
&& strategy.getRfidUseFlag() == 1;
|
||||
|
||||
long fromMillis = start.atStartOfDay(SHANGHAI).toInstant().toEpochMilli();
|
||||
// 老 :260-268:endDate=今天 → 上界取今天 00:00(当天数据未跑完),否则 23:59:59(含边界)
|
||||
LocalDate endExclusive = end.equals(today) ? end : end.plusDays(1);
|
||||
long toMillis = endExclusive.atStartOfDay(SHANGHAI).toInstant().toEpochMilli() - 1;
|
||||
|
||||
List<Map<String, Object>> summaryByGender = new ArrayList<>();
|
||||
List<Map<String, Object>> daily = new ArrayList<>();
|
||||
List<Map<String, Object>> toiletGenders = new ArrayList<>();
|
||||
for (Map.Entry<Integer, GenderAgg> entry : new TreeMap<>(byGender).entrySet()) {
|
||||
Integer gender = entry.getKey();
|
||||
GenderAgg agg = entry.getValue();
|
||||
long rfidCount = rfidUsed
|
||||
? paperUseLogEsGateway.distinctRfidCount(index, localCluster, districtId, gender,
|
||||
fromMillis, toMillis, deviceType, positionMode)
|
||||
: paperUseLogEsGateway.countState2(index, localCluster, districtId, gender,
|
||||
fromMillis, toMillis, deviceType, positionMode);
|
||||
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("gender", gender);
|
||||
item.put("genderText", genderText(gender));
|
||||
item.put("paperLengthM", round3(agg.paperCm / 100.0));
|
||||
item.put("useCount", agg.useCount);
|
||||
item.put("perUseLengthM", agg.useCount > 0 ? round3(agg.paperCm / 100.0 / agg.useCount) : null);
|
||||
item.put("deviceDayCount", agg.deviceDayCount());
|
||||
item.put("rfidCount", rfidCount);
|
||||
summaryByGender.add(item);
|
||||
|
||||
for (Map.Entry<String, DailyAgg> dayEntry : agg.daily.entrySet()) {
|
||||
DailyAgg dayAgg = dayEntry.getValue();
|
||||
Map<String, Object> dayItem = new LinkedHashMap<>();
|
||||
dayItem.put("date", dayEntry.getKey());
|
||||
dayItem.put("gender", gender);
|
||||
dayItem.put("paperLengthM", round3(dayAgg.paperCm / 100.0));
|
||||
dayItem.put("useCount", dayAgg.useCount);
|
||||
dayItem.put("deviceDayCount", (long) dayAgg.positionKeys.size());
|
||||
daily.add(dayItem);
|
||||
}
|
||||
|
||||
Map<String, Object> genderItem = new LinkedHashMap<>();
|
||||
genderItem.put("gender", gender);
|
||||
genderItem.put("genderText", genderText(gender));
|
||||
toiletGenders.add(genderItem);
|
||||
}
|
||||
|
||||
Map<String, Object> dateRange = new LinkedHashMap<>();
|
||||
dateRange.put("start", start.toString());
|
||||
dateRange.put("end", end.toString());
|
||||
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("districtId", districtId);
|
||||
result.put("districtName", district.getDistrictName());
|
||||
result.put("dateRange", dateRange);
|
||||
result.put("toiletGenders", toiletGenders);
|
||||
result.put("summaryByGender", summaryByGender);
|
||||
result.put("daily", daily);
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<MonthlyStatistics> queryMonthlyStatistics(Long districtId, LocalDate start, LocalDate end,
|
||||
Long buildingId, String floor, Long toiletId,
|
||||
Integer deviceType) {
|
||||
LambdaQueryWrapper<MonthlyStatistics> wrapper = new LambdaQueryWrapper<MonthlyStatistics>()
|
||||
.eq(MonthlyStatistics::getSoftDelete, 0)
|
||||
.eq(MonthlyStatistics::getDistrictId, districtId)
|
||||
.ge(MonthlyStatistics::getStatisticsTime, Date.from(start.atStartOfDay(SHANGHAI).toInstant()))
|
||||
// [start 00:00:00, end 23:59:59],用左闭右开避免秒精度丢行
|
||||
.lt(MonthlyStatistics::getStatisticsTime, Date.from(end.plusDays(1).atStartOfDay(SHANGHAI).toInstant()));
|
||||
if (buildingId != null) {
|
||||
wrapper.eq(MonthlyStatistics::getBuildingId, buildingId);
|
||||
}
|
||||
if (StringUtils.hasText(floor)) {
|
||||
wrapper.eq(MonthlyStatistics::getFloor, floor);
|
||||
}
|
||||
if (toiletId != null) {
|
||||
wrapper.eq(MonthlyStatistics::getToiletId, toiletId);
|
||||
}
|
||||
if (deviceType != null) {
|
||||
wrapper.eq(MonthlyStatistics::getDeviceType, deviceType);
|
||||
}
|
||||
return monthlyStatisticsMapper.selectList(wrapper);
|
||||
}
|
||||
|
||||
/** 老 :176-249 聚合:gender 分组 + 按日分组;台日=每天 distinct 厕位/厕数之和(positionSet) */
|
||||
private Map<Integer, GenderAgg> aggregate(List<MonthlyStatistics> rows) {
|
||||
Map<Integer, GenderAgg> byGender = new LinkedHashMap<>();
|
||||
for (MonthlyStatistics row : rows) {
|
||||
if (row.getGender() == null) {
|
||||
continue;
|
||||
}
|
||||
GenderAgg agg = byGender.computeIfAbsent(row.getGender(), gender -> new GenderAgg());
|
||||
int paperLength = row.getPaperLength() == null ? 0 : row.getPaperLength();
|
||||
int useCount = row.getUseCount() == null ? 0 : row.getUseCount();
|
||||
agg.paperCm += paperLength;
|
||||
boolean positionRow = row.getPositionId() != null && row.getPositionId() > 0;
|
||||
// useCount 仅厕位模式(runl_mode=0)的厕位行累计,厕墙恒 0(老 :200-203/220-222 意图;
|
||||
// 老代码日分组已存在分支漏判 runlMode,此处按意图统一,见设计文档差异点)
|
||||
boolean countUse = positionRow && row.getRunlMode() != null && row.getRunlMode() == 0;
|
||||
if (countUse) {
|
||||
agg.useCount += useCount;
|
||||
}
|
||||
String day = DAY_FORMAT.format(row.getStatisticsTime().toInstant().atZone(SHANGHAI));
|
||||
DailyAgg dailyAgg = agg.daily.computeIfAbsent(day, key -> new DailyAgg());
|
||||
dailyAgg.paperCm += paperLength;
|
||||
if (countUse) {
|
||||
dailyAgg.useCount += useCount;
|
||||
}
|
||||
// 厕位行按 positionId、厕墙行按 toiletId 计台(老 "p_"/"t_" positionSet)
|
||||
if (positionRow) {
|
||||
dailyAgg.positionKeys.add("p_" + row.getPositionId());
|
||||
} else {
|
||||
dailyAgg.positionKeys.add("t_" + row.getToiletId());
|
||||
}
|
||||
}
|
||||
return byGender;
|
||||
}
|
||||
|
||||
private String dictionaryValue(String field) {
|
||||
Object value = redisTemplate.opsForHash().get(DICTIONARY_KEY, field);
|
||||
if (value == null) {
|
||||
throw new IllegalStateException("Redis Dictionary 缺少 " + field + " 字段,拒绝查询");
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
private static LocalDate parseDay(String value, String name) {
|
||||
if (!StringUtils.hasText(value)) {
|
||||
throw new IllegalArgumentException(name + " 必填,格式 yyyy-MM-dd");
|
||||
}
|
||||
try {
|
||||
return LocalDate.parse(value.trim());
|
||||
} catch (java.time.format.DateTimeParseException e) {
|
||||
throw new IllegalArgumentException(name + " 格式非法,要求 yyyy-MM-dd: " + value);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePositive(Long value, String name) {
|
||||
if (value != null && value < 1) {
|
||||
throw new IllegalArgumentException(name + " 必须为正整数");
|
||||
}
|
||||
}
|
||||
|
||||
private static double round3(double value) {
|
||||
return BigDecimal.valueOf(value).setScale(3, RoundingMode.HALF_UP).doubleValue();
|
||||
}
|
||||
|
||||
private static String genderText(Integer gender) {
|
||||
if (gender == null) {
|
||||
return "未知";
|
||||
}
|
||||
switch (gender) {
|
||||
case 0: return "男";
|
||||
case 1: return "女";
|
||||
case 2: return "母婴";
|
||||
case 3: return "残卫";
|
||||
case 4: return "第三";
|
||||
case 5: return "其他";
|
||||
case 99: return "未知";
|
||||
default: return "未知";
|
||||
}
|
||||
}
|
||||
|
||||
private static final class GenderAgg {
|
||||
long paperCm;
|
||||
long useCount;
|
||||
final Map<String, DailyAgg> daily = new TreeMap<>();
|
||||
|
||||
long deviceDayCount() {
|
||||
return daily.values().stream().mapToLong(day -> day.positionKeys.size()).sum();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class DailyAgg {
|
||||
long paperCm;
|
||||
long useCount;
|
||||
final Set<String> positionKeys = new LinkedHashSet<>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user