From 3495384ef51cec9309b420a5ad2f4a9a975d28e1 Mon Sep 17 00:00:00 2001 From: anthonywj Date: Fri, 24 Mar 2023 10:54:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/glxp/api/dao/idc/DbDao.java | 89 +++ .../api/idc/controller/IdcController.java | 70 ++ .../com/glxp/api/idc/service/IdcService.java | 30 + .../api/idc/service/impl/IdcServiceImpl.java | 747 ++++++++++++++++++ .../com/glxp/api/idc/thread/AsyncIdcTask.java | 53 ++ .../java/com/glxp/api/idc/utils/DBAUtils.java | 400 ++++++++++ .../java/com/glxp/api/idc/utils/IDCUtils.java | 110 +++ src/main/resources/application-dev.yml | 3 + .../resources/mybatis/mapper/idc/DbMapper.xml | 31 + 9 files changed, 1533 insertions(+) create mode 100644 src/main/java/com/glxp/api/dao/idc/DbDao.java create mode 100644 src/main/java/com/glxp/api/idc/controller/IdcController.java create mode 100644 src/main/java/com/glxp/api/idc/service/IdcService.java create mode 100644 src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java create mode 100644 src/main/java/com/glxp/api/idc/thread/AsyncIdcTask.java create mode 100644 src/main/java/com/glxp/api/idc/utils/DBAUtils.java create mode 100644 src/main/java/com/glxp/api/idc/utils/IDCUtils.java create mode 100644 src/main/resources/mybatis/mapper/idc/DbMapper.xml diff --git a/src/main/java/com/glxp/api/dao/idc/DbDao.java b/src/main/java/com/glxp/api/dao/idc/DbDao.java new file mode 100644 index 000000000..fcce7e00a --- /dev/null +++ b/src/main/java/com/glxp/api/dao/idc/DbDao.java @@ -0,0 +1,89 @@ +package com.glxp.api.dao.idc; + +import org.apache.ibatis.annotations.*; + +import java.util.List; +import java.util.Map; + +/*获取表相关信息*/ +@Mapper +public interface DbDao { + @Insert("${sql}") + int save(@Param("sql") String sql); + + @Update("${sql}") + int update(@Param("sql") String sql); + + @Delete("${sql}") + int delete(@Param("sql") String sql); + + Map get(@Param("sql") String sql); + + List> list(Map map); + + int count(Map map); + + @Select("select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables" + + " where table_schema = (select database())") + List> listMysql(); + + @Select("select tname as \"tableName\",'oracle' as \"engine\",tname as \"tableComment\",sysdate as \"createTime\" from tab where instr('BIN,ACT',substr(tname,1,3))<1") + List> listOracle(); + + @Select("select name tableName, 'sqlserver' engine, name tableComment, crdate createTime from SYSOBJECTS" + + " WHERE XTYPE='U' ORDER BY NAME") + List> listSqlserver(); + + @Select("select count(*) from information_schema.tables where table_schema = (select database())") + int countMysql(Map map); + + @Select("select count(*) from tab where tabtype='TABLE' and instr('BIN,ACT',substr(tname,1,3))<1") + int countOracle(Map map); + + @Select("select count(*) from SYSOBJECTS where XTYPE='U'") + int countSqlserver(Map map); + + @Select("select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables \r\n" + + " where table_schema = (select database()) and lower(table_name) = lower(#{tableName})") + Map getMysql(String tableName); + + @Select("select tname as \"tableName\",'oracle' \"engine\",tname \"tableComment\",sysdate \"createTime\" from tab \r\n" + + " where tname= upper(#{tableName})") + Map getOracle(String tableName); + + @Select("select name tableName, 'sqlserver' engine, name tableComment, crdate createTime from SYSOBJECTS" + + " WHERE lower(name)=lower(#{tableName})") + Map getSqlserver(String tableName); + + @Select("select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra as extra,table_name tableName, " + + "concat('(',if(NUMERIC_PRECISION>0,CONVERT(NUMERIC_PRECISION,CHAR),''),if(NUMERIC_SCALE>0,concat(',',CONVERT(NUMERIC_SCALE,CHAR)),''),if(CHARACTER_MAXIMUM_LENGTH>0,CONVERT(CHARACTER_MAXIMUM_LENGTH,CHAR),''),')') as width " + + " from information_schema.columns\r\n" + + " where lower(table_name) = lower(#{tableName}) and table_schema = (select database()) order by ordinal_position") + List> listColumnsMysql(String tableName); + + @Select("select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra as extra,table_name tableName, " + + "concat('(',if(NUMERIC_PRECISION>0,CONVERT(NUMERIC_PRECISION,CHAR),''),if(NUMERIC_SCALE>0,concat(',',CONVERT(NUMERIC_SCALE,CHAR)),''),if(CHARACTER_MAXIMUM_LENGTH>0,CONVERT(CHARACTER_MAXIMUM_LENGTH,CHAR),''),')') as width " + + " from information_schema.columns\r\n" + + " where lower(table_name) = lower(#{tableName}) column_key='PRI' and table_schema = (select database()) order by ordinal_position") + List> listKeyMysql(String tableName); + + @Select("select cname as \"columnName\",coltype as \"dataType\",cname \"columnComment\",'' as \"columnKey\",'' as \"extra\",tname \"tableName\", " + + " '('||decode(sign(PRECISION),1,to_char(PRECISION),'')||decode(sign(SCALE),1,','||to_char(SCALE),'')||decode(sign(PRECISION),1,'',decode(sign(SCALE),1,'',to_char(width)))||')' as width " + + "from col \r\n" + + " where tname = upper(#{tableName}) order by colno") + List> listColumnsOracle(String tableName); + + @Select("select syscolumns.name columnName, systypes.name as dataType, syscolumns.name columnComment, \r\n" + + "(select 'PRI' from sysindexkeys,sysobjects,sysindexes where sysindexkeys.id=syscolumns.id and sysindexkeys.colid=syscolumns.colid \r\n" + + " and sysindexkeys.id=syscolumns.id and sysindexes.id=syscolumns.id and sysindexkeys.indid=sysindexes.indid and sysobjects.name=sysindexes.name and sysobjects.xtype='PK'" + + ") columnKey,'' extra,#{tableName} tableName, " + + "'('+case when syscolumns.xprec>0 then cast(syscolumns.xprec as varchar) else '' end + case when syscolumns.xscale>0 then ','+cast(syscolumns.xscale as varchar) else '' end +case when syscolumns.xscale>0 or syscolumns.xprec>0 then '' else cast(syscolumns.length as varchar) end +')' as width " + + " from syscolumns,\r\n" + + " systypes" + + " where syscolumns.xtype=systypes.xtype and syscolumns.id = OBJECT_ID(#{tableName}) order by syscolumns.colid") + List> listColumnsSqlserver(String tableName); + + + @Select("select c.column_name as \"columnName\" from user_constraints con,user_cons_columns c where con.constraint_name=c.constraint_name and con.constraint_type='P' and c.table_name=upper(#{tableName})") + List> listKeyOracle(String tableName); +} diff --git a/src/main/java/com/glxp/api/idc/controller/IdcController.java b/src/main/java/com/glxp/api/idc/controller/IdcController.java new file mode 100644 index 000000000..795e50daa --- /dev/null +++ b/src/main/java/com/glxp/api/idc/controller/IdcController.java @@ -0,0 +1,70 @@ +package com.glxp.api.idc.controller; + +import com.glxp.api.common.res.BaseResponse; +import com.glxp.api.dao.idc.DbDao; +import com.glxp.api.idc.service.IdcService; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.Map; + + +/** + * 中继服务接口 + */ +@RestController +public class IdcController { + @Resource + IdcService idcService; + @Resource + DbDao dbDao; + + /*通用上传接口*/ +// @Log("中继服务数据接收,下级往上级上传") + @RequestMapping(value = "/spssync/common/upload") + public BaseResponse upload(HttpServletRequest request, + @RequestParam("content") String content, + @RequestParam(value = "files", required = false) MultipartFile[] files) { + // + return idcService.receive("U", request, content, files); + } + + /*通用下发接口*/ +// @Log("中继服务数据接收,上级往下级下发") + @RequestMapping(value = "/spssync/common/issued") + public BaseResponse issued(HttpServletRequest request, + @RequestParam("content") String content, + @RequestParam(value = "files", required = false) MultipartFile[] files) { + // + return idcService.receive("I", request, content, files); + } + + // @Log("数据同步测试") + @RequestMapping(value = "/spssync/common/test") + public BaseResponse test(HttpServletRequest request, @RequestBody Map params) { + // + return idcService.send(params); + } + + // @Log("数据同步任务列表拉取") + @RequestMapping(value = "/spssync/common/list") + public BaseResponse list(HttpServletRequest request, @RequestBody(required = false) Map params) { + return idcService.taskList(request, params); + } + + // @Log("数据同步任务根据任务ID下载数据") + @RequestMapping(value = "/spssync/common/download") + @ResponseBody + public BaseResponse download(HttpServletRequest request, @RequestBody Map params) { + return idcService.download(request, params); + } + + // @Log("数据同步任务根据任务ID,返回下载成功标记") + @RequestMapping(value = "/spssync/common/success") + @ResponseBody + public BaseResponse success(HttpServletRequest request, @RequestBody Map params) { + return idcService.downlaodSuccess(request, params); + } +} diff --git a/src/main/java/com/glxp/api/idc/service/IdcService.java b/src/main/java/com/glxp/api/idc/service/IdcService.java new file mode 100644 index 000000000..297006798 --- /dev/null +++ b/src/main/java/com/glxp/api/idc/service/IdcService.java @@ -0,0 +1,30 @@ +package com.glxp.api.idc.service; + +import com.glxp.api.common.res.BaseResponse; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import java.util.Map; + +/*数据中继数据中心(接收)*/ +public interface IdcService { + + + BaseResponse receive(String direction, HttpServletRequest request, + String content, MultipartFile[] files); + + + BaseResponse send(Map params); + + BaseResponse send(String messageType, String tableName, Map params); + + BaseResponse taskList(HttpServletRequest request, Map params); + + BaseResponse download(HttpServletRequest request, Map params); + + void pull(); + + BaseResponse downlaodSuccess(HttpServletRequest request, Map params); + + public void asyncIdcTask(); +} 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 new file mode 100644 index 000000000..c65e5f472 --- /dev/null +++ b/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java @@ -0,0 +1,747 @@ +package com.glxp.api.idc.service.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.glxp.api.common.res.BaseResponse; +import com.glxp.api.common.util.ResultVOUtils; +import com.glxp.api.dao.idc.DbDao; +import com.glxp.api.dao.schedule.ScheduledDao; +import com.glxp.api.entity.system.ScheduledEntity; +import com.glxp.api.entity.system.SystemParamConfigEntity; +import com.glxp.api.idc.service.IdcService; +import com.glxp.api.idc.utils.DBAUtils; +import com.glxp.api.idc.utils.IDCUtils; +import com.glxp.api.req.system.ScheduledRequest; +import com.glxp.api.service.system.SystemParamConfigService; +import com.glxp.api.util.CustomUtil; +import com.glxp.api.util.DateUtil; +import com.glxp.api.util.FileUtils; +import lombok.extern.slf4j.Slf4j; +import okhttp3.*; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.text.WordUtils; +import org.apache.tools.ant.util.DateUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.support.CronSequenceGenerator; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.TimeUnit; + + +/*数据中继数据中心(接收)*/ +@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; + + @Value("${API_KEY}") + private String apiKey; + @Value("${API_SECRET}") + private String apiSecret; + + /*自助平台->UDI*/ + private final String[] SPMS_TO_UDI_TABLES = { + "basic_udirel/updateTime//耗材字典", "basic_products///耗材字典信息详情", "company_product_relevance/updateTime//供应商关联信息", + "basic_corp/updateTime//往来单位", "thr_bustype_origin/updateTime//第三方单据类型", + "thr_corp/updateTime//第三方往来单位", "thr_dept/updateTime//第三方部门", + "thr_inv_products/updateTime//第三方库存", "thr_inv_warehouse///第三方仓库", + "thr_order/updateTime//第三方单据", "thr_order_detail/updateTime//第三方单据详情", + "thr_products/updateTime//第三方产品信息", "sup_cert/updateTime/filePath/资质证书信息", + "sup_cert_set/updateTime//供应商资质证书设置", "sup_company/updateTime//配送供应商资质信息", + "sup_manufacturer/updateTime//生产企业资质信息", "sup_product/updateTime//产品资质信息"}; + /*UDI->自助平台*/ + private final String[] UDI_TO_SPMS_TABLES = { + "basic_udirel/updateTime//耗材字典", "basic_products///耗材字典信息详情", "company_product_relevance/updateTime//供应商关联信息", + "basic_corp/updateTime//往来单位", "thr_bustype_origin/updateTime//第三方单据类型", + "thr_corp/updateTime//第三方往来单位", "thr_dept/updateTime//第三方部门", + "thr_inv_products/updateTime//第三方库存", "thr_inv_warehouse///第三方仓库", + "thr_order/updateTime//第三方单据", "thr_order_detail/updateTime//第三方单据详情", + "thr_products/updateTime//第三方产品信息", "sup_cert/updateTime/filePath/资质证书信息", + "sup_cert_set/updateTime//供应商资质证书设置", "sup_company/updateTime//配送供应商资质信息", + "sup_manufacturer/updateTime//生产企业资质信息", "sup_product/updateTime//产品资质信息", + "productinfo/updateTime//国家库UDI数据,udicompany/updateTime//国际库医疗器械注册人信息"}; + + private int orderNum = 0; + + @Resource + private SystemParamConfigService systemParamConfigService; + + @Resource + private JdbcTemplate jdbcTemplate; + + @Resource + private DbDao dbDao; + + @Resource + private ScheduledDao scheduledDao; + + @Override + public BaseResponse taskList(HttpServletRequest request, Map params) { + Map map = new HashMap(); + map.put("sql", "select taskId from basic_upload_status where status='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_upload_status where taskId='" + params.get("taskId") + "'"); + if (map == null) + ResultVOUtils.error(9000, "下载任务不存在"); + String json = FileUtils.readFileAll(map.get("cacheFilePath").toString()); + JSONObject object = JSON.parseObject(json); + return ResultVOUtils.success(object); + } + + /*拉取前一级中继服务数据*/ + @Override + public void pull() { + Map query = new HashMap(); + String result = post(getNextHost("U") + "/spssync/common/list", query); + JSONObject json = JSON.parseObject(result); + if (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")); + pullData(getNextHost("U") + "/spssync/common/list", params); + } + } + } + } + + @Async + @Override + public void asyncIdcTask() { + initTable(); + String[] tNames = {}; + String direction = getDirection(); + + if (direction.equals("I")) { + tNames = SPMS_TO_UDI_TABLES; + } else { + tNames = UDI_TO_SPMS_TABLES; + } + /*为顶级或末级,以及下游或上游连通,可执行*/ + Boolean isLastLevel = isLastLevel(direction); + Boolean isRelay = isRelay(direction); + System.out.print("-----数据传输任务开始----" + direction + "\n"); + System.out.print("-----是否顶级或末级服务:" + isLastLevel + ",是否转发数据:" + isRelay + "----\n"); + if (isLastLevel && isRelay) { + for (String t : tNames) { + uploadData(t); + } + } + + } + + private void uploadData(String t) { + String[] tn = t.split("/"); + String lastUpdateTime = getUpdateTime(tn[0]); + Date nowUpdateTime = new Date(); + Map map = new HashMap(); + String sqlWhere = "not exists (select fkId from idc_record where fkId=" + tn[0] + ".id)"; + if (!StringUtils.isEmpty(tn[1])) { + sqlWhere += " and ifnull(" + tn[1] + ",now())>=cast('" + lastUpdateTime + "' as datetime)"; + } else { + map.put("isEnd", "1"); + } + map.put("sqlWhere", sqlWhere); + map.put("tableName", tn[0]); + map.put("filePathColumn", tn[2]); + map.put("messageType", tn[3]); + map.put("messageName", tn[3]); + ScheduledRequest scheduledRequest = new ScheduledRequest(); + scheduledRequest.setCronName("sync" + WordUtils.capitalizeFully(tn[0], new char[]{'_'}).replace("_", "")); + ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); + String cron = scheduledEntity != null ? scheduledEntity.getCron() : ""; + boolean send = true; + if (!StringUtils.isEmpty(cron)) { + CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron); + Date nextTimePoint = cronSequenceGenerator.next(DateUtil.parseDate(lastUpdateTime)); + send = nextTimePoint.before(nowUpdateTime); + } + //if(send) { + BaseResponse result = send(map); + if (result.getCode() == 20000) { + setUpdateTime(tn[0], DateUtil.formatDate(nowUpdateTime)); + } + //} + } + + private String pullData(String url, 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(url) + .method("POST", body) + .addHeader("Content-Type", "application/json") + .build(); + String result = ""; + MultipartFile[] files = {}; + try { + Response response = client.newCall(request).execute(); + result = response.body().toString(); + JSONObject json = JSONObject.parseObject(result); + analyToDB(json, files); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + 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, 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().toString(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return result; + } + + + public BaseResponse downlaodSuccess(HttpServletRequest request, Map params) { + String sql = "update basic_upload_status set status='1' where taskId='" + params.get("taskId") + "'"; + dbDao.update(sql); + return ResultVOUtils.success(); + } + + /*direction方向 U 上传,I 下发,往自助平台为上传,往UDI管理系统为下发*/ + @Override + public BaseResponse receive(String direction, 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"); + + FileUtils.makeDirectory(filePath + datePath + "/"); + FileUtils.makeDirectory(backFilePath + datePath + "/"); + String fileName = filePath + datePath + "/" + json.getString("messageType") + "-" + json.getString("messageId") + ".udi"; + String backFileName = backFilePath + datePath + "/" + json.getString("messageType") + "-" + json.getString("messageId") + ".udi"; + FileUtils.SaveFileAs(content, fileName); + FileUtils.SaveFileAs(content, backFileName); + 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(); + } + + } + saveUploadStatus(json.getString("messageType"), json.getString("messageId"), fileName); + /*解析入库*/ + if (isLastLevel(direction)) { + analyToDB(json, files); + } else { + //需要转发 + if (isRelay(direction)) { + relay(direction, request.getHeader("reqNo"), content, saveFiles); + } + } + 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) { + if (sendOnPage(messageType, tableName, params)) + return ResultVOUtils.success(); + return ResultVOUtils.error(9999, ""); + } + + + private void saveUploadStatus(String messageType, String taskId, String fileName) { + Map map = new HashMap(); + map.put("id", CustomUtil.getId()); + map.put("taskId", taskId); + map.put("cacheFilePath", fileName); + map.put("status", "0"); + map.put("type", messageType); + 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 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 sendOnPage(String messageType, String tableName, Map params) { + boolean success = true; + 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 map = new HashMap(); + String where = DBAUtils.convertWhere(column, params, ""); + sql += !StringUtils.isEmpty(where) ? " where " + where : ""; + map.put("sql", sql); + int total = dbDao.count(map); + String direction = getDirection(); + 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++; + saveIdcLog(messageType, "", tableName + ">" + where, 0, total); + if (total > 0) { + params.put("page", 0); + params.put("limit", limit); + for (int i = 0; i < Math.ceil(total / limit) + 1; i++) { + params.replace("page", i * limit); + params.replace("limit", limit); + Map data = getData(messageType, tableName, params); + 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("id") + "','" + isEnd + "',now())"; + } + + orderNum++; + String result = relay(direction, "", JSON.toJSONString(data), files); + 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); + } + } + } + } + } + } + return success; + } + + /*解析到数据库*/ + private void analyToDB(JSONObject jsonObject, MultipartFile[] files) { + if (!StringUtils.isEmpty(jsonObject.getString("tableName"))) { + List> list = jsonObject.getObject("data", List.class); + analyData(jsonObject.getString("tableName"), list); + //图片处理 + } + } + + /*按表名解析数据到数据库,子表暂未处理*/ + private void analyData(String tableName, List> list) { + String tName = DBAUtils.tableRealName(tableName); + String sql = "insert into " + tName + "("; + String del = "delete from " + tName + " where "; + String upd = "update " + tName + " set "; + String[] keyColumn = new String[10]; + String[] keyDataType = new String[10]; + List> columnList = dbDao.listColumnsMysql(tName); + 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")); + String extra = columnList.get(i).get("extra") != null && columnList.get(i).get("extra").toLowerCase().contains("auto_i") ? "A" : "N"; + sql += extra.equals("N") ? columnList.get(i).get("columnName").toString() : ""; + col += extra.equals("N") ? 1 : 0; + if (columnList.get(i).get("columnKey") != null && !StringUtils.isEmpty(columnList.get(i).get("columnKey").toString()) && + ("PRI,UNI,MUL".contains(columnList.get(i).get("columnKey").toString())) && extra.equals("N")) { + keyColumn[key] = columnList.get(i).get("columnName").toString(); + keyDataType[key] = columnList.get(i).get("dataType").toString().contains("char") ? "C" : "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 " : " "; + updateWhere += keyColumn[z] + " = " + (keyDataType[z].equals("C") ? "'" : "") + list.get(i).get(keyColumn[z]) + (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 extra = columnList.get(k).get("extra") != null && columnList.get(k).get("extra").toLowerCase().contains("auto") ? "A" : "N"; + String attrName = columnList.get(k).get("attrName"); + String value = list.get(i).get(attrName) != null ? list.get(i).get(attrName).toString() : ""; + 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"; + if (extra.equals("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++; + } + } + if (n > 0) + dbDao.save(sql); + + } + + } + + + /*转发下级或上级中继服务*/ + private String relay(String direction, String reqNo, String content, String[] files) { + String host = getNextHost(direction); + String result = ""; + if (StringUtils.isEmpty(reqNo)) + reqNo = UUID.randomUUID().toString(); + if (!StringUtils.isEmpty(host)) { + host += direction.equals("U") ? "/spssync/common/upload" : "/spssync/common/issued"; + 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(String direction) { + SystemParamConfigEntity systemParamConfigEntity = getSystemParamConfig("upper_server_ip", "中继上传服务地址", "", ""); + //direction.equals("U") ? getSystemParamConfig("upper_server_ip","中继上传服务地址" , "",""): + //getSystemParamConfig("sync_idc_lower_host","下级(下发)中继服务地址" , "","") ; + return systemParamConfigEntity.getParamValue(); + } + + /*判断是否最后一级*/ + private boolean isLastLevel(String direction) { + SystemParamConfigEntity systemParamConfigEntity = + direction.equals("I") ? getSystemParamConfig("sync_idc_top", "是否顶级中继服务(连接自助平台)", "0", "0:否;1:是(是,接收下级上传数据后解析入库)") : + getSystemParamConfig("sync_idc_final", "是否末级中继服务(连接UDI管理系统)", "0", "0:否;1:是(是,接收上级下发数据后解析入库)"); + return systemParamConfigEntity.getParamValue().equals("0") ? false : true; + + } + + /*检查当前系统为自助平台(下发)还是UDI系统(上传),返回传输方向,如果都未设置,默认返回下发*/ + private String getDirection() { + SystemParamConfigEntity systemParamConfigEntity = getSystemParamConfig("sync_idc_top", "是否顶级中继服务(连接自助平台)", "0", "0:否;1:是(是,接收下级上传数据后解析入库)"); + System.out.print(">>>>>>>>>>" + JSON.toJSONString(systemParamConfigEntity) + "\n"); + if (systemParamConfigEntity.getParamValue().equals("0")) { + systemParamConfigEntity = getSystemParamConfig("sync_idc_final", "是否末级中继服务(连接UDI管理系统)", "0", "0:否;1:是(是,接收上级下发数据后解析入库)"); + if (systemParamConfigEntity.getParamValue().equals("1")) + return "U"; + } + return "I"; + + } + + /*是否需要转发*/ + private boolean isRelay(String direction) { + String relayStr = direction.equals("U") ? "sync_upstream_enable" : "sync_downstream_enable"; + + SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey(relayStr); + if (systemParamConfigEntity != null && systemParamConfigEntity.getParamValue() != null && + systemParamConfigEntity.getParamValue().equals("0")) { + return false; + } + return true; + } + + + /*由表名获取数据*/ + private Map getData(String messageType, 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 : ""; + params.put("sql", sql); + List> list = dbDao.list(params); + 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("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())); + } 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_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/idc/thread/AsyncIdcTask.java b/src/main/java/com/glxp/api/idc/thread/AsyncIdcTask.java new file mode 100644 index 000000000..2e76095ef --- /dev/null +++ b/src/main/java/com/glxp/api/idc/thread/AsyncIdcTask.java @@ -0,0 +1,53 @@ +package com.glxp.api.idc.thread; + +import com.glxp.api.dao.schedule.ScheduledDao; +import com.glxp.api.entity.system.ScheduledEntity; +import com.glxp.api.idc.service.IdcService; +import com.glxp.api.req.system.ScheduledRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; +import org.springframework.scheduling.support.CronTrigger; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +@Component +@EnableScheduling +public class AsyncIdcTask implements SchedulingConfigurer { + + final Logger logger = LoggerFactory.getLogger(AsyncIdcTask.class); + + @Resource + private ScheduledDao scheduledDao; + + @Resource + private IdcService idcService; + + @Override + public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { + scheduledTaskRegistrar.addTriggerTask(() -> process(), + triggerContext -> { + ScheduledRequest scheduledRequest = new ScheduledRequest(); + scheduledRequest.setCronName("syncIdc"); + logger.info("syncIdc----------------"); + ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); + String cron = scheduledEntity != null ? scheduledEntity.getCron() : "5 * * * * ?"; + + if (cron.isEmpty()) { + logger.error("cron is null"); + } + logger.info("syncIdc----------------"); + return new CronTrigger(cron).nextExecutionTime(triggerContext); + }); + } + + private void process() { + logger.info("syncIdc----process------------"); + + idcService.asyncIdcTask(); + } + +} diff --git a/src/main/java/com/glxp/api/idc/utils/DBAUtils.java b/src/main/java/com/glxp/api/idc/utils/DBAUtils.java new file mode 100644 index 000000000..b72a2cde1 --- /dev/null +++ b/src/main/java/com/glxp/api/idc/utils/DBAUtils.java @@ -0,0 +1,400 @@ +package com.glxp.api.idc.utils; + +import com.glxp.api.util.DateUtil; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + + +/** + * @author chenqf + */ +public class DBAUtils { + private static final Logger logger = LoggerFactory.getLogger(DBAUtils.class); + private static final String keywords = ",limit,offset,sort,order,isLike,menuId,menuCode,page,isAuto,pageNo,pageSize,pageNum,sqlOrder,join,"; + + /*解析where条件*/ + public static String parseWhere(String sql, Map columns, Map params, String dataWhere) { + return parseWhere(sql, columns, params, dataWhere, false); + } + + public static String parseWhere(String sql, Map columns, Map params, String dataWhere, boolean isSimpleWhere) { + String where = ""; + boolean isAnd0 = false; + boolean isLike = params != null && params.get("isLike") != null && params.get("isLike").toString().equals("true") ? true : false; + + if (sql.contains("sqlWhere") || !sql.contains(" 0) { + for (int i = s1 - 1; i > -1; i--) { + if (!sql.substring(i, i + 1).equals(" ") && !sql.substring(i, i + 1).equals("\n")) { + isAnd0 = true; + } + } + } + if (!StringUtils.isEmpty(dataWhere)) { + if (sql.contains("sqlWhere")) { + if (params.get("sqlWhere") != null && !StringUtils.isEmpty(params.get("sqlWhere").toString())) { + String temp = params.get("sqlWhere").toString(); + params.replace("sqlWhere", "(" + temp + ") and (" + dataWhere + ")"); + } else { + params.put("sqlWhere", dataWhere); + } + } + } + while (sql.contains("", a + 1); + int h = sql.indexOf("isLike", a + 1); + int j = sql.indexOf("!=", a + 1); + if (h > a && h < b) { + sql = j > 1 && j < b ? sql.substring(0, a) + "[!=isLike]" + sql.substring(b + 1) : sql.substring(0, a) + "[==isLike]" + sql.substring(b + 1); + n = 0; + continue; + } + + int c = sql.indexOf(""); + String str = sql.substring(b + 1, c); + int e1 = str.indexOf("#{"); + int e2 = str.indexOf("}", e1 + 1); + if (e1 < 0) { + e1 = str.indexOf("${"); + } + String key = str.substring(e1 + 2, e2).trim(); + Object obj = params.get(key); + String value = obj != null ? obj.toString() : ""; + if (key.equals("sqlWhere")) { + if (StringUtils.isEmpty(value)) + value = where; + } + if (!StringUtils.isEmpty(value)) { + str = str.length() > e2 + 1 ? str.substring(0, e1) + "'" + value + "'" + str.substring(e2 + 1) : str.substring(0, e1) + "'" + value + "'"; + if (!isAnd0 && n == 0) { + if (str.length() > 3) { + if (str.trim().substring(0, 4).toLowerCase().equals("and ")) { + str = str.substring(str.toLowerCase().indexOf("and ") + 3); + } + if (str.trim().substring(0, 3).toLowerCase().equals("or ")) { + str = str.substring(str.toLowerCase().indexOf("or ") + 2); + } + } + } + n++; + } else { + str = ""; + } + sql = sql.length() > c + 5 ? sql.substring(0, a) + str + sql.substring(c + 5) : sql.substring(0, a) + str; + } + + } + if (sql.contains("")) { + sql = n > 0 ? sql.replaceAll("", " where ") : sql.replaceAll("", ""); + if (!StringUtils.isEmpty(dataWhere) && !sql.contains("sqlWhere")) { + sql = n > 0 ? sql.replaceAll("", " and (" + dataWhere + ")") : sql.replaceAll("", " (" + dataWhere + ")"); + } else { + sql = sql.replaceAll("", ""); + } + } + if (sql.contains("=isLike")) { + int a1, a2, b1, b2; + a1 = sql.indexOf("[==isLike]"); + a2 = sql.indexOf("", a1 + 1); + + if (a1 > 0 && a2 > 0) { + if (isLike) { + sql = sql.length() > a2 + 5 ? sql.substring(0, a1) + sql.substring(a1 + 10, a2) + sql.substring(a2 + 5) : sql.substring(0, a1) + sql.substring(a1 + 10, a2); + } else { + sql = sql.length() > a2 + 5 ? sql.substring(0, a1) + sql.substring(a2 + 5) : sql.substring(0, a1); + } + } + b1 = sql.indexOf("[!=isLike]"); + b2 = sql.indexOf("", b1 + 1); + if (b1 > 0 && b2 > 0) { + if (!isLike) { + sql = sql.length() > b2 + 5 ? sql.substring(0, b1) + sql.substring(b1 + 10, b2) + sql.substring(b2 + 5) : sql.substring(0, b1) + sql.substring(b1 + 10, b2); + } else { + sql = sql.length() > b2 + 5 ? sql.substring(0, b1) + sql.substring(b2 + 5) : sql.substring(0, b1); + } + } + } + + sql = parseParams(sql, columns, params); + return sql; + } + + public static String parseParams(String sql, Map columns, Map params) { + String[] iStr = {"#{", "${"}; + for (int k = 0; k < 2; k++) { + while (sql.contains(iStr[k])) { + int a1 = sql.indexOf(iStr[k]); + int a2 = sql.indexOf("}", a1 + 1); + String key = sql.substring(a1 + 2, a2); + Object obj = params.get(key); + String value = obj != null ? obj.toString() : ""; + String dataType = "0"; + if (columns != null) { + Map map = (Map) columns.get(key); + dataType = map != null && map.get("dataType") != null ? map.get("dataType").toString() : "0"; + } + sql = sql.substring(0, a1) + (dataType.equals("0") ? "'" : "") + value + (dataType.equals("0") ? "'" : "") + sql.substring(a2 + 1); + } + } + + return sql; + + } + + + /*生成where条件*/ + public static String convertWhere(Map colums, Map params, String dataWhere) { + String where = convertWhere(colums, params, dataWhere, ""); + return where; + } + + public static String convertWhere(Map colums, Map params, String dataWhere, String sql) { + String where = ""; + String value = ""; + boolean isLike = false; + boolean isKey = false; + if (params != null && params.get("isLike") != null && params.get("isLike").toString().equals("true")) + isLike = true; + if (colums != null) { + Iterator keys = colums.keySet().iterator(); + isKey = keys.hasNext(); + } + for (String key : params.keySet()) { + Object obj = params.get(key); + value = ""; + if (obj != null) + value = obj.toString(); + if (!StringUtils.isEmpty(value) && !keywords.contains("," + key + ",") && (StringUtils.isEmpty(sql) || (!StringUtils.isEmpty(sql) && !sql.contains("{" + key + "}")))) { + + if (key.equals("sqlWhere") || key.equals("join")) { + where += (!StringUtils.isEmpty(where) ? " and " : "") + " " + value; + } else { + String[] cns = key.split(","); + String wh = ""; + for (String cn : cns) { + String columnName = cn;//StringUtils.propertyToField(cn); + + + if (colums != null && isKey && colums.get(cn) == null) { + logger.error("列名" + columnName + "不存在,属性" + cn + "错误."); + } else { + if (!StringUtils.isEmpty(wh)) + wh += " or "; + if (isLike) { + wh += "instr(" + columnName + ",'" + value + "')>0"; + } else { + wh += columnName + "='" + value + "'"; + } + } + } + where += !StringUtils.isEmpty(wh) ? (!StringUtils.isEmpty(where) ? " and " : "") + "(" + wh + ")" : ""; + } + } + } + + if (!StringUtils.isEmpty(dataWhere)) { + if (!StringUtils.isEmpty(where)) { + where = "(" + where + ") and (" + dataWhere + ")"; + } else { + where = dataWhere; + } + } + if (!StringUtils.isEmpty(where)) + where = "(" + where + ")"; + return where; + } + + + /*生成insert语句*/ + public static String parseInsert(Map params, Map columns) { + String ins = ""; + String vas = ""; + String sql = ""; + for (String key : columns.keySet()) { + String value = ""; + + if (params.get(key) != null) + value = params.get(key).toString(); + Map map = (Map) columns.get(key); + String dataType = map.get("dataType").toString(); + String extra = map.get("extra") != null && map.get("extra").toString().toLowerCase().contains("auto") ? "A" : "N"; + + if (map.get("columnKey").toString().equals("PRI") && extra.equals("N")) { + if (StringUtils.isEmpty(value)) { + + } + } + + value = DBAUtils.escape(value); + if (!StringUtils.isEmpty(value)) { + if (!StringUtils.isEmpty(ins)) + ins += ","; + ins += map.get("columnName").toString(); + + if (!StringUtils.isEmpty(vas)) + vas += ","; + + if (StringUtils.isEmpty(value)) { + vas += "null"; + } else { + if (dataType.equals("C")) { + vas += "'" + value + "'"; + } else if (dataType.equals("N")) { + vas += value; + } else { + String str = ""; + Date date = new Date(); + if (StringUtils.isNumeric(value)) { + date = new Date(Long.valueOf(value)); + str = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); + } else { + str = DateUtil.formatDate(IDCUtils.parseDate(value), "yyyy-MM-dd HH:mm:ss"); + } + vas += "cast('" + str + "' as datetime)"; + + } + } + } + } + if (!StringUtils.isEmpty(ins) && !StringUtils.isEmpty(vas)) { + sql = "(" + ins + ") values (" + vas + ")"; + } + + return sql; + } + + + public static String convertColumnType(String dataType, String dbType, String width) { + String columnType = dataType.toLowerCase(); + if (dbType.contains("oracle.")) { + columnType = columnType.contains("varchar") ? width.contains("(") ? ("varchar2" + width) : ("varchar2(" + width + ")") : + columnType.contains("char") ? width.contains("(") ? ("char" + width) : ("char(" + width + ")") : + columnType.contains("date") || columnType.contains("time") ? "date" : + columnType.contains("int") || columnType.contains("num") || columnType.contains("dec") ? width.contains("(") ? ("number" + width + "") : ("number(" + width + ")") : ""; + } else { + columnType = columnType.contains("varchar") ? width.contains("(") ? ("varchar" + width) : ("varchar(" + width + ")") : + columnType.contains("char") ? width.contains("(") ? ("char" + width) : ("char(" + width + ")") : + columnType.contains("date") || columnType.contains("time") ? "datetime" : + columnType.contains("int") || columnType.contains("num") || columnType.contains("dec") ? width != null && width.contains(",") ? width.contains("(") ? ("decimal" + width + "") : ("decimal(" + width + ")") : "int" : ""; + } + + return columnType; + } + + private static String aliasNameChars = "_hijklmnopqrstuvwxyzabcdefg9876543210"; + private static String realNamechars = "abcdefghijklmnopqrstuvwxyz_0123456789"; + + /*表名简单转义*/ + public static String tableAliasName(String tableName) { + String tname = "2d"; + for (int i = 0; i < tableName.length(); i++) { + int idx = realNamechars.indexOf(tableName.substring(i, i + 1).toLowerCase()); + if (idx > -1) { + tname += aliasNameChars.substring(idx, idx + 1); + } else { + tname += tableName.substring(i, i + 1).toLowerCase(); + } + } + return tname; + } + + /*获取实际表名*/ + public static String tableRealName(String tableName) { + String tname = ""; + String result = ""; + tableName = tableName.replaceAll("202c", ","); + String[] tabs = tableName.split(","); + for (int n = 0; n < tabs.length; n++) { + String t = tabs[n]; + tname = ""; + if (!StringUtils.isEmpty(t)) { + if (!StringUtils.isEmpty(result)) + result += ","; + if (t.length() > 2 && t.substring(0, 2).toLowerCase().equals("2d")) { + t = t.substring(2); + for (int i = 0; i < t.length(); i++) { + int idx = aliasNameChars.indexOf(t.substring(i, i + 1).toLowerCase()); + if (idx > -1) { + tname += realNamechars.substring(idx, idx + 1); + } else { + tname += t.substring(i, i + 1); + } + } + } else { + tname = t; + } + result += tname; + } + } + return result; + } + + public static String tableJoinWhere(String tableName, Map columns) { + String result = ""; + if (tableName.contains(",")) { + String[] tabs = tableName.split(","); + String mTname = ((Map) columns.get("id")).get("tableName").toString(); + //String sTname = ((Map)columns.get(StringUtils.uncapitalize(StringUtils.columnToJava(mTname,"false"))+"Id")).get("tableName").toString(); + //result = mTname+".id="+sTname+"."+mTname+"_id"; + } + return result; + + } + + + /*从update语句解析select*/ + public static String updateSqlToSelectSql(String sql, Map map) { + String aSql = sql.toLowerCase(); + String result = ""; + if (map == null) + map = new HashMap(); + if (aSql.contains("update ")) { + int uIdx = aSql.indexOf("update "); + int sIdx = aSql.indexOf(" set "); + String tName = aSql.substring(uIdx + 7, sIdx); + int wIdx = aSql.indexOf(" where "); + String where = aSql.substring(wIdx + 7); + if (where.contains("=")) { + result = "select * from " + tName + " where "; + String[] tWh = where.split(" and "); + for (int i = 0; i < tWh.length; i++) { + if (tWh[i].contains("=")) { + if (!tWh[i].substring(0, tWh[i].indexOf("=")).toLowerCase().trim().equals("data_version")) { + result += tWh[i]; + map.put("businessId", tWh[i].substring(tWh[i].indexOf("=") + 1).replaceAll("'", "")); + break; + } + } + } + } + map.put("tableName", tName); + } + return result; + } + + public static String escape(String str) { + String value = str; + if (value != null && value.contains("'")) { + value = value.replaceAll("'", "AAA12345ZZZ"); + value = value.replaceAll("AAA12345ZZZ", "''"); + } + return value; + } + + +} diff --git a/src/main/java/com/glxp/api/idc/utils/IDCUtils.java b/src/main/java/com/glxp/api/idc/utils/IDCUtils.java new file mode 100644 index 000000000..5ea830f4d --- /dev/null +++ b/src/main/java/com/glxp/api/idc/utils/IDCUtils.java @@ -0,0 +1,110 @@ +package com.glxp.api.idc.utils; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + + +/** + * @author chenqf + */ +public class IDCUtils { + private static final Logger logger = LoggerFactory.getLogger(IDCUtils.class); + private static String aliasNameChars = "_hijklmnopqrstuvwxyzabcdefg9876543210"; + private static String realNamechars = "abcdefghijklmnopqrstuvwxyz_0123456789"; + private final static String MONTH_EN = "JanFebMarAprMayJunJulAugSepOctNovDec"; + + //判断是否json字符串 + public static boolean isJson(String json) { + if(json!=null&&json.contains("{")&&json.contains("}")&&json.contains("\"")) + return true; + return false; + } + public static Date parseDate(String str) { + return parseDate(str,"yyyy-MM-dd HH:mm:ss"); + } + public static Date parseDate(String str,String fmt) { + String dateStr=str.replace("?", " "); + SimpleDateFormat df = new SimpleDateFormat(fmt); + if(str!=null&&!str.contains("-")&&str.length()>7&&StringUtils.isNumeric(str.substring(0, 4))&& + Long.valueOf(str.substring(0,4))>1899&&Long.valueOf(str.substring(0,4))<2199&& + StringUtils.isNumeric(str.substring(4, 6))&&StringUtils.isNumeric(str.substring(6, 8))&& + (!str.contains(" ")||(str.contains(" ")&&str.indexOf(" ")>7))) { + dateStr = str.substring(0,4)+"-"+str.substring(4,6)+"-"+str.substring(6,8); + dateStr+=" "; + if(str.contains(" ")) { + if(str.length()>9) { + dateStr+= str.substring(9,11); + if(str.length()>11) { + dateStr+=":"+str.substring(11,13); + if(str.length()>13) { + dateStr+=":"+str.substring(13,15); + } + } else { + dateStr+=":00:00"; + } + } else { + dateStr+="00:00:00"; + } + } else { + if(str.length()>8) { + dateStr+= str.substring(8,10); + if(str.length()>10) { + dateStr+=":"+str.substring(10,12); + if(str.length()>13) { + dateStr+=":"+str.substring(12,14); + } if(str.length()>12) { + dateStr+=":"+str.substring(12,13)+"0"; + } + } else { + dateStr+=":00:00"; + } + } else { + dateStr+="00:00:00"; + } + } + } else if(str!=null&&str.contains("CST")) { + //Wed Feb 16 15:35:31 CST 2022 + int index = str.indexOf("CST"); + int month = (MONTH_EN.indexOf(str.substring(index -16,index -13))+3)/3; + dateStr = str.substring(index+4)+"-"; + if(month<10) + dateStr+="0"; + dateStr+= month+"-"+str.substring(index -12,index -10)+" "+str.substring(index -9,index -1); + } else if(str!=null&&str.contains("GMT")) { + int index = str.indexOf("GMT"); + int month = (MONTH_EN.indexOf(str.substring(index -16,index -13))+3)/3; + if(str.contains("GMT+")) { + dateStr = str.substring(index+10)+"-"; + } else { + dateStr = str.substring(index+4)+"-"; + } + if(month<10) + dateStr+="0"; + dateStr+= month+"-"+str.substring(index -12,index -10)+" "+str.substring(index -9,index -1); + } else if (str!=null&&str.equals("-30609820800000")) { + dateStr="1000-01-01 00:01:01"; + } else if (str!=null&&str.length()==4&&StringUtils.isNumeric(str)&&Integer.valueOf(str)>999) { + dateStr=str+"-01-01 00:01:01"; + } else if (str!=null&&str.length()==6&&StringUtils.isNumeric(str)) { + dateStr=str.substring(0, 4)+"-"+str.substring(4)+"-01 00:01:01"; + } else if(str!=null&&StringUtils.isNumeric(str)&&str.length()>8&&(Long.valueOf(str.substring(0,4))<1899||Long.valueOf(str.substring(0,4))>2200)) { + dateStr=df.format(new Date(Long.valueOf(str))); + } + dateStr = dateStr.replace("T", " "); + + Date date = null; + try { + date = df.parse(dateStr); + + } catch (ParseException e) { + logger.error(e.getMessage()); + } + return date; + } + +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 8c3e36de4..673a8ded8 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -51,3 +51,6 @@ UDI_KEY: 6b137c66-6286-46c6-8efa-c2f5dd9237df UDI_SERVER_URL: https://www.udims.com/UDI_DL_Server_test SPMS_KEY: lCOdWCBKS6Kw45wdnnqUTELXyuSKnXEs +back_file_path: D:/share/udisps/back/ +API_KEY: 1101 +API_SECRET: zBITspLNvuoEd4FaamlSoqxRHmNsmQ9L diff --git a/src/main/resources/mybatis/mapper/idc/DbMapper.xml b/src/main/resources/mybatis/mapper/idc/DbMapper.xml new file mode 100644 index 000000000..89d29bb30 --- /dev/null +++ b/src/main/resources/mybatis/mapper/idc/DbMapper.xml @@ -0,0 +1,31 @@ + + + + + + + +