5700fca7 康伟

kangwei:

    1 设备运行时,补全日期、月份数据
1 个父辈 f79698db
package com.skua.modules.common.service;
import java.util.List;
import java.util.Map;
/**
* @auther kangwei
* @create 2024-11-19-12:09
*/
public interface IPgCommonService {
List<Map<String, Object>> queryForList(String sql);
Map<String, Object> queryForMap(String sql);
}
package com.skua.modules.common.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.skua.modules.common.service.IPgCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @auther kangwei
* @create 2024-11-19-12:10
*/
@Service
@DS("pg-db")
public class PgCommonServiceImpl implements IPgCommonService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Map<String, Object>> queryForList(String sql) {
return jdbcTemplate.queryForList(sql);
}
@Override
public Map<String, Object> queryForMap(String sql) {
return jdbcTemplate.queryForMap(sql);
}
}
......@@ -8,7 +8,7 @@ import com.skua.core.context.SpringContextUtils;
import com.skua.core.service.IFactoryInfoService;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.DateUtils;
import com.skua.modules.equipment.vo.EquipmentRealTimeVO;
import com.skua.modules.common.service.IPgCommonService;
import com.skua.modules.erp.vo.MetricInfoVO;
import com.skua.modules.erp.vo.RealTimeHistoryDataVO;
import com.skua.modules.guest.util.DateUtil;
......@@ -26,8 +26,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;
@Api(tags="设备-设备运行台时报表")
......@@ -40,15 +42,20 @@ public class TestMetricInfoController {
@Autowired
private IFactoryInfoService iFactoryInfoService;
@Autowired
private IPgCommonService pgCommonService;
@AutoLog(value = "设备-设备运行台时报表")
@ApiOperation(value = "设备-设备运行台时报表(timeType:1 当日数据,=2表示月数据;=3表示年数据)", notes = "设备-设备运行台时报表")
@GetMapping(value = "/realTimeHistoryData")
public Result<RealTimeHistoryDataVO> equipRealTimeHistoryDataCtrl(@RequestParam(value = "departId") String departId,
@RequestParam(value = "startTime") String startTime,
@RequestParam(value = "endTime") String endTime,
@RequestParam(value = "timeType",defaultValue = "1")String timeType) {
@RequestParam(value = "time") String time,@RequestParam(value = "timeType",defaultValue = "1")String timeType,
@RequestParam(value = "startTime") String startTime, @RequestParam(value = "endTime") String endTime) {
if("3".equals(timeType)){
startTime = time+"-01-01";
endTime = time+"-12-31";
}
if(StringUtils.isEmpty(startTime)){
startTime = DateUtil.getCurrentDate() ;
}
......@@ -61,6 +68,7 @@ public class TestMetricInfoController {
if(endTime != null && endTime.length() < 19){
endTime = endTime+" 23:59:59";
}
Result<RealTimeHistoryDataVO> result = new Result<>();
RealTimeHistoryDataVO realTimeHistoryDataVO = new RealTimeHistoryDataVO();
//String fields = "TCSWSCLC_PLC300_Tag182,TCSWSCLC_PLC300_Tag185";
......@@ -94,113 +102,132 @@ public class TestMetricInfoController {
fields = fields.substring(0, fields.length() - 1);
}
List<Map<String, Object>> pgDataList = queryFactoryInfos(departId, fields, startTime, endTime, timeType);
List<Map<String, Object>> dataList = null;
if (timeType.equals("1")){//获取时数据
dataList = getHourData(metricInfoVOList ,pgDataList);
}else{//获取日数据或月数据
dataList = getDaysAndMonthsData(metricInfoVOList ,pgDataList,startTime, endTime,timeType);
}
realTimeHistoryDataVO.setHeadList( metricInfoVOList);
realTimeHistoryDataVO.setDataList( dataList );
result.setResult( realTimeHistoryDataVO);
return result;
}
private List<Map<String, Object>> getHourData( List<MetricInfoVO> metricInfoVOList , List<Map<String, Object>> pgDataList){
List<Map<String, Object>> dataList = new ArrayList<>();
if(pgDataList != null ) {
dataList = new ArrayList<>(pgDataList.size());
Map<String, Object> dataMap = null;
//elect ts as time,nm as monitorId,v as monitorValue
String equipmentCode = null;
//select ts as time,nm as monitorId,v as monitorValue
for (Map<String, Object> paramMap : pgDataList) {
dataMap = new HashMap<>();
dataMap.put("time",paramMap.get("time") );
if(timeType.equals( "1")){
dataMap.put("time",DateUtils.formatTime(Long.parseLong(paramMap.get("time") + "000")) );
}
dataMap.put("time",DateUtils.formatTime(Long.parseLong(paramMap.get("time") + "000")) );
for(MetricInfoVO metricInfoVO : metricInfoVOList){
dataMap.put( metricInfoVO.getEquipmentCode()+"_YX" , JSUtils.format(paramMap.get(metricInfoVO.getYxExpress()),"0.00"));//运行
dataMap.put( metricInfoVO.getEquipmentCode()+"_GZ" , JSUtils.format(paramMap.get(metricInfoVO.getGzExpress()),"0.00"));//故障
//时数据
if (timeType.equals("1")) dataMap.put( metricInfoVO.getEquipmentCode()+"_ZT" ,JSUtils.subtract( 60, paramMap.get(metricInfoVO.getYxExpress()),paramMap.get(metricInfoVO.getGzExpress()) ) );//暂停
//日数据
if (timeType.equals("2")) dataMap.put( metricInfoVO.getEquipmentCode()+"_ZT" ,JSUtils.subtract( 24*60, paramMap.get(metricInfoVO.getYxExpress()),paramMap.get(metricInfoVO.getGzExpress()) ) );//暂停
//月数据
if (timeType.equals("3")) dataMap.put( metricInfoVO.getEquipmentCode()+"_ZT" ,JSUtils.subtract( 30*24*60, paramMap.get(metricInfoVO.getYxExpress()),paramMap.get(metricInfoVO.getGzExpress()) ) );//暂停
dataMap.put( metricInfoVO.getEquipmentCode()+"_ZT" ,JSUtils.subtract( 60, paramMap.get(metricInfoVO.getYxExpress()),paramMap.get(metricInfoVO.getGzExpress()) ) );//暂停
}
dataList.add( dataMap );
}
}
/* //拼装数据
Map<String, Map<String,String>> linkedHashMap = new LinkedHashMap<String, Map<String,String>>();//返回数据结果
String timeTemp = null;
Map<String,String> dataMap = null;
if(pgDataList != null ){
//elect ts as time,nm as monitorId,v as monitorValue
for( Map<String, Object> paramMap : pgDataList){
timeTemp = DateUtils.formatTime(Long.parseLong(paramMap.get("time") + "000"));
dataMap = linkedHashMap.get(timeTemp);
if(dataMap == null ){
dataMap = new HashMap<>();
dataMap.put("time",timeTemp);
for (Map.Entry<String, String> entry : nameMap.entrySet()) {
dataMap.put(entry.getKey(),"0");
}
}
// 使用For-Each迭代entries,通过Map.entrySet遍历key和value
for (Map.Entry<String, String> entry : nameMap.entrySet()) {
System.out.println("entry.getKey()" +entry.getKey()+ "****"+"paramMap.get() = "+paramMap.get(entry.getKey()));
if(entry.getKey().equals( ConvertUtils.getString(paramMap.get(entry.getKey())) )){
dataMap.put( entry.getValue() , ConvertUtils.getString(paramMap.get(entry.getKey())));
return dataList;
}
private List<Map<String, Object>> getDaysAndMonthsData( List<MetricInfoVO> metricInfoVOList , List<Map<String, Object>> pgDataList,String startTime, String endTime,String timeType){
List<Map<String, Object>> dataList = new ArrayList<>();
List<String> timeList = null;
Map<String, Object> dataMap = null;//数据变量
//日数据
if (timeType.equals("2")){
timeList = DateUtil.getDaysBetween(startTime, endTime,"yyyy-MM-dd HH:mm:ss");
}
//月数据
if (timeType.equals("3")){
timeList = DateUtil.getMonthsBetween(startTime, endTime,"yyyy-MM-dd HH:mm:ss");
}
for(String date : timeList){
dataMap = new HashMap<>();
dataMap.put("time",date);
for(MetricInfoVO metricInfoVO : metricInfoVOList){
dataMap.put( metricInfoVO.getEquipmentCode()+"_YX" , "0.00");//运行
dataMap.put( metricInfoVO.getEquipmentCode()+"_GZ" , "0.00");//故障
dataMap.put( metricInfoVO.getEquipmentCode()+"_ZT" ,"0.00" );//暂停
}
dataList.add(dataMap);
}
if(pgDataList != null ) {
for(Map<String, Object> dataMapTemp : dataList){
//select ts as time,nm as monitorId,v as monitorValue
for (Map<String, Object> paramMap : pgDataList) {
if( dataMapTemp.get("time").equals( paramMap.get("time") ) ){
for(MetricInfoVO metricInfoVO : metricInfoVOList){
dataMapTemp.put( metricInfoVO.getEquipmentCode()+"_YX" , JSUtils.format(paramMap.get(metricInfoVO.getYxExpress()),"0.00"));//运行
dataMapTemp.put( metricInfoVO.getEquipmentCode()+"_GZ" , JSUtils.format(paramMap.get(metricInfoVO.getGzExpress()),"0.00"));//故障
//时数据
if (timeType.equals("1")) dataMapTemp.put( metricInfoVO.getEquipmentCode()+"_ZT" ,JSUtils.subtract( 60, paramMap.get(metricInfoVO.getYxExpress()),paramMap.get(metricInfoVO.getGzExpress()) ) );//暂停
//日数据
if (timeType.equals("2")) dataMapTemp.put( metricInfoVO.getEquipmentCode()+"_ZT" ,JSUtils.subtract( 24*60, paramMap.get(metricInfoVO.getYxExpress()),paramMap.get(metricInfoVO.getGzExpress()) ) );//暂停
//月数据
if (timeType.equals("3")) dataMapTemp.put( metricInfoVO.getEquipmentCode()+"_ZT" ,JSUtils.subtract( 30*24*60, paramMap.get(metricInfoVO.getYxExpress()),paramMap.get(metricInfoVO.getGzExpress()) ) );//暂停
}
break;
}
}
linkedHashMap.put(timeTemp,dataMap);
}
}*/
realTimeHistoryDataVO.setHeadList( metricInfoVOList);
realTimeHistoryDataVO.setDataList( dataList );
result.setResult( realTimeHistoryDataVO);
return result;
}
return dataList;
}
public List<Map<String, Object>> queryFactoryInfos(String departId, String fields, String startTime, String endTime, String tableType) {
public List<Map<String, Object>> queryFactoryInfos(String departId, String fields, String startTime, String endTime, String timeType) {
String fieldSqlPart = "";
String[] fieldsArr = fields.split(",");
for(int i = 0; i < fieldsArr.length; ++i) {
fieldSqlPart = fieldSqlPart + ",'" + fieldsArr[i] + "'";
}
if (!StringUtils.isEmpty(fieldSqlPart)) {
fieldSqlPart = fieldSqlPart.substring(1);
}
JdbcTemplate pgDB = (JdbcTemplate)SpringContextUtils.getBean("pg-db");
JdbcTemplate masterDB = (JdbcTemplate)SpringContextUtils.getBean("master");
List<Map<String, Object>> result = new ArrayList();
List<Map<String, Object>> factoryDesign = masterDB.queryForList("select * from sys_factory_device where depart_id = '" + departId + "'");
if (factoryDesign != null && factoryDesign.size() != 0) {
String countTableName = ConvertUtils.getString(((Map)factoryDesign.get(0)).get("device_id"));
String sql = null;
//时数据
if (tableType.equals("1")) {
sql = "select ts as time,nm as monitorId,v as monitorValue from " + countTableName + "_count where nm in(" + fieldSqlPart + ") and ts>=? and ts <? ";
}
//日数据
if (tableType.equals("2")) {
sql = "select aaa.date as time , aaa.monitorId ,SUM( to_number ( aaa.monitorValue, '9999999999999999.99' ) )as monitorValue from (";
sql += "select to_char(to_timestamp(ts::INT8)::DATE,'YYYY-MM-DD') as date, ts as time,nm as monitorId,v as monitorValue from " + countTableName + "_count where nm in(" + fieldSqlPart + ") and ts>=? and ts <? ";
sql += " )aaa group by aaa.date ,aaa.monitorId order by aaa.date asc";
}
//月数据
if (tableType.equals("3")) {
sql = "select aaa.date as time , aaa.monitorId ,SUM( to_number ( aaa.monitorValue, '9999999999999999.99' ) )as monitorValue from (";
sql += "select to_char(to_timestamp(ts::INT8)::DATE,'YYYY-MM') as date, ts as time,nm as monitorId,v as monitorValue from " + countTableName + "_count where nm in(" + fieldSqlPart + ") and ts>=? and ts <? ";
sql += " )aaa group by aaa.date ,aaa.monitorId order by aaa.date asc";
}
try {
String tsStartTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm:ss")) / 1000L);
String tsEndTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss")) / 1000L);
List dataList = null;
//时数据
if (timeType.equals("1")) {
sql = "select ts as time,nm as monitorId,v as monitorValue from " + countTableName + "_count where nm in(" + fieldSqlPart + ") and ts>='"+tsStartTime+"' and ts <'"+tsEndTime+"' ";
}
//日数据
if (timeType.equals("2")) {
sql = "select aaa.date as time , aaa.monitorId ,SUM( to_number ( aaa.monitorValue, '9999999999999999.99' ) )as monitorValue from (";
sql += "select to_char(to_timestamp(ts::INT8)::DATE,'YYYY-MM-DD') as date, ts as time,nm as monitorId,v as monitorValue from " + countTableName + "_count where nm in(" + fieldSqlPart + ") and ts>='"+tsStartTime+"' and ts <'"+tsEndTime+"' ";
sql += " )aaa group by aaa.date ,aaa.monitorId order by aaa.date asc";
}
//月数据
if (timeType.equals("3")) {
sql = "select aaa.date as time , aaa.monitorId ,SUM( to_number ( aaa.monitorValue, '9999999999999999.99' ) )as monitorValue from (";
sql += "select to_char(to_timestamp(ts::INT8)::DATE,'YYYY-MM') as date, ts as time,nm as monitorId,v as monitorValue from " + countTableName + "_count where nm in(" + fieldSqlPart + ") and ts>='"+tsStartTime+"' and ts <'"+tsEndTime+"' ";
sql += " )aaa group by aaa.date ,aaa.monitorId order by aaa.date asc";
}
List<Map<String, Object>> dataList = null;
try {
dataList = pgDB.queryForList(sql, new Object[]{tsStartTime, tsEndTime});
// List<Map<String, Object>> pgDataList = iFactoryInfoService.queryFactoryInfos(departId, fields, startTime, endTime, "1");
dataList = pgCommonService.queryForList(sql);
} catch (DataAccessException var23) {
Object pgDbSlaveObject = SpringContextUtils.getBean("pg-db-slave");
Object pgDbSlaveObject = SpringContextUtils.getBean("pg-db-slave");
if (pgDbSlaveObject != null) {
pgDB = (JdbcTemplate)pgDbSlaveObject;
dataList = pgDB.queryForList(sql, new Object[]{tsStartTime, tsEndTime});
JdbcTemplate pgDB = (JdbcTemplate)pgDbSlaveObject;
dataList = pgDB.queryForList(sql);
}
var23.printStackTrace();
}
this.dataRowToCellByTime(result, dataList);
} catch (Exception var24) {
......@@ -216,7 +243,6 @@ public class TestMetricInfoController {
List<String> orderTsList = new ArrayList();
Map<String, Map<String, Object>> resultMap = new HashMap();
Iterator var5 = dataList.iterator();
while(true) {
while(var5.hasNext()) {
Map<String, Object> tempMap = (Map)var5.next();
......
......@@ -2,11 +2,9 @@ package com.skua.modules.guest.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Stream;
......@@ -97,6 +95,62 @@ public class DateUtil {
}
/***
* 获取两个时间的日期
* @param start
* @param end
* @return
*/
public static List<String> getDaysBetween(String start, String end,String format) {
List<String> days = new ArrayList<>();
if(format == null ){
format = "yyyy-MM-dd";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
// 解析日期
LocalDate startDate = LocalDate.parse(start, formatter);
LocalDate endDate = LocalDate.parse(end, formatter);
LocalDate currentDate = LocalDate.now();//创建一个LocalData对象获取当前日期
if(endDate.isAfter( currentDate)){
endDate = currentDate;
}
long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
for (int i = 0; i <= numOfDaysBetween; i++) {
LocalDate date = startDate.plusDays(i);
days.add(date.toString()); // 格式化为 yyyy-MM
}
return days;
}
/***
* 获取两个时间的月份
* @param start
* @param end
* @return
*/
public static List<String> getMonthsBetween(String start, String end,String format) {
if(format == null ){
format = "yyyy-MM-dd";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
// 解析日期
LocalDate startDate = LocalDate.parse(start, formatter);
LocalDate endDate = LocalDate.parse(end, formatter);
LocalDate currentDate = LocalDate.now();//创建一个LocalData对象获取当前日期
if(endDate.isAfter( currentDate)){
endDate = currentDate;
}
List<String> months = new ArrayList<>();
// 获取起始和结束月份
YearMonth startMonth = YearMonth.from(startDate);
YearMonth endMonth = YearMonth.from(endDate);
// 遍历月份并添加到列表
YearMonth currentMonth = startMonth;
while (!currentMonth.isAfter(endMonth)) {
months.add(currentMonth.toString()); // 格式化为 yyyy-MM
currentMonth = currentMonth.plusMonths(1);
}
return months;
}
/**
* 获取两个日期之间的所有日期 (年月日)
......@@ -123,10 +177,9 @@ public class DateUtil {
if(currentDate.getTime() < endDate.getTime() ){
endDate = currentDate;
}
//用Calendar 进行日期比较判断
Calendar calendar = Calendar.getInstance();
while (startDate.getTime() < endDate.getTime()){
while (startDate.getTime() <= endDate.getTime()){
// 把日期添加到集合
list.add(formatSDF.format(startDate));
// 设置日期
......
......@@ -121,4 +121,15 @@ public class MaterialInfo {
@Excel(name = "修订过程", width = 15)
@ApiModelProperty(value = "修订过程")
private String updateHistory;
@ApiModelProperty(value = "文件号")
private String fileCode;
@ApiModelProperty(value = "使用范围")
private String useScope;
@ApiModelProperty(value = "审核状态:0 未发布 1 已发布(待审核)2 审核驳回 3 审核通过")
@Dict(dicCode = "purchase_plan_status")
private String status;
}
......
......@@ -322,4 +322,14 @@ public class ScreenDataServiceImpl implements IScreenDataService {
return months;
}
public static void main(String[] args) {
String startTime = "2024-01-01";//
String endTime = "2024-12-31";
String format ="yyyy-MM-dd";
List<String> dateList = getMonthsBetween(startTime, endTime);
for(String date : dateList){
System.out.println("date:"+date);
}
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!