Commit a25cba7d authored by 韩桐桐's avatar 韩桐桐

feat(cyl):市级为维度,各市的充装次数和累计充装量

parent 76222cab
......@@ -111,4 +111,7 @@ public interface CylinderInfoMapper extends BaseMapper<CylinderInfo> {
List<CylinderInfoDto> queryCylinderOfInspectionOverdue();
List<CylinderInfoDto> queryCylinderOfUnqualifiedQuestion();
List<Map<String, Object>> countFillingTimesAndQuantityByCity();
}
......@@ -402,6 +402,18 @@
</if>
</where>
</select>
<select id="countFillingTimesAndQuantityByCity" resultType="java.util.Map">
SELECT regionCode AS regionCode,
COUNT(cfr.app_id) AS fillingTimes,
ROUND(SUM(cfr.filling_quantity)) AS fillingQuantity
FROM (SELECT cu.app_id,
SUBSTRING_INDEX(SUBSTRING_INDEX(cu.region_code, '#', 2), '#', -1) AS regionCode
FROM tz_cylinder_unit cu) cu
LEFT JOIN tz_cylinder_filling_record cfr ON cu.app_id = cfr.app_id
GROUP BY regionCode
</select>
<select id="countNumber" resultType="java.lang.Integer">
SELECT count(DISTINCT(onlyCode))
FROM(
......
package com.yeejoin.amos.boot.module.cylinder.flc.biz.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
......@@ -1349,9 +1350,16 @@ public class CylinderInfoController extends BaseController {
@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
@ApiOperation(httpMethod = "GET", value = "某个企业充装次数及累计充装量统计")
@GetMapping(value = "/countFillingTimesAndQuantityByCompany")
public ResponseModel<Map<String,Object>> countFillingTimesAndQuantityByCompany(@RequestParam(value = "companyId") String companyId,
@RequestParam(value = "startTime", required = false) String startTime,
@RequestParam(value = "endTime", required = false) String endTime) {
return ResponseHelper.buildResponse(cylinderInfoServiceImpl.getBaseMapper().countFillingTimesAndQuantityByCompany(companyId, startTime, endTime));
public ResponseModel<JSONObject> countFillingTimesAndQuantityByCompany(@RequestParam(value = "companyId") String companyId,
@RequestParam(value = "startTime", required = false) String startTime,
@RequestParam(value = "endTime", required = false) String endTime) {
return ResponseHelper.buildResponse(cylinderInfoServiceImpl.countFillingTimesAndQuantityByCompany(companyId, startTime, endTime));
}
@TycloudOperation(ApiLevel = UserType.AGENCY)
@ApiOperation(httpMethod = "GET", value = "市级为维度,各市的充装次数和累计充装量")
@GetMapping(value = "/countFillingTimesAndQuantityByCity")
public ResponseModel<Object> countFillingTimesAndQuantityByCity() {
return ResponseHelper.buildResponse(cylinderInfoServiceImpl.countFillingTimesAndQuantityByCity());
}
}
package com.yeejoin.amos.boot.module.cylinder.flc.biz.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
......@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.yeejoin.amos.boot.biz.common.utils.DateUtils;
import com.yeejoin.amos.boot.module.cylinder.api.dto.WarningMsgDto;
import com.yeejoin.amos.boot.module.cylinder.api.entity.ESCylinderFillingRecordDto;
import com.yeejoin.amos.boot.module.cylinder.api.entity.ESCylinderInfoDto;
import com.yeejoin.amos.boot.module.cylinder.api.entity.MsgLog;
import com.yeejoin.amos.boot.module.cylinder.api.enums.EarlyWarningLevelEnum;
......@@ -33,6 +35,11 @@ import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.metrics.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.BeanUtils;
......@@ -54,9 +61,11 @@ import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* 气瓶基本信息服务实现类
......@@ -69,6 +78,11 @@ import java.util.stream.Collectors;
public class CylinderInfoServiceImpl extends BaseService<CylinderInfoDto, CylinderInfo, CylinderInfoMapper>
implements ICylinderInfoService {
public static final String REGION_CODE = "regionCode";
public static final String FILLING_TIMES = "fillingTimes";
public static final String FILLING_QUANTITY = "fillingQuantity";
public static final String REGION_NAME = "regionName";
private static final String INDEX_NAME = "cylinder_filling";
@Autowired
CylinderUnitServiceImpl cylinderUnitServiceImpl;
......@@ -1208,4 +1222,104 @@ public class CylinderInfoServiceImpl extends BaseService<CylinderInfoDto, Cylind
public Page<CylinderInfoDto> cyinderOutInfoList(Page<CylinderInfoDto> page, CylinderInfoDto cylinderInfoDto, String sort, List<String> appids) {
return cylinderInfoMapper.cyinderOutInfoList(page, cylinderInfoDto, sort, appids);
}
public JSONObject countFillingTimesAndQuantityByCompany(String companyId, String startTime, String endTime) {
Map<String, Object> stringObjectMap = cylinderInfoMapper.countFillingTimesAndQuantityByCompany(companyId, startTime, endTime);
// 组装成柱状图能渲染的格式
//{
// "seriesData": [
// {
// "data": [
// 3
// ],
// "name": "充装次数(次)"
// },
// {
// "data": [
// 1
// ],
// "name": "累计充装量(kg)"
// }
// ],
// "axisData": [
// "总数"
// ]
// }
String template = "{\"seriesData\": [{\"data\": [%s],\"name\": \"充装次数(次)\"},{\"data\": [%s],\"name\": \"累计充装量(kg)\"}],\"axisData\": [\"总数\"]}";
String format = String.format(template,
ObjectUtils.isEmpty(stringObjectMap.get("fillingTimes")) ? 0 : stringObjectMap.get("fillingTimes"),
ObjectUtils.isEmpty(stringObjectMap.get("fillingQuantity")) ? 0 : stringObjectMap.get("fillingQuantity"));
return JSON.parseObject(format);
}
public List<Map<String, Object>> countFillingTimesAndQuantityByCity() {
// 查询所有的市级城市
List<RegionModel> regionModelList = Systemctl.regionClient.queryByLevel("2").getResult();
// 并行处理每个城市的数据
List<CompletableFuture<Map<String, Object>>> futures = regionModelList.stream()
.map(regionModel -> CompletableFuture.supplyAsync(() -> esQuery(regionModel)))
.collect(Collectors.toList());
return futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
}
private Map<String, Object> esQuery(RegionModel regionModel) {
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 将 regionModel.getRegionCode() 转换成匹配中间部分的格式
String regionCodePattern = "*" + regionModel.getRegionCode() + "*";
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.wildcardQuery("regionCode", regionCodePattern));
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.aggregation(
AggregationBuilders.cardinality("total_filling_count")
.field("sequenceNbr.keyword")
);
searchSourceBuilder.aggregation(
AggregationBuilders.sum("total_filling_quantity")
.field("fillingQuantity")
);
searchRequest.source(searchSourceBuilder);
System.out.println("Search Request: " + searchRequest);
try {
// 执行搜索请求
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
// 获取所有聚合结果
Aggregations aggregations = searchResponse.getAggregations();
ParsedCardinality cardinalityAgg = aggregations.get("total_filling_count");
long uniqueCount = cardinalityAgg.getValue();
Sum sumAgg = aggregations.get("total_filling_quantity");
double totalSum = sumAgg.getValue();
System.out.println("填充数量的唯一值数量: " + uniqueCount);
System.out.println("填充量的总和: " + totalSum);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put(REGION_CODE, regionModel.getRegionCode());
resultMap.put(REGION_NAME, regionModel.getRegionName());
resultMap.put(FILLING_TIMES, uniqueCount);
resultMap.put(FILLING_QUANTITY, totalSum);
return resultMap;
} catch (IOException e) {
e.printStackTrace();
}
return Collections.emptyMap();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment