ZyDictJob.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package com.yihu.wlyy.job;
  2. import com.yihu.wlyy.entity.zydict.*;
  3. import com.yihu.wlyy.repository.zydict.*;
  4. import com.yihu.wlyy.service.third.jw.JwPrescriptionService;
  5. import net.sf.json.JSONArray;
  6. import net.sf.json.JSONObject;
  7. import org.quartz.Job;
  8. import org.quartz.JobExecutionContext;
  9. import org.quartz.JobExecutionException;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.List;
  17. import java.util.concurrent.ExecutionException;
  18. /**
  19. * Created by chenweida on 2017/8/10.
  20. * 每天1点同步 智业的字典数据
  21. */
  22. public class ZyDictJob implements Job {
  23. private Logger logger = LoggerFactory.getLogger(ZyDictJob.class);
  24. @Autowired
  25. private ZyCommonDictDao zyCommonDictDao;
  26. @Autowired
  27. private ZyIvDeptDictDao zyIvDeptDictDao;
  28. @Autowired
  29. private ZyIvDeptStaffAllotDictDao zyIvDeptStaffAllotDictDao;
  30. @Autowired
  31. private ZyIvDiagnoseClassDictDao zyIvDiagnoseClassDictDao;
  32. @Autowired
  33. private ZyIvDiagnoseDictDao zyIvDiagnoseDictDao;
  34. @Autowired
  35. private ZyIvOrgPhysicAllotDictDao zyIvOrgPhysicAllotDictDao;
  36. @Autowired
  37. private ZyIvPhysicDictDao zyIvPhysicDictDao;
  38. @Autowired
  39. private ZyIvStaffDictDao zyIvStaffDictDao;
  40. @Autowired
  41. private ZyIvStaffRegTypeAllotDictDao zyIvStaffRegTypeAllotDictDao;
  42. @Autowired
  43. private ZyIvSubjectClassDictDao zyIvSubjectClassDictDao;
  44. @Autowired
  45. private JwPrescriptionService jwPrescriptionService;
  46. @Override
  47. public void execute(JobExecutionContext context) throws JobExecutionException {
  48. try {
  49. //同步 zy_common_dict
  50. synchronizeZyCommonDict();
  51. //同步 zy_iv_dept_dict
  52. synchronizeZy_iv_dept_dict();
  53. //同步 zy_iv_dept_staff_allot_dict
  54. synchronizeZy_iv_dept_staff_allot_dict();
  55. //同步 zy_iv_diagnose_class_dict
  56. synchronizeZy_iv_diagnose_class_dict();
  57. //同步 zy_iv_org_physic_allot_dict
  58. synchronizeZy_iv_org_physic_allot_dict();
  59. //同步 zy_iv_physic_dict
  60. synchronizeZy_iv_physic_dict();
  61. //同步 zy_iv_staff_dict
  62. synchronizeZ_iv_staff_dict();
  63. //同步 zy_iv_staff_reg_type_allot_dict
  64. synchronizeZy_iv_staff_reg_type_allot_dict();
  65. //同步 zy_iv_subject_class_dict
  66. synchronizeZy_iv_subject_class_dict();
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. @Transactional
  72. private void synchronizeZy_iv_subject_class_dict() throws Exception {
  73. String dictName = this.IV_SUBJECT_CLASS_DICT;
  74. List<ZyIvSubjectClassDict> zyIvSubjectClassDicts = new ArrayList<>();
  75. //得到list
  76. commonGetOneDict(zyIvSubjectClassDicts, ZyIvSubjectClassDict.class, dictName);
  77. if (zyIvSubjectClassDicts.size() > 0) {
  78. //清空表
  79. zyIvSubjectClassDictDao.deleteAll();
  80. //新增数据
  81. zyIvSubjectClassDictDao.save(zyIvSubjectClassDicts);
  82. }
  83. }
  84. @Transactional
  85. private void synchronizeZy_iv_staff_reg_type_allot_dict() throws Exception {
  86. String dictName = this.IV_STAFF_REG_TYPE_ALLOT_DICT;
  87. List<ZyIvStaffRegTypeAllotDict> zyIvStaffRegTypeAllotDicts = new ArrayList<>();
  88. //得到list
  89. commonGetOneDict(zyIvStaffRegTypeAllotDicts, ZyIvStaffRegTypeAllotDict.class, dictName);
  90. //清空表
  91. zyIvStaffRegTypeAllotDictDao.deleteAll();
  92. //新增数据
  93. zyIvStaffRegTypeAllotDictDao.save(zyIvStaffRegTypeAllotDicts);
  94. }
  95. @Transactional
  96. private void synchronizeZ_iv_staff_dict() throws Exception {
  97. String dictName = this.IV_STAFF_DICT;
  98. List<ZyIvStaffDict> zyIvStaffDicts = new ArrayList<>();
  99. //得到list
  100. commonGetOneDict(zyIvStaffDicts, ZyIvDeptDict.class, dictName);
  101. //清空表
  102. zyIvStaffDictDao.deleteAll();
  103. //新增数据
  104. zyIvStaffDictDao.save(zyIvStaffDicts);
  105. }
  106. @Transactional
  107. private void synchronizeZy_iv_physic_dict() throws Exception {
  108. String dictName = this.IV_PHYSIC_DICT;
  109. List<ZyIvPhysicDict> zyIvPhysicDicts = new ArrayList<>();
  110. //得到list
  111. commonGetOneDict(zyIvPhysicDicts, ZyIvPhysicDict.class, dictName);
  112. //清空表
  113. zyIvPhysicDictDao.deleteAll();
  114. //新增数据
  115. zyIvPhysicDictDao.save(zyIvPhysicDicts);
  116. }
  117. @Transactional
  118. private void synchronizeZy_iv_org_physic_allot_dict() throws Exception {
  119. String dictName = this.IV_ORG_PHYSIC_ALLOT_DICT;
  120. List<ZyIvOrgPhysicAllotDict> zyIvOrgPhysicAllotDicts = new ArrayList<>();
  121. //得到list
  122. commonGetOneDict(zyIvOrgPhysicAllotDicts, ZyIvOrgPhysicAllotDict.class, dictName);
  123. if (zyIvOrgPhysicAllotDicts.size() > 0) {
  124. //清空表
  125. zyIvOrgPhysicAllotDictDao.deleteAll();
  126. //新增数据
  127. zyIvOrgPhysicAllotDictDao.save(zyIvOrgPhysicAllotDicts);
  128. }
  129. }
  130. @Transactional
  131. private void synchronizeZy_iv_diagnose_class_dict() throws Exception {
  132. String dictName = this.IV_DIAGNOSE_CLASS_DICT;
  133. List<ZyIvDiagnoseClassDict> zIvDiagnoseClassDicts = new ArrayList<>();
  134. //得到list
  135. commonGetOneDict(zIvDiagnoseClassDicts, ZyIvDiagnoseClassDict.class, dictName);
  136. if (zIvDiagnoseClassDicts.size() > 0) {
  137. //清空表
  138. zyIvDiagnoseClassDictDao.deleteAll();
  139. //新增数据
  140. zyIvDiagnoseClassDictDao.save(zIvDiagnoseClassDicts);
  141. }
  142. }
  143. @Transactional
  144. private void synchronizeZy_iv_dept_staff_allot_dict() throws Exception {
  145. String dictName = this.IV_DEPT_STAFF_ALLOT_DICT;
  146. List<ZyIvDeptStaffAllotDict> syIvDeptStaffAllotDictz = new ArrayList<>();
  147. //得到list
  148. commonGetOneDict(syIvDeptStaffAllotDictz, ZyIvDeptStaffAllotDict.class, dictName);
  149. if (syIvDeptStaffAllotDictz.size() > 0) {
  150. //清空表
  151. zyIvDeptStaffAllotDictDao.deleteAll();
  152. //新增数据
  153. zyIvDeptStaffAllotDictDao.save(syIvDeptStaffAllotDictz);
  154. }
  155. }
  156. @Transactional
  157. private void synchronizeZyCommonDict() throws Exception {
  158. String[] dictNames = new String[]{
  159. this.IV_PHYSIC_FORM_DICT, this.IV_PHYSIC_INJECT_PLACE_DICT, this.IV_PHYSIC_SKIN_TEST_DICT,
  160. this.IV_RATE_TYPE_DICT, this.IV_RECIPE_FREQUENCY_DICT, this.IV_RECIPE_USAGE_DICT,
  161. this.IV_SEX_DICT, this.IV_PHYSIC_TOXICOLOGY_TYPE_DICT, this.IV_MEASURE_UNIT_DICT,
  162. this.IV_DEPT_TYPE_DICT};
  163. List<ZyCommonDict> zyCommonDictz = new ArrayList<>();
  164. Boolean isFlush = true;
  165. for (String dictName : dictNames) {
  166. String json = null;
  167. try {
  168. //调用智业的接口得到字典
  169. json = jwPrescriptionService.getDictForI(dictName);
  170. JSONObject returnJson = JSONObject.fromObject(json);
  171. if (returnJson.getInt("status") != 200) {
  172. throw new Exception("zy dict error status not 200");
  173. }
  174. if (returnJson.containsKey("data")) {
  175. JSONArray dataJSONArray = returnJson.getJSONObject("data").getJSONArray("returnData");
  176. ZyCommonDict[] zyCommonDictArrays = (ZyCommonDict[]) JSONArray.toArray(dataJSONArray, ZyCommonDict.class);
  177. zyCommonDictz.addAll(Arrays.asList(zyCommonDictArrays));
  178. } else {
  179. throw new Exception("zy dict error no contain data");
  180. }
  181. } catch (Exception e) {
  182. isFlush = false; //设置状态不能更新
  183. logger.error("dictName:" + dictName);
  184. logger.error("returnJson:" + json);
  185. logger.error(e.getMessage());
  186. }
  187. }
  188. //判断是否可以更新
  189. if (isFlush) {
  190. // 判断返回的数据是否大于0
  191. if (zyCommonDictz.size() > 0) {
  192. //清空表
  193. zyCommonDictDao.deleteAll();
  194. //新增数据
  195. zyCommonDictDao.save(zyCommonDictz);
  196. }
  197. }
  198. }
  199. @Transactional
  200. private void synchronizeZy_iv_dept_dict() throws Exception {
  201. String dictName = this.IV_DEPT_DICT;
  202. List<ZyIvDeptDict> zyIvDeptDicts = new ArrayList<>();
  203. //得到list
  204. commonGetOneDict(zyIvDeptDicts, ZyIvDeptDict.class, dictName);
  205. if (zyIvDeptDicts.size() > 0) {
  206. //清空表
  207. zyIvDeptDictDao.deleteAll();
  208. //新增数据
  209. zyIvDeptDictDao.save(zyIvDeptDicts);
  210. }
  211. }
  212. /**
  213. * 得到单个字典
  214. *
  215. * @param tList
  216. * @param clazz class
  217. * @param dictName 字典名称
  218. */
  219. private void commonGetOneDict(List tList, Class clazz, String dictName) {
  220. String json = null;
  221. try {
  222. //调用智业的接口得到字典
  223. json = jwPrescriptionService.getDictForI(dictName);
  224. JSONObject returnJson = JSONObject.fromObject(json);
  225. if (returnJson.getInt("status") != 200) {
  226. throw new Exception("zy dict error status not 200");
  227. }
  228. if (returnJson.containsKey("data")) {
  229. JSONArray dataJSONArray = returnJson.getJSONObject("data").getJSONArray("returnData");
  230. tList.addAll(Arrays.asList(JSONArray.toArray(dataJSONArray, clazz)));
  231. } else {
  232. throw new Exception("zy dict error no contain data");
  233. }
  234. } catch (Exception e) {
  235. logger.error("dictName:" + dictName);
  236. logger.error("returnJson:" + json);
  237. logger.error(e.getMessage());
  238. }
  239. }
  240. /**
  241. * 智业的字典
  242. */
  243. public static final String IV_MEASURE_UNIT_DICT = "IV_MEASURE_UNIT_DICT";//("计量单位字典", "IV_MEASURE_UNIT_DICT"),
  244. public static final String IV_PHYSIC_FORM_DICT = " IV_PHYSIC_FORM_DICT";//("药品剂型字典", "IV_PHYSIC_FORM_DICT"),
  245. public static final String IV_PHYSIC_INJECT_PLACE_DICT = " IV_PHYSIC_INJECT_PLACE_DICT(";//"计量单位字典", "IV_PHYSIC_INJECT_PLACE_DICT"),
  246. public static final String IV_PHYSIC_SKIN_TEST_DICT = " IV_PHYSIC_SKIN_TEST_DICT";//("皮试类型字典", "IV_PHYSIC_SKIN_TEST_DICT"),
  247. public static final String IV_RATE_TYPE_DICT = " IV_RATE_TYPE_DICT";//("费别字典", "IV_RATE_TYPE_DICT"),
  248. public static final String IV_RECIPE_FREQUENCY_DICT = " IV_RECIPE_FREQUENCY_DICT";//("频次字典", "IV_RECIPE_FREQUENCY_DICT"),
  249. public static final String IV_RECIPE_USAGE_DICT = " IV_RECIPE_USAGE_DICT";//("用药方法字典", "IV_RECIPE_USAGE_DICT"),
  250. public static final String IV_SEX_DICT = " IV_SEX_DICT";//("性别字典", "IV_SEX_DICT"),
  251. public static final String IV_PHYSIC_DICT = " IV_PHYSIC_DICT";//("药品字典", "IV_PHYSIC_DICT"),
  252. public static final String IV_ORG_PHYSIC_ALLOT_DICT = " IV_ORG_PHYSIC_ALLOT_DICT";//("机构药品分发字典", "IV_ORG_PHYSIC_ALLOT_DICT "),
  253. public static final String IV_SUBJECT_CLASS_DICT = " IV_SUBJECT_CLASS_DICT";//("科目类别字典", "IV_SUBJECT_CLASS_DICT"),
  254. public static final String IV_PHYSIC_TOXICOLOGY_TYPE_DICT = " IV_PHYSIC_TOXICOLOGY_TYPE_DICT";//("药品毒理分类字典", "IV_PHYSIC_TOXICOLOGY_TYPE_DICT"),
  255. public static final String IV_DEPT_DICT = " IV_DEPT_DICT";//("科室字典", "IV_DEPT_DICT"),
  256. public static final String IV_DEPT_TYPE_DICT = " IV_DEPT_TYPE_DICT";//("科室类型字典", "IV_DEPT_TYPE_DICT"),
  257. public static final String IV_DIAGNOSE_DICT = " IV_DIAGNOSE_DICT";//("诊断字典", "IV_DIAGNOSE_DICT"),
  258. public static final String IV_DIAGNOSE_CLASS_DICT = " IV_DIAGNOSE_CLASS_DICT";//("诊断类别字典", "IV_DIAGNOSE_CLASS_DICT"),
  259. public static final String IV_STAFF_DICT = " IV_STAFF_DICT";//("员工字典", "IV_STAFF_DICT"),
  260. public static final String IV_STAFF_REG_TYPE_ALLOT_DICT = " IV_STAFF_REG_TYPE_ALLOT_DICT";//("员工挂号类型配置字典", "IV_STAFF_REG_TYPE_ALLOT_DICT"),
  261. public static final String IV_DEPT_STAFF_ALLOT_DICT = " IV_DEPT_STAFF_ALLOT_DICT";//("科室员工配置字典", "IV_DEPT_STAFF_ALLOT_DICT");
  262. /**
  263. * 字典表的枚举类
  264. */
  265. public enum TableName {
  266. zy_common_dict("通用字典", "zy_common_dict"),
  267. zy_iv_dept_dict("科室字典", "zy_iv_dept_dict"),
  268. zy_iv_dept_staff_allot_dict("科室员工配置字典", "zy_iv_dept_staff_allot_dict"),
  269. zy_iv_diagnose_class_dict("诊断类别字典", "zy_iv_diagnose_class_dict"),
  270. zy_iv_diagnose_dict("诊断字典表", "zy_iv_diagnose_dict"),
  271. zy_iv_org_physic_allot_dict("机构药品分发字典", "zy_iv_org_physic_allot_dict"),
  272. zy_iv_physic_dict("药品字典", "zy_iv_physic_dict"),
  273. zy_iv_staff_dict("员工字典", "zy_iv_staff_dict"),
  274. zy_iv_staff_reg_type_allot_dict("员工挂号类型配置字典", "zy_iv_staff_reg_type_allot_dict"),
  275. zy_iv_subject_class_dict("科目类别字典", "zy_iv_subject_class_dict");
  276. TableName(String name, String dict) {
  277. this.name = name;
  278. this.dict = dict;
  279. }
  280. private String name;
  281. private String dict;
  282. public String getName() {
  283. return name;
  284. }
  285. public void setName(String name) {
  286. this.name = name;
  287. }
  288. public String getDict() {
  289. return dict;
  290. }
  291. public void setDict(String dict) {
  292. this.dict = dict;
  293. }
  294. }
  295. /**
  296. * 根据字典获取表名
  297. *
  298. * @param dictName
  299. * @return
  300. */
  301. public String getTableName(String dictName) {
  302. switch (dictName) {
  303. case IV_PHYSIC_FORM_DICT: {
  304. return TableName.zy_common_dict.getDict();
  305. }
  306. case IV_PHYSIC_INJECT_PLACE_DICT: {
  307. return TableName.zy_common_dict.getDict();
  308. }
  309. case IV_PHYSIC_SKIN_TEST_DICT: {
  310. return TableName.zy_common_dict.getDict();
  311. }
  312. case IV_RATE_TYPE_DICT: {
  313. return TableName.zy_common_dict.getDict();
  314. }
  315. case IV_RECIPE_FREQUENCY_DICT: {
  316. return TableName.zy_common_dict.getDict();
  317. }
  318. case IV_RECIPE_USAGE_DICT: {
  319. return TableName.zy_common_dict.getDict();
  320. }
  321. case IV_SEX_DICT: {
  322. return TableName.zy_common_dict.getDict();
  323. }
  324. case IV_PHYSIC_TOXICOLOGY_TYPE_DICT: {
  325. return TableName.zy_common_dict.getDict();
  326. }
  327. case IV_MEASURE_UNIT_DICT: {
  328. return TableName.zy_common_dict.getDict();
  329. }
  330. case IV_DEPT_TYPE_DICT: {
  331. return TableName.zy_common_dict.getDict();
  332. }
  333. case IV_PHYSIC_DICT: {
  334. return TableName.zy_iv_physic_dict.getDict();
  335. }
  336. case IV_ORG_PHYSIC_ALLOT_DICT: {
  337. return TableName.zy_iv_org_physic_allot_dict.getDict();
  338. }
  339. case IV_SUBJECT_CLASS_DICT: {
  340. return TableName.zy_iv_subject_class_dict.getDict();
  341. }
  342. case IV_DIAGNOSE_DICT: {
  343. return TableName.zy_iv_diagnose_dict.getDict();
  344. }
  345. case IV_DIAGNOSE_CLASS_DICT: {
  346. return TableName.zy_iv_diagnose_class_dict.getDict();
  347. }
  348. case IV_STAFF_DICT: {
  349. return TableName.zy_iv_staff_dict.getDict();
  350. }
  351. case IV_STAFF_REG_TYPE_ALLOT_DICT: {
  352. return TableName.zy_iv_staff_reg_type_allot_dict.getDict();
  353. }
  354. case IV_DEPT_STAFF_ALLOT_DICT: {
  355. return TableName.zy_iv_dept_staff_allot_dict.getDict();
  356. }
  357. case IV_DEPT_DICT: {
  358. return TableName.zy_iv_dept_dict.getDict();
  359. }
  360. }
  361. return "";
  362. }
  363. }