2fecda74 厉元元

自定义分析改造

1 个父辈 c3ba1225
......@@ -32,17 +32,18 @@ public class CustomConsumeController {
/**
* <pre>
* 自定义分析获取数据类型
* 自定义分析获取数据类型
* </pre>
* @param granularity 时间粒度
* @return
* @author Li Yuanyuan, 2021年1月29日 下午3:07:12
*/
@ApiOperation(value="自定义分析获取数据类型", notes="自定义分析获取数据类型")
@GetMapping(value = "/getDataType")
public Result<List<TreeData>> getDataType(){
public Result<List<TreeData>> getDataType(String granularity){
Result<List<TreeData>> result = new Result<List<TreeData>>();
List<TreeData> dataTypeList = new ArrayList<TreeData>();
dataTypeList = customConsumeService.getDataType();
dataTypeList = customConsumeService.getDataType(granularity);
result.setResult(dataTypeList);
result.setSuccess(true);
return result;
......@@ -140,9 +141,9 @@ public class CustomConsumeController {
@ApiOperation(value="填报指标获取", notes="填报指标获取")
@GetMapping(value = "/queryDataFillIndex")
public Result<List<Map<String,Object>>> queryDataFillIndex(String reportId) {
public Result<List<Map<String,Object>>> queryDataFillIndex(String reportId,String granularity) {
Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();
List<Map<String, Object>> list = customConsumeService.queryDataFillIndex(reportId);
List<Map<String, Object>> list = customConsumeService.queryDataFillIndex(reportId,granularity);
result.setResult(list);
result.setSuccess(true);
return result;
......
......@@ -41,4 +41,9 @@ public class AnalyseCustomConsume {
@Excel(name = "历史指标标记名称", width = 15)
@ApiModelProperty(value = "历史指标标记名称")
private java.lang.String analyseTitle;
@Excel(name = "分析指标时间粒度", width = 15)
@ApiModelProperty(value = "分析指标时间粒度")
private java.lang.String analyseGranularity;
}
......
......@@ -54,7 +54,7 @@ public interface ICustomConsumeService {
* @return
* @author Li Yuanyuan, 2021年1月29日 下午3:10:42
*/
public List<TreeData> getDataType();
public List<TreeData> getDataType(String granularity);
/**
* <pre>
* 实时数据/水质水量
......@@ -89,10 +89,11 @@ public interface ICustomConsumeService {
* 报表数据项获取
* </pre>
* @param reportId
* @param granularity 时间粒度
* @return
* @author Li Yuanyuan, 2021年2月3日 下午3:39:44
*/
public List<Map<String, Object>> queryDataFillIndex(String reportId);
public List<Map<String, Object>> queryDataFillIndex(String reportId,String granularity);
}
......
......@@ -274,7 +274,7 @@ public class CustomConsumeServiceImpl implements ICustomConsumeService {
pattern = "yyyy-MM";
type = "year";
}
List<Map<String, Object>> list = reportItemService.getReportItemData(departId,reportId, startTime, endTime, indexs);
List<Map<String, Object>> list = reportItemService.getReportItemData(departId,reportId, startTime, endTime, indexs,granularity);
//获取部门数据
List<Map<String, Object>> handleList = new ArrayList<Map<String,Object>>();//接收处理结果
String[] factoryArray = departId.split(",");
......@@ -366,7 +366,7 @@ public class CustomConsumeServiceImpl implements ICustomConsumeService {
if(!StringUtils.isEmpty(contrastType)&&contrastType.contains("tb")) {
String tbStartTime = DateUtils.formatAddTime(startTime,pattern,Calendar.YEAR,-1);
String tbEndTime = DateUtils.formatAddTime(endTime,pattern,Calendar.YEAR,-1);
List<Map<String, Object>> tbList =reportItemService.getReportItemData(departId,reportId, tbStartTime, tbEndTime, indexs);
List<Map<String, Object>> tbList =reportItemService.getReportItemData(departId,reportId, tbStartTime, tbEndTime, indexs,granularity);
for(Map<String, Object> map : tbList) {
tbMap.put(ConvertUtils.getString(map.get("time")+""+map.get("departId")), map);
}
......@@ -374,7 +374,7 @@ public class CustomConsumeServiceImpl implements ICustomConsumeService {
if(!StringUtils.isEmpty(contrastType)&&contrastType.contains("hb")) {
String hbStartTime = DateUtils.formatAddTime(startTime,pattern,Calendar.MONTH,-1);
String hbEndTime = DateUtils.formatAddTime(endTime,pattern,Calendar.MONTH,-1);
List<Map<String, Object>> hbList =reportItemService.getReportItemData(departId,reportId, hbStartTime, hbEndTime, indexs);
List<Map<String, Object>> hbList =reportItemService.getReportItemData(departId,reportId, hbStartTime, hbEndTime, indexs,granularity);
for(Map<String, Object> map : hbList) {
hbMap.put(ConvertUtils.getString(map.get("time")+""+map.get("departId")), map);
}
......@@ -402,26 +402,31 @@ public class CustomConsumeServiceImpl implements ICustomConsumeService {
}
@Override
public List<TreeData> getDataType() {
public List<TreeData> getDataType(String granularity) {
List<Map<String,Object>> dataTypeList = new ArrayList<Map<String,Object>>();
Map<String,Object> ssmap = new HashMap<String,Object>();
ssmap.put("id", "ss");
ssmap.put("pid", "");
ssmap.put("title", "实时");
dataTypeList.add(ssmap);
Map<String,Object> szmap = new HashMap<String,Object>();
szmap.put("id", "sz");
szmap.put("pid", "ss");
szmap.put("title", "采集数据");
if("hour".equals(granularity)||"day".equals(granularity)) {
dataTypeList.add(ssmap);
dataTypeList.add(szmap);
}
Map<String,Object> tbmap = new HashMap<String,Object>();
tbmap.put("id", "tb");
tbmap.put("pid", "");
tbmap.put("title", "填报");
dataTypeList.add(szmap);
dataTypeList.add(tbmap);
if("day".equals(granularity)||"month".equals(granularity)) {
dataTypeList.add(tbmap);
}
QueryWrapper<FReportManage> fReportManageQueryWrapper = new QueryWrapper<>();
fReportManageQueryWrapper.eq("status","1");
fReportManageQueryWrapper.eq("pro_status","1");
fReportManageQueryWrapper.eq("time_type", granularity);
fReportManageQueryWrapper.orderByAsc("sort_num");
List<FReportManage> fReportManages = reportManageService.list(fReportManageQueryWrapper);
for (FReportManage fReportManage : fReportManages) {
......@@ -431,6 +436,12 @@ public class CustomConsumeServiceImpl implements ICustomConsumeService {
report.put("title", fReportManage.getReportName());
dataTypeList.add(report);
}
//填报追加公式计算字段
Map<String, Object> formulaReport = new HashMap<>();
formulaReport.put("id", "formula");
formulaReport.put("pid", "tb");
formulaReport.put("title", "填报计算");
dataTypeList.add(formulaReport);
//将业务数据转换为构造树所需的数据结构
List<TransTreeModel> transTreeModels = dataTypeList.stream().map(bo -> convertToTreeModel(bo)).collect(Collectors.toList());
List<TreeData> treeList=TreeUtils.buildTreeByRootId(transTreeModels, "");
......@@ -477,9 +488,14 @@ public class CustomConsumeServiceImpl implements ICustomConsumeService {
}
@Override
public List<Map<String, Object>> queryDataFillIndex(String reportId) {
public List<Map<String, Object>> queryDataFillIndex(String reportId,String granularity) {
JdbcTemplate masterDB = (JdbcTemplate)SpringContextUtils.getBean("master");
List<Map<String, Object>> list = masterDB.queryForList("select item_code as indexCode,item_alias as indexName,unit as indexUnit from f_report_item where report_id='"+reportId+"' and is_use_power_analyse='1' order by sort_num");
List<Map<String, Object>> list = null;
if("formula".equals(reportId)) {
list = masterDB.queryForList("select formula_code as indexCode,formula_name as indexName,formula_unit as indexUnit from f_report_itemv_formula where report_id='"+reportId+"' and formula_granularity='"+granularity+"' order by sort_num");
}else {
list = masterDB.queryForList("select item_code as indexCode,item_alias as indexName,unit as indexUnit from f_report_item where report_id='"+reportId+"' and is_use_power_analyse='1' order by sort_num");
}
return list;
}
}
......
......@@ -39,7 +39,7 @@ public interface IFReportItemService extends IService<FReportItem> {
* @return
* @author Li Yuanyuan, 2021年2月3日 下午5:03:47
*/
List<Map<String,Object>> getReportItemData(String departIds,String reportId,String startTime,String endTime,String index);
List<Map<String,Object>> getReportItemData(String departIds,String reportId,String startTime,String endTime,String index,String granularity);
Map<String, Object> getReportData(String reportId, String dataId);
......
package com.skua.modules.report.service.impl;
import com.skua.common.report.ReportViewUtil;
import com.skua.modules.report.entity.FReportItem;
import com.skua.modules.report.mapper.FReportItemMapper;
import com.skua.modules.report.service.IFReportItemService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.skua.common.report.CustomReportUtil;
import com.skua.common.report.ReportViewUtil;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.util.ConvertUtils;
import com.skua.modules.report.entity.FReportItem;
import com.skua.modules.report.mapper.FReportItemMapper;
import com.skua.modules.report.service.IFReportItemService;
/**
* 报表数据项管理
......@@ -96,35 +103,104 @@ public class FReportItemServiceImpl extends ServiceImpl<FReportItemMapper, FRepo
return list;
}
//fReportManageQueryWrapper.eq("status","1");
//fReportManageQueryWrapper.eq("pro_status","1");
//fReportManageQueryWrapper.eq("time_type", granularity);
@Override
public List<Map<String, Object>> getReportItemData(String departIds,String reportId, String startTime, String endTime,String indexs) {
public List<Map<String, Object>> getReportItemData(String departIds,String reportId, String startTime, String endTime,String indexs,String granularity) {
String[] indexArray = indexs.split(",");
//查询报表计算指标
JdbcTemplate masterDB = (JdbcTemplate)SpringContextUtils.getBean("master");
String formulaSql = "select formula_code,formula_info from f_report_itemv_formula where formula_granularity='"+granularity+"'";
List<Map<String, Object>> formulaList = masterDB.queryForList(formulaSql);
Map<String,String> formulaMap = new HashMap<String,String>();
for(Map<String,Object> map : formulaList) {
formulaMap.put(ConvertUtils.getString(map.get("formula_code")), ConvertUtils.getString(map.get("formula_info")));
}
//对计算指标进行解析,追加到基础查询指标
Set<String> indexSet = new LinkedHashSet<String>();//查询指标
//存在计算指标,重新构造基础指标集合
String selectPart = "";
String headSelectPart = "";
String formulaIds = "";
for(String index : indexArray) {
selectPart = selectPart+","+index;
headSelectPart = headSelectPart+", max(case when item_code = '"+index+"' then CONCAT(item_alias,'(',unit,')') else '' end) as '"+index+"'";
formulaIds = formulaIds+","+"'"+index+"'";
if(index.contains("formula_")) {
String formula = formulaMap.get(index);
selectPart = selectPart+",round("+formulaMap.get(index).replace("{", "IFNULL(").replace("}", ",0)")+",2) as "+index;
//提取计算指标
List<String> formulaIndexList = CustomReportUtil.getFormulaElement(formula);
for(String formulaIndex : formulaIndexList) {
indexSet.add(formulaIndex);
}
}else {
selectPart = selectPart+","+index;
indexSet.add(index);
}
}
if(!StringUtils.isEmpty(formulaIds)) {
selectPart = selectPart.substring(1);
headSelectPart = headSelectPart.substring(1);
formulaIds = formulaIds.substring(1);
indexs = String.join(",", indexSet);
//对基础查询指标按照报表进行分组
String queryDataFillFieldSql="select item_code,report_id from f_report_item where report_id in(select id from f_report_manage where status='1' and pro_status='1' and time_type='"+granularity+"')";
List<Map<String, Object>> baseDataFillFieldList = masterDB.queryForList(queryDataFillFieldSql);
Map<String,String> fieldToReportIdMap = new HashMap<String,String>();
for(Map<String,Object> map : baseDataFillFieldList) {
fieldToReportIdMap.put(ConvertUtils.getString(map.get("item_code")), ConvertUtils.getString(map.get("report_id")));
}
String headWhereInPart = "item_code IN("+formulaIds+")";
Map<String,String> paramMap = new HashMap<String,String>();
paramMap.put("viewName", ReportViewUtil.buildView(reportId, indexs, departIds, startTime, endTime));
//paramMap.put("viewName", "view_"+reportId.substring(0, 4));
paramMap.put("startTime", startTime);
paramMap.put("endTime", endTime);
paramMap.put("dataSelectPart", selectPart);
paramMap.put("headSelectPart", headSelectPart);
paramMap.put("headWhereInPart", headWhereInPart);
paramMap.put("reportId", reportId);
paramMap.put("departIds", departIds);
return fReportItemMapper.getReportItemData(paramMap);
String[] tempIndexArray = indexs.split(",");
Map<String,List<String>> reportIndexGroup = new HashMap<String,List<String>>();
for(String key : tempIndexArray) {
String tempReportId = fieldToReportIdMap.get(key);
List<String> reportIndexList = reportIndexGroup.get(tempReportId);
if(reportIndexList!=null) {
reportIndexList.add(key);
}else {
reportIndexList = new ArrayList<String>();
reportIndexList.add(key);
reportIndexGroup.put(tempReportId, reportIndexList);
}
}
//基于报表做基础指标查询
Set<String> reportIdSet = reportIndexGroup.keySet();
//构造日期机构主参照表动态视图
String baseViewPattern = "%Y-%m-%d";
String baseViewGranularity = "DAY";
String[] departIdArray = departIds.split(",");
String temp = "";
for(String tempDepartId : departIdArray) {
temp = temp+",'"+tempDepartId+"'";
}
if(StringUtils.isNotEmpty(temp)) {
temp = temp.substring(1);
}
String tempStartTime = startTime;
String tempEndTime = endTime;
if("month".equals(granularity)) {
baseViewPattern = "%Y-%m";
baseViewGranularity = "MONTH";
tempStartTime = startTime+"-01";
tempEndTime = endTime+"-01";
}
String baseViewName= "(SELECT a.time,b.id as depart_id,b.depart_name FROM (\r\n" +
"SELECT \r\n" +
" DATE_FORMAT(DATE_ADD('"+tempStartTime+"', INTERVAL t.help_topic_id "+baseViewGranularity+"), '"+baseViewPattern+"') AS time\r\n" +
"FROM \r\n" +
" mysql.help_topic t \r\n" +
"WHERE \r\n" +
" t.help_topic_id <= TIMESTAMPDIFF("+baseViewGranularity+", '"+tempStartTime+"', '"+tempEndTime+"')\r\n" +
") a left join (select * from sys_depart where id in("+Arrays.stream(departIds.split(",")).map(id -> "'" + id + "'").collect(Collectors.joining(","))+")) b on 1=1)";
int tag=1;
String sql = "select aa.time,aa.depart_id as departId,aa.depart_name as departName"+selectPart+" from "+baseViewName+" aa ";
for(String tempReportId : reportIdSet) {
String viewName = "view"+tag;
List<String> indexList = reportIndexGroup.get(tempReportId);
sql = sql +"left join "+ReportViewUtil.buildView(tempReportId, String.join(",", indexList), departIds, startTime, endTime) +viewName+ " on aa.time = "+viewName+".time and aa.depart_id = "+viewName+".depart_id ";
tag++;
}
System.out.println("======>>>>"+sql);
return masterDB.queryForList(sql);
}
public static void main(String[] args) {
String ids = "1,2,3,4";
String str = Arrays.stream(ids.split(",")).map(id -> "'" + id + "'").collect(Collectors.joining(","));
System.out.println(str);
}
@Override
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!