PatientDiseaseService.java 22 KB

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