|
|
@@ -0,0 +1,158 @@
|
|
|
+package com.lc.ibps.platform.plan.job;
|
|
|
+
|
|
|
+import com.lc.ibps.base.core.util.AppUtil;
|
|
|
+import com.lc.ibps.base.core.util.Collections;
|
|
|
+import com.lc.ibps.base.framework.id.UniqueIdUtil;
|
|
|
+import com.lc.ibps.base.framework.table.ICommonDao;
|
|
|
+import com.lc.ibps.base.saas.token.ITenantTokenService;
|
|
|
+import com.lc.ibps.base.web.context.ContextUtil;
|
|
|
+import com.lc.ibps.bpmn.api.IBpmInstService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.quartz.JobExecutionContext;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.YearMonth;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+public class StandardSubstanceJob extends AbstractJob{
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(StandardSubstanceJob.class);
|
|
|
+ private ICommonDao<?> commonDao = AppUtil.getBean(ICommonDao.class);
|
|
|
+ private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void executeJob(JobExecutionContext context) throws Exception {
|
|
|
+
|
|
|
+ periodicVerification();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void periodicVerification()throws Exception{
|
|
|
+ List<Map<String, Object>> addList = new ArrayList<>();
|
|
|
+ //1查出所有计划的数据并且去重
|
|
|
+ String sql = "select z.id_ as zId_ ,z.biao_zhun_wu_zhi_ , z.bian_hao_ ,z.he_cha_ping_lv_ , z.he_cha_shi_jian_ , j.bao_zhuang_gui_ge, " +
|
|
|
+ " z.he_cha_fang_fa_ , z.he_cha_ren_yuan_ ,z.biao_wu_id_ , z.he_cha_jie_guo_pa ,f.id_ as fId_,f.di_dian_,j.bian_zhi_bu_men_ " +
|
|
|
+ " from t_bzwzqjhcjh f join t_bzwzqjhcjhxq z on f.id_ = z.parent_id_ join t_sjhcjcsjpz j on z.biao_wu_id_ = j.id_ " +
|
|
|
+ " JOIN ( " +
|
|
|
+ " SELECT z2.biao_wu_id_, MAX( z2.create_time_ ) AS max_create_time_ " +
|
|
|
+ " FROM t_bzwzqjhcjhxq z2 JOIN t_bzwzqjhcjh f2 ON z2.parent_id_ = f2.id_ JOIN t_sjhcjcsjpz j2 ON z2.biao_wu_id_ = j2.id_ " +
|
|
|
+ " WHERE f2.shi_fou_guo_shen_ = '已完成' AND f2.nian_du_ = YEAR ( NOW( ) ) AND j2.shi_fou_guo_shen_ = '正常' " +
|
|
|
+ " GROUP BY z2.biao_wu_id_ ) t ON z.biao_wu_id_ = t.biao_wu_id_ " +
|
|
|
+ " AND z.create_time_ = t.max_create_time_ " +
|
|
|
+ " where f.shi_fou_guo_shen_ = '已完成' AND f.nian_du_ = YEAR ( NOW( ) ) AND j.shi_fou_guo_shen_ = '正常' " ;
|
|
|
+ List<Map<String, Object>> list = (List<Map<String, Object>>) commonDao.query(sql);
|
|
|
+ if(Collections.isEmpty(list)){
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ //2根据上面的数据分别计算上次校准日期+校准周期是否等于今天,是则推送校准记录
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ int currentYear = today.getYear();
|
|
|
+
|
|
|
+ for (Map<String,Object> item:list) {
|
|
|
+ String startData = item.get("he_cha_shi_jian_").toString();
|
|
|
+ LocalDate start = LocalDate.parse(startData, FMT);
|
|
|
+ //开始计算
|
|
|
+ int cycleMonths = Integer.parseInt(item.get("he_cha_ping_lv_").toString());
|
|
|
+ if (cycleMonths <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 保持原始"日",用于月末边界(如 1-31 + 1月 → 2-28)
|
|
|
+ int originalDay = start.getDayOfMonth();
|
|
|
+
|
|
|
+ // 从 start 往后推,找到本年内第一个周期日
|
|
|
+ LocalDate cursor = start;
|
|
|
+ while (cursor.getYear() < currentYear) {
|
|
|
+ cursor = plusMonthsKeepDay(cursor, cycleMonths, originalDay);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断今天是否是周期日
|
|
|
+ while (cursor.getYear() == currentYear) {
|
|
|
+ if (cursor.equals(today)) {
|
|
|
+ addList.add(item);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (cursor.isAfter(today)) {
|
|
|
+ break; // 已超过今天,不用再往后看
|
|
|
+ }
|
|
|
+ cursor = plusMonthsKeepDay(cursor, cycleMonths, originalDay);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //需要查询当天是否已经推送过了,不能重复推送
|
|
|
+ List<String> dislodge = without(addList);
|
|
|
+ if(Collections.isEmpty(dislodge)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.startWorkflow(dislodge,"bzwzqjhcjlb","Process_133qma1");
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> without(List<Map<String, Object>> added) throws Exception{
|
|
|
+ List<Map<String, Object>> addList = new ArrayList<>();
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
+ if(Collections.isEmpty(added)){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String ids = added.stream()
|
|
|
+ .map(map -> String.valueOf(map.get("biao_wu_id_")))
|
|
|
+ .collect(Collectors.joining(","));
|
|
|
+ String date = LocalDate.now().toString();
|
|
|
+ String sql = "select * from t_bzwzqjhcjlb where find_in_set(biao_wu_id_,'"+ids+"') and qi_jian_he_cha_sh = '" + date + "'";
|
|
|
+ List<Map<String, Object>> list = (List<Map<String, Object>>) commonDao.query(sql);
|
|
|
+ if(Collections.isNotEmpty(list)){
|
|
|
+ Set<String> bCodes = list.stream()
|
|
|
+ .map(it -> (String) it.get("biao_wu_id_"))
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ addList = added.stream()
|
|
|
+ .filter(it -> !bCodes.contains(it.get("biao_wu_id_")))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }else{
|
|
|
+ //当天还没有进行推送
|
|
|
+ addList=added;
|
|
|
+ }
|
|
|
+ for (Map<String,Object> item:addList) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ String id = UniqueIdUtil.getId();
|
|
|
+ map.put("id_",id);
|
|
|
+ map.put("bian_zhi_bu_men_",item.get("bian_zhi_bu_men_").toString());
|
|
|
+ map.put("di_dian_",item.get("di_dian_").toString());
|
|
|
+ map.put("bian_zhi_ren_",item.get("he_cha_ren_yuan_").toString());
|
|
|
+ map.put("bian_zhi_shi_jian",date+" 06:00");
|
|
|
+ map.put("qi_jian_he_cha_sh",date);
|
|
|
+ map.put("biao_wu_id_",item.get("biao_wu_id_").toString());
|
|
|
+ map.put("bian_hao_",item.get("bian_hao_").toString());
|
|
|
+ map.put("biao_zhun_wu_zhi_",item.get("biao_zhun_wu_zhi_").toString());
|
|
|
+ map.put("qi_jian_he_cha_fa",item.get("he_cha_fang_fa_").toString());
|
|
|
+ map.put("gui_ge_xing_hao_",item.get("bao_zhuang_gui_ge").toString());
|
|
|
+ map.put("he_cha_ji_lu_",item.get("he_cha_jie_guo_pa").toString());
|
|
|
+ // map.put("shang_yi_ci_he_ch",item.get("shang_ci_he_cha_r").toString());
|
|
|
+ map.put("he_cha_pin_lv_",item.get("he_cha_ping_lv_").toString());
|
|
|
+ map.put("ji_hua_zi_biao_id",item.get("zId_").toString());
|
|
|
+ map.put("ji_hua_zhu_biao_i",item.get("fId_").toString());
|
|
|
+ map.put("shi_fou_guo_shen_","待处理");
|
|
|
+ result.add(id);
|
|
|
+ commonDao.execute(this.buildInsertSql(map, "t_bzwzqjhcjlb"));
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void startWorkflow(List<String> dislodge,String formKey,String defKey) {
|
|
|
+ if (Collections.isEmpty(dislodge)) return;
|
|
|
+ IBpmInstService bpmInstService = AppUtil.getBean(IBpmInstService.class);
|
|
|
+ if (StringUtils.isBlank(ContextUtil.getCurrentAccessToken())) {
|
|
|
+ ITenantTokenService tenantTokenService = AppUtil.getBean(ITenantTokenService.class);
|
|
|
+ String accessToken = tenantTokenService.getAccessToken();
|
|
|
+ ContextUtil.setCurrentAccessToken(accessToken);
|
|
|
+ }
|
|
|
+ String[] arr = dislodge.toArray(new String[0]);
|
|
|
+ bpmInstService.startFlowFromList(arr, formKey, defKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ //工具方法
|
|
|
+ private static LocalDate plusMonthsKeepDay(LocalDate date, int months, int originalDay) {
|
|
|
+ YearMonth targetYm = YearMonth.from(date).plusMonths(months);
|
|
|
+ return targetYm.atDay(Math.min(originalDay, targetYm.lengthOfMonth()));
|
|
|
+ }
|
|
|
+}
|