|  | @ -0,0 +1,175 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.contacts;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.contacts.BasePatientContactsOrgDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.PatientDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.family.PatientFamilyMemberDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.service.device.PatientDeviceService;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.service.family.PatientFamilyMemberService;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.service.sign.ServicePackageService;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.org.BaseOrgDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.patient.BasePatientFamilyMemberDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.contacts.BasePatientContactsOrgDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DevicePatientDevice;
 | 
	
		
			
				|  |  | import com.yihu.jw.org.dao.BaseOrgDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.LatitudeUtils;
 | 
	
		
			
				|  |  | import org.apache.commons.collections.map.HashedMap;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created with IntelliJ IDEA.
 | 
	
		
			
				|  |  |  *
 | 
	
		
			
				|  |  |  * @Author: yeshijie
 | 
	
		
			
				|  |  |  * @Date: 2021/5/7
 | 
	
		
			
				|  |  |  * @Description:
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class ContactsService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientFamilyMemberDao memberDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientDeviceDao patientDeviceDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientDeviceService patientDeviceService;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientDao basePatientDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientFamilyMemberService patientFamilyMemberService;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientContactsOrgDao contactsOrgDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseOrgDao baseOrgDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private ServicePackageService servicePackageService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 查找居民联系服务站
 | 
	
		
			
				|  |  |      * @param patient
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public BaseOrgDO getContactsOrg(String patient){
 | 
	
		
			
				|  |  |         BasePatientContactsOrgDO contactsOrgDO = contactsOrgDao.findByPatient(patient);
 | 
	
		
			
				|  |  |         if(contactsOrgDO == null){
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return baseOrgDao.findByCode(contactsOrgDO.getOrgCode());
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 设置居民联系服务站
 | 
	
		
			
				|  |  |      * @param patient
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void setContactsOrg(String patient,String orgCode){
 | 
	
		
			
				|  |  |         BasePatientContactsOrgDO contactsOrgDO = contactsOrgDao.findByPatient(patient);
 | 
	
		
			
				|  |  |         if(contactsOrgDO == null){
 | 
	
		
			
				|  |  |             contactsOrgDO = new BasePatientContactsOrgDO();
 | 
	
		
			
				|  |  |             contactsOrgDO.setPatient(patient);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         contactsOrgDO.setOrgCode(orgCode);
 | 
	
		
			
				|  |  |         contactsOrgDao.save(contactsOrgDO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取居民的联系人
 | 
	
		
			
				|  |  |      * @param patient 居民
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONArray getPatientContacts(String patient, Integer isContacts) {
 | 
	
		
			
				|  |  |         JSONArray resultArray = new JSONArray();
 | 
	
		
			
				|  |  |         String sql = "select *,t1.id fid " +
 | 
	
		
			
				|  |  |                 " from " +
 | 
	
		
			
				|  |  |                 "    base_patient_family_member t1, " +
 | 
	
		
			
				|  |  |                 "    base_patient t2 " +
 | 
	
		
			
				|  |  |                 " where " +
 | 
	
		
			
				|  |  |                 "    t2.id in (select family_member from base_patient_family_member where patient = ? ) " +
 | 
	
		
			
				|  |  |                 "    and t1.patient = ? " +
 | 
	
		
			
				|  |  |                 "    and t1.family_member = t2.id ";
 | 
	
		
			
				|  |  |         if(isContacts == 1){
 | 
	
		
			
				|  |  |             sql += " and t1.is_contacts = "+ isContacts;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, new Object[]{patient, patient});
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if (result != null && result.size() > 0) {
 | 
	
		
			
				|  |  |             for (Map<String, Object> map : result) {
 | 
	
		
			
				|  |  |                 JSONObject obj = new JSONObject();
 | 
	
		
			
				|  |  |                 obj.put("id", map.get("id"));
 | 
	
		
			
				|  |  |                 obj.put("fid", map.get("fid"));
 | 
	
		
			
				|  |  |                 obj.put("name", map.get("name"));
 | 
	
		
			
				|  |  |                 obj.put("isContacts", map.get("is_contacts"));
 | 
	
		
			
				|  |  |                 obj.put("sex", map.get("sex"));
 | 
	
		
			
				|  |  |                 obj.put("birthday", map.get("birthday"));
 | 
	
		
			
				|  |  |                 obj.put("idcard", StringUtils.isEmpty(String.valueOf(map.get("idcard"))) ? "" : map.get("idcard").toString());
 | 
	
		
			
				|  |  |                 obj.put("photo", map.get("photo"));
 | 
	
		
			
				|  |  |                 obj.put("mobile", map.get("mobile"));
 | 
	
		
			
				|  |  |                 obj.put("address", StringUtils.isEmpty(String.valueOf(map.get("address"))) ? "" : map.get("address"));
 | 
	
		
			
				|  |  |                 obj.put("familyRelation", map.get("family_relation"));
 | 
	
		
			
				|  |  |                 obj.put("familyRelationName", PatientFamilyMemberService.relations.get(map.get("family_relation")));
 | 
	
		
			
				|  |  |                 resultArray.add(obj);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return resultArray;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 设置一键联系人
 | 
	
		
			
				|  |  |      * @param fid
 | 
	
		
			
				|  |  |      * @param patient
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |     public void setContacts(String fid,String patient){
 | 
	
		
			
				|  |  |         BasePatientFamilyMemberDO familyMemberDO = memberDao.findOne(fid);
 | 
	
		
			
				|  |  |         BasePatientDO patientDO = basePatientDao.findById(patient);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //设置爱牵挂智能设备sos号码
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             List<DevicePatientDevice> patientDeviceList = patientDeviceDao.findByPatient(patient);
 | 
	
		
			
				|  |  |             for(DevicePatientDevice patientDevice : patientDeviceList){
 | 
	
		
			
				|  |  |                 if("4".equals(patientDevice.getCategoryCode())||"7".equals(patientDevice.getCategoryCode())){
 | 
	
		
			
				|  |  |                     patientDeviceService.updAqgDeviceSosInfo(patientDevice.getDeviceSn(),"1",
 | 
	
		
			
				|  |  |                             PatientFamilyMemberService.relations.get(familyMemberDO.getFamilyRelation()),patientDO.getMobile(),"1",null);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         memberDao.updateContactsByPatient(patient);
 | 
	
		
			
				|  |  |         familyMemberDO.setIsContacts(1);
 | 
	
		
			
				|  |  |         memberDao.save(familyMemberDO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取居民签约机构
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<Map<String,Object>> findSignOrg(String patient,String lng,String lat){
 | 
	
		
			
				|  |  |         List<Map<String,Object>> result = new ArrayList<>();
 | 
	
		
			
				|  |  |         List<BaseOrgDO> orgDOList = servicePackageService.findSignOrg(patient);
 | 
	
		
			
				|  |  |         for (BaseOrgDO orgDO : orgDOList){
 | 
	
		
			
				|  |  |             Map<String,Object> map = new HashedMap();
 | 
	
		
			
				|  |  |             map.put("distance", LatitudeUtils.getDistance(lat,lng,orgDO.getLatitude(),orgDO.getLongitude()));
 | 
	
		
			
				|  |  |             map.put("id",orgDO.getId());
 | 
	
		
			
				|  |  |             map.put("code",orgDO.getCode());
 | 
	
		
			
				|  |  |             map.put("name",orgDO.getName());
 | 
	
		
			
				|  |  |             map.put("photo",orgDO.getPhoto());
 | 
	
		
			
				|  |  |             map.put("mobile",orgDO.getMobile());
 | 
	
		
			
				|  |  |             map.put("address",orgDO.getAddress());
 | 
	
		
			
				|  |  |             result.add(map);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |