PatientDiseaseService.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package com.yihu.wlyy.service.app.disease;
  2. import com.yihu.wlyy.entity.dict.Disease;
  3. import com.yihu.wlyy.entity.patient.SignFamily;
  4. import com.yihu.wlyy.entity.doctor.team.sign.DoctorPatientGroupInfo;
  5. import com.yihu.wlyy.entity.patient.Patient;
  6. import com.yihu.wlyy.entity.patient.PatientDisease;
  7. import com.yihu.wlyy.repository.dict.DiseaseDao;
  8. import com.yihu.wlyy.repository.doctor.DoctorPatientGroupInfoDao;
  9. import com.yihu.wlyy.repository.patient.PatientDao;
  10. import com.yihu.wlyy.repository.patient.PatientDiseaseDao;
  11. import com.yihu.wlyy.repository.patient.SignFamilyDao;
  12. import com.yihu.wlyy.service.BaseService;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.json.JSONArray;
  15. import org.json.JSONObject;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.data.redis.core.StringRedisTemplate;
  18. import org.springframework.jdbc.core.JdbcTemplate;
  19. import org.springframework.stereotype.Service;
  20. import javax.transaction.Transactional;
  21. import java.util.*;
  22. /**
  23. * 患者疾病服务
  24. * <p>
  25. * Created by lyr on 2016/09/09.
  26. */
  27. @Service
  28. @Transactional
  29. public class PatientDiseaseService extends BaseService {
  30. @Autowired
  31. DiseaseDao diseaseDao;
  32. @Autowired
  33. PatientDiseaseDao patientDiseaseDao;
  34. @Autowired
  35. DoctorPatientGroupInfoDao groupInfoDao;
  36. @Autowired
  37. StringRedisTemplate redisTemplate;
  38. @Autowired
  39. SignFamilyDao signFamilyDao;
  40. @Autowired
  41. PatientDao patientDao;
  42. @Autowired
  43. JdbcTemplate jdbcTemplate;
  44. /**
  45. * 根据居民code查询疾病
  46. *
  47. * @param patient 居民code
  48. * @return
  49. */
  50. public List<PatientDisease> getPatientDisease(String patient) {
  51. return patientDiseaseDao.findByPatient(patient);
  52. }
  53. /**
  54. * 根据居民code查询疾病
  55. *
  56. * @param patient 居民code
  57. * @return
  58. */
  59. public List<PatientDisease> getPatientDiseaseSs(String patient) {
  60. return patientDiseaseDao.findByPatientAndSignType(patient,"1");
  61. }
  62. /**
  63. * 更新患者疾病表
  64. *
  65. * @param patient 患者
  66. * @param disease 疾病
  67. * @return
  68. */
  69. public boolean updatePatientDisease(String patient, String disease) {
  70. try {
  71. // 删除患者家庭签约疾病
  72. patientDiseaseDao.updateDiseaseDel(patient);
  73. if (!StringUtils.isEmpty(disease) && !disease.equals("0")) {
  74. String[] diseases = disease.split(",");
  75. List<PatientDisease> patientDiseases = new ArrayList<>();
  76. for (String dis : diseases) {
  77. if (StringUtils.isEmpty(dis)) {
  78. continue;
  79. } else {
  80. PatientDisease patientDisease = new PatientDisease();
  81. // 疾病信息
  82. Disease disea = diseaseDao.findByCode(dis);
  83. // 患者
  84. patientDisease.setPatient(patient);
  85. // 疾病
  86. patientDisease.setDisease(dis);
  87. patientDisease.setDel("1");
  88. patientDisease.setSignType("2");
  89. if (disea != null) {
  90. // 疾病名字
  91. patientDisease.setDiseaseName(disea.getName());
  92. }
  93. // 更新日期
  94. patientDisease.setCzrq(new Date());
  95. patientDiseases.add(patientDisease);
  96. }
  97. }
  98. List<String> diseaseList = Arrays.asList(diseases);
  99. // 重新分组
  100. if (!changeGroupInfo(patient, diseaseList)) {
  101. throw new Exception("set group falied");
  102. }
  103. if (patientDiseases.size() > 0) {
  104. Iterable<PatientDisease> savedPatientDiseases = patientDiseaseDao.save(patientDiseases);
  105. JSONArray redisValues = new JSONArray();
  106. for (PatientDisease patientDisease : savedPatientDiseases) {
  107. JSONObject redisValue = new JSONObject();
  108. redisValue.put("disease", patientDisease.getDisease());
  109. redisValue.put("diseaseName", patientDisease.getDiseaseName());
  110. redisValue.put("del", patientDisease.getDel());
  111. redisValue.put("signType", patientDisease.getSignType());
  112. redisValues.put(redisValue);
  113. }
  114. List<PatientDisease> ssDisease = patientDiseaseDao.findByPatientSsDisease(patient);
  115. if (ssDisease != null && ssDisease.size() > 0) {
  116. for (PatientDisease pd : ssDisease) {
  117. JSONObject redisValue = new JSONObject();
  118. redisValue.put("disease", pd.getDisease());
  119. redisValue.put("diseaseName", pd.getDiseaseName());
  120. redisValue.put("del", pd.getDel());
  121. redisValue.put("signType", pd.getSignType());
  122. redisValues.put(redisValue);
  123. }
  124. }
  125. // redis缓存患者疾病
  126. redisTemplate.opsForValue().set("disease:" + patient, redisValues.toString());
  127. }
  128. } else {
  129. JSONArray redisValues = new JSONArray();
  130. List<PatientDisease> ssDisease = patientDiseaseDao.findByPatientSsDisease(patient);
  131. List<String> disList = new ArrayList<>();
  132. if (ssDisease != null && ssDisease.size() > 0) {
  133. for (PatientDisease pd : ssDisease) {
  134. JSONObject redisValue = new JSONObject();
  135. redisValue.put("disease", pd.getDisease());
  136. redisValue.put("diseaseName", pd.getDiseaseName());
  137. redisValue.put("del", pd.getDel());
  138. redisValue.put("signType", pd.getSignType());
  139. redisValues.put(redisValue);
  140. disList.add(pd.getDisease());
  141. }
  142. }
  143. redisTemplate.opsForValue().set("disease:" + patient, redisValues.toString());
  144. // 重新分组
  145. if (!changeGroupInfo(patient, disList)) {
  146. throw new Exception("set group falied");
  147. }
  148. }
  149. return true;
  150. } catch (Exception e) {
  151. e.printStackTrace();
  152. return false;
  153. }
  154. }
  155. /**
  156. * 病人分组
  157. *
  158. * @param patient
  159. * @param diseases
  160. * @return
  161. * @throws Exception
  162. */
  163. public boolean changeGroupInfo(String patient, List<String> diseases) throws Exception {
  164. SignFamily jjSign = signFamilyDao.findSignByPatient(patient, 2);
  165. SignFamily ssSign = signFamilyDao.findSignByPatient(patient, 1);
  166. Patient p = patientDao.findByCode(patient);
  167. if (jjSign == null && ssSign == null) {
  168. throw new Exception("can not find patient's sign info");
  169. }
  170. if (patient == null) {
  171. throw new Exception("can not find patient's info");
  172. }
  173. if (diseases.contains("1") || diseases.contains("2")) {
  174. // 慢病分组
  175. if (jjSign != null) {
  176. if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctor(), patient, "2", "2") < 1) {
  177. DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
  178. ncdGroup.setDoctor(jjSign.getDoctor());
  179. ncdGroup.setPatient(patient);
  180. ncdGroup.setSignType("2");
  181. ncdGroup.setCzrq(new Date());
  182. ncdGroup.setStatus(1);
  183. ncdGroup.setPname(jjSign.getName());
  184. ncdGroup.setQyrq(jjSign.getApplyDate());
  185. ncdGroup.setDqrq(jjSign.getEnd());
  186. ncdGroup.setGroup("2");
  187. groupInfoDao.save(ncdGroup);
  188. }
  189. if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctorHealth(), patient, "2", "2") < 1) {
  190. DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
  191. ncdJgGroup.setDoctor(jjSign.getDoctorHealth());
  192. ncdJgGroup.setPatient(patient);
  193. ncdJgGroup.setSignType("2");
  194. ncdJgGroup.setCzrq(new Date());
  195. ncdJgGroup.setStatus(1);
  196. ncdJgGroup.setPname(jjSign.getName());
  197. ncdJgGroup.setQyrq(jjSign.getApplyDate());
  198. ncdJgGroup.setDqrq(jjSign.getEnd());
  199. ncdJgGroup.setGroup("2");
  200. groupInfoDao.save(ncdJgGroup);
  201. }
  202. }
  203. if (ssSign != null) {
  204. if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctor(), patient, "2", "1") < 1) {
  205. DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
  206. ncdGroup.setDoctor(ssSign.getDoctor());
  207. ncdGroup.setPatient(patient);
  208. ncdGroup.setSignType("1");
  209. ncdGroup.setCzrq(new Date());
  210. ncdGroup.setStatus(1);
  211. ncdGroup.setPname(ssSign.getName());
  212. ncdGroup.setQyrq(ssSign.getApplyDate());
  213. ncdGroup.setDqrq(ssSign.getEnd());
  214. ncdGroup.setGroup("2");
  215. groupInfoDao.save(ncdGroup);
  216. }
  217. if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctorHealth(), patient, "2", "1") < 1) {
  218. DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
  219. ncdJgGroup.setDoctor(ssSign.getDoctorHealth());
  220. ncdJgGroup.setPatient(patient);
  221. ncdJgGroup.setSignType("1");
  222. ncdJgGroup.setCzrq(new Date());
  223. ncdJgGroup.setStatus(1);
  224. ncdJgGroup.setPname(ssSign.getName());
  225. ncdJgGroup.setQyrq(ssSign.getApplyDate());
  226. ncdJgGroup.setDqrq(ssSign.getEnd());
  227. ncdJgGroup.setGroup("2");
  228. groupInfoDao.save(ncdJgGroup);
  229. }
  230. }
  231. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "1", 0);
  232. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "3", 0);
  233. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "1", 0);
  234. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "3", 0);
  235. } else {
  236. boolean isSixFive = false;
  237. String birth = p.getIdcard().substring(6, 14);
  238. int year = Integer.valueOf(birth.substring(0, 4));
  239. int month = Integer.valueOf(birth.substring(4, 6));
  240. int day = Integer.valueOf(birth.substring(6));
  241. Calendar cal = Calendar.getInstance();
  242. int age = cal.get(Calendar.YEAR) - year;
  243. //周岁计算
  244. if (cal.get(Calendar.MONTH) > (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) > day)) {
  245. age--;
  246. }
  247. if (age >= 65) {
  248. isSixFive = true;
  249. }
  250. if (isSixFive) {
  251. // 65岁以上分组
  252. if (jjSign != null) {
  253. if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctor(), patient, "3", "2") < 1) {
  254. DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
  255. ncdGroup.setDoctor(jjSign.getDoctor());
  256. ncdGroup.setPatient(patient);
  257. ncdGroup.setSignType("2");
  258. ncdGroup.setCzrq(new Date());
  259. ncdGroup.setStatus(1);
  260. ncdGroup.setPname(jjSign.getName());
  261. ncdGroup.setQyrq(jjSign.getApplyDate());
  262. ncdGroup.setDqrq(jjSign.getEnd());
  263. ncdGroup.setGroup("3");
  264. groupInfoDao.save(ncdGroup);
  265. }
  266. if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctorHealth(), patient, "3", "2") < 1) {
  267. DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
  268. ncdJgGroup.setDoctor(jjSign.getDoctorHealth());
  269. ncdJgGroup.setPatient(patient);
  270. ncdJgGroup.setSignType("2");
  271. ncdJgGroup.setCzrq(new Date());
  272. ncdJgGroup.setStatus(1);
  273. ncdJgGroup.setPname(jjSign.getName());
  274. ncdJgGroup.setQyrq(jjSign.getApplyDate());
  275. ncdJgGroup.setDqrq(jjSign.getEnd());
  276. ncdJgGroup.setGroup("3");
  277. groupInfoDao.save(ncdJgGroup);
  278. }
  279. }
  280. if (ssSign != null) {
  281. if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctor(), patient, "3", "1") < 1) {
  282. DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
  283. ncdGroup.setDoctor(ssSign.getDoctor());
  284. ncdGroup.setPatient(patient);
  285. ncdGroup.setSignType("1");
  286. ncdGroup.setCzrq(new Date());
  287. ncdGroup.setStatus(1);
  288. ncdGroup.setPname(ssSign.getName());
  289. ncdGroup.setQyrq(ssSign.getApplyDate());
  290. ncdGroup.setDqrq(ssSign.getEnd());
  291. ncdGroup.setGroup("3");
  292. groupInfoDao.save(ncdGroup);
  293. }
  294. if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctorHealth(), patient, "3", "1") < 1) {
  295. DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
  296. ncdJgGroup.setDoctor(ssSign.getDoctorHealth());
  297. ncdJgGroup.setPatient(patient);
  298. ncdJgGroup.setSignType("1");
  299. ncdJgGroup.setCzrq(new Date());
  300. ncdJgGroup.setStatus(1);
  301. ncdJgGroup.setPname(ssSign.getName());
  302. ncdJgGroup.setQyrq(ssSign.getApplyDate());
  303. ncdJgGroup.setDqrq(ssSign.getEnd());
  304. ncdJgGroup.setGroup("3");
  305. groupInfoDao.save(ncdJgGroup);
  306. }
  307. }
  308. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "1", 0);
  309. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "2", 0);
  310. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "1", 0);
  311. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "2", 0);
  312. } else {
  313. // 普通分组
  314. if (jjSign != null) {
  315. if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctor(), patient, "1", "2") < 1) {
  316. DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
  317. ncdGroup.setDoctor(jjSign.getDoctor());
  318. ncdGroup.setPatient(patient);
  319. ncdGroup.setSignType("2");
  320. ncdGroup.setCzrq(new Date());
  321. ncdGroup.setStatus(1);
  322. ncdGroup.setPname(jjSign.getName());
  323. ncdGroup.setQyrq(jjSign.getApplyDate());
  324. ncdGroup.setDqrq(jjSign.getEnd());
  325. ncdGroup.setGroup("1");
  326. groupInfoDao.save(ncdGroup);
  327. }
  328. if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctorHealth(), patient, "1", "2") < 1) {
  329. DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
  330. ncdJgGroup.setDoctor(jjSign.getDoctorHealth());
  331. ncdJgGroup.setPatient(patient);
  332. ncdJgGroup.setSignType("2");
  333. ncdJgGroup.setCzrq(new Date());
  334. ncdJgGroup.setStatus(1);
  335. ncdJgGroup.setPname(jjSign.getName());
  336. ncdJgGroup.setQyrq(jjSign.getApplyDate());
  337. ncdJgGroup.setDqrq(jjSign.getEnd());
  338. ncdJgGroup.setGroup("1");
  339. groupInfoDao.save(ncdJgGroup);
  340. }
  341. }
  342. if (ssSign != null) {
  343. if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctor(), patient, "1", "1") < 1) {
  344. DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
  345. ncdGroup.setDoctor(ssSign.getDoctor());
  346. ncdGroup.setPatient(patient);
  347. ncdGroup.setSignType("1");
  348. ncdGroup.setCzrq(new Date());
  349. ncdGroup.setStatus(1);
  350. ncdGroup.setPname(ssSign.getName());
  351. ncdGroup.setQyrq(ssSign.getApplyDate());
  352. ncdGroup.setDqrq(ssSign.getEnd());
  353. ncdGroup.setGroup("1");
  354. groupInfoDao.save(ncdGroup);
  355. }
  356. if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctorHealth(), patient, "1", "1") < 1) {
  357. DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
  358. ncdJgGroup.setDoctor(ssSign.getDoctorHealth());
  359. ncdJgGroup.setPatient(patient);
  360. ncdJgGroup.setSignType("1");
  361. ncdJgGroup.setCzrq(new Date());
  362. ncdJgGroup.setStatus(1);
  363. ncdJgGroup.setPname(ssSign.getName());
  364. ncdJgGroup.setQyrq(ssSign.getApplyDate());
  365. ncdJgGroup.setDqrq(ssSign.getEnd());
  366. ncdJgGroup.setGroup("1");
  367. groupInfoDao.save(ncdJgGroup);
  368. }
  369. }
  370. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "2", 0);
  371. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "3", 0);
  372. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "2", 0);
  373. groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "3", 0);
  374. }
  375. }
  376. return true;
  377. }
  378. /**
  379. * 更新患者疾病到redis
  380. */
  381. public void updateToRedis() {
  382. String sql = "select * from wlyy_patient_disease";
  383. List<Map<String, Object>> patientDiseases = jdbcTemplate.queryForList(sql);
  384. Map<String, JSONArray> diseases = new HashMap<>();
  385. if (patientDiseases != null) {
  386. for (Map<String, Object> map : patientDiseases) {
  387. if(!diseases.containsKey(map.get("patient").toString())){
  388. diseases.put(map.get("patient").toString(), new JSONArray());
  389. }
  390. if(map.get("del").toString().equals("1")) {
  391. JSONObject disease = new JSONObject();
  392. disease.put("disease", map.get("disease"));
  393. disease.put("diseaseName", map.get("disease_name"));
  394. disease.put("del", map.get("del"));
  395. disease.put("signType", map.get("sign_type"));
  396. if (diseases.containsKey(map.get("patient").toString())) {
  397. diseases.get(map.get("patient").toString()).put(disease);
  398. } else {
  399. JSONArray jsonArray = new JSONArray();
  400. jsonArray.put(disease);
  401. diseases.put(map.get("patient").toString(), jsonArray);
  402. }
  403. }
  404. }
  405. }
  406. for (String key : diseases.keySet()) {
  407. redisTemplate.opsForValue().set("disease:" + key, diseases.get(key).toString());
  408. }
  409. }
  410. /**
  411. * 更新疾病到疾病表
  412. */
  413. public void updateToDisease() {
  414. String gxySql = "insert into wlyy_patient_disease(patient,disease,disease_name,del,sign_type,czrq) select a.code,'1','高血压','1','?1',now() from wlyy_patient a,wlyy_sign_family b where a.code = b.patient and b.type = ?2 and b.status > 0 and a.disease = 1";
  415. String tnbSql = "insert into wlyy_patient_disease(patient,disease,disease_name,del,sign_type,czrq) select a.code,'2','糖尿病','1','?1',now() from wlyy_patient a,wlyy_sign_family b where a.code = b.patient and b.type = ?2 and b.status > 0 and a.disease = 2";
  416. jdbcTemplate.update(gxySql.replace("?1", "1").replace("?2", "1"));
  417. jdbcTemplate.update(gxySql.replace("?1", "2").replace("?2", "2"));
  418. jdbcTemplate.update(tnbSql.replace("?1", "1").replace("?2", "1"));
  419. jdbcTemplate.update(tnbSql.replace("?1", "2").replace("?2", "2"));
  420. String tgSql = "select code from wlyy_patient where disease = 3 ";
  421. List<Map<String, Object>> codes = jdbcTemplate.queryForList(tgSql);
  422. for (Map<String, Object> map : codes) {
  423. String sql = "insert into wlyy_patient_disease(patient,disease,disease_name,del,sign_type,czrq) values('" +
  424. map.get("code").toString() + "','?1','?2','1','?3',now())";
  425. if (signFamilyDao.countPatientSsSign(map.get("code").toString()) > 0) {
  426. jdbcTemplate.update(sql.replace("?1", "1").replace("?2", "高血压").replace("?3", "1"));
  427. jdbcTemplate.update(sql.replace("?1", "2").replace("?2", "糖尿病").replace("?3", "1"));
  428. }
  429. if (signFamilyDao.countPatientJtSign(map.get("code").toString()) > 0) {
  430. jdbcTemplate.update(sql.replace("?1", "1").replace("?2", "高血压").replace("?3", "2"));
  431. jdbcTemplate.update(sql.replace("?1", "2").replace("?2", "糖尿病").replace("?3", "2"));
  432. }
  433. }
  434. }
  435. }