package com.yssh.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.yssh.dao.DictRecordMapper; import com.yssh.dao.Yssh3dreliMapper; import com.yssh.entity.DictRecord; import com.yssh.entity.Yssh2dreli; import com.yssh.entity.Yssh3dreli; import com.yssh.service.Yssh3dreliService; import com.yssh.utils.StringUtils; import lombok.Synchronized; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.ParseException; 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 Yssh3dreliServiceImpl * @Description TODO * @date 2022/12/3 21:25 * @Version 1.0 */ @Service public class Yssh3dreliServiceImpl extends ServiceImpl implements Yssh3dreliService { @Autowired private Yssh3dreliMapper mapper; @Autowired private DictRecordMapper dictRecordMapper; @Override public List getAll() { return mapper.getAll(); } @Override public List query(String name) { return mapper.query(name); } /** * 创建3维热力表接口 */ @Transactional(rollbackFor = Exception.class) @Synchronized @Override public int create3dreliTable() { String tablePrefix = "yssh_3dreli_"; // Date date = new Date(); // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); // String localDday = dateFormat.format(date); // System.out.println("localDday = " + localDday); // String tableName = tablePrefix+localDday; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); Calendar calendar = Calendar.getInstance(); //明天 calendar.add(Calendar.DATE, +1); Date start = calendar.getTime(); String tomorrow = dateFormat.format(start); String tableName = tablePrefix + tomorrow; System.out.println("tableName = " + tableName); //把新增的表增加到字典表中 DictRecord dictRecord = new DictRecord(); dictRecord.setTableName(tableName); dictRecord.setResOne("3"); dictRecord.setResTwo(tomorrow); dictRecord.setCreateTime(dateFormat("yyyy-MM-dd HH:mm:ss")); dictRecordMapper.insertDictRecord(dictRecord); return mapper.create3dreliTable(tableName); } /** * 创建当天3维热力表接口 */ @Transactional(rollbackFor = Exception.class) @Synchronized @Override public int preRreate3dreliTable() { String tablePrefix = "yssh_3dreli_"; Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); String localDday = dateFormat.format(date); System.out.println("localDday = " + localDday); String tableName = tablePrefix + localDday; //把新增的表增加到字典表中 DictRecord dictRecord = new DictRecord(); dictRecord.setTableName(tableName); dictRecord.setResOne("3"); dictRecord.setResTwo(localDday); dictRecord.setCreateTime(dateFormat("yyyy-MM-dd HH:mm:ss")); dictRecordMapper.insertDictRecord(dictRecord); return mapper.create3dreliTable(tableName); } /** * 删除3维热力表接口 */ @Transactional(rollbackFor = Exception.class) @Synchronized @Override public int drop3dreliTable() { String tablePrefix = "yssh_3dreli_"; //定义日期格式 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 mapper.drop3dreliTable(tableName); } /** * 批量新增3维热力记录 * * @param yssh3dreli 3维热力记录 * @return 结果 */ @Transactional(rollbackFor = Exception.class) @Synchronized @Override public int batchInsert3dreliData(List yssh3dreli) { /*Yssh3dreli yssh3dreli1 =new Yssh3dreli(); for (Yssh3dreli yssh3dre2: yssh3dreli) { System.out.println("yssh3dre2 = " + yssh3dre2.getName()); yssh3dreli1.setName(yssh3dre2.getName()); yssh3dreli1.setLat(yssh3dre2.getLat()); yssh3dreli1.setLon(yssh3dre2.getLon()); yssh3dreli1.setHeight0(yssh3dre2.getHeight0()); yssh3dreli1.setHeight10(yssh3dre2.getHeight10()); yssh3dreli1.setHeight20(yssh3dre2.getHeight20()); yssh3dreli1.setHeight30(yssh3dre2.getHeight30()); yssh3dreli1.setHeight40(yssh3dre2.getHeight40()); yssh3dreli1.setHeight50(yssh3dre2.getHeight50()); yssh3dreli1.setHeight60(yssh3dre2.getHeight60()); yssh3dreli1.setHeight70(yssh3dre2.getHeight70()); yssh3dreli1.setHeight80(yssh3dre2.getHeight80()); yssh3dreli1.setHeight90(yssh3dre2.getHeight90()); yssh3dreli1.setTime(yssh3dre2.getTime()); }*/ String tablePrefix = "yssh_3dreli_"; Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); String localDday = dateFormat.format(date); String tableName = tablePrefix + localDday; return mapper.batchInsert3dreliData(tableName, yssh3dreli); } /** * 批量新增2维热力记录 * * @param yssh2drelis 2维热力记录 * @return 结果 */ @Transactional(rollbackFor = Exception.class) @Synchronized @Override public int batchInsert2dreliData(List yssh2drelis) { String tablePrefix = "yssh_2dreli_"; Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); String localDday = dateFormat.format(date); String tableName = tablePrefix + localDday; return mapper.batchInsert2dreliData(tableName, yssh2drelis); } /** * 查询接口 */ @Override public List query3data(Yssh3dreli yssh3dreli, String tableName) { List resultList = mapper.query3data(yssh3dreli.getTime(), tableName); return resultList; } /** * 查询3维热力表接口 * 1、根据time字段传入的开始时间和结束时间 * 2、根据时间查询记录表,获取数据表名 * 3、查询出结果、合并结果并返回结果 * ①time为同一天的不同时间 * ②time为不同一天的范围时间 */ @Override public List thrDDataQuery(String startTime, String endOfTime) { String tablePrefix = "yssh_3dreli_"; 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 = mapper.thrDDataQuery1(tableName, startTime, endOfTime); } else { //②不在同一个表查询 List list1 = new ArrayList<>(); tableName = tablePrefix + strTime; list1 = mapper.thrDDataQuery2(tableName, startTime); List list2 = new ArrayList<>(); tableName = tablePrefix + endTime; list2 = mapper.thrDDataQuery3(tableName, endOfTime); //合并结果 resultList.addAll(list1); resultList.addAll(list2); } return resultList; } /** * 查询3维热力表接口 * 1、根据time字段传入的开始时间和结束时间 * 2、根据时间查询记录表,获取数据表名 * 3、查询出结果、合并结果并返回结果 * ①time为同一天的不同时间 * ②time为不同一天的范围时间 */ @Override public List qubyt(String name, String startTime, String endOfTime) throws ParseException { String tablePrefix = "yssh_3dreli_"; 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 = mapper.thrDDataQuery11(name, tableName, startTime, endOfTime); return resultList; } else { //②不在同一个表查询 创建一个返回的list List lists = new ArrayList<>(); //去字典记录表中查询出需要查询的表名 List resDictRecords = dictRecordMapper.selectDictRecordList3dByTime(strTime, endTime); for (DictRecord dicts : resDictRecords) { System.out.println("dicts = " + dicts.getTableName()); lists.addAll(mapper.thrDDataQuery01(name, dicts.getTableName()));//将返回的结果添加到list中 } return lists; } } /** * 将字符串格式转日期,如:yyyy-MM-dd ||自定义格式 * * @param date 日期字符串 * @param dateFormat 设置将字符串格式转日期格式,这个与date的格式必须一致 * @param tarFormat 设置目标格式 * @return 返回格式化的日期,默认格式:yyyy-MM-dd * @throws ParseException 分析时意外地出现了错误异常 */ public String strToDateFormat(String date, String dateFormat, String tarFormat) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(StringUtils.isBlank(dateFormat) ? "yyyyMMdd" : dateFormat); formatter.setLenient(false); Date newDate = formatter.parse(date); formatter = new SimpleDateFormat(StringUtils.isBlank(tarFormat) ? "yyyy-MM-dd" : tarFormat); return formatter.format(newDate); } /** * 获取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)); } /** * JAVA计算两个日期相差多少天 * * @param date1str * @param date2str * @return */ public int daysBetween(String date1str, String date2str) throws ParseException { int count = 0; if ("".equals(date1str) || date1str == null || "".equals(date2str) || date2str == null) { return count; } SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date date1 = format.parse(date1str); Date date2 = format.parse(date2str); count = Math.abs((int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24))); return count; /*System.out.println("当前时间:" + dateFormat("yyyy-MM-dd HH:mm:ss")); String date1 = strToDateFormat(startTime, "yyyyMMddHH", "yyyy-MM-dd HH:mm:ss"); String date2 = strToDateFormat(endOfTime, "yyyyMMddHH", "yyyy-MM-dd HH:mm:ss"); //String转Date Date date11 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date1); Date date22 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date2); // 获取相差的天数 Calendar calendar = Calendar.getInstance(); calendar.setTime(date11); long timeInMillis1 = calendar.getTimeInMillis(); calendar.setTime(date22); long timeInMillis2 = calendar.getTimeInMillis(); long betweenDays = ((timeInMillis2 - timeInMillis1) / (1000L * 3600L * 24L)) + 2; System.out.println("betweenDays = " + betweenDays);*/ } }