diff --git a/docus-webservice/dataConfig/collectList.json b/docus-webservice/dataConfig/collectList.json new file mode 100644 index 0000000..0f1bd31 --- /dev/null +++ b/docus-webservice/dataConfig/collectList.json @@ -0,0 +1,25 @@ +{ + "task_count": 4, + "collectList":[ + { + "collectsys_code": "13", + "requestUrl": "http://localhost:9302/collect", + "remark": "电生理" + }, + { + "collectsys_code": "3", + "requestUrl": "http://localhost:9302/collect", + "remark": "PACS" + }, + { + "collectsys_code": "14", + "requestUrl": "http://localhost:9303/collect", + "remark": "内镜" + }, + { + "collectsys_code": "4", + "requestUrl": "http://localhost:9304/collect", + "remark": "心电图" + } + ] +} \ No newline at end of file diff --git a/docus-webservice/pom.xml b/docus-webservice/pom.xml index b0b3955..3f7b868 100644 --- a/docus-webservice/pom.xml +++ b/docus-webservice/pom.xml @@ -73,6 +73,21 @@ spring-boot-starter-test test + + org.apache.httpcomponents + httpclient + 4.5.2 + + + org.apache.httpcomponents + httpmime + 4.4 + + + org.apache.httpcomponents + httpcore + 4.4.5 + diff --git a/docus-webservice/src/main/java/com/docus/webservice/dto/CollectList.java b/docus-webservice/src/main/java/com/docus/webservice/dto/CollectList.java new file mode 100644 index 0000000..42d14aa --- /dev/null +++ b/docus-webservice/src/main/java/com/docus/webservice/dto/CollectList.java @@ -0,0 +1,12 @@ +package com.docus.webservice.dto; + +import lombok.Data; + +@Data +public class CollectList { + private Long id; + //采集器id + private String collectsys_code; + private String requestUrl; + private String remark; +} diff --git a/docus-webservice/src/main/java/com/docus/webservice/entity/AfInterfaceCollect.java b/docus-webservice/src/main/java/com/docus/webservice/entity/AfInterfaceCollect.java new file mode 100644 index 0000000..95d9272 --- /dev/null +++ b/docus-webservice/src/main/java/com/docus/webservice/entity/AfInterfaceCollect.java @@ -0,0 +1,24 @@ +package com.docus.webservice.entity; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.Date; + +@Data +@ApiModel("文件库中的省中医病案采集表") +public class AfInterfaceCollect { + + private Long id; + @ApiModelProperty(value = "任务流水号") + private String serialnum; + @ApiModelProperty(value = "记账号") + private String jzh; + @ApiModelProperty(value = "任务数") + private Integer taskCount; + @ApiModelProperty(value = "完成数") + private Integer completeCount; + @ApiModelProperty(value = "创建时间") + private Date createTime; +} diff --git a/docus-webservice/src/main/java/com/docus/webservice/entity/AfInterfaceCollectSub.java b/docus-webservice/src/main/java/com/docus/webservice/entity/AfInterfaceCollectSub.java new file mode 100644 index 0000000..1ccc2c9 --- /dev/null +++ b/docus-webservice/src/main/java/com/docus/webservice/entity/AfInterfaceCollectSub.java @@ -0,0 +1,25 @@ +package com.docus.webservice.entity; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +@ApiModel("文件库中的省中医病案采集-子任务表") +public class AfInterfaceCollectSub { + private Long id; + @ApiModelProperty(value = "af_interface_collect表id") + private Long afInterfaceCollectId; + @ApiModelProperty(value = "采集器id") + private String collectsysCode; + @ApiModelProperty(value = "子任务流水号 同一份文件子任务需一致") + private String serialnumSub; + @ApiModelProperty(value = "记账号") + private String jzh; + @ApiModelProperty(value = "分类名称") + private String assortName; + @ApiModelProperty(value = "状态 0:未发起,1:成功,2:失败") + private Integer state; + @ApiModelProperty(value = "请求信息") + private String requestMessage; +} diff --git a/docus-webservice/src/main/java/com/docus/webservice/entity/TBasic.java b/docus-webservice/src/main/java/com/docus/webservice/entity/TBasic.java index 4b2c139..c684805 100644 --- a/docus-webservice/src/main/java/com/docus/webservice/entity/TBasic.java +++ b/docus-webservice/src/main/java/com/docus/webservice/entity/TBasic.java @@ -163,5 +163,19 @@ public class TBasic implements Serializable { @ApiModelProperty(value = "所在院区") private String wardPalce; - + /** + * 特殊处理 y:n + * @param isDead + */ + public void setIsDead(String isDead) { + this.isDead = isDead!=null&&isDead.toLowerCase().equals("y")?1:0; + } + + public Integer getIsDead() { + return isDead; + } + + public void setIsDead(Integer isDead) { + this.isDead = isDead; + } } diff --git a/docus-webservice/src/main/java/com/docus/webservice/enums/Codes.java b/docus-webservice/src/main/java/com/docus/webservice/enums/Codes.java index 5122bef..4c887ee 100644 --- a/docus-webservice/src/main/java/com/docus/webservice/enums/Codes.java +++ b/docus-webservice/src/main/java/com/docus/webservice/enums/Codes.java @@ -27,8 +27,11 @@ public enum Codes { //接收三级节点 // PAT_INFO("202","PatInfo"), //静态文件根元素名 - SELECT_COLUMNS("10000","selectColumns") - ; + SELECT_COLUMNS("10000","selectColumns"), + //collectList静态文件根元素名 + JSON_COLLECTLIST("800","\\dataConfig\\collectList.json"), + JSON_TASK_COUNT("801","task_count"), + JSON_COLLECTLIST_ROOT("802","collectList"); //代码 private String code; //描述 diff --git a/docus-webservice/src/main/java/com/docus/webservice/handler/ITBasicWebService.java b/docus-webservice/src/main/java/com/docus/webservice/handler/ITBasicWebService.java index e6f4176..52f075f 100644 --- a/docus-webservice/src/main/java/com/docus/webservice/handler/ITBasicWebService.java +++ b/docus-webservice/src/main/java/com/docus/webservice/handler/ITBasicWebService.java @@ -12,8 +12,8 @@ public interface ITBasicWebService { * @return */ @WebMethod - String savaAndSub(String tbasic); + String OUT_HOS_REG(String tbasic); @WebMethod - String updateAndSub(String tbasic); + String OUT_HOS_CAN(String tbasic); } diff --git a/docus-webservice/src/main/java/com/docus/webservice/handler/TBasicWebService.java b/docus-webservice/src/main/java/com/docus/webservice/handler/TBasicWebService.java index 0614d8e..bc493d3 100644 --- a/docus-webservice/src/main/java/com/docus/webservice/handler/TBasicWebService.java +++ b/docus-webservice/src/main/java/com/docus/webservice/handler/TBasicWebService.java @@ -2,15 +2,18 @@ package com.docus.webservice.handler; import com.alibaba.fastjson.JSON; +import com.docus.webservice.dto.CollectList; import com.docus.webservice.dto.SelectColumn; +import com.docus.webservice.entity.AfInterfaceCollect; +import com.docus.webservice.entity.AfInterfaceCollectSub; import com.docus.webservice.entity.MrReportError; + import com.docus.webservice.entity.TBasic; import com.docus.webservice.enums.Codes; +import com.docus.webservice.mapper.AfInterfaceCollectMapper; import com.docus.webservice.service.IMrReportErrorService; import com.docus.webservice.service.ITBasicService; -import com.docus.webservice.utils.JsonUtils; -import com.docus.webservice.utils.ResultUtils; -import com.docus.webservice.utils.XmlUtils; +import com.docus.webservice.utils.*; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -18,10 +21,9 @@ import javax.jws.WebService; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.util.*; @WebService public class TBasicWebService implements ITBasicWebService { @@ -32,6 +34,9 @@ public class TBasicWebService implements ITBasicWebService { @Autowired private IMrReportErrorService mrReportErrorService; + @Autowired + private AfInterfaceCollectMapper afInterfaceCollectMapper; + /** * 获取jar包所在位置 * @@ -60,41 +65,12 @@ public class TBasicWebService implements ITBasicWebService { } @Override - public String savaAndSub(String tbasic) { - XmlUtils xmlUtils; + public String OUT_HOS_REG(String tbasic) { try { //解析xml - xmlUtils = new XmlUtils(new ByteArrayInputStream(tbasic.getBytes("UTF-8"))); - //解析json映射文件 - String json = JsonUtils.readJsonFile(CurrentPath() + Codes.JSON_ADDRESS.getMessage()); - Map jsonMap = JSON.parseObject(json, Map.class); HashMap tBasicMap = new HashMap<>(); HashMap tBasicSubMap = new HashMap<>(); - List selectColumns = JSON.parseArray(String.valueOf(jsonMap.get(Codes.SELECT_COLUMNS.getMessage())), SelectColumn.class); - String value; - for (SelectColumn selectColumn : selectColumns) { - try { - value = xmlUtils.getElementText(selectColumn.getQualityColumn()) == null ? null : String.valueOf(xmlUtils.getElementText(selectColumn.getQualityColumn())); - } catch (RuntimeException e) { - String message = e.getMessage(); - return ResultUtils.fail(message); - } - if (StringUtils.isBlank(value)) { - if (selectColumn.getByNull() == 0) { - insertLog(tbasic); - return ResultUtils.fail(selectColumn.getQualityColumn() + " 字段不能为空值"); - } else { - continue; - } - } else { - if (selectColumn.getTableName().equals("t_basic")) { - tBasicMap.put(selectColumn.getColumnName(), value); - } - if (selectColumn.getTableName().equals("t_basic_sub")) { - tBasicSubMap.put(selectColumn.getColumnName(), value); - } - } - } + parseXml(tbasic,tBasicMap,tBasicSubMap); //处理业务 TBasic tBasic; if (tBasicMap != null && tBasicMap.size() > 0) { @@ -102,6 +78,7 @@ public class TBasicWebService implements ITBasicWebService { tBasic.setCreateTime(new Date()); try { itBasicService.savaAndSub(tBasic, tBasicSubMap); + this.saveAndCall(tBasic); } catch (RuntimeException e) { insertLog(tbasic); return ResultUtils.fail(e.getMessage()); @@ -117,44 +94,57 @@ public class TBasicWebService implements ITBasicWebService { return ResultUtils.success().asXML(); } - @Override - public String updateAndSub(String tbasic) { - XmlUtils xmlUtils; + /** + * 解析xml + * @param tbasic + * @param tBasicMap + * @param tBasicSubMap + */ + private void parseXml(String tbasic, HashMap tBasicMap, HashMap tBasicSubMap) { + XmlUtils xmlUtils = null; + try { - //解析xml xmlUtils = new XmlUtils(new ByteArrayInputStream(tbasic.getBytes("UTF-8"))); - //解析json映射文件 - String path = CurrentPath() + Codes.JSON_ADDRESS.getMessage(); - String json = JsonUtils.readJsonFile(path); - Map jsonMap = JSON.parseObject(json, Map.class); - HashMap tBasicMap = new HashMap<>(); - HashMap tBasicSubMap = new HashMap<>(); - List selectColumns = JSON.parseArray(String.valueOf(jsonMap.get(Codes.SELECT_COLUMNS.getMessage())), SelectColumn.class); - String value; - for (SelectColumn selectColumn : selectColumns) { - try { - value = xmlUtils.getElementText(selectColumn.getQualityColumn()) == null ? null : String.valueOf(xmlUtils.getElementText(selectColumn.getQualityColumn())); - } catch (RuntimeException e) { - String message = e.getMessage(); - return ResultUtils.fail(message); - } - if (StringUtils.isBlank(value)) { - if (selectColumn.getByNull() == 0) { - insertLog(tbasic); - return ResultUtils.fail(selectColumn.getQualityColumn() + " 字段不能为空值"); - } else { - continue; - - } + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + //解析json映射文件 + String json = JsonUtils.readJsonFile(CurrentPath() + Codes.JSON_ADDRESS.getMessage()); + Map jsonMap = JSON.parseObject(json, Map.class); + + List selectColumns = JSON.parseArray(String.valueOf(jsonMap.get(Codes.SELECT_COLUMNS.getMessage())), SelectColumn.class); + String value; + for (SelectColumn selectColumn : selectColumns) { + try { + value = xmlUtils.getElementText(selectColumn.getQualityColumn()) == null ? null : String.valueOf(xmlUtils.getElementText(selectColumn.getQualityColumn())); + } catch (RuntimeException e) { + String message = e.getMessage(); + throw new RuntimeException(message); + } + if (StringUtils.isBlank(value)) { + if (selectColumn.getByNull() == 0) { + insertLog(tbasic); + throw new RuntimeException(selectColumn.getQualityColumn() + " 字段不能为空值"); } else { - if (selectColumn.getTableName().equals("t_basic")) { - tBasicMap.put(selectColumn.getColumnName(), value); - } - if (selectColumn.getTableName().equals("t_basic_sub")) { - tBasicSubMap.put(selectColumn.getColumnName(), value); - } + continue; + } + } else { + if (selectColumn.getTableName().equals("t_basic")) { + tBasicMap.put(selectColumn.getColumnName(), value); + } + if (selectColumn.getTableName().equals("t_basic_sub")) { + tBasicSubMap.put(selectColumn.getColumnName(), value); } } + } + } + + @Override + public String OUT_HOS_CAN(String tbasic) { + try { + HashMap tBasicMap = new HashMap<>(); + HashMap tBasicSubMap = new HashMap<>(); + parseXml(tbasic,tBasicMap,tBasicSubMap); //处理业务 TBasic tBasic; if (tBasicMap != null && tBasicMap.size() > 0) { @@ -166,8 +156,10 @@ public class TBasicWebService implements ITBasicWebService { tBasic.setUpdateTime(new Date()); try { itBasicService.updateAndSub(tBasic, tBasicSubMap); + this.saveAndCall(tBasic); } catch (RuntimeException e) { insertLog(tbasic); + e.printStackTrace(); return ResultUtils.fail(e.getMessage()); } } else { @@ -180,4 +172,54 @@ public class TBasicWebService implements ITBasicWebService { } return ResultUtils.success().asXML(); } + + public void saveAndCall(TBasic tBasic) throws URISyntaxException { + //解析json映射文件 + String json = JsonUtils.readJsonFile(CurrentPath() + Codes.JSON_COLLECTLIST.getMessage()); + if (StringUtils.isNotBlank(json)) { + Map jsonMap = JSON.parseObject(json, Map.class); + //任务数 + Integer task_count= JSON.parseObject(String.valueOf(jsonMap.get(Codes.JSON_TASK_COUNT.getMessage())), Integer.class); + + List collectLists = JSON.parseArray(String.valueOf(jsonMap.get(Codes.JSON_COLLECTLIST_ROOT.getMessage())), CollectList.class); + + //添加省中医病案采集 + SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0); + AfInterfaceCollect afc=new AfInterfaceCollect(); + Long afcId=idWorker.nextId(); + afc.setId(afcId); + afc.setJzh(tBasic.getJzh()); + afc.setTaskCount(task_count); + afc.setCreateTime(new Date()); + afInterfaceCollectMapper.addAfInterfaceCollect(afc); + + //添加省中医病案采集-子任务 + List list=new ArrayList<>(); + for (CollectList collectList : collectLists) { + AfInterfaceCollectSub afInterfaceCollectSub = new AfInterfaceCollectSub(); + Long afcsId= idWorker.nextId(); + afInterfaceCollectSub.setId(afcsId); + afInterfaceCollectSub.setAfInterfaceCollectId(afcId); + afInterfaceCollectSub.setCollectsysCode(collectList.getCollectsys_code()); + afInterfaceCollectSub.setJzh(tBasic.getJzh()); + collectList.setId(afcsId); + list.add(afInterfaceCollectSub); + } + afInterfaceCollectMapper.addAfInterfaceCollectSub(list); + + for (CollectList collectList : collectLists) { + //调用http发送请求 + this.sendHttp(collectList,tBasic.getJzh()); + } + } + } + + private String sendHttp(CollectList collectList,String jzh) throws URISyntaxException { + Map params= new HashMap<>(); + params.put("empId",jzh); + params.put("collectSubId",String.valueOf(collectList.getId())); + return HttpUtils.get(collectList.getRequestUrl(),params); + } + + } diff --git a/docus-webservice/src/main/java/com/docus/webservice/mapper/AfInterfaceCollectMapper.java b/docus-webservice/src/main/java/com/docus/webservice/mapper/AfInterfaceCollectMapper.java new file mode 100644 index 0000000..20dc1b8 --- /dev/null +++ b/docus-webservice/src/main/java/com/docus/webservice/mapper/AfInterfaceCollectMapper.java @@ -0,0 +1,15 @@ +package com.docus.webservice.mapper; + +import com.docus.webservice.entity.AfInterfaceCollect; +import com.docus.webservice.entity.AfInterfaceCollectSub; +import org.apache.ibatis.annotations.Param; + +import java.util.Date; +import java.util.List; + +public interface AfInterfaceCollectMapper { + + int addAfInterfaceCollect(AfInterfaceCollect afInterfaceCollect); + + int addAfInterfaceCollectSub(List list); +} diff --git a/docus-webservice/src/main/java/com/docus/webservice/utils/HttpUtils.java b/docus-webservice/src/main/java/com/docus/webservice/utils/HttpUtils.java new file mode 100644 index 0000000..333cd51 --- /dev/null +++ b/docus-webservice/src/main/java/com/docus/webservice/utils/HttpUtils.java @@ -0,0 +1,211 @@ +package com.docus.webservice.utils; + +import com.alibaba.fastjson.JSON; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.*; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Map; + +public class HttpUtils { + private static PoolingHttpClientConnectionManager cm; + private static String EMPTY_STR = ""; + private static String UTF_8 = "UTF-8"; + + private static void init() { + if (cm == null) { + cm = new PoolingHttpClientConnectionManager(); + cm.setMaxTotal(50);// 整个连接池最大连接数 + cm.setDefaultMaxPerRoute(5);// 每路由最大连接数,默认值是2 + } + } + + /** + * 通过连接池获取HttpClient + * + * @return + */ + private static CloseableHttpClient getHttpClient() { + init(); + return HttpClients.custom().setConnectionManager(cm).build(); + } + /** + * @param url + * @return + */ + public static String get(String url) { + HttpGet httpGet = new HttpGet(url); + return getResult(httpGet); + } + /** + * @param url、params + * @return + */ + public static String get(String url, Map params) throws URISyntaxException { + URIBuilder ub = new URIBuilder(); + ub.setPath(url); + + ArrayList pairs = covertParams2NVPS(params); + ub.setParameters(pairs); + + HttpGet httpGet = new HttpGet(ub.build()); + return getResult(httpGet); + } + /** + * @param url、headers、params + * @return + */ + public static String get(String url, Map headers, Map params) + throws URISyntaxException { + URIBuilder ub = new URIBuilder(); + ub.setPath(url); + + if (params != null) { + ArrayList pairs = covertParams2NVPS(params); + ub.setParameters(pairs); + } + + HttpGet httpGet = new HttpGet(ub.build()); + for (Map.Entry param : headers.entrySet()) { + httpGet.addHeader(param.getKey(), String.valueOf(param.getValue())); + } + return getResult(httpGet); + } + /** + * @param url + * @return + */ + public static String post(String url) { + HttpPost httpPost = new HttpPost(url); + return getResult(httpPost); + } + /** + * @param url、params + * @return + */ + public static String post(String url, Map params) throws UnsupportedEncodingException { + HttpPost httpPost = new HttpPost(url); + httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), "utf-8"));//设置表单提交编码 + +// httpPost.setEntity(new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_FORM_URLENCODED)); + return getResult(httpPost); + } + + public static String post(String url, Object params, Map head) throws UnsupportedEncodingException { + HttpPost httpPost = new HttpPost(url); + System.out.println(params); + httpPost.setEntity(new StringEntity(params.toString()));//设置表单提交编码 + if (params != null) { + for (Map.Entry param : head.entrySet()) { + httpPost.addHeader(param.getKey(), String.valueOf(param.getValue())); + } + } +// httpPost.setEntity(new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_FORM_URLENCODED)); + return getResult(httpPost); + } + + private static ArrayList covertParams2NVPS(Map params) { + ArrayList pairs = new ArrayList(); + for (Map.Entry param : params.entrySet()) { + if (param.getValue() != null) { + pairs.add(new BasicNameValuePair(param.getKey(), param.getValue())); + } + } + + return pairs; + } + /** + * @param url、headers、params + * @return + */ + public static String post(String url, Map headers, Map params) + throws UnsupportedEncodingException { + HttpPost httpPost = new HttpPost(url); + System.out.println(params); + if (params != null) { + for (Map.Entry param : headers.entrySet()) { + httpPost.addHeader(param.getKey(), String.valueOf(param.getValue())); + } + } + + httpPost.setEntity(new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_JSON)); + + return getResult(httpPost); + } + + /** + * 处理Http请求 + * + * @param request + * @return + */ + private static String getResult(HttpRequestBase request) { + // CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpClient httpClient = getHttpClient(); + try { + CloseableHttpResponse response = httpClient.execute(request); + // response.getStatusLine().getStatusCode(); + HttpEntity entity = response.getEntity(); + if (entity != null) { + // long len = entity.getContentLength();// -1 表示长度未知 + String result = EntityUtils.toString(entity, UTF_8); + response.close(); + // httpClient.close(); + return result; + } + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + + } + + return EMPTY_STR; + } + + /** + * 处理Http请求 + * + * @param requestBuilder + * @return + */ + private static String getResult(RequestBuilder requestBuilder) { + // CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpClient httpClient = getHttpClient(); + try { + CloseableHttpResponse response = httpClient.execute(requestBuilder.build()); + // response.getStatusLine().getStatusCode(); + HttpEntity entity = response.getEntity(); + if (entity != null) { + // long len = entity.getContentLength();// -1 表示长度未知 + String result = EntityUtils.toString(entity, UTF_8); + response.close(); + // httpClient.close(); + return result; + } + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + + } + + return EMPTY_STR; + } +} diff --git a/docus-webservice/src/main/java/com/docus/webservice/utils/SnowflakeIdWorker.java b/docus-webservice/src/main/java/com/docus/webservice/utils/SnowflakeIdWorker.java new file mode 100644 index 0000000..38aaabc --- /dev/null +++ b/docus-webservice/src/main/java/com/docus/webservice/utils/SnowflakeIdWorker.java @@ -0,0 +1,139 @@ +package com.docus.webservice.utils; + +public class SnowflakeIdWorker { + /** + * 开始时间截 (2015-01-01) + */ + private final long twepoch = 1420041600000L; + /** + * 机器id所占的位数 + */ + private final long workerIdBits = 5L; + /** + * 数据标识id所占的位数 + */ + private final long datacenterIdBits = 5L; + /** + * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) + */ + private final long maxWorkerId = -1L ^ (-1L << workerIdBits); + /** + * 支持的最大数据标识id,结果是31 + */ + private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); + /** + * 序列在id中占的位数 + */ + private final long sequenceBits = 12L; + /** + * 机器ID向左移12位 + */ + private final long workerIdShift = sequenceBits; + /** + * 数据标识id向左移17位(12+5) + */ + private final long datacenterIdShift = sequenceBits + workerIdBits; + /** + * 时间截向左移22位(5+5+12) + */ + private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; + /** + * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) + */ + private final long sequenceMask = -1L ^ (-1L << sequenceBits); + /** + * 工作机器ID(0~31) + */ + private long workerId; + /** + * 数据中心ID(0~31) + */ + private long datacenterId; + /** + * 毫秒内序列(0~4095) + */ + private long sequence = 0L; + /** + * 上次生成ID的时间截 + */ + private long lastTimestamp = -1L; + /** + * 构造函数 + * @param workerId 工作ID (0~31) + * @param datacenterId 数据中心ID (0~31) + */ + public SnowflakeIdWorker(long workerId, long datacenterId) { + if (workerId > maxWorkerId || workerId < 0) { + throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); + } + if (datacenterId > maxDatacenterId || datacenterId < 0) { + throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); + } + this.workerId = workerId; + this.datacenterId = datacenterId; + } + /** + * 获得下一个ID (该方法是线程安全的) + * @return SnowflakeId + */ + public synchronized long nextId() { + long timestamp = timeGen(); + // 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 + if (timestamp < lastTimestamp) { + throw new RuntimeException( + String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); + } + // 如果是同一时间生成的,则进行毫秒内序列 + if (lastTimestamp == timestamp) { + sequence = (sequence + 1) & sequenceMask; + // 毫秒内序列溢出 + if (sequence == 0) { + //阻塞到下一个毫秒,获得新的时间戳 + timestamp = tilNextMillis(lastTimestamp); + } + } + // 时间戳改变,毫秒内序列重置 + else { + sequence = 0L; + } + // 上次生成ID的时间截 + lastTimestamp = timestamp; + // 移位并通过或运算拼到一起组成64位的ID + return ((timestamp - twepoch) << timestampLeftShift) // + | (datacenterId << datacenterIdShift) // + | (workerId << workerIdShift) // + | sequence; + } + /** + * 阻塞到下一个毫秒,直到获得新的时间戳 + * @param lastTimestamp 上次生成ID的时间截 + * @return 当前时间戳 + */ + protected long tilNextMillis(long lastTimestamp) { + long timestamp = timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = timeGen(); + } + return timestamp; + } + /** + * 返回以毫秒为单位的当前时间 + * @return 当前时间(毫秒) + */ + protected long timeGen() { + return System.currentTimeMillis(); + } + + public static void main(String[] args) throws InterruptedException { + SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0); + for (int i = 0; i < 10; i++) { + long id = idWorker.nextId(); + Thread.sleep(1); + System.out.println(id); + } + } + + +} + + diff --git a/docus-webservice/src/main/resources/mapper/AfInterfaceCollectMapper.xml b/docus-webservice/src/main/resources/mapper/AfInterfaceCollectMapper.xml new file mode 100644 index 0000000..e754a78 --- /dev/null +++ b/docus-webservice/src/main/resources/mapper/AfInterfaceCollectMapper.xml @@ -0,0 +1,17 @@ + + + + + + insert into docus_archivefile.af_interface_collect (id,jzh,task_count,create_time) values (#{id},#{jzh},#{taskCount},#{createTime}) + + + insert into docus_archivefile.af_interface_collect_sub (id,af_interface_collect_id,collectsys_code,jzh) values + + (#{val.id},#{val.afInterfaceCollectId},#{val.collectsysCode},#{val.jzh}) + + + + \ No newline at end of file