package com.yssh.service.impl; import com.yssh.dao.DictRecordMapper; import com.yssh.dao.Yssh2dreliMapper; import com.yssh.entity.DictRecord; import com.yssh.entity.Yssh2dreli; import com.yssh.service.Yssh2dreliService; import lombok.Synchronized; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * @author lishijia * @ClassName Yssh2dreliServiceImpl * @Description TODO * @date 2022/12/3 21:25 * @Version 1.0 */ @Service public class Yssh2dreliServiceImpl implements Yssh2dreliService { @Autowired private Yssh2dreliMapper yssh2dreliMapper; @Autowired private DictRecordMapper dictRecordMapper; @Override public List getAll() { return yssh2dreliMapper.getAll(); } /** * 创建2维热力表接口 */ @Transactional(rollbackFor = Exception.class) @Synchronized @Override public int create2dreliTable() { String tablePrefix = "yssh_2dreli_"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); Calendar calendar1 = Calendar.getInstance(); //明天 calendar1.add(Calendar.DATE, +1); Date start = calendar1.getTime(); String tomorrow = dateFormat.format(start); String tableName = tablePrefix + tomorrow; System.out.println("tableName = " + tableName); //把新增的表增加到字典表中 DictRecord dictRecord = new DictRecord(); dictRecord.setTableName(tableName); dictRecord.setResOne("2"); dictRecord.setResTwo(tomorrow); dictRecord.setCreateTime(dateFormat("yyyy-MM-dd HH:mm:ss")); dictRecordMapper.insertDictRecord(dictRecord); return yssh2dreliMapper.create2dreliTable(tableName); } /** * 创建当天2维热力表接口 */ @Transactional(rollbackFor = Exception.class) @Synchronized @Override public int preReate2dreliTable() { String tablePrefix = "yssh_2dreli_"; Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); String localDday = dateFormat.format(date); String tableName = tablePrefix + localDday; int total = yssh2dreliMapper.create2dreliTable(tableName); //把新增的表增加到字典表中 DictRecord dictRecord = new DictRecord(); dictRecord.setTableName(tableName); dictRecord.setResOne("2"); dictRecord.setResTwo(localDday); dictRecord.setCreateTime(dateFormat("yyyy-MM-dd HH:mm:ss")); dictRecordMapper.insertDictRecord(dictRecord); return total; } /** * 获取LocalDateTime的指定日期格式 * * @param ofPattern 设置时间格式:yyyy-MM-dd HH:mm:ss * @return 2023-02-09 09:51:43 */ public String dateFormat(String ofPattern) { return LocalDateTime.now().format(DateTimeFormatter.ofPattern(ofPattern)); } /** * 删除2维热力表接口 */ @Transactional(rollbackFor = Exception.class) @Synchronized @Override public int drop2dreliTable() { String tablePrefix = "yssh_2dreli_"; //定义日期格式 SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd"); Calendar calendar = Calendar.getInstance(); //前一年 calendar.add(Calendar.YEAR, -1); Date start = calendar.getTime(); String yearBefore = format2.format(start); String tableName = tablePrefix + yearBefore; return yssh2dreliMapper.drop2dreliTable(tableName); } /** * 查询接口 */ @Override public List query2data(Yssh2dreli yssh2dreli, String tableName) { List resultList = yssh2dreliMapper.query2data(yssh2dreli.getTime(), tableName); return resultList; } /** * 查询2维热力表接口 * 1、根据time字段传入的开始时间和结束时间 * 2、根据时间查询记录表,获取数据表名 * 3、查询出结果、合并结果并返回结果 * ①time为同一天的不同时间 * ②time为不同一天的范围时间 */ @Override public List twoDDataQuery(String startTime, String endOfTime) { String tablePrefix = "yssh_2dreli_"; String tableName = ""; System.out.println("startTime = " + startTime); System.out.println("endOfTime = " + endOfTime); String strTime = startTime.substring(0, 8); String endTime = endOfTime.substring(0, 8); List resultList = new ArrayList<>(); //根据查询时间判断查询的数据表 //①同一个表范围查询 if (strTime.equals(endTime)) { tableName = tablePrefix + strTime; System.out.println("tableName = " + tableName); resultList = yssh2dreliMapper.twoDDataQuery1(tableName, startTime, endOfTime); } else { //②不在同一个表查询 List list1 = new ArrayList<>(); tableName = tablePrefix + strTime; list1 = yssh2dreliMapper.twoDDataQuery2(tableName, startTime); List list2 = new ArrayList<>(); tableName = tablePrefix + endTime; list2 = yssh2dreliMapper.twoDDataQuery3(tableName, endOfTime); //合并结果 resultList.addAll(list1); resultList.addAll(list2); } return resultList; } /** * 查询2维热力表接口 * 1、根据time字段传入的开始时间和结束时间 * 2、根据时间查询记录表,获取数据表名 * 3、查询出结果、合并结果并返回结果 * ①time为同一天的不同时间 * ②time为不同一天的范围时间 */ @Override public List qubyt(String startTime, String endOfTime) { String tablePrefix = "yssh_2dreli_"; String tableName = ""; System.out.println("startTime = " + startTime); System.out.println("endOfTime = " + endOfTime); String strTime = startTime.substring(0, 8); String endTime = endOfTime.substring(0, 8); List resultList = new ArrayList<>(); //根据查询时间判断查询的数据表 //①同一个表范围查询 if (strTime.equals(endTime)) { tableName = tablePrefix + strTime; System.out.println("tableName = " + tableName); resultList = yssh2dreliMapper.twoDDataQuery1(tableName, startTime, endOfTime); return resultList; } else { //②不在同一个表查询 创建一个返回的list List lists = new ArrayList<>(); //去字典记录表中查询出需要查询的表名 List resDictRecords = dictRecordMapper.selectDictRecordList2dByTime(strTime, endTime); for (DictRecord dicts : resDictRecords) { System.out.println("dicts = " + dicts.getTableName()); lists.addAll(yssh2dreliMapper.twoDDataQuery(dicts.getTableName()));//将返回的结果添加到list中 } return lists; } } }