|  | @ -1,24 +1,242 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.hospital.prescription.service.entrance;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.dict.dao.DictHospitalDeptDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorHospitalDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorRoleDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.dict.DictHospitalDeptDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorRoleDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.hospital.mapping.DoctorMappingDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.mapping.dao.DoctorMappingDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.IdCardUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.utils.hibernate.HibenateUtils;
 | 
	
		
			
				|  |  | import com.yihu.utils.network.HttpResponse;
 | 
	
		
			
				|  |  | import com.yihu.utils.network.HttpUtils;
 | 
	
		
			
				|  |  | import com.yihu.utils.security.MD5;
 | 
	
		
			
				|  |  | import javafx.scene.DepthTest;
 | 
	
		
			
				|  |  | import org.apache.commons.collections.map.HashedMap;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.transaction.Transactional;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | import java.util.Random;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * 眼科医院专用service
 | 
	
		
			
				|  |  |  * Created by Trick on 2020/1/19.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | @Transactional
 | 
	
		
			
				|  |  | public class YkyyEntranceService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static Logger logger = LoggerFactory.getLogger(YkyyEntranceService.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private final static String url="http://192.168.20.55:10023/ykyy/createSQLQuery";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private final static String orgCode ="350211A5004";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private final static String orgName ="厦门大学附属厦门眼科中心";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DictHospitalDeptDao hospitalDeptDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorHospitalDao baseDoctorHospitalDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorRoleDao baseDoctorRoleDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DoctorMappingDao doctorMappingDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorDao baseDoctorDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HibenateUtils hibenateUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public List<Map<String, Object>> createSQLQuery(String sql, Map<String, Object> params, Integer page, Integer size){
 | 
	
		
			
				|  |  |         return hibenateUtils.createSQLQuery( sql,  params,  page,  size);
 | 
	
		
			
				|  |  |         return hibenateUtils.createSQLQuery(sql,params,page,size);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 同步科室信息
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String updateYkyyDept()throws Exception{
 | 
	
		
			
				|  |  |         String sql ="select code AS \"code\",name as \"name\",consultdeptflag as \"consultDeptFlag\" from V_HLW_KSXX";
 | 
	
		
			
				|  |  |         Map<String,Object> params = new HashedMap();
 | 
	
		
			
				|  |  |         params.put("sql",sql);
 | 
	
		
			
				|  |  |         logger.info("updateYkyyDept:"+sql);
 | 
	
		
			
				|  |  |         HttpResponse response = HttpUtils.doGet(url,params);
 | 
	
		
			
				|  |  |         String content = response.getContent();
 | 
	
		
			
				|  |  |         logger.info("response:"+content);
 | 
	
		
			
				|  |  |         JSONObject rs = JSON.parseObject(content);
 | 
	
		
			
				|  |  |         Integer status = rs.getInteger("status");
 | 
	
		
			
				|  |  |         if(status!=null&&status == 200){
 | 
	
		
			
				|  |  |             JSONArray array = rs.getJSONArray("detailModelList");
 | 
	
		
			
				|  |  |             logger.info("dept size:"+array.size());
 | 
	
		
			
				|  |  |             if(array!=null&&array.size()>0){
 | 
	
		
			
				|  |  |                 for(int i=0;i<array.size();i++){
 | 
	
		
			
				|  |  |                     JSONObject deptjson = array.getJSONObject(i);
 | 
	
		
			
				|  |  |                     String code = deptjson.getString("code");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                     List<DictHospitalDeptDO> list = hospitalDeptDao.findByOrgCodeAndCode(orgCode,code);
 | 
	
		
			
				|  |  |                     if(list!=null&&list.size()>0){
 | 
	
		
			
				|  |  |                         DictHospitalDeptDO deptDO = list.get(0);
 | 
	
		
			
				|  |  |                         deptDO.setName(deptjson.getString("name"));
 | 
	
		
			
				|  |  |                         deptDO.setConsultDeptFlag(deptjson.getString("consultdeptflag"));
 | 
	
		
			
				|  |  |                         hospitalDeptDao.save(deptDO);
 | 
	
		
			
				|  |  |                     }else {
 | 
	
		
			
				|  |  |                         //新增
 | 
	
		
			
				|  |  |                         DictHospitalDeptDO deptDO = new DictHospitalDeptDO();
 | 
	
		
			
				|  |  |                         deptDO.setOrgCode(orgCode);
 | 
	
		
			
				|  |  |                         deptDO.setCode(code);
 | 
	
		
			
				|  |  |                         deptDO.setName(deptjson.getString("name"));
 | 
	
		
			
				|  |  |                         deptDO.setConsultDeptFlag(deptjson.getString("consultdeptflag"));
 | 
	
		
			
				|  |  |                         deptDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |                         hospitalDeptDao.save(deptDO);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return "success";
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String updateYkyyDoctor()throws Exception{
 | 
	
		
			
				|  |  |         String sql ="select y.code AS \"code\",y.name AS \"name\",y.idCard AS \"idcard\",y.jobtitlecode AS \"jobTitleCode\",y.jobTitleName AS \"jobTitleName\",y.dept AS \"dept\",y.deptName AS \"deptName\" from v_Hlw_Ysxx y where y.idcard is not null";
 | 
	
		
			
				|  |  |         Map<String,Object> params = new HashedMap();
 | 
	
		
			
				|  |  |         params.put("sql",sql);
 | 
	
		
			
				|  |  |         HttpResponse response = HttpUtils.doGet(url,params);
 | 
	
		
			
				|  |  |         String content = response.getContent();
 | 
	
		
			
				|  |  |         logger.info("response:"+content);
 | 
	
		
			
				|  |  |         JSONObject rs = JSON.parseObject(content);
 | 
	
		
			
				|  |  |         Integer status = rs.getInteger("status");
 | 
	
		
			
				|  |  |         if(status!=null&&status == 200){
 | 
	
		
			
				|  |  |             JSONArray array = rs.getJSONArray("detailModelList");
 | 
	
		
			
				|  |  |             logger.info("doctor size:"+array.size());
 | 
	
		
			
				|  |  |             if(array!=null&&array.size()>0) {
 | 
	
		
			
				|  |  |                 for (int i = 0; i < array.size(); i++) {
 | 
	
		
			
				|  |  |                     JSONObject doctorJson = array.getJSONObject(i);
 | 
	
		
			
				|  |  |                     String idCard = doctorJson.getString("idCard");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                     //过滤身份证脏数据
 | 
	
		
			
				|  |  |                     if(idCard.length()>=15){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         List<BaseDoctorDO> doctorDOs = baseDoctorDao.findByIdcard(idCard);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         if(doctorDOs!=null&&doctorDOs.size()>0){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             BaseDoctorDO doctor = doctorDOs.get(0);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             doctor.setJobTitleCode(doctorJson.getString("jobtitlecode"));
 | 
	
		
			
				|  |  |                             doctor.setJobTitleName(doctorJson.getString("jobTitleName"));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             BaseDoctorDO temp = baseDoctorDao.save(doctor);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             List<BaseDoctorHospitalDO> hospitalDOs = baseDoctorHospitalDao.findByOrgCodeAndDeptCodeAndDoctorCode(orgCode,doctorJson.getString("dept"),doctor.getId());
 | 
	
		
			
				|  |  |                             if(hospitalDOs!=null&&hospitalDOs.size()>0){
 | 
	
		
			
				|  |  |                                 //机构信息部门信息
 | 
	
		
			
				|  |  |                                 BaseDoctorHospitalDO hospitalDO = hospitalDOs.get(0);
 | 
	
		
			
				|  |  |                                 hospitalDO.setDeptCode(doctorJson.getString("dept"));
 | 
	
		
			
				|  |  |                                 hospitalDO.setDeptName(doctorJson.getString("deptName"));
 | 
	
		
			
				|  |  |                                 hospitalDO.setDel("1");
 | 
	
		
			
				|  |  |                                 baseDoctorHospitalDao.save(hospitalDO);
 | 
	
		
			
				|  |  |                             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         }else{
 | 
	
		
			
				|  |  |                             //新增医生
 | 
	
		
			
				|  |  |                             BaseDoctorDO doctor = new BaseDoctorDO();
 | 
	
		
			
				|  |  |                             doctor.setName(doctorJson.getString("name"));
 | 
	
		
			
				|  |  |                             doctor.setIdcard(idCard);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             doctor.setSex(Integer.parseInt(IdCardUtil.getSexForIdcard_new(idCard)));
 | 
	
		
			
				|  |  |                             doctor.setBirthday(IdCardUtil.getBirthdayForIdcard(idCard));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             doctor.setProvinceCode("350000");
 | 
	
		
			
				|  |  |                             doctor.setProvinceName("福建省");
 | 
	
		
			
				|  |  |                             doctor.setTownCode("350203");
 | 
	
		
			
				|  |  |                             doctor.setTownName("思明区");
 | 
	
		
			
				|  |  |                             doctor.setCityCode("350200");
 | 
	
		
			
				|  |  |                             doctor.setCityName("厦门市");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             //认证信息设置
 | 
	
		
			
				|  |  |                             String salt = randomString(5);
 | 
	
		
			
				|  |  |                             String pw = idCard.substring(idCard.length() - 6);
 | 
	
		
			
				|  |  |                             doctor.setPassword(MD5.md5Hex(pw + "{" + salt + "}"));
 | 
	
		
			
				|  |  |                             doctor.setDel("1");
 | 
	
		
			
				|  |  |                             doctor.setEnabled(1);
 | 
	
		
			
				|  |  |                             doctor.setLocked(0);
 | 
	
		
			
				|  |  |                             doctor.setCreateTime(new Date());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             doctor.setJobTitleCode(doctorJson.getString("jobtitlecode"));
 | 
	
		
			
				|  |  |                             doctor.setJobTitleName(doctorJson.getString("jobTitleName"));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             BaseDoctorDO temp = baseDoctorDao.save(doctor);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             //机构信息部门信息
 | 
	
		
			
				|  |  |                             BaseDoctorHospitalDO hospitalDO = new BaseDoctorHospitalDO();
 | 
	
		
			
				|  |  |                             hospitalDO.setDoctorCode(temp.getId());
 | 
	
		
			
				|  |  |                             hospitalDO.setOrgCode(orgCode);
 | 
	
		
			
				|  |  |                             hospitalDO.setOrgName(orgName);
 | 
	
		
			
				|  |  |                             hospitalDO.setDeptCode(doctorJson.getString("dept"));
 | 
	
		
			
				|  |  |                             hospitalDO.setDeptName(doctorJson.getString("deptName"));
 | 
	
		
			
				|  |  |                             hospitalDO.setDel("1");
 | 
	
		
			
				|  |  |                             baseDoctorHospitalDao.save(hospitalDO);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             //保存角色
 | 
	
		
			
				|  |  |                             BaseDoctorRoleDO role = new BaseDoctorRoleDO();
 | 
	
		
			
				|  |  |                             role.setDoctorCode(temp.getId());
 | 
	
		
			
				|  |  |                             role.setRoleCode("specialist");
 | 
	
		
			
				|  |  |                             baseDoctorRoleDao.save(role);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             //保存mapping
 | 
	
		
			
				|  |  |                             DoctorMappingDO mappingDO = new DoctorMappingDO();
 | 
	
		
			
				|  |  |                             mappingDO.setIdcard(idCard);
 | 
	
		
			
				|  |  |                             mappingDO.setDoctor(temp.getId());
 | 
	
		
			
				|  |  |                             mappingDO.setDoctorName(temp.getName());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             mappingDO.setOrgCode(orgCode);
 | 
	
		
			
				|  |  |                             mappingDO.setOrgName(orgName);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             mappingDO.setMappingCode(doctorJson.getString("code"));
 | 
	
		
			
				|  |  |                             mappingDO.setMappingName(temp.getName());
 | 
	
		
			
				|  |  |                             mappingDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                             doctorMappingDao.save(mappingDO);
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return "success";
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String randomString(int length) {
 | 
	
		
			
				|  |  |         String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 | 
	
		
			
				|  |  |         StringBuffer buffer = new StringBuffer();
 | 
	
		
			
				|  |  |         Random random = new Random();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         for(int i = 0; i < length; ++i) {
 | 
	
		
			
				|  |  |             int pos = random.nextInt(str.length());
 | 
	
		
			
				|  |  |             buffer.append(str.charAt(pos));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return buffer.toString();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |