package com.product.kt.test.service; import cn.hutool.core.util.RandomUtil; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.product.core.dao.BaseDao; import com.product.core.entity.FieldSetEntity; import com.product.datasource.dao.Dao; import com.product.datasource.entity.DataBaseEntity; import com.product.kt.test.config.SyncConfig; import com.product.util.BaseUtil; import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Map; /** * 写入数据工具类 * * @author 86151 */ @Component public class WriteToolService { @Autowired BaseDao baseDao; private static Map autoFieldMap = Maps.newHashMap(); /** * 处理表名 * @param tableName * @return */ private String dealTableName(String tableName) { if (SyncConfig.PREFIX_DEAL_SIGN == 0) { tableName = tableName.replaceFirst(SyncConfig.TARGET_TABLE_PREFIX, ""); } else if (SyncConfig.PREFIX_DEAL_SIGN == 1) { tableName = SyncConfig.TARGET_TABLE_PREFIX + tableName; } return tableName; } public void writeData(String sourceLinkUUID, List listFse, String tableName, String timeField) throws InterruptedException { //获取数据同步配置 FieldSetEntity sourceDataSourceFse = baseDao.getFieldSetEntity("product_sys_data_sync_manager", SyncConfig.DATA_SOURCE_MAP.get(sourceLinkUUID), false); DataBaseEntity sourceDbe = null; Dao sourceDao = null; try { sourceDbe = new DataBaseEntity(sourceDataSourceFse); sourceDao = sourceDbe.getDao(); FieldSetEntity tempFse; List> partition = Lists.partition(listFse, 100); for (int i = 0; i < partition.size(); i++) { Thread.currentThread().sleep(RandomUtil.randomInt(800, 1200)); List fieldSetEntityList = partition.get(i); for (int i1 = 0; i1 < fieldSetEntityList.size(); i1++) { FieldSetEntity ff = fieldSetEntityList.get(i1); ff.setTableName(dealTableName(tableName)); //初始化时间字段 // if (!BaseUtil.strIsNull(timeField) && ff.getObject(timeField.toLowerCase()) == null) { if (!BaseUtil.strIsNull(timeField)) { // fieldSetEntityList.get(i1).getMeta().removeField(timeField.toLowerCase()); ff.setValue(timeField.toLowerCase(), new Date()); } // 初始化id String IDField = SyncConfig.SYNC_TABLE_MAP.get(tableName).get("order_field").toString(); if (!BaseUtil.strIsNull(IDField)) { ff.setValue(IDField.toLowerCase(), getID(tableName)); } sourceDao.add(ff); } } if (true) { return; } for (int i = 0; i < listFse.size(); i++) { tempFse = listFse.get(i); //初始化表名 tempFse.setTableName(dealTableName(tableName)); //初始化时间字段 if (!BaseUtil.strIsNull(timeField) && tempFse.getObject(timeField.toLowerCase()) == null) { tempFse.setValue(timeField.toLowerCase(), new Date()); } sourceDao.add(tempFse); } } catch (Exception e) { e.printStackTrace(); } finally { if (sourceDao != null) { sourceDao.closeConnection(); } } } private int getID(String tableName) { autoFieldMap.merge(tableName, 1, Integer::sum); return autoFieldMap.get(tableName); } }