增加自助平台同步任务
							parent
							
								
									d022e057fb
								
							
						
					
					
						commit
						a4bd9353fd
					
				| @ -1,68 +1,225 @@ | |||||||
| package com.glxp.api.idc.controller; | 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.common.res.BaseResponse; | ||||||
| import com.glxp.api.dao.idc.DbDao; | import com.glxp.api.dao.idc.DbDao; | ||||||
| import com.glxp.api.idc.service.IdcService; | import com.glxp.api.idc.service.IdcService; | ||||||
| import org.springframework.web.bind.annotation.*; |  | ||||||
| import org.springframework.web.multipart.MultipartFile; |  | ||||||
| 
 | 
 | ||||||
| import javax.annotation.Resource; | import cn.hutool.core.io.IoUtil; | ||||||
| import javax.servlet.http.HttpServletRequest; | import lombok.extern.slf4j.Slf4j; | ||||||
| import java.util.Map; |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 中继服务接口 |  * 中继服务接口 | ||||||
|  */ |  */ | ||||||
|  | @Slf4j | ||||||
| @RestController | @RestController | ||||||
| public class IdcController { | public class IdcController { | ||||||
| 	@Resource |     @Resource | ||||||
| 	IdcService idcService; |     IdcService idcService; | ||||||
| 	@Resource |     @Resource | ||||||
| 	DbDao dbDao; |     DbDao dbDao; | ||||||
| 	/*通用上传接口*/ | 
 | ||||||
| 	//@Log("中继服务数据接收,下级往上级上传")
 |     /*通用上传接口*/ | ||||||
| 	@RequestMapping(value = "/spssync/common/upload") |     //@Log("中继服务数据接收,下级往上级上传")
 | ||||||
| 	public BaseResponse upload(HttpServletRequest request, |     @RequestMapping(value = "/spssync/common/upload") | ||||||
| 							   @RequestParam("content") String content, |     public BaseResponse upload(HttpServletRequest request, | ||||||
| 							   @RequestParam(value = "files", required = false) MultipartFile[] files) { |                                @RequestParam("content") String content, | ||||||
| 		//
 |                                @RequestParam(value = "files", required = false) MultipartFile[] files) { | ||||||
| 		return idcService.receive("U",request, content, files); |         //
 | ||||||
| 	} |         return idcService.receive( request, content, files); | ||||||
| 	/*通用下发接口*/ |     } | ||||||
| 	//@Log("中继服务数据接收,上级往下级下发")
 | 
 | ||||||
| 	@RequestMapping(value = "/spssync/common/issued") |    | ||||||
| 	public BaseResponse issued(HttpServletRequest request, |     //@Log("数据同步测试")
 | ||||||
| 			@RequestParam("content") String content, |     @RequestMapping(value = "/spssync/common/test") | ||||||
| 			@RequestParam(value = "files", required = false) MultipartFile[] files) { |     public BaseResponse test(HttpServletRequest request, @RequestBody Map<String, Object> params) { | ||||||
| 		//
 |         //
 | ||||||
| 		return idcService.receive("I",request, content, files); |         return idcService.send(params); | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	//@Log("数据同步测试")
 |     //@Log("数据同步任务列表拉取")
 | ||||||
| 	@RequestMapping(value = "/spssync/common/test") |     @RequestMapping(value = "/spssync/common/list") | ||||||
| 	public BaseResponse test(HttpServletRequest request,@RequestBody Map<String, Object> params) { |     public BaseResponse list(HttpServletRequest request, @RequestBody(required = false) Map<String, Object> params) { | ||||||
| 		//
 |         return idcService.taskList(request, params); | ||||||
| 		return idcService.send(params); |     } | ||||||
| 	} | 
 | ||||||
| 
 |     //@Log("数据同步任务根据任务ID下载数据")
 | ||||||
| 	//@Log("数据同步任务列表拉取")
 |     @RequestMapping(value = "/spssync/common/download") | ||||||
| 	@RequestMapping(value = "/spssync/common/list") |     @ResponseBody | ||||||
| 	public BaseResponse list(HttpServletRequest request,@RequestBody(required = false) Map<String, Object> params) { |     public BaseResponse download(HttpServletRequest request, @RequestBody Map<String, Object> params) { | ||||||
| 		return idcService.taskList(request,params); |         return idcService.download(request, params); | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	//@Log("数据同步任务根据任务ID下载数据")
 |     //@Log("数据同步任务根据任务ID,返回下载成功标记")
 | ||||||
| 	@RequestMapping(value = "/spssync/common/download") |     @RequestMapping(value = "/spssync/common/success") | ||||||
| 	@ResponseBody |     @ResponseBody | ||||||
| 	public BaseResponse download(HttpServletRequest request,@RequestBody Map<String, Object> params) { |     public BaseResponse success(HttpServletRequest request, @RequestBody Map<String, Object> params) { | ||||||
| 		return idcService.download(request,params); |         return idcService.downlaodSuccess(request, params); | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	//@Log("数据同步任务根据任务ID,返回下载成功标记")
 |     @RequestMapping("/mapi/**") | ||||||
| 	@RequestMapping(value = "/spssync/common/success") |     public Object mapi(HttpServletRequest request, HttpServletResponse httpServletResponse) { | ||||||
| 	@ResponseBody |         String uri = parseUri(request.getRequestURL().toString()); | ||||||
| 	public BaseResponse success(HttpServletRequest request,@RequestBody Map<String, Object> params) { | 
 | ||||||
| 		return idcService.downlaodSuccess(request,params); |         if (uri.contains("getImage")) { | ||||||
| 	} |             return redirectIamge(request, httpServletResponse, uri); | ||||||
|  |         } else { | ||||||
|  |             return redirect(request, uri); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     HttpEntity<String> buildHeader(HttpServletRequest request) { | ||||||
|  | 
 | ||||||
|  |         log.info(request.getMethod()); | ||||||
|  | 
 | ||||||
|  |         String bodyContent = ""; | ||||||
|  |         Map<String, Object> headerParam = new HashMap<>(); | ||||||
|  | 
 | ||||||
|  |         if (request.getMethod().equals(HttpMethod.GET)) { | ||||||
|  |             Map<String, Object> bodyParam = new HashMap<String, Object>(); | ||||||
|  |             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<String> 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<String> 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<String> httpEntity = buildHeader(request);
 | ||||||
|  |         log.info(request.getMethod()); | ||||||
|  |         Map<String, Object> headerParam = new HashMap<>(); | ||||||
|  |         Map<String, Object> bodyParam = new HashMap<String, Object>(); | ||||||
|  |         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<String> 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<String> http = new HttpEntity<>(JSON.toJSONString(bodyParam), headers); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         log.info(uri); | ||||||
|  | //        ResponseEntity<Resource> entity = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, Resource.class);
 | ||||||
|  | //        ResponseEntity<Resource> responseEntity = restTemplate.exchange(uri,
 | ||||||
|  | //                HttpMethod.GET, http, Resource.class, new Object[0]);
 | ||||||
|  | //        ResponseEntity<HttpServletResponse> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, http, HttpServletResponse.class);
 | ||||||
|  | 
 | ||||||
|  |         ResponseEntity<byte[]> 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<String> 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<JSONObject> 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; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -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> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, | ||||||
|  |                               @Nullable ResponseExtractor<T> 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(); | ||||||
|  |             } | ||||||
|  |         }*/ | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,31 +1,35 @@ | |||||||
| package com.glxp.api.idc.service; | 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; | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| /*数据中继数据中心(接收)*/ | import javax.servlet.http.HttpServletRequest; | ||||||
| public interface IdcService { |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     BaseResponse receive(String direction, HttpServletRequest request, |  | ||||||
|                          String content, MultipartFile[] files); |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     BaseResponse send(Map<String, Object> params); |  | ||||||
| 
 |  | ||||||
|     BaseResponse send(String messageType, String tableName, Map<String, Object> params); |  | ||||||
| 
 | 
 | ||||||
|     BaseResponse taskList(HttpServletRequest request, Map<String, Object> params); | import org.springframework.web.multipart.MultipartFile; | ||||||
| 
 | 
 | ||||||
|     BaseResponse download(HttpServletRequest request, Map<String, Object> params); | import com.glxp.api.common.res.BaseResponse; | ||||||
| 
 | 
 | ||||||
|     public void asyncFetchTask(); |  | ||||||
| 
 | 
 | ||||||
|     BaseResponse downlaodSuccess(HttpServletRequest request, Map<String, Object> params); | /*数据中继数据中心(接收)*/ | ||||||
|  | public interface IdcService { | ||||||
| 
 | 
 | ||||||
|  | 	 | ||||||
|  | 	BaseResponse receive(HttpServletRequest request, | ||||||
|  | 			String content,MultipartFile[] files); | ||||||
|  | 
 | ||||||
|  | 	 | ||||||
|  | 	BaseResponse send(Map<String,Object> params); | ||||||
|  | 	BaseResponse send(String messageType,String tableName,Map<String,Object> params); | ||||||
|  | 	 | ||||||
|  | 	BaseResponse taskList(HttpServletRequest request,Map<String,Object> params); | ||||||
|  | 	 | ||||||
|  | 	BaseResponse download(HttpServletRequest request,Map<String,Object> params); | ||||||
|  | 	 | ||||||
|  | 	public void asyncFetchTask(); | ||||||
|  | 	 | ||||||
|  | 	BaseResponse downlaodSuccess(HttpServletRequest request,Map<String,Object> params); | ||||||
|  | 	 | ||||||
|     public void asyncIdcTask(); |     public void asyncIdcTask(); | ||||||
| 
 |     void asyncUdiTask(); | ||||||
|  |     void asyncSpsTask(); | ||||||
|  |   | ||||||
| } | } | ||||||
|  | |||||||
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								| @ -1,400 +1,399 @@ | |||||||
| package com.glxp.api.idc.utils; | package com.glxp.sale.admin.idc.utils; | ||||||
| 
 |  | ||||||
| import com.glxp.api.util.DateUtil; |  | ||||||
| import org.apache.commons.lang3.StringUtils; |  | ||||||
| import org.slf4j.Logger; |  | ||||||
| import org.slf4j.LoggerFactory; |  | ||||||
| 
 | 
 | ||||||
| import java.util.Date; | import java.util.Date; | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.Iterator; | import java.util.Iterator; | ||||||
| import java.util.Map; | 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 |  * @author chenqf | ||||||
|  */ |  */ | ||||||
| public class DBAUtils { | 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,"; |     private static final String keywords = ",limit,offset,sort,order,isLike,menuId,menuCode,page,isAuto,pageNo,pageSize,pageNum,sqlOrder,join,"; | ||||||
| 
 |        | ||||||
|     /*解析where条件*/ |     /*解析where条件*/ | ||||||
|     public static String parseWhere(String sql, Map<String, Object> columns, Map<String, Object> params, String dataWhere) { |     public static String parseWhere(String sql,Map<String,Object> columns,Map<String,Object> params,String dataWhere) { | ||||||
|         return parseWhere(sql, columns, params, dataWhere, false); |     	return parseWhere(sql,columns,params,dataWhere,false); | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public static String parseWhere(String sql, Map<String, Object> columns, Map<String, Object> 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("<if")) { |  | ||||||
|             where = convertWhere(columns, params, dataWhere, sql); |  | ||||||
|             if (!sql.contains("<if") && !StringUtils.isEmpty(where)) { |  | ||||||
|                 sql += sql.toLowerCase().contains(" where ") ? " and " + where : " where " + where; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|         int n = 0; |  | ||||||
|         if (sql.contains("<if")) { |  | ||||||
|             int s1 = sql.indexOf("<if"); |  | ||||||
|             if (isSimpleWhere && s1 > 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("<if")) { |  | ||||||
|                 int a = sql.indexOf("<if"); |  | ||||||
|                 int b = sql.indexOf(">", 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("</if>"); |  | ||||||
|                 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("<where>")) { |  | ||||||
|             sql = n > 0 ? sql.replaceAll("<where>", " where ") : sql.replaceAll("<where>", ""); |  | ||||||
|             if (!StringUtils.isEmpty(dataWhere) && !sql.contains("sqlWhere")) { |  | ||||||
|                 sql = n > 0 ? sql.replaceAll("</where>", " and (" + dataWhere + ")") : sql.replaceAll("</where>", " (" + dataWhere + ")"); |  | ||||||
|             } else { |  | ||||||
|                 sql = sql.replaceAll("</where>", ""); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|         if (sql.contains("=isLike")) { |  | ||||||
|             int a1, a2, b1, b2; |  | ||||||
|             a1 = sql.indexOf("[==isLike]"); |  | ||||||
|             a2 = sql.indexOf("</if>", 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("</if>", 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<String, Object> columns, Map<String, Object> 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<String, Object> map = (Map<String, Object>) 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<String, Object> colums, Map<String, Object> params, String dataWhere) { |  | ||||||
|         String where = convertWhere(colums, params, dataWhere, ""); |  | ||||||
|         return where; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public static String convertWhere(Map<String, Object> colums, Map<String, Object> 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<String,Object> columns,Map<String,Object> params,String dataWhere,boolean isSimpleWhere) { | ||||||
| 
 | 		String where=""; | ||||||
|     /*生成insert语句*/ | 		boolean isAnd0 = false; | ||||||
|     public static String parseInsert(Map<String, Object> params, Map<String, Object> columns) { | 		boolean isLike = params!=null&¶ms.get("isLike")!=null&¶ms.get("isLike").toString().equals("true") ? true : false; | ||||||
|         String ins = ""; | 		 | ||||||
|         String vas = ""; | 		if(sql.contains("sqlWhere")||!sql.contains("<if")) { | ||||||
|         String sql = ""; | 			where = convertWhere(columns,params,dataWhere,sql); | ||||||
|         for (String key : columns.keySet()) { | 			if(!sql.contains("<if")&&!StringUtils.isEmpty(where)) { | ||||||
|             String value = ""; | 				sql+=sql.toLowerCase().contains(" where ") ? " and "+where : " where " +where; | ||||||
| 
 | 			} | ||||||
|             if (params.get(key) != null) | 		} | ||||||
|                 value = params.get(key).toString(); | 	    int n=0; | ||||||
|             Map<String, Object> map = (Map<String, Object>) columns.get(key); | 		if(sql.contains("<if")) { | ||||||
|             String dataType = map.get("dataType").toString(); | 			int s1 = sql.indexOf("<if"); | ||||||
|             String extra = map.get("extra") != null && map.get("extra").toString().toLowerCase().contains("auto") ? "A" : "N"; | 			if(isSimpleWhere&&s1>0) { | ||||||
| 
 | 				for(int i=s1-1;i>-1;i--) { | ||||||
|             if (map.get("columnKey").toString().equals("PRI") && extra.equals("N")) { | 					if(!sql.substring(i,i+1).equals(" ")&&!sql.substring(i,i+1).equals("\n")) { | ||||||
|                 if (StringUtils.isEmpty(value)) { | 						isAnd0 = true; | ||||||
| 
 | 					} | ||||||
|                 } | 				} | ||||||
|             } | 			} | ||||||
| 
 | 			if(!StringUtils.isEmpty(dataWhere)) { | ||||||
|             value = DBAUtils.escape(value); | 				if(sql.contains("sqlWhere")) { | ||||||
|             if (!StringUtils.isEmpty(value)) { | 					if(params.get("sqlWhere")!=null&&!StringUtils.isEmpty(params.get("sqlWhere").toString())) { | ||||||
|                 if (!StringUtils.isEmpty(ins)) | 						String temp = params.get("sqlWhere").toString(); | ||||||
|                     ins += ","; | 						params.replace("sqlWhere", "("+temp+") and ("+dataWhere+")"); | ||||||
|                 ins += map.get("columnName").toString(); | 					} else { | ||||||
| 
 | 						params.put("sqlWhere", dataWhere); | ||||||
|                 if (!StringUtils.isEmpty(vas)) | 					} | ||||||
|                     vas += ","; | 				} | ||||||
| 
 | 			} | ||||||
|                 if (StringUtils.isEmpty(value)) { | 			while(sql.contains("<if")) { | ||||||
|                     vas += "null"; | 				int a = sql.indexOf("<if"); | ||||||
|                 } else { | 				int b = sql.indexOf(">",a+1); | ||||||
|                     if (dataType.equals("C")) { | 				int h = sql.indexOf("isLike",a+1); | ||||||
|                         vas += "'" + value + "'"; | 				int j = sql.indexOf("!=",a+1); | ||||||
|                     } else if (dataType.equals("N")) { | 				if(h>a&&h<b) { | ||||||
|                         vas += value; | 					sql = j>1&&j<b ? sql.substring(0,a)+"[!=isLike]"+sql.substring(b+1) : sql.substring(0,a)+"[==isLike]"+sql.substring(b+1); | ||||||
|                     } else { | 					n=0; | ||||||
|                         String str = ""; | 					continue; | ||||||
|                         Date date = new Date(); | 				} | ||||||
|                         if (StringUtils.isNumeric(value)) { | 				 | ||||||
|                             date = new Date(Long.valueOf(value)); | 				int c = sql.indexOf("</if>"); | ||||||
|                             str = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); | 				String str = sql.substring(b+1,c); | ||||||
|                         } else { | 				int e1 = str.indexOf("#{"); | ||||||
|                             str = DateUtil.formatDate(IDCUtils.parseDate(value), "yyyy-MM-dd HH:mm:ss"); | 				int e2 = str.indexOf("}",e1 +1); | ||||||
|                         } | 				if(e1<0) { | ||||||
|                         vas += "cast('" + str + "' as datetime)"; | 					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(ins) && !StringUtils.isEmpty(vas)) { | 					if(StringUtils.isEmpty(value)) | ||||||
|             sql = "(" + ins + ") values (" + vas + ")"; | 						value = where; | ||||||
|         } | 				} | ||||||
| 
 | 				if(!StringUtils.isEmpty(value)) { | ||||||
|         return sql; | 					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 ")) { | ||||||
|     public static String convertColumnType(String dataType, String dbType, String width) { | 								str = str.substring(str.toLowerCase().indexOf("and ")+3); | ||||||
|         String columnType = dataType.toLowerCase(); | 							} | ||||||
|         if (dbType.contains("oracle.")) { | 							if(str.trim().substring(0, 3).toLowerCase().equals("or ")) { | ||||||
|             columnType = columnType.contains("varchar") ? width.contains("(") ? ("varchar2" + width) : ("varchar2(" + width + ")") : | 								str = str.substring(str.toLowerCase().indexOf("or ")+2); | ||||||
|                     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 { | 					n++; | ||||||
|             columnType = columnType.contains("varchar") ? width.contains("(") ? ("varchar" + width) : ("varchar(" + width + ")") : | 				} else { | ||||||
|                     columnType.contains("char") ? width.contains("(") ? ("char" + width) : ("char(" + width + ")") : | 					str = ""; | ||||||
|                             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" : ""; | 				sql = sql.length()>c+5 ? sql.substring(0,a) +str+sql.substring(c+5) : sql.substring(0,a) +str; | ||||||
|         } | 			} | ||||||
| 
 | 			 | ||||||
|         return columnType; | 		}  | ||||||
|     } | 		if(sql.contains("<where>")) { | ||||||
| 
 | 			sql = n>0 ? sql.replaceAll("<where>", " where ") : sql.replaceAll("<where>", ""); | ||||||
|     private static String aliasNameChars = "_hijklmnopqrstuvwxyzabcdefg9876543210"; | 			if(!StringUtils.isEmpty(dataWhere)&&!sql.contains("sqlWhere")) { | ||||||
|     private static String realNamechars = "abcdefghijklmnopqrstuvwxyz_0123456789"; | 				sql = n>0 ? sql.replaceAll("</where>", " and ("+dataWhere+")") : sql.replaceAll("</where>", " ("+dataWhere+")"); | ||||||
| 
 | 			} else { | ||||||
|     /*表名简单转义*/ | 				sql = sql.replaceAll("</where>", ""); | ||||||
|     public static String tableAliasName(String tableName) { | 			} | ||||||
|         String tname = "2d"; | 		} | ||||||
|         for (int i = 0; i < tableName.length(); i++) { | 		if(sql.contains("=isLike")) { | ||||||
|             int idx = realNamechars.indexOf(tableName.substring(i, i + 1).toLowerCase()); | 			int a1,a2,b1,b2; | ||||||
|             if (idx > -1) { | 			a1 = sql.indexOf("[==isLike]"); | ||||||
|                 tname += aliasNameChars.substring(idx, idx + 1); | 			a2 = sql.indexOf("</if>",a1+1); | ||||||
|             } else { | 			 | ||||||
|                 tname += tableName.substring(i, i + 1).toLowerCase(); | 			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); | ||||||
|         return tname; | 				} else { | ||||||
|     } | 					sql = sql.length()>a2+5 ? sql.substring(0,a1)+sql.substring(a2+5) : sql.substring(0,a1); | ||||||
| 
 | 				} | ||||||
|     /*获取实际表名*/ | 			} | ||||||
|     public static String tableRealName(String tableName) { | 			b1 = sql.indexOf("[!=isLike]"); | ||||||
|         String tname = ""; | 			b2 = sql.indexOf("</if>",b1 +1); | ||||||
|         String result = ""; | 			if(b1>0&&b2>0) { | ||||||
|         tableName = tableName.replaceAll("202c", ","); | 				if(!isLike) { | ||||||
|         String[] tabs = tableName.split(","); | 					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); | ||||||
|         for (int n = 0; n < tabs.length; n++) { | 				} else { | ||||||
|             String t = tabs[n]; | 					sql = sql.length()>b2+5 ? sql.substring(0,b1)+sql.substring(b2+5) : sql.substring(0,b1); | ||||||
|             tname = ""; | 				} | ||||||
|             if (!StringUtils.isEmpty(t)) { | 			} | ||||||
|                 if (!StringUtils.isEmpty(result)) | 		} | ||||||
|                     result += ","; | 		 | ||||||
|                 if (t.length() > 2 && t.substring(0, 2).toLowerCase().equals("2d")) { | 		sql =parseParams(sql,columns,params); | ||||||
|                     t = t.substring(2); | 		return sql; | ||||||
|                     for (int i = 0; i < t.length(); i++) { | 	} | ||||||
|                         int idx = aliasNameChars.indexOf(t.substring(i, i + 1).toLowerCase()); | 	 | ||||||
|                         if (idx > -1) { | 	public static String parseParams(String sql,Map<String,Object> columns,Map<String,Object> params) { | ||||||
|                             tname += realNamechars.substring(idx, idx + 1); | 		String[] iStr={"#{","${"}; | ||||||
|                         } else { | 		for(int k=0;k<2;k++) { | ||||||
|                             tname += t.substring(i, i + 1); | 			while(sql.contains(iStr[k])) { | ||||||
|                         } | 				int a1 = sql.indexOf(iStr[k]); | ||||||
|                     } | 				int a2 = sql.indexOf("}",a1 +1); | ||||||
|                 } else { | 				String key = sql.substring(a1+2,a2); | ||||||
|                     tname = t; | 				Object obj = params.get(key); | ||||||
|                 } | 				String value = obj!=null ? obj.toString() : ""; | ||||||
|                 result += tname; | 				String dataType = "0"; | ||||||
|             } | 				if(columns!=null) { | ||||||
|         } | 					Map<String,Object> map = (Map<String, Object>) columns.get(key); | ||||||
|         return result; | 					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); | ||||||
|     public static String tableJoinWhere(String tableName, Map<String, Object> columns) { | 			} | ||||||
|         String result = ""; | 		} | ||||||
|         if (tableName.contains(",")) { | 		 | ||||||
|             String[] tabs = tableName.split(","); | 		return sql; | ||||||
|             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; | 	/*生成where条件*/ | ||||||
| 
 | 	public static String convertWhere(Map<String,Object> colums,Map<String,Object> params,String dataWhere) { | ||||||
|     } | 		String where=convertWhere(colums,params,dataWhere,""); | ||||||
| 
 | 		return where; | ||||||
| 
 | 	} | ||||||
|     /*从update语句解析select*/ | 	public static String convertWhere(Map<String,Object> colums,Map<String,Object> params,String dataWhere,String sql) { | ||||||
|     public static String updateSqlToSelectSql(String sql, Map<String, Object> map) { | 		String where=""; | ||||||
|         String aSql = sql.toLowerCase(); | 		String value =""; | ||||||
|         String result = ""; | 		boolean isLike = false; | ||||||
|         if (map == null) | 		boolean isKey = false; | ||||||
|             map = new HashMap<String, Object>(); | 		if(params!=null&¶ms.get("isLike")!=null&¶ms.get("isLike").toString().equals("true")) | ||||||
|         if (aSql.contains("update ")) { | 			isLike = true; | ||||||
|             int uIdx = aSql.indexOf("update "); | 		if(colums!=null) { | ||||||
|             int sIdx = aSql.indexOf(" set "); | 			Iterator keys = colums.keySet().iterator(); | ||||||
|             String tName = aSql.substring(uIdx + 7, sIdx); | 			isKey = keys.hasNext(); | ||||||
|             int wIdx = aSql.indexOf(" where "); | 		} | ||||||
|             String where = aSql.substring(wIdx + 7); | 		for(String key : params.keySet()){ | ||||||
|             if (where.contains("=")) { | 			Object obj = params.get(key); | ||||||
|                 result = "select * from " + tName + " where "; | 			value = ""; | ||||||
|                 String[] tWh = where.split(" and "); | 			if(obj!=null) | ||||||
|                 for (int i = 0; i < tWh.length; i++) { | 				value = obj.toString(); | ||||||
|                     if (tWh[i].contains("=")) { | 			if(!StringUtils.isEmpty(value)&&!keywords.contains(","+key+",")&&(StringUtils.isEmpty(sql)||(!StringUtils.isEmpty(sql)&&!sql.contains("{"+key+"}")))) { | ||||||
|                         if (!tWh[i].substring(0, tWh[i].indexOf("=")).toLowerCase().trim().equals("data_version")) { | 				 | ||||||
|                             result += tWh[i]; | 				if(key.equals("sqlWhere")||key.equals("join")) { | ||||||
|                             map.put("businessId", tWh[i].substring(tWh[i].indexOf("=") + 1).replaceAll("'", "")); | 					where+=(!StringUtils.isEmpty(where) ? " and " : "") +" "+value; | ||||||
|                             break; | 				} else { | ||||||
|                         } | 					String[] cns = key.split(","); | ||||||
|                     } | 					String wh = ""; | ||||||
|                 } | 					for (String cn:cns) { | ||||||
|             } | 						String columnName = cn;//StringUtils.propertyToField(cn);
 | ||||||
|             map.put("tableName", tName); | 						 | ||||||
|         } | 						 | ||||||
|         return result; | 						if(colums!=null&&isKey&&colums.get(cn)==null) { | ||||||
|     } | 							logger.error("列名"+columnName+"不存在,属性"+cn+"错误."); | ||||||
| 
 | 						} else { | ||||||
|     public static String escape(String str) { | 							if(!StringUtils.isEmpty(wh))  | ||||||
|         String value = str; | 								wh+=" or "; | ||||||
|         if (value != null && value.contains("'")) { | 							if(isLike) { | ||||||
|             value = value.replaceAll("'", "CCCBBBAAA12345ZZZYYYXXX"); | 								wh+="instr("+columnName+",'"+value+"')>0"; | ||||||
|             value = value.replaceAll("CCCBBBAAA12345ZZZYYYXXX", "''"); | 							} else { | ||||||
|         } | 								wh+=columnName+"='"+value+"'"; | ||||||
|         return 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<String,Object> params,Map<String,Object> 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<String,Object> map = (Map<String, Object>) 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<String,Object> 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<String,Object> map) { | ||||||
|  | 		String aSql = sql.toLowerCase(); | ||||||
|  | 		String result = ""; | ||||||
|  | 		if(map==null) | ||||||
|  | 			map = new HashMap<String,Object>(); | ||||||
|  | 		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; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	 | ||||||
| } | } | ||||||
|  | |||||||
					Loading…
					
					
				
		Reference in New Issue