PatientDiseaseService.java 22 KB

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