123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- 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.*;
- /**
- * 患者标签信息服务
- * <p>
- * 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<String, JSONObject> result = new HashMap<>();
- List<Map<String, Object>> 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<String, Object> 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<SignPatientLabelInfo> 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<SignPatientLabel> 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<String, Object> 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<SignPatientLabelInfo> getPatientLabelByLabelType(String patient, String labelType) {
- List<SignPatientLabelInfo> 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<String, JSONObject> result = new HashMap<>();
- List<Map<String, Object>> 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<String, Object> 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<SignPatientLabelInfo> 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<PatientDisease> diseases = diseaseDao.findByDel("1");
- Iterator<Patient> patients = patientDao.findAll().iterator();
- Map<String, String> 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<Patient> patients = patientDao.findAll().iterator();
- Map<String, Patient> patientNames = new HashMap<>();
- Map<String, SignPatientLabelInfo> normalLabel = new HashMap<>();
- Map<String, SignPatientLabelInfo> manbingLabel = new HashMap<>();
- Map<String, SignPatientLabelInfo> sixFiveLabel = new HashMap<>();
- while (patients.hasNext()) {
- Patient p = patients.next();
- patientNames.put(p.getCode(), p);
- }
- List<DoctorPatientGroupInfo> 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<DoctorPatientGroupInfo> 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<DoctorPatientGroupInfo> 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;
- }
- }
|