中继服务(Idc)首次提交

version1
admin 2 years ago
parent e879dce55f
commit 6144d1f271

@ -0,0 +1,75 @@
package com.glxp.sale.admin.idc.controller;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
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.multipart.MultipartFile;
import com.glxp.sale.admin.annotation.Log;
import com.glxp.sale.admin.idc.dao.DbDao;
import com.glxp.sale.admin.idc.service.IdcService;
import com.glxp.sale.common.res.BaseResponse;
/**
*
*/
@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<String, Object> params) {
//
return idcService.send(params);
}
@Log("数据同步任务列表拉取")
@RequestMapping(value = "/spssync/common/list")
public BaseResponse list(HttpServletRequest request,@RequestBody(required = false) Map<String, Object> params) {
return idcService.taskList(request,params);
}
@Log("数据同步任务根据任务ID下载数据")
@RequestMapping(value = "/spssync/common/download")
@ResponseBody
public BaseResponse download(HttpServletRequest request,@RequestBody Map<String, Object> params) {
return idcService.download(request,params);
}
@Log("数据同步任务根据任务ID,返回下载成功标记")
@RequestMapping(value = "/spssync/common/success")
@ResponseBody
public BaseResponse success(HttpServletRequest request,@RequestBody Map<String, Object> params) {
return idcService.downlaodSuccess(request,params);
}
}

@ -0,0 +1,84 @@
package com.glxp.sale.admin.idc.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/*获取表相关信息*/
@Mapper
public interface DbDao {
@Insert("${sql}")
int save(@Param("sql")String sql);
@Update("${sql}")
int update(@Param("sql")String sql);
@Delete("${sql}")
int delete(@Param("sql")String sql);
Map<String,Object> get(@Param("sql")String sql);
List<Map<String,Object>> list(Map<String,Object> map);
int count(Map<String,Object> map);
@Select("select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables"
+ " where table_schema = (select database())")
List<Map<String, Object>> listMysql();
@Select("select tname as \"tableName\",'oracle' as \"engine\",tname as \"tableComment\",sysdate as \"createTime\" from tab where instr('BIN,ACT',substr(tname,1,3))<1")
List<Map<String, Object>> listOracle();
@Select("select name tableName, 'sqlserver' engine, name tableComment, crdate createTime from SYSOBJECTS"
+ " WHERE XTYPE='U' ORDER BY NAME")
List<Map<String, Object>> listSqlserver();
@Select("select count(*) from information_schema.tables where table_schema = (select database())")
int countMysql(Map<String, Object> map);
@Select("select count(*) from tab where tabtype='TABLE' and instr('BIN,ACT',substr(tname,1,3))<1")
int countOracle(Map<String, Object> map);
@Select("select count(*) from SYSOBJECTS where XTYPE='U'")
int countSqlserver(Map<String, Object> map);
@Select("select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables \r\n"
+ " where table_schema = (select database()) and lower(table_name) = lower(#{tableName})")
Map<String, String> getMysql(String tableName);
@Select("select tname as \"tableName\",'oracle' \"engine\",tname \"tableComment\",sysdate \"createTime\" from tab \r\n"
+ " where tname= upper(#{tableName})")
Map<String, String> getOracle(String tableName);
@Select("select name tableName, 'sqlserver' engine, name tableComment, crdate createTime from SYSOBJECTS"
+ " WHERE lower(name)=lower(#{tableName})")
Map<String, String> getSqlserver(String tableName);
@Select("select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra as extra,table_name tableName, "
+ "concat('(',if(NUMERIC_PRECISION>0,CONVERT(NUMERIC_PRECISION,CHAR),''),if(NUMERIC_SCALE>0,concat(',',CONVERT(NUMERIC_SCALE,CHAR)),''),if(CHARACTER_MAXIMUM_LENGTH>0,CONVERT(CHARACTER_MAXIMUM_LENGTH,CHAR),''),')') as width "
+ " from information_schema.columns\r\n"
+ " where lower(table_name) = lower(#{tableName}) and table_schema = (select database()) order by ordinal_position")
List<Map<String, String>> listColumnsMysql(String tableName);
@Select("select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra as extra,table_name tableName, "
+ "concat('(',if(NUMERIC_PRECISION>0,CONVERT(NUMERIC_PRECISION,CHAR),''),if(NUMERIC_SCALE>0,concat(',',CONVERT(NUMERIC_SCALE,CHAR)),''),if(CHARACTER_MAXIMUM_LENGTH>0,CONVERT(CHARACTER_MAXIMUM_LENGTH,CHAR),''),')') as width "
+ " from information_schema.columns\r\n"
+ " where lower(table_name) = lower(#{tableName}) column_key='PRI' and table_schema = (select database()) order by ordinal_position")
List<Map<String, String>> listKeyMysql(String tableName);
@Select("select cname as \"columnName\",coltype as \"dataType\",cname \"columnComment\",'' as \"columnKey\",'' as \"extra\",tname \"tableName\", "
+" '('||decode(sign(PRECISION),1,to_char(PRECISION),'')||decode(sign(SCALE),1,','||to_char(SCALE),'')||decode(sign(PRECISION),1,'',decode(sign(SCALE),1,'',to_char(width)))||')' as width "
+ "from col \r\n"
+" where tname = upper(#{tableName}) order by colno")
List<Map<String, String>> listColumnsOracle(String tableName);
@Select("select syscolumns.name columnName, systypes.name as dataType, syscolumns.name columnComment, \r\n"
+"(select 'PRI' from sysindexkeys,sysobjects,sysindexes where sysindexkeys.id=syscolumns.id and sysindexkeys.colid=syscolumns.colid \r\n"
+" and sysindexkeys.id=syscolumns.id and sysindexes.id=syscolumns.id and sysindexkeys.indid=sysindexes.indid and sysobjects.name=sysindexes.name and sysobjects.xtype='PK'"
+") columnKey,'' extra,#{tableName} tableName, "
+"'('+case when syscolumns.xprec>0 then cast(syscolumns.xprec as varchar) else '' end + case when syscolumns.xscale>0 then ','+cast(syscolumns.xscale as varchar) else '' end +case when syscolumns.xscale>0 or syscolumns.xprec>0 then '' else cast(syscolumns.length as varchar) end +')' as width "
+ " from syscolumns,\r\n"
+ " systypes"
+ " where syscolumns.xtype=systypes.xtype and syscolumns.id = OBJECT_ID(#{tableName}) order by syscolumns.colid")
List<Map<String, String>> listColumnsSqlserver(String tableName);
@Select("select c.column_name as \"columnName\" from user_constraints con,user_cons_columns c where con.constraint_name=c.constraint_name and con.constraint_type='P' and c.table_name=upper(#{tableName})")
List<Map<String, String>> listKeyOracle(String tableName);
}

@ -0,0 +1,31 @@
package com.glxp.sale.admin.idc.service;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import com.glxp.sale.common.res.BaseResponse;
/*数据中继数据中心(接收)*/
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);
BaseResponse download(HttpServletRequest request,Map<String,Object> params);
void pull();
BaseResponse downlaodSuccess(HttpServletRequest request,Map<String,Object> params);
public void asyncIdcTask();
}

@ -0,0 +1,750 @@
package com.glxp.sale.admin.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.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.sale.admin.dao.info.ScheduledDao;
import com.glxp.sale.admin.entity.info.ScheduledEntity;
import com.glxp.sale.admin.entity.param.SystemParamConfigEntity;
import com.glxp.sale.admin.idc.dao.DbDao;
import com.glxp.sale.admin.idc.service.IdcService;
import com.glxp.sale.admin.idc.utils.DBAUtils;
import com.glxp.sale.admin.idc.utils.IDCUtils;
import com.glxp.sale.admin.req.info.ScheduledRequest;
import com.glxp.sale.admin.service.param.SystemParamConfigService;
import com.glxp.sale.admin.util.CustomUtil;
import com.glxp.sale.admin.util.DateUtil;
import com.glxp.sale.admin.util.FileUtils;
import com.glxp.sale.common.res.BaseResponse;
import com.glxp.sale.common.util.ResultVOUtils;
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;
@Value("${API_KEY}")
private String apiKey;
@Value("${API_SECRET}")
private String apiSecret;
/*自助平台->UDI*/
private final String[] SPMS_TO_UDI_TABLES= {
"basic_udirel/updateTime//耗材字典","basic_products///耗材字典信息详情","company_product_relevance/updateTime//供应商关联信息",
"basic_corp/updateTime//往来单位","thr_bustype_origin/updateTime//第三方单据类型",
"thr_corp/updateTime//第三方往来单位","thr_dept/updateTime//第三方部门",
"thr_inv_products/updateTime//第三方库存","thr_inv_warehouse///第三方仓库",
"thr_order/updateTime//第三方单据","thr_order_detail/updateTime//第三方单据详情",
"thr_products/updateTime//第三方产品信息","sup_cert/updateTime/filePath/资质证书信息",
"sup_cert_set/updateTime//供应商资质证书设置","sup_company/updateTime//配送供应商资质信息",
"sup_manufacturer/updateTime//生产企业资质信息","sup_product/updateTime//产品资质信息"};
/*UDI->自助平台*/
private final String[] UDI_TO_SPMS_TABLES= {
"basic_udirel/updateTime//耗材字典","basic_products///耗材字典信息详情","company_product_relevance/updateTime//供应商关联信息",
"basic_corp/updateTime//往来单位","thr_bustype_origin/updateTime//第三方单据类型",
"thr_corp/updateTime//第三方往来单位","thr_dept/updateTime//第三方部门",
"thr_inv_products/updateTime//第三方库存","thr_inv_warehouse///第三方仓库",
"thr_order/updateTime//第三方单据","thr_order_detail/updateTime//第三方单据详情",
"thr_products/updateTime//第三方产品信息","sup_cert/updateTime/filePath/资质证书信息",
"sup_cert_set/updateTime//供应商资质证书设置","sup_company/updateTime//配送供应商资质信息",
"sup_manufacturer/updateTime//生产企业资质信息","sup_product/updateTime//产品资质信息",
"productinfo/updateTime//国家库UDI数据,udicompany/updateTime//国际库医疗器械注册人信息"};
private int orderNum=0;
@Resource
private SystemParamConfigService systemParamConfigService;
@Resource
private JdbcTemplate jdbcTemplate;
@Resource
private DbDao dbDao;
@Resource
private ScheduledDao scheduledDao;
@Override
public BaseResponse taskList(HttpServletRequest request,Map<String,Object> params) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("sql", "select taskId from basic_upload_status where status='0'");
List<Map<String,Object>> list = dbDao.list(map);
return ResultVOUtils.success(list);
}
@Override
public BaseResponse download(HttpServletRequest request,Map<String,Object> params) {
Map<String,Object> map = dbDao.get("select * from basic_upload_status where taskId='"+params.get("taskId")+"'");
if(map==null)
ResultVOUtils.error(9000, "下载任务不存在");
String json = FileUtils.readFileAll(map.get("cacheFilePath").toString());
JSONObject object = JSON.parseObject(json);
return ResultVOUtils.success(object);
}
/*拉取前一级中继服务数据*/
@Override
public void pull() {
Map<String,Object> query = new HashMap<String,Object>();
String result = post(getNextHost("U")+"/spssync/common/list",query);
JSONObject json = JSON.parseObject(result);
if(json.getInteger("code")==20000&&json.getString("data")!=null) {
List<Map> list = JSON.parseArray(json.getString("data"), Map.class);
if(list!=null) {
for(Map map:list) {
Map<String,Object> params = new HashMap<String,Object>();
params.put("taskId", map.get("taskId"));
pullData(getNextHost("U")+"/spssync/common/list",params);
}
}
}
}
@Async
@Override
public void asyncIdcTask() {
initTable();
String[] tNames= {};
String direction = getDirection();
if(direction.equals("I")) {
tNames = SPMS_TO_UDI_TABLES;
} else {
tNames = UDI_TO_SPMS_TABLES;
}
/*为顶级或末级,以及下游或上游连通,可执行*/
Boolean isLastLevel = isLastLevel(direction);
Boolean isRelay = isRelay(direction);
System.out.print("-----数据传输任务开始----"+direction+"\n");
System.out.print("-----是否顶级或末级服务:"+isLastLevel+",是否转发数据:"+isRelay+"----\n");
if(isLastLevel&&isRelay) {
for(String t:tNames) {
uploadData(t);
}
}
}
private void uploadData(String t) {
String[] tn = t.split("/");
String lastUpdateTime = getUpdateTime(tn[0]);
Date nowUpdateTime = new Date();
Map<String,Object> map = new HashMap<String,Object>();
String sqlWhere = "not exists (select fkId from idc_record where fkId="+tn[0]+".id)";
if(!StringUtils.isEmpty(tn[1])) {
sqlWhere+=" and ifnull("+tn[1]+",now())>=cast('"+lastUpdateTime+"' as datetime)";
} else {
map.put("isEnd", "1");
}
map.put("sqlWhere", sqlWhere);
map.put("tableName", tn[0]);
map.put("filePathColumn", tn[2]);
map.put("messageType", tn[3]);
map.put("messageName", tn[3]);
ScheduledRequest scheduledRequest = new ScheduledRequest();
scheduledRequest.setCronName("sync"+WordUtils.capitalizeFully(tn[0], new char[]{'_'}).replace("_", ""));
ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
String cron = scheduledEntity!=null ? scheduledEntity.getCron() : "";
boolean send = true;
if(!StringUtils.isEmpty(cron)) {
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
Date nextTimePoint = cronSequenceGenerator.next(DateUtil.parseDate(lastUpdateTime));
send = nextTimePoint.before(nowUpdateTime);
}
//if(send) {
BaseResponse result = send(map);
if(result.getCode()==20000) {
setUpdateTime(tn[0],DateUtil.formatDate(nowUpdateTime));
}
//}
}
private String pullData(String url,Map<String,Object> 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<String,Object> 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<String,Object> params) {
String sql = "update basic_upload_status set status='1' where taskId='"+params.get("taskId")+"'";
dbDao.update(sql);
return ResultVOUtils.success();
}
/*direction方向 U 上传I 下发往自助平台为上传往UDI管理系统为下发*/
@Override
public BaseResponse receive(String direction,HttpServletRequest request,
String content,MultipartFile[] files) {
/*授权检查,后续完善*/
String apiKey = request.getHeader("apiKey");
String secretKey = request.getHeader("secretKey");
/*******/
JSONObject json = JSONObject.parseObject(content);
String datePath = DateUtil.formatDate(new Date(),"yyyy-MM-dd");
FileUtils.makeDirectory(filePath +datePath+"/");
FileUtils.makeDirectory(backFilePath +datePath+"/");
String fileName = filePath +datePath+"/"+json.getString("messageType")+"-"+json.getString("messageId")+".udi";
String backFileName = backFilePath +datePath+"/"+json.getString("messageType")+"-"+json.getString("messageId")+".udi";
FileUtils.SaveFileAs(content, fileName);
FileUtils.SaveFileAs(content, backFileName);
String[] saveFiles= {};
if(files!=null) {
saveFiles = new String[files.length];
int n=0;
try {
for(MultipartFile file:files) {
saveFiles[n] = filePath +datePath+"/"+json.getString("messageId")+"/"+file.getOriginalFilename();
writeFile(file.getBytes(), filePath +datePath+"/"+json.getString("messageId")+"/", file.getOriginalFilename());
writeFile(file.getBytes(), backFilePath +datePath+"/"+json.getString("messageId")+"/", file.getOriginalFilename());
n++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
saveUploadStatus(json.getString("messageType"),json.getString("messageId"),fileName);
/*解析入库*/
if(isLastLevel(direction)) {
analyToDB(json,files);
} else {
//需要转发
if(isRelay(direction)) {
relay(direction,request.getHeader("reqNo"),content,saveFiles);
}
}
return ResultVOUtils.success();
}
/*数据同步,从数据库获取数据下发或上传下级中继服务*/
@Override
public BaseResponse send(Map<String,Object> params) {
return send(params.get("messageType").toString(),params.get("tableName").toString(),params);
}
@Override
public BaseResponse send(String messageType,String tableName,Map<String,Object> params) {
if(sendOnPage(messageType,tableName,params))
return ResultVOUtils.success();
return ResultVOUtils.error(9999, "");
}
private void saveUploadStatus(String messageType,String taskId,String fileName) {
Map<String,Object> map = new HashMap<String,Object>();
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<String,Object> 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<String,Object> map = new HashMap<String,Object>();
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<String,Object> 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<String,Object> map = new HashMap<String,Object>();
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<String,Object> 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<String,Object> params) {
boolean success=true;
Map<String,String> table = dbDao.getMysql(tableName);
if(!(table!=null&&table.get("tableName")!=null))
return false;
String sql = "select count(*) from "+tableName;
Map<String,Object> column = getColumn(tableName);
Map<String,Object> map = new HashMap<String,Object>();
String where = DBAUtils.convertWhere(column,params,"");
sql+=!StringUtils.isEmpty(where) ? " where "+where : "";
map.put("sql", sql);
int total = dbDao.count(map);
String direction = getDirection();
String isEnd = params.get("isEnd")!=null ? params.get("isEnd").toString() : "0";
int limit = 50;
String[] files = new String[limit];
String filePathColumn = params.get("filePathColumn")!=null ? params.get("filePathColumn").toString() : "";
orderNum++;
saveIdcLog(messageType,"",tableName +">"+where,0,total);
if(total>0) {
params.put("page", 0);
params.put("limit", limit);
for(int i=0;i<Math.ceil(total/limit)+1;i++) {
params.replace("page", i *limit);
params.replace("limit",limit);
Map<String,Object> data = getData(messageType,tableName,params);
String recordSql = "";
orderNum++;
saveIdcLog(messageType,"",tableName +">"+where,i *limit,total);
if(data.get("data")!=null) {
List<Map> list = JSONObject.parseArray(JSON.toJSONString(data.get("data")), Map.class);
if(list!=null&&list.size()>0) {
for(int m=0;m<list.size();m++) {
if(!StringUtils.isEmpty(filePathColumn)&&list.get(m).get(filePathColumn)!=null) {
files[m] = list.get(m).get(filePathColumn).toString();
}
recordSql+=!StringUtils.isEmpty(recordSql) ? "," : "";
recordSql+="('"+UUID.randomUUID().toString().replaceAll("-", "")+"','"+tableName+"','"+list.get(m).get("id")+"','"+isEnd+"',now())";
}
orderNum++;
String result = relay(direction,"",JSON.toJSONString(data),files);
if(IDCUtils.isJson(result)) {
JSONObject json = JSON.parseObject(result);
if(json.getInteger("code")==20000) {
if(!StringUtils.isEmpty(recordSql)) {
dbDao.save("insert into idc_record (id,type,fkId,isEnd,createTime) values "+recordSql);
}
saveDownloadStatus(messageType,json.getString("reqNo"),messageType+" 记录数:"+list.size()+"/"+total);
saveIdcLog(messageType,"",tableName +">success",i *limit,total);
} else {
success = false;
logger.error(result);
saveIdcLog(messageType,"",tableName +">"+result,i *limit,total);
}
}
}
}
}
}
return success;
}
/*解析到数据库*/
private void analyToDB(JSONObject jsonObject,MultipartFile[] files) {
if(!StringUtils.isEmpty(jsonObject.getString("tableName"))) {
List<Map<String,Object>> list = jsonObject.getObject("data", List.class);
analyData(jsonObject.getString("tableName"),list);
//图片处理
}
}
/*按表名解析数据到数据库,子表暂未处理*/
private void analyData(String tableName,List<Map<String,Object>> 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<Map<String,String>> columnList = dbDao.listColumnsMysql(tName);
int key=0;
int col=0;
for(int i=0;i<columnList.size();i++) {
if(col>0)
sql+=",";
columnList.get(i).put("attrName", columnList.get(i).get("columnName"));
String extra = columnList.get(i).get("extra")!=null&&columnList.get(i).get("extra").toLowerCase().contains("auto_i") ? "A" : "N";
sql+=extra.equals("N") ? columnList.get(i).get("columnName").toString() : "";
col+=extra.equals("N") ? 1 : 0;
if(columnList.get(i).get("columnKey")!=null&&!StringUtils.isEmpty(columnList.get(i).get("columnKey").toString())&&
("PRI,UNI,MUL".contains(columnList.get(i).get("columnKey").toString()))&&extra.equals("N")) {
keyColumn[key] = columnList.get(i).get("columnName").toString();
keyDataType[key]=columnList.get(i).get("dataType").toString().contains("char") ? "C" : "N";
key++;
}
}
sql+=") values ";
if(list!=null&&list.size()>0) {
int n = 0;
int d = 0;
for(int i=0;i<list.size();i++) {
String operateMode = list.get(i).get("operateMode") !=null ? list.get(i).get("operateMode").toString() : "A";
String updateWhere ="";
String updateSet = "";
if("A,D,U".contains(operateMode)) {
for(int z=0;z<keyColumn.length;z++ ) {
if(list.get(i).get(keyColumn[z])!=null&&!StringUtils.isEmpty(list.get(i).get(keyColumn[z]).toString())) {
updateWhere+=!StringUtils.isEmpty(updateWhere) ? " and " : " ";
updateWhere+=keyColumn[z]+" = "+(keyDataType[z].equals("C") ? "'" :"") +list.get(i).get(keyColumn[z]) +(keyDataType[z].equals("C") ? "'" :"");
}
}
if("A,D".contains(operateMode)&&!StringUtils.isEmpty(updateWhere))
dbDao.delete(del+updateWhere);
}
/*A 新增 U 更新*/
if("A,U".contains(operateMode)) {
sql+=n>0 ? "," : "";
sql+="(";
int m = 0;
int h = 0;
for(int k=0;k<columnList.size();k++) {
String extra = columnList.get(k).get("extra")!=null&&columnList.get(k).get("extra").toLowerCase().contains("auto") ? "A" : "N";
String attrName = columnList.get(k).get("attrName");
String value = list.get(i).get(attrName) !=null ? list.get(i).get(attrName).toString() : "";
String dataType = columnList.get(k).get("dataType").toLowerCase().contains("char") || columnList.get(k).get("dataType").toLowerCase().contains("text") ?
"C" : columnList.get(k).get("dataType").toLowerCase().contains("date") ? "D" : "N" ;
if(extra.equals("N")) {
sql+=m>0 ? "," : "";
boolean ups = list.get(i).containsKey(attrName) ? true : false ;
updateSet+=ups&&h>0 ? "," : "";
updateSet+=ups ? columnList.get(k).get("columnName")+"=" : "";
if(StringUtils.isEmpty(value)) {
sql+="null";
updateSet+=ups ? "null" : "";
} else {
if(dataType.equals("C")) {
sql+="'"+value+"'";
updateSet+=ups ? "'"+value+"'" : "";
} else if (dataType.equals("N")) {
sql+=value;
updateSet+=ups ? value : "";
} else {
Date date = new Date();
date = StringUtils.isNumeric(value) ? new Date(Long.valueOf(value)) : IDCUtils.parseDate(value);
String str = DateUtils.format(date, "yyyy-MM-dd HH:mm:ss");
sql+="cast('"+str+"' as datetime)";
updateSet+=ups ? "cast('"+str+"' as datetime)" : "";
}
}
m++;
if(operateMode.equals("U")&&!StringUtils.isEmpty(updateSet)&&!StringUtils.isEmpty(updateWhere))
dbDao.update(upd + updateSet+" where "+updateWhere);
h+=ups ? 1 : 0;
}
}
sql+=")";
n++;
}
}
if(n>0)
dbDao.save(sql);
}
}
/*转发下级或上级中继服务*/
private String relay(String direction,String reqNo,String content,String[] files) {
String host = getNextHost(direction);
String result="";
if(StringUtils.isEmpty(reqNo))
reqNo = UUID.randomUUID().toString();
if(!StringUtils.isEmpty(host)) {
host+=direction.equals("U") ? "/spssync/common/upload" : "/spssync/common/issued";
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)//设置连接超时时间
.readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间
.build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
String fileType = "application/octet-stream";
for (int i = 0; i < files.length; i++) {
if(!StringUtils.isEmpty(files[i])) {
File file = new File(files[i]);
builder.addFormDataPart("files",files[i],
RequestBody.create(MediaType.parse(fileType), file));
}
}
builder.addFormDataPart("content", content);
RequestBody body = builder.build();
Request req = new Request.Builder()
.url(host)
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("format", "json")
.addHeader("apiKey", apiKey)
.addHeader("secretKey", apiSecret)
.addHeader("reqNo", reqNo)
.addHeader("timestamp",DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"))
.addHeader("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept")
.build();
try {
Response response = client.newCall(req).execute();
result=response.body().string();
log.debug("result--->"+result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
log.debug("未配置中继服务地址");
}
return result;
}
/*获取转发服务地址当前值允许单向只使用参数upper_server_ip*/
private String getNextHost(String direction) {
SystemParamConfigEntity systemParamConfigEntity = getSystemParamConfig("upper_server_ip","中继上传服务地址" , "","");
//direction.equals("U") ? getSystemParamConfig("upper_server_ip","中继上传服务地址" , "",""):
//getSystemParamConfig("sync_idc_lower_host","下级(下发)中继服务地址" , "","") ;
return systemParamConfigEntity.getParamValue();
}
/*判断是否最后一级*/
private boolean isLastLevel(String direction) {
SystemParamConfigEntity systemParamConfigEntity =
direction.equals("I") ? getSystemParamConfig("sync_idc_top","是否顶级中继服务(连接自助平台)" , "0","01是(是,接收下级上传数据后解析入库)"):
getSystemParamConfig("sync_idc_final","是否末级中继服务(连接UDI管理系统)" , "0","01是(是,接收上级下发数据后解析入库)") ;
return systemParamConfigEntity.getParamValue().equals("0") ? false : true ;
}
/*检查当前系统为自助平台下发还是UDI系统(上传),返回传输方向,如果都未设置,默认返回下发*/
private String getDirection() {
SystemParamConfigEntity systemParamConfigEntity = getSystemParamConfig("sync_idc_top","是否顶级中继服务(连接自助平台)" , "0","01是(是,接收下级上传数据后解析入库)");
System.out.print(">>>>>>>>>>"+JSON.toJSONString(systemParamConfigEntity)+"\n");
if(systemParamConfigEntity.getParamValue().equals("0")) {
systemParamConfigEntity = getSystemParamConfig("sync_idc_final","是否末级中继服务(连接UDI管理系统)" , "0","01是(是,接收上级下发数据后解析入库)");
if(systemParamConfigEntity.getParamValue().equals("1"))
return "U";
}
return "I";
}
/*是否需要转发*/
private boolean isRelay(String direction) {
String relayStr = direction.equals("U") ? "sync_upstream_enable" : "sync_downstream_enable";
SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey(relayStr);
if (systemParamConfigEntity!=null&&systemParamConfigEntity.getParamValue()!=null&&
systemParamConfigEntity.getParamValue().equals("0")) {
return false;
}
return true;
}
/*由表名获取数据*/
private Map<String,Object> getData(String messageType,String tableName,Map<String,Object> params) {
String sql = "select "+tableName+".*,'A' as operateMode from "+tableName;
Map<String,Object> column = getColumn(tableName);
String where = DBAUtils.convertWhere(column,params,"");
sql+=!StringUtils.isEmpty(where) ? " where "+where : "";
params.put("sql", sql);
List<Map<String,Object>> list = dbDao.list(params);
Map<String,Object> map = new HashMap<String,Object>();
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(0);
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<String,Object> getColumn(String tableName) {
Map<String,Object> map = new HashMap<String,Object>();
List<Map<String,String>> columnList = new ArrayList<Map<String,String>>();
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<String,Object> column = new HashMap<String,Object>();
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<String,Object> map = dbDao.get("select * from idc_status where id='"+id+"'");
if(map!=null&&map.get("id")!=null) {
updateTime = DateUtil.formatDate(IDCUtils.parseDate(map.get("statusTime").toString()));
} else {
updateTime = "2000-01-01 00:00:00";
String sql = "insert into idc_status (id,statusTime) values ('"+id+"',cast('"+updateTime+"' as datetime))";
dbDao.save(sql);
}
return updateTime;
}
private String setUpdateTime(String id,String updateTime) {
String sql = "";
Map<String,Object> 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<date_sub(now(),interval 2 day) and isEnd='0'");
}
private void alterTable(String tableName,String sql) {
Map<String,String> map = dbDao.getMysql(tableName);
if(!(map!=null&&map.get("tableName")!=null)) {
jdbcTemplate.execute(sql);
}
}
}

@ -0,0 +1,54 @@
package com.glxp.sale.admin.idc.thread;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import com.glxp.sale.admin.dao.info.ScheduledDao;
import com.glxp.sale.admin.entity.info.ScheduledEntity;
import com.glxp.sale.admin.idc.service.IdcService;
import com.glxp.sale.admin.req.info.ScheduledRequest;
@Component
@EnableScheduling
public class AsyncIdcTask implements SchedulingConfigurer {
final Logger logger = LoggerFactory.getLogger(AsyncIdcTask.class);
@Resource
private ScheduledDao scheduledDao;
@Resource
private IdcService idcService;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(() -> process(),
triggerContext -> {
ScheduledRequest scheduledRequest = new ScheduledRequest();
scheduledRequest.setCronName("syncIdc");
logger.info("syncIdc----------------");
ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
String cron = scheduledEntity!=null ? scheduledEntity.getCron() : "5 * * * * ?";
if (cron.isEmpty()) {
logger.error("cron is null");
}
logger.info("syncIdc----------------");
return new CronTrigger(cron).nextExecutionTime(triggerContext);
});
}
private void process() {
logger.info("syncIdc----process------------");
idcService.asyncIdcTask();
}
}

@ -0,0 +1,399 @@
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 String keywords = ",limit,offset,sort,order,isLike,menuId,menuCode,page,isAuto,pageNo,pageSize,pageNum,sqlOrder,join,";
/*解析where条件*/
public static String parseWhere(String sql,Map<String,Object> columns,Map<String,Object> params,String dataWhere) {
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;
}
/*生成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("'", "AAA12345ZZZ");
value = value.replaceAll("AAA12345ZZZ", "''");
}
return value;
}
}

@ -0,0 +1,112 @@
package com.glxp.sale.admin.idc.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author chenqf
*/
public class IDCUtils {
private static final Logger logger = LoggerFactory.getLogger(IDCUtils.class);
private static String aliasNameChars = "_hijklmnopqrstuvwxyzabcdefg9876543210";
private static String realNamechars = "abcdefghijklmnopqrstuvwxyz_0123456789";
private final static String MONTH_EN = "JanFebMarAprMayJunJulAugSepOctNovDec";
//判断是否json字符串
public static boolean isJson(String json) {
if(json!=null&&json.contains("{")&&json.contains("}")&&json.contains("\""))
return true;
return false;
}
public static Date parseDate(String str) {
return parseDate(str,"yyyy-MM-dd HH:mm:ss");
}
public static Date parseDate(String str,String fmt) {
String dateStr=str.replace("?", " ");
SimpleDateFormat df = new SimpleDateFormat(fmt);
if(str!=null&&!str.contains("-")&&str.length()>7&&StringUtils.isNumeric(str.substring(0, 4))&&
Long.valueOf(str.substring(0,4))>1899&&Long.valueOf(str.substring(0,4))<2199&&
StringUtils.isNumeric(str.substring(4, 6))&&StringUtils.isNumeric(str.substring(6, 8))&&
(!str.contains(" ")||(str.contains(" ")&&str.indexOf(" ")>7))) {
dateStr = str.substring(0,4)+"-"+str.substring(4,6)+"-"+str.substring(6,8);
dateStr+=" ";
if(str.contains(" ")) {
if(str.length()>9) {
dateStr+= str.substring(9,11);
if(str.length()>11) {
dateStr+=":"+str.substring(11,13);
if(str.length()>13) {
dateStr+=":"+str.substring(13,15);
}
} else {
dateStr+=":00:00";
}
} else {
dateStr+="00:00:00";
}
} else {
if(str.length()>8) {
dateStr+= str.substring(8,10);
if(str.length()>10) {
dateStr+=":"+str.substring(10,12);
if(str.length()>13) {
dateStr+=":"+str.substring(12,14);
} if(str.length()>12) {
dateStr+=":"+str.substring(12,13)+"0";
}
} else {
dateStr+=":00:00";
}
} else {
dateStr+="00:00:00";
}
}
} else if(str!=null&&str.contains("CST")) {
//Wed Feb 16 15:35:31 CST 2022
int index = str.indexOf("CST");
int month = (MONTH_EN.indexOf(str.substring(index -16,index -13))+3)/3;
dateStr = str.substring(index+4)+"-";
if(month<10)
dateStr+="0";
dateStr+= month+"-"+str.substring(index -12,index -10)+" "+str.substring(index -9,index -1);
} else if(str!=null&&str.contains("GMT")) {
int index = str.indexOf("GMT");
int month = (MONTH_EN.indexOf(str.substring(index -16,index -13))+3)/3;
if(str.contains("GMT+")) {
dateStr = str.substring(index+10)+"-";
} else {
dateStr = str.substring(index+4)+"-";
}
if(month<10)
dateStr+="0";
dateStr+= month+"-"+str.substring(index -12,index -10)+" "+str.substring(index -9,index -1);
} else if (str!=null&&str.equals("-30609820800000")) {
dateStr="1000-01-01 00:01:01";
} else if (str!=null&&str.length()==4&&StringUtils.isNumeric(str)&&Integer.valueOf(str)>999) {
dateStr=str+"-01-01 00:01:01";
} else if (str!=null&&str.length()==6&&StringUtils.isNumeric(str)) {
dateStr=str.substring(0, 4)+"-"+str.substring(4)+"-01 00:01:01";
} else if(str!=null&&StringUtils.isNumeric(str)&&str.length()>8&&(Long.valueOf(str.substring(0,4))<1899||Long.valueOf(str.substring(0,4))>2200)) {
dateStr=df.format(new Date(Long.valueOf(str)));
}
dateStr = dateStr.replace("T", " ");
Date date = null;
try {
date = df.parse(dateStr);
} catch (ParseException e) {
logger.error(e.getMessage());
}
return date;
}
}
Loading…
Cancel
Save