You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
9.1 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.docus.bgts.config;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.docus.bgts.entity.TableJsonRead;
import com.docus.bgts.enums.Codes;
import com.docus.bgts.facade.IBgtsService;
import com.docus.bgts.service.CheckIntegrityService;
import com.docus.bgts.utils.FileUtils;
import com.docus.bgts.utils.HttpUtils;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Component
public class MyScheduling {
@Value("${beat.url}")
private String beatUrl;
public static String syncFlag;
private final String tempfilePrefix = "dataConfig\\temp";
private final String tempDataFileName = "collectTimeTemp";
private final String lastTimeStr = "lastTime";
// static {
// syncFlag=FileUtils.getJsonByName("syncFlag").toString();
// }
@Autowired
IBgtsService bgtsService;
@Autowired
CheckIntegrityService checkIntegrityService;
private Logger logger = LogManager.getLogger(MyScheduling.class);
@Autowired
private ThreadPoolExecutor threadPoolExecutor;
//5分钟执行一次心跳
@Scheduled(fixedRate = 1000 * 5)
public void beat() {
Map<String, String> params = new HashMap<>();
params.put("code", String.valueOf(FileUtils.getJsonByName("collectorid")));
try {
HttpUtils.get(beatUrl, params);
} catch (Exception e) {
e.printStackTrace();
logger.info("心跳推送出错,可能是住院服务没有开启");
}
}
/**
* 手麻/重症采集器
* 10分钟执行一次
*/
// @Scheduled(fixedRate = 1000 * 60 * 10)
// public void collect() {
// String collectorid = String.valueOf(FileUtils.getJsonByName("collectorid"));
// if ((!collectorid.equals(Codes.SMCODE.getCode())) && (!collectorid.equals(Codes.ZZCODE.getCode()))) {
// return;
// }
// bgtsService.timerCollect();
// }
// // 时间
// private static List<String> date = new ArrayList<>();
// // 上次任务执行时间
// private static int currentIndex = 0;
// public static void main(String[] args) throws Exception{
// MyScheduling myScheduling = new MyScheduling();
//
// for (int i = 0; i <=50;i++){
// myScheduling.collectByExamNo();
// TimeUnit.MILLISECONDS.sleep(500);
// }
//
//// System.out.println(date);
//// System.out.println(currentIndex);
// }
// private static void initDate() throws Exception {
// String configStart = String.valueOf(FileUtils.getJsonByName("collectStartDate"));
// String configEnd = String.valueOf(FileUtils.getJsonByName("collectEndDate"));
// if (StrUtil.isBlank(configStart) || StrUtil.isBlank(configEnd)) {
// throw new RuntimeException("check homeQualitySet.json collectStartDate AND collectEndDate confg");
// }
// DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// LocalDate localDate = LocalDate.parse(configStart, dtf);
// LocalDate end = LocalDate.parse(configEnd);
// while (!localDate.isAfter(end)) {
// date.add(localDate.toString());
// localDate = localDate.plusDays(1);
// }
// }
/**
* 批量按需采集
* 10分钟执行一次
*/
//
// @Scheduled(fixedDelay = 1000 * 60 * 10)
// public void collectByExamNo() {
// String collectOpen = String.valueOf(FileUtils.getJsonByName("collectOpen"));
// if (collectOpen.equals("1")) {
// logger.info("按需采集任务执行----");
//// if (CollUtil.isEmpty(date)) {
//// try {
//// initDate();
//// } catch (Exception e) {
//// logger.error(e.getMessage());
//// return;
//// }
//// }
//// if (currentIndex >= date.size()) {
//// currentIndex = 0;
//// }
//// logger.info("index:{},Thread {}", currentIndex, Thread.currentThread().getName());
//// String collectStartDate = date.get(currentIndex);
//// String collectEndDate = collectStartDate + "\t23:59:59";
// String collectStartDate = String.valueOf(FileUtils.getJsonByName("collectStartDate"))+ "\t00:00:00";
// String collectEndDate = String.valueOf(FileUtils.getJsonByName("collectEndDate"))+ "\t23:59:59";
// logger.info("按需采集开始----------开始时间---" + collectStartDate + "------结束时间---" + collectEndDate);
// String collectorid = String.valueOf(FileUtils.getJsonByName("collectorid"));
// CountDownLatch countDownLatch = new CountDownLatch(1);
// threadPoolExecutor.execute(() -> {
// bgtsService.collectByDate(collectStartDate, collectEndDate, collectorid);
// countDownLatch.countDown();
// });
// try {
// countDownLatch.await();
// } catch (Exception e) {
// logger.error("JUC countDownLatch error {}", e.getMessage());
// }
// logger.info("{},采集结束----", collectStartDate);
// currentIndex++;
// System.gc();
// }
//
// }
@Scheduled(cron ="0 0 0 * * ?")
public void collect(){
String intervalDayOpen = String.valueOf(FileUtils.getJsonByName("intervalDayOpen"));
if (intervalDayOpen.equals("1")){
String collectorid = String.valueOf(FileUtils.getJsonByName("collectorid"));
int day = Integer.parseInt(String.valueOf(FileUtils.getJsonByName("intervalDay")));
logger.info("0点采集前"+day+"天出院数据");
Map<String, String> dateCollectionTime = getDateCollectionTime(new Date(), day);
logger.info("采集"+dateCollectionTime.get("startDate")+"至-------"+dateCollectionTime.get("endDate")+"数据");
bgtsService.collectByDate(dateCollectionTime.get("startDate"),dateCollectionTime.get("endDate"),collectorid);
}
}
/**
* 获取前x天开始与结束时间
*
* @param date
* @return
*/
public static Map<String, String> getDateCollectionTime(Date date, int day) {
Date dBefore;
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(date);//把当前时间赋给日历
calendar.add(Calendar.DAY_OF_MONTH, -day); //设置为前day天
dBefore = calendar.getTime(); //得到前一天的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
String defaultStartDate = sdf.format(dBefore); //格式化前一天
defaultStartDate = defaultStartDate + " 00:00:00";
String defaultEndDate = defaultStartDate.substring(0, 10) + " 23:59:59";
Map<String, String> map = new HashMap<>();
map.put("startDate", defaultStartDate);
map.put("endDate", defaultEndDate);
return map;
}
/**
* 完整性校验
*/
// @Scheduled(fixedRate = 1000 * 5 * 60)
// public void syncIntegrality() {
// if (null == syncFlag || "0".equals(syncFlag)) {
//
// } else {
// //获取最后时间
// String lastDate = getLastDate();
// if(StringUtils.isNotBlank(lastDate)) {
// Date date = new Date();
// boolean flag = checkIntegrityService.addSyncIntegrality(lastDate, null);
// if(flag) {
// refreshLastDate(date);
// }
// }else{
// logger.info("采集时间为空");
// }
// }
// }
/**
* 上次采集时间
* @return
*/
// private String getLastDate(){
// TableJsonRead tableJsonRead = new TableJsonRead();
// Map<String,String> map = tableJsonRead.Read(tempfilePrefix, tempDataFileName,Map.class);
// return map.get(lastTimeStr);
// }
/**
* 刷新上次采集时间
*/
// private void refreshLastDate(Date lastDate){
// String lastTime = DateUtil.format(lastDate, "yyyy-MM-dd HH:mm:ss");
// TableJsonRead tableJsonRead = new TableJsonRead();
// Map<String, String> map = new HashMap<>();
// map.put(lastTimeStr, lastTime);
// tableJsonRead.Save(tempfilePrefix, tempDataFileName, JSON.toJSONString(map));
// }
}