|  | @ -1,10 +1,13 @@
 | 
												
													
														
															|  | package com.yihu.jw.care.service.contacts;
 |  | package com.yihu.jw.care.service.contacts;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import com.alibaba.fastjson.JSON;
 | 
												
													
														
															|  | import com.alibaba.fastjson.JSONArray;
 |  | import com.alibaba.fastjson.JSONArray;
 | 
												
													
														
															|  | import com.alibaba.fastjson.JSONObject;
 |  | import com.alibaba.fastjson.JSONObject;
 | 
												
													
														
															|  | import com.yihu.jw.care.dao.contacts.BasePatientContactsOrgDao;
 |  | import com.yihu.jw.care.dao.contacts.BasePatientContactsOrgDao;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.care.dao.contacts.PatientSosContactsDao;
 | 
												
													
														
															|  | import com.yihu.jw.care.dao.device.PatientDeviceDao;
 |  | import com.yihu.jw.care.dao.device.PatientDeviceDao;
 | 
												
													
														
															|  | import com.yihu.jw.care.dao.family.PatientFamilyMemberDao;
 |  | import com.yihu.jw.care.dao.family.PatientFamilyMemberDao;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.care.service.device.NetworkCardService;
 | 
												
													
														
															|  | import com.yihu.jw.care.service.device.PatientDeviceService;
 |  | import com.yihu.jw.care.service.device.PatientDeviceService;
 | 
												
													
														
															|  | import com.yihu.jw.care.service.family.PatientFamilyMemberService;
 |  | import com.yihu.jw.care.service.family.PatientFamilyMemberService;
 | 
												
													
														
															|  | import com.yihu.jw.care.service.sign.ServicePackageService;
 |  | import com.yihu.jw.care.service.sign.ServicePackageService;
 | 
												
											
												
													
														
															|  | @ -12,20 +15,24 @@ import com.yihu.jw.entity.base.org.BaseOrgDO;
 | 
												
													
														
															|  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 |  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
												
													
														
															|  | import com.yihu.jw.entity.base.patient.BasePatientFamilyMemberDO;
 |  | import com.yihu.jw.entity.base.patient.BasePatientFamilyMemberDO;
 | 
												
													
														
															|  | import com.yihu.jw.entity.care.contacts.BasePatientContactsOrgDO;
 |  | import com.yihu.jw.entity.care.contacts.BasePatientContactsOrgDO;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.entity.care.contacts.PatientSosContactsDO;
 | 
												
													
														
															|  | import com.yihu.jw.entity.care.device.DevicePatientDevice;
 |  | import com.yihu.jw.entity.care.device.DevicePatientDevice;
 | 
												
													
														
															|  | import com.yihu.jw.org.dao.BaseOrgDao;
 |  | import com.yihu.jw.org.dao.BaseOrgDao;
 | 
												
													
														
															|  | import com.yihu.jw.patient.dao.BasePatientDao;
 |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.restmodel.ResponseContant;
 | 
												
													
														
															|  | import com.yihu.jw.util.common.LatitudeUtils;
 |  | import com.yihu.jw.util.common.LatitudeUtils;
 | 
												
													
														
															|  | import org.apache.commons.collections.map.HashedMap;
 |  | import org.apache.commons.collections.map.HashedMap;
 | 
												
													
														
															|  | import org.apache.commons.lang3.StringUtils;
 |  | import org.apache.commons.lang3.StringUtils;
 | 
												
													
														
															|  | 
 |  | import org.assertj.core.util.DateUtil;
 | 
												
													
														
															|  | import org.springframework.beans.factory.annotation.Autowired;
 |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
												
													
														
															|  | import org.springframework.jdbc.core.JdbcTemplate;
 |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
												
													
														
															|  | 
 |  | import org.springframework.security.core.parameters.P;
 | 
												
													
														
															|  | import org.springframework.stereotype.Service;
 |  | import org.springframework.stereotype.Service;
 | 
												
													
														
															|  | import org.springframework.transaction.annotation.Transactional;
 |  | import org.springframework.transaction.annotation.Transactional;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | import java.util.ArrayList;
 |  | 
 | 
												
													
														
															|  | import java.util.List;
 |  | 
 | 
												
													
														
															|  | import java.util.Map;
 |  | 
 | 
												
													
														
															|  | 
 |  | import javax.xml.crypto.Data;
 | 
												
													
														
															|  | 
 |  | import java.util.*;
 | 
												
													
														
															|  | 
 |  | import java.util.stream.Collectors;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * Created with IntelliJ IDEA.
 |  |  * Created with IntelliJ IDEA.
 | 
												
											
												
													
														
															|  | @ -55,6 +62,10 @@ public class ContactsService {
 | 
												
													
														
															|  |     private BaseOrgDao baseOrgDao;
 |  |     private BaseOrgDao baseOrgDao;
 | 
												
													
														
															|  |     @Autowired
 |  |     @Autowired
 | 
												
													
														
															|  |     private ServicePackageService servicePackageService;
 |  |     private ServicePackageService servicePackageService;
 | 
												
													
														
															|  | 
 |  |     @Autowired
 | 
												
													
														
															|  | 
 |  |     private PatientSosContactsDao sosContactsDao;
 | 
												
													
														
															|  | 
 |  |     @Autowired
 | 
												
													
														
															|  | 
 |  |     private NetworkCardService networkCardService;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     /**
 |  |     /**
 | 
												
													
														
															|  |      * 查找居民联系服务站
 |  |      * 查找居民联系服务站
 | 
												
											
												
													
														
															|  | @ -126,7 +137,7 @@ public class ContactsService {
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     /**
 |  |     /**
 | 
												
													
														
															|  |      * 设置一键联系人
 |  | 
 | 
												
													
														
															|  | 
 |  |      * 设置一键联系人  未使用
 | 
												
													
														
															|  |      * @param fid
 |  |      * @param fid
 | 
												
													
														
															|  |      * @param patient
 |  |      * @param patient
 | 
												
													
														
															|  |      */
 |  |      */
 | 
												
											
												
													
														
															|  | @ -202,4 +213,160 @@ public class ContactsService {
 | 
												
													
														
															|  |         return result;
 |  |         return result;
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * 获取居民紧急联系人
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     public List<PatientSosContactsDO> getPatientSosContacts(String patient){
 | 
												
													
														
															|  | 
 |  |         List<PatientSosContactsDO> sosContactsDOS = sosContactsDao.findByPatientAndDel(patient,1);
 | 
												
													
														
															|  | 
 |  |         for (PatientSosContactsDO tmp:sosContactsDOS){
 | 
												
													
														
															|  | 
 |  |             tmp.setRelationName(PatientFamilyMemberService.relations.get(tmp.getRelation()));
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         return sosContactsDOS;
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      *设置居民紧急联系人
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     public JSONObject updatePatientSosContacts(String patient,String num,Integer operType,String name,String relation) throws Exception {
 | 
												
													
														
															|  | 
 |  |         JSONObject result = new JSONObject();
 | 
												
													
														
															|  | 
 |  |         List<PatientSosContactsDO> list = sosContactsDao.findByPatientOrderByUpdateTimeDesc(patient);
 | 
												
													
														
															|  | 
 |  |         if (4==operType){//一个月只能删除一次
 | 
												
													
														
															|  | 
 |  |             if (list.size()>0){
 | 
												
													
														
															|  | 
 |  |                 Date updateTime = list.get(0).getUpdateTime();
 | 
												
													
														
															|  | 
 |  |                 if (null==updateTime){
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 }else {
 | 
												
													
														
															|  | 
 |  |                     Long timeDiffer = DateUtil.timeDifference(new Date(),updateTime);
 | 
												
													
														
															|  | 
 |  |                     if (timeDiffer<=(1000L*3600*24*30)){
 | 
												
													
														
															|  | 
 |  |                         Long day = (1000L*3600*24*30-timeDiffer)/(1000L*3600*24)+1;
 | 
												
													
														
															|  | 
 |  |                         result.put(ResponseContant.resultFlag,ResponseContant.fail);
 | 
												
													
														
															|  | 
 |  |                         result.put(ResponseContant.resultMsg,"您本月删除紧急联系人次数已使用,请"+day+"天后再试");
 | 
												
													
														
															|  | 
 |  |                         return result;
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |                 String[] numTmp = num.split("_");
 | 
												
													
														
															|  | 
 |  |                 String sql = " select pd.device_sn,wd.sim from  wlyy_patient_device pd inner join wlyy_devices wd  " +
 | 
												
													
														
															|  | 
 |  |                         "on pd.device_sn = wd.device_code and pd.del=0 and wd.sim is not null and pd.category_code in('7','4')  " +
 | 
												
													
														
															|  | 
 |  |                         "where pd.user='"+patient+"' ";
 | 
												
													
														
															|  | 
 |  |                 List<Map<String,Object>> sims = jdbcTemplate.queryForList(sql);
 | 
												
													
														
															|  | 
 |  |                 //修改
 | 
												
													
														
															|  | 
 |  |                 delSosNumber(sims,patient,numTmp);
 | 
												
													
														
															|  | 
 |  |             }else {
 | 
												
													
														
															|  | 
 |  |                 result.put(ResponseContant.resultFlag,ResponseContant.fail);
 | 
												
													
														
															|  | 
 |  |                 result.put(ResponseContant.resultMsg,"该用户已无紧急联系人");
 | 
												
													
														
															|  | 
 |  |                 return result;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         if (1==operType){//新增联系人
 | 
												
													
														
															|  | 
 |  |             list = sosContactsDao.findByPatientAndDel(patient,1);
 | 
												
													
														
															|  | 
 |  |             Integer seqid=1;
 | 
												
													
														
															|  | 
 |  |             if (list.size()>=2){
 | 
												
													
														
															|  | 
 |  |                 result.put(ResponseContant.resultFlag,ResponseContant.fail);
 | 
												
													
														
															|  | 
 |  |                 result.put(ResponseContant.resultMsg,"该用户紧急联系人已上线");
 | 
												
													
														
															|  | 
 |  |                 return result;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |             else {
 | 
												
													
														
															|  | 
 |  |                 if (list.size()>0){
 | 
												
													
														
															|  | 
 |  |                     seqid = list.get(0).getPhoneSeqid();
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |                 PatientSosContactsDO contactsDO = new PatientSosContactsDO();
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 list = sosContactsDao.findByPatientAndDel(patient,0);
 | 
												
													
														
															|  | 
 |  |                 if (list.size()>0) {
 | 
												
													
														
															|  | 
 |  |                     contactsDO =list.get(0);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |                 else {
 | 
												
													
														
															|  | 
 |  |                     contactsDO.setPhoneSeqid(1==seqid?2:1);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |                 contactsDO.setDel(1);
 | 
												
													
														
															|  | 
 |  |                 contactsDO.setPatient(patient);
 | 
												
													
														
															|  | 
 |  |                 contactsDO.setSosName(name);
 | 
												
													
														
															|  | 
 |  |                 contactsDO.setSosPhone(num);
 | 
												
													
														
															|  | 
 |  |                 contactsDO.setRelation(relation);
 | 
												
													
														
															|  | 
 |  |                 contactsDO.setSuccessFlag(0);
 | 
												
													
														
															|  | 
 |  |                 sosContactsDao.save(contactsDO);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         result.put(ResponseContant.resultFlag,ResponseContant.success);
 | 
												
													
														
															|  | 
 |  |         result.put(ResponseContant.resultMsg,"修改成功");
 | 
												
													
														
															|  | 
 |  |         return result;
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     public void delSosNumber(List<Map<String,Object>> sims,String patient,String[] numTmp) {
 | 
												
													
														
															|  | 
 |  |         String delSuccess = "";//白名单删除号码
 | 
												
													
														
															|  | 
 |  |         for (Map<String,Object> simTmp : sims) {
 | 
												
													
														
															|  | 
 |  |             String sim = simTmp.get("sim").toString();
 | 
												
													
														
															|  | 
 |  |             try {
 | 
												
													
														
															|  | 
 |  |                 JSONArray respons = networkCardService.getPatientContacts(sim, null, null, null);
 | 
												
													
														
															|  | 
 |  |                 List<Map<String, Object>> memberArr = null;
 | 
												
													
														
															|  | 
 |  |                 if (respons.size() > 0) {
 | 
												
													
														
															|  | 
 |  |                     respons.getJSONObject(0).getJSONArray("memVoiceWhiteList");
 | 
												
													
														
															|  | 
 |  |                     memberArr = (List<Map<String, Object>>) JSONArray.parse(respons.getJSONObject(0).getJSONArray("memVoiceWhiteList").toJSONString());
 | 
												
													
														
															|  | 
 |  |                     List<String> WhiteList = memberArr.stream().map(e -> e.get("whiteNumber").toString()).collect(Collectors.toList());
 | 
												
													
														
															|  | 
 |  |                     String del_num = "";
 | 
												
													
														
															|  | 
 |  |                     if (WhiteList.contains(numTmp[0])) {
 | 
												
													
														
															|  | 
 |  |                         del_num = numTmp[0];
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  |                     if (numTmp.length == 2) {
 | 
												
													
														
															|  | 
 |  |                         if (WhiteList.contains(numTmp[1])) {
 | 
												
													
														
															|  | 
 |  |                             if (StringUtils.isNotBlank(del_num)) {
 | 
												
													
														
															|  | 
 |  |                                 del_num += "_" + numTmp[1];
 | 
												
													
														
															|  | 
 |  |                             } else {
 | 
												
													
														
															|  | 
 |  |                                 del_num = numTmp[1];
 | 
												
													
														
															|  | 
 |  |                             }
 | 
												
													
														
															|  | 
 |  |                         }
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  |                     if (StringUtils.isNotBlank(del_num)) {
 | 
												
													
														
															|  | 
 |  | //                        String response = networkCardService.setPatientContacts(sim, null, null, "4", del_num, null);
 | 
												
													
														
															|  | 
 |  | //                        if (StringUtils.isNotBlank(response)) {
 | 
												
													
														
															|  | 
 |  |                         if (StringUtils.isNotBlank("111")) {//todo 对接时不做
 | 
												
													
														
															|  | 
 |  |                             String[] DelNums = del_num.split("_");
 | 
												
													
														
															|  | 
 |  |                             for (String tmp:DelNums){
 | 
												
													
														
															|  | 
 |  |                                 if (!delSuccess.contains(tmp)){//本次已删除过 不再作删除操作
 | 
												
													
														
															|  | 
 |  |                                     String deviceCode = simTmp.get("device_sn").toString();
 | 
												
													
														
															|  | 
 |  |                                     JSONObject aqgDeviceInfo = patientDeviceService.getAqgDeviceInfo(deviceCode);
 | 
												
													
														
															|  | 
 |  |                                     JSONArray aqgDeviceSosNum = aqgDeviceInfo.getJSONArray("sos_numbers");
 | 
												
													
														
															|  | 
 |  |                                     Integer numIndex=0;
 | 
												
													
														
															|  | 
 |  |                                     for (int i=0;i<aqgDeviceSosNum.size();i++){
 | 
												
													
														
															|  | 
 |  |                                         JSONObject aqgTmp = aqgDeviceSosNum.getJSONObject(i);
 | 
												
													
														
															|  | 
 |  |                                         String aqgNum = aqgTmp.getString("num");
 | 
												
													
														
															|  | 
 |  |                                         if (aqgNum.equals(tmp)){
 | 
												
													
														
															|  | 
 |  |                                             numIndex = aqgTmp.getInteger("seqid");
 | 
												
													
														
															|  | 
 |  |                                             break;
 | 
												
													
														
															|  | 
 |  |                                         }
 | 
												
													
														
															|  | 
 |  |                                     }
 | 
												
													
														
															|  | 
 |  |                                     if (numIndex!=0){
 | 
												
													
														
															|  | 
 |  |                                       JSONObject response2 =  patientDeviceService.updAqgDeviceSosInfo(deviceCode,numIndex+"",null,null,null,"1");
 | 
												
													
														
															|  | 
 |  |                                       if (response2.getBoolean("success")){
 | 
												
													
														
															|  | 
 |  |                                           delSuccess += "_"+tmp;
 | 
												
													
														
															|  | 
 |  |                                       }
 | 
												
													
														
															|  | 
 |  |                                     }
 | 
												
													
														
															|  | 
 |  |                                 }
 | 
												
													
														
															|  | 
 |  |                             }
 | 
												
													
														
															|  | 
 |  |                         }
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |             } catch (Exception e) {
 | 
												
													
														
															|  | 
 |  |                 e.printStackTrace();
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         //保存修改记录
 | 
												
													
														
															|  | 
 |  |         List<PatientSosContactsDO> modify = new ArrayList<>();
 | 
												
													
														
															|  | 
 |  |         for (String num:numTmp){
 | 
												
													
														
															|  | 
 |  |             if (StringUtils.isNotBlank(num)){
 | 
												
													
														
															|  | 
 |  |                 PatientSosContactsDO patientSosContactsDO = sosContactsDao.findByPatientAndSosPhone(patient,num);
 | 
												
													
														
															|  | 
 |  |                 patientSosContactsDO.setUpdateTime(new Date());
 | 
												
													
														
															|  | 
 |  |                 patientSosContactsDO.setDel(0);
 | 
												
													
														
															|  | 
 |  |                 if (delSuccess.contains(num)){
 | 
												
													
														
															|  | 
 |  |                     patientSosContactsDO.setSuccessFlag(2);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |                 else {
 | 
												
													
														
															|  | 
 |  |                     patientSosContactsDO.setSuccessFlag(-2);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |                 modify.add(patientSosContactsDO);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         sosContactsDao.save(modify);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | }
 |  | }
 |