123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- package com.yihu.wlyy.service.app.disease;
- import com.yihu.wlyy.entity.dict.Disease;
- import com.yihu.wlyy.entity.patient.SignFamily;
- import com.yihu.wlyy.entity.doctor.team.sign.DoctorPatientGroupInfo;
- import com.yihu.wlyy.entity.patient.Patient;
- import com.yihu.wlyy.entity.patient.PatientDisease;
- import com.yihu.wlyy.repository.dict.DiseaseDao;
- import com.yihu.wlyy.repository.doctor.DoctorPatientGroupInfoDao;
- import com.yihu.wlyy.repository.patient.PatientDao;
- import com.yihu.wlyy.repository.patient.PatientDiseaseDao;
- import com.yihu.wlyy.repository.patient.SignFamilyDao;
- import com.yihu.wlyy.service.BaseService;
- import org.apache.commons.lang3.StringUtils;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Service;
- import javax.transaction.Transactional;
- import java.util.*;
- /**
- * 患者疾病服务
- * <p>
- * Created by lyr on 2016/09/09.
- */
- @Service
- @Transactional
- public class PatientDiseaseService extends BaseService {
- @Autowired
- DiseaseDao diseaseDao;
- @Autowired
- PatientDiseaseDao patientDiseaseDao;
- @Autowired
- DoctorPatientGroupInfoDao groupInfoDao;
- @Autowired
- StringRedisTemplate redisTemplate;
- @Autowired
- SignFamilyDao signFamilyDao;
- @Autowired
- PatientDao patientDao;
- @Autowired
- JdbcTemplate jdbcTemplate;
- /**
- * 根据居民code查询疾病
- *
- * @param patient 居民code
- * @return
- */
- public List<PatientDisease> getPatientDisease(String patient) {
- return patientDiseaseDao.findByPatient(patient);
- }
- /**
- * 根据居民code查询疾病
- *
- * @param patient 居民code
- * @return
- */
- public List<PatientDisease> getPatientDiseaseSs(String patient) {
- return patientDiseaseDao.findByPatientAndSignType(patient,"1");
- }
- /**
- * 更新患者疾病表
- *
- * @param patient 患者
- * @param disease 疾病
- * @return
- */
- public boolean updatePatientDisease(String patient, String disease) {
- try {
- // 删除患者家庭签约疾病
- patientDiseaseDao.updateDiseaseDel(patient);
- if (!StringUtils.isEmpty(disease) && !disease.equals("0")) {
- String[] diseases = disease.split(",");
- List<PatientDisease> patientDiseases = new ArrayList<>();
- for (String dis : diseases) {
- if (StringUtils.isEmpty(dis)) {
- continue;
- } else {
- PatientDisease patientDisease = new PatientDisease();
- // 疾病信息
- Disease disea = diseaseDao.findByCode(dis);
- // 患者
- patientDisease.setPatient(patient);
- // 疾病
- patientDisease.setDisease(dis);
- patientDisease.setDel("1");
- patientDisease.setSignType("2");
- if (disea != null) {
- // 疾病名字
- patientDisease.setDiseaseName(disea.getName());
- }
- // 更新日期
- patientDisease.setCzrq(new Date());
- patientDiseases.add(patientDisease);
- }
- }
- List<String> diseaseList = Arrays.asList(diseases);
- // 重新分组
- if (!changeGroupInfo(patient, diseaseList)) {
- throw new Exception("set group falied");
- }
- if (patientDiseases.size() > 0) {
- Iterable<PatientDisease> savedPatientDiseases = patientDiseaseDao.save(patientDiseases);
- JSONArray redisValues = new JSONArray();
- for (PatientDisease patientDisease : savedPatientDiseases) {
- JSONObject redisValue = new JSONObject();
- redisValue.put("disease", patientDisease.getDisease());
- redisValue.put("diseaseName", patientDisease.getDiseaseName());
- redisValue.put("del", patientDisease.getDel());
- redisValue.put("signType", patientDisease.getSignType());
- redisValues.put(redisValue);
- }
- List<PatientDisease> ssDisease = patientDiseaseDao.findByPatientSsDisease(patient);
- if (ssDisease != null && ssDisease.size() > 0) {
- for (PatientDisease pd : ssDisease) {
- JSONObject redisValue = new JSONObject();
- redisValue.put("disease", pd.getDisease());
- redisValue.put("diseaseName", pd.getDiseaseName());
- redisValue.put("del", pd.getDel());
- redisValue.put("signType", pd.getSignType());
- redisValues.put(redisValue);
- }
- }
- // redis缓存患者疾病
- redisTemplate.opsForValue().set("disease:" + patient, redisValues.toString());
- }
- } else {
- JSONArray redisValues = new JSONArray();
- // 重新分组
- if (!changeGroupInfo(patient, new ArrayList<String>())) {
- throw new Exception("set group falied");
- }
- List<PatientDisease> ssDisease = patientDiseaseDao.findByPatientSsDisease(patient);
- if (ssDisease != null && ssDisease.size() > 0) {
- for (PatientDisease pd : ssDisease) {
- JSONObject redisValue = new JSONObject();
- redisValue.put("disease", pd.getDisease());
- redisValue.put("diseaseName", pd.getDiseaseName());
- redisValue.put("del", pd.getDel());
- redisValue.put("signType", pd.getSignType());
- redisValues.put(redisValue);
- }
- }
- redisTemplate.opsForValue().set("disease:" + patient, redisValues.toString());
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 病人分组
- *
- * @param patient
- * @param diseases
- * @return
- * @throws Exception
- */
- public boolean changeGroupInfo(String patient, List<String> diseases) throws Exception {
- SignFamily jjSign = signFamilyDao.findSignByPatient(patient, 2);
- SignFamily ssSign = signFamilyDao.findSignByPatient(patient, 1);
- Patient p = patientDao.findByCode(patient);
- if (jjSign == null && ssSign == null) {
- throw new Exception("can not find patient's sign info");
- }
- if (patient == null) {
- throw new Exception("can not find patient's info");
- }
- if (diseases.contains("1") || diseases.contains("2")) {
- // 慢病分组
- if (jjSign != null) {
- if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctor(), patient, "2", "2") < 1) {
- DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
- ncdGroup.setDoctor(jjSign.getDoctor());
- ncdGroup.setPatient(patient);
- ncdGroup.setSignType("2");
- ncdGroup.setCzrq(new Date());
- ncdGroup.setStatus(1);
- ncdGroup.setPname(jjSign.getName());
- ncdGroup.setQyrq(jjSign.getApplyDate());
- ncdGroup.setDqrq(jjSign.getEnd());
- ncdGroup.setGroup("2");
- groupInfoDao.save(ncdGroup);
- }
- if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctorHealth(), patient, "2", "2") < 1) {
- DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
- ncdJgGroup.setDoctor(jjSign.getDoctorHealth());
- ncdJgGroup.setPatient(patient);
- ncdJgGroup.setSignType("2");
- ncdJgGroup.setCzrq(new Date());
- ncdJgGroup.setStatus(1);
- ncdJgGroup.setPname(jjSign.getName());
- ncdJgGroup.setQyrq(jjSign.getApplyDate());
- ncdJgGroup.setDqrq(jjSign.getEnd());
- ncdJgGroup.setGroup("2");
- groupInfoDao.save(ncdJgGroup);
- }
- }
- if (ssSign != null) {
- if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctor(), patient, "2", "1") < 1) {
- DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
- ncdGroup.setDoctor(ssSign.getDoctor());
- ncdGroup.setPatient(patient);
- ncdGroup.setSignType("1");
- ncdGroup.setCzrq(new Date());
- ncdGroup.setStatus(1);
- ncdGroup.setPname(ssSign.getName());
- ncdGroup.setQyrq(ssSign.getApplyDate());
- ncdGroup.setDqrq(ssSign.getEnd());
- ncdGroup.setGroup("2");
- groupInfoDao.save(ncdGroup);
- }
- if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctorHealth(), patient, "2", "1") < 1) {
- DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
- ncdJgGroup.setDoctor(ssSign.getDoctorHealth());
- ncdJgGroup.setPatient(patient);
- ncdJgGroup.setSignType("1");
- ncdJgGroup.setCzrq(new Date());
- ncdJgGroup.setStatus(1);
- ncdJgGroup.setPname(ssSign.getName());
- ncdJgGroup.setQyrq(ssSign.getApplyDate());
- ncdJgGroup.setDqrq(ssSign.getEnd());
- ncdJgGroup.setGroup("2");
- groupInfoDao.save(ncdJgGroup);
- }
- }
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "1", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "3", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "1", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "3", 0);
- } else {
- boolean isSixFive = false;
- String birth = p.getIdcard().substring(6, 14);
- int year = Integer.valueOf(birth.substring(0, 4));
- int month = Integer.valueOf(birth.substring(4, 6));
- int day = Integer.valueOf(birth.substring(6));
- Calendar cal = Calendar.getInstance();
- int age = cal.get(Calendar.YEAR) - year;
- //周岁计算
- if (cal.get(Calendar.MONTH) > (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) > day)) {
- age--;
- }
- if (age >= 65) {
- isSixFive = true;
- }
- if (isSixFive) {
- // 65岁以上分组
- if (jjSign != null) {
- if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctor(), patient, "3", "2") < 1) {
- DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
- ncdGroup.setDoctor(jjSign.getDoctor());
- ncdGroup.setPatient(patient);
- ncdGroup.setSignType("2");
- ncdGroup.setCzrq(new Date());
- ncdGroup.setStatus(1);
- ncdGroup.setPname(jjSign.getName());
- ncdGroup.setQyrq(jjSign.getApplyDate());
- ncdGroup.setDqrq(jjSign.getEnd());
- ncdGroup.setGroup("3");
- groupInfoDao.save(ncdGroup);
- }
- if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctorHealth(), patient, "3", "2") < 1) {
- DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
- ncdJgGroup.setDoctor(jjSign.getDoctorHealth());
- ncdJgGroup.setPatient(patient);
- ncdJgGroup.setSignType("2");
- ncdJgGroup.setCzrq(new Date());
- ncdJgGroup.setStatus(1);
- ncdJgGroup.setPname(jjSign.getName());
- ncdJgGroup.setQyrq(jjSign.getApplyDate());
- ncdJgGroup.setDqrq(jjSign.getEnd());
- ncdJgGroup.setGroup("3");
- groupInfoDao.save(ncdJgGroup);
- }
- }
- if (ssSign != null) {
- if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctor(), patient, "3", "1") < 1) {
- DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
- ncdGroup.setDoctor(ssSign.getDoctor());
- ncdGroup.setPatient(patient);
- ncdGroup.setSignType("1");
- ncdGroup.setCzrq(new Date());
- ncdGroup.setStatus(1);
- ncdGroup.setPname(ssSign.getName());
- ncdGroup.setQyrq(ssSign.getApplyDate());
- ncdGroup.setDqrq(ssSign.getEnd());
- ncdGroup.setGroup("3");
- groupInfoDao.save(ncdGroup);
- }
- if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctorHealth(), patient, "3", "1") < 1) {
- DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
- ncdJgGroup.setDoctor(ssSign.getDoctorHealth());
- ncdJgGroup.setPatient(patient);
- ncdJgGroup.setSignType("1");
- ncdJgGroup.setCzrq(new Date());
- ncdJgGroup.setStatus(1);
- ncdJgGroup.setPname(ssSign.getName());
- ncdJgGroup.setQyrq(ssSign.getApplyDate());
- ncdJgGroup.setDqrq(ssSign.getEnd());
- ncdJgGroup.setGroup("3");
- groupInfoDao.save(ncdJgGroup);
- }
- }
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "1", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "2", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "1", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "2", 0);
- } else {
- // 普通分组
- if (jjSign != null) {
- if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctor(), patient, "1", "2") < 1) {
- DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
- ncdGroup.setDoctor(jjSign.getDoctor());
- ncdGroup.setPatient(patient);
- ncdGroup.setSignType("2");
- ncdGroup.setCzrq(new Date());
- ncdGroup.setStatus(1);
- ncdGroup.setPname(jjSign.getName());
- ncdGroup.setQyrq(jjSign.getApplyDate());
- ncdGroup.setDqrq(jjSign.getEnd());
- ncdGroup.setGroup("1");
- groupInfoDao.save(ncdGroup);
- }
- if (groupInfoDao.countDoctorPatientGroupType(jjSign.getDoctorHealth(), patient, "1", "2") < 1) {
- DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
- ncdJgGroup.setDoctor(jjSign.getDoctorHealth());
- ncdJgGroup.setPatient(patient);
- ncdJgGroup.setSignType("2");
- ncdJgGroup.setCzrq(new Date());
- ncdJgGroup.setStatus(1);
- ncdJgGroup.setPname(jjSign.getName());
- ncdJgGroup.setQyrq(jjSign.getApplyDate());
- ncdJgGroup.setDqrq(jjSign.getEnd());
- ncdJgGroup.setGroup("1");
- groupInfoDao.save(ncdJgGroup);
- }
- }
- if (ssSign != null) {
- if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctor(), patient, "1", "1") < 1) {
- DoctorPatientGroupInfo ncdGroup = new DoctorPatientGroupInfo();
- ncdGroup.setDoctor(ssSign.getDoctor());
- ncdGroup.setPatient(patient);
- ncdGroup.setSignType("1");
- ncdGroup.setCzrq(new Date());
- ncdGroup.setStatus(1);
- ncdGroup.setPname(ssSign.getName());
- ncdGroup.setQyrq(ssSign.getApplyDate());
- ncdGroup.setDqrq(ssSign.getEnd());
- ncdGroup.setGroup("1");
- groupInfoDao.save(ncdGroup);
- }
- if (groupInfoDao.countDoctorPatientGroupType(ssSign.getDoctorHealth(), patient, "1", "1") < 1) {
- DoctorPatientGroupInfo ncdJgGroup = new DoctorPatientGroupInfo();
- ncdJgGroup.setDoctor(ssSign.getDoctorHealth());
- ncdJgGroup.setPatient(patient);
- ncdJgGroup.setSignType("1");
- ncdJgGroup.setCzrq(new Date());
- ncdJgGroup.setStatus(1);
- ncdJgGroup.setPname(ssSign.getName());
- ncdJgGroup.setQyrq(ssSign.getApplyDate());
- ncdJgGroup.setDqrq(ssSign.getEnd());
- ncdJgGroup.setGroup("1");
- groupInfoDao.save(ncdJgGroup);
- }
- }
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "2", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctor() : ssSign.getDoctor(), patient, "3", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "2", 0);
- groupInfoDao.updateDoctorPatientGroup(jjSign != null ? jjSign.getDoctorHealth() : ssSign.getDoctorHealth(), patient, "3", 0);
- }
- }
- return true;
- }
- /**
- * 更新患者疾病到redis
- */
- public void updateToRedis() {
- String sql = "select * from wlyy_patient_disease";
- List<Map<String, Object>> patientDiseases = jdbcTemplate.queryForList(sql);
- Map<String, JSONArray> diseases = new HashMap<>();
- if (patientDiseases != null) {
- for (Map<String, Object> map : patientDiseases) {
- if(!diseases.containsKey(map.get("patient").toString())){
- diseases.put(map.get("patient").toString(), new JSONArray());
- }
- if(map.get("del").toString().equals("1")) {
- JSONObject disease = new JSONObject();
- disease.put("disease", map.get("disease"));
- disease.put("diseaseName", map.get("disease_name"));
- disease.put("del", map.get("del"));
- disease.put("signType", map.get("sign_type"));
- if (diseases.containsKey(map.get("patient").toString())) {
- diseases.get(map.get("patient").toString()).put(disease);
- } else {
- JSONArray jsonArray = new JSONArray();
- jsonArray.put(disease);
- diseases.put(map.get("patient").toString(), jsonArray);
- }
- }
- }
- }
- for (String key : diseases.keySet()) {
- redisTemplate.opsForValue().set("disease:" + key, diseases.get(key).toString());
- }
- }
- /**
- * 更新疾病到疾病表
- */
- public void updateToDisease() {
- 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";
- 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";
- jdbcTemplate.update(gxySql.replace("?1", "1").replace("?2", "1"));
- jdbcTemplate.update(gxySql.replace("?1", "2").replace("?2", "2"));
- jdbcTemplate.update(tnbSql.replace("?1", "1").replace("?2", "1"));
- jdbcTemplate.update(tnbSql.replace("?1", "2").replace("?2", "2"));
- String tgSql = "select code from wlyy_patient where disease = 3 ";
- List<Map<String, Object>> codes = jdbcTemplate.queryForList(tgSql);
- for (Map<String, Object> map : codes) {
- String sql = "insert into wlyy_patient_disease(patient,disease,disease_name,del,sign_type,czrq) values('" +
- map.get("code").toString() + "','?1','?2','1','?3',now())";
- if (signFamilyDao.countPatientSsSign(map.get("code").toString()) > 0) {
- jdbcTemplate.update(sql.replace("?1", "1").replace("?2", "高血压").replace("?3", "1"));
- jdbcTemplate.update(sql.replace("?1", "2").replace("?2", "糖尿病").replace("?3", "1"));
- }
- if (signFamilyDao.countPatientJtSign(map.get("code").toString()) > 0) {
- jdbcTemplate.update(sql.replace("?1", "1").replace("?2", "高血压").replace("?3", "2"));
- jdbcTemplate.update(sql.replace("?1", "2").replace("?2", "糖尿病").replace("?3", "2"));
- }
- }
- }
- }
|