|  | @ -2,11 +2,15 @@ package com.yihu.jw.care.endpoint.consult;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.service.consult.ConsultService;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorRoleDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.service.BaseDoctorInfoService;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorRoleDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.im.ConsultTeamDo;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.wx.WxWechatDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.hospital.message.SystemMessageDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.file_upload.FileUploadService;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.family.service.WlyyFamilyMemberService;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.message.service.SystemMessageService;
 | 
	
	
		
			
				|  | @ -20,12 +24,14 @@ import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
 | 
	
		
			
				|  |  | import com.yihu.jw.rm.hospital.BaseHospitalRequestMapping;
 | 
	
		
			
				|  |  | import com.yihu.jw.rm.patient.PatientRequestMapping;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.FileUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.IdCardUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.dao.WechatDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.service.WechatInfoService;
 | 
	
		
			
				|  |  | import com.yihu.jw.wlyy.service.WlyyBusinessService;
 | 
	
		
			
				|  |  | import io.swagger.annotations.Api;
 | 
	
		
			
				|  |  | import io.swagger.annotations.ApiOperation;
 | 
	
		
			
				|  |  | import io.swagger.annotations.ApiParam;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
	
		
			
				|  | @ -94,7 +100,192 @@ public class PatientConsultEndpoint extends EnvelopRestEndpoint {
 | 
	
		
			
				|  |  | 	private String wechat_appid;
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	private FileUtil fileUtil;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	private ConsultService consultService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	@GetMapping(value = "isExistsUnfinishedOnline")
 | 
	
		
			
				|  |  | 	@ApiOperation(value = "查询居民是否存在未结束的在线咨询")
 | 
	
		
			
				|  |  | 	public Envelop isExistsUnfinishedOnline(
 | 
	
		
			
				|  |  | 			@ApiParam(name = "doctor", value = "医生CODE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "doctor",required = false) String doctor,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "patient", value = "居民CODE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "patient",required = true) String patient)throws Exception {
 | 
	
		
			
				|  |  | 		try {
 | 
	
		
			
				|  |  | 			String consultCode = consultService.getUnfinishedConsult(patient, doctor);
 | 
	
		
			
				|  |  | 			return success("请求成功",consultCode);
 | 
	
		
			
				|  |  | 		}catch (Exception e){
 | 
	
		
			
				|  |  | 			return failedException(e);
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	@PostMapping(value = "addOnlineConsult")
 | 
	
		
			
				|  |  | 	@ApiOperation(value = "新增在线咨询")
 | 
	
		
			
				|  |  | 	public Envelop addOnlineConsult(
 | 
	
		
			
				|  |  | 			@ApiParam(name = "patient", value = "居民CODE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "patient",required = false) String patient,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "doctor", value = "医生CODE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "doctor",required = true) String doctor,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "symptoms", value = "主述")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "symptoms",required = false) String symptoms,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "images", value = "图片")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "images",required = false) String images,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "voice", value = "语音")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "voice",required = false) String voice,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "type", value = "咨询类型:1专家咨询 14导诊助手 17专家视频咨询")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "type",required = false) Integer type,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "source", value = "咨询类型来源")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "source",required = false) String source)throws Exception{
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 		System.out.println("symptoms="+symptoms);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 		if (type == null) {
 | 
	
		
			
				|  |  | 			type = 23;
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		ConsultTeamDo consult = new ConsultTeamDo();
 | 
	
		
			
				|  |  | 		// 设置咨询类型:23 在线咨询
 | 
	
		
			
				|  |  | 		consult.setType(type);
 | 
	
		
			
				|  |  | 		// 设置主要症状
 | 
	
		
			
				|  |  | 		consult.setSymptoms(symptoms);
 | 
	
		
			
				|  |  | 		// 设置咨询图片URL
 | 
	
		
			
				|  |  | 		consult.setImages(images);
 | 
	
		
			
				|  |  | 		// 设置咨询语音URL
 | 
	
		
			
				|  |  | 		consult.setVoice(voice);
 | 
	
		
			
				|  |  | 		consult.setDoctor(doctor);
 | 
	
		
			
				|  |  | 		consult.setPatient(patient);
 | 
	
		
			
				|  |  | 		// 保存到数据库
 | 
	
		
			
				|  |  | 		synchronized (patient.intern()){//新增同步方法
 | 
	
		
			
				|  |  | 			JSONObject re = null;
 | 
	
		
			
				|  |  | 			re = consultService.addTeamConsult(consult, patient,doctor,source);
 | 
	
		
			
				|  |  | 			//发送系统消息
 | 
	
		
			
				|  |  | 			SystemMessageDO systemMessageDO = new SystemMessageDO();
 | 
	
		
			
				|  |  | 			systemMessageDO.setTitle("新增在线咨询");
 | 
	
		
			
				|  |  | 			systemMessageDO.setType("850");
 | 
	
		
			
				|  |  | 			systemMessageDO.setReceiver(re.getString("doctor"));
 | 
	
		
			
				|  |  | 			systemMessageDO.setReceiverName(re.getString("doctorName"));
 | 
	
		
			
				|  |  | 			systemMessageDO.setRelationCode(re.getString("sessiond_id"));
 | 
	
		
			
				|  |  | 			systemMessageDO.setSender(re.getString("patient"));
 | 
	
		
			
				|  |  | 			systemMessageDO.setSenderName(re.getString("patientName"));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 			net.sf.json.JSONObject data = new net.sf.json.JSONObject();
 | 
	
		
			
				|  |  | 			data.put("name",re.getString("patientName"));
 | 
	
		
			
				|  |  | 			data.put("age", IdCardUtil.getAgeForIdcard(re.getString("patient_idcard")));
 | 
	
		
			
				|  |  | 			data.put("gender",IdCardUtil.getSexForIdcard_new(re.getString("patient_idcard")));
 | 
	
		
			
				|  |  | 			data.put("question",symptoms);
 | 
	
		
			
				|  |  | 			data.put("type",1);
 | 
	
		
			
				|  |  | 			systemMessageDO.setData(data.toString());
 | 
	
		
			
				|  |  | 			consultService.saveMessage(systemMessageDO);
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		return success("操作成功", consult);
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	@GetMapping(value = "records")
 | 
	
		
			
				|  |  | 	@ApiOperation(value = "患者咨询记录查询")
 | 
	
		
			
				|  |  | 	public Envelop records(
 | 
	
		
			
				|  |  | 			@ApiParam(name = "patient", value = "居民id")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "patient",required = true) String patient,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "title", value = "咨询标题关键字")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "title",required = false) String title,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "id", value = "咨询ID")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "id",required = false) String id,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "type", value = "咨询类型")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "type",required = true) String type,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "status", value = "咨询状态:0进行中,1已完成,-1患者取消,-2超时未响应自动关闭")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "status",required = false) Integer status,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "page", value = "第几页")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "page",required = false) int page,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "pagesize", value = "分页大小")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "pagesize",required = false) int pagesize,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "doctorName", value = "医生名字")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "doctorName",required = false) String doctorName)throws Exception{
 | 
	
		
			
				|  |  | 		try {
 | 
	
		
			
				|  |  | 			List<Map<String,Object>>  data = consultService.findConsultRecordByPatient(patient, id,type, page,pagesize, title,status,doctorName);
 | 
	
		
			
				|  |  | 			Long total = imService.countConsultRecordByPatient(patient, id,type,title);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 			JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  | 			result.put("total",total);
 | 
	
		
			
				|  |  | 			result.put("list",data);
 | 
	
		
			
				|  |  | 			return success(result);
 | 
	
		
			
				|  |  | 		}catch (Exception e){
 | 
	
		
			
				|  |  | 			return failedException(e);
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	@PostMapping(value = "finish")
 | 
	
		
			
				|  |  | 	@ApiOperation(value = "居民结束咨询", notes = "居民结束咨询")
 | 
	
		
			
				|  |  | 	public Envelop finish(
 | 
	
		
			
				|  |  | 			@ApiParam(name = "consult", value = "咨询CODE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "consult",required = false) String consult,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "patientCode", value = "居民COEE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "patientCode",required = false) String patientCode) throws Exception {
 | 
	
		
			
				|  |  | 		int resutl = consultService.finish(consult,patientCode,1);
 | 
	
		
			
				|  |  | 		BasePatientDO basePatientDO = basePatientService.findByIdAndDel(patientCode);
 | 
	
		
			
				|  |  | 		JSONObject msgObj = new JSONObject();
 | 
	
		
			
				|  |  | 		msgObj.put("msg",basePatientDO.getName()+"结束了咨询");
 | 
	
		
			
				|  |  | 		msgObj.put("consultcode",consult);
 | 
	
		
			
				|  |  | 		String jsonStr = "";
 | 
	
		
			
				|  |  | 		if(1 == resutl){
 | 
	
		
			
				|  |  | 			jsonStr = "{\"id\":\""+UUID.randomUUID().toString()+"\",\"sender_id\":\""+patientCode+"\",\"sender_name\":\"系统\",\"timestamp\":"+System.currentTimeMillis()+",\"content_type\":7,\"content\":"+msgObj.toString()+",\"business_type\":1}";
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 		return success(jsonStr);
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	@GetMapping(value = "getTopic")
 | 
	
		
			
				|  |  | 	@ApiOperation(value = "根据咨询CODE获取session_id", notes = "根据咨询CODE获取session_id")
 | 
	
		
			
				|  |  | 	public Envelop getTopic(
 | 
	
		
			
				|  |  | 			@ApiParam(name = "consult", value = "咨询CODE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "consult",required = false) String consult)throws Exception {
 | 
	
		
			
				|  |  | 		try {
 | 
	
		
			
				|  |  | 			return success("请求成功",imService.getTopic(consult));
 | 
	
		
			
				|  |  | 		}catch (Exception e){
 | 
	
		
			
				|  |  | 			return failedException(e);
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	@PostMapping(value = "append")
 | 
	
		
			
				|  |  | 	@ApiOperation(value = "居民咨询发消息(追问接口)")
 | 
	
		
			
				|  |  | 	public ListEnvelop append(
 | 
	
		
			
				|  |  | 			@ApiParam(name = "consult", value = "咨询CODE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "consult",required = true) String consult,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "content", value = "追问内容")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "content",required = true) String content,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "type", value = "追问内容类型:1文字,2图片,3语音  ... (im消息类型)")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "type",required = true) Integer type,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "times", value = "")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "times",required = false) Integer times,
 | 
	
		
			
				|  |  | 			@ApiParam(name = "patientCode", value = "居民CODE")
 | 
	
		
			
				|  |  | 			@RequestParam(value = "patientCode",required = false) String patientCode) throws Exception {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 		try {
 | 
	
		
			
				|  |  | 			List<String> failed = new ArrayList<>();
 | 
	
		
			
				|  |  | 			//处理语音文件
 | 
	
		
			
				|  |  | 			if(3 == type){//语音文件
 | 
	
		
			
				|  |  | 				String voicepath = imService.fetchWxVoices(wechat_appid);
 | 
	
		
			
				|  |  | 				logger.info("voice_path:"+voicepath);
 | 
	
		
			
				|  |  | 				JSONObject obj = new JSONObject();
 | 
	
		
			
				|  |  | 				String voiceurl = "";
 | 
	
		
			
				|  |  | 				// 将临时语音拷贝到正式存储路径下
 | 
	
		
			
				|  |  | 				if (StringUtils.isNotEmpty(voicepath)) {
 | 
	
		
			
				|  |  | 					voiceurl = fileUtil.copyTempVoice(voicepath,fastdfs_file_url);
 | 
	
		
			
				|  |  | 					obj.put("path", voiceurl);
 | 
	
		
			
				|  |  | 					obj.put("times", times);
 | 
	
		
			
				|  |  | 					content = obj.toString();
 | 
	
		
			
				|  |  | 				}
 | 
	
		
			
				|  |  | 			}else if(2 == type){//图片文件
 | 
	
		
			
				|  |  | 				String imagepath = imService.fetchWxImages(wechat_appid);
 | 
	
		
			
				|  |  | 				logger.info("image_path:"+imagepath);
 | 
	
		
			
				|  |  | 				// 将临时语音拷贝到正式存储路径下
 | 
	
		
			
				|  |  | 				if (StringUtils.isNotEmpty(imagepath)) {
 | 
	
		
			
				|  |  | 					content = fileUploadService.uploadWxImage(imagepath);
 | 
	
		
			
				|  |  | 				}
 | 
	
		
			
				|  |  | 			}else{}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 			failed = imService.append(consult,content,type,times,patientCode,wechat_appid);
 | 
	
		
			
				|  |  | 			return success(failed);
 | 
	
		
			
				|  |  | 		}catch (Exception e){
 | 
	
		
			
				|  |  | 			return failedListEnvelopException(e);
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	@GetMapping(value = BaseHospitalRequestMapping.PatientIM.participants)
 | 
	
		
			
				|  |  | 	@ApiOperation(value = "获取会话成员")
 |