综合统计加载慢问题

This commit is contained in:
wanggeng888 2021-04-20 19:51:46 +08:00
parent 1ce135f78e
commit d995a2c91a
5 changed files with 153 additions and 34 deletions

View File

@ -4,9 +4,9 @@ import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.ParamsException;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.utils.RegexUtil;
import com.cm.inspection.reportform.CheckDetailTable;
import com.cm.inspection.service.check.ICheckService;
import com.cm.inspection.service.count.ICountService;
import com.cm.inspection.service.enterprise.IEnterpriseService;
@ -154,8 +154,12 @@ public class CountController extends AbstractController {
public SuccessResultData<Map<String, Object>> countCheckDetailTable(@PathVariable("year") String year,
@RequestParam(name = "areaId", required = false) String areaId,
@RequestParam(name = "areaLevel", required = false) Integer areaLevel) {
Map<String, Object> result = CheckDetailTable.getInstance().get(year, areaId);
if(result == null) {
return countService.countCheckDetailTable(areaId, areaLevel, year);
}
return new SuccessResultData<>(result);
}
@ApiOperation(value = "隐患上报详情表", notes = "隐患上报详情表接口")
@ApiImplicitParams({

View File

@ -0,0 +1,123 @@
package com.cm.inspection.reportform;
import com.cm.common.utils.DateUtil;
import com.cm.inspection.service.count.ICountService;
import com.cm.inspection.service.count.impl.CountServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: WorkTable
* @Description: 综合统计
* @Author: wanggeng
* @Date: 2021/4/20 4:47 下午
* @Version: 1.0
*/
public class CheckDetailTable {
private static final Logger LOG = LoggerFactory.getLogger(CheckDetailTable.class);
/**
* 保存报表内容结构为 {year:{areaId:{具体内容}}}
*/
public Map<String, Map<String, Map<String, Object>>> detailTableMap = new ConcurrentHashMap<>();
private static CheckDetailTable checkDetailTable = CheckDetailTableBuilder.checkDetailTable;
private ICountService countService;
private CheckDetailTable() {
}
public static CheckDetailTable getInstance() {
return checkDetailTable;
}
public CheckDetailTable setCountService(ICountService countService) {
this.countService = countService;
return this;
}
/**
* 得到内容
*
* @param year
* @param areaId
* @return
*/
public Map<String, Object> get(String year, String areaId) {
Map<String, Map<String, Object>> yearMap = detailTableMap.get(year);
if (yearMap == null) {
return null;
}
areaId = StringUtils.isEmpty(areaId) ? CountServiceImpl.DEFAULT_AREA_ID : areaId;
Map<String, Object> areaIdMap = yearMap.get(areaId);
if (areaIdMap == null) {
return null;
}
return areaIdMap;
}
/**
* 刷新统计内容
*/
public void refresh() {
LOG.info("刷新综合统计,开始....");
long startTime = System.currentTimeMillis();
int currentYear = Integer.parseInt(DateUtil.getYear());
for (int year = currentYear; year >= 2020; year--) {
refresh("", 3, String.valueOf(year));
}
long endTime = System.currentTimeMillis();
LOG.info("刷新综合统计,结束,总耗时:{}", (endTime - startTime) / 1000 + "");
}
/**
* 递归加载统计内容
*
* @param areaId
* @param level
* @param year
*/
private void refresh(String areaId, Integer level, String year) {
Map<String, Object> detailMap = getData(areaId, level, year);
if (StringUtils.isEmpty(areaId)) {
areaId = CountServiceImpl.DEFAULT_AREA_ID;
}
List<Map<String, Object>> resultMapList = (List<Map<String, Object>>) detailMap.get("resultList");
if (resultMapList.isEmpty()) {
return;
}
for (Map<String, Object> resultMap : resultMapList) {
Map<String, Map<String, Object>> yearMap = detailTableMap.get(year);
if (yearMap == null) {
yearMap = new HashMap<>();
detailTableMap.put(year, yearMap);
}
yearMap.put(areaId, detailMap);
String resultAreaId = (String) resultMap.get("areaId");
refresh(resultAreaId, level + 1, year);
}
}
/**
* 统计数据
*
* @param areaId
* @param level
* @param year
* @return
*/
private Map<String, Object> getData(String areaId, Integer level, String year) {
return countService.countCheckDetailTable(areaId, level, year).getData();
}
private static class CheckDetailTableBuilder {
public final static CheckDetailTable checkDetailTable = new CheckDetailTable();
}
}

View File

@ -1,12 +1,10 @@
package com.cm.inspection.service.count.impl;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.pojo.dtos.datadictionary.DataDictionaryDTO;
import com.cm.common.plugin.service.datadictionary.IDataDictionaryService;
import com.cm.common.result.SuccessResultData;
import com.cm.common.token.app.AppTokenManager;
import com.cm.common.token.app.entity.AppToken;
import com.cm.common.utils.HashMapUtil;
import com.cm.inspection.pojo.dtos.check.CheckDTO;
import com.cm.inspection.pojo.dtos.checkitem.CheckItemDTO;
@ -28,7 +26,6 @@ import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -67,7 +64,7 @@ public class CountServiceImpl extends BaseService implements ICountService {
private ICheckItemOptionService checkItemOptionService;
@Autowired
private ICheckItemService checkItemService;
private static final String DEFAULT_AREA_ID = "3f62e230-47a5-4ad9-ab01-08fd2c5218d8";
public static final String DEFAULT_AREA_ID = "3f62e230-47a5-4ad9-ab01-08fd2c5218d8";
@Override
public SuccessResultData<Map<String, Object>> countWorkTable(String areaId, Integer areaLevel, String startDate, String endDate) throws SearchException {

View File

@ -1,6 +1,7 @@
package com.cm.inspection.startup;
import com.cm.common.utils.DateUtil;
import com.cm.inspection.reportform.CheckDetailTable;
import com.cm.inspection.service.count.ICountService;
import com.cm.inspection.service.dingding.IDingDingMsgService;
import com.cm.inspection.service.dischargepermit.IDischargePermitService;
@ -14,9 +15,6 @@ import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
@ -54,20 +52,17 @@ public class StartUp implements ApplicationRunner {
processService.deployProcess("check-self", "网格员检查上报流程");
// 初始化统计防止第一次打开慢
Map<String, Object> params = new HashMap<>();
enterpriseService.countEnterpriseByNature(params);
for (int year = currentYear; year >= startYear; year--) {
final String yearStr = String.valueOf(year);
new Thread(() -> {
try {
countService.countCheckDetailTable("", 3, yearStr);
countService.countHiddenDangerReportDetail("", 3, yearStr);
} catch (Exception e) {
e.printStackTrace();
}
CheckDetailTable.getInstance().setCountService(countService).refresh();
}).start();
}
/**
* 每天0点和1点跑一次
*/
@Scheduled(cron = "0 0 23 * * ?")
public void checkDetailTable() {
CheckDetailTable.getInstance().setCountService(countService).refresh();
}
/**

View File

@ -64,18 +64,18 @@
<td>
<a href="javascript:void(0);" class="area-name" data-area-id="{{item.areaId}}" data-area-name="{{item.areaName}}">{{item.areaName}} <i class="fa fa-mail-forward"></i></a>
</td>
<td>{{item.enterpriseReceiveCount}}</td>
<td>{{item.checkPlanCount}}</td>
<td>{{item.checkCount}}</td>
<td>{{item.completeRatio}}%</td>
<td>{{item.needReCheck}}</td>
<td>{{item.countTimeoutCheck}}</td>
<td>{{item.countCheckHiddenDanger}}</td>
<td>{{item.countReCheckHiddenDanger}}</td>
<td>{{item.countRectification}}</td>
<td>{{item.countImmediatelyRectification}}</td>
<td>{{item.rectificationRate}}%</td>
<td>{{item.countUnCoordination}}</td>
<td>{{item.enterpriseReceiveCount ? item.enterpriseReceiveCount : 0}}</td>
<td>{{item.checkPlanCount ? item.checkPlanCount : 0}}</td>
<td>{{item.checkCount ? item.checkCount : 0}}</td>
<td>{{item.completeRatio ? item.completeRatio : 0}}%</td>
<td>{{item.needReCheck ? item.needReCheck : 0}}</td>
<td>{{item.countTimeoutCheck ? item.countTimeoutCheck : 0}}</td>
<td>{{item.countCheckHiddenDanger ? item.countCheckHiddenDanger : 0}}</td>
<td>{{item.countReCheckHiddenDanger ? item.countReCheckHiddenDanger : 0}}</td>
<td>{{item.countRectification ? item.countRectification : 0}}</td>
<td>{{item.countImmediatelyRectification ? item.countImmediatelyRectification : 0}}</td>
<td>{{item.rectificationRate ? item.rectificationRate : 0}}%</td>
<td>{{item.countUnCoordination ? item.countUnCoordination : 0}}</td>
</tr>
{{# } }}
</tbody>