| | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.date.DateTime; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.date.LocalDateTimeUtil; |
| | | import cn.hutool.core.thread.ThreadUtil; |
| | | import cn.hutool.core.util.NumberUtil; |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.google.common.collect.Sets; |
| | | import com.product.common.utils.spring.SpringUtils; |
| | | import com.product.core.config.CoreConst; |
| | | import com.product.core.config.Global; |
| | |
| | | import java.sql.SQLException; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.concurrent.Callable; |
| | | import java.util.concurrent.CompletionService; |
| | | import java.util.concurrent.ExecutionException; |
| | | import java.util.concurrent.Future; |
| | | import java.util.concurrent.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | this.commonService = SpringBeanUtil.getBean(CommonService.class); |
| | | } |
| | | return commonService; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | Date parse = DateUtil.parse("2023-10-23 18:22:50", "yyyy-MM-dd HH:mm:ss"); |
| | | Date parse1 = DateUtil.parse("2023-10-23 18:24:11", "yyyy-MM-dd HH:mm:ss"); |
| | | Date parse2 = DateUtil.parse("2023-10-23 18:24:21", "yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | Set<Date> set = Sets.newHashSet(parse, parse1, parse2); |
| | | |
| | | List<Date> sets = CollectionUtil.toList(set.toArray(new Date[0])); |
| | | |
| | | Optional<Date> max = sets.stream().max(Comparator.comparing((a) -> a.getTime())); |
| | | System.out.println(max.get()); |
| | | |
| | | } |
| | | |
| | | |
| | | public void splitTableData() { |
| | | FieldSetEntity reportDbConfig = getBaseDao().getFieldSetByFilter(CmnConst.PRODUCT_SYS_DATA_SYNC_MANAGER, "SYNC_NAME=?", new Object[]{"报表数据库"}, false); |
| | | if (FieldSetEntity.isEmpty(reportDbConfig)) { |
| | | throw new BaseException(ErrorCode.REPORT_DB_CONFIG_FAIL); |
| | | } |
| | | DataBaseEntity dbe = new DataBaseEntity(reportDbConfig); |
| | | Dao reportDao = dbe.getDao(); |
| | | Set<String> trackingTableSet = QuerySqlParseUtil.getAllTableName(reportDao, dbe.getDbName(), "da_t_wip_tracking"); |
| | | |
| | | for (String tableName : trackingTableSet) { |
| | | //获取年份从表名最后一个 下划线开始截取 |
| | | String year = tableName.substring(tableName.lastIndexOf("_") + 1); |
| | | if (StringUtils.equalsAny(year, "2017", "2018") || year.length() > 4) { |
| | | continue; |
| | | } |
| | | //获取表前缀 |
| | | String tablePrefix = tableName.substring(0, tableName.lastIndexOf("_")); |
| | | ExecutorService executorService = Executors.newFixedThreadPool(12); |
| | | for (int i = 1; i <= 12; i++) { |
| | | if ("2023".equals(year) && i > 9) { |
| | | break; |
| | | } |
| | | final int finalI = i; |
| | | executorService.submit(() -> { |
| | | Dao currentDao = dbe.newDao(); |
| | | //获取当前月份 以MM格式化 |
| | | String month = String.format("%02d", finalI); |
| | | //检查月份对应的表是否存在 |
| | | String monthTableName = tablePrefix + "_" + year + month; |
| | | Set<String> allTableName = QuerySqlParseUtil.getAllTableName(reportDao, dbe.getDbName(), tablePrefix + "_" + year + month); |
| | | if (allTableName.size() == 0 || !allTableName.contains(monthTableName)) { |
| | | //根据原始表结构创建新表 |
| | | String sql = "create table " + monthTableName + " like " + tableName; |
| | | currentDao.executeSql(sql); |
| | | SpringMVCContextHolder.getSystemLogger().info("创建表:" + monthTableName); |
| | | } |
| | | String sql = "INSERT INTO " + monthTableName + " SELECT * FROM " + tableName + " WHERE MONTH(update_date)=" + finalI; |
| | | currentDao.executeSql(sql); |
| | | currentDao.closeConnection(); |
| | | }); |
| | | |
| | | } |
| | | executorService.shutdown(); |
| | | while (true) { |
| | | try { |
| | | if (executorService.awaitTermination(5, TimeUnit.SECONDS)) break; |
| | | Thread.sleep(5000); |
| | | SpringMVCContextHolder.getSystemLogger().info("线程等待中..."); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | reportDao.closeConnection(); |
| | | } |
| | | |
| | | /** |
| | |
| | | success = true; |
| | | SpringMVCContextHolder.getSystemLogger().info("回写机号:" + serialNumber + "成功"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | SpringMVCContextHolder.getSystemLogger().error("回写机号:" + serialNumber + "失败"); |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | throw new BaseException(ErrorCode.INSERT_DATA_FAIL); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 插入数据到主库 |
| | | * |
| | |
| | | if (DataTableEntity.isEmpty(masterDataTable)) { |
| | | continue; |
| | | } |
| | | Object[] objects = masterDataTable.getData().stream().map(item -> item.getString(historyEntity.getPrimaryField())).toArray(); |
| | | Object[] objects = masterDataTable.getData().stream().map(item -> { |
| | | String primaryValue = item.getString(historyEntity.getPrimaryField()); |
| | | //判断是否为数值且包含小数点 小数点后面是否全是0 |
| | | if (NumberUtil.isNumber(primaryValue) && primaryValue.contains(".") && primaryValue.substring(primaryValue. |
| | | indexOf(".") + 1).matches("^0*$")) { |
| | | //返回整数字符串 |
| | | return primaryValue.substring(0, primaryValue.indexOf(".")); |
| | | } |
| | | return primaryValue; |
| | | }).toArray(); |
| | | //查询主库数据是否存在 |
| | | DataTableEntity list = dao.getList(historyEntity.getTableName(), |
| | | BaseUtil.buildQuestionMarkFilter(historyEntity.getPrimaryField(), objects.length, true), |
| | |
| | | //查询已存在的数据 |
| | | DataTableEntity list = dao.getList(historyEntity.getTableName(), |
| | | BaseUtil.buildQuestionMarkFilter(historyEntity.getPrimaryField(), value.size(), true), |
| | | value.stream().map(item -> item.getString(historyEntity.getPrimaryField())).toArray()); |
| | | value.stream().map(item -> { |
| | | String primaryValue = item.getString(historyEntity.getPrimaryField()); |
| | | //判断是否为数值且包含小数点 小数点后面是否全是0 |
| | | if (NumberUtil.isNumber(primaryValue) && primaryValue.contains(".") && primaryValue.substring(primaryValue. |
| | | indexOf(".") + 1).matches("^0*$")) { |
| | | //返回整数字符串 |
| | | return primaryValue.substring(0, primaryValue.indexOf(".")); |
| | | } |
| | | return primaryValue; |
| | | }).toArray()); |
| | | List<String> existIds = DataTableEntity.isEmpty(list) ? null : list.getData().stream().map(item -> item.getString(historyEntity.getPrimaryField())).collect(Collectors.toList()); |
| | | for (FieldSetEntity fieldSetEntity : value) { |
| | | String primaryValue = fieldSetEntity.getString(historyEntity.getPrimaryField()); |
| | |
| | | } |
| | | } |
| | | } |
| | | if (CmnConst.T_WIP_TRACKING.equalsIgnoreCase(targetTableName) && dt.getRows() > 1) { |
| | | List<FieldSetEntity> data = dt.getData(); |
| | | FieldSetEntity newData = null; |
| | | for (int i = 0; i < data.size(); i++) { |
| | | FieldSetEntity fieldSetEntity = data.get(i); |
| | | if ("ch-kt".equals(fieldSetEntity.getValue("pre_master_key"))) { |
| | | data.remove(i); |
| | | dt.removeFieldSetEntity(i); |
| | | break; |
| | | } else if (newData == null) { |
| | | newData = fieldSetEntity; |
| | | continue; |
| | | } |
| | | |
| | | Date date = fieldSetEntity.getDate(historyEntity.getTimeField()); |
| | | if (date.getTime() > newData.getDate(historyEntity.getTimeField()).getTime()) { |
| | | newData = fieldSetEntity; |
| | | } |
| | | } |
| | | if (newData != null) { |
| | | dt = new DataTableEntity(); |
| | | dt.addFieldSetEntity(newData); |
| | | } |
| | | //在list中取出时间最近的数据 |
| | | Optional<FieldSetEntity> max = data.stream().max(Comparator.comparing((a) -> a.getDate(historyEntity.getTimeField()).getTime())); |
| | | |
| | | } |
| | | DataTableEntity subData = dt.clones(); |
| | | DataTableEntity masterData = dt.clones(); |
| | | for (int i = 0; i < subData.getRows(); i++) { |
| | | FieldSetEntity fs = subData.getFieldSetEntity(i); |
| | | String preMasterKey = fs.getString("pre_master_key"); |
| | | if (historyEntity.getPrimaryField().equals(preMasterKey)) { |
| | | preMasterKey = null; |
| | | } |
| | | if ("ch-kt".equals(fs.getString("source_info")) && StringUtils.isEmpty(preMasterKey)) { |
| | | preMasterKey = fs.getString(historyEntity.getPrimaryField()); |
| | | } |
| | |
| | | fs.remove("~table_name~"); |
| | | masterData.getFieldSetEntity(i).remove("~table_name~"); |
| | | masterData.setFieldValue(i, "pre_master_key", null); |
| | | if (StringUtils.isEmpty(historyEntity.getPrimaryField())) { |
| | | subData.removeFieldSetEntity(i); |
| | | masterData.removeFieldSetEntity(i); |
| | | i--; |
| | | } |
| | | } |
| | | historyEntity.setArchivedDataTable(dt); |
| | | historyEntity.setMasterDataTable(masterData); |
| | |
| | | CompletionService<DataTableEntity> objectCompletionService = ThreadUtil.newCompletionService(); |
| | | //多线程查询单张表,等待所有线程查询完毕 |
| | | for (String tableName : tableArray) { |
| | | objectCompletionService.submit(() -> dao.getList("select a.*,'" + tableName + "' as '~table_name~' from " + tableName + "a where " + filterFieldName + " = ?", new Object[]{serialNumber})); |
| | | objectCompletionService.submit(() -> dao.getList("select a.*,'" + tableName + "' as '~table_name~' from " + tableName + " a where " + filterFieldName + " = ?", new Object[]{serialNumber})); |
| | | } |
| | | DataTableEntity data = new DataTableEntity(); |
| | | Future<DataTableEntity> take = objectCompletionService.take(); |
| | | for (int i = 0; i < tableArray.length; i++) { |
| | | DataTableEntity dataTableEntity = take.get(); |
| | | DataTableEntity dataTableEntity = objectCompletionService.take().get(); |
| | | BaseUtil.dataTableMerge(data, dataTableEntity); |
| | | } |
| | | |
| | | if (DataTableEntity.isEmpty(data) && !"product_sn".equals(filterFieldName)) { |
| | | throw new BaseException(errorCodes[1]); |
| | | } |
| | | return data; |
| | | } |