package com.yihu.wlyy.service.app.label; import com.yihu.wlyy.entity.doctor.profile.Doctor; import com.yihu.wlyy.entity.doctor.team.sign.DoctorPatientGroupInfo; import com.yihu.wlyy.entity.doctor.team.sign.SignPatientLabel; import com.yihu.wlyy.entity.doctor.team.sign.SignPatientLabelInfo; import com.yihu.wlyy.entity.patient.Patient; import com.yihu.wlyy.entity.patient.PatientDisease; import com.yihu.wlyy.entity.patient.SignFamily; import com.yihu.wlyy.repository.doctor.DoctorDao; import com.yihu.wlyy.repository.doctor.DoctorPatientGroupInfoDao; import com.yihu.wlyy.repository.doctor.SignPatientLabelInfoDao; 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 com.yihu.wlyy.util.DateUtil; import com.yihu.wlyy.util.IdCardUtil; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; /** * 患者标签信息服务 *

* Created by lyr on 2016/10/9. */ @Service @Transactional public class SignPatientLabelInfoService extends BaseService { @Autowired SignPatientLabelService labelService; @Autowired DoctorDao doctorDao; @Autowired PatientDao patientDao; @Autowired SignPatientLabelInfoDao labelInfoDao; @Autowired PatientDiseaseDao diseaseDao; @Autowired DoctorPatientGroupInfoDao groupInfoDao; @Autowired JdbcTemplate jdbcTemplate; @Autowired SignFamilyDao signFamilyDao; /** * 根据标签查询患者信息 * * @param labelCode 标签code * @param labelType 标签类型 * @param page 第几页 * @param pagesize 页大小 * @return */ public JSONArray getPatientByLabel(String doctor, String labelCode, String labelType, Long teamCode, int page, int pagesize) throws Exception { Doctor doc = doctorDao.findByCode(doctor); if (doc == null) { throw new Exception("doctor info can not find"); } Map result = new HashMap<>(); List> signList = new ArrayList<>(); int start = page * pagesize; String sql = ""; Object[] args = null; if (labelCode.equals("0")) { sql = "select a.*" + " from" + " (select * from wlyy_sign_family where " + (doc.getLevel() == 2 ? " doctor" : "doctor_health") + " = ? and status > 0 and admin_team_code = ?) a" + " left join" + " (select * from wlyy_sign_patient_label_info where label_type = ? and status = 1) b" + " on a.patient = b.patient where b.patient is null limit " + start + "," + pagesize; args = new Object[]{doctor, teamCode, labelType}; } else { sql = "select a.*" + " from" + " (select * from wlyy_sign_family where " + (doc.getLevel() == 2 ? " doctor" : "doctor_health") + " = ? and status > 0 and admin_team_code = ?) a" + " join" + " (select * from wlyy_sign_patient_label_info where label = ? and label_type = ? and status = 1) b" + " on a.patient = b.patient limit " + start + "," + pagesize; args = new Object[]{doctor, teamCode, labelCode, labelType}; } signList = jdbcTemplate.queryForList(sql, args); if (signList != null && signList.size() > 0) { for (Map sign : signList) { Patient p = patientDao.findByCode(sign.get("patient") == null ? "" : sign.get("patient").toString()); if (p == null) { continue; } if (result.containsKey(p.getCode())) { JSONObject jsonP = result.get(p.getCode()); if (!String.valueOf(jsonP.get("signType")).equals(String.valueOf(sign.get("type")))) { jsonP.put("signType", 3); } continue; } //List labels = labelInfoDao.findByPatientAndStatus(sign.get("patient").toString(), 1); JSONObject json = new JSONObject(); // 设置患者标识 json.put("code", p.getCode()); // 设置患者姓名 json.put("name", p.getName()); // 设置患者头像 json.put("photo", p.getPhoto()); // 设置患者年龄 json.put("age", DateUtil.getAgeByBirthday(p.getBirthday())); // 设置患者性别 json.put("sex", p.getSex()); // 设置签约日期 json.put("qyrq", sign.get("apply_date") != null ? DateUtil.dateToStr((Date) sign.get("apply_date"), DateUtil.YYYY_MM_DD) : ""); // 设置签约类型 json.put("signType", sign.get("type") == null ? "" : sign.get("type")); // 身份证号 json.put("idcard", p.getIdcard()); // 患者标签 //json.put("labels", labels == null ? "" : labels); result.put(p.getCode(), json); } } return result.size() > 0 ? new JSONArray(result.values()) : new JSONArray(); } /** * 查询某个标签类型的所有标签居民数统计 * * @param labelType 标签类型 * @param teamCode 标签类型为4时,不能为空 * @return */ public JSONArray getPatientAmountByLabelType(String doctor, String labelType, Long teamCode) throws Exception { Doctor doc = doctorDao.findByCode(doctor); if (doc == null) { throw new Exception("doctor info can not find"); } List labels = labelService.getLabelsByTypeAndTeam(labelType, teamCode); JSONArray result = new JSONArray(); SignPatientLabel labelNo = new SignPatientLabel(); labelNo.setLabelCode("0"); labelNo.setLabelName("未分组"); labelNo.setStatus(1); labelNo.setIsSystem(1); labelNo.setLabelType(labelType); labelNo.setSort(999999999); labels.add(labelNo); if (labels != null) { for (SignPatientLabel label : labels) { JSONObject json = new JSONObject(); json.put("labelCode", label.getLabelCode()); json.put("labelName", label.getLabelName()); json.put("isSystem", label.getIsSystem()); int amount = 0; String sql = ""; Object[] args = null; if (label.getLabelCode().equals("0")) { sql = "select count(DISTINCT a.patient) count" + " from" + " (select * from wlyy_sign_family where " + (doc.getLevel() == 2 ? " doctor" : "doctor_health") + " = ? and status > 0 " + (teamCode > 0 ? "and admin_team_code = ?" : "") + ") a " + " left join" + " (select * from wlyy_sign_patient_label_info where label_type = ? and status = 1) b" + " on a.patient = b.patient where b.patient is null "; if (teamCode > 0) { args = new Object[]{doctor, teamCode, labelType}; } else { args = new Object[]{doctor, labelType}; } } else { sql = "select count(DISTINCT a.patient) count" + " from" + " (select * from wlyy_sign_family where " + (doc.getLevel() == 2 ? " doctor" : "doctor_health") + " = ? and status > 0 " + (teamCode > 0 ? "and admin_team_code = ?" : "") + ") a " + " join" + " (select * from wlyy_sign_patient_label_info where label = ? and label_type = ? and status = 1) b" + " on a.patient = b.patient "; if (teamCode > 0) { args = new Object[]{doctor, teamCode, label.getLabelCode(), labelType}; } else { args = new Object[]{doctor, label.getLabelCode(), labelType}; } } Map count = jdbcTemplate.queryForMap(sql, args); if (count != null && count.containsKey("count")) { amount = Integer.valueOf(String.valueOf(count.get("count"))); } json.put("amount", amount); result.put(json); } } return result; } /** * 设置患者标签 * * @param patient 患者名称 * @param idcard 身份证号 * @param patientName 患者姓名 * @param health 健康情况标签 * @param disease 疾病类型标签 * @param custom 自定义标签 * @return */ public int setPatientLabels(String patient, String idcard, String patientName, String health, String disease, String custom) { Patient p = patientDao.findByCode(patient); if (p == null && (StringUtils.isEmpty(idcard) || StringUtils.isEmpty(patientName))) { return 0; } else { idcard = p.getIdcard(); patientName = p.getName(); } String[] diseaseArr = disease.split(","); String[] customArr = custom.split(","); // 健康情况标签 int healthAmount = labelInfoDao.deleteByPatientAndLabelTypeAndStatus(patient, "2", 1); if (StringUtils.isNotEmpty(health)) { SignPatientLabelInfo healthLabel = new SignPatientLabelInfo(); SignPatientLabel label = labelService.getLabelByCodeAndType(health, "2"); if (label == null) { return -1; } healthLabel.setPatient(patient); healthLabel.setPname(patientName); healthLabel.setLabelType("2"); healthLabel.setLabel(health); healthLabel.setLabelName(label.getLabelName()); healthLabel.setStatus(1); healthLabel.setCzrq(new Date()); labelInfoDao.save(healthLabel); } else { return -2; } // 疾病类型标签 int disAmount = labelInfoDao.deleteByPatientAndLabelTypeAndStatus(patient, "3", 1); if (diseaseArr != null && diseaseArr.length > 0) { for (String diseaseLabel : diseaseArr) { if (StringUtils.isEmpty(diseaseLabel)) { continue; } SignPatientLabelInfo disLabel = new SignPatientLabelInfo(); SignPatientLabel label = labelService.getLabelByCodeAndType(diseaseLabel, "3"); if (label == null) { return -1; } disLabel.setPatient(patient); disLabel.setPname(patientName); disLabel.setLabelType("3"); disLabel.setLabel(diseaseLabel); disLabel.setLabelName(label.getLabelName()); disLabel.setStatus(1); disLabel.setCzrq(new Date()); labelInfoDao.save(disLabel); } } // 自定义标签 int cusAmount = labelInfoDao.deleteByPatientAndLabelTypeAndStatus(patient, "4", 1); if (customArr != null && customArr.length > 0) { for (String customLabel : customArr) { if (StringUtils.isEmpty(customLabel)) { continue; } SignPatientLabelInfo cusLabel = new SignPatientLabelInfo(); SignPatientLabel label = labelService.getLabelByCodeAndType(customLabel, "4"); if (label == null) { return -1; } cusLabel.setPatient(patient); cusLabel.setPname(patientName); cusLabel.setLabelType("4"); cusLabel.setLabel(customLabel); cusLabel.setLabelName(label.getLabelName()); cusLabel.setStatus(1); cusLabel.setCzrq(new Date()); labelInfoDao.save(cusLabel); } } // 卫计委三大分组 int wjwAmount = labelInfoDao.deleteByPatientAndLabelTypeAndStatus(patient, "1", 1); if (diseaseArr != null && diseaseArr.length > 0 && (Arrays.asList(diseaseArr).contains("1") || Arrays.asList(diseaseArr).contains("2"))) { SignPatientLabelInfo disLabel = new SignPatientLabelInfo(); disLabel.setPatient(patient); disLabel.setPname(patientName); disLabel.setLabelType("1"); disLabel.setLabel("2"); disLabel.setLabelName("慢病人群"); disLabel.setStatus(1); disLabel.setCzrq(new Date()); labelInfoDao.save(disLabel); } else { int age = IdCardUtil.getAgeForIdcard(idcard); SignPatientLabelInfo disLabel = new SignPatientLabelInfo(); disLabel.setPatient(patient); disLabel.setPname(patientName); disLabel.setLabelType("1"); disLabel.setStatus(1); disLabel.setCzrq(new Date()); if (age >= 65) { disLabel.setLabel("3"); disLabel.setLabelName("65岁以上人群"); } else { disLabel.setLabel("1"); disLabel.setLabelName("普通人群"); } labelInfoDao.save(disLabel); } return 1; } /** * 查询某个患者的某个类型的标签 * * @param patient 患者code * @param labelType 标签类型 * @return */ public List getPatientLabelByLabelType(String patient, String labelType) { List labels = new ArrayList<>(); if (StringUtils.isEmpty(labelType)) { labels = labelInfoDao.findByPatientAndStatus(patient, 1); } else { labels = labelInfoDao.findByPatientAndLabelTypeAndStatus(patient, labelType, 1); } return labels; } /** * 根据患者姓名或标签名称查询 * * @param doctor * @param filter * @return * @throws Exception */ public JSONArray searchPatientByNameOrLabel(String doctor, String filter, String labelCode, String labelType) throws Exception { Doctor doc = doctorDao.findByCode(doctor); if (doc == null) { throw new Exception("doctor info can not find"); } Map result = new HashMap<>(); List> signList = new ArrayList<>(); Object[] args = null; String sql = "select a.*" + " from" + " (select * from wlyy_sign_family where " + (doc.getLevel() == 2 ? " doctor" : "doctor_health") + " = ? and status > 0) a" + " left join" + " (select * from wlyy_sign_patient_label_info where status = 1 " + (StringUtils.isNotEmpty(labelCode) ? " and label = ? " : "") + (StringUtils.isNotEmpty(labelType) ? " and label_type = ? " : "") + ") b" + " on a.patient = b.patient where a.name like ? or b.label_name like ?"; if (StringUtils.isNotEmpty(labelCode)) { args = new Object[]{doctor, labelCode, labelType, "%" + filter + "%", "%" + filter + "%"}; } else if (StringUtils.isEmpty(labelCode) && StringUtils.isNotEmpty(labelType)) { args = new Object[]{doctor, labelType, "%" + filter + "%", "%" + filter + "%"}; } else { args = new Object[]{doctor, "%" + filter + "%", "%" + filter + "%"}; } signList = jdbcTemplate.queryForList(sql, args); if (signList != null && signList.size() > 0) { for (Map sign : signList) { Patient p = patientDao.findByCode(sign.get("patient") == null ? "" : sign.get("patient").toString()); if (p == null) { continue; } if (result.containsKey(p.getCode())) { JSONObject jsonP = result.get(p.getCode()); if (!String.valueOf(jsonP.get("signType")).equals(String.valueOf(sign.get("type")))) { jsonP.put("signType", 3); } continue; } List labels = labelInfoDao.findByPatientAndStatus(sign.get("patient").toString(), 1); JSONObject json = new JSONObject(); // 设置患者标识 json.put("code", p.getCode()); // 设置患者姓名 json.put("name", p.getName()); // 设置患者头像 json.put("photo", p.getPhoto()); // 设置患者年龄 json.put("age", DateUtil.getAgeByBirthday(p.getBirthday())); // 设置患者性别 json.put("sex", p.getSex()); // 设置签约日期 json.put("qyrq", sign.get("apply_date") != null ? DateUtil.dateToStr((Date) sign.get("apply_date"), DateUtil.YYYY_MM_DD) : ""); // 设置签约类型 json.put("signType", sign.get("type") == null ? "" : sign.get("type")); // 身份证号 json.put("idcard", p.getIdcard()); // 患者标签 json.put("labels", labels == null ? "" : labels); result.put(p.getCode(), json); } } return result.size() > 0 ? new JSONArray(result.values()) : new JSONArray(); } /** * 添加居民到某个标签 * * @param patient 患者 * @param labelCode 标签code * @param labelType 标签类型 * @return */ public int addPatientLabel(String patient, String labelCode, String labelType) { Patient p = patientDao.findByCode(patient); if (p == null) { return -1; } SignPatientLabelInfo labelInfo = labelInfoDao.findByPatientAndLabelAndLabelTypeAndStatus(patient, labelCode, labelType, 1); if (labelInfo != null) { return -2; } SignPatientLabel label = labelService.getLabelByCodeAndType(labelCode, labelType); if (label == null) { return -3; } labelInfo = new SignPatientLabelInfo(); labelInfo.setPatient(patient); labelInfo.setPname(p.getName()); labelInfo.setLabel(labelCode); labelInfo.setLabelType(labelType); labelInfo.setLabelName(label.getLabelName()); labelInfo.setStatus(1); labelInfo.setCzrq(new Date()); labelInfoDao.save(labelInfo); return 1; } /** * 删除居民的某个标签 * * @param patient 患者 * @param labelCode 标签code * @param labelType 标签类型 * @return */ public int deletePatientLabel(String patient, String labelCode, String labelType) { SignPatientLabelInfo labelInfo = labelInfoDao.findByPatientAndLabelAndLabelTypeAndStatus(patient, labelCode, labelType, 1); if (labelInfo == null) { return -1; } labelInfoDao.delete(labelInfo); return 1; } /** * 疾病转标签 * * @return */ public boolean diseaseToLabel() { List diseases = diseaseDao.findByDel("1"); Iterator patients = patientDao.findAll().iterator(); Map patientNames = new HashMap<>(); while (patients.hasNext()) { Patient p = patients.next(); patientNames.put(p.getCode(), p.getName()); } for (PatientDisease disease : diseases) { SignPatientLabelInfo labelInfo = new SignPatientLabelInfo(); labelInfo.setPatient(disease.getPatient()); labelInfo.setPname(patientNames.get(disease.getPatient())); labelInfo.setLabel(disease.getDisease()); labelInfo.setLabelName(disease.getDiseaseName()); labelInfo.setLabelType("3"); labelInfo.setStatus(1); labelInfo.setCzrq(new Date()); labelInfoDao.save(labelInfo); } return true; } /** * 分组转标签 * * @return */ public boolean groupToLabel() throws Exception { Iterator patients = patientDao.findAll().iterator(); Map patientNames = new HashMap<>(); Map normalLabel = new HashMap<>(); Map manbingLabel = new HashMap<>(); Map sixFiveLabel = new HashMap<>(); while (patients.hasNext()) { Patient p = patients.next(); patientNames.put(p.getCode(), p); } List groupInfos = groupInfoDao.findByGroupAndStatus("1", 1); for (DoctorPatientGroupInfo groupInfo : groupInfos) { Patient p = patientNames.get(groupInfo.getPatient()); if (p == null) { continue; } SignPatientLabelInfo labelInfo = new SignPatientLabelInfo(); labelInfo.setPatient(p.getCode()); labelInfo.setPname(p.getName()); labelInfo.setLabel("1"); labelInfo.setLabelName("普通人群"); labelInfo.setLabelType("1"); labelInfo.setStatus(1); labelInfo.setCzrq(new Date()); normalLabel.put(p.getCode(), labelInfo); } List groupInfos1 = groupInfoDao.findByGroupAndStatus("2", 1); for (DoctorPatientGroupInfo groupInfo : groupInfos1) { Patient p = patientNames.get(groupInfo.getPatient()); if (p == null) { continue; } SignPatientLabelInfo labelInfo = new SignPatientLabelInfo(); labelInfo.setPatient(p.getCode()); labelInfo.setPname(p.getName()); labelInfo.setLabel("2"); labelInfo.setLabelName("慢病人群"); labelInfo.setLabelType("1"); labelInfo.setStatus(1); labelInfo.setCzrq(new Date()); manbingLabel.put(p.getCode(), labelInfo); } List groupInfos2 = groupInfoDao.findByGroupAndStatus("3", 1); for (DoctorPatientGroupInfo groupInfo : groupInfos2) { Patient p = patientNames.get(groupInfo.getPatient()); if (p == null) { continue; } SignPatientLabelInfo labelInfo = new SignPatientLabelInfo(); labelInfo.setPatient(p.getCode()); labelInfo.setPname(p.getName()); labelInfo.setLabel("3"); labelInfo.setLabelName("65岁以上人群"); labelInfo.setLabelType("1"); labelInfo.setStatus(1); labelInfo.setCzrq(new Date()); sixFiveLabel.put(p.getCode(), labelInfo); } if (normalLabel.size() > 0) { for (SignPatientLabelInfo labelInfo : normalLabel.values()) { labelInfoDao.save(labelInfo); } } if (manbingLabel.size() > 0) { for (SignPatientLabelInfo labelInfo : manbingLabel.values()) { labelInfoDao.save(labelInfo); } } if (sixFiveLabel.size() > 0) { for (SignPatientLabelInfo labelInfo : sixFiveLabel.values()) { labelInfoDao.save(labelInfo); } } return true; } }