|  | @ -1,5 +1,6 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.dao.*;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.specialist.*;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.MixEnvelop;
 | 
	
	
		
			
				|  | @ -7,6 +8,7 @@ import com.yihu.jw.restmodel.specialist.*;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.ObjEnvelop;
 | 
	
		
			
				|  |  | import com.yihu.jw.rm.specialist.SpecialistMapping;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.IdCardUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
	
		
			
				|  | @ -15,8 +17,10 @@ import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | import java.text.ParseException;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | import java.util.stream.Collectors;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by Trick on 2018/4/25.
 | 
	
	
		
			
				|  | @ -776,15 +780,19 @@ public class SpecialistService{
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public MixEnvelop searchPatientInSpecialist(String doctorCode,String keywords,Integer page,Integer pageSize) throws Exception{
 | 
	
		
			
				|  |  |         String sql1 = " select count(1) as num ";
 | 
	
		
			
				|  |  |         String sql2 = " select p.name as name,p.idcard,p.code,p.photo ";
 | 
	
		
			
				|  |  |         String sql2 = " select p.name as name,p.idcard,p.code,p.photo,CASE WHEN wsf.status ='-4' THEN '已到期' WHEN wsf.status ='0' THEN '待签约' WHEN wsf.status ='1' THEN '已签约' WHEN wsf.status ='2' THEN '已签约' WHEN wsf.status ='3' THEN '已签约' ELSE '待签约' END  status  ";
 | 
	
		
			
				|  |  |         String whereSql ="";
 | 
	
		
			
				|  |  |         if(!StringUtils.isEmpty(keywords)){
 | 
	
		
			
				|  |  |             whereSql+=" and (p.name like '%"+keywords+"%' or p.idcard like '%"+keywords+"%' or p.mobile like '%"+keywords+"%') ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         String centerSql =" from "+basedb+".wlyy_patient p " +
 | 
	
		
			
				|  |  |                 " LEFT JOIN wlyy_specialist.wlyy_specialist_patient_relation r on p.code=r.patient and sign_status='1' and doctor='"+doctorCode+"'"+
 | 
	
		
			
				|  |  |                 " where r.id is not null "+whereSql;
 | 
	
		
			
				|  |  |         String sqlCount=sql1+centerSql;
 | 
	
		
			
				|  |  |         String centerSql =" from "+basedb+".wlyy_patient p LEFT JOIN  "+basedb+".wlyy_sign_family wsf ON p.code=wsf.patient AND wsf.type='1'" +
 | 
	
		
			
				|  |  |                 " WHERE p.code not in (SELECT r.patient FROM  wlyy_specialist.wlyy_specialist_patient_relation r WHERE r.sign_status = '1' and doctor='"+doctorCode+"')"+" AND p.openid IS NOT NULL "+
 | 
	
		
			
				|  |  |                whereSql;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String countCenterSql =" from "+basedb+".wlyy_patient p " +
 | 
	
		
			
				|  |  |                 " WHERE p.code not in (SELECT r.patient FROM  wlyy_specialist.wlyy_specialist_patient_relation r WHERE r.sign_status = '1' and doctor='"+doctorCode+"')"+" AND p.openid IS NOT NULL "+
 | 
	
		
			
				|  |  |                 whereSql;
 | 
	
		
			
				|  |  |         String sqlCount=sql1+countCenterSql;
 | 
	
		
			
				|  |  |         String sql=sql2+centerSql+" LIMIT "+(page-1)*pageSize+","+pageSize;
 | 
	
		
			
				|  |  |         List<Map<String,Object>> map = jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  |         List<Map<String,Object>> countMap = jdbcTemplate.queryForList(sqlCount);
 | 
	
	
		
			
				|  | @ -800,6 +808,7 @@ public class SpecialistService{
 | 
	
		
			
				|  |  |             m.put("name",one.get("name"));
 | 
	
		
			
				|  |  |             m.put("age",age);
 | 
	
		
			
				|  |  |             m.put("sex",sex);
 | 
	
		
			
				|  |  |             m.put("status",one.get("status"));
 | 
	
		
			
				|  |  |             if("1".equals(sex)){
 | 
	
		
			
				|  |  |                 sexName="男";
 | 
	
		
			
				|  |  |             }else if("2".equals(sex)){
 | 
	
	
		
			
				|  | @ -812,17 +821,75 @@ public class SpecialistService{
 | 
	
		
			
				|  |  |             m.put("photo",one.get("photo"));
 | 
	
		
			
				|  |  |             resultList.add(m);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         Long co=Long.parseLong("100");
 | 
	
		
			
				|  |  |         return MixEnvelop.getSuccessListWithPage(SpecialistMapping.api_success,resultList,page,pageSize,countMap.size()>0?Long.valueOf(countMap.get(0).get("num")+""):0);
 | 
	
		
			
				|  |  | //        return MixEnvelop.getSuccessListWithPage(SpecialistMapping.api_success,resultList,page,pageSize,co);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 医生的专科信息
 | 
	
		
			
				|  |  |      * @param doctor
 | 
	
		
			
				|  |  |      * 病人的家签信息、专科开通信息
 | 
	
		
			
				|  |  |      * @param doctorCode 医生code
 | 
	
		
			
				|  |  |      * @param patientCode 病人id
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public ObjEnvelop doctorForSpecialistInfo(String doctor){
 | 
	
		
			
				|  |  |     public JSONObject doctorForSpecialistInfo(String doctorCode, String patientCode)  throws Exception{
 | 
	
		
			
				|  |  |         JSONObject jsonObject = new JSONObject();
 | 
	
		
			
				|  |  |         //1、获取居民基础信息
 | 
	
		
			
				|  |  |         String preSql = "SELECT p.name as name,CASE p.sex WHEN 1 THEN '男' WHEN 2 THEN '女' ELSE '未知' END as sex,IFNULL(year( from_days( datediff( now(), p.birthday))),'未知') as age," +
 | 
	
		
			
				|  |  |                 " p.photo as photo,p.idcard as idcard,p.mobile as mobile,p.medicare_number as medicareNumber,p.ssc as ssc," +
 | 
	
		
			
				|  |  |                 " CASE  WHEN wsf.doctor_name is null THEN '无' ELSE wsf.doctor_name END as doctorName,CASE  WHEN wsf.hospital_name is NULL THEN '无' ELSE wsf.hospital_name END as hospitalName,CASE  WHEN wsf.mobile is NULL THEN '无' ELSE wsf.mobile END as doctorMobole ";
 | 
	
		
			
				|  |  |         String patientSql = " from " + basedb + ".wlyy_patient p LEFT JOIN " + basedb + ".wlyy_sign_family wsf " +
 | 
	
		
			
				|  |  |                 " ON p.code=wsf.patient AND wsf.type='2' AND wsf.status='1' " +
 | 
	
		
			
				|  |  |                 " LEFT JOIN " + basedb + ".wlyy_doctor wd ON wsf.doctor =wd.code WHERE p.code='" + patientCode + "'";
 | 
	
		
			
				|  |  |         //一个居民 生效的家签信息只会有一条
 | 
	
		
			
				|  |  |         Map<String, Object> map = jdbcTemplate.queryForMap(preSql + patientSql);
 | 
	
		
			
				|  |  |         jsonObject.put("patientInfo", map);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //2、获取医生信息(所属医院、科室、姓名)
 | 
	
		
			
				|  |  |         String doctorSql = "SELECT wd.hospital_name as hospitalName,wd.dept_name as deptName,wd.name as name FROM " + basedb + ".wlyy_doctor wd WHERE wd.code='" + doctorCode + "'";
 | 
	
		
			
				|  |  |         jsonObject.put("doctorInfo", jdbcTemplate.queryForMap(doctorSql));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //3、获取该医生所属团队及团队成员信息
 | 
	
		
			
				|  |  |         String teamSql = "SELECT at.name teamName, tm.team_id , tm.doctor_code, wd.NAME as doctorName  FROM " + basedb + ".wlyy_doctor wd LEFT JOIN  " + basedb + ".wlyy_admin_team_member tm ON wd. CODE = tm.doctor_code, " + basedb + ".wlyy_admin_team at " +
 | 
	
		
			
				|  |  |                 "WHERE tm.team_id IN ( SELECT watm.team_id teamId FROM  " + basedb + ".wlyy_admin_team_member watm WHERE watm.doctor_code ='" + doctorCode + "'" + " AND watm.available = '1' ) AND tm.available='1' AND `at`.id=tm.team_id";
 | 
	
		
			
				|  |  |         List<Map<String, Object>> teamList = jdbcTemplate.queryForList(teamSql);
 | 
	
		
			
				|  |  |         Map<Integer, List<Map<String, Object>>> m = teamList.stream().collect(Collectors.groupingBy(tem -> ((Integer) tem.get("team_id")).intValue(), Collectors.toList()));
 | 
	
		
			
				|  |  |         List<Map<String, Object>> mapList = new ArrayList<>();
 | 
	
		
			
				|  |  |         for (Integer i : m.keySet()) {
 | 
	
		
			
				|  |  |             Map<String, Object> objectMap = new HashMap<>();
 | 
	
		
			
				|  |  |             objectMap.put("teamId", i);
 | 
	
		
			
				|  |  |             objectMap.put("teamName", m.get(i).get(0).get("teamName"));
 | 
	
		
			
				|  |  |             objectMap.put("teamDoctors", m.get(i));
 | 
	
		
			
				|  |  |             mapList.add(objectMap);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         jsonObject.put("teamAndDoctors", mapList);
 | 
	
		
			
				|  |  |         //获取专科团队疾病标签
 | 
	
		
			
				|  |  |         teamSql = "SELECT wtdr.disease_name from  wlyy.wlyy_team_disease_relation wtdr WHERE wtdr.team_code IN ( SELECT watm.team_id teamId FROM  " + basedb + ".wlyy_admin_team_member watm WHERE watm.doctor_code ='" + doctorCode + "'" + " AND watm.available = '1' ) AND wtdr.del='1'";
 | 
	
		
			
				|  |  |         //new BeanPropertyRowMapper(
 | 
	
		
			
				|  |  |         List<String> stringList = jdbcTemplate.queryForList(teamSql, String.class);
 | 
	
		
			
				|  |  |         jsonObject.put("teamDisease", stringList.stream().collect(Collectors.joining(" ")));
 | 
	
		
			
				|  |  |         //疾病标签
 | 
	
		
			
				|  |  |         teamSql = "SELECT wspl.label_code,wspl.label_name from  wlyy.wlyy_sign_patient_label wspl WHERE wspl.label_type='8' AND status='1'";
 | 
	
		
			
				|  |  |         List<Map<String, Object>> signPatientLabelList = jdbcTemplate.queryForList(teamSql);
 | 
	
		
			
				|  |  |         jsonObject.put("signPatientLabels", signPatientLabelList);
 | 
	
		
			
				|  |  |         return jsonObject;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Map<String,Object> resultMap = new HashMap<>();
 | 
	
		
			
				|  |  |         return ObjEnvelop.getSuccess(SpecialistMapping.api_success,resultMap);
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 专科医生发起签约
 | 
	
		
			
				|  |  |      * @param specialistPatientRelationDO
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public ObjEnvelop<SpecialistPatientRelationDO> createPatientInSpeciaRelation(SpecialistPatientRelationDO specialistPatientRelationDO) throws Exception {
 | 
	
		
			
				|  |  |         specialistPatientRelationDO.setSignStatus("1");
 | 
	
		
			
				|  |  |         specialistPatientRelationDO.setSignDate(new Date());
 | 
	
		
			
				|  |  |         specialistPatientRelationDO.setCreateUser(specialistPatientRelationDO.getDoctor());
 | 
	
		
			
				|  |  |         specialistPatientRelationDO.setCreateUserName(specialistPatientRelationDO.getDoctorName());
 | 
	
		
			
				|  |  |         specialistPatientRelationDO.setStatus("1");//计管师分配状态
 | 
	
		
			
				|  |  |         specialistPatientRelationDO.setSignYear(String.valueOf(DateUtil.getNowYear()));
 | 
	
		
			
				|  |  |         specialistPatientRelationDO.setSignDate(new Date());
 | 
	
		
			
				|  |  |         specialistPatientRelationDO = specialistPatientRelationDao.save(specialistPatientRelationDO);
 | 
	
		
			
				|  |  |         return ObjEnvelop.getSuccess("success", specialistPatientRelationDO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |