diff --git a/src/main/java/com/glxp/api/idc/controller/IdcController.java b/src/main/java/com/glxp/api/idc/controller/IdcController.java index 0540442b..e3ce5dbf 100644 --- a/src/main/java/com/glxp/api/idc/controller/IdcController.java +++ b/src/main/java/com/glxp/api/idc/controller/IdcController.java @@ -1,68 +1,225 @@ package com.glxp.api.idc.controller; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.multipart.MultipartFile; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; 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; +import cn.hutool.core.io.IoUtil; +import lombok.extern.slf4j.Slf4j; /** * 中继服务接口 */ +@Slf4j @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); - } + @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( 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); + } + + @RequestMapping("/mapi/**") + public Object mapi(HttpServletRequest request, HttpServletResponse httpServletResponse) { + String uri = parseUri(request.getRequestURL().toString()); + + if (uri.contains("getImage")) { + return redirectIamge(request, httpServletResponse, uri); + } else { + return redirect(request, uri); + } + } + + + HttpEntity buildHeader(HttpServletRequest request) { + + log.info(request.getMethod()); + + String bodyContent = ""; + Map headerParam = new HashMap<>(); + + if (request.getMethod().equals(HttpMethod.GET)) { + Map bodyParam = new HashMap(); + Enumeration pNames = request.getParameterNames(); + while (pNames.hasMoreElements()) { + String name = (String) pNames.nextElement(); + String value = request.getParameter(name); + bodyParam.put(name, value); + } + + bodyContent = JSON.toJSONString(bodyParam); + } else { + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(request.getInputStream())); + bodyContent = IoUtil.read(reader); + log.info(bodyContent); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + HttpHeaders headers = new HttpHeaders(); + Enumeration enumeration = request.getHeaderNames(); + while (enumeration.hasMoreElements()) { + String name = enumeration.nextElement(); + String value = request.getHeader(name); + headerParam.put(name, value); + headers.add(name, value); + } + //headers.add("Content-Type", "application/json;charset=UTF-8"); + HttpEntity http = new HttpEntity<>(bodyContent, headers); + return http; + } + + private byte[] redirectIamge(HttpServletRequest request, HttpServletResponse httpServletResponse, String uri) { + uri = uri.replace("mapi/", ""); + uri = "http://116.204.106.103:9150/UDI_SPMS_SERVER/" + uri; + RestTemplate restTemplate = new RestTemplate(); +// HttpEntity httpEntity = buildHeader(request); + log.info(request.getMethod()); + Map headerParam = new HashMap<>(); + Map bodyParam = new HashMap(); + Enumeration pNames = request.getParameterNames(); + String parm = ""; + while (pNames.hasMoreElements()) { + String name = (String) pNames.nextElement(); + String value = request.getParameter(name); + bodyParam.put(name, value); + parm = parm + name + "=" + value + "&"; + } + uri = uri + "?" + parm.substring(0, parm.length() - 1); + HttpHeaders headers = new HttpHeaders(); + Enumeration enumeration = request.getHeaderNames(); + while (enumeration.hasMoreElements()) { + String name = enumeration.nextElement(); + String value = request.getHeader(name); + headerParam.put(name, value); + headers.add(name, value); + } + //headers.add("Content-Type", "application/json;charset=UTF-8"); + HttpEntity http = new HttpEntity<>(JSON.toJSONString(bodyParam), headers); + + + log.info(uri); +// ResponseEntity entity = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, Resource.class); +// ResponseEntity responseEntity = restTemplate.exchange(uri, +// HttpMethod.GET, http, Resource.class, new Object[0]); +// ResponseEntity responseEntity = restTemplate.exchange(uri, HttpMethod.GET, http, HttpServletResponse.class); + + ResponseEntity rsp = restTemplate.getForEntity(uri, byte[].class); +// String targetPath = "D:\\wmslog\\splash-down.png"; +// try { +// Files.write(Paths.get(targetPath), Objects.requireNonNull(rsp.getBody(), +// "未获取到下载文件")); +// } catch (IOException e) { +// e.printStackTrace(); +// } + + return rsp.getBody(); + + } + + + private JSONObject redirect(HttpServletRequest request, String uri) { + + RestTemplate restTemplate = new RestTemplate(); + HttpEntity httpEntity = buildHeader(request); + log.info(uri); + uri = uri.replace("mapi/", ""); + uri = "http://116.204.106.103:9150/UDI_SPMS_SERVER/" + uri; + log.info(uri); + ResponseEntity responseBody = null; + if (request.getMethod().equals("POST")) + responseBody = restTemplate.postForEntity(uri, httpEntity, JSONObject.class); + else if (request.getMethod().equals("GET")) { + responseBody = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, JSONObject.class); + } else if (request.getMethod().equals("PUT")) { + responseBody = restTemplate.exchange(uri, HttpMethod.PUT, httpEntity, JSONObject.class); + } else if (request.getMethod().equals("DELETE")) { + responseBody = restTemplate.exchange(uri, HttpMethod.DELETE, httpEntity, JSONObject.class); + } + JSONObject result = responseBody.getBody(); + return result; + } + + + private String parseUri(String url) { + String uri = url; + int a1 = uri.indexOf("://"); + int a2 = 0; + if (a1 > 0) { + a2 = uri.indexOf("/", a1 + 4); + if (a2 > 0) { + uri = uri.substring(a2 + 1); + } + } + return uri; + } + + } diff --git a/src/main/java/com/glxp/api/idc/service/DownloadRestTemplate.java b/src/main/java/com/glxp/api/idc/service/DownloadRestTemplate.java new file mode 100644 index 00000000..205d08be --- /dev/null +++ b/src/main/java/com/glxp/api/idc/service/DownloadRestTemplate.java @@ -0,0 +1,52 @@ +package com.glxp.api.idc.service; + +import java.io.IOException; +import java.net.URI; + +import org.springframework.http.HttpMethod; +import org.springframework.http.client.ClientHttpRequest; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.web.client.RequestCallback; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.ResponseExtractor; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +import com.beust.jcommander.internal.Nullable; + +import cn.hutool.core.lang.Assert; + +public class DownloadRestTemplate extends RestTemplate { + public DownloadRestTemplate(ClientHttpRequestFactory requestFactory) { + super(requestFactory); + } + + protected T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, + @Nullable ResponseExtractor responseExtractor) throws RestClientException { + + Assert.notNull(url, "URI is required"); + Assert.notNull(method, "HttpMethod is required"); + ClientHttpResponse response = null; + try { + ClientHttpRequest request = createRequest(url, method); + if (requestCallback != null) { + requestCallback.doWithRequest(request); + } + response = request.execute(); + handleResponse(url, method, response); + return (responseExtractor != null ? responseExtractor.extractData(response) : null); + } catch (IOException ex) { + String resource = url.toString(); + String query = url.getRawQuery(); + resource = (query != null ? resource.substring(0, resource.indexOf('?')) : resource); + throw new ResourceAccessException("I/O error on " + method.name() + + " request for \"" + resource + "\": " + ex.getMessage(), ex); + } + /*finally { + if (response != null) { + response.close(); + } + }*/ + } +} diff --git a/src/main/java/com/glxp/api/idc/service/IdcService.java b/src/main/java/com/glxp/api/idc/service/IdcService.java index 5b8d6e70..f079c9b0 100644 --- a/src/main/java/com/glxp/api/idc/service/IdcService.java +++ b/src/main/java/com/glxp/api/idc/service/IdcService.java @@ -1,31 +1,35 @@ 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); +import javax.servlet.http.HttpServletRequest; - BaseResponse taskList(HttpServletRequest request, Map params); +import org.springframework.web.multipart.MultipartFile; - BaseResponse download(HttpServletRequest request, Map params); +import com.glxp.api.common.res.BaseResponse; - public void asyncFetchTask(); - BaseResponse downlaodSuccess(HttpServletRequest request, Map params); +/*数据中继数据中心(接收)*/ +public interface IdcService { + + BaseResponse receive(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); + + public void asyncFetchTask(); + + BaseResponse downlaodSuccess(HttpServletRequest request,Map params); + public void asyncIdcTask(); - + void asyncUdiTask(); + void asyncSpsTask(); + } 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 66244a98..e20d2265 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 @@ -1,5 +1,31 @@ package com.glxp.api.idc.service.impl; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; + +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 com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.glxp.api.common.res.BaseResponse; @@ -16,746 +42,809 @@ 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; +import lombok.extern.slf4j.Slf4j; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +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; - /*自助平台->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 final String[] SYNC_TABLES= { + "entrustAction////////委托验收,basicProducts//basic_udirel///updateTime///耗材字典","/basicProducts/basic_products//////耗材字典信息详情", + "//company_product_relevance///updateTime///供应商关联信息", + "basicCorp//basic_corp///updateTime///往来单位","typeThird//thr_bustype_origin///updateTime///第三方单据类型", + "basicThirdCorp//thr_corp///updateTime///第三方往来单位","//thr_dept///updateTime///第三方部门", + "//thr_inv_products///updateTime///第三方库存","basicThirdInv//thr_inv_warehouse//////第三方仓库", + "basicThirdBusOrder//thr_order///updateTime///第三方业务单据","/basicThirdBusOrder/thr_order_detail///updateTime///第三方单据详情", + "basicThirdProducts//thr_products///updateTime///第三方产品信息","//sup_cert///updateTime/filePath//资质证书信息", + "//sup_cert_set///updateTime///供应商资质证书设置","companyCert//sup_company///updateTime///配送企业", + "manufacturerCert//sup_manufacturer///updateTime///生产企业","productCert//sup_product///updateTime///产品资质信息", + "dbDiProducts//productinfo///updateTime///DI产品信息,//udicompany///updateTime///国际库医疗器械注册人信息"}; + + 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 taskId from basic_upload_status where status='0'"); - List> list = dbDao.list(map); - return ResultVOUtils.success(list); + @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); + } + + + /*UDI系统上传自助平台*/ + @Override + public void asyncUdiTask() { + asyncDataTask(true); } - - @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 asyncSpsTask() { + asyncDataTask(false); } - - /*拉取前一级中继服务数据*/ - @Async - @Override - public void asyncFetchTask() { - String[] directions = {"I", "U"}; - for (String dir : directions) { - Map query = new HashMap(); - String host = getNextHost(dir); - if (!StringUtils.isEmpty(host)) { - String result = post(host + "/spssync/common/list", query); - 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 + "/spssync/common/list", params); - } - } - } - } - } - } - } - - @Async - @Override + + /*拉取前一级中继服务数据*/ + @Async + @Override + public void asyncFetchTask() { + + Map query = new HashMap(); + String host = getNextHost(); + if(!StringUtils.isEmpty(host)) { + String result = post(host+"/spssync/common/list",query); + 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+"/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 if (direction.equals("U")) { - tNames = UDI_TO_SPMS_TABLES; - } - /*为顶级或末级,以及下游或上游连通,可执行*/ - Boolean isLastLevel = isLastLevel(direction); - Boolean isRelay = isRelay(direction); - logger.info("-----数据传输任务开始----" + direction + "\n"); - logger.info("-----是否需要上传或下发数据:" + 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(); - List> keyList = dbDao.listKeyMysql(tn[0]); - String keyColumn = keyList != null && keyList.size() > 0 ? keyList.get(0).get("columnName").toString() : "id"; - Map map = new HashMap(); - String sqlWhere = "not exists (select fkId from idc_record where type='" + tn[0] + "' and fkId=" + tn[0] + "." + keyColumn + ")"; - if (!StringUtils.isEmpty(tn[1])) { - sqlWhere += " and " + tn[1] + ">=cast('" + lastUpdateTime + "' as datetime)"; - } else { - map.put("isEnd", "1"); - } - if (tn[0].equals("thr_order")) - logger.info("-------------------" + sqlWhere); - 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, "yyyy-MM-dd HH:mm:ss")); - } - } - } - - private String fetchData(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); + 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")) + 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 = "not exists (select fkId from idc_record where type='"+tnames[2]+"' and fkId="+tnames[2]+"."+keyColumn+")"; + if(!StringUtils.isEmpty(tnames[2])) { + sqlWhere+=" and "+tnames[5]+">=cast('"+lastUpdateTime+"' as datetime)"; + } else { + map.put("isEnd", "1"); + } + map.put("sqlWhere", sqlWhere); + map.put("dataWhere", tnames[7]); + map.put("tableKey", tnames[0]); + map.put("tableName", tnames[2]); + map.put("masterLinkColumn", tnames[3]); + map.put("subLinkColumn", tnames[4]); + map.put("filePathColumn", tnames[6]); + map.put("messageType", tnames[8]); + map.put("messageName", tnames[8]); + 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)) { + setUpdateTime(tnames[2],DateUtil.formatDate(nowUpdateTime,"yyyy-MM-dd HH:mm:ss")); + } + } + } + } + + + + private String fetchData(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(); + } + /****/ + @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"); + + 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; + 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(); + } } - saveUploadStatus(json.getString("messageType"), json.getString("messageId"), fileName); + saveUploadStatus(json.getString("messageType"),json.getString("messageId"),fileName); /*解析入库*/ - if (isLastLevel(direction)) { - analyToDB(json, files); + if(isLastLevel()) { + analyToDB(json,files); } else { - //需要转发 - if (isRelay(direction)) { - relay(direction, request.getHeader("reqNo"), content, saveFiles); - } + //需要转发 + if(isRelay()) { + relay(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) { - try { - if (sendOnPage(messageType, tableName, params)) - return ResultVOUtils.success(); - } catch (Exception ex) { - logger.error(ex.getMessage()); - } - 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 = false; - 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 (tableName.equals("thr_order")) - logger.info("tableName-->" + tableName + "-->row-->" + total + "-->sqlWhere-->" + where); - if (total > 0) { - success = true; - params.put("page", 0); - params.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++) { - 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(keyColumn) + "','" + 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() : ""; - 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"; - 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 = - direction.equals("U") ? getSystemParamConfig("upper_server_host", "自助平台数据接收服务地址", "", "接收由UDI系统上传的数据") : - direction.equals("I") ? getSystemParamConfig("lower_server_host", "UDI系统数据接收服务地址", "", "接收由自助平台下发的数据") : null; - 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(String direction) { - return direction.equals("I") || direction.equals("U") ? true : false; - } - - /*检查当前系统为自助平台(下发)还是UDI系统(上传),返回传输方向,如果都未设置,默认返回下发*/ - private String getDirection() { - SystemParamConfigEntity systemParamConfigEntity = getSystemParamConfig("sync_system_type", "系统类型", "IDC", "UDI(UDI管理系统,由UDI系统往自助平台上传数据),SPS(自助平台,由自助平台往UDI系统下发数据),IDC(中继服务,只接收,转发或暂存数据供下一级服务拉取数据)"); - return systemParamConfigEntity.getParamValue().equals("UDI") ? "U" : systemParamConfigEntity.getParamValue().equals("SPS") ? "I" : "N"; - } - - /*是否需要转发*/ - private boolean isRelay(String direction) { - String relayHost = getNextHost(direction); - - return !StringUtils.isEmpty(relayHost); - } - - - /*由表名获取数据*/ - 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()), "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_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); - } - - } - + 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(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 saveExportStatus(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_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 isUplaod) { + boolean success=false; + String tableName = params.get("tableName").toString(); + String tableKey = params.get("tableKey").toString(); + String[] childs = {}; + 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 whMap = new HashMap(); + whMap.put("sqlWhere", params.get("sqlWhere")); + whMap.put("dataWhere", params.get("dataWhere")); + + Map map = new HashMap(); + String where = DBAUtils.convertWhere(column,whMap,""); + 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; + params.put("page", 0); + params.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); + 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&¶ms.get("masterLinkColumn")!=null&¶ms.get("subLinkColumn")!=null) { + for(int z=0;z childMap = new HashMap<>(); + childMap.put(params.get("subLinkColumn").toString(), keyValue); + List> childList = getList(chidTnames[2],childMap); + list.get(m).put("tableName"+z, chidTnames[2]); + list.get(m).put("data"+z, childList); + } + } + } + + orderNum++; + if(isUplaod) { + String result = relay("",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); + } + } + } else { + syncAddTask(data); + } + } + } + } + + } + + return success; + } + + /*增加同步任务*/ + private void syncAddTask(Map json) { + String content = JSON.toJSONString(json); + String datePath = DateUtil.formatDate(new Date(),"yyyy-MM-dd"); + + FileUtils.makeDirectory(filePath +datePath+"/"); + FileUtils.makeDirectory(backFilePath +datePath+"/"); + String fileName = filePath +datePath+"/"+json.get("messageType")+"-"+json.get("messageId")+".udi"; + String backFileName = backFilePath +datePath+"/"+json.get("messageType")+"-"+json.get("messageId")+".udi"; + FileUtils.SaveFileAs(content, fileName); + FileUtils.SaveFileAs(content, backFileName); + + + saveExportStatus(json.get("messageType").toString(),json.get("messageId").toString(),fileName); + } + + + + /*解析到数据库*/ + 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;i0) + 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;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<6;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); + } + } + + } + if(n>0) + dbDao.save(sql); + + } + + } + + + /*转发下级或上级中继服务*/ + private String relay(String reqNo,String content,String[] files) { + String host = getNextHost(); + 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); + } + + + 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 : ""; + params.put("sql", sql); + List> list = dbDao.list(params); + return list; + } + /*由表名获取数据*/ + private Map getData(String messageType,String tableName,Map params) { + List> list = getList(tableName,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 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/idc/thread/AsyncFetchTask.java b/src/main/java/com/glxp/api/idc/thread/AsyncFetchTask.java index 81c46f0f..3b24ee4a 100644 --- a/src/main/java/com/glxp/api/idc/thread/AsyncFetchTask.java +++ b/src/main/java/com/glxp/api/idc/thread/AsyncFetchTask.java @@ -1,9 +1,7 @@ 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 javax.annotation.Resource; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.EnableScheduling; @@ -12,13 +10,17 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; -import javax.annotation.Resource; +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; + @Component @EnableScheduling public class AsyncFetchTask implements SchedulingConfigurer { - final Logger logger = LoggerFactory.getLogger(AsyncIdcTask.class); + final Logger logger = LoggerFactory.getLogger(AsyncFetchTask.class); @Resource private ScheduledDao scheduledDao; @@ -34,7 +36,7 @@ public class AsyncFetchTask implements SchedulingConfigurer { scheduledRequest.setCronName("syncFetch"); logger.info("syncFetch----------------"); ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); - String cron = scheduledEntity != null ? scheduledEntity.getCron() : "* 0/30 * * * ?"; + String cron = scheduledEntity!=null ? scheduledEntity.getCron() : "* 3/30 * * * ?"; if (cron.isEmpty()) { logger.error("cron is null"); @@ -45,9 +47,9 @@ public class AsyncFetchTask implements SchedulingConfigurer { } private void process() { - logger.info("syncFetch----process------------"); + logger.info("syncFetch----process------------"); - idcService.asyncFetchTask(); - } + idcService.asyncFetchTask(); + } } diff --git a/src/main/java/com/glxp/api/idc/utils/DBAUtils.java b/src/main/java/com/glxp/api/idc/utils/DBAUtils.java index 8858fbf3..8fa5633c 100644 --- a/src/main/java/com/glxp/api/idc/utils/DBAUtils.java +++ b/src/main/java/com/glxp/api/idc/utils/DBAUtils.java @@ -1,400 +1,399 @@ -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; +package com.glxp.sale.admin.idc.utils; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.glxp.sale.admin.util.DateUtil; + + /** * @author chenqf */ public class DBAUtils { - private static final Logger logger = LoggerFactory.getLogger(DBAUtils.class); + 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; + public static String parseWhere(String sql,Map columns,Map params,String dataWhere) { + return parseWhere(sql,columns,params,dataWhere,false); } - - - /*生成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("'", "CCCBBBAAA12345ZZZYYYXXX"); - value = value.replaceAll("CCCBBBAAA12345ZZZYYYXXX", "''"); - } - return value; - } - - + public static String parseWhere(String sql,Map columns,Map params,String dataWhere,boolean isSimpleWhere) { + String where=""; + boolean isAnd0 = false; + boolean isLike = params!=null&¶ms.get("isLike")!=null&¶ms.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&&h1&&j"); + 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&¶ms.get("isLike")!=null&¶ms.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-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;n2&&t.substring(0, 2).toLowerCase().equals("2d")) { + t = t.substring(2); + for(int i=0;i-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 process(), triggerContext -> { ScheduledRequest scheduledRequest = new ScheduledRequest(); - scheduledRequest.setCronName("syncIdc"); - logger.info("syncIdc----------------"); + 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/30 * * * ?"; if (cron.isEmpty()) { logger.error("cron is null"); } - logger.info("syncIdc----------------"); + logger.info("syncIdcSps----------------"); return new CronTrigger(cron).nextExecutionTime(triggerContext); }); } private void process() { - logger.info("syncIdc----process------------"); + logger.info("syncIdcSps----process------------"); - idcService.asyncIdcTask(); - } + idcService.asyncSpsTask(); + } }