ArchiveServer.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package thyyxxk.webserver.service.archive;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.format.FastDateFormat;
  4. import cn.hutool.crypto.asymmetric.KeyType;
  5. import cn.hutool.crypto.asymmetric.RSA;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  8. import com.dtflys.forest.Forest;
  9. import com.dtflys.forest.http.ForestRequest;
  10. import lombok.Data;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import thyyxxk.webserver.config.envionment.ArchiveConfig;
  16. import thyyxxk.webserver.config.exception.BizException;
  17. import thyyxxk.webserver.config.exception.ExceptionEnum;
  18. import thyyxxk.webserver.dao.his.archive.PatientArchiveDao;
  19. import thyyxxk.webserver.entity.ResultVo;
  20. import thyyxxk.webserver.entity.archive.PatientArchive;
  21. import thyyxxk.webserver.http.websocket.dto.ArchiveSocketParam;
  22. import thyyxxk.webserver.http.websocket.SocketV2;
  23. import thyyxxk.webserver.service.hutoolcache.UserCache;
  24. import thyyxxk.webserver.utils.*;
  25. import java.io.IOException;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. @Slf4j
  30. @Service
  31. public class ArchiveServer {
  32. private final ArchiveConfig archiveData;
  33. private final PatientArchiveDao patientArchiveDao;
  34. private final SocketV2 socketV2;
  35. private final UserCache userCache;
  36. private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdTugOj9/hmOJIeXB7BOVKlczqjhXp6s7cvXoSrxhllgApZOivyLaq/4hsCw0m+70xj0GNIMIy8DFKKhz5z0DdOdPwoUwIbzmH8VZfgSM3eridfYJ8wnP3XOLlz3daX5jwD2rcNAMGar9vbi4k7gnkbH7vlRpujdeJyhqcCONKMwIDAQAB";
  37. public static RSA rsa = new RSA(null, publicKey);
  38. @Value("${is-prod}")
  39. private Boolean isProd;
  40. @Data
  41. public static class UploadResult {
  42. private String id;
  43. private String path;
  44. }
  45. public ArchiveServer(
  46. ArchiveConfig archive,
  47. PatientArchiveDao patientArchiveDao, SocketV2 socketV2,
  48. UserCache userCache) {
  49. this.socketV2 = socketV2;
  50. this.patientArchiveDao = patientArchiveDao;
  51. this.archiveData = archive;
  52. this.userCache = userCache;
  53. }
  54. public List<PatientArchive> getPatientArchives(String patNo, Integer times) {
  55. List<PatientArchive> array = patientArchiveDao.getPatientList(patNo, times);
  56. return TreeUtilV2.create(array)
  57. .sort((a) -> {
  58. if (a.getSort() == null)
  59. return Integer.MAX_VALUE;
  60. return a.getSort();
  61. })
  62. .execute();
  63. }
  64. public ForestRequest<?> sendApi(String url) {
  65. return sendApi(url, null);
  66. }
  67. public ForestRequest<?> sendApi(String url, String code) {
  68. String now = FastDateFormat.getInstance("yyyy-MM-ddHH:mm").format(new DateTime());
  69. String currentCode = code == null ? TokenUtil.getInstance().getTokenUserId() : code;
  70. String string = currentCode + "_" + now;
  71. String authorization = rsa.encryptBase64(string, KeyType.PublicKey);
  72. return Forest.post(archiveData.getArchiveUrl() + url)
  73. .addHeader("content-type", "application/json")
  74. .addHeader("Authorization", authorization);
  75. }
  76. public ForestRequest<?> sendEmrApi(String url) {
  77. return sendApi("/emr" + url);
  78. }
  79. public String submitTask(List<TaskPatient> data) {
  80. ForestRequest<?> api = sendEmrApi("/submitTask");
  81. String code = TokenUtil.getInstance().getTokenUserId();
  82. String name = userCache.getUserInfoByToken().getName();
  83. for (TaskPatient item : data) {
  84. item.setSubmitCode(code);
  85. item.setSubmitName(name);
  86. }
  87. api.addBody(data);
  88. api.execute();
  89. return "";
  90. }
  91. public ResultVo<String> sort(TaskPatient data) {
  92. ListUtil.batchList(data.getArchives(), PatientArchiveDao.class, BaseMapper::updateById);
  93. JSONObject js = new JSONObject() {{
  94. put("code", "change");
  95. put("data", new JSONObject() {{
  96. put("code", TokenUtil.getInstance().getTokenUserId());
  97. put("message", "排序");
  98. }});
  99. }};
  100. socketV2.sendArchiveMessage(
  101. ArchiveSocketParam.of(
  102. data.getPatNo() + "_" + data.getTimes(),
  103. js.toJSONString())
  104. );
  105. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "排序成功。");
  106. }
  107. public Object upload(MultipartFile file, String patNo, Integer times, String parent) {
  108. ForestRequest<?> forestRequest = sendEmrApi("/upload");
  109. try {
  110. forestRequest.contentFormUrlEncoded()
  111. .addHeader("content-type", "multipart/form-data")
  112. .addFile("file", file.getInputStream(), file.getOriginalFilename(), file.getContentType())
  113. .addBody("patNo", patNo)
  114. .addBody("times", times)
  115. .addBody("parent", parent);
  116. } catch (IOException e) {
  117. throw new RuntimeException(e);
  118. }
  119. return forestRequest.execute();
  120. }
  121. public Object delFile(PatientArchive archive) {
  122. return sendEmrApi("/delFile").addBody(archive).execute();
  123. }
  124. public Object addDir(PatientArchive archive) {
  125. return sendEmrApi("/addDir").addBody(archive).execute();
  126. }
  127. public Object rename(String id, String name) {
  128. return sendEmrApi("/rename")
  129. .addHeader("content-type", "multipart/form-data")
  130. .contentFormUrlEncoded()
  131. .addBody("id", id)
  132. .addBody("name", name)
  133. .execute();
  134. }
  135. public Object getAllLog(String patNo, Integer times) {
  136. return sendEmrApi("/getAllLog")
  137. .addHeader("content-type", "multipart/form-data")
  138. .contentFormUrlEncoded()
  139. .addBody("patNo", patNo)
  140. .addBody("times", times)
  141. .execute();
  142. }
  143. public Object getTaskNameList() {
  144. ForestRequest<?> forestRequest = sendEmrApi("/getTaskNameList");
  145. return forestRequest.execute();
  146. }
  147. /**
  148. * 上传文件到服务器,如果需要删除请调用这个接口
  149. * 自动识别换当前环境
  150. * {@link #delUploadById(String) }
  151. *
  152. * @param file 上传的文件
  153. * @param path 文件的路径不能包含文件名称 如 xxx.png 是不行的
  154. * @return 返回文件的id,文件的地址
  155. */
  156. public UploadResult uploadFile(MultipartFile file, String path) {
  157. return uploadFile(file, path, !isProd, null);
  158. }
  159. public UploadResult uploadFileByCode(MultipartFile file, String path, String code) {
  160. return uploadFile(file, path, !isProd, code);
  161. }
  162. public UploadResult uploadFile(MultipartFile file, String path, boolean isTest) {
  163. return uploadFile(file, path, isTest, null);
  164. }
  165. /**
  166. * 上传文件到服务器,如果需要删除请调用这个接口 {@link #delUploadById(String) }
  167. *
  168. * @param file 上传的文件
  169. * @param path 文件的路径不能包含文件名称 如 xxx.png 是不行的
  170. * @param isTest 是否放入测试的文件夹
  171. * @return 返回文件的id,文件的地址
  172. */
  173. public UploadResult uploadFile(MultipartFile file, String path, boolean isTest, String userCode) {
  174. JSONObject execute;
  175. try {
  176. execute = sendApi("/uploadFile", userCode)
  177. .contentFormUrlEncoded()
  178. .addHeader("content-type", "multipart/form-data")
  179. .addFile("file", file.getInputStream(), file.getOriginalFilename(), file.getContentType())
  180. .addBody("path", path)
  181. .addBody("isTest", isTest)
  182. .execute(JSONObject.class);
  183. } catch (Exception e) {
  184. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "上传文件错误:" + e.getMessage());
  185. }
  186. Integer code = execute.getInteger("code");
  187. if (ExceptionEnum.SUCCESS.getCode() != code) {
  188. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "上传文件错误:" + execute.getString("message"));
  189. }
  190. return execute.getObject("data", UploadResult.class);
  191. }
  192. public void delUploadById(String id) {
  193. JSONObject execute = sendApi("/delUploadById/" + id).execute(JSONObject.class);
  194. if (ExceptionEnum.SUCCESS.getCode() != execute.getInteger("code")) {
  195. throw new BizException(ExceptionEnum.LOGICAL_ERROR, execute.getString("message"));
  196. }
  197. }
  198. public UploadResult uploadFileByStr(String data, String path, boolean isTest, String userCode) {
  199. JSONObject execute;
  200. Map<String, Object> body = new HashMap<>();
  201. body.put("data", data);
  202. body.put("path", path);
  203. body.put("isTest", isTest);
  204. try {
  205. execute = sendApi("/uploadByStr", userCode)
  206. .addHeader("content-type", "application/json")
  207. .addBody(body)
  208. .execute(JSONObject.class);
  209. } catch (Exception e) {
  210. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "上传文件错误:" + e.getMessage());
  211. }
  212. Integer code = execute.getInteger("code");
  213. if (ExceptionEnum.SUCCESS.getCode() != code) {
  214. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "上传文件错误:" + execute.getString("message"));
  215. }
  216. return execute.getObject("data", UploadResult.class);
  217. }
  218. public UploadResult uploadFileByStr(String data, String path, boolean isTest) {
  219. return uploadFileByStr(data, path, isTest, null);
  220. }
  221. public UploadResult uploadFileByStr(String data, String path) {
  222. return uploadFileByStr(data, path, !isProd, null);
  223. }
  224. public UploadResult uploadFileByBytes(byte[] data, String path, boolean isTest, String userCode) {
  225. JSONObject execute;
  226. Map<String, Object> body = new HashMap<>();
  227. body.put("data", data);
  228. body.put("path", path);
  229. body.put("isTest", isTest);
  230. try {
  231. execute = sendApi("/uploadByByte", userCode)
  232. .addHeader("content-type", "application/json")
  233. .addBody(body)
  234. .execute(JSONObject.class);
  235. } catch (Exception e) {
  236. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "上传文件错误:" + e.getMessage());
  237. }
  238. Integer code = execute.getInteger("code");
  239. if (ExceptionEnum.SUCCESS.getCode() != code) {
  240. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "上传文件错误:" + execute.getString("message"));
  241. }
  242. return execute.getObject("data", UploadResult.class);
  243. }
  244. public UploadResult uploadFileByBytes(byte[] data, String path, boolean isTest) {
  245. return uploadFileByBytes(data, path, isTest, null);
  246. }
  247. public UploadResult uploadFileByBytes(byte[] data, String path) {
  248. return uploadFileByBytes(data, path, !isProd, null);
  249. }
  250. /**
  251. * 创建文件
  252. *
  253. * @param file 文件
  254. * @param path 地址用 /开头
  255. * @return 返回你传入的地址,如果你是工作集成平台的话可以指定用这个地址访问到
  256. */
  257. public String createFile(MultipartFile file, String path) {
  258. JSONObject execute;
  259. try {
  260. execute = sendApi("/createFile").contentFormUrlEncoded()
  261. .addHeader("content-type", "multipart/form-data")
  262. .addFile("file", file.getInputStream(), file.getOriginalFilename(), file.getContentType())
  263. .addBody("path", path)
  264. .execute(JSONObject.class);
  265. } catch (Exception e) {
  266. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "上传文件错误:" + e.getMessage());
  267. }
  268. Integer code = execute.getInteger("code");
  269. if (ExceptionEnum.SUCCESS.getCode() != code) {
  270. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "上传文件错误:" + execute.getString("message"));
  271. }
  272. return execute.getObject("data", String.class);
  273. }
  274. public String deleteFile(String path) {
  275. Map<String, String> map = new HashMap<>();
  276. map.put("path", path);
  277. JSONObject execute = sendApi("/deleteFile")
  278. .addBody(map)
  279. .execute(JSONObject.class);
  280. Integer code = execute.getInteger("code");
  281. if (ExceptionEnum.SUCCESS.getCode() != code) {
  282. throw new BizException(ExceptionEnum.LOGICAL_ERROR, execute.getString("message"));
  283. }
  284. return execute.getObject("data", String.class);
  285. }
  286. }