|  | @ -1,8 +1,10 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.consult;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.doorCoach.BaseDoorCoachOrderDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.sign.ServicePackageSignRecordDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.service.family.PatientFamilyMemberService;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.service.sign.ServicePackageService;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
 | 
	
	
		
			
				|  | @ -12,12 +14,15 @@ import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.servicePackage.ServicePackageSignRecordDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.assistance.EmergencyAssistanceDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.doorCoach.BaseDoorCoachOrderDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.securitymonitoring.SecurityMonitoringOrderDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.dao.ConsultDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.dao.ConsultTeamDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.util.ImUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.ResponseContant;
 | 
	
		
			
				|  |  | import com.yihu.jw.utils.EntityUtils;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.apache.poi.ss.formula.functions.T;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -50,6 +55,8 @@ public class ConsultTeamService {
 | 
	
		
			
				|  |  |     private ServicePackageSignRecordDao servicePackageSignRecordDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoorCoachOrderDao baseDoorCoachOrderDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientFamilyMemberService familyMemberService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public JSONObject updateIMMsg(String sessionId,String sessionType,String msgId,String content){
 | 
	
	
		
			
				|  | @ -209,44 +216,6 @@ public class ConsultTeamService {
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param assistanceDO
 | 
	
		
			
				|  |  |      * @param endOperator 操作者ID
 | 
	
		
			
				|  |  |      * @param endType 1居民 2医生
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public boolean finishEmergencyConsult(EmergencyAssistanceDO assistanceDO, String endOperator, int endType){
 | 
	
		
			
				|  |  |         ConsultTeamDo consultTeam = consultTeamDao.queryByRelationCode(assistanceDO.getId());
 | 
	
		
			
				|  |  |         ConsultDo consult = consultDao.findOne(consultTeam.getConsult());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sessionId = assistanceDO.getPatient() + "_" + consultTeam.getConsult() + "_" + consultTeam.getType();
 | 
	
		
			
				|  |  |         String operatorId = endOperator;
 | 
	
		
			
				|  |  |         String operatorName=null;
 | 
	
		
			
				|  |  |         if (endType==1){
 | 
	
		
			
				|  |  |             BasePatientDO patientDO = patientDao.findById(endOperator);
 | 
	
		
			
				|  |  |             operatorName = patientDO.getName();
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             BaseDoctorDO doctorDO = doctorDao.findById(endOperator);
 | 
	
		
			
				|  |  |             operatorName = doctorDO.getName();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             JSONObject obj =  imUtill.endTopics(sessionId,operatorId,operatorName,consultTeam.getConsult());
 | 
	
		
			
				|  |  |             if (obj == null||obj.getInteger("status") == -1) {
 | 
	
		
			
				|  |  |                 return false;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             consultTeam.setEndMsgId(obj.getString("id"));
 | 
	
		
			
				|  |  |             consult.setEndTime(new Date());
 | 
	
		
			
				|  |  |             consultTeam.setEndTime(new Date());
 | 
	
		
			
				|  |  |             consultTeam.setStatus(1);
 | 
	
		
			
				|  |  |             consultDao.save(consult);
 | 
	
		
			
				|  |  |             consultTeamDao.save(consultTeam);
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return true;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 添加上门辅导服务咨询
 | 
	
		
			
				|  |  |      *
 | 
	
	
		
			
				|  | @ -327,21 +296,117 @@ public class ConsultTeamService {
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param assistanceDO
 | 
	
		
			
				|  |  |      * 添加安防警报咨询
 | 
	
		
			
				|  |  |      * @param orderId
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONObject addSecurityMoConsult(String orderId,String patient,String proxyPatient,String symptoms) throws Exception {
 | 
	
		
			
				|  |  |         JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 判断居民是否已经签约
 | 
	
		
			
				|  |  |         List<ServicePackageSignRecordDO> signRecordDOs = servicePackageSignRecordDao.findByStatusAndPatient(1,patient);
 | 
	
		
			
				|  |  |         if(signRecordDOs.size() == 0){
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "当前居民未签约,无法进行安放救助咨询!";
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 添加咨询记录
 | 
	
		
			
				|  |  |         BasePatientDO patientDO = patientDao.findById(patient);
 | 
	
		
			
				|  |  |         if(null == patientDO){
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "当前咨询的居民不存在!";
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //咨询记录
 | 
	
		
			
				|  |  |         String title = patientDO.getName() + "疑似离开安全区域";
 | 
	
		
			
				|  |  |         ConsultDo consult = addConsult(patient,title,symptoms,patientDO.getPhone(),22);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //咨询详细信息
 | 
	
		
			
				|  |  |         ConsultTeamDo consultTeam = new ConsultTeamDo();
 | 
	
		
			
				|  |  |         consultTeam.setType(22);  //安防警报咨询
 | 
	
		
			
				|  |  | //        consultTeam.setAdminTeamId(signFamily.getAdminTeamId());
 | 
	
		
			
				|  |  |         consultTeam.setRelationCode(orderId); //关联业务code
 | 
	
		
			
				|  |  |         consultTeam.setSymptoms(symptoms);
 | 
	
		
			
				|  |  |         consultTeam.setPatient(patient);
 | 
	
		
			
				|  |  |         consultTeam.setName(patientDO.getName());
 | 
	
		
			
				|  |  |         consultTeam.setBirthday(patientDO.getBirthday());
 | 
	
		
			
				|  |  |         consultTeam.setSex(patientDO.getSex());
 | 
	
		
			
				|  |  |         consultTeam.setPhoto(patientDO.getPhoto());
 | 
	
		
			
				|  |  |         consultTeam.setCzrq(new Date());
 | 
	
		
			
				|  |  |         consultTeam.setDel("1");
 | 
	
		
			
				|  |  |         consultTeam.setStatus(0);
 | 
	
		
			
				|  |  |         consultTeam.setEvaluate(0);
 | 
	
		
			
				|  |  |         consultTeam.setDoctorRead(1); // 医生未读数量为1
 | 
	
		
			
				|  |  |         consult.setRelationCode(orderId);//关联业务code
 | 
	
		
			
				|  |  |         consultTeam.setConsult(consult.getId()); // 设置咨询标识
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //(im创建咨询) 安防警报咨询的sessionid为居民code+咨询id+咨询类型
 | 
	
		
			
				|  |  |         String sessionId = patient + "_" + consult.getId() + "_" + consultTeam.getType();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //4、安防警报咨询-参与者  提供服务的助老员 老人本人 家属
 | 
	
		
			
				|  |  |         JSONObject participants = new JSONObject();
 | 
	
		
			
				|  |  |         participants.put(patient, 0);
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(proxyPatient)&&(!proxyPatient.equals(patient))){
 | 
	
		
			
				|  |  |             participants.put(proxyPatient, 0);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         //家属
 | 
	
		
			
				|  |  |         JSONArray familyArr =  familyMemberService.getPatientMembers(patient,null,null,null);
 | 
	
		
			
				|  |  |         for (int i=0;i<familyArr.size();i++){
 | 
	
		
			
				|  |  |             JSONObject tmp = familyArr.getJSONObject(0);
 | 
	
		
			
				|  |  |             String familyId = tmp.getString("id");
 | 
	
		
			
				|  |  |             participants.put(familyId, 0);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //加入团队医生
 | 
	
		
			
				|  |  |         List<Map<String,Object>> doctorList = servicePackageService.fingdDoctorByPaitenId(patient,"preventLost");
 | 
	
		
			
				|  |  |         for(Map<String,Object> map:doctorList){
 | 
	
		
			
				|  |  |             participants.put(String.valueOf(map.get("id")), 0);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String content = patientDO.getName() + "发起安防警报申请";
 | 
	
		
			
				|  |  |         JSONObject messages = imUtill.getCreateTopicMessage(patient, patientDO.getName(), consult.getTitle(), content, consult.getImages(), "");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject imResponseJson = imUtill.createTopics(sessionId, consult.getId(), content, participants, messages, ImUtil.SESSION_TYPE_EMERGENCY_ASSISTANCE);
 | 
	
		
			
				|  |  |         if (imResponseJson == null || imResponseJson.getInteger("status") == -1) {
 | 
	
		
			
				|  |  |             String failMsg = "发起服务咨询时:IM" + imResponseJson.getString("message");
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.success);
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         consultTeam.setStartMsgId(imResponseJson.get("start_msg_id").toString());
 | 
	
		
			
				|  |  |         consultTeamDao.save(consultTeam);
 | 
	
		
			
				|  |  |         consultDao.save(consult);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultFlag, ResponseContant.success);
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultMsg, consultTeam);
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 结束咨询
 | 
	
		
			
				|  |  |      * @param relationCode
 | 
	
		
			
				|  |  |      * @param patient sessionid 组成部分
 | 
	
		
			
				|  |  |      * @param endOperator 操作者ID
 | 
	
		
			
				|  |  |      * @param endType 1居民 2医生
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public boolean finishDoorCoachConsult(BaseDoorCoachOrderDO assistanceDO, String endOperator, int endType){
 | 
	
		
			
				|  |  |         ConsultTeamDo consultTeam = consultTeamDao.queryByRelationCode(assistanceDO.getId());
 | 
	
		
			
				|  |  |     public boolean finishConsult(String relationCode,String patient, String endOperator, int endType){
 | 
	
		
			
				|  |  |         ConsultTeamDo consultTeam = consultTeamDao.queryByRelationCode(relationCode);
 | 
	
		
			
				|  |  |         if (consultTeam==null){
 | 
	
		
			
				|  |  |             return true;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         ConsultDo consult = consultDao.findOne(consultTeam.getConsult());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sessionId = assistanceDO.getPatient() + "_" + consultTeam.getConsult() + "_" + consultTeam.getType();
 | 
	
		
			
				|  |  |         String sessionId = patient+ "_" + consultTeam.getConsult() + "_" + consultTeam.getType();
 | 
	
		
			
				|  |  |         String operatorId = endOperator;
 | 
	
		
			
				|  |  |         String operatorName=null;
 | 
	
		
			
				|  |  |         if (endType==1){
 |