From 4236c930bf5d2de9fe9c360ecd4467f0d8033409 Mon Sep 17 00:00:00 2001 From: anthonywj Date: Mon, 3 Apr 2023 17:33:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=90=8C=E6=AD=A5=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../glxp/api/constant/BasicProcessStatus.java | 2 +- .../java/com/glxp/api/constant/Constant.java | 5 + .../sync/BasicDownloadStatusEntity.java | 2 +- .../entity/sync/BasicExportStatusEntity.java | 2 +- .../api/idc/service/impl/IdcServiceImpl.java | 1790 +++++++++-------- .../inout/impl/IoOrderServiceImpl.java | 4 + .../java/com/glxp/api/task/AsyncSpsTask.java | 2 +- 7 files changed, 912 insertions(+), 895 deletions(-) diff --git a/src/main/java/com/glxp/api/constant/BasicProcessStatus.java b/src/main/java/com/glxp/api/constant/BasicProcessStatus.java index 35e92dc0..b028a12d 100644 --- a/src/main/java/com/glxp/api/constant/BasicProcessStatus.java +++ b/src/main/java/com/glxp/api/constant/BasicProcessStatus.java @@ -27,7 +27,7 @@ public class BasicProcessStatus { public static final int BASIC_THIRD_ORDER = 6; //往来单位 public static final int NEW_ALL_DATA = 7; //所有最新数据 public static final int NEW_ALL_BUS = 8; - public static final int NEW_ALL_ORDER = 9; + public static final String NEW_ALL_ORDER = "9"; public static final int NEW_ALL_DI = 10; public static final int SCHEDULE_NORMAL = 1; //常规任务 diff --git a/src/main/java/com/glxp/api/constant/Constant.java b/src/main/java/com/glxp/api/constant/Constant.java index 294d245a..794ffa0d 100644 --- a/src/main/java/com/glxp/api/constant/Constant.java +++ b/src/main/java/com/glxp/api/constant/Constant.java @@ -207,6 +207,11 @@ public class Constant { */ public static final String ORDER_STATUS_AUDITED = "audited"; + /** + * 待审核 + */ + public static final String ORDER_STATUS_WAIT_AUDITED = "waitAudited"; + /** * 单据-供应商已审核 */ diff --git a/src/main/java/com/glxp/api/entity/sync/BasicDownloadStatusEntity.java b/src/main/java/com/glxp/api/entity/sync/BasicDownloadStatusEntity.java index 9842cd3e..d91f106f 100644 --- a/src/main/java/com/glxp/api/entity/sync/BasicDownloadStatusEntity.java +++ b/src/main/java/com/glxp/api/entity/sync/BasicDownloadStatusEntity.java @@ -18,7 +18,7 @@ public class BasicDownloadStatusEntity { private String taskId; private String idDatas; private Integer status; - private Integer type; + private String type; private Date updateTime; private Integer scheduleType; private Date startTime; diff --git a/src/main/java/com/glxp/api/entity/sync/BasicExportStatusEntity.java b/src/main/java/com/glxp/api/entity/sync/BasicExportStatusEntity.java index 4326a37e..f0441ade 100644 --- a/src/main/java/com/glxp/api/entity/sync/BasicExportStatusEntity.java +++ b/src/main/java/com/glxp/api/entity/sync/BasicExportStatusEntity.java @@ -9,7 +9,7 @@ public class BasicExportStatusEntity { private String id; private String idDatas; private Integer status; - private Integer type; + private String type; private Date updateTime; private Integer scheduleType; private Date startTime; diff --git a/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java b/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java index 0b99bf21..f4ca386f 100644 --- a/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java +++ b/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java @@ -56,930 +56,938 @@ import okhttp3.Response; @Slf4j @Service public class IdcServiceImpl implements IdcService { - private static final Logger logger = LoggerFactory.getLogger(IdcServiceImpl.class); - @Value("${file_path}") - private String filePath; - @Value("${back_file_path}") - private String backFilePath; + private static final Logger logger = LoggerFactory.getLogger(IdcServiceImpl.class); + @Value("${file_path}") + private String filePath; + @Value("${back_file_path}") + private String backFilePath; - @Value("${API_KEY}") + @Value("${API_KEY}") private String apiKey; @Value("${API_SECRET}") private String apiSecret; /*同步表,格式:同步设置表列名/同步设置表列名(子表时设置,主表不设置)/主表唯一列(多列逗号分隔)/主表关联列/子表关联列/数据库实际表/时间列/图片或文件列/数据条件/说明*/ - private final String[] SYNC_TABLES= { - "entrustAction//basic_entrust_accept/id//////委托验收","basicProducts//basic_udirel/id///updateTime///耗材字典","/basicProducts/basic_products/id/uuid/uuid////耗材字典信息详情", - "//company_product_relevance////updateTime///供应商关联信息", - "basicCorp//basic_corp/id///updateTime///往来单位","typeThird//thr_bustype_origin/id///updateTime///第三方单据类型", - "basicThirdCorp//thr_corp/id///updateTime///第三方往来单位","//thr_dept/id///updateTime///第三方部门", - "//thr_inv_products/id///updateTime///第三方库存","basicThirdInv//thr_inv_warehouse/id//////第三方仓库", - "basicThirdBusOrder//thr_order/id///updateTime///第三方业务单据","/basicThirdBusOrder/thr_order_detail/orderIdFk///updateTime///第三方单据详情", - "basicThirdProducts//thr_products/id///updateTime///第三方产品信息","//sup_cert/id///updateTime/filePath//资质证书信息", - "//sup_cert_set/id///updateTime///供应商资质证书设置","companyCert//sup_company/customerId///updateTime///配送企业", - "manufacturerCert//sup_manufacturer/id///updateTime///生产企业","productCert//sup_product/id///updateTime///产品资质信息", - "//udicompany/id///updateTime///国际库医疗器械注册人信息"}; - //"dbDiProducts//productinfo/id///updateTime///DI产品信息", - private int orderNum=0; - - @Resource + private final String[] SYNC_TABLES = { + "entrustAction//basic_entrust_accept/id//////委托验收", "basicProducts//basic_udirel/id///updateTime///耗材字典", "/basicProducts/basic_products/id/uuid/uuid////耗材字典信息详情", + "//company_product_relevance////updateTime///供应商关联信息", + "basicCorp//basic_corp/id///updateTime///往来单位", "typeThird//thr_bustype_origin/id///updateTime///第三方单据类型", + "basicThirdCorp//thr_corp/id///updateTime///第三方往来单位", "//thr_dept/id///updateTime///第三方部门", + "//thr_inv_products/id///updateTime///第三方库存", "basicThirdInv//thr_inv_warehouse/id//////第三方仓库", + "basicThirdBusOrder//thr_order/id///updateTime///第三方业务单据", "/basicThirdBusOrder/thr_order_detail/orderIdFk///updateTime///第三方单据详情", + "basicThirdProducts//thr_products/id///updateTime///第三方产品信息", "//sup_cert/id///updateTime/filePath//资质证书信息", + "//sup_cert_set/id///updateTime///供应商资质证书设置", "companyCert//sup_company/customerId///updateTime///配送企业", + "manufacturerCert//sup_manufacturer/id///updateTime///生产企业", "productCert//sup_product/id///updateTime///产品资质信息", + "//udicompany/id///updateTime///国际库医疗器械注册人信息"}; + //"dbDiProducts//productinfo/id///updateTime///DI产品信息", + private int orderNum = 0; + + @Resource private SystemParamConfigService systemParamConfigService; - @Resource - private JdbcTemplate jdbcTemplate; + @Resource + private JdbcTemplate jdbcTemplate; - @Resource - private DbDao dbDao; + @Resource + private DbDao dbDao; - @Resource + @Resource private ScheduledDao scheduledDao; - @Override - public BaseResponse taskList(HttpServletRequest request,Map params) { - Map map = new HashMap(); - map.put("sql", "select id as taskId from basic_export_status where receiveStatus='0'"); - List> list = dbDao.list(map); - return ResultVOUtils.success(list); - } - - @Override - public BaseResponse download(HttpServletRequest request,Map params) { - Map map = dbDao.get("select * from basic_export_status where id='"+params.get("taskId")+"'"); - if(map==null) - ResultVOUtils.error(9000, "下载任务不存在"); - String json = FileUtils.readFileAll(map.get("cacheFilePath").toString()); - - JSONObject object = JSON.parseObject(json); - if(object!=null&&object.getInteger("code")!=null) { - BaseResponse response = JSON.parseObject(json,BaseResponse.class); - return response; - } - - return ResultVOUtils.success(object); - } - - - /*UDI系统上传自助平台*/ - @Override + @Override + public BaseResponse taskList(HttpServletRequest request, Map params) { + Map map = new HashMap(); + map.put("sql", "select id as taskId from basic_export_status where receiveStatus='0'"); + List> list = dbDao.list(map); + return ResultVOUtils.success(list); + } + + @Override + public BaseResponse download(HttpServletRequest request, Map params) { + Map map = dbDao.get("select * from basic_export_status where id='" + params.get("taskId") + "'"); + if (map == null) + ResultVOUtils.error(9000, "下载任务不存在"); + String json = FileUtils.readFileAll(map.get("cacheFilePath").toString()); + + JSONObject object = JSON.parseObject(json); + if (object != null && object.getInteger("code") != null) { + BaseResponse response = JSON.parseObject(json, BaseResponse.class); + return response; + } + + return ResultVOUtils.success(object); + } + + + /*UDI系统上传自助平台*/ + @Override public void asyncUdiTask() { - logger.info("UDI管理系统同步任务开始--------"); - asyncDataTask(true); + logger.info("UDI管理系统同步任务开始--------"); + asyncDataTask(true); } + /*自助平台生成任务,不上传*/ - @Override + @Override public void asyncSpsTask() { - logger.info("自助平台同步任务开始--------"); - asyncDataTask(false); - } - - /*拉取前一级中继服务数据*/ - @Async - @Override - public void asyncFetchUdiTask() { - fetchTask(false); - } - @Async - @Override - public void asyncFetchTask() { - fetchTask(true); - - - } - - - private void fetchTask(boolean isIdc) { - String host = getNextHost(); - if(!isIdc) { - Map map = dbDao.get("select * from sync_data_set limit 1"); - if(map!=null&&map.get("syncIp")!=null) - host = map.get("syncIp").toString(); - } - logger.info("fetch from ip:"+host); - if(!StringUtils.isEmpty(host)) { - String result = post(host+"/spssync/common/list",null); - - if(IDCUtils.isJson(result)) { - JSONObject json = JSON.parseObject(result); - if(json!=null&&json.getInteger("code")==20000&&json.getString("data")!=null) { - List list = JSON.parseArray(json.getString("data"), Map.class); - if(list!=null) { - for(Map map:list) { - Map params = new HashMap(); - params.put("taskId", map.get("taskId")); - fetchData(host,params); - } - } - } - } - } - } - - @Async - @Override + logger.info("自助平台同步任务开始--------"); + asyncDataTask(false); + } + + /*拉取前一级中继服务数据*/ + @Async + @Override + public void asyncFetchUdiTask() { + fetchTask(false); + } + + @Async + @Override + public void asyncFetchTask() { + fetchTask(true); + + + } + + + private void fetchTask(boolean isIdc) { + String host = getNextHost(); + if (!isIdc) { + Map map = dbDao.get("select * from sync_data_set limit 1"); + if (map != null && map.get("syncIp") != null) + host = map.get("syncIp").toString(); + } + logger.info("fetch from ip:" + host); + if (!StringUtils.isEmpty(host)) { + String result = post(host + "/spssync/common/list", null); + + if (IDCUtils.isJson(result)) { + JSONObject json = JSON.parseObject(result); + if (json != null && json.getInteger("code") == 20000 && json.getString("data") != null) { + List list = JSON.parseArray(json.getString("data"), Map.class); + if (list != null) { + for (Map map : list) { + Map params = new HashMap(); + params.put("taskId", map.get("taskId")); + fetchData(host, params); + } + } + } + } + } + } + + @Async + @Override public void asyncIdcTask() { - initTable(); - Map map = dbDao.get("select * from idc_var where code='system_type'"); - if(map==null) { - dbDao.save("insert into idc_var (code,content) values ('system_type','IDC')"); - } - } - - - private void asyncDataTask(boolean isUpload) { - initTable(); - Map map = dbDao.get("select * from sync_data_set limit 1"); - if(map!=null&&map.get("downstreamEnable")!=null&&map.get("downstreamEnable").toString().equals("1")) { - if(isUpload) { - if(!(map.get("syncIp")!=null&&!StringUtils.isEmpty(map.get("syncIp").toString()))) { - logger.error("中继服务地址未配置"); - return; - } - } - for(int i=0;i> keyList = dbDao.listKeyMysql(tnames[2]); - String keyColumn = keyList!=null&&keyList.size()>0 ? keyList.get(0).get("columnName").toString() : "id"; - Map map = new HashMap(); - - String sqlWhere = ""; - if(!StringUtils.isEmpty(tnames[6])) { - sqlWhere+=" "+tnames[6]+">=cast('"+lastUpdateTime+"' as datetime)"; - } else { - sqlWhere = "not exists (select fkId from idc_record where type='"+tnames[2]+"' and fkId="+tnames[2]+"."+keyColumn+")"; - map.put("isEnd", "1"); - } - map.put("sqlWhere", sqlWhere); - - map.put("tableKey", tnames[0]); - map.put("tableName", tnames[2]); - map.put("uniqueColumn", tnames[3]); - map.put("masterLinkColumn", tnames[4]); - map.put("subLinkColumn", tnames[5]); - map.put("filePathColumn", tnames[7]); - map.put("dataWhere", tnames[8]); - map.put("messageType", tnames[9]); - map.put("messageName", tnames[9]); - ScheduledRequest scheduledRequest = new ScheduledRequest(); - scheduledRequest.setCronName("sync"+WordUtils.capitalizeFully(tnames[2], new char[]{'_'}).replace("_", "")); - ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); - String cron = scheduledEntity!=null ? scheduledEntity.getCron() : ""; - - if(!StringUtils.isEmpty(cron)) { - CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron); - Date nextTimePoint = cronSequenceGenerator.next(DateUtil.parseDate(lastUpdateTime)); - sync = nextTimePoint.before(nowUpdateTime); - } - if(sync) { - if(syncMasterData(map,isUpload,syncIp)) { - setUpdateTime(tnames[2],DateUtil.formatDate(nowUpdateTime,"yyyy-MM-dd HH:mm:ss")); - } - } - } - } - - - - private String fetchData(String host,Map params) { - OkHttpClient client = new OkHttpClient().newBuilder() - .build(); - MediaType mediaType = MediaType.parse("application/json"); - RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(params)); - Request request = new Request.Builder() - .url(host+"/spssync/common/download") - .method("POST", body) - .addHeader("Content-Type", "application/json") - .build(); - String result = ""; - MultipartFile[] files = {}; - try { - boolean success=true; - Response response = client.newCall(request).execute(); - result = response.body().string(); - JSONObject json = JSONObject.parseObject(result); - - if(isLastLevel()) { - if(json.get("code")!=null) { - success = analyToDB(json.getJSONObject("data"),files); - } else { - success = analyToDB(json,files); - } - if(!success) - logger.info("fetchData-->解析失败"); - } else { - if(json.get("code")!=null) { - syncAddTaskStatus(json.getJSONObject("data"),false,success); - } else { - syncAddTaskStatus(json,false,success); - } - } - - if(success) { - String ret = post(host+"/spssync/common/success",params); - - } - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - logger.error("fetchData-->"+e.getMessage()); - } - return result; - } - - private String post(String url,Map params) { - OkHttpClient client = new OkHttpClient().newBuilder() - .build(); - MediaType mediaType = MediaType.parse("application/json"); - RequestBody body = RequestBody.create(mediaType, ""); - if(params!=null) - body = RequestBody.create(mediaType,JSON.toJSONString(params)); - Request request = new Request.Builder() - .url(url) - .method("POST", body) - .addHeader("Content-Type", "application/json") - .build(); - String result = ""; - try { - Response response = client.newCall(request).execute(); - result = response.body().string(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return result; - } - - - public BaseResponse downlaodSuccess(HttpServletRequest request,Map params) { - String sql = "update basic_export_status set receiveStatus='1' where id='"+params.get("taskId")+"'"; - dbDao.update(sql); - return ResultVOUtils.success(); - } - /****/ - @Override - public BaseResponse receive(HttpServletRequest request, - String content,MultipartFile[] files) { - /*授权检查,后续完善*/ - String apiKey = request.getHeader("apiKey"); - String secretKey = request.getHeader("secretKey"); - /*******/ - JSONObject json = JSONObject.parseObject(content); - - String datePath = DateUtil.formatDate(new Date(),"yyyy-MM-dd"); - String fileName = filePath +datePath+"/"+json.getString("messageType")+"-"+json.getString("messageId")+".udi"; - String backFileName = backFilePath +datePath+"/"+json.getString("messageType")+"-"+json.getString("messageId")+".udi"; - if(!FileUtils.makeDirectory( filePath +datePath)) - IDCUtils.createDirectory(filePath +datePath+"/"); - if(!FileUtils.makeDirectory(backFilePath +datePath)) - IDCUtils.createDirectory(filePath +datePath+"/"); - - FileUtils.SaveFileAs(content, fileName); + initTable(); + Map map = dbDao.get("select * from idc_var where code='system_type'"); + if (map == null) { + dbDao.save("insert into idc_var (code,content) values ('system_type','IDC')"); + } + } + + + private void asyncDataTask(boolean isUpload) { + initTable(); + Map map = dbDao.get("select * from sync_data_set limit 1"); + if (map != null && map.get("downstreamEnable") != null && map.get("downstreamEnable").toString().equals("1")) { + if (isUpload) { + if (!(map.get("syncIp") != null && !StringUtils.isEmpty(map.get("syncIp").toString()))) { + logger.error("中继服务地址未配置"); + return; + } + } + for (int i = 0; i < SYNC_TABLES.length; i++) { + String[] tnames = SYNC_TABLES[i].split("/"); + saveIdcLog("---", "", map.get(tnames[0]) + SYNC_TABLES[i], 0, 0); + if (!StringUtils.isEmpty(tnames[0]) && map != null && map.get(tnames[0]) != null && map.get(tnames[0]).toString().equals("1")) { + String syncIp = map.get("syncIp") != null ? map.get("syncIp").toString() : ""; + syncData(SYNC_TABLES[i], isUpload, syncIp); + } + } + } + } + + private void syncData(String t, boolean isUpload, String syncIp) { + boolean sync = true; + String[] tnames = t.split("/"); + + String lastUpdateTime = getUpdateTime(tnames[2]); + Date nowUpdateTime = new Date(); + if (!StringUtils.isEmpty(tnames[0])) { + + List> keyList = dbDao.listKeyMysql(tnames[2]); + String keyColumn = keyList != null && keyList.size() > 0 ? keyList.get(0).get("columnName").toString() : "id"; + Map map = new HashMap(); + + String sqlWhere = ""; + if (!StringUtils.isEmpty(tnames[6])) { + sqlWhere += " " + tnames[6] + ">=cast('" + lastUpdateTime + "' as datetime)"; + } else { + sqlWhere = "not exists (select fkId from idc_record where type='" + tnames[2] + "' and fkId=" + tnames[2] + "." + keyColumn + ")"; + map.put("isEnd", "1"); + } + map.put("sqlWhere", sqlWhere); + + map.put("tableKey", tnames[0]); + map.put("tableName", tnames[2]); + map.put("uniqueColumn", tnames[3]); + map.put("masterLinkColumn", tnames[4]); + map.put("subLinkColumn", tnames[5]); + map.put("filePathColumn", tnames[7]); + map.put("dataWhere", tnames[8]); + map.put("messageType", tnames[9]); + map.put("messageName", tnames[9]); + ScheduledRequest scheduledRequest = new ScheduledRequest(); + scheduledRequest.setCronName("sync" + WordUtils.capitalizeFully(tnames[2], new char[]{'_'}).replace("_", "")); + ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); + String cron = scheduledEntity != null ? scheduledEntity.getCron() : ""; + + if (!StringUtils.isEmpty(cron)) { + CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron); + Date nextTimePoint = cronSequenceGenerator.next(DateUtil.parseDate(lastUpdateTime)); + sync = nextTimePoint.before(nowUpdateTime); + } + if (sync) { + if (syncMasterData(map, isUpload, syncIp)) { + setUpdateTime(tnames[2], DateUtil.formatDate(nowUpdateTime, "yyyy-MM-dd HH:mm:ss")); + } + } + } + } + + + private String fetchData(String host, Map params) { + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(params)); + Request request = new Request.Builder() + .url(host + "/spssync/common/download") + .method("POST", body) + .addHeader("Content-Type", "application/json") + .build(); + String result = ""; + MultipartFile[] files = {}; + try { + boolean success = true; + Response response = client.newCall(request).execute(); + result = response.body().string(); + JSONObject json = JSONObject.parseObject(result); + + if (isLastLevel()) { + if (json.get("code") != null) { + success = analyToDB(json.getJSONObject("data"), files); + } else { + success = analyToDB(json, files); + } + if (!success) + logger.info("fetchData-->解析失败"); + } else { + if (json.get("code") != null) { + syncAddTaskStatus(json.getJSONObject("data"), false, success); + } else { + syncAddTaskStatus(json, false, success); + } + } + + if (success) { + String ret = post(host + "/spssync/common/success", params); + + } + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + logger.error("fetchData-->" + e.getMessage()); + } + return result; + } + + private String post(String url, Map params) { + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, ""); + if (params != null) + body = RequestBody.create(mediaType, JSON.toJSONString(params)); + Request request = new Request.Builder() + .url(url) + .method("POST", body) + .addHeader("Content-Type", "application/json") + .build(); + String result = ""; + try { + Response response = client.newCall(request).execute(); + result = response.body().string(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return result; + } + + + public BaseResponse downlaodSuccess(HttpServletRequest request, Map params) { + String sql = "update basic_export_status set receiveStatus='1' where id='" + params.get("taskId") + "'"; + dbDao.update(sql); + return ResultVOUtils.success(); + } + + /****/ + @Override + public BaseResponse receive(HttpServletRequest request, + String content, MultipartFile[] files) { + /*授权检查,后续完善*/ + String apiKey = request.getHeader("apiKey"); + String secretKey = request.getHeader("secretKey"); + /*******/ + JSONObject json = JSONObject.parseObject(content); + + String datePath = DateUtil.formatDate(new Date(), "yyyy-MM-dd"); + String fileName = filePath + datePath + "/" + json.getString("messageType") + "-" + json.getString("messageId") + ".udi"; + String backFileName = backFilePath + datePath + "/" + json.getString("messageType") + "-" + json.getString("messageId") + ".udi"; + if (!FileUtils.makeDirectory(filePath + datePath)) + IDCUtils.createDirectory(filePath + datePath + "/"); + if (!FileUtils.makeDirectory(backFilePath + datePath)) + IDCUtils.createDirectory(filePath + datePath + "/"); + + FileUtils.SaveFileAs(content, fileName); FileUtils.SaveFileAs(content, backFileName); - String[] saveFiles= {}; - if(files!=null) { - saveFiles = new String[files.length]; - int n=0; + String[] saveFiles = {}; + if (files != null) { + saveFiles = new String[files.length]; + int n = 0; try { - for(MultipartFile file:files) { - saveFiles[n] = filePath +datePath+"/"+json.getString("messageId")+"/"+file.getOriginalFilename(); - writeFile(file.getBytes(), filePath +datePath+"/"+json.getString("messageId")+"/", file.getOriginalFilename()); - writeFile(file.getBytes(), backFilePath +datePath+"/"+json.getString("messageId")+"/", file.getOriginalFilename()); - n++; - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + for (MultipartFile file : files) { + saveFiles[n] = filePath + datePath + "/" + json.getString("messageId") + "/" + file.getOriginalFilename(); + writeFile(file.getBytes(), filePath + datePath + "/" + json.getString("messageId") + "/", file.getOriginalFilename()); + writeFile(file.getBytes(), backFilePath + datePath + "/" + json.getString("messageId") + "/", file.getOriginalFilename()); + n++; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } - syncAddTaskStatus(json,true,true); + syncAddTaskStatus(json, true, true); /*解析入库*/ - if(isLastLevel()) { - if(!analyToDB(json,files)) - return ResultVOUtils.error(9000, "解析失败"); + if (isLastLevel()) { + if (!analyToDB(json, files)) + return ResultVOUtils.error(9000, "解析失败"); } else { - //需要转发 - if(isRelay()) { - String host = getNextHost(); - String result = relay(request.getHeader("reqNo"),content,saveFiles,host); - if(IDCUtils.isJson(result)) { - BaseResponse object = JSON.parseObject(result,BaseResponse.class); - return object; - } else { - return ResultVOUtils.error(9000, "转发失败"); - } - - } + //需要转发 + if (isRelay()) { + String host = getNextHost(); + String result = relay(request.getHeader("reqNo"), content, saveFiles, host); + if (IDCUtils.isJson(result)) { + BaseResponse object = JSON.parseObject(result, BaseResponse.class); + return object; + } else { + return ResultVOUtils.error(9000, "转发失败"); + } + + } + } + return ResultVOUtils.success(); + } + + + /*数据同步,从数据库获取数据下发或上传下级中继服务*/ + @Override + public BaseResponse send(Map params) { + + return send(params.get("messageType").toString(), params.get("tableName").toString(), params); + + + } + + @Override + public BaseResponse send(String messageType, String tableName, Map params) { + + return ResultVOUtils.error(9999, ""); + } + + + private void saveUploadStatus(Map params) { + Map map = new HashMap(); + map.putAll(params); + map.put("id", CustomUtil.getId()); + map.put("updateTime", new Date()); + Map columns = getColumn("basic_upload_status"); + String sql = "insert into basic_upload_status " + DBAUtils.parseInsert(map, columns); + + dbDao.save(sql); + } + + private void saveExportStatus(Map params) { + Map map = new HashMap(); + map.put("receiveStatus", "0"); + map.putAll(params); + map.put("id", CustomUtil.getId()); + map.put("updateTime", new Date()); + Map columns = getColumn("basic_export_status"); + String sql = "insert into basic_export_status " + DBAUtils.parseInsert(map, columns); + + dbDao.save(sql); + } + + private void saveDownloadStatus(String messageType, String taskId, String remark) { + Map map = new HashMap(); + map.put("id", CustomUtil.getId()); + map.put("taskId", taskId); + map.put("status", "0"); + map.put("type", messageType); + map.put("updateTime", new Date()); + map.put("remark", remark); + Map columns = getColumn("basic_download_status"); + String sql = "insert into basic_download_status " + DBAUtils.parseInsert(map, columns); + + dbDao.save(sql); + } + + private void saveIdcLog(String messageType, String taskId, String content, int offset, int total) { + Map map = new HashMap(); + map.put("id", CustomUtil.getId()); + map.put("taskId", taskId); + map.put("orderNum", orderNum); + map.put("type", messageType); + map.put("createTime", new Date()); + map.put("content", content); + map.put("offset", offset); + map.put("total", total); + Map columns = getColumn("idc_log"); + String sql = "insert into idc_log " + DBAUtils.parseInsert(map, columns); + + dbDao.save(sql); + } + + + private boolean syncMasterData(Map params, boolean isUpload, String syncIp) { + boolean success = false; + String tableName = params.get("tableName").toString(); + String tableKey = params.get("tableKey").toString(); + String[] childs = new String[10]; + int childNum = -1; + /*子表*/ + for (String str : SYNC_TABLES) { + if (str.contains("/" + tableKey + "/")) { + childNum++; + childs[childNum] = str; + } } - return ResultVOUtils.success(); - } - - - /*数据同步,从数据库获取数据下发或上传下级中继服务*/ - @Override - public BaseResponse send(Map params) { - - return send(params.get("messageType").toString(),params.get("tableName").toString(),params); - - - } - @Override - public BaseResponse send(String messageType,String tableName,Map params) { - - return ResultVOUtils.error(9999, ""); - } - - - private void saveUploadStatus(Map params) { - Map map = new HashMap(); - map.putAll(params); - map.put("id", CustomUtil.getId()); - map.put("updateTime", new Date()); - Map columns = getColumn("basic_upload_status"); - String sql = "insert into basic_upload_status "+DBAUtils.parseInsert(map, columns); - - dbDao.save(sql); - } - - private void saveExportStatus(Map params) { - Map map = new HashMap(); - map.put("receiveStatus", "0"); - map.putAll(params); - map.put("id", CustomUtil.getId()); - map.put("updateTime", new Date()); - Map columns = getColumn("basic_export_status"); - String sql = "insert into basic_export_status "+DBAUtils.parseInsert(map, columns); - - dbDao.save(sql); - } - private void saveDownloadStatus(String messageType,String taskId,String remark) { - Map map = new HashMap(); - map.put("id", CustomUtil.getId()); - map.put("taskId", taskId); - map.put("status", "0"); - map.put("type", messageType); - map.put("updateTime", new Date()); - map.put("remark", remark); - Map columns = getColumn("basic_download_status"); - String sql = "insert into basic_download_status "+DBAUtils.parseInsert(map, columns); - - dbDao.save(sql); - } - private void saveIdcLog(String messageType,String taskId,String content,int offset,int total) { - Map map = new HashMap(); - map.put("id", CustomUtil.getId()); - map.put("taskId", taskId); - map.put("orderNum", orderNum); - map.put("type", messageType); - map.put("createTime", new Date()); - map.put("content", content); - map.put("offset", offset); - map.put("total", total); - Map columns = getColumn("idc_log"); - String sql = "insert into idc_log "+DBAUtils.parseInsert(map, columns); - - dbDao.save(sql); - } - - - private boolean syncMasterData(Map params,boolean isUpload,String syncIp) { - boolean success=false; - String tableName = params.get("tableName").toString(); - String tableKey = params.get("tableKey").toString(); - String[] childs = new String[10]; - int childNum = -1; - /*子表*/ - for(String str:SYNC_TABLES) { - if(str.contains("/"+tableKey+"/")) { - childNum++; - childs[childNum] = str; - } - } - Map table = dbDao.getMysql(tableName); - if(!(table!=null&&table.get("tableName")!=null)) - return false; - String sql = "select count(*) from "+tableName; - Map column = getColumn(tableName); - - Map whereParams = new HashMap(); - whereParams.put("sqlWhere", params.get("sqlWhere")); - whereParams.put("dataWhere", params.get("dataWhere")); - - Map map = new HashMap(); - String where = DBAUtils.convertWhere(column,whereParams,""); - sql+=!StringUtils.isEmpty(where) ? " where "+where : ""; - map.put("sql", sql); - int total = dbDao.count(map); - - String isEnd = params.get("isEnd")!=null ? params.get("isEnd").toString() : "0"; - int limit = 50; - String[] files = new String[limit]; - String filePathColumn = params.get("filePathColumn")!=null ? params.get("filePathColumn").toString() : ""; - orderNum++; - String messageType = params.get("messageType").toString(); - saveIdcLog(messageType,"",tableName +">"+where,0,total); - - - if(total>0) { - success = true; - - whereParams.put("page", 0); - whereParams.put("limit", limit); - - List> keyList = dbDao.listKeyMysql(tableName); - String keyColumn = keyList!=null&&keyList.size()>0 ? keyList.get(0).get("columnName").toString() : "id"; - for(int i=0;i data = getData(messageType,tableName,params,whereParams); - String recordSql = ""; - orderNum++; - saveIdcLog(messageType,"",tableName +">"+where,i *limit,total); - if(data.get("data")!=null) { - - List list = JSONObject.parseArray(JSON.toJSONString(data.get("data")), Map.class); - if(list!=null&&list.size()>0) { - for(int m=0;m-1) { - for(int z=0;z childMap = new HashMap<>(); - childMap.put(chidTnames[5], keyValue); - List> childList = getList(chidTnames[2],childMap); - list.get(m).put("tableName"+z, chidTnames[2]); - list.get(m).put("data"+z, childList); - } - } - } - } - data.replace("data", list); - orderNum++; - - if(isUpload) { - String result = ""; - try { - result = relay("",JSON.toJSONString(data),files,syncIp); - } catch (Exception ex) { - - } - if(IDCUtils.isJson(result)) { - JSONObject json = JSON.parseObject(result); - if(json.getInteger("code")==20000) { - if(!StringUtils.isEmpty(recordSql)) { - dbDao.save("insert into idc_record (id,type,fkId,isEnd,createTime) values "+recordSql); - } - saveDownloadStatus(messageType,json.getString("reqNo"),messageType+" 记录数:"+list.size()+"/"+total); - saveIdcLog(messageType,"",tableName +">success",i *limit,total); - } else { - success = false; - logger.error(result); - saveIdcLog(messageType,"",tableName +">"+result,i *limit,total); - } - } else { - success = false; - saveIdcLog(messageType,"",syncIp+":"+tableName +">fail:上传地址未连通",i *limit,total); - } - } else { - if(!StringUtils.isEmpty(recordSql)) { - dbDao.save("insert into idc_record (id,type,fkId,isEnd,createTime) values "+recordSql); - } - saveIdcLog(messageType,"",tableName +">success",i *limit,total); - } - syncAddTaskStatus(data,isUpload,success); - } - } - } - - } - - return success; - } - - /*增加同步任务状态*/ - private void syncAddTaskStatus(Map json,boolean isUpload,boolean success) { - String content = JSON.toJSONString(json); - String datePath = DateUtil.formatDate(new Date(),"yyyy-MM-dd"); - String fileName = filePath +datePath+"/"+json.get("messageType")+"-"+json.get("messageId")+".udi"; - String backFileName = backFilePath +datePath+"/"+json.get("messageType")+"-"+json.get("messageId")+".udi"; - if(!FileUtils.makeDirectory( filePath +datePath)) - IDCUtils.createDirectory(filePath +datePath+"/"); - if(!FileUtils.makeDirectory(backFilePath +datePath)) - IDCUtils.createDirectory(filePath +datePath+"/"); - - FileUtils.SaveFileAs(content, fileName); + Map table = dbDao.getMysql(tableName); + if (!(table != null && table.get("tableName") != null)) + return false; + String sql = "select count(*) from " + tableName; + Map column = getColumn(tableName); + + Map whereParams = new HashMap(); + whereParams.put("sqlWhere", params.get("sqlWhere")); + whereParams.put("dataWhere", params.get("dataWhere")); + + Map map = new HashMap(); + String where = DBAUtils.convertWhere(column, whereParams, ""); + sql += !StringUtils.isEmpty(where) ? " where " + where : ""; + map.put("sql", sql); + int total = dbDao.count(map); + + String isEnd = params.get("isEnd") != null ? params.get("isEnd").toString() : "0"; + int limit = 50; + String[] files = new String[limit]; + String filePathColumn = params.get("filePathColumn") != null ? params.get("filePathColumn").toString() : ""; + orderNum++; + String messageType = params.get("messageType").toString(); + saveIdcLog(messageType, "", tableName + ">" + where, 0, total); + + + if (total > 0) { + success = true; + + whereParams.put("page", 0); + whereParams.put("limit", limit); + + List> keyList = dbDao.listKeyMysql(tableName); + String keyColumn = keyList != null && keyList.size() > 0 ? keyList.get(0).get("columnName").toString() : "id"; + for (int i = 0; i < Math.ceil(total / limit) + 1; i++) { + whereParams.replace("page", i * limit); + whereParams.replace("limit", limit); + Map data = getData(messageType, tableName, params, whereParams); + String recordSql = ""; + orderNum++; + saveIdcLog(messageType, "", tableName + ">" + where, i * limit, total); + if (data.get("data") != null) { + + List list = JSONObject.parseArray(JSON.toJSONString(data.get("data")), Map.class); + if (list != null && list.size() > 0) { + for (int m = 0; m < list.size(); m++) { + if (!StringUtils.isEmpty(filePathColumn) && list.get(m).get(filePathColumn) != null) { + files[m] = list.get(m).get(filePathColumn).toString(); + } + recordSql += !StringUtils.isEmpty(recordSql) ? "," : ""; + recordSql += "('" + UUID.randomUUID().toString().replaceAll("-", "") + "','" + tableName + "','" + list.get(m).get(keyColumn) + "','" + isEnd + "',now())"; + if (childNum > -1) { + for (int z = 0; z < childNum + 1; z++) { + String[] chidTnames = childs[z].split("/"); + if (!StringUtils.isEmpty(chidTnames[4]) && !StringUtils.isEmpty(chidTnames[5])) { + String keyValue = list.get(i).get(chidTnames[4]).toString(); + Map childMap = new HashMap<>(); + childMap.put(chidTnames[5], keyValue); + List> childList = getList(chidTnames[2], childMap); + list.get(m).put("tableName" + z, chidTnames[2]); + list.get(m).put("data" + z, childList); + } + } + } + } + data.replace("data", list); + orderNum++; + + if (isUpload) { + String result = ""; + try { + result = relay("", JSON.toJSONString(data), files, syncIp); + } catch (Exception ex) { + + } + if (IDCUtils.isJson(result)) { + JSONObject json = JSON.parseObject(result); + if (json.getInteger("code") == 20000) { + if (!StringUtils.isEmpty(recordSql)) { + dbDao.save("insert into idc_record (id,type,fkId,isEnd,createTime) values " + recordSql); + } + saveDownloadStatus(messageType, json.getString("reqNo"), messageType + " 记录数:" + list.size() + "/" + total); + saveIdcLog(messageType, "", tableName + ">success", i * limit, total); + } else { + success = false; + logger.error(result); + saveIdcLog(messageType, "", tableName + ">" + result, i * limit, total); + } + } else { + success = false; + saveIdcLog(messageType, "", syncIp + ":" + tableName + ">fail:上传地址未连通", i * limit, total); + } + } else { + if (!StringUtils.isEmpty(recordSql)) { + dbDao.save("insert into idc_record (id,type,fkId,isEnd,createTime) values " + recordSql); + } + saveIdcLog(messageType, "", tableName + ">success", i * limit, total); + } + syncAddTaskStatus(data, isUpload, success); + } + } + } + + } + + return success; + } + + /*增加同步任务状态*/ + private void syncAddTaskStatus(Map json, boolean isUpload, boolean success) { + String content = JSON.toJSONString(json); + String datePath = DateUtil.formatDate(new Date(), "yyyy-MM-dd"); + String fileName = filePath + datePath + "/" + json.get("messageType") + "-" + json.get("messageId") + ".udi"; + String backFileName = backFilePath + datePath + "/" + json.get("messageType") + "-" + json.get("messageId") + ".udi"; + if (!FileUtils.makeDirectory(filePath + datePath)) + IDCUtils.createDirectory(filePath + datePath + "/"); + if (!FileUtils.makeDirectory(backFilePath + datePath)) + IDCUtils.createDirectory(filePath + datePath + "/"); + + FileUtils.SaveFileAs(content, fileName); FileUtils.SaveFileAs(content, backFileName); - + //type,transportType,superiorService,subordinateService,cacheFilePath - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("type", json.get("messageType")); map.put("transportType", isUpload ? "1" : "2"); map.put("scheduleType", isUpload ? "1" : "0"); map.put("taskId", json.get("messageId")); map.put("cacheFilePath", fileName); map.put("status", success ? "1" : "0"); - + saveExportStatus(map); - } - - - - /*解析到数据库*/ - private boolean analyToDB(JSONObject jsonObject,MultipartFile[] files) { - - if(jsonObject.get("data")!=null) { - logger.error("json>>>>>>>>>"); - String tableName = ""; - String uniqueColumn = ""; - Object object = jsonObject.get("data"); - List> list = new ArrayList<>(); - if(object instanceof List) { - list = jsonObject.getObject("data", List.class); - tableName = jsonObject.getString("tableName"); - uniqueColumn = jsonObject.getString("uniqueColumn"); - } else { - JSONObject obj = jsonObject.getJSONObject("data"); - list = obj.getObject("data", List.class); - tableName = obj.getString("tableName"); - uniqueColumn = obj.getString("uniqueColumn"); - } - logger.error("table-->"+tableName); - if(!StringUtils.isEmpty(tableName)) { - return analyData(tableName,uniqueColumn,list); - } else { - logger.error("数据格式错误:无数据标记"); - } - //图片处理 - } - return false; - } - - /*按表名解析数据到数据库,子表暂未处理*/ - private boolean analyData(String tableName,String uniqueColumn,List> list) { - logger.error("ana-->"+tableName+"--list-->"+JSON.toJSONString(list)); - String tName = DBAUtils.tableRealName(tableName); - String sql="replace "+tName +"("; - String del="delete from "+tName+" where "; - String upd="update "+tName+" set "; - String[] keyColumn=new String[30]; - String[] keyDataType=new String[30]; - List> columnList = dbDao.listColumnsMysql(tName); - boolean result = false; - int key=0; - int col=0; - for(int i=0;i0) - sql+=","; - columnList.get(i).put("attrName", columnList.get(i).get("columnName")); - sql+= columnList.get(i).get("columnName").toString() ; - col++; - if(uniqueColumn!=null&&columnList.get(i).get("columnKey")!=null&&(","+uniqueColumn+",").contains(","+columnList.get(i).get("columnName")+",")) { - keyColumn[key] = columnList.get(i).get("columnName").toString(); - keyDataType[key]=columnList.get(i).get("dataType").toString().contains("char") ? "C" : columnList.get(i).get("dataType").toString().contains("date") ? "D" : "N"; - key++; - } else { - if(columnList.get(i).get("columnKey")!=null&&!StringUtils.isEmpty(columnList.get(i).get("columnKey").toString())&& - ("PRI".contains(columnList.get(i).get("columnKey").toString()))) { - keyColumn[key] = columnList.get(i).get("columnName").toString(); - keyDataType[key]=columnList.get(i).get("dataType").toString().contains("char") ? "C" : columnList.get(i).get("dataType").toString().contains("date") ? "D" : "N"; - key++; - } - } - } - sql+=") values "; - - if(list!=null&&list.size()>0) { - int n = 0; - int d = 0; - for(int i=0;i0 ? "," : ""; - sql+="("; - int m = 0; - int h = 0; - for(int k=0;k0 ? "," : ""; - boolean ups = list.get(i).containsKey(attrName) ? true : false ; - updateSet+=ups&&h>0 ? "," : ""; - updateSet+=ups ? columnList.get(k).get("columnName")+"=" : ""; - if(StringUtils.isEmpty(value)) { - sql+="null"; - updateSet+=ups ? "null" : ""; - } else { - if(dataType.equals("C")) { - sql+="'"+value+"'"; - updateSet+=ups ? "'"+value+"'" : ""; - } else if (dataType.equals("N")) { - sql+=value; - updateSet+=ups ? value : ""; - } else { - Date date = new Date(); - date = StringUtils.isNumeric(value) ? new Date(Long.valueOf(value)) : IDCUtils.parseDate(value); - String str = DateUtils.format(date, "yyyy-MM-dd HH:mm:ss"); - sql+="cast('"+str+"' as datetime)"; - updateSet+=ups ? "cast('"+str+"' as datetime)" : ""; - } - } - m++; - if(operateMode.equals("U")&&!StringUtils.isEmpty(updateSet)&&!StringUtils.isEmpty(updateWhere)) - dbDao.update(upd + updateSet+" where "+updateWhere); - h+=ups ? 1 : 0; - } - - sql+=")"; - n++; - } - - for(int m=0;m<30;m++) { - if(list.get(i).get("tableName"+m)!=null&&list.get(i).get("data"+m)!=null) { - Object obj = list.get(i).get("data"+m); - List> chList = new ArrayList>(); - for (Object o : (List) obj) { - chList.add((Map)o); - } - analyData(list.get(i).get("tableName"+m).toString(),"",chList); - } else { - break; - } - } - - } - if(n>0) { - result = (dbDao.save(sql)>0); - if(!result) - logger.error(tableName+"-->fetchSave Fail"); - } - - } - return result; - } - - - /*转发下级或上级中继服务*/ - private String relay(String reqNo,String content,String[] files,String ip) { - String host = ip; - String result=""; - if(StringUtils.isEmpty(reqNo)) - reqNo = UUID.randomUUID().toString(); - if(!StringUtils.isEmpty(host)) { - host+="/spssync/common/upload" ; - OkHttpClient client = new OkHttpClient().newBuilder() - .connectTimeout(30, TimeUnit.SECONDS)//设置连接超时时间 - .readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间 - .build(); - MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); - - MultipartBody.Builder builder = new MultipartBody.Builder(); - builder.setType(MultipartBody.FORM); - String fileType = "application/octet-stream"; - for (int i = 0; i < files.length; i++) { - if(!StringUtils.isEmpty(files[i])) { - File file = new File(files[i]); - builder.addFormDataPart("files",files[i], - RequestBody.create(MediaType.parse(fileType), file)); - } - } - builder.addFormDataPart("content", content); - RequestBody body = builder.build(); - - Request req = new Request.Builder() - .url(host) - .method("POST", body) - .addHeader("Content-Type", "application/x-www-form-urlencoded") - .addHeader("format", "json") - .addHeader("apiKey", apiKey) - .addHeader("secretKey", apiSecret) - .addHeader("reqNo", reqNo) - .addHeader("timestamp",DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) - .addHeader("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept") - .build(); - try { - Response response = client.newCall(req).execute(); - result=response.body().string(); - log.debug("result--->"+result); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } else { - log.debug("未配置中继服务地址"); - } - return result; - } - - /*获取转发服务地址,当前值允许单向,只使用参数upper_server_ip*/ - private String getNextHost() { - SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey("upper_server_ip"); - String host = systemParamConfigEntity!=null ? systemParamConfigEntity.getParamValue() : ""; - host = !StringUtils.isEmpty(host)&&host.substring(host.length() -1).equals("/") ? host.substring(0,host.length() -1) : host; - return host; - } - - /*判断是否需要解析入库,中继服务只转发*/ - private boolean isLastLevel() { - Map map = dbDao.get("select * from idc_var where code='system_type'"); - return !(map!=null&&map.get("content")!=null&&map.get("content").toString().equals("IDC")) ; - } - - /*是否需要转发*/ - private boolean isRelay() { - String relayHost = getNextHost(); - return StringUtils.isEmpty(relayHost) ? false : true; - } - - - private List> getList(String tableName,Map params) { - String sql = "select "+tableName+".*,'A' as operateMode from "+tableName; - Map column = getColumn(tableName); - - String where = DBAUtils.convertWhere(column,params,""); - sql+=!StringUtils.isEmpty(where) ? " where "+where : ""; - Map map = new HashMap(); - map.putAll(params); - map.put("sql", sql); - List> list = dbDao.list(map); - return list; - } - /*由表名获取数据*/ - private Map getData(String messageType,String tableName,Map params,Map whereParams) { - List> list = getList(tableName,whereParams); - Map map = new HashMap(); - map.put("messageId", UUID.randomUUID().toString().replaceAll("-", "")); - map.put("messageType", messageType); - map.put("apiCode", "common"); - map.put("tableName", DBAUtils.tableAliasName(tableName)); - map.put("uniqueColumn", params.get("uniqueColumn")); - map.put("filePathColumn", params.get("filePathColumn")); - map.put("sendTime", new Date()); - map.put("version", "1.0"); - map.put("total", list.size()); - map.put("data", list); - return map; - } - - - - - private SystemParamConfigEntity getSystemParamConfig(String paramKey,String paramName,String defaultValue,String paramExplain) { - SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey(paramKey); - if(systemParamConfigEntity==null||(systemParamConfigEntity!=null&&StringUtils.isEmpty(systemParamConfigEntity.getParamKey()))) { - systemParamConfigEntity = new SystemParamConfigEntity(); - systemParamConfigEntity.setParamKey(paramKey); - systemParamConfigEntity.setParamName(paramName); - systemParamConfigEntity.setParentId(0); - systemParamConfigEntity.setParamValue(defaultValue); - systemParamConfigEntity.setParamExplain(paramExplain); - systemParamConfigEntity.setParamStatus(1); - systemParamConfigEntity.setParamType(0); - systemParamConfigService.insert(systemParamConfigEntity); - } - System.out.print(JSON.toJSONString(systemParamConfigEntity)+"\n"); - return systemParamConfigEntity; - } - private void writeFile(byte[] file, String filePath, String fileName) throws Exception { - File targetFile = new File(filePath); - if (!targetFile.exists()) { - targetFile.mkdirs(); - } - FileOutputStream out = new FileOutputStream(filePath + fileName); - out.write(file); - out.flush(); - out.close(); - } - - - /*把列数据转Map*/ - private Map getColumn(String tableName) { - Map map = new HashMap(); - List> columnList = new ArrayList>(); - String columnKey=""; - String[] tnames = tableName.split(","); - for(String tname:tnames) { - columnList = dbDao.listColumnsMysql(tname); - if(columnList!=null&&columnList.size()>0) { - for(int i=0;i column = new HashMap(); - String dataType =columnList.get(i).get("dataType").toLowerCase().contains("char") || columnList.get(i).get("dataType").toLowerCase().contains("text")? - "C" : columnList.get(i).get("dataType").toLowerCase().contains("date") ? "D" : "N" ;; - String attrName = columnList.get(i).get("columnName").toString(); - if(map.get(attrName)==null) { - column.put("tableName", tname); - column.put("dataType", dataType); - column.put("columnName", columnList.get(i).get("columnName")); - if(columnList.get(i).get("columnKey")!=null) { - column.put("columnKey", columnList.get(i).get("columnKey")); - } else { - column.put("columnKey","N"); - } - if(map.get(attrName)!=null) { - map.put(tname.toLowerCase()+"."+attrName, column); - } else { - map.put(attrName, column); - } - } - } - } - } - return map; - } - - - private String getUpdateTime(String id) { - String updateTime = ""; - Map map = dbDao.get("select * from idc_status where id='"+id+"'"); - if(map!=null&&map.get("id")!=null) { - updateTime = DateUtil.formatDate(IDCUtils.parseDate(map.get("statusTime").toString()),"yyyy-MM-dd HH:mm:ss"); - } else { - updateTime = "2000-01-01 00:00:00"; - String sql = "insert into idc_status (id,statusTime) values ('"+id+"',cast('"+updateTime+"' as datetime))"; - dbDao.save(sql); - } - return updateTime; - } - private String setUpdateTime(String id,String updateTime) { - String sql = ""; - Map map = dbDao.get("select * from idc_status where id='"+id+"'"); - if(map!=null&&map.get("id")!=null) { - sql = "update idc_status set statusTime=cast('"+updateTime+"' as datetime) where id='"+id+"'"; - dbDao.update(sql); - } else { - sql = "insert into idc_status (id,statusTime) values ('"+id+"',cast('"+updateTime+"' as datetime))"; - dbDao.save(sql); - } - return updateTime; - } - private void initTable() { - alterTable("idc_var","create table idc_var (code varchar(60),content varchar(200),PRIMARY KEY (code))"); - alterTable("idc_status","create table idc_status (id varchar(36),statusTime datetime,PRIMARY KEY (id))"); - alterTable("idc_record","create table idc_record (id varchar(36),type varchar(60),fkId varchar(36),isEnd char(1),createTime datetime,primary key (id),INDEX i_idc_record_fk_id (fkId),INDEX i_idc_record_create_time (createTime))"); - alterTable("idc_log","create table idc_log (id varchar(36),orderNum int,taskId varchar(60),type varchar(60),content varchar(2000),offset int,total int,createTime datetime,PRIMARY KEY (id))"); - dbDao.delete("delete from idc_record where createTime map = dbDao.getMysql(tableName); - if(!(map!=null&&map.get("tableName")!=null)) { - jdbcTemplate.execute(sql); - } - - } - + } + + + /*解析到数据库*/ + private boolean analyToDB(JSONObject jsonObject, MultipartFile[] files) { + + if (jsonObject.get("data") != null) { + logger.error("json>>>>>>>>>"); + String tableName = ""; + String uniqueColumn = ""; + Object object = jsonObject.get("data"); + List> list = new ArrayList<>(); + if (object instanceof List) { + list = jsonObject.getObject("data", List.class); + tableName = jsonObject.getString("tableName"); + uniqueColumn = jsonObject.getString("uniqueColumn"); + } else { + JSONObject obj = jsonObject.getJSONObject("data"); + list = obj.getObject("data", List.class); + tableName = obj.getString("tableName"); + uniqueColumn = obj.getString("uniqueColumn"); + } + logger.error("table-->" + tableName); + if (!StringUtils.isEmpty(tableName)) { + return analyData(tableName, uniqueColumn, list); + } else { + logger.error("数据格式错误:无数据标记"); + } + //图片处理 + } + return false; + } + + /*按表名解析数据到数据库,子表暂未处理*/ + private boolean analyData(String tableName, String uniqueColumn, List> list) { + logger.error("ana-->" + tableName + "--list-->" + JSON.toJSONString(list)); + String tName = DBAUtils.tableRealName(tableName); + String sql = "replace " + tName + "("; + String del = "delete from " + tName + " where "; + String upd = "update " + tName + " set "; + String[] keyColumn = new String[30]; + String[] keyDataType = new String[30]; + List> columnList = dbDao.listColumnsMysql(tName); + boolean result = false; + int key = 0; + int col = 0; + for (int i = 0; i < columnList.size(); i++) { + if (col > 0) + sql += ","; + columnList.get(i).put("attrName", columnList.get(i).get("columnName")); + sql += columnList.get(i).get("columnName").toString(); + col++; + if (uniqueColumn != null && columnList.get(i).get("columnKey") != null && ("," + uniqueColumn + ",").contains("," + columnList.get(i).get("columnName") + ",")) { + keyColumn[key] = columnList.get(i).get("columnName").toString(); + keyDataType[key] = columnList.get(i).get("dataType").toString().contains("char") ? "C" : columnList.get(i).get("dataType").toString().contains("date") ? "D" : "N"; + key++; + } else { + if (columnList.get(i).get("columnKey") != null && !StringUtils.isEmpty(columnList.get(i).get("columnKey").toString()) && + ("PRI".contains(columnList.get(i).get("columnKey").toString()))) { + keyColumn[key] = columnList.get(i).get("columnName").toString(); + keyDataType[key] = columnList.get(i).get("dataType").toString().contains("char") ? "C" : columnList.get(i).get("dataType").toString().contains("date") ? "D" : "N"; + key++; + } + } + } + sql += ") values "; + + if (list != null && list.size() > 0) { + int n = 0; + int d = 0; + for (int i = 0; i < list.size(); i++) { + String operateMode = list.get(i).get("operateMode") != null ? list.get(i).get("operateMode").toString() : "A"; + String updateWhere = ""; + String updateSet = ""; + if ("A,D,U".contains(operateMode)) { + for (int z = 0; z < keyColumn.length; z++) { + if (list.get(i).get(keyColumn[z]) != null && !StringUtils.isEmpty(list.get(i).get(keyColumn[z]).toString())) { + updateWhere += !StringUtils.isEmpty(updateWhere) ? " and " : " "; + String value = list.get(i).get(keyColumn[z]) != null ? list.get(i).get(keyColumn[z]).toString() : ""; + value = keyDataType[z].equals("D") ? "cast('" + DateUtil.formatDate(IDCUtils.parseDate(value), "yyyy-MM-dd HH:mm:ss") + "' as datetime)" : value; + updateWhere += keyColumn[z] + " = " + (keyDataType[z].equals("C") ? "'" : "") + value + (keyDataType[z].equals("C") ? "'" : ""); + } + } + + if ("A,D".contains(operateMode) && !StringUtils.isEmpty(updateWhere)) + dbDao.delete(del + updateWhere); + } + + /*A 新增 U 更新*/ + if ("A,U".contains(operateMode)) { + sql += n > 0 ? "," : ""; + sql += "("; + int m = 0; + int h = 0; + for (int k = 0; k < columnList.size(); k++) { + String attrName = columnList.get(k).get("attrName"); + String value = list.get(i).get(attrName) != null ? list.get(i).get(attrName).toString() : ""; + value = DBAUtils.escape(value); + String dataType = columnList.get(k).get("dataType").toLowerCase().contains("char") || columnList.get(k).get("dataType").toLowerCase().contains("text") ? + "C" : columnList.get(k).get("dataType").toLowerCase().contains("date") ? "D" : "N"; + + sql += m > 0 ? "," : ""; + boolean ups = list.get(i).containsKey(attrName) ? true : false; + updateSet += ups && h > 0 ? "," : ""; + updateSet += ups ? columnList.get(k).get("columnName") + "=" : ""; + if (StringUtils.isEmpty(value)) { + sql += "null"; + updateSet += ups ? "null" : ""; + } else { + if (dataType.equals("C")) { + sql += "'" + value + "'"; + updateSet += ups ? "'" + value + "'" : ""; + } else if (dataType.equals("N")) { + sql += value; + updateSet += ups ? value : ""; + } else { + Date date = new Date(); + date = StringUtils.isNumeric(value) ? new Date(Long.valueOf(value)) : IDCUtils.parseDate(value); + String str = DateUtils.format(date, "yyyy-MM-dd HH:mm:ss"); + sql += "cast('" + str + "' as datetime)"; + updateSet += ups ? "cast('" + str + "' as datetime)" : ""; + } + } + m++; + if (operateMode.equals("U") && !StringUtils.isEmpty(updateSet) && !StringUtils.isEmpty(updateWhere)) + dbDao.update(upd + updateSet + " where " + updateWhere); + h += ups ? 1 : 0; + } + + sql += ")"; + n++; + } + + for (int m = 0; m < 30; m++) { + if (list.get(i).get("tableName" + m) != null && list.get(i).get("data" + m) != null) { + Object obj = list.get(i).get("data" + m); + List> chList = new ArrayList>(); + for (Object o : (List) obj) { + chList.add((Map) o); + } + analyData(list.get(i).get("tableName" + m).toString(), "", chList); + } else { + break; + } + } + + } + if (n > 0) { + result = (dbDao.save(sql) > 0); + if (!result) + logger.error(tableName + "-->fetchSave Fail"); + } + + } + return result; + } + + + /*转发下级或上级中继服务*/ + private String relay(String reqNo, String content, String[] files, String ip) { + String host = ip; + String result = ""; + if (StringUtils.isEmpty(reqNo)) + reqNo = UUID.randomUUID().toString(); + if (!StringUtils.isEmpty(host)) { + host += "/spssync/common/upload"; + OkHttpClient client = new OkHttpClient().newBuilder() + .connectTimeout(30, TimeUnit.SECONDS)//设置连接超时时间 + .readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间 + .build(); + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); + + MultipartBody.Builder builder = new MultipartBody.Builder(); + builder.setType(MultipartBody.FORM); + String fileType = "application/octet-stream"; + for (int i = 0; i < files.length; i++) { + if (!StringUtils.isEmpty(files[i])) { + File file = new File(files[i]); + builder.addFormDataPart("files", files[i], + RequestBody.create(MediaType.parse(fileType), file)); + } + } + builder.addFormDataPart("content", content); + RequestBody body = builder.build(); + + Request req = new Request.Builder() + .url(host) + .method("POST", body) + .addHeader("Content-Type", "application/x-www-form-urlencoded") + .addHeader("format", "json") + .addHeader("apiKey", apiKey) + .addHeader("secretKey", apiSecret) + .addHeader("reqNo", reqNo) + .addHeader("timestamp", DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) + .addHeader("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept") + .build(); + try { + Response response = client.newCall(req).execute(); + result = response.body().string(); + log.debug("result--->" + result); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } else { + log.debug("未配置中继服务地址"); + } + return result; + } + + /*获取转发服务地址,当前值允许单向,只使用参数upper_server_ip*/ + private String getNextHost() { + SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey("upper_server_ip"); + String host = systemParamConfigEntity != null ? systemParamConfigEntity.getParamValue() : ""; + host = !StringUtils.isEmpty(host) && host.substring(host.length() - 1).equals("/") ? host.substring(0, host.length() - 1) : host; + return host; + } + + /*判断是否需要解析入库,中继服务只转发*/ + private boolean isLastLevel() { + Map map = dbDao.get("select * from idc_var where code='system_type'"); + return !(map != null && map.get("content") != null && map.get("content").toString().equals("IDC")); + } + + /*是否需要转发*/ + private boolean isRelay() { + String relayHost = getNextHost(); + return StringUtils.isEmpty(relayHost) ? false : true; + } + + + private List> getList(String tableName, Map params) { + String sql = "select " + tableName + ".*,'A' as operateMode from " + tableName; + Map column = getColumn(tableName); + + String where = DBAUtils.convertWhere(column, params, ""); + sql += !StringUtils.isEmpty(where) ? " where " + where : ""; + Map map = new HashMap(); + map.putAll(params); + map.put("sql", sql); + List> list = dbDao.list(map); + return list; + } + + /*由表名获取数据*/ + private Map getData(String messageType, String tableName, Map params, Map whereParams) { + List> list = getList(tableName, whereParams); + Map map = new HashMap(); + map.put("messageId", UUID.randomUUID().toString().replaceAll("-", "")); + map.put("messageType", messageType); + map.put("apiCode", "common"); + map.put("tableName", DBAUtils.tableAliasName(tableName)); + map.put("uniqueColumn", params.get("uniqueColumn")); + map.put("filePathColumn", params.get("filePathColumn")); + map.put("sendTime", new Date()); + map.put("version", "1.0"); + map.put("total", list.size()); + map.put("data", list); + return map; + } + + + private SystemParamConfigEntity getSystemParamConfig(String paramKey, String paramName, String defaultValue, String paramExplain) { + SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey(paramKey); + if (systemParamConfigEntity == null || (systemParamConfigEntity != null && StringUtils.isEmpty(systemParamConfigEntity.getParamKey()))) { + systemParamConfigEntity = new SystemParamConfigEntity(); + systemParamConfigEntity.setParamKey(paramKey); + systemParamConfigEntity.setParamName(paramName); + systemParamConfigEntity.setParentId(0); + systemParamConfigEntity.setParamValue(defaultValue); + systemParamConfigEntity.setParamExplain(paramExplain); + systemParamConfigEntity.setParamStatus(1); + systemParamConfigEntity.setParamType(0); + systemParamConfigService.insert(systemParamConfigEntity); + } + System.out.print(JSON.toJSONString(systemParamConfigEntity) + "\n"); + return systemParamConfigEntity; + } + + private void writeFile(byte[] file, String filePath, String fileName) throws Exception { + File targetFile = new File(filePath); + if (!targetFile.exists()) { + targetFile.mkdirs(); + } + FileOutputStream out = new FileOutputStream(filePath + fileName); + out.write(file); + out.flush(); + out.close(); + } + + + /*把列数据转Map*/ + private Map getColumn(String tableName) { + Map map = new HashMap(); + List> columnList = new ArrayList>(); + String columnKey = ""; + String[] tnames = tableName.split(","); + for (String tname : tnames) { + columnList = dbDao.listColumnsMysql(tname); + if (columnList != null && columnList.size() > 0) { + for (int i = 0; i < columnList.size(); i++) { + Map column = new HashMap(); + String dataType = columnList.get(i).get("dataType").toLowerCase().contains("char") || columnList.get(i).get("dataType").toLowerCase().contains("text") ? + "C" : columnList.get(i).get("dataType").toLowerCase().contains("date") ? "D" : "N"; + ; + String attrName = columnList.get(i).get("columnName").toString(); + if (map.get(attrName) == null) { + column.put("tableName", tname); + column.put("dataType", dataType); + column.put("columnName", columnList.get(i).get("columnName")); + if (columnList.get(i).get("columnKey") != null) { + column.put("columnKey", columnList.get(i).get("columnKey")); + } else { + column.put("columnKey", "N"); + } + if (map.get(attrName) != null) { + map.put(tname.toLowerCase() + "." + attrName, column); + } else { + map.put(attrName, column); + } + } + } + } + } + return map; + } + + + private String getUpdateTime(String id) { + String updateTime = ""; + Map map = dbDao.get("select * from idc_status where id='" + id + "'"); + if (map != null && map.get("id") != null) { + updateTime = DateUtil.formatDate(IDCUtils.parseDate(map.get("statusTime").toString()), "yyyy-MM-dd HH:mm:ss"); + } else { + updateTime = "2000-01-01 00:00:00"; + String sql = "insert into idc_status (id,statusTime) values ('" + id + "',cast('" + updateTime + "' as datetime))"; + dbDao.save(sql); + } + return updateTime; + } + + private String setUpdateTime(String id, String updateTime) { + String sql = ""; + Map map = dbDao.get("select * from idc_status where id='" + id + "'"); + if (map != null && map.get("id") != null) { + sql = "update idc_status set statusTime=cast('" + updateTime + "' as datetime) where id='" + id + "'"; + dbDao.update(sql); + } else { + sql = "insert into idc_status (id,statusTime) values ('" + id + "',cast('" + updateTime + "' as datetime))"; + dbDao.save(sql); + } + return updateTime; + } + + private void initTable() { + alterTable("idc_var", "create table idc_var (code varchar(60),content varchar(200),PRIMARY KEY (code))"); + alterTable("idc_status", "create table idc_status (id varchar(36),statusTime datetime,PRIMARY KEY (id))"); + alterTable("idc_record", "create table idc_record (id varchar(36),type varchar(60),fkId varchar(36),isEnd char(1),createTime datetime,primary key (id),INDEX i_idc_record_fk_id (fkId),INDEX i_idc_record_create_time (createTime))"); + alterTable("idc_log", "create table idc_log (id varchar(36),orderNum int,taskId varchar(60),type varchar(60),content varchar(2000),offset int,total int,createTime datetime,PRIMARY KEY (id))"); + dbDao.delete("delete from idc_record where createTime map = dbDao.getMysql(tableName); + if (!(map != null && map.get("tableName") != null)) { + jdbcTemplate.execute(sql); + } + + } + } diff --git a/src/main/java/com/glxp/api/service/inout/impl/IoOrderServiceImpl.java b/src/main/java/com/glxp/api/service/inout/impl/IoOrderServiceImpl.java index ecacc744..3f01e2b5 100644 --- a/src/main/java/com/glxp/api/service/inout/impl/IoOrderServiceImpl.java +++ b/src/main/java/com/glxp/api/service/inout/impl/IoOrderServiceImpl.java @@ -547,6 +547,10 @@ public class IoOrderServiceImpl implements IoOrderService { filterOrderRequest.setStatuses(Arrays.asList(1, 3, 5, 10, 7)); filterOrderRequest.setDealStatuses(Arrays.asList(1, 2, 3, 4)); break; + case Constant.ORDER_STATUS_WAIT_AUDITED: + filterOrderRequest.setStatuses(Arrays.asList(10, 7)); + filterOrderRequest.setDealStatuses(Arrays.asList(3, 4)); + break; default: break; } diff --git a/src/main/java/com/glxp/api/task/AsyncSpsTask.java b/src/main/java/com/glxp/api/task/AsyncSpsTask.java index 73deacca..314fefea 100644 --- a/src/main/java/com/glxp/api/task/AsyncSpsTask.java +++ b/src/main/java/com/glxp/api/task/AsyncSpsTask.java @@ -36,7 +36,7 @@ public class AsyncSpsTask implements SchedulingConfigurer { scheduledRequest.setCronName("syncIdcSps"); logger.info("syncIdcSps----------------"); ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); - String cron = scheduledEntity != null ? scheduledEntity.getCron() : "* 0/30 * * * ?"; + String cron = scheduledEntity != null ? scheduledEntity.getCron() : "0 0/1 * * * ?"; if (cron.isEmpty()) { logger.error("cron is null");