T100738
2024-05-12 a0c549301bd941c6899dfe63f21db86fb2046b5f
product-server-data-center/src/main/java/com/product/data/center/service/CommonService.java
@@ -21,12 +21,14 @@
import com.product.datasource.entity.DataBaseEntity;
import com.product.quartz.service.impl.SysJobService;
import com.product.util.BaseUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
@@ -57,6 +59,7 @@
   @Value("${data.system.name}")
   private String dataSystemName;
   @Transactional
   public void deleteCenterLog() {
      try {
         //在mysql的information_schema 表中查询指定表是否存在
@@ -115,43 +118,108 @@
            String createTableSql = "create table " + tableNameStr + " like product_sys_data_center_log";
            getBaseDao().executeUpdate(createTableSql, new Object[]{});
         }
         //查询各类型配置最后一次成功的数据
         String sqlFilter = "SELECT * FROM (SELECT uuid\n" +
               "FROM product_sys_data_center_log\n" +
               "WHERE uuid IN (\n" +
               "\tSELECT uuid\n" +
               "\tFROM product_sys_data_center_log\n" +
               "\tWHERE id IN (\n" +
               "\t\tSELECT max(a.id)\n" +
               "\t\tFROM product_sys_data_center_log a\n" +
               "\t\t\tJOIN product_sys_data_center_log b ON a.uuid = b.pre_step_uuid\n" +
               "\t\tWHERE a.type = 1\n" +
               "\t\t\tAND b.type = 2\n" +
               "\t\t\tAND (a.result = 1\n" +
               "\t\t\t\tOR (a.deal_flag = 1\n" +
               "\t\t\t\t\tAND a.deal_result = 1))\n" +
               "\t\t\tAND (b.result = 1\n" +
               "\t\t\t\tOR (b.deal_flag = 1\n" +
               "\t\t\t\t\tAND b.deal_result = 1))\n" +
               "\t\tGROUP BY a.config_uuid\n" +
               "\t)\n" +
               "\tUNION ALL\n" +
               "\tSELECT aa.uuid\n" +
               "\tFROM product_sys_data_center_log aa\n" +
               "\t\tJOIN product_sys_data_center_log bb ON aa.pre_step_uuid = bb.uuid\n" +
               "\tWHERE aa.type = 2\n" +
               "\t\tAND bb.type = 1\n" +
               "\t\tAND bb.id IN (\n" +
               "\t\t\tSELECT max(a.id)\n" +
               "\t\t\tFROM product_sys_data_center_log a\n" +
               "\t\t\t\tJOIN product_sys_data_center_log b ON a.uuid = b.pre_step_uuid\n" +
               "\t\t\tWHERE a.type = 1\n" +
               "\t\t\t\tAND b.type = 2\n" +
               "\t\t\t\tAND (a.result = 1\n" +
               "\t\t\t\t\tOR (a.deal_flag = 1\n" +
               "\t\t\t\t\t\tAND a.deal_result = 1))\n" +
               "\t\t\t\tAND (b.result = 1\n" +
               "\t\t\t\t\tOR (b.deal_flag = 1\n" +
               "\t\t\t\t\t\tAND b.deal_result = 1))\n" +
               "\t\t\tGROUP BY a.config_uuid\n" +
               "\t\t)\n" +
               "\tUNION ALL\n" +
               "\tSELECT uuid\n" +
               "\tFROM product_sys_data_center_log a\n" +
               "\tWHERE a.type = 5\n" +
               "\t\tAND detail != 3\n" +
               "\t\tAND (a.result = 1\n" +
               "\t\t\tOR (a.deal_flag = 1\n" +
               "\t\t\t\tAND a.deal_result = 1))\n" +
               ")) ddd";
         //插入数据根据创建时间的年月分别插入到对应的表中
         for (DateTime dateTime : tableSuffixList) {
            String insertSql = "insert into " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " select * from product_sys_data_center_log where created_utc_datetime >= str_to_date(concat(?, '-01 00:00:00'), '%Y-%m-%d %H:%i:%s') and created_utc_datetime < str_to_date(concat(?, '-01 00:00:00'), '%Y-%m-%d %H:%i:%s') + interval 1 month" +
                  " and id >(select ifnull(max(id),0) from " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " ) and (result = 1 or ( deal_flag = 1 AND deal_result = 1 ) )";
                  " and id >(select ifnull(max(id),0) from " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " ) and (result = 1 or ( deal_flag = 1 AND deal_result = 1 ) ) and created_utc_datetime <now() + interval -7 day";
            getBaseDao().executeUpdate(insertSql, new Object[]{DateUtil.format(dateTime, "yyyy-MM"), DateUtil.format(dateTime, "yyyy-MM")});
            //删除数据已插入的数据
//                //删除数据已插入的数据
            StringBuilder deleteSql = new StringBuilder();
            deleteSql.append(" DELETE FROM product_sys_data_center_log a INNER JOIN ");
            deleteSql.append(" product_sys_data_center_log b on a.id=b.pre_step_uuid and a.type=1 and b.type=2 ");
            deleteSql.append(" select a.id FROM product_sys_data_center_log a INNER JOIN ");
            deleteSql.append(" product_sys_data_center_log b on a.uuid=b.pre_step_uuid and a.type=1 and b.type=2 ");
            deleteSql.append(" where ");
            deleteSql.append(" a.id>= (select ifnull(min(id),0) from " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " ) ");
            deleteSql.append(" and a.id <= (select ifnull(max(id),0) from " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " ) ");
            deleteSql.append(" (a.result = 1 or ( a.deal_flag = 1 AND a.deal_result = 1 ) ) ");
            deleteSql.append(" a.type=1 ");
            deleteSql.append(" and (a.result = 1 or ( a.deal_flag = 1 AND a.deal_result = 1 ) ) ");
            deleteSql.append(" and a.type=1 ");
            deleteSql.append(" and a.uuid not in (").append(sqlFilter).append(") ");
            //先删除采集日志数据
            getBaseDao().executeUpdate(deleteSql.toString(), new Object[]{});
//            getBaseDao().executeUpdate(deleteSql.toString(), new Object[]{});
            DataTableEntity dt = getBaseDao().listTable(deleteSql.toString(), new Object[]{});
            if (!DataTableEntity.isEmpty(dt)) {
               List<Integer> ids = dt.getData().stream().map(row -> row.getInteger("id")).collect(Collectors.toList());
               getBaseDao().delete("product_sys_data_center_log", BaseUtil.buildQuestionMarkFilter("id", ids.size(), true), ids.toArray());
            }
            deleteSql.setLength(0);
            //再删除提取日志数据
            deleteSql.append(" DELETE FROM product_sys_data_center_log a LEFT JOIN ");
            deleteSql.append(" product_sys_data_center_log b on b.id=a.pre_step_uuid and b.type=1 and a.type=2 ");
            deleteSql.append(" select a.id FROM product_sys_data_center_log a LEFT JOIN ");
            deleteSql.append(" product_sys_data_center_log b on b.uuid=a.pre_step_uuid and b.type=1 and a.type=2 ");
            deleteSql.append(" where ");
            deleteSql.append(" a.id>= (select ifnull(min(id),0) from " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " ) ");
            deleteSql.append(" and a.id <= (select ifnull(max(id),0) from " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " ) ");
            deleteSql.append(" (a.result = 1 or ( a.deal_flag = 1 AND a.deal_result = 1 ) ) ");
            deleteSql.append(" a.type=2 and b.id is null  ");
            deleteSql.append(" and (a.result = 1 or ( a.deal_flag = 1 AND a.deal_result = 1 ) ) ");
            deleteSql.append(" and a.type=2 and b.id is null  ");
            deleteSql.append(" and a.uuid not in (").append(sqlFilter).append(") ");
            getBaseDao().executeUpdate(deleteSql.toString(), new Object[]{});
//            getBaseDao().executeUpdate(deleteSql.toString(), new Object[]{});
            dt = getBaseDao().listTable(deleteSql.toString(), new Object[]{});
            if (!DataTableEntity.isEmpty(dt)) {
               List<Integer> ids = dt.getData().stream().map(row -> row.getInteger("id")).collect(Collectors.toList());
               getBaseDao().delete("product_sys_data_center_log", BaseUtil.buildQuestionMarkFilter("id", ids.size(), true), ids.toArray());
            }
            deleteSql.setLength(0);
            //再删除其他日志数据
            deleteSql.append(" DELETE FROM product_sys_data_center_log a  ");
            deleteSql.append(" select a.id FROM product_sys_data_center_log a  ");
            deleteSql.append(" where ");
            deleteSql.append(" a.id>= (select ifnull(min(id),0) from " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " ) ");
            deleteSql.append(" and a.id <= (select ifnull(max(id),0) from " + tableName + "_" + DateUtil.format(dateTime, "yyyyMM") + " ) ");
            deleteSql.append(" (a.result = 1 or ( a.deal_flag = 1 AND a.deal_result = 1 ) ) ");
            deleteSql.append(" a.type>2 ");
            getBaseDao().executeUpdate(deleteSql.toString());
            deleteSql.append(" and (a.result = 1 or ( a.deal_flag = 1 AND a.deal_result = 1 ) ) ");
            deleteSql.append(" and a.type>2 ");
            deleteSql.append(" and a.uuid not in (").append(sqlFilter).append(") ");
            dt = getBaseDao().listTable(deleteSql.toString(), new Object[]{});
            if (!DataTableEntity.isEmpty(dt)) {
               List<Integer> ids = dt.getData().stream().map(row -> row.getInteger("id")).collect(Collectors.toList());
               getBaseDao().delete("product_sys_data_center_log", BaseUtil.buildQuestionMarkFilter("id", ids.size(), true), ids.toArray());
            }
         }
//         fs = null;
@@ -167,7 +235,6 @@
         throw e;
      }
   }
   /**
    * 创建定时任务