package com.yssh.service; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import com.yssh.entity.*; import com.yssh.mapper.*; import com.yssh.utils.CalculateUtils; import com.yssh.utils.DateUtils; import com.yssh.utils.StringUtils; import com.yssh.utils.TableStrategy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import com.google.common.collect.Lists; import javax.annotation.Resource; @Service public class SuYuanService { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource private SuYuanMapper suYuanMapper; @Resource private AsyncService asyncService; @Resource private DictRecordMapper dictRecordMapper; @Resource private CommonService commonService; @Resource private FeedbackMapper feedbackMapper; @Resource private QxshMapper qxshMapper; @Resource private LocationMapper locationMapper; @Resource private LocationService locService; @Resource private AlertConfigMapper alertConfigMapper; private static HashMap sites = new HashMap<>(); public double getJcyj() { return alertConfigMapper.getAll().get(0).getJcyj(); } //@Transactional @Async("threadPoolTaskExecutor") public void insertSuYuanDatasAsync(List lists, String time) throws Exception { //插入数据 List> list = Lists.partition(lists, AsyncService.BATCH_INSERT_NUMBER); CountDownLatch countDownLatch = new CountDownLatch(list.size()); for (List corpReserveList : list) { asyncService.executeAsync(TableStrategy.getTableStrategy(time), corpReserveList, suYuanMapper, countDownLatch); } countDownLatch.await(); logger.info(lists.size() + "条数据入库完成-----"); } public void insertSuYuanDatas(List lists, String time) { List> subLists = Lists.partition(lists, AsyncService.BATCH_INSERT_NUMBER); for (List sub : subLists) { suYuanMapper.batchInsert(TableStrategy.getTableStrategy(time), sub); } logger.info("------ " + time + ".csv," + lists.size() + " 条数据已入库 ------"); } public Integer isTableExists(String tableName) { return suYuanMapper.isTableExists(tableName); } public int createNewTable(String tableName) { return suYuanMapper.createTable(tableName); } public List selectSuYuan2d(Date date) { String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date); List ids2d = CalculateUtils.assembleId(commonService.getCheckPoints2d()); DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); if (StringUtils.isNull(dictRecord)) { return null; } return suYuanMapper.get2d(dictRecord.getTableName(), ids2d); } public List selectSuYuan3d(String name, Date date) { MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name); if (StringUtils.isNull(checkPoint)) { return null; } int range = 10; String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date); List ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range); DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); if (StringUtils.isNull(dictRecord)) { return null; } return suYuanMapper.get3d(dictRecord.getTableName(), ids3d); } public Map selectSuYuan100(String name, Date date) { Map result = new HashMap<>(); MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name); if (StringUtils.isNull(checkPoint)) { return null; } int range = 10; String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date); List ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range); DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); if (StringUtils.isNull(dictRecord)) { return null; } if (suYuanMapper.isTableExists(dictRecord.getTableName()) == 0) return null; List list = suYuanMapper.getDistanceSuYuan(dictRecord.getTableName(), ids3d); AtomicInteger i = new AtomicInteger(0); list.stream().forEach(s -> { s.setName(checkPoint.getName()); i.getAndIncrement(); s.setVocsName(checkPoint.getName() + "-" + i.longValue()); }); if (StringUtils.isNotEmpty(list)) { Collections.sort(list); DistanceSuYuan max = list.get(0); FeedbackDetail feedbackDetail = new FeedbackDetail(null, dictRecord.getTableName(), max.getName(), max.getId(), max.getVocsName(), max.getVocsValue(), null, null, null, DateUtils.getNowDate()); feedbackMapper.insert(feedbackDetail); result.put("feedbackId", feedbackDetail.getId()); } result.put("data", list); return result; } public Map selectSuYuan200(String name, Date date) { Map result = new HashMap(); MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name); if (StringUtils.isNull(checkPoint)) { return null; } int range = 20; String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date); List ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range); DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); if (StringUtils.isNull(dictRecord)) { return null; } if (suYuanMapper.isTableExists(dictRecord.getTableName()) == 0) return null; List list = suYuanMapper.getDistanceSuYuan(dictRecord.getTableName(), ids3d); AtomicInteger i = new AtomicInteger(0); list.stream().forEach(s -> { s.setName(checkPoint.getName()); i.getAndIncrement(); s.setVocsName(checkPoint.getName() + "-" + i.longValue()); }); if (StringUtils.isNotEmpty(list)) { Collections.sort(list); DistanceSuYuan max = list.get(0); FeedbackDetail feedbackDetail = new FeedbackDetail(null, dictRecord.getTableName(), max.getName(), max.getId(), max.getVocsName(), max.getVocsValue(), null, null, null, DateUtils.getNowDate()); feedbackMapper.insert(feedbackDetail); result.put("feedbackId", feedbackDetail.getId()); } result.put("data", list); return result; } public Map selectSuYuan300(String name, Date date) { Map result = new HashMap(); MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name); if (StringUtils.isNull(checkPoint)) { return null; } int range = 30; String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date); List ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range); DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); if (StringUtils.isNull(dictRecord)) { return null; } if (suYuanMapper.isTableExists(dictRecord.getTableName()) == 0) return null; List list = suYuanMapper.getDistanceSuYuan(dictRecord.getTableName(), ids3d); AtomicInteger i = new AtomicInteger(0); list.stream().forEach(s -> { s.setName(checkPoint.getName()); i.getAndIncrement(); s.setVocsName(checkPoint.getName() + "-" + i.longValue()); }); if (StringUtils.isNotEmpty(list)) { Collections.sort(list); DistanceSuYuan max = list.get(0); FeedbackDetail feedbackDetail = new FeedbackDetail(null, dictRecord.getTableName(), max.getName(), max.getId(), max.getVocsName(), max.getVocsValue(), null, null, null, DateUtils.getNowDate()); feedbackMapper.insert(feedbackDetail); result.put("feedbackId", feedbackDetail.getId()); } result.put("data", list); return result; } public Map selectSuYuan500(String name, Date date) { Map result = new HashMap<>(); MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name); if (StringUtils.isNull(checkPoint)) { return null; } int range = 50; String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date); List ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range); DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); if (StringUtils.isNull(dictRecord)) { return null; } if (suYuanMapper.isTableExists(dictRecord.getTableName()) == 0) return null; List list = suYuanMapper.getDistanceSuYuan(dictRecord.getTableName(), ids3d); AtomicInteger i = new AtomicInteger(0); list.stream().forEach(s -> { s.setName(checkPoint.getName()); i.getAndIncrement(); s.setVocsName(checkPoint.getName() + "-" + i.longValue()); }); if (StringUtils.isNotEmpty(list)) { Collections.sort(list); DistanceSuYuan max = list.get(0); FeedbackDetail feedbackDetail = new FeedbackDetail(null, dictRecord.getTableName(), max.getName(), max.getId(), max.getVocsName(), max.getVocsValue(), null, null, null, DateUtils.getNowDate()); feedbackMapper.insert(feedbackDetail); result.put("feedbackId", feedbackDetail.getId()); } result.put("data", list); return result; } public List getMonitorData(String name) { MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name); if (StringUtils.isNull(checkPoint)) { return null; } /*List recordList = dictRecordMapper.selectDictRecordList(new DictRecord()); List tableNames = new ArrayList(); for (DictRecord dr : recordList) { if (suYuanMapper.isTableExists(dr.getTableName()) > 0) { tableNames.add(dr.getTableName()); } } return suYuanMapper.getMonitorData(tableNames, checkPoint.getX() + "_" + checkPoint.getY() + "_" + checkPoint.getZ());*/ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); String end = DateUtils.getYyyyMmDdHh(calendar.getTime()); //dateFormat.format(calendar.getTime()); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.add(Calendar.DATE, -7); String start = DateUtils.getYyyyMmDdHh(calendar.getTime()); return qxshMapper.getNewMonitorData(checkPoint.getName(), start, end); } public int updateVocsName(Date date, String id, String vocsName) { String table = "su_yuan_" + DateUtils.getYyyyMmDdHh(date); // new Date() if (isTableExists(table) == 0) { return 0; } //return suYuanMapper.updateVocsName(table, id, vocsName); return 1; } public List queryQxsh(Date date) { String time = DateUtils.getYyyyMmDdHh(date); return qxshMapper.selectByTime(time); } public List> selectRects(Double[] x, Double[] y) { List> list = new ArrayList<>(); for (int i = 0; i < x.length; i++) { list.add(CalculateUtils.calcRect(x[i], y[i])); } return list; } public SuYuan700 selectSuYuan700ById(String id, Date date) { id = id.substring(0, id.lastIndexOf("_") + 1) + "0"; String time = DateUtils.getYyyyMmDdHhMmSs(date).substring(0, 13) + ":00:00"; return suYuanMapper.selectSuYuan700ById(id, time); } public SuYuan700 selectSuYuan46ById(String id, Date date) { id = id.substring(0, id.lastIndexOf("_") + 1) + "0"; String time = DateUtils.getYyyyMmDdHhMmSs(date).substring(0, 13) + ":00:00"; return suYuanMapper.selectSuYuan46ById(id, time); } public String selectAddrByXY(double x, double y) { // 1米=0.0000089932 List locations = locationMapper.selectByXY(x, y); if (null != locations && locations.size() > 0) { return locations.get(0).getName(); } /*locations = locService.selectVocAddrs(x, y); if (null != locations && locations.size() > 0) { return locations.get(0).getName(); }*/ return null; } public String selectSuYuanIdByName(String name) { if (sites.containsKey(name)) { return sites.get(name); } MonitorPointPosition mpp = commonService.select3dCheckPointByName(name); if (null == mpp) { return null; } String suYuanId = mpp.getId().substring(0, mpp.getId().lastIndexOf("_") + 1) + "0"; if (!sites.containsKey(name)) { sites.put(name, suYuanId); } return suYuanId; } /** * 根据ID查询快速溯源 */ public List selectFastById(String id, Date date) { String time = DateUtils.getYyyyMmDdHhMmSs(date); return suYuanMapper.selectFastById(id, time); } }