PackageEndPoint.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. package com.yihu.ehr.pack.controller;
  2. import com.yihu.ehr.constants.ApiVersion;
  3. import com.yihu.ehr.constants.ErrorCode;
  4. import com.yihu.ehr.constants.ServiceApi;
  5. import com.yihu.ehr.controller.EnvelopRestEndPoint;
  6. import com.yihu.ehr.elasticsearch.ElasticSearchUtil;
  7. import com.yihu.ehr.exception.ApiException;
  8. import com.yihu.ehr.fastdfs.FastDFSUtil;
  9. import com.yihu.ehr.model.packs.EsDetailsPackage;
  10. import com.yihu.ehr.model.packs.EsSimplePackage;
  11. import com.yihu.ehr.model.security.MKey;
  12. import com.yihu.ehr.pack.common.MgrApi;
  13. import com.yihu.ehr.pack.feign.SecurityClient;
  14. import com.yihu.ehr.pack.service.RedisService;
  15. import com.yihu.ehr.pack.task.FastDFSTask;
  16. import com.yihu.ehr.profile.AnalyzeStatus;
  17. import com.yihu.ehr.profile.ArchiveStatus;
  18. import com.yihu.ehr.profile.queue.RedisCollection;
  19. import com.yihu.ehr.util.encrypt.RSA;
  20. import com.yihu.ehr.util.rest.Envelop;
  21. import io.swagger.annotations.Api;
  22. import io.swagger.annotations.ApiOperation;
  23. import io.swagger.annotations.ApiParam;
  24. import org.apache.commons.collections.CollectionUtils;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.beans.factory.annotation.Value;
  31. import org.springframework.data.domain.Page;
  32. import org.springframework.data.redis.core.RedisTemplate;
  33. import org.springframework.http.HttpStatus;
  34. import org.springframework.http.MediaType;
  35. import org.springframework.http.ResponseEntity;
  36. import org.springframework.web.bind.annotation.*;
  37. import org.springframework.web.multipart.MultipartFile;
  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpServletResponse;
  40. import java.io.ByteArrayInputStream;
  41. import java.io.Serializable;
  42. import java.text.DateFormat;
  43. import java.text.SimpleDateFormat;
  44. import java.util.*;
  45. /**
  46. * 档案包控制器。
  47. *
  48. * @author Sand
  49. * @version 1.0
  50. */
  51. @RestController
  52. @RequestMapping(ApiVersion.Version1_0)
  53. @Api(value = "PackageEndPoint", description = "档案包", tags = {"档案包服务-档案包"})
  54. public class PackageEndPoint extends EnvelopRestEndPoint {
  55. private static final Logger logger = LoggerFactory.getLogger(PackageEndPoint.class);
  56. private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  57. private static final String INDEX = "json_archives";
  58. private static final String TYPE = "info";
  59. private static final Integer PAGE_SIZE = 1000;
  60. @Value("${deploy.region}")
  61. private Short adminRegion;
  62. @Autowired
  63. private SecurityClient securityClient;
  64. @Autowired
  65. private FastDFSUtil fastDFSUtil;
  66. @Autowired
  67. private ElasticSearchUtil elasticSearchUtil;
  68. @Autowired
  69. private RedisTemplate<String, Serializable> redisTemplate;
  70. @Autowired
  71. private FastDFSTask fastDFSTask;
  72. @Autowired
  73. private RedisService redisService;
  74. /* @Autowired
  75. private JsonArchivesService jsonArchivesService;*/
  76. @RequestMapping(value = ServiceApi.Packages.Packages, method = RequestMethod.POST)
  77. @ApiOperation(value = "接收档案", notes = "从集成开放平台接收健康档案数据包")
  78. public boolean savePackageWithOrg (
  79. @ApiParam(name = "pack", value = "档案包", allowMultiple = true)
  80. @RequestPart() MultipartFile pack,
  81. @ApiParam(name = "org_code", value = "机构代码")
  82. @RequestParam(value = "org_code") String orgCode,
  83. @ApiParam(name = "package_crypto", value = "档案包解压密码,二次加密")
  84. @RequestParam(value = "package_crypto") String packageCrypto,
  85. @ApiParam(name = "md5", value = "档案包MD5")
  86. @RequestParam(value = "md5", required = false) String md5,
  87. @ApiParam(name = "packType", value = "包类型 默认为1(结构化) 1结构化档案,2文件档案,3链接档案,4数据集档案")
  88. @RequestParam(value = "packType", required = false) Integer packType,
  89. HttpServletRequest request) throws Exception {
  90. MKey key = securityClient.getOrgKey(orgCode);
  91. if (key == null || key.getPrivateKey() == null) {
  92. throw new ApiException(HttpStatus.FORBIDDEN, ErrorCode.FORBIDDEN, "Invalid private key, maybe you miss the organization code?");
  93. }
  94. String password;
  95. try {
  96. password = RSA.decrypt(packageCrypto, RSA.genPrivateKey(key.getPrivateKey()));
  97. } catch (Exception ex) {
  98. throw new ApiException(HttpStatus.FORBIDDEN, ErrorCode.FORBIDDEN, "javax.crypto.BadPaddingException." + ex.getMessage());
  99. }
  100. String clientId = getClientId(request);
  101. if (packType == null){
  102. packType = 1;
  103. }
  104. //更改成异步--->>防止大文件接收,导致阻塞,超时等问题
  105. fastDFSTask.savePackageWithOrg(pack.getInputStream(), password, orgCode, md5, clientId, packType);
  106. return true;
  107. }
  108. @RequestMapping(value = ServiceApi.Packages.Packages, method = RequestMethod.DELETE)
  109. @ApiOperation(value = "批量删除档案包", notes = "每次删除一万条记录")
  110. public boolean deletePackages(
  111. @ApiParam(name = "filters", value = "过滤器,为空检索所有条件")
  112. @RequestParam(value = "filters", required = false) String filters,
  113. @ApiParam(name = "count", value = "删除数量", required = true, defaultValue = "10000")
  114. @RequestParam(value = "count") Integer count) throws Exception {
  115. if (count > 10000) {
  116. count = 10000;
  117. }
  118. Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, filters, 1, count);
  119. List<String> idList = new ArrayList<>();
  120. for (Map<String, Object> temp : result) {
  121. String [] tokens = String.valueOf(temp.get("remote_path")).split(":");
  122. fastDFSUtil.delete(tokens[0], tokens[1]);
  123. idList.add(String.valueOf(temp.get("_id")));
  124. }
  125. if (idList.size() > 0) {
  126. String [] _id = new String[idList.size()];
  127. elasticSearchUtil.bulkDelete(INDEX, TYPE, idList.toArray(_id));
  128. }
  129. return true;
  130. }
  131. @RequestMapping(value = ServiceApi.Packages.PackagesByFilter, method = RequestMethod.DELETE)
  132. @ApiOperation(value = "批量删除档案包")
  133. public boolean deletePackages(
  134. @ApiParam(name = "filters", value = "过滤器,为空检索所有条件,慎用!最好先条件查询,确定是否是需要删除的数据")
  135. @RequestParam(value = "filters", required = false) String filters) throws Exception {
  136. Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, filters, 1, 10000);
  137. while (CollectionUtils.isNotEmpty(result.getContent())){
  138. List<String> idList = new ArrayList<>();
  139. for (Map<String, Object> temp : result.getContent()) {
  140. String [] tokens = String.valueOf(temp.get("remote_path")).split(":");
  141. fastDFSUtil.delete(tokens[0], tokens[1]);
  142. idList.add(String.valueOf(temp.get("_id")));
  143. }
  144. if (idList.size() > 0) {
  145. String [] _id = new String[idList.size()];
  146. elasticSearchUtil.bulkDelete(INDEX, TYPE, idList.toArray(_id));
  147. }
  148. result = elasticSearchUtil.page(INDEX, TYPE, filters, 1, 10000);
  149. }
  150. return true;
  151. }
  152. @RequestMapping(value = ServiceApi.Packages.Package, method = RequestMethod.DELETE)
  153. @ApiOperation(value = "删除单个档案包", notes = "删除一个数据包")
  154. public boolean deletePackage (
  155. @ApiParam(name = "id", value = "档案包编号")
  156. @PathVariable(value = "id") String id) throws Exception {
  157. Map<String, Object> source = elasticSearchUtil.findById(INDEX, TYPE, id);
  158. if (null == source) {
  159. return false;
  160. }
  161. String [] tokens = String.valueOf(source.get("remote_path")).split(":");
  162. fastDFSUtil.delete(tokens[0], tokens[1]);
  163. elasticSearchUtil.delete(INDEX, TYPE, id);
  164. return true;
  165. }
  166. @RequestMapping(value = ServiceApi.Packages.Analyze, method = RequestMethod.PUT)
  167. @ApiOperation(value = "更新档案包分析状态", notes = "更新档案包分析状态")
  168. public boolean analyzeStatus(
  169. @ApiParam(name = "id", value = "档案包编号", required = true)
  170. @PathVariable(value = "id") String id,
  171. @ApiParam(name = "status", value = "状态", required = true)
  172. @RequestParam(value = "status") AnalyzeStatus status,
  173. @ApiParam(name = "errorType", value = "错误类型(0 = 正常; -1 = 质控服务出错; -2 解析服务出错; 1 = 压缩包有误; 2 = Json文件有误; 3 = Json数据有误)", required = true)
  174. @RequestParam(value = "errorType") int errorType,
  175. @ApiParam(name = "message", value = "消息", required = true)
  176. @RequestBody String message) throws Exception {
  177. Map<String, Object> updateSource = new HashMap<>();
  178. if (status == AnalyzeStatus.Failed) {
  179. if (3 <= errorType && errorType <= 7) {
  180. updateSource.put("analyze_fail_count", 3);
  181. } else {
  182. Map<String, Object> sourceMap = elasticSearchUtil.findById(INDEX, TYPE, id);
  183. if (null == sourceMap) {
  184. return false;
  185. }
  186. if ((int)sourceMap.get("analyze_fail_count") < 3) {
  187. int failCount = (int)sourceMap.get("analyze_fail_count");
  188. updateSource.put("analyze_fail_count", failCount + 1);
  189. }
  190. }
  191. } else if (status == AnalyzeStatus.Acquired) {
  192. updateSource.put("analyze_date", dateFormat.format(new Date()));
  193. }
  194. updateSource.put("message", message);
  195. updateSource.put("error_type", errorType);
  196. updateSource.put("analyze_status", status.ordinal());
  197. elasticSearchUtil.voidUpdate(INDEX, TYPE, id, updateSource);
  198. return true;
  199. }
  200. @RequestMapping(value = ServiceApi.Packages.Resolve, method = {RequestMethod.PUT})
  201. @ApiOperation(value = "更新档案包解析状态", notes = "更新档案包解析状态")
  202. public boolean resolveStatus(
  203. @ApiParam(name = "id", value = "档案包编号", required = true)
  204. @PathVariable(value = "id") String id,
  205. @ApiParam(name = "status", value = "状态", required = true)
  206. @RequestParam(value = "status") ArchiveStatus status,
  207. @ApiParam(name = "errorType", value = "错误类型(0 = 正常; -1 = 质控服务出错; -2 解析服务出错; 1 = 压缩包有误; 2 = Json文件有误; 3 = Json数据有误)", required = true)
  208. @RequestParam(value = "errorType") int errorType,
  209. @ApiParam(name = "message", value = "消息", required = true)
  210. @RequestBody String message) throws Exception {
  211. Map<String, Object> updateSource = new HashMap<>();
  212. if (status == ArchiveStatus.Finished) {
  213. //入库成功
  214. Map<String, String> map = objectMapper.readValue(message, Map.class);
  215. updateSource.put("profile_id", map.get("profile_id"));
  216. updateSource.put("demographic_id", map.get("demographic_id"));
  217. updateSource.put("event_type", map.get("event_type"));
  218. updateSource.put("event_no", map.get("event_no"));
  219. updateSource.put("event_date", map.get("event_date"));
  220. updateSource.put("patient_id", map.get("patient_id"));
  221. updateSource.put("dept", map.get("dept"));
  222. updateSource.put("delay", map.get("delay"));
  223. updateSource.put("re_upload_flg", map.get("re_upload_flg"));
  224. updateSource.put("finish_date", dateFormat.format(new Date()));
  225. updateSource.put("message", "resolve success");
  226. updateSource.put("resourced", 1);
  227. updateSource.put("defect", map.get("defect"));
  228. updateSource.put("patient_name", map.get("patient_name"));
  229. } else if (status == ArchiveStatus.Acquired) {
  230. //开始入库
  231. updateSource.put("parse_date", dateFormat.format(new Date()));
  232. updateSource.put("message", message);
  233. } else {
  234. //入库失败
  235. updateSource.put("finish_date", null);
  236. if (3 <= errorType && errorType <= 7) {
  237. updateSource.put("fail_count", 3);
  238. } else {
  239. Map<String, Object> sourceMap = elasticSearchUtil.findById(INDEX, TYPE, id);
  240. if (null == sourceMap) {
  241. return false;
  242. }
  243. if ((int)sourceMap.get("fail_count") < 3) {
  244. int failCount = (int)sourceMap.get("fail_count");
  245. updateSource.put("fail_count", failCount + 1);
  246. }
  247. }
  248. updateSource.put("message", message);
  249. updateSource.put("resourced", 0);
  250. }
  251. updateSource.put("error_type", errorType);
  252. updateSource.put("archive_status", status.ordinal());
  253. elasticSearchUtil.voidUpdate(INDEX, TYPE, id, updateSource);
  254. return true;
  255. }
  256. @RequestMapping(value = ServiceApi.Packages.Analyzes, method = RequestMethod.PUT)
  257. @ApiOperation(value = "根据条件批量修改档案包质控状态", notes = "根据条件批量修改档案包质控状态")
  258. public Integer updateAnalyzer (
  259. @ApiParam(name = "filters", value = "条件", required = true)
  260. @RequestParam(value = "filters") String filters,
  261. @ApiParam(name = "status", value = "状态", required = true)
  262. @RequestParam(value = "status") AnalyzeStatus status,
  263. @ApiParam(name = "page", value = "消息", required = true)
  264. @RequestParam(value = "page") Integer page,
  265. @ApiParam(name = "size", value = "状态", required = true)
  266. @RequestParam(value = "size") Integer size) throws Exception {
  267. Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, filters, page, size);
  268. List<Map<String, Object>> updateSourceList = new ArrayList<>();
  269. final int _status = status.ordinal();
  270. result.forEach(item -> {
  271. Map<String, Object> updateSource = new HashMap<>();
  272. updateSource.put("_id", item.get("_id"));
  273. updateSource.put("analyze_status", status.ordinal());
  274. if (_status == 2) {
  275. updateSource.put("analyze_fail_count", 3);
  276. } else if(_status == 0){
  277. updateSource.put("analyze_fail_count", 0);
  278. updateSource.put("analyze_status",0);
  279. updateSource.put("archive_status",0);
  280. } else {
  281. updateSource.put("analyze_fail_count", 0);
  282. }
  283. updateSourceList.add(updateSource);
  284. });
  285. elasticSearchUtil.bulkUpdate(INDEX, TYPE, updateSourceList);
  286. return result.getNumberOfElements();
  287. }
  288. @RequestMapping(value = ServiceApi.Packages.Resolves, method = RequestMethod.PUT)
  289. @ApiOperation(value = "根据条件批量修改档案包解析状态", notes = "根据条件批量修改档案包解析状态")
  290. public Integer update(
  291. @ApiParam(name = "filters", value = "条件", required = true)
  292. @RequestParam(value = "filters") String filters,
  293. @ApiParam(name = "status", value = "状态", required = true)
  294. @RequestParam(value = "status") ArchiveStatus status,
  295. @ApiParam(name = "page", value = "消息", required = true)
  296. @RequestParam(value = "page") Integer page,
  297. @ApiParam(name = "size", value = "状态", required = true)
  298. @RequestParam(value = "size") Integer size) throws Exception {
  299. Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, filters, page, size);
  300. List<Map<String, Object>> updateSourceList = new ArrayList<>();
  301. final int _status = status.ordinal();
  302. result.forEach(item -> {
  303. Map<String, Object> updateSource = new HashMap<>();
  304. updateSource.put("_id", item.get("_id"));
  305. updateSource.put("archive_status", status.ordinal());
  306. if (_status == 2) {
  307. updateSource.put("fail_count", 3);
  308. } else {
  309. updateSource.put("fail_count", 0);
  310. }
  311. updateSourceList.add(updateSource);
  312. });
  313. elasticSearchUtil.bulkUpdate(INDEX, TYPE, updateSourceList);
  314. return result.getNumberOfElements();
  315. }
  316. @RequestMapping(value = ServiceApi.Packages.Packages, method = RequestMethod.GET)
  317. @ApiOperation(value = "搜索档案包")
  318. public Envelop page(
  319. @ApiParam(name = "filters", value = "过滤条件")
  320. @RequestParam(value = "filters", required = false) String filters,
  321. @ApiParam(name = "page", value = "页码", required = true, defaultValue = "1")
  322. @RequestParam(value = "page") int page,
  323. @ApiParam(name = "size", value = "分页大小", required = true, defaultValue = "15")
  324. @RequestParam(value = "size") int size) throws Exception {
  325. Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, filters, page, size);
  326. Envelop envelop = getPageResult(result.getContent(), (int)result.getTotalElements(), page, size);
  327. return envelop;
  328. }
  329. @RequestMapping(value = ServiceApi.Packages.Search, method = RequestMethod.GET)
  330. @ApiOperation(value = "搜索档案包")
  331. public List<EsDetailsPackage> search (
  332. @ApiParam(name = "filters", value = "过滤条件")
  333. @RequestParam(value = "filters", required = false) String filters,
  334. @ApiParam(name = "sorts", value = "排序")
  335. @RequestParam(value = "sorts", required = false) String sorts,
  336. @ApiParam(name = "page", value = "页码", required = true, defaultValue = "1")
  337. @RequestParam(value = "page") int page,
  338. @ApiParam(name = "size", value = "分页大小", required = true, defaultValue = "15")
  339. @RequestParam(value = "size") int size) throws Exception {
  340. Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, filters, sorts, page, size);
  341. List<EsDetailsPackage> esDetailsPackages = new ArrayList<>();
  342. for (Map<String, Object> temp : result) {
  343. esDetailsPackages.add(objectMapper.readValue(objectMapper.writeValueAsString(temp), EsDetailsPackage.class));
  344. }
  345. return esDetailsPackages;
  346. }
  347. @RequestMapping(value = ServiceApi.Packages.Package, method = RequestMethod.GET)
  348. @ApiOperation(value = "获取档案包", notes = "获取档案包的信息")
  349. public EsSimplePackage getPackage(
  350. @ApiParam(name = "id", value = "档案包编号")
  351. @PathVariable(value = "id") String id) throws Exception {
  352. Map<String, Object> source = elasticSearchUtil.findById(INDEX, TYPE, id);
  353. if (source != null) {
  354. EsSimplePackage esSimplePackage = new EsSimplePackage();
  355. esSimplePackage.set_id(String.valueOf(source.get("_id")));
  356. esSimplePackage.setPwd(String.valueOf(source.get("pwd")));
  357. esSimplePackage.setRemote_path(String.valueOf(source.get("remote_path")));
  358. esSimplePackage.setClient_id(String.valueOf(source.get("client_id")));
  359. esSimplePackage.setReceive_date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(String.valueOf(source.get("receive_date"))));
  360. return esSimplePackage;
  361. }
  362. return null;
  363. }
  364. @RequestMapping(value = ServiceApi.Packages.Download, method = {RequestMethod.GET})
  365. @ApiOperation(value = "下载档案包", notes = "下载档案包")
  366. public ResponseEntity downloadPackage(
  367. @ApiParam(name = "id", value = "档案包编号")
  368. @PathVariable(value = "id") String id,
  369. HttpServletResponse response) throws Exception {
  370. Map<String, Object> source = elasticSearchUtil.findById(INDEX, TYPE, id);
  371. if (source != null) {
  372. String [] tokens = String.valueOf(source.get("remote_path")).split(":");
  373. byte [] data = fastDFSUtil.download(tokens[0], tokens[1]);
  374. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  375. response.setHeader("Content-Disposition", "attachment; filename=" + id + ".zip");
  376. IOUtils.copy(new ByteArrayInputStream(data), response.getOutputStream());
  377. response.flushBuffer();
  378. return new ResponseEntity(HttpStatus.OK);
  379. }
  380. return null;
  381. }
  382. @RequestMapping(value = ServiceApi.Packages.PackageCrypto, method = RequestMethod.POST)
  383. @ApiOperation(value = "档案包密码加密")
  384. public String getPackageCrypto(
  385. @ApiParam(name = "org_code", value = "机构代码")
  386. @RequestParam(value = "org_code") String orgCode,
  387. @ApiParam(name = "package_crypto", value = "档案包解压密码,二次加密")
  388. @RequestParam(value = "package_crypto") String packageCrypto) throws Exception {
  389. MKey key = securityClient.getOrgKey(orgCode);
  390. if (key == null || key.getPublicKey() == null) {
  391. throw new ApiException(HttpStatus.FORBIDDEN, ErrorCode.FORBIDDEN, "Invalid private key, maybe you miss the organization code?");
  392. }
  393. return RSA.encrypt(packageCrypto, RSA.genPublicKey(key.getPublicKey()));
  394. }
  395. @RequestMapping(value = ServiceApi.Packages.Queue, method = RequestMethod.GET)
  396. @ApiOperation(value = "获取相关队列数")
  397. public long queueSize(
  398. @ApiParam(name = "queue", value = "队列 - 质控:analyze_queue 解析:resolve_queue 省平台:provincial_platform_queue", required = true)
  399. @RequestParam(value = "queue") String queue) throws Exception {
  400. if (!queue.endsWith("_vice")) {
  401. return redisTemplate.opsForList().size(queue);
  402. } else {
  403. return redisTemplate.opsForSet().size(queue);
  404. }
  405. }
  406. @RequestMapping(value = ServiceApi.Packages.Queue, method = RequestMethod.DELETE)
  407. @ApiOperation(value = "删除相关队列")
  408. public boolean deleteQueue(
  409. @ApiParam(name = "queue", value = "队列 - 解析:resolve_queue 质控:analyze_queue 省平台:provincial_platform_queue")
  410. @RequestParam(value = "queue") String queue) throws Exception {
  411. redisTemplate.delete(queue);
  412. return true;
  413. }
  414. @RequestMapping(value = ServiceApi.Packages.UploadProvincialQueueSize, method = RequestMethod.POST)
  415. @ApiOperation(value = "添加省平台上传队列", notes = "通过事件时间,添加已解析的档案包到队列中")
  416. public String uploadProvincialQueue(
  417. @ApiParam(name = "sorts", value = "排序(建议使用默认值,以解析较早之前的数据)", defaultValue = "event_date")
  418. @RequestParam(value = "sorts", required = false) String sorts,
  419. @ApiParam(name = "startDate", value = "开始时间(eg:2017-01-01)", required = true )
  420. @RequestParam(value = "startDate") String startDate,
  421. @ApiParam(name = "endDate", value = "结束时间(eg:2017-01-02)", required = true )
  422. @RequestParam(value = "endDate") String endDate) throws Exception {
  423. StringBuilder stringBuilder = new StringBuilder();
  424. stringBuilder.append("event_date>=" + startDate + " 00:00:00;");
  425. stringBuilder.append("event_date<" + endDate + " 00:00:00;");
  426. stringBuilder.append("archive_status=3");
  427. int count = (int)elasticSearchUtil.count(INDEX, TYPE, stringBuilder.toString());
  428. if (count > 0) {
  429. int page = count / PAGE_SIZE + 1;
  430. for (int i = 1; i <= page; i++) {
  431. Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, stringBuilder.toString(), sorts, i, PAGE_SIZE);
  432. for (Map<String, Object> item : result) {
  433. EsSimplePackage esSimplePackage = new EsSimplePackage();
  434. esSimplePackage.set_id(String.valueOf(item.get("_id")));
  435. esSimplePackage.setPwd(String.valueOf(item.get("pwd")));
  436. esSimplePackage.setRemote_path(String.valueOf(item.get("remote_path")));
  437. esSimplePackage.setClient_id(String.valueOf(item.get("client_id")));
  438. esSimplePackage.setRowkey(String.valueOf(item.get("profile_id")));
  439. esSimplePackage.setPatient_id(String.valueOf(item.get("patient_id")));
  440. esSimplePackage.setEvent_date(String.valueOf(item.get("event_date")));
  441. esSimplePackage.setEvent_no(String.valueOf(item.get("event_no")));
  442. esSimplePackage.setEvent_type( Integer.valueOf(item.get("event_type").toString()));
  443. esSimplePackage.setOrg_code(String.valueOf(item.get("org_code")));
  444. esSimplePackage.setIdcard_no(String.valueOf(item.get("demographic_id")));
  445. esSimplePackage.setPatient_name(String.valueOf(item.get("patient_name")));
  446. if (String.valueOf(item.get("org_code")) !=null ){
  447. String orgName = redisService.getOrgName(String.valueOf(item.get("org_code")));
  448. if (StringUtils.isNoneEmpty(orgName)) {
  449. esSimplePackage.setOrg_name(orgName);
  450. }
  451. }
  452. //存入省平台上传队列
  453. redisTemplate.opsForList().leftPush(RedisCollection.ProvincialPlatformQueue, objectMapper.writeValueAsString(esSimplePackage));
  454. }
  455. }
  456. return "操作成功!";
  457. } else {
  458. return "该段时间内无数据";
  459. }
  460. }
  461. @RequestMapping(value = MgrApi.Packages.Status, method = RequestMethod.PUT)
  462. @ApiOperation(value = "将接收时间为某段时间的结构化档案,状态更改为待解析", notes = "将接收时间为某段时间的结构化档案")
  463. public long bulkUpdate(@ApiParam(name = "startDate", value = "开始时间(eg:2017-01-01 00:00:00)", required = false )
  464. @RequestParam(value = "startDate",required = false) String startDate,
  465. @ApiParam(name = "endDate", value = "结束时间(eg:2017-01-01 00:00:00)", required = false )
  466. @RequestParam(value = "endDate",required = false) String endDate,
  467. @ApiParam(name = "orgCode", value = "机构编码", required = false )
  468. @RequestParam(value = "orgCode",required = false) String orgCode,
  469. @ApiParam(name = "packType", value = "包类型", required = false )
  470. @RequestParam(value = "packType",required = false) String packType){
  471. String filters = "archive_status<>0;";
  472. if(StringUtils.isNotBlank(startDate)){
  473. filters+="receive_date>="+startDate+";";
  474. }
  475. if(StringUtils.isNotBlank(endDate)){
  476. filters+="receive_date<="+endDate+";";
  477. }
  478. if(StringUtils.isNotBlank(orgCode)){
  479. filters += "org_code="+orgCode+";";
  480. }
  481. if(StringUtils.isNotBlank(packType)){
  482. filters += "pack_type="+packType+";";
  483. }
  484. long count = elasticSearchUtil.count(INDEX, TYPE, filters);
  485. Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, filters, 1, 10000);
  486. while (CollectionUtils.isNotEmpty(result.getContent())) {
  487. List<Map<String, Object>> updateSourceList = new ArrayList<>();
  488. result.forEach(item -> {
  489. Map<String, Object> updateSource = new HashMap<>();
  490. updateSource.put("_id", item.get("_id"));
  491. updateSource.put("analyze_status",0);
  492. updateSource.put("fail_count",0);
  493. updateSource.put("archive_status",0);
  494. updateSource.put("analyze_fail_count",0);
  495. updateSourceList.add(updateSource);
  496. });
  497. if (!updateSourceList.isEmpty()) {
  498. elasticSearchUtil.bulkUpdate(INDEX, TYPE, updateSourceList);
  499. }
  500. result = elasticSearchUtil.page(INDEX, TYPE, filters, 1, 10000);
  501. }
  502. return count;
  503. }
  504. //-------------------------------------------------
  505. /*@RequestMapping(value = ServiceApi.Packages.Migrate, method = RequestMethod.POST)
  506. @ApiOperation(value = "数据迁移")
  507. public boolean migrate() throws Exception {
  508. jsonArchivesService.migrate();
  509. return true;
  510. }*/
  511. }