|
@@ -0,0 +1,234 @@
|
|
|
+package com.persagy.apm.energy.report.monthly.outline.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.thread.ExecutorBuilder;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.function.model.Function;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.function.service.IFunctionService;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.functiongroup.model.FunctionGroup;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.functiongroup.service.IFunctionGroupService;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.rel.typefunction.model.ReportTypeFunctionRel;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.rel.typefunction.model.dto.QueryReportTypeFunctionRelDTO;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.rel.typefunction.service.IReportTypeFunctionRelService;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.rel.typefunctiongroup.model.ReportTypeFunctionGroupRel;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.rel.typefunctiongroup.model.dto.QueryReportTypeFunctionGroupRelDTO;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.rel.typefunctiongroup.service.IReportTypeFunctionGroupRelService;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.type.constant.enums.BuildingTypeEnum;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.type.model.ReportType;
|
|
|
+import com.persagy.apm.energy.report.monthly.config.type.service.IReportTypeService;
|
|
|
+import com.persagy.apm.energy.report.monthly.detail.business.model.OpenPower;
|
|
|
+import com.persagy.apm.energy.report.monthly.detail.business.model.dto.AddReportBusinessDetailDTO;
|
|
|
+import com.persagy.apm.energy.report.monthly.detail.business.model.vo.GroupInfo;
|
|
|
+import com.persagy.apm.energy.report.monthly.detail.business.model.vo.PowerItemVO;
|
|
|
+import com.persagy.apm.energy.report.monthly.detail.business.service.IReportBusinessDetailService;
|
|
|
+import com.persagy.apm.energy.report.monthly.functionvalue.service.IFunctionValueService;
|
|
|
+import com.persagy.apm.energy.report.monthly.outline.constants.enums.BusinessReportParagraphs;
|
|
|
+import com.persagy.apm.energy.report.monthly.outline.constants.enums.ReportStateEnum;
|
|
|
+import com.persagy.apm.energy.report.monthly.outline.model.ReportOutline;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.assertj.core.util.Lists;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.LinkedBlockingQueue;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 消费消息线程池
|
|
|
+ *
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/12 2:53 下午
|
|
|
+ **/
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class GenerateReportThreadPool {
|
|
|
+ @Autowired
|
|
|
+ private IReportBusinessDetailService businessDetailService;
|
|
|
+ @Autowired
|
|
|
+ private IReportTypeService reportTypeService;
|
|
|
+ @Autowired
|
|
|
+ private IReportTypeFunctionGroupRelService reportTypeFunctionGroupRelService;
|
|
|
+ @Autowired
|
|
|
+ private IReportTypeFunctionRelService reportTypeFunctionRelService;
|
|
|
+ @Autowired
|
|
|
+ private IFunctionGroupService functionGroupService;
|
|
|
+ @Autowired
|
|
|
+ private IFunctionService functionService;
|
|
|
+ @Autowired
|
|
|
+ private IFunctionValueService functionValueService;
|
|
|
+
|
|
|
+ private final ExecutorService es;
|
|
|
+
|
|
|
+ public GenerateReportThreadPool() {
|
|
|
+ // 初始化线程池
|
|
|
+ es = ExecutorBuilder.create()
|
|
|
+ .setCorePoolSize(5)
|
|
|
+ .setMaxPoolSize(10)
|
|
|
+ .setWorkQueue(new LinkedBlockingQueue<>(100))
|
|
|
+ .setHandler(new ThreadPoolExecutor.CallerRunsPolicy())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成报告
|
|
|
+ *
|
|
|
+ * @param reportOutline 报告outline对象
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/24 4:09 下午
|
|
|
+ */
|
|
|
+ public void generateReport(ReportOutline reportOutline) {
|
|
|
+ es.execute(() -> {
|
|
|
+ try {
|
|
|
+ String reportTypeId = reportOutline.getReportTypeId();
|
|
|
+ ReportType reportType = reportTypeService.queryReportTypeDetail(reportTypeId);
|
|
|
+ if (BuildingTypeEnum.BUSINESS.getType().equals(reportType.getBuildingType())) {
|
|
|
+ AddReportBusinessDetailDTO addReportBusinessDetailDTO = new AddReportBusinessDetailDTO();
|
|
|
+ OpenPower openPower = getOpenPower(reportOutline);
|
|
|
+ // TODO: 2021/5/24 给其他属性赋值
|
|
|
+ addReportBusinessDetailDTO.setOpenPower(openPower);
|
|
|
+ businessDetailService.createReportBusinessDetail(addReportBusinessDetailDTO);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("生成报告失败", e);
|
|
|
+ reportOutline.setStatus(ReportStateEnum.BUILD_FAILED.getType());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取在营项目用电量
|
|
|
+ *
|
|
|
+ * @param reportOutline 报告outline对象
|
|
|
+ * @return 在营项目用电量
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/24 4:14 下午
|
|
|
+ */
|
|
|
+ private OpenPower getOpenPower(ReportOutline reportOutline) {
|
|
|
+ OpenPower openPower = new OpenPower();
|
|
|
+ String reportTypeId = reportOutline.getReportTypeId();
|
|
|
+ Date reportMonth = reportOutline.getReportMonth();
|
|
|
+
|
|
|
+ // 获取在营项目用电量下拥有的信息点分组和信息点
|
|
|
+ List<String> groupIdList = getGroupsByParagraph(
|
|
|
+ reportTypeId, BusinessReportParagraphs.OPEN_POWER);
|
|
|
+
|
|
|
+ openPower.setGroups(getPowerGroupInfos(groupIdList, reportMonth));
|
|
|
+
|
|
|
+ List<String> functionIdList = getFunctionsByParagraph(
|
|
|
+ reportTypeId, BusinessReportParagraphs.OPEN_POWER);
|
|
|
+ openPower.setItems(getPowerItemInfos(functionIdList, reportMonth));
|
|
|
+ // TODO: 2021/5/24 计算统计值
|
|
|
+ // openPower.setSummary();
|
|
|
+ return openPower;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用电量条目信息
|
|
|
+ *
|
|
|
+ * @param functionIdList 用电量条目列表
|
|
|
+ * @param reportMonth 报告月份
|
|
|
+ * @return 用电量条目列表
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/24 7:15 下午
|
|
|
+ */
|
|
|
+ private List<PowerItemVO> getPowerItemInfos(List<String> functionIdList, Date reportMonth) {
|
|
|
+ List<PowerItemVO> powerItemVOList = Lists.newArrayList();
|
|
|
+ if (CollectionUtils.isEmpty(functionIdList)) {
|
|
|
+ return powerItemVOList;
|
|
|
+ }
|
|
|
+ for (String functionId : functionIdList) {
|
|
|
+ Function function = functionService.queryFunctionDetail(functionId);
|
|
|
+ String itemId = function.getItemId();
|
|
|
+ // TODO: 2021/5/24 获取条目名称
|
|
|
+ String itemName = "";
|
|
|
+ PowerItemVO powerItemVO = new PowerItemVO();
|
|
|
+ powerItemVO.setName(itemName);
|
|
|
+ // TODO: 2021/5/24 调用functionValueService获取值
|
|
|
+ // powerItemVO.setCurrentMonth();
|
|
|
+ // ...
|
|
|
+ powerItemVOList.add(powerItemVO);
|
|
|
+ }
|
|
|
+ return powerItemVOList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用电量分组信息
|
|
|
+ *
|
|
|
+ * @param groupIdList 分组id列表
|
|
|
+ * @param reportMonth 报告月份
|
|
|
+ * @return 分组对象列表
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/24 7:12 下午
|
|
|
+ */
|
|
|
+ private List<GroupInfo<PowerItemVO>> getPowerGroupInfos(List<String> groupIdList, Date reportMonth) {
|
|
|
+ List<GroupInfo<PowerItemVO>> groupInfoList = Lists.newArrayList();
|
|
|
+ if (CollectionUtils.isEmpty(groupIdList)) {
|
|
|
+ return groupInfoList;
|
|
|
+ }
|
|
|
+ for (String groupId : groupIdList) {
|
|
|
+ GroupInfo<PowerItemVO> groupInfo = new GroupInfo<>();
|
|
|
+ FunctionGroup functionGroup = functionGroupService.queryFunctionGroupDetail(groupId);
|
|
|
+ groupInfo.setGroupName(functionGroup.getName());
|
|
|
+ // 获取组下的分组(本期不做)
|
|
|
+ // 获取组下的分项
|
|
|
+ String functionIdStr = functionGroup.getFunctionIds();
|
|
|
+ List<String> functionIds = Lists.newArrayList(functionIdStr.split(","));
|
|
|
+ List<PowerItemVO> powerItemInfos = getPowerItemInfos(functionIds, reportMonth);
|
|
|
+ groupInfo.setItems(powerItemInfos);
|
|
|
+ // TODO: 2021/5/24 计算统计值
|
|
|
+ // groupInfo.setSummary();
|
|
|
+ }
|
|
|
+ return groupInfoList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据段落获取报告类型关联的分组
|
|
|
+ *
|
|
|
+ * @param reportTypeId 报告类型
|
|
|
+ * @param belongParagraph 报告段落
|
|
|
+ * @return 报告段落包含的分组id列表
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/24 4:31 下午
|
|
|
+ */
|
|
|
+ private List<String> getGroupsByParagraph(String reportTypeId, String belongParagraph) {
|
|
|
+ List<String> groupIdList = Lists.newArrayList();
|
|
|
+ QueryReportTypeFunctionGroupRelDTO queryReportTypeFunctionGroupRelDTO = new QueryReportTypeFunctionGroupRelDTO();
|
|
|
+ queryReportTypeFunctionGroupRelDTO.setReportTypeId(reportTypeId);
|
|
|
+ queryReportTypeFunctionGroupRelDTO.setBelongParagraph(belongParagraph);
|
|
|
+ List<ReportTypeFunctionGroupRel> reportTypeFunctionGroupRels = reportTypeFunctionGroupRelService.
|
|
|
+ queryReportTypeFunctionGroupRelList(queryReportTypeFunctionGroupRelDTO);
|
|
|
+ if (!CollectionUtils.isEmpty(reportTypeFunctionGroupRels)) {
|
|
|
+ groupIdList = reportTypeFunctionGroupRels.stream().
|
|
|
+ map(ReportTypeFunctionGroupRel::getFunctionGroupId).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return groupIdList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据段落获取报告类型关联的信息点
|
|
|
+ *
|
|
|
+ * @param reportTypeId 报告类型
|
|
|
+ * @param belongParagraph 报告段落
|
|
|
+ * @return 报告段落包含的信息点id列表
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/24 4:31 下午
|
|
|
+ */
|
|
|
+ private List<String> getFunctionsByParagraph(String reportTypeId, String belongParagraph) {
|
|
|
+ List<String> functionIdList = Lists.newArrayList();
|
|
|
+ QueryReportTypeFunctionRelDTO queryReportTypeFunctionRelDTO = new QueryReportTypeFunctionRelDTO();
|
|
|
+ queryReportTypeFunctionRelDTO.setReportTypeId(reportTypeId);
|
|
|
+ queryReportTypeFunctionRelDTO.setBelongParagraph(belongParagraph);
|
|
|
+ List<ReportTypeFunctionRel> reportTypeFunctionRels = reportTypeFunctionRelService.queryReportTypeFunctionRelList(queryReportTypeFunctionRelDTO);
|
|
|
+ if (!CollectionUtils.isEmpty(reportTypeFunctionRels)) {
|
|
|
+ functionIdList = reportTypeFunctionRels.stream().
|
|
|
+ map(ReportTypeFunctionRel::getFunctionId).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return functionIdList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|