| 
					
				 | 
			
			
				@ -1,19 +1,32 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				package com.yihu.wlyy.web.patient.device; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.fasterxml.jackson.databind.ObjectMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.entity.device.DeviceDetail; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.entity.device.PatientDevice; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.entity.device.PatientHealthTime; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.entity.patient.Patient; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.entity.wechat.WechatTemplateConfig; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.repository.deviece.PatientHealthTimeDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.repository.patient.PatientDeviceDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.repository.wechat.WechatTemplateConfigDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.service.app.device.PatientDeviceService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.service.app.device.WlyyDeviceDetailService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.service.app.health.PatientHealthIndexService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.service.common.account.PatientService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.service.third.health.bank.CreditLogService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.task.PushMsgTask; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.util.DateUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.util.HttpClientUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.web.BaseController; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.web.doctor.device.DoctorDeviceController; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.wlyy.wechat.util.WeiXinAccessTokenUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import io.swagger.annotations.Api; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import io.swagger.annotations.ApiOperation; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import io.swagger.annotations.ApiParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.apache.commons.lang3.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.json.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.slf4j.LoggerFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.beans.factory.annotation.Value; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.data.domain.Page; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -23,6 +36,7 @@ import org.springframework.web.bind.annotation.RequestMethod; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.web.bind.annotation.RequestParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.web.bind.annotation.ResponseBody; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.Date; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.HashMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.Map; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -36,6 +50,7 @@ import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				@RequestMapping(value = "patient/device") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				@Api(value = "患者设备管理", description = "患者设备管理") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				public class PatientDeviceController extends BaseController { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static Logger logger = LoggerFactory.getLogger(PatientDeviceController.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private PatientDeviceService patientDeviceService; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -47,6 +62,18 @@ public class PatientDeviceController extends BaseController { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private HttpClientUtil HttpClientUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private PatientHealthIndexService patientHealthIndexService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private WlyyDeviceDetailService wlyyDeviceDetailService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private CreditLogService creditLogService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private PatientService patientService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private WechatTemplateConfigDao templateConfigDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private PushMsgTask pushMsgTask; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private WeiXinAccessTokenUtils weiXinAccessTokenUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Value("${yihu.yihu_OpenPlatform_url}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private String url; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -186,6 +213,8 @@ public class PatientDeviceController extends BaseController { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            // 设置患者标识 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            device.setUser(getRepUID()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            device.setUser(getUID()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //首绑加分判断 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            boolean flag = patientDeviceService.isFirstNewBinding(device.getDeviceSn(),device.getUserType()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if(!getUID().equals(getRepUID())&&device.getId()==null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                device.setAgent(getRepUID()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -193,6 +222,72 @@ public class PatientDeviceController extends BaseController { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            synchronized (sn.intern()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                patientDeviceService.saveDevice(device); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //判断是否为积分设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            DeviceDetail deviceDetail = wlyyDeviceDetailService.findBySn(device.getDeviceSn()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (deviceDetail!=null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                //修改设备表中{"1":"0", "2":"0"}的绑定次数 和其他绑定信息, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                wlyyDeviceDetailService.updateAfterBinding(device.getDeviceSn(),device.getUserIdcard(),device.getUserType(),new Date(),flag); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                Patient patient = patientService.findByCode(device.getUser()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if (flag){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String creditDetail = "{\"tradeType\":\"HEALTH_TASK\",\"flag\":\"BIND\",\"tradeDirection\":1,\"status\":1,\"patientId\":\""+device.getUser()+"\",\"hospital\":\""+patient.getTown()+"\"}"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(creditDetail); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    com.alibaba.fastjson.JSONObject response = creditLogService.insert(jsonObject); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    System.out.println("添加积分返回数据======"+response.toJSONString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String status = response.getString("status"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if ("200".equals(status)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        List<Map<String,Object>> list = (List<Map<String,Object>>)response.get("detailModelList"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        if (list!=null && list.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            String integrate = String.valueOf(list.get(0).get("integrate")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            String flagType = String.valueOf(list.get(0).get("flag")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            //@TODO 调用发送微信模板接口 
			 | 
		
	
		
			
				 | 
				 | 
			
			
					                        //Patient people = patientDao.findByCode(device.getUser()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String openId = patient.getOpenid(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String name = patient.getName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    org.json.JSONObject sendJson = new org.json.JSONObject(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String first = ""; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String remark = ""; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String deviceName = device.getDeviceName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    WechatTemplateConfig templateConfig = templateConfigDao.findByScene("template_patient_bind_device","bdsb"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    first = templateConfig.getFirst(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    first = first.replace("key1",(deviceName==null?"":deviceName)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    remark = templateConfig.getRemark(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    sendJson.put("keyword1", deviceName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    sendJson.put("keyword2", DateUtil.dateToStrLong(device.getCzrq())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    sendJson.put("keyword3", templateConfig.getKeyword3()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    sendJson.put("first", first); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    sendJson.put("remark", remark); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    sendJson.put("url", templateConfig.getUrl());//带参数的模板跳转链接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    System.out.println(sendJson.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    pushMsgTask.putWxMsg(weiXinAccessTokenUtils.getAccessToken(), 22, openId, name, sendJson); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//	                    //发送代理人 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//	                    jsonArray = weiXinOpenIdUtils.getAgentOpenId(patient, openId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//	                    if (jsonArray != null && jsonArray.length() > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//		                    for (int i = 0; i < jsonArray.length(); i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    org.json.JSONObject j = jsonArray.getJSONObject(i); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    Patient member = (Patient) j.get("member"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    int start = url.indexOf("&toUser="); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    int end = url.indexOf("&", start + 1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    String touser = url.substring(start, end); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    url = url.replace(touser, "&toUser=" + member.getCode()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    //name患者姓名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    sendJson.put("first", weiXinOpenIdUtils.getTitleMes(people, (int) j.get("relation"), name) + first); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    sendJson.put("url", url); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//			                    pushMsgTask.putWxMsg(weiXinAccessTokenUtils.getAccessToken(), 19, member.getOpenid(), name, sendJson); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//		                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//	                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                }catch (Exception e){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    logger.info("设备绑定微信模板消息发送失败:"+e.getMessage()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//	                        e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return success("设备保存成功!"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } catch (Exception ex) { 
			 |