Index: ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/ljsrmyy/LyUseRecordMainResult.java =================================================================== diff -u --- ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/ljsrmyy/LyUseRecordMainResult.java (revision 0) +++ ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/ljsrmyy/LyUseRecordMainResult.java (revision 17646) @@ -0,0 +1,344 @@ +package com.forgon.disinfectsystem.useRecord.hrepPath.ljsrmyy; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Index; +import javax.persistence.Table; + +/** + * 手麻使用记录调用处理结果 + * @author shuyongfu + * + */ +@Entity +@Table(name="LyUseRecordMainResult",indexes={@Index(columnList="id",name="id_index") +,@Index(columnList="useDate",name="useDate_index") +,@Index(columnList="hospitalNum",name="hospitalNum_index") +,@Index(columnList="BarCode",name="barCode_index") +,@Index(columnList="status",name="status_index")}) +public class LyUseRecordMainResult { + + /** + * 流水号 + */ + private Long id; + + /** + * 日期 + */ + private Date useDate; + + /** + * 诊疗号 + */ + private String treatmentNum; + + /** + * 住院号 + */ + private String hospitalNum; + + /** + * 病人姓名 + */ + private String patientName; + + /** + * 病人年龄 + */ + private String patientAge; + + /** + * 病人性别 + */ + private String patientSex; + + /** + * 病人身份证号 + */ + private String patientIDCard; + + /** + * 医生姓名 + */ + private String doctorName; + + /** + * 手术名称 + */ + private String operation; + + /** + * 手术间 + */ + private String operationRoom; + + /** + * 病区 + */ + private String patientArea; + + /** + * 病室 + */ + private String roomNumber; + + /** + * 床位 + */ + private String bedNumber; + + /** + * 科室名称 + */ + private String depart; + + /** + * 洗手护士 + */ + private String washHandNurse; + + /** + * 巡回护士 + */ + private String circuitNurse; + + /** + * 特殊感染类型 + */ + private String specialInfection; + + /** + * 手术状态(NOW_STATE) + */ + private String nowState; + + /** + * 器械包名称(QXName) + */ + private String qxName; + + /** + * 器械包条码(BarCode) + */ + private String barCode; + + /** + * 科室编码(DepartCoding) + */ + private String departCoding; + + /** + * 状态(初始为空,成功是为1,不成功则为0) + */ + private Integer status; + + public static Integer STATUS_SUCC = 1; + public static Integer STATUS_FAIL = 0; + + /** + * 最后更新时间 + */ + private Date lastUpdateTime; + + @Id + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Date getUseDate() { + return useDate; + } + + public void setUseDate(Date useDate) { + this.useDate = useDate; + } + + public String getTreatmentNum() { + return treatmentNum; + } + + public void setTreatmentNum(String treatmentNum) { + this.treatmentNum = treatmentNum; + } + + public String getHospitalNum() { + return hospitalNum; + } + + public void setHospitalNum(String hospitalNum) { + this.hospitalNum = hospitalNum; + } + + public String getPatientName() { + return patientName; + } + + public void setPatientName(String patientName) { + this.patientName = patientName; + } + + public String getPatientAge() { + return patientAge; + } + + public void setPatientAge(String patientAge) { + this.patientAge = patientAge; + } + + public String getPatientSex() { + return patientSex; + } + + public void setPatientSex(String patientSex) { + this.patientSex = patientSex; + } + + public String getPatientIDCard() { + return patientIDCard; + } + + public void setPatientIDCard(String patientIDCard) { + this.patientIDCard = patientIDCard; + } + + public String getDoctorName() { + return doctorName; + } + + public void setDoctorName(String doctorName) { + this.doctorName = doctorName; + } + + public String getOperation() { + return operation; + } + + public void setOperation(String operation) { + this.operation = operation; + } + + public String getOperationRoom() { + return operationRoom; + } + + public void setOperationRoom(String operationRoom) { + this.operationRoom = operationRoom; + } + + public String getPatientArea() { + return patientArea; + } + + public void setPatientArea(String patientArea) { + this.patientArea = patientArea; + } + + public String getRoomNumber() { + return roomNumber; + } + + public void setRoomNumber(String roomNumber) { + this.roomNumber = roomNumber; + } + + public String getBedNumber() { + return bedNumber; + } + + public void setBedNumber(String bedNumber) { + this.bedNumber = bedNumber; + } + + public String getDepart() { + return depart; + } + + public void setDepart(String depart) { + this.depart = depart; + } + + public String getWashHandNurse() { + return washHandNurse; + } + + public void setWashHandNurse(String washHandNurse) { + this.washHandNurse = washHandNurse; + } + + public String getCircuitNurse() { + return circuitNurse; + } + + public void setCircuitNurse(String circuitNurse) { + this.circuitNurse = circuitNurse; + } + + public String getSpecialInfection() { + return specialInfection; + } + + public void setSpecialInfection(String specialInfection) { + this.specialInfection = specialInfection; + } + + @Column(name="NOW_STATE") + public String getNowState() { + return nowState; + } + + public void setNowState(String nowState) { + this.nowState = nowState; + } + + @Column(name="QXName") + public String getQxName() { + return qxName; + } + + public void setQxName(String qxName) { + this.qxName = qxName; + } + + @Column(name="BarCode") + public String getBarCode() { + return barCode; + } + + public void setBarCode(String barCode) { + this.barCode = barCode; + } + + @Column(name="DepartCoding") + public String getDepartCoding() { + return departCoding; + } + + public void setDepartCoding(String departCoding) { + this.departCoding = departCoding; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public Date getLastUpdateTime() { + return lastUpdateTime; + } + + public void setLastUpdateTime(Date lastUpdateTime) { + this.lastUpdateTime = lastUpdateTime; + } + +} Index: ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/zsly/HerpPathManagerImpl.java =================================================================== diff -u --- ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/zsly/HerpPathManagerImpl.java (revision 0) +++ ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/zsly/HerpPathManagerImpl.java (revision 17646) @@ -0,0 +1,455 @@ +package com.forgon.disinfectsystem.useRecord.hrepPath.zsly; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import net.sf.json.JSONObject; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.collections4.Predicate; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.jfree.chart.axis.DateAxis; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; + +import com.forgon.Constants; +import com.forgon.databaseadapter.service.DateQueryAdapter; +import com.forgon.directory.acegi.tools.AcegiHelper; +import com.forgon.directory.model.OrgUnit; +import com.forgon.directory.model.BarcodeDevice; +import com.forgon.directory.vo.LoginUserData; +import com.forgon.disinfectsystem.barcode.service.BarcodeManager; +import com.forgon.disinfectsystem.basedatamanager.supplyroomconfig.service.SupplyRoomConfigManager; +import com.forgon.disinfectsystem.common.CssdUtils; +import com.forgon.disinfectsystem.datasynchronization.model.PatientInfoVO; +import com.forgon.disinfectsystem.datasynchronization.service.DataSynchronizationManager; +import com.forgon.disinfectsystem.entity.assestmanagement.ExpensiveDisposablegoods; +import com.forgon.disinfectsystem.entity.basedatamanager.materialdefinition.MaterialDefinition; +import com.forgon.disinfectsystem.entity.basedatamanager.supplyroomconfig.SupplyRoomConfig; +import com.forgon.disinfectsystem.entity.basedatamanager.toussedefinition.TousseDefinition; +import com.forgon.disinfectsystem.entity.basedatamanager.toussedefinition.TousseInstance; +import com.forgon.disinfectsystem.entity.basedatamanager.toussedefinition.TousseInstanceUseRecord; +import com.forgon.disinfectsystem.entity.departmentstock.DepartmentStock; +import com.forgon.disinfectsystem.entity.foreigntousseapplication.ForeignTousseApplication; +import com.forgon.disinfectsystem.entity.idcarddefinition.IDCardDefinition; +import com.forgon.disinfectsystem.entity.idcardinstance.IDCardInstance; +import com.forgon.disinfectsystem.entity.invoicemanager.Invoice; +import com.forgon.disinfectsystem.entity.invoicemanager.InvoicePlan; +import com.forgon.disinfectsystem.entity.recyclingapplication.RecyclingApplication; +import com.forgon.disinfectsystem.entity.useRecord.UseDiposableGoodsItem; +import com.forgon.disinfectsystem.entity.useRecord.UseRecord; +import com.forgon.disinfectsystem.idcardinstance.service.IDCardInstanceManager; +import com.forgon.disinfectsystem.recyclingapplication.service.RecyclingApplicationManager; +import com.forgon.disinfectsystem.stockmanage.departmentstock.service.DepartmentStockManager; +import com.forgon.disinfectsystem.tousse.toussedefinition.service.TousseInstanceManager; +import com.forgon.disinfectsystem.tousseitem.service.TousseItemManager; +import com.forgon.disinfectsystem.useRecord.hrepPath.CommonHerpPathManagerImpl; +import com.forgon.disinfectsystem.useRecord.hrepPath.HerpPathManager; +import com.forgon.disinfectsystem.useRecord.service.UseRecordManager; +import com.forgon.disinfectsystem.vo.UseMaterialGoodsItem; +import com.forgon.tools.MathTools; +import com.forgon.tools.db.DatabaseUtil; +import com.forgon.tools.hibernate.ObjectDao; +import com.forgon.tools.string.StringTools; + +/** + * 与手麻系统接口对接的实现类 + * @author shuyongfu + * @since date:2017-04-01 + */ +public class HerpPathManagerImpl extends CommonHerpPathManagerImpl implements HerpPathManager { + +private Logger logger = Logger.getLogger(this.getClass()); + + private ObjectDao objectDao; + + private TousseInstanceManager tousseInstanceManager; + + private JdbcTemplate pathJdbcTemplate; + + private UseRecordManager useRecordManager; + + private DataSynchronizationManager dataSynchronizationManager; + + public void setTousseInstanceManager( + TousseInstanceManager tousseInstanceManager) { + this.tousseInstanceManager = tousseInstanceManager; + } + + public void setObjectDao(ObjectDao objectDao) { + this.objectDao = objectDao; + } + + public void setPathJdbcTemplate(JdbcTemplate pathJdbcTemplate) { + this.pathJdbcTemplate = pathJdbcTemplate; + } + + public void setUseRecordManager(UseRecordManager useRecordManager) { + this.useRecordManager = useRecordManager; + } + + public void setDataSynchronizationManager( + DataSynchronizationManager dataSynchronizationManager) { + this.dataSynchronizationManager = dataSynchronizationManager; + } + + /** + * 根据病历号及申请人提交回调 + * @param patientNumber 病历号 + * @param applicant 申请人 + * @return + */ + public String submitUseRecordByPatientNumberAndApplicant(String patientNumber,String applicant){ + logger.info("更新该病历的器械包使用记录接口:patientNumber=" + patientNumber+",applicant="+applicant); + final JSONObject jsonObject = new JSONObject(); + final List succReceiveIdList = new ArrayList();//回调执行成功的医嘱编号 + final List failReceiveIdList = new ArrayList();//回调执行失败的医嘱编号 + try { + if(StringUtils.isNotBlank(patientNumber) && StringUtils.isNotBlank(applicant)){ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + Date now = new Date(); + String dateStr = sdf.format(now); + String sql = "select receive_id,bar_code,exec_dept_code,exec_dept_name,doc_name,patient_name,hospital_no,convert(varchar(100), charge_date ,20) charge_date from sm_mate_advice t " + + "where advice_flag=5 and hospital_no='" + patientNumber + + "' and bar_code is not null and bar_code <> '' and charge_date between convert(datetime , '"+dateStr+" 00:00:00') and convert(datetime , '"+dateStr+" 23:59:59')" + + " order by exec_dept_code,exec_dept_name"; + + //根据住院号查询 + Map map = pathJdbcTemplate + .query(sql, + new ResultSetExtractor>() { + boolean success = true; + StringBuffer message = new StringBuffer(""); + @Override + public Map extractData(ResultSet rs) + throws SQLException, DataAccessException { + Map innerMap = new HashMap(); + while (rs.next()) { + String barCode = rs.getString("bar_code"); + if(StringUtils.isBlank(barCode)){ + continue; + } + TousseInstance tousseInstance = tousseInstanceManager.getTousseInstanceByBarcode(barCode.trim()); + //1.如果bar_code对应到器械包条码不存在或者器械包条码存在但已关联了使用记录 + if(tousseInstance == null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统未找到。"); + failReceiveIdList.add(rs.getString("receive_id")); + continue; + } + /*if(tousseInstance.getUseRecord_id() != null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统已生成使用记录。"); + //已生成使用记录的器械包不更新为失败 + continue; + }*/ + innerMap.put(rs.getString("receive_id"), new String[] { + rs.getString("exec_dept_code"), rs.getString("exec_dept_name"), + rs.getString("doc_name"),rs.getString("patient_name"), + rs.getString("bar_code"),rs.getString("hospital_no"),rs.getString("charge_date")}); + } + jsonObject.put("return", success ? "1" : "0"); + jsonObject.put("errMsg", message.toString()); + return innerMap; + } + }); + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + generateUseRecordAndCreateRecyclingApplication(map , applicant , succReceiveIdList); + }/*else{ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "未找到相关医嘱信息或医嘱信息表的条码关联不到消毒供应系统的器械包条码"); + }*/ + }else if(StringUtils.isBlank(patientNumber)){ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "住院号参数为空"); + }else if(StringUtils.isBlank(applicant)){ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "申请人参数为空"); + } + } catch (Exception e) { + if(e.getMessage().indexOf("Could not get JDBC Connection") == -1){ + logger.error("根据住院号更新该病历的器械包使用记录报错:" + e); + failReceiveIdList.addAll(succReceiveIdList);//如果整体调用失败,则成功的记录也追加到失败的集合中 + }else{ + logger.error("herp系统path库连接获取失败导致根据住院号更新该病历的器械包使用记录报错:" + e); + } + try{ + if(e.getMessage().indexOf("Could not get JDBC Connection") == -1){ + updateRecordStatus(false , failReceiveIdList); + } + }catch(Exception e1){ + logger.error("更新herp path库该病历的医嘱信息记录失败时报错:" + e1); + } + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "调用回调接口过程中出现异常."); + logger.error("更新该病历的器械包使用记录异常", e); + } + return jsonObject.toString(); + } + + + /** + * 回调记录更新为成功或失败(仅中山六院使用) + * @param success 是否成功或失败 + * @param receiveIdList 医嘱信息编号集合 + */ + @Override + public void updateRecordStatus(boolean success , List receiveIdList){ + String sql = "update sm_mate_advice set update_time=getDate(), status = " + + (success ? 1 : 0) + " where 1=2"; + if(CollectionUtils.isNotEmpty(receiveIdList)){ + String receiveIds = ""; + for (int i = 0; i < receiveIdList.size(); i++) { + receiveIds += "'" + receiveIdList.get(i) + "'"; + if(i < receiveIdList.size() - 1){ + receiveIds += ","; + } + } + sql += " or receive_id in (" + receiveIds + ")"; + } + try { + pathJdbcTemplate.update(sql); + } catch (Exception e) { + e.printStackTrace(); + logger.error(e); + } + } + + /** + * 中六的herp回写接口调用失败的定时重新调用处理 + */ + @Override + public void timingSubmitUseRecord(){ + String applicant = "系统回写"; + final JSONObject jsonObject = new JSONObject(); + final List succReceiveIdList = new ArrayList();//回调执行成功的医嘱编号 + final List failReceiveIdList = new ArrayList();//回调执行失败的医嘱编号 + try { + String sql = "select receive_id,bar_code,exec_dept_code,exec_dept_name,doc_name,patient_name,hospital_no,convert(varchar(100), charge_date ,20) charge_date from sm_mate_advice t " + + "where advice_flag=5 and status = 0 " + + " and bar_code is not null and bar_code <> ''" + + " order by exec_dept_code,exec_dept_name"; + + //根据住院号查询 + Map map = pathJdbcTemplate + .query(sql, + new ResultSetExtractor>() { + boolean success = true; + StringBuffer message = new StringBuffer(""); + @Override + public Map extractData(ResultSet rs) + throws SQLException, DataAccessException { + Map innerMap = new HashMap(); + while (rs.next()) { + String barCode = rs.getString("bar_code"); + if(StringUtils.isBlank(barCode)){ + continue; + } + TousseInstance tousseInstance = tousseInstanceManager.getTousseInstanceByBarcode(barCode.trim()); + //1.如果bar_code对应到器械包条码不存在或者器械包条码存在但已关联了使用记录 + if(tousseInstance == null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统未找到。"); + failReceiveIdList.add(rs.getString("receive_id")); + continue; + } + /*if(tousseInstance.getUseRecord_id() != null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统已生成使用记录。"); + //已生成使用记录的器械包不更新为失败 + continue; + }*/ + //更新中间表的更新时间 + + innerMap.put(rs.getString("receive_id"), new String[] { + rs.getString("exec_dept_code"), rs.getString("exec_dept_name"), + rs.getString("doc_name"),rs.getString("patient_name"), + rs.getString("bar_code"),rs.getString("hospital_no"),rs.getString("charge_date")}); + } + jsonObject.put("return", success ? "1" : "0"); + jsonObject.put("errMsg", message.toString()); + return innerMap; + } + }); + + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + generateUseRecordAndCreateRecyclingApplication(map , applicant , succReceiveIdList); + }else{ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "定时调用中六手麻回调失败的记录-未找到相关医嘱信息或医嘱信息表的条码关联不到消毒供应系统的器械包条码"); + } + } catch (Exception e) { + failReceiveIdList.addAll(succReceiveIdList);//如果整体调用失败,则成功的记录也追加到失败的集合中 + updateRecordStatus(false , failReceiveIdList); + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "定时调用中六手麻回调失败的记录-调用回调接口过程中出现异常."); + logger.error("定时调用中六手麻回调失败的记录-更新该病历的器械包使用记录异常", e); + } + } + + /** + * 根据结果集生成使用记录并创建申请单(仅中山六院使用) + * @param map + * @param patientNumber + * @param applicant + */ + private boolean generateUseRecordAndCreateRecyclingApplication(Map map , + String applicant , List succReceiveIdList) throws Exception{ + boolean success = true; + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + List idList = new ArrayList(); + String departCoding = null; + String depart = null; + Set departCodingSet = new HashSet(); + UseRecord useRecord = null; + + Date now = new Date(); + for(String key : map.keySet()){ + String[] objectArray = map.get(key); + succReceiveIdList.add(key); + + //同步病人信息 + PatientInfoVO patientInfo = null; + try{ + patientInfo = dataSynchronizationManager.findPatientInfoByHospitalNumOrTreatmentNum(objectArray[5], "hospitalNum"); + }catch(Exception ex){ + ex.printStackTrace(); + } + /*PatientInfoVO[] vos = dataSynchronizationManager.findPatientInfoByHospitalNum(objectArray[5]); + if (vos != null && vos.length > 0){ + patientInfo = CollectionUtils.find(Arrays.asList(vos), new Predicate() { + @Override + public boolean evaluate(PatientInfoVO vo) { + return StringUtils.isNotBlank(vo.getOperation()) || StringUtils.isNotBlank(vo.getDoctorName()); + } + }); + if(patientInfo == null && ArrayUtils.isNotEmpty(vos)){ + patientInfo = vos[0]; + } + }*/ + + if(patientInfo == null){ + //根据住院号查不到病人信息时,应给予阻止 + + continue; + }else if(!departCodingSet.contains(objectArray[0])){ + departCoding = objectArray[0]; + depart = objectArray[1]; + //1.新增使用记录(同一个部门只生成一个使用记录) + useRecord = new UseRecord(); + useRecord.setDepartCoding(objectArray[0]); + useRecord.setDepart(objectArray[1]); + useRecord.setDoctorName(objectArray[2]); + useRecord.setEnteringDate(now); + + //录入日期取中间表的日期 + try{ + useRecord.setEnteringDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(objectArray[6])); + }catch(Exception e){ + logger.debug("使用记录录入日期转换赋值失败"); + } + + useRecord.setApplicationTime(now);//由于回调时生成使用记录立即转换为申请单,所以申请时间也是当前时间 + useRecord.setPatientName(objectArray[3]); + useRecord.setHospitalNum(objectArray[5]); + useRecord.setStatus(UseRecord.STATUS_APPLIED); + + if(patientInfo != null){ + //诊疗号 + /*if(StringUtils.isNotBlank(patientInfo.getPatientNumber())){ + useRecord.setTreatmentNum(patientInfo.getPatientNumber()); + }*/ + + //病人姓名 + if(StringUtils.isBlank(objectArray[3]) && StringUtils.isNotBlank(patientInfo.getPatientName())){ + useRecord.setPatientName(patientInfo.getPatientName()); + } + + //病人年龄 + if(StringUtils.isNotBlank(patientInfo.getPatientAge())){ + useRecord.setPatientAge(patientInfo.getPatientAge()); + } + + //病人性别 + if(StringUtils.isNotBlank(patientInfo.getPatientSex())){ + useRecord.setPatientSex(patientInfo.getPatientSex()); + } + + //病人身份证号 + if(StringUtils.isNotBlank(patientInfo.getPatientIDCard())){ + useRecord.setPatientIDCard(patientInfo.getPatientIDCard()); + } + + //医生姓名 + if(StringUtils.isBlank(objectArray[2]) && StringUtils.isNotBlank(patientInfo.getDoctorName())){ + useRecord.setDoctorName(patientInfo.getDoctorName()); + } + + //手术名称 + if(StringUtils.isNotBlank(patientInfo.getOperation())){ + useRecord.setOperation(patientInfo.getOperation()); + } + + // 病室 + if(StringUtils.isNotBlank(patientInfo.getRoomNumber())){ + useRecord.setRoomNumber(patientInfo.getRoomNumber()); + } + + // 床位 + if(StringUtils.isNotBlank(patientInfo.getBedNumber())){ + useRecord.setBedNumber(patientInfo.getBedNumber()); + } + } + useRecordManager.saveOrUpdate(useRecord); + + idList.add(useRecord.getId()); + + departCodingSet.add(objectArray[0]); + } + //2.更新器械包实例的使用记录(id外键)及状态,不可使用执行sql的方式,由于userecord还未持久化,所以直接更新器械包实例表的使用记录外键会导致违反外键约束 + /*objectDao.excuteSQL("update TousseInstance set useRecord_id = " + + useRecord.getId() + ", status='" + TousseInstance.STATUS_USED + "' where id in (select id from barcodeDevice where barcode='" + objectArray[4] + "')");*/ + TousseInstance tousseInstance = tousseInstanceManager.getTousseInstanceByBarcode(objectArray[4]); + if(tousseInstance != null){ + tousseInstance.setUseRecord_id(useRecord.getId()); + tousseInstance.setStatus(TousseInstance.STATUS_USED); + tousseInstanceManager.saveOrUpdate(tousseInstance); + } + } + + //3.转换成对应申请单applicant, depart, departCodeing三个参数待定(4.0转申请单方法的idList只传一个元素,所以循环调用) + /*for(Long id : idList){ + List tranUserecordIdList = new ArrayList(); + tranUserecordIdList.add(id); + useRecordManager.createRecyclingApplicationByUseRecords(tranUserecordIdList, applicant, depart, departCoding , now); + }*/ + + //原有老的方式调用 + if(success){ + useRecordManager.createRecyclingApplicationByUseRecords(StringUtils.join(idList.toArray(), ";"), applicant, depart, departCoding); + + //如果调用成功则将对应的医嘱信息表的记录的状态置为1 + updateRecordStatus(true , succReceiveIdList); + }else{ + updateRecordStatus(false , succReceiveIdList); + } + + }else{ + //暂不处理 + } + return success; + } +} Index: ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/CommonHerpPathManagerImpl.java =================================================================== diff -u --- ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/CommonHerpPathManagerImpl.java (revision 0) +++ ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/CommonHerpPathManagerImpl.java (revision 17646) @@ -0,0 +1,512 @@ +package com.forgon.disinfectsystem.useRecord.hrepPath; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import net.sf.json.JSONObject; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; + +import com.forgon.directory.model.OrgUnit; +import com.forgon.disinfectsystem.datasynchronization.model.PatientInfoVO; +import com.forgon.disinfectsystem.entity.basedatamanager.toussedefinition.TousseInstance; +import com.forgon.disinfectsystem.entity.useRecord.UseRecord; +import com.forgon.disinfectsystem.tousse.toussedefinition.service.TousseInstanceManager; +import com.forgon.disinfectsystem.useRecord.service.UseRecordManager; +import com.forgon.tools.hibernate.ObjectDao; + +/** + * 与手麻系统使用记录接口对接的公共的实现类 + * @author shuyongfu + * @since date:2017-04-01 + */ +public class CommonHerpPathManagerImpl { + + private Logger logger = Logger.getLogger(this.getClass()); + + protected ObjectDao getObjectDao() { + return null; + } + + protected TousseInstanceManager getTousseInstanceManager() { + return null; + } + + protected JdbcTemplate getPathJdbcTemplate() { + return null; + } + + protected UseRecordManager getUseRecordManager() { + return null; + } + + protected String submitUseRecordByPatientNumberAndApplicant(String patientNumber,String applicant) { + logger.debug("更新该病历的器械包使用记录接口:住院号patientNumber=" + patientNumber+",申请人applicant="+applicant); + final JSONObject jsonObject = new JSONObject(); + final List succUserecordIdList = new ArrayList();//回调执行成功的使用记录id + final List failUserecordIdList = new ArrayList();//回调执行失败的使用记录id + try { + final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + Date now = new Date(); + final String dateStr = sdf.format(now); + //如果定时器程序调用时不传病人号的参数 + if(StringUtils.isBlank(patientNumber)){ + String mainSql = "select um.id from UserecordMain um where 1=1 " + + "and id in (select userId from UserecordDetail ud where ud.status is null or ud.status <> 1)" + + "and (um.status is null or um.status <> 1) order by um.id"; + + //根据住院号或诊疗号查询,map的数据格式为:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + List idList = getPathJdbcTemplate() + .query(mainSql, + new ResultSetExtractor>() { + public List extractData(ResultSet rs) + throws SQLException, DataAccessException { + List idResultList = new ArrayList(); + while (rs.next()) { + Number mainId = (Number)rs.getObject("id"); + if(mainId != null){ + idResultList.add(mainId.longValue()); + } + } + return idResultList; + } + }); + + if(CollectionUtils.isNotEmpty(idList)){ + for(Long id : idList){ + String sql = "select um.id mainId,useDate,treatmentNum,hospitalNum,patientName,patientAge,patientSex,patientIDCard," + + " doctorName,operation,operationRoom,patientArea,roomNumber,bedNumber,ascriptionDepartment,washHandNurse," + + " circuitNurse,specialInfection,um.status mainStatus,ud.id detailId,userId,barCode,amount,ud.status detailStatus " + + " from UserecordMain um join UserecordDetail ud on um.id=ud.userId where 1=1 and um.id=" + id + + " and (um.status is null or um.status <> 1) and (ud.status is null or ud.status <> 1) order by um.id"; + //根据住院号或诊疗号查询,map的数据格式为:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + Map map = getPathJdbcTemplate() + .query(sql, + new ResultSetExtractor>() { + boolean success = true; + StringBuffer message = new StringBuffer(""); + @Override + public Map extractData(ResultSet rs) + throws SQLException, DataAccessException { + Map innerMap = new HashMap(); + while (rs.next()) { + String barCode = rs.getString("barcode"); + if(StringUtils.isBlank(barCode)){ + continue; + } + TousseInstance tousseInstance = getTousseInstanceManager().getTousseInstanceByBarcode(barCode.trim()); + //1.如果bar_code对应到器械包条码不存在或者器械包条码存在但已关联了使用记录 + if(tousseInstance == null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统未找到。"); + failUserecordIdList.add(rs.getString("mainId")); + continue; + } + /*if(tousseInstance.getTousseFixedBarcode()){ + success = false; + message.append("条码:" + barCode + "为固定条码,不能用于使用记录。"); + failUserecordList.add(rs.getString("receive_id")); + continue; + }*/ + if(tousseInstance.getUseRecord_id() != null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统已生成使用记录。"); + //已生成使用记录的器械包不更新为失败 + continue; + } + + Object mainIdObj = rs.getObject("mainId"); + Object useDateObj = rs.getObject("useDate"); + String useDateStr = useDateObj == null ? dateStr : sdf.format((Date)useDateObj); + Object treatmentNumObj = rs.getObject("treatmentNum"); + Object hospitalNumObj = rs.getObject("hospitalNum"); + Object patientNameObj = rs.getObject("patientName"); + Object patientAgeObj = rs.getObject("patientAge"); + Object patientSexObj = rs.getObject("patientSex"); + Object patientIDCardObj = rs.getObject("patientIDCard"); + Object doctorNameObj = rs.getObject("doctorName"); + Object operationObj = rs.getObject("operation"); + Object operationRoomObj = rs.getObject("operationRoom"); + Object patientAreaObj = rs.getObject("patientArea"); + Object roomNumberObj = rs.getObject("roomNumber"); + Object bedNumberObj = rs.getObject("bedNumber"); + Object ascriptionDepartmentObj = rs.getObject("ascriptionDepartment"); + Object washHandNurseObj = rs.getObject("washHandNurse"); + Object circuitNurseObj = rs.getObject("circuitNurse"); + Object specialInfectionObj = rs.getObject("specialInfection"); + Object mainStatusObj = rs.getObject("mainStatus"); + Object detailIdObj = rs.getObject("detailId"); + Object amountObj = rs.getObject("amount"); + Object detailStatusObj = rs.getObject("detailStatus"); + + //barcode作为唯一的key + innerMap.put(barCode, new String[] { + String.valueOf(mainIdObj), useDateStr,String.valueOf(treatmentNumObj),String.valueOf(hospitalNumObj),String.valueOf(patientNameObj), + String.valueOf(patientAgeObj),String.valueOf(patientSexObj),String.valueOf(patientIDCardObj),String.valueOf(doctorNameObj),String.valueOf(operationObj), + String.valueOf(operationRoomObj),String.valueOf(patientAreaObj),String.valueOf(roomNumberObj),String.valueOf(bedNumberObj),String.valueOf(ascriptionDepartmentObj), + String.valueOf(washHandNurseObj),String.valueOf(circuitNurseObj),String.valueOf(specialInfectionObj),String.valueOf(mainStatusObj),String.valueOf(detailIdObj) + ,String.valueOf(amountObj),String.valueOf(detailStatusObj)}); + } + jsonObject.put("return", success ? "1" : "0"); + jsonObject.put("errMsg", message.toString()); + return innerMap; + } + }); + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + //map数据格式:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + generateUseRecordAndCreateRecyclingApplication(map , patientNumber, applicant , succUserecordIdList); + }else{ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "未找到相关医嘱信息或医嘱信息表的条码关联不到消毒供应系统的器械包条码"); + } + } + } + }else if(StringUtils.isNotBlank(patientNumber) && StringUtils.isNotBlank(applicant)){ + String sql = "select um.id mainId,useDate,treatmentNum,hospitalNum,patientName,patientAge,patientSex,patientIDCard," + + "doctorName,operation,operationRoom,patientArea,roomNumber,bedNumber,ascriptionDepartment,washHandNurse," + + "circuitNurse,specialInfection,um.status mainStatus,ud.id detailId,userId,barCode,amount,ud.status detailStatus " + + "from UserecordMain um join UserecordDetail ud on um.id=ud.userId where 1=1 "; + if(StringUtils.isNotBlank(patientNumber)){ + sql += "and (hospitalNum = '"+patientNumber+"' or treatmentNum = '"+patientNumber+"') "; + } + sql += "and (um.status is null or um.status <> 1) and (ud.status is null or ud.status <> 1) order by um.id"; + + //根据住院号或诊疗号查询,map的数据格式为:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + Map map = getPathJdbcTemplate() + .query(sql, + new ResultSetExtractor>() { + boolean success = true; + StringBuffer message = new StringBuffer(""); + @Override + public Map extractData(ResultSet rs) + throws SQLException, DataAccessException { + Map innerMap = new HashMap(); + while (rs.next()) { + String barCode = rs.getString("barcode"); + if(StringUtils.isBlank(barCode)){ + continue; + } + TousseInstance tousseInstance = getTousseInstanceManager().getTousseInstanceByBarcode(barCode.trim()); + //1.如果bar_code对应到器械包条码不存在或者器械包条码存在但已关联了使用记录 + if(tousseInstance == null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统未找到。"); + failUserecordIdList.add(rs.getString("mainId")); + continue; + } + /*if(tousseInstance.getTousseFixedBarcode()){ + success = false; + message.append("条码:" + barCode + "为固定条码,不能用于使用记录。"); + failUserecordList.add(rs.getString("receive_id")); + continue; + }*/ + if(tousseInstance.getUseRecord_id() != null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统已生成使用记录。"); + //已生成使用记录的器械包不更新为失败 + continue; + } + + Object mainIdObj = rs.getObject("mainId"); + Object useDateObj = rs.getObject("useDate"); + String useDateStr = useDateObj == null ? dateStr : sdf.format((Date)useDateObj); + Object treatmentNumObj = rs.getObject("treatmentNum"); + Object hospitalNumObj = rs.getObject("hospitalNum"); + Object patientNameObj = rs.getObject("patientName"); + Object patientAgeObj = rs.getObject("patientAge"); + Object patientSexObj = rs.getObject("patientSex"); + Object patientIDCardObj = rs.getObject("patientIDCard"); + Object doctorNameObj = rs.getObject("doctorName"); + Object operationObj = rs.getObject("operation"); + Object operationRoomObj = rs.getObject("operationRoom"); + Object patientAreaObj = rs.getObject("patientArea"); + Object roomNumberObj = rs.getObject("roomNumber"); + Object bedNumberObj = rs.getObject("bedNumber"); + Object ascriptionDepartmentObj = rs.getObject("ascriptionDepartment"); + Object washHandNurseObj = rs.getObject("washHandNurse"); + Object circuitNurseObj = rs.getObject("circuitNurse"); + Object specialInfectionObj = rs.getObject("specialInfection"); + Object mainStatusObj = rs.getObject("mainStatus"); + Object detailIdObj = rs.getObject("detailId"); + Object amountObj = rs.getObject("amount"); + Object detailStatusObj = rs.getObject("detailStatus"); + + //barcode作为唯一的key + innerMap.put(barCode, new String[] { + String.valueOf(mainIdObj), useDateStr,String.valueOf(treatmentNumObj),String.valueOf(hospitalNumObj),String.valueOf(patientNameObj), + String.valueOf(patientAgeObj),String.valueOf(patientSexObj),String.valueOf(patientIDCardObj),String.valueOf(doctorNameObj),String.valueOf(operationObj), + String.valueOf(operationRoomObj),String.valueOf(patientAreaObj),String.valueOf(roomNumberObj),String.valueOf(bedNumberObj),String.valueOf(ascriptionDepartmentObj), + String.valueOf(washHandNurseObj),String.valueOf(circuitNurseObj),String.valueOf(specialInfectionObj),String.valueOf(mainStatusObj),String.valueOf(detailIdObj) + ,String.valueOf(amountObj),String.valueOf(detailStatusObj)}); + } + jsonObject.put("return", success ? "1" : "0"); + jsonObject.put("errMsg", message.toString()); + return innerMap; + } + }); + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + //map数据格式:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + generateUseRecordAndCreateRecyclingApplication(map , patientNumber, applicant , succUserecordIdList); + }else{ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "未找到相关医嘱信息或医嘱信息表的条码关联不到消毒供应系统的器械包条码"); + } + } + } catch (RuntimeException e) { + failUserecordIdList.addAll(succUserecordIdList);//如果整体调用失败,则成功的记录也追加到失败的集合中 + updateRecordStatus(false , failUserecordIdList); + jsonObject.put("return", "0"); + jsonObject.put("errMsg", e.getMessage()); + logger.error("更新该病历的器械包使用记录异常", e); + } catch (Exception e) { + if(e.getMessage().indexOf("Could not get JDBC Connection") == -1){ + logger.error("根据住院号更新该病历的器械包使用记录报错:" + e); + failUserecordIdList.addAll(succUserecordIdList);//如果整体调用失败,则成功的记录也追加到失败的集合中 + }else{ + logger.error("herp系统path库连接获取失败导致根据住院号更新该病历的器械包使用记录报错:" + e); + } + try{ + if(e.getMessage().indexOf("Could not get JDBC Connection") == -1){ + updateRecordStatus(false , failUserecordIdList); + } + }catch(Exception e1){ + logger.error("更新herp path库该病历的医嘱信息记录失败时报错:" + e1); + } + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "调用回调接口过程中出现异常."); + logger.error("更新该病历的器械包使用记录异常", e); + } + return jsonObject.toString(); + } + + /** + * 根据结果集生成使用记录并创建申请单(除中山六院外的系统使用) + * @param map 数据格式:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + * @param patientNumber + * @param applicant + */ + protected boolean generateUseRecordAndCreateRecyclingApplication(Map map , + String patientNumber,String applicant , List succUserecordIdList) throws Exception{ + boolean success = true; + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + //使用记录id + List idList = new ArrayList(); + String departCoding = null; + String depart = null; + Set departCodingSet = new HashSet(); + UseRecord useRecord = null; + + Date now = new Date(); + for(String barcode : map.keySet()){ + //数组顺序依次为:mainIdObj,useDateStr,treatmentNumObj,hospitalNumObj,patientNameObj, patientAgeObj,patientSexObj,patientIDCardObj, + //doctorNameObj,operationObj,operationRoomObj,patientAreaObj,roomNumberObj,bedNumberObj,ascriptionDepartmentObj,washHandNurseObj,circuitNurseObj,specialInfectionObj, + //mainStatusObj,detailIdObj,amountObj,detailStatusObj + String[] patientArray = map.get(barcode); + if(!succUserecordIdList.contains(patientArray[0])){ + succUserecordIdList.add(patientArray[0]); + } + + depart = patientArray[14]; + if(StringUtils.isBlank(depart)){ + throw new RuntimeException("科室为空"); + } + OrgUnit orgUnit = + (OrgUnit)getObjectDao().getBySql(OrgUnit.class.getSimpleName(), "where name='" + depart + "'"); + if(orgUnit == null){ + throw new RuntimeException("科室" + depart + "在供应系统不存在"); + } + + //构造病人信息 + PatientInfoVO patientInfo = new PatientInfoVO(); + patientInfo.setOperationTime(patientArray[1]); + patientInfo.setClinicNumber(patientArray[2]); + patientInfo.setHospitalNumber(patientArray[3]); + patientInfo.setPatientName(patientArray[4]); + patientInfo.setPatientAge(patientArray[5]); + patientInfo.setPatientSex(patientArray[6]); + patientInfo.setPatientIDCard(patientArray[7]); + patientInfo.setDoctorName(patientArray[8]); + patientInfo.setOperation(patientArray[9]); + patientInfo.setPatientArea(patientArray[11]); + patientInfo.setRoomNumber(patientArray[12]); + patientInfo.setBedNumber(patientArray[13]); + patientInfo.setAscriptionDepartment(depart); + + /** + * 病人vo类增加洗手护士、巡回护士、特殊感染类型 + */ + patientInfo.setWashHandNurse(patientArray[15]); + patientInfo.setCircuitNurse(patientArray[16]); + patientInfo.setSpecialInfection(patientArray[17]); + patientInfo.setType(patientNumber.equals(patientInfo.getHospitalNumber()) ? PatientInfoVO.TYPE_HOSPITAL : PatientInfoVO.TYPE_CLINIC); + + departCoding = orgUnit.getOrgUnitCoding(); + if(!departCodingSet.contains(departCoding)){ + //1.新增使用记录(同一个部门只生成一个使用记录) + useRecord = new UseRecord(); + useRecord.setDepartCoding(departCoding); + useRecord.setDepart(depart); + useRecord.setDoctorName(patientArray[8]); + useRecord.setEnteringDate(now); + + //录入日期取中间表的日期 + try{ + useRecord.setEnteringDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(patientInfo.getOperationTime())); + }catch(Exception e){ + logger.debug("使用记录录入日期转换赋值失败"); + try{ + useRecord.setEnteringDate(new SimpleDateFormat("yyyy-MM-dd").parse(patientInfo.getOperationTime())); + }catch(Exception e1){ + useRecord.setEnteringDate(now); + logger.debug("使用记录录入日期转换赋值失败"); + } + } + + useRecord.setApplicationTime(now);//由于回调时生成使用记录立即转换为申请单,所以申请时间也是当前时间 + useRecord.setPatientName(patientInfo.getPatientName()); + useRecord.setHospitalNum(patientInfo.getHospitalNumber()); + useRecord.setTreatmentNum(patientInfo.getClinicNumber()); + useRecord.setStatus(UseRecord.STATUS_APPLIED); + + //病人年龄 + if(StringUtils.isNotBlank(patientInfo.getPatientAge())){ + useRecord.setPatientAge(patientInfo.getPatientAge()); + } + + //病人性别 + if(StringUtils.isNotBlank(patientInfo.getPatientSex())){ + useRecord.setPatientSex(patientInfo.getPatientSex()); + } + + //病人身份证号 + if(StringUtils.isNotBlank(patientInfo.getPatientIDCard())){ + useRecord.setPatientIDCard(patientInfo.getPatientIDCard()); + } + + //医生姓名 + if(StringUtils.isNotBlank(patientInfo.getDoctorName())){ + useRecord.setDoctorName(patientInfo.getDoctorName()); + } + + //手术名称 + if(StringUtils.isNotBlank(patientInfo.getOperation())){ + useRecord.setOperation(patientInfo.getOperation()); + } + + // 病室 + if(StringUtils.isNotBlank(patientInfo.getRoomNumber())){ + useRecord.setRoomNumber(patientInfo.getRoomNumber()); + } + + // 床位 + if(StringUtils.isNotBlank(patientInfo.getBedNumber())){ + useRecord.setBedNumber(patientInfo.getBedNumber()); + } + + // 洗手护士 + if(StringUtils.isNotBlank(patientInfo.getWashHandNurse())){ + useRecord.setWashHandNurse(patientInfo.getWashHandNurse()); + } + + // 巡回护士 + if(StringUtils.isNotBlank(patientInfo.getCircuitNurse())){ + useRecord.setCircuitNurse(patientInfo.getCircuitNurse()); + } + + // 特殊感染类型 + if(StringUtils.isNotBlank(patientInfo.getSpecialInfection())){ + useRecord.setSpecialInfection(patientInfo.getSpecialInfection()); + } + getUseRecordManager().saveOrUpdate(useRecord); + + idList.add(useRecord.getId()); + + departCodingSet.add(departCoding); + } + TousseInstance tousseInstance = getTousseInstanceManager().getTousseInstanceByBarcode(barcode); + if(tousseInstance != null){ + tousseInstance.setUseRecord_id(useRecord.getId()); + tousseInstance.setStatus(TousseInstance.STATUS_USED); + getTousseInstanceManager().saveOrUpdate(tousseInstance); + } + } + + //原有老的方式调用 + if(success){ + getUseRecordManager().createRecyclingApplicationByUseRecords(StringUtils.join(idList.toArray(), ";"), applicant, depart, departCoding); + + //如果调用成功则将对应的医嘱信息表的记录的状态置为1 + updateRecordStatus(true , succUserecordIdList); + }else{ + updateRecordStatus(false , succUserecordIdList); + } + + }else{ + //暂不处理 + } + return success; + } + + /** + * 回调记录更新为成功或失败 + * @param success 是否成功或失败 + * @param userecordIdList herp接口使用记录id集合 + */ + protected void updateRecordStatus(boolean success , List userecordIdList){ + String userecordUpdateSql = "update UserecordMain set status = " + + (success ? 1 : 0) + " where 1=2"; + String userecordDetailUpdateSql = "update UserecordDetail set status = " + + (success ? 1 : 0) + " where 1=2"; + if(CollectionUtils.isNotEmpty(userecordIdList)){ + String userecordIds = ""; + for (int i = 0; i < userecordIdList.size(); i++) { + userecordIds += "'" + userecordIdList.get(i) + "'"; + if(i < userecordIdList.size() - 1){ + userecordIds += ","; + } + } + userecordUpdateSql += " or id in (" + userecordIds + ")"; + userecordDetailUpdateSql += " or userId in (" + userecordIds + ")"; + } + try { + getPathJdbcTemplate().update(userecordUpdateSql); + if(success){ + getPathJdbcTemplate().update(userecordDetailUpdateSql); + } + } catch (Exception e) { + e.printStackTrace(); + logger.error(e); + } + } + + /** + * 手麻回调失败的记录重新回调 + * 定时器定时调用 + */ + protected void timingSubmitUseRecord(){ + logger.info("定时调用手麻回调失败的记录重新回调的方法:" + new Date()); + String applicant = "系统定时回写"; + final JSONObject jsonObject = new JSONObject(); + try { + //病人号不传,执行所有未成功的病人号 + submitUseRecordByPatientNumberAndApplicant(null, applicant); + } catch (Exception e) { + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "定时调用手麻回调失败的记录-调用回调接口过程中出现异常."); + logger.error("定时调用手麻回调失败的记录-更新该病历的器械包使用记录异常", e); + } + } +} Index: ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/HerpPathManager.java =================================================================== diff -u --- ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/HerpPathManager.java (revision 0) +++ ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/HerpPathManager.java (revision 17646) @@ -0,0 +1,24 @@ +package com.forgon.disinfectsystem.useRecord.hrepPath; + + +/** + * 与手麻系统接口对接的interface接口 + * @author shuyongfu + * @since date:2017-04-01 + * + */ +public interface HerpPathManager { + + /** + * 根据病历号及申请人提交回调 + * @param patientNumber 病历号(通常是住院号) + * @param applicant 申请人 + * @return + */ + public String submitUseRecordByPatientNumberAndApplicant(String patientNumber,String applicant); + + /** + * 定时调用手麻系统的使用记录 + */ + public void timingSubmitUseRecord(); +} Index: ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/ljsrmyy/HerpPathManagerImpl.java =================================================================== diff -u --- ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/ljsrmyy/HerpPathManagerImpl.java (revision 0) +++ ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/ljsrmyy/HerpPathManagerImpl.java (revision 17646) @@ -0,0 +1,539 @@ +package com.forgon.disinfectsystem.useRecord.hrepPath.ljsrmyy; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import net.sf.json.JSONObject; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; + +import com.forgon.directory.model.OrgUnit; +import com.forgon.disinfectsystem.datasynchronization.model.PatientInfoVO; +import com.forgon.disinfectsystem.entity.basedatamanager.toussedefinition.TousseInstance; +import com.forgon.disinfectsystem.entity.useRecord.UseRecord; +import com.forgon.disinfectsystem.tousse.toussedefinition.service.TousseInstanceManager; +import com.forgon.disinfectsystem.useRecord.hrepPath.CommonHerpPathManagerImpl; +import com.forgon.disinfectsystem.useRecord.hrepPath.HerpPathManager; +import com.forgon.disinfectsystem.useRecord.service.UseRecordManager; +import com.forgon.tools.date.DateTools; +import com.forgon.tools.db.DatabaseUtil; +import com.forgon.tools.hibernate.ObjectDao; + +/** + * 与手麻系统接口对接的实现类 + * @author shuyongfu + * @since date:2017-04-01 + */ +public class HerpPathManagerImpl extends CommonHerpPathManagerImpl implements HerpPathManager { + + private Logger logger = Logger.getLogger(this.getClass()); + + private ObjectDao objectDao; + + private TousseInstanceManager tousseInstanceManager; + + private JdbcTemplate pathJdbcTemplate; + + private UseRecordManager useRecordManager; + + /** + * 每次定时任务处理使用记录的数量 + */ + private int processUseRecordAmountPerTime = 100; + + public void setTousseInstanceManager( + TousseInstanceManager tousseInstanceManager) { + this.tousseInstanceManager = tousseInstanceManager; + } + + public void setObjectDao(ObjectDao objectDao) { + this.objectDao = objectDao; + } + + public void setPathJdbcTemplate(JdbcTemplate pathJdbcTemplate) { + this.pathJdbcTemplate = pathJdbcTemplate; + } + + public void setUseRecordManager(UseRecordManager useRecordManager) { + this.useRecordManager = useRecordManager; + } + + private volatile static ScheduledExecutorService executorService; + + private static ScheduledExecutorService getExecutorService() { + if (executorService == null) { + synchronized (HerpPathManagerImpl.class) { + if (executorService == null) { + executorService = Executors.newScheduledThreadPool(1); + } + } + } + return executorService; + } + + @Override + public String submitUseRecordByPatientNumberAndApplicant(String patientNumber,String applicant) { + logger.debug("更新该病历的器械包使用记录接口:住院号patientNumber=" + patientNumber+",申请人applicant="+applicant); + final JSONObject jsonObject = new JSONObject(); + final List succUserecordIdList = new ArrayList();//回调执行成功的使用记录id + final List failUserecordIdList = new ArrayList();//回调执行失败的使用记录id + try { + final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date now = new Date(); + final String dateStr = sdf.format(now); + + String sql = "select id,useDate,treatmentNum,hospitalNum,patientName,patientAge,patientSex,patientIDCard," + + " doctorName,operation,operationRoom,patientArea,roomNumber,bedNumber,depart,washHandNurse," + + " circuitNurse,specialInfection,NOW_STATE,QXName,BarCode,DepartCoding " + + " from LYUserecordMain um where 1=1 "; + //如果病历号不为空时,则按病历号进行过滤 + if(StringUtils.isNotBlank(patientNumber)){ + sql += " and (hospitalNum = '"+patientNumber+"' or treatmentNum = '"+patientNumber+"') "; + }else{ + //否则查询出不在回写成功范围内的数据(数量加以限制,否则处理时间会比较长),为为常量的子查询条件oracle只支持1000个,所以超过1000时需要转换成select的union查询 + String processedSql = getProcessedQueryIdUnionSql(); + if(StringUtils.isNotBlank(processedSql)){ + sql += " and id not in ("+ processedSql +")"; + } + } + sql += " and rownum < " + processUseRecordAmountPerTime + " order by id"; + //根据住院号或诊疗号查询,map的数据格式为:{id=[id="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + Map map = pathJdbcTemplate + .query(sql, + new ResultSetExtractor>() { + boolean success = true; + StringBuffer message = new StringBuffer(""); + @Override + public Map extractData(ResultSet rs) + throws SQLException, DataAccessException { + Map innerMap = new HashMap(); + while (rs.next()) { + Object idObj = rs.getObject("id"); + String barCode = rs.getString("BarCode"); + if(StringUtils.isBlank(barCode)){ + continue; + } + TousseInstance tousseInstance = tousseInstanceManager.getTousseInstanceByBarcode(barCode.trim()); + //1.如果bar_code对应到器械包条码不存在或者器械包条码存在但已关联了使用记录 + if(tousseInstance == null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统未找到。"); + failUserecordIdList.add(String.valueOf(idObj)); + continue; + } + if(tousseInstance.getUseRecord_id() != null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统已生成使用记录。"); + //已生成使用记录的器械包不更新为失败 + continue; + } + + Object useDateObj = rs.getObject("useDate"); + String useDateStr = useDateObj == null ? dateStr : sdf.format((Date)useDateObj); + Object treatmentNumObj = rs.getObject("treatmentNum"); + Object hospitalNumObj = rs.getObject("hospitalNum"); + Object patientNameObj = rs.getObject("patientName"); + Object patientAgeObj = rs.getObject("patientAge"); + Object patientSexObj = rs.getObject("patientSex"); + Object patientIDCardObj = rs.getObject("patientIDCard"); + Object doctorNameObj = rs.getObject("doctorName"); + Object operationObj = rs.getObject("operation"); + Object operationRoomObj = rs.getObject("operationRoom"); + Object patientAreaObj = rs.getObject("patientArea"); + Object roomNumberObj = rs.getObject("roomNumber"); + Object bedNumberObj = rs.getObject("bedNumber"); + Object departObj = rs.getObject("depart"); + Object washHandNurseObj = rs.getObject("washHandNurse"); + Object circuitNurseObj = rs.getObject("circuitNurse"); + Object specialInfectionObj = rs.getObject("specialInfection"); + Object nowStatusObj = rs.getObject("NOW_STATE"); + Object qxNameObj = rs.getObject("QXName"); + Object departCodingObj = rs.getObject("DepartCoding"); + + + //barcode作为唯一的key + innerMap.put(String.valueOf(idObj), new String[] { + barCode, useDateStr,String.valueOf(treatmentNumObj),String.valueOf(hospitalNumObj),String.valueOf(patientNameObj), + String.valueOf(patientAgeObj),String.valueOf(patientSexObj),String.valueOf(patientIDCardObj),String.valueOf(doctorNameObj), + String.valueOf(operationObj),String.valueOf(operationRoomObj),String.valueOf(patientAreaObj),String.valueOf(roomNumberObj), + String.valueOf(bedNumberObj),String.valueOf(departObj), String.valueOf(washHandNurseObj),String.valueOf(circuitNurseObj), + String.valueOf(specialInfectionObj),String.valueOf(nowStatusObj),String.valueOf(qxNameObj),String.valueOf(departCodingObj)}); + } + jsonObject.put("return", success ? "1" : "0"); + jsonObject.put("errMsg", message.toString()); + return innerMap; + } + }); + + //先把结果保存至消毒供应系统回调结果表,新数据则插入,旧数据则更新处理时间 + Set keySet = map.keySet(); + if(CollectionUtils.isNotEmpty(keySet)){ + for(String key : keySet){ + String[] dataArray = map.get(key); + //1.先查询该id在回调结果表中是否存在 + LyUseRecordMainResult result = + (LyUseRecordMainResult)objectDao.getByProperty(LyUseRecordMainResult.class.getSimpleName(), "id", Long.valueOf(key)); + if(result == null){ + result = new LyUseRecordMainResult(); + result.setLastUpdateTime(new Date()); + result.setId(Long.valueOf(key)); + result.setBarCode(dataArray[0]); + result.setUseDate(DateTools.convertTime(dataArray[1], sdf)); + result.setTreatmentNum(dataArray[2]); + result.setHospitalNum(dataArray[3]); + result.setPatientName(dataArray[4]); + result.setPatientAge(dataArray[5]); + result.setPatientSex(dataArray[6]); + result.setPatientIDCard(dataArray[7]); + result.setDoctorName(dataArray[8]); + result.setOperation(dataArray[9]); + result.setOperationRoom(dataArray[10]); + result.setPatientArea(dataArray[11]); + result.setRoomNumber(dataArray[12]); + result.setBedNumber(dataArray[13]); + result.setDepart(dataArray[14]); + result.setWashHandNurse(dataArray[15]); + result.setCircuitNurse(dataArray[16]); + result.setSpecialInfection(dataArray[17]); + result.setNowState(dataArray[18]); + result.setQxName(dataArray[19]); + result.setDepartCoding(dataArray[20]); + }else{ + result.setLastUpdateTime(new Date()); + } + objectDao.saveOrUpdate(result); + } + } + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + //map数据格式:{id1=[barcode="",patientName="",....],id1=[barcode="",patientName="",....]} + generateUseRecordAndCreateRecyclingApplication(map , patientNumber, applicant , succUserecordIdList); + }else{ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "未找到相关医嘱信息或医嘱信息表的条码关联不到消毒供应系统的器械包条码"); + } + } catch (RuntimeException e) { + failUserecordIdList.addAll(succUserecordIdList);//如果整体调用失败,则成功的记录也追加到失败的集合中 + updateRecordStatus(false , failUserecordIdList); + jsonObject.put("return", "0"); + jsonObject.put("errMsg", e.getMessage()); + logger.error("更新该病历的器械包使用记录异常", e); + } catch (Exception e) { + if(e.getMessage().indexOf("Could not get JDBC Connection") == -1){ + logger.error("根据住院号更新该病历的器械包使用记录报错:" + e); + failUserecordIdList.addAll(succUserecordIdList);//如果整体调用失败,则成功的记录也追加到失败的集合中 + }else{ + logger.error("herp系统path库连接获取失败导致根据住院号更新该病历的器械包使用记录报错:" + e); + } + try{ + if(e.getMessage().indexOf("Could not get JDBC Connection") == -1){ + updateRecordStatus(false , failUserecordIdList); + } + }catch(Exception e1){ + logger.error("更新herp path库该病历的医嘱信息记录失败时报错:" + e1); + } + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "调用回调接口过程中出现异常."); + logger.error("更新该病历的器械包使用记录异常", e); + } + return jsonObject.toString(); + } + + /** + * 查询已经处理成功的使用记录id的联接查询语句 + * @return 如:select 1 from dual union select * from dual union select 3 from dual ...... + */ + private String getProcessedQueryIdUnionSql(){ + String unionQuerySql = null; + ResultSet rs = objectDao.executeSql("select id from LYUserecordMainResult where status=" + LyUseRecordMainResult.STATUS_SUCC); + try{ + List sqlList = new ArrayList(); + while(rs.next()){ + sqlList.add("select "+ rs.getLong("id") +" from dual"); + } + if(CollectionUtils.isNotEmpty(sqlList)){ + unionQuerySql = StringUtils.join(sqlList , " union "); + } + }catch(Exception e){ + e.printStackTrace(); + }finally{ + DatabaseUtil.closeResultSetAndStatement(rs); + } + return unionQuerySql; + } + + /** + * 根据结果集生成使用记录并创建申请单(除中山六院外的系统使用) + * @param map 数据格式:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + * @param patientNumber + * @param applicant + */ + public boolean generateUseRecordAndCreateRecyclingApplication(Map map , + String patientNumber,String applicant , List succUserecordIdList) throws Exception{ + boolean success = true; + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + //使用记录id + List idList = new ArrayList(); + String departCoding = null; + String depart = null; + Set departCodingSet = new HashSet(); + UseRecord useRecord = null; + + Date now = new Date(); + for(String id : map.keySet()){ + //数组顺序依次为:barcodeObj,useDateStr,treatmentNumObj,hospitalNumObj,patientNameObj,patientAgeObj,patientSexObj,patientIDCardObj, + //doctorNameObj,operationObj,operationRoomObj,patientAreaObj,roomNumberObj,bedNumberObj,ascriptionDepartmentObj,washHandNurseObj, + //circuitNurseObj,specialInfectionObj + String[] patientArray = map.get(id); + if(!succUserecordIdList.contains(id)){ + succUserecordIdList.add(id); + } + + depart = patientArray[14]; + if(StringUtils.isBlank(depart)){ + throw new RuntimeException("科室为空"); + } + OrgUnit orgUnit = + (OrgUnit)objectDao.getBySql(OrgUnit.class.getSimpleName(), "where name='" + depart + "'"); + if(orgUnit == null){ + throw new RuntimeException("科室" + depart + "在供应系统不存在"); + } + + //构造病人信息 + PatientInfoVO patientInfo = new PatientInfoVO(); + patientInfo.setClinicNumber(patientArray[2]); + patientInfo.setHospitalNumber(patientArray[3]); + patientInfo.setPatientName(patientArray[4]); + patientInfo.setPatientAge(patientArray[5]); + patientInfo.setPatientSex(patientArray[6]); + patientInfo.setPatientIDCard(patientArray[7]); + patientInfo.setDoctorName(patientArray[8]); + patientInfo.setOperation(patientArray[9]); + patientInfo.setOpRoomId(patientArray[10]); + patientInfo.setPatientArea(patientArray[11]); + patientInfo.setRoomNumber(patientArray[12]); + patientInfo.setBedNumber(patientArray[13]); + patientInfo.setAscriptionDepartment(depart); + + /** + * 病人vo类增加洗手护士、巡回护士、特殊感染类型 + */ + patientInfo.setWashHandNurse(patientArray[15]); + patientInfo.setCircuitNurse(patientArray[16]); + patientInfo.setSpecialInfection(patientArray[17]); + patientInfo.setType(patientNumber.equals(patientInfo.getHospitalNumber()) ? PatientInfoVO.TYPE_HOSPITAL : PatientInfoVO.TYPE_CLINIC); + + departCoding = orgUnit.getOrgUnitCoding(); + if(!departCodingSet.contains(departCoding)){ + //1.新增使用记录(同一个部门只生成一个使用记录) + useRecord = new UseRecord(); + useRecord.setDepartCoding(departCoding); + useRecord.setDepart(depart); + useRecord.setDoctorName(patientArray[8]); + useRecord.setEnteringDate(now); + + //录入日期取中间表的日期 + try{ + useRecord.setEnteringDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(patientArray[1])); + }catch(Exception e){ + logger.debug("使用记录录入日期转换赋值失败"); + useRecord.setEnteringDate(now); + } + + useRecord.setApplicationTime(now);//由于回调时生成使用记录立即转换为申请单,所以申请时间也是当前时间 + useRecord.setPatientName(patientInfo.getPatientName()); + useRecord.setHospitalNum(patientInfo.getHospitalNumber()); + useRecord.setTreatmentNum(patientInfo.getClinicNumber()); + useRecord.setStatus(UseRecord.STATUS_APPLIED); + + //病人年龄 + if(StringUtils.isNotBlank(patientInfo.getPatientAge())){ + useRecord.setPatientAge(patientInfo.getPatientAge()); + } + + //病人性别 + if(StringUtils.isNotBlank(patientInfo.getPatientSex())){ + useRecord.setPatientSex(patientInfo.getPatientSex()); + } + + //病人身份证号 + if(StringUtils.isNotBlank(patientInfo.getPatientIDCard())){ + useRecord.setPatientIDCard(patientInfo.getPatientIDCard()); + } + + //医生姓名 + if(StringUtils.isNotBlank(patientInfo.getDoctorName())){ + useRecord.setDoctorName(patientInfo.getDoctorName()); + } + + //手术名称 + if(StringUtils.isNotBlank(patientInfo.getOperation())){ + useRecord.setOperation(patientInfo.getOperation()); + } + + // 病室 + if(StringUtils.isNotBlank(patientInfo.getRoomNumber())){ + useRecord.setRoomNumber(patientInfo.getRoomNumber()); + } + + // 床位 + if(StringUtils.isNotBlank(patientInfo.getBedNumber())){ + useRecord.setBedNumber(patientInfo.getBedNumber()); + } + + // 洗手护士 + if(StringUtils.isNotBlank(patientInfo.getWashHandNurse())){ + useRecord.setWashHandNurse(patientInfo.getWashHandNurse()); + } + + // 巡回护士 + if(StringUtils.isNotBlank(patientInfo.getCircuitNurse())){ + useRecord.setCircuitNurse(patientInfo.getCircuitNurse()); + } + + // 特殊感染类型 + if(StringUtils.isNotBlank(patientInfo.getSpecialInfection())){ + useRecord.setSpecialInfection(patientInfo.getSpecialInfection()); + } + useRecordManager.saveOrUpdate(useRecord); + + idList.add(useRecord.getId()); + + departCodingSet.add(departCoding); + } + TousseInstance tousseInstance = tousseInstanceManager.getTousseInstanceByBarcode(patientArray[0]); + if(tousseInstance != null){ + tousseInstance.setUseRecord_id(useRecord.getId()); + tousseInstance.setStatus(TousseInstance.STATUS_USED); + tousseInstanceManager.saveOrUpdate(tousseInstance); + } + } + + //原有老的方式调用 + if(success){ + useRecordManager.createRecyclingApplicationByUseRecords(StringUtils.join(idList.toArray(), ";"), applicant, depart, departCoding); + + //如果调用成功则将对应的医嘱信息表的记录的状态置为1 + updateRecordStatus(true , succUserecordIdList); + }else{ + updateRecordStatus(false , succUserecordIdList); + } + + }else{ + //暂不处理 + } + return success; + } + + /** + * 回调记录更新为成功或失败 + * @param success 是否成功或失败 + * @param userecordIdList herp接口使用记录id集合 + */ + public void updateRecordStatus(boolean success , List userecordIdList){ + try { + //更新消毒供应追溯系统的手麻使用记录处理状态 + updateCSSDUserRecordStatus(success , userecordIdList); + //更新手麻系统的使用记录处理状态 + updateHerpPathUserRecordStatus(success , userecordIdList); + } catch (Exception e) { + e.printStackTrace(); + logger.error(e); + } + } + + /** + * 回调记录更新为成功或失败(消毒供应追溯系统) + * @param success 是否成功或失败 + * @param userecordIdList herp接口使用记录id集合 + */ + private void updateCSSDUserRecordStatus(boolean success , List userecordIdList){ + String userecordUpdateSql = "update LyUseRecordMainResult set lastUpdateTime=sysdate , status = " + + (success ? LyUseRecordMainResult.STATUS_SUCC : LyUseRecordMainResult.STATUS_FAIL) + " where 1=2"; + if(CollectionUtils.isNotEmpty(userecordIdList)){ + String userecordIds = ""; + for (int i = 0; i < userecordIdList.size(); i++) { + userecordIds += "'" + userecordIdList.get(i) + "'"; + if(i < userecordIdList.size() - 1){ + userecordIds += ","; + } + } + userecordUpdateSql += " or id in (" + userecordIds + ")"; + } + try { + objectDao.executeUpdate(userecordUpdateSql); + } catch (Exception e) { + e.printStackTrace(); + logger.error(e); + } + } + + /** + * 回调记录更新为成功或失败(手麻系统) + * @param success 是否成功或失败 + * @param userecordIdList herp接口使用记录id集合 + */ + private void updateHerpPathUserRecordStatus(boolean success , List userecordIdList){ + String userecordUpdateSql = "update LyUseRecordMain set status = " + + (success ? 1 : 0) + " where 1=2"; + if(CollectionUtils.isNotEmpty(userecordIdList)){ + String userecordIds = ""; + for (int i = 0; i < userecordIdList.size(); i++) { + userecordIds += "'" + userecordIdList.get(i) + "'"; + if(i < userecordIdList.size() - 1){ + userecordIds += ","; + } + } + userecordUpdateSql += " or id in (" + userecordIds + ")"; + } + try { + pathJdbcTemplate.update(userecordUpdateSql); + } catch (Exception e) { + e.printStackTrace(); + logger.error(e); + } + } + + /** + * 定时调用手麻回调记录重新回调 + * 定时器定时调用 + */ + @Override + public void timingSubmitUseRecord(){ + logger.info("定时调用手麻回调记录重新回调的方法:"); + getExecutorService().schedule(new Runnable() { + @Override + public void run() { + try { + String applicant = "系统定时回写"; + final JSONObject jsonObject = new JSONObject(); + try { + //病人号不传,执行所有未成功的病人号 + submitUseRecordByPatientNumberAndApplicant(null, applicant); + } catch (Exception e) { + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "定时调用手麻回调记录重新回调过程中出现异常."); + logger.error("定时调用手麻回调记录重新回调-更新该病历的器械包使用记录异常", e); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }, 5, TimeUnit.SECONDS); + } +} Index: ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/zsykzx/HerpPathManagerImpl.java =================================================================== diff -u --- ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/zsykzx/HerpPathManagerImpl.java (revision 0) +++ ssts-userecord/src/main/java/com/forgon/disinfectsystem/useRecord/hrepPath/zsykzx/HerpPathManagerImpl.java (revision 17646) @@ -0,0 +1,545 @@ +package com.forgon.disinfectsystem.useRecord.hrepPath.zsykzx; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import net.sf.json.JSONObject; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; + +import com.forgon.directory.model.OrgUnit; +import com.forgon.disinfectsystem.datasynchronization.model.PatientInfoVO; +import com.forgon.disinfectsystem.entity.basedatamanager.toussedefinition.TousseInstance; +import com.forgon.disinfectsystem.entity.useRecord.UseRecord; +import com.forgon.disinfectsystem.tousse.toussedefinition.service.TousseInstanceManager; +import com.forgon.disinfectsystem.useRecord.hrepPath.CommonHerpPathManagerImpl; +import com.forgon.disinfectsystem.useRecord.hrepPath.HerpPathManager; +import com.forgon.disinfectsystem.useRecord.service.UseRecordManager; +import com.forgon.tools.hibernate.ObjectDao; + +/** + * 中山眼科中心:与手麻系统使用记录接口对接的实现类 + * @author shuyongfu + * @since date:2017-04-01 + */ +public class HerpPathManagerImpl extends CommonHerpPathManagerImpl implements HerpPathManager { + + private Logger logger = Logger.getLogger(this.getClass()); + + private ObjectDao objectDao; + + private TousseInstanceManager tousseInstanceManager; + + private JdbcTemplate pathJdbcTemplate; + + private UseRecordManager useRecordManager; + + public void setTousseInstanceManager( + TousseInstanceManager tousseInstanceManager) { + this.tousseInstanceManager = tousseInstanceManager; + } + + public void setObjectDao(ObjectDao objectDao) { + this.objectDao = objectDao; + } + + public void setPathJdbcTemplate(JdbcTemplate pathJdbcTemplate) { + this.pathJdbcTemplate = pathJdbcTemplate; + } + + public void setUseRecordManager(UseRecordManager useRecordManager) { + this.useRecordManager = useRecordManager; + } + + @Override + public ObjectDao getObjectDao() { + return objectDao; + } + + @Override + public TousseInstanceManager getTousseInstanceManager() { + return tousseInstanceManager; + } + + @Override + public JdbcTemplate getPathJdbcTemplate() { + return pathJdbcTemplate; + } + + @Override + public UseRecordManager getUseRecordManager() { + return useRecordManager; + } + + @Override + public String submitUseRecordByPatientNumberAndApplicant(String patientNumber,String applicant) { + logger.debug("更新该病历的器械包使用记录接口:住院号patientNumber=" + patientNumber+",申请人applicant="+applicant); + final JSONObject jsonObject = new JSONObject(); + final List succUserecordIdList = new ArrayList();//回调执行成功的使用记录id + final List failUserecordIdList = new ArrayList();//回调执行失败的使用记录id + try { + final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + Date now = new Date(); + final String dateStr = sdf.format(now); + //如果定时器程序调用时不传病人号的参数 + if(StringUtils.isBlank(patientNumber)){ + String mainSql = "select um.id from UserecordMain um where 1=1 " + + "and id in (select userId from UserecordDetail ud where ud.status is null or ud.status <> 1)" + + "and (um.status is null or um.status <> 1) order by um.id"; + + //根据住院号或诊疗号查询,map的数据格式为:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + List idList = pathJdbcTemplate + .query(mainSql, + new ResultSetExtractor>() { + public List extractData(ResultSet rs) + throws SQLException, DataAccessException { + List idResultList = new ArrayList(); + while (rs.next()) { + Number mainId = (Number)rs.getObject("id"); + if(mainId != null){ + idResultList.add(mainId.longValue()); + } + } + return idResultList; + } + }); + + if(CollectionUtils.isNotEmpty(idList)){ + for(Long id : idList){ + String sql = "select um.id mainId,useDate,treatmentNum,hospitalNum,patientName,patientAge,patientSex,patientIDCard," + + " doctorName,operation,operationRoom,patientArea,roomNumber,bedNumber,ascriptionDepartment,washHandNurse," + + " circuitNurse,specialInfection,um.status mainStatus,ud.id detailId,userId,barCode,amount,ud.status detailStatus " + + " from UserecordMain um join UserecordDetail ud on um.id=ud.userId where 1=1 and um.id=" + id + + " and (um.status is null or um.status <> 1) and (ud.status is null or ud.status <> 1) order by um.id"; + //根据住院号或诊疗号查询,map的数据格式为:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + Map map = pathJdbcTemplate + .query(sql, + new ResultSetExtractor>() { + boolean success = true; + StringBuffer message = new StringBuffer(""); + @Override + public Map extractData(ResultSet rs) + throws SQLException, DataAccessException { + Map innerMap = new HashMap(); + while (rs.next()) { + String barCode = rs.getString("barcode"); + if(StringUtils.isBlank(barCode)){ + continue; + } + TousseInstance tousseInstance = tousseInstanceManager.getTousseInstanceByBarcode(barCode.trim()); + //1.如果bar_code对应到器械包条码不存在或者器械包条码存在但已关联了使用记录 + if(tousseInstance == null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统未找到。"); + failUserecordIdList.add(rs.getString("mainId")); + continue; + } + /*if(tousseInstance.getTousseFixedBarcode()){ + success = false; + message.append("条码:" + barCode + "为固定条码,不能用于使用记录。"); + failUserecordList.add(rs.getString("receive_id")); + continue; + }*/ + if(tousseInstance.getUseRecord_id() != null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统已生成使用记录。"); + //已生成使用记录的器械包不更新为失败 + continue; + } + + Object mainIdObj = rs.getObject("mainId"); + Object useDateObj = rs.getObject("useDate"); + String useDateStr = useDateObj == null ? dateStr : sdf.format((Date)useDateObj); + Object treatmentNumObj = rs.getObject("treatmentNum"); + Object hospitalNumObj = rs.getObject("hospitalNum"); + Object patientNameObj = rs.getObject("patientName"); + Object patientAgeObj = rs.getObject("patientAge"); + Object patientSexObj = rs.getObject("patientSex"); + Object patientIDCardObj = rs.getObject("patientIDCard"); + Object doctorNameObj = rs.getObject("doctorName"); + Object operationObj = rs.getObject("operation"); + Object operationRoomObj = rs.getObject("operationRoom"); + Object patientAreaObj = rs.getObject("patientArea"); + Object roomNumberObj = rs.getObject("roomNumber"); + Object bedNumberObj = rs.getObject("bedNumber"); + Object ascriptionDepartmentObj = rs.getObject("ascriptionDepartment"); + Object washHandNurseObj = rs.getObject("washHandNurse"); + Object circuitNurseObj = rs.getObject("circuitNurse"); + Object specialInfectionObj = rs.getObject("specialInfection"); + Object mainStatusObj = rs.getObject("mainStatus"); + Object detailIdObj = rs.getObject("detailId"); + Object amountObj = rs.getObject("amount"); + Object detailStatusObj = rs.getObject("detailStatus"); + + //barcode作为唯一的key + innerMap.put(barCode, new String[] { + String.valueOf(mainIdObj), useDateStr,String.valueOf(treatmentNumObj),String.valueOf(hospitalNumObj),String.valueOf(patientNameObj), + String.valueOf(patientAgeObj),String.valueOf(patientSexObj),String.valueOf(patientIDCardObj),String.valueOf(doctorNameObj),String.valueOf(operationObj), + String.valueOf(operationRoomObj),String.valueOf(patientAreaObj),String.valueOf(roomNumberObj),String.valueOf(bedNumberObj),String.valueOf(ascriptionDepartmentObj), + String.valueOf(washHandNurseObj),String.valueOf(circuitNurseObj),String.valueOf(specialInfectionObj),String.valueOf(mainStatusObj),String.valueOf(detailIdObj) + ,String.valueOf(amountObj),String.valueOf(detailStatusObj)}); + } + jsonObject.put("return", success ? "1" : "0"); + jsonObject.put("errMsg", message.toString()); + return innerMap; + } + }); + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + //map数据格式:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + generateUseRecordAndCreateRecyclingApplication(map , patientNumber, applicant , succUserecordIdList); + }else{ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "未找到相关医嘱信息或医嘱信息表的条码关联不到消毒供应系统的器械包条码"); + } + } + } + }else if(StringUtils.isNotBlank(patientNumber) && StringUtils.isNotBlank(applicant)){ + String sql = "select um.id mainId,useDate,treatmentNum,hospitalNum,patientName,patientAge,patientSex,patientIDCard," + + "doctorName,operation,operationRoom,patientArea,roomNumber,bedNumber,ascriptionDepartment,washHandNurse," + + "circuitNurse,specialInfection,um.status mainStatus,ud.id detailId,userId,barCode,amount,ud.status detailStatus " + + "from UserecordMain um join UserecordDetail ud on um.id=ud.userId where 1=1 "; + if(StringUtils.isNotBlank(patientNumber)){ + sql += "and (hospitalNum = '"+patientNumber+"' or treatmentNum = '"+patientNumber+"') "; + } + sql += "and (um.status is null or um.status <> 1) and (ud.status is null or ud.status <> 1) order by um.id"; + + //根据住院号或诊疗号查询,map的数据格式为:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + Map map = pathJdbcTemplate + .query(sql, + new ResultSetExtractor>() { + boolean success = true; + StringBuffer message = new StringBuffer(""); + @Override + public Map extractData(ResultSet rs) + throws SQLException, DataAccessException { + Map innerMap = new HashMap(); + while (rs.next()) { + String barCode = rs.getString("barcode"); + if(StringUtils.isBlank(barCode)){ + continue; + } + TousseInstance tousseInstance = tousseInstanceManager.getTousseInstanceByBarcode(barCode.trim()); + //1.如果bar_code对应到器械包条码不存在或者器械包条码存在但已关联了使用记录 + if(tousseInstance == null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统未找到。"); + failUserecordIdList.add(rs.getString("mainId")); + continue; + } + /*if(tousseInstance.getTousseFixedBarcode()){ + success = false; + message.append("条码:" + barCode + "为固定条码,不能用于使用记录。"); + failUserecordList.add(rs.getString("receive_id")); + continue; + }*/ + if(tousseInstance.getUseRecord_id() != null){ + success = false; + message.append("条码:" + barCode + "在消毒供应系统已生成使用记录。"); + //已生成使用记录的器械包不更新为失败 + continue; + } + + Object mainIdObj = rs.getObject("mainId"); + Object useDateObj = rs.getObject("useDate"); + String useDateStr = useDateObj == null ? dateStr : sdf.format((Date)useDateObj); + Object treatmentNumObj = rs.getObject("treatmentNum"); + Object hospitalNumObj = rs.getObject("hospitalNum"); + Object patientNameObj = rs.getObject("patientName"); + Object patientAgeObj = rs.getObject("patientAge"); + Object patientSexObj = rs.getObject("patientSex"); + Object patientIDCardObj = rs.getObject("patientIDCard"); + Object doctorNameObj = rs.getObject("doctorName"); + Object operationObj = rs.getObject("operation"); + Object operationRoomObj = rs.getObject("operationRoom"); + Object patientAreaObj = rs.getObject("patientArea"); + Object roomNumberObj = rs.getObject("roomNumber"); + Object bedNumberObj = rs.getObject("bedNumber"); + Object ascriptionDepartmentObj = rs.getObject("ascriptionDepartment"); + Object washHandNurseObj = rs.getObject("washHandNurse"); + Object circuitNurseObj = rs.getObject("circuitNurse"); + Object specialInfectionObj = rs.getObject("specialInfection"); + Object mainStatusObj = rs.getObject("mainStatus"); + Object detailIdObj = rs.getObject("detailId"); + Object amountObj = rs.getObject("amount"); + Object detailStatusObj = rs.getObject("detailStatus"); + + //barcode作为唯一的key + innerMap.put(barCode, new String[] { + String.valueOf(mainIdObj), useDateStr,String.valueOf(treatmentNumObj),String.valueOf(hospitalNumObj),String.valueOf(patientNameObj), + String.valueOf(patientAgeObj),String.valueOf(patientSexObj),String.valueOf(patientIDCardObj),String.valueOf(doctorNameObj),String.valueOf(operationObj), + String.valueOf(operationRoomObj),String.valueOf(patientAreaObj),String.valueOf(roomNumberObj),String.valueOf(bedNumberObj),String.valueOf(ascriptionDepartmentObj), + String.valueOf(washHandNurseObj),String.valueOf(circuitNurseObj),String.valueOf(specialInfectionObj),String.valueOf(mainStatusObj),String.valueOf(detailIdObj) + ,String.valueOf(amountObj),String.valueOf(detailStatusObj)}); + } + jsonObject.put("return", success ? "1" : "0"); + jsonObject.put("errMsg", message.toString()); + return innerMap; + } + }); + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + //map数据格式:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + generateUseRecordAndCreateRecyclingApplication(map , patientNumber, applicant , succUserecordIdList); + }else{ + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "未找到相关医嘱信息或医嘱信息表的条码关联不到消毒供应系统的器械包条码"); + } + } + } catch (RuntimeException e) { + failUserecordIdList.addAll(succUserecordIdList);//如果整体调用失败,则成功的记录也追加到失败的集合中 + updateRecordStatus(false , failUserecordIdList); + jsonObject.put("return", "0"); + jsonObject.put("errMsg", e.getMessage()); + logger.error("更新该病历的器械包使用记录异常", e); + } catch (Exception e) { + if(e.getMessage().indexOf("Could not get JDBC Connection") == -1){ + logger.error("根据住院号更新该病历的器械包使用记录报错:" + e); + failUserecordIdList.addAll(succUserecordIdList);//如果整体调用失败,则成功的记录也追加到失败的集合中 + }else{ + logger.error("herp系统path库连接获取失败导致根据住院号更新该病历的器械包使用记录报错:" + e); + } + try{ + if(e.getMessage().indexOf("Could not get JDBC Connection") == -1){ + updateRecordStatus(false , failUserecordIdList); + } + }catch(Exception e1){ + logger.error("更新herp path库该病历的医嘱信息记录失败时报错:" + e1); + } + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "调用回调接口过程中出现异常."); + logger.error("更新该病历的器械包使用记录异常", e); + } + return jsonObject.toString(); + } + + /** + * 根据结果集生成使用记录并创建申请单(除中山六院外的系统使用) + * @param map 数据格式:{barcode1=[mainId="",patientName="",....amount="1"],barcode2=[mainId="",patientName="",....amount="1"]} + * @param patientNumber + * @param applicant + */ + public boolean generateUseRecordAndCreateRecyclingApplication(Map map , + String patientNumber,String applicant , List succUserecordIdList) throws Exception{ + boolean success = true; + if(map != null && CollectionUtils.isNotEmpty(map.keySet())){ + //使用记录id + List idList = new ArrayList(); + String departCoding = null; + String depart = null; + Set departCodingSet = new HashSet(); + UseRecord useRecord = null; + + Date now = new Date(); + for(String barcode : map.keySet()){ + //数组顺序依次为:mainIdObj,useDateStr,treatmentNumObj,hospitalNumObj,patientNameObj, patientAgeObj,patientSexObj,patientIDCardObj, + //doctorNameObj,operationObj,operationRoomObj,patientAreaObj,roomNumberObj,bedNumberObj,ascriptionDepartmentObj,washHandNurseObj,circuitNurseObj,specialInfectionObj, + //mainStatusObj,detailIdObj,amountObj,detailStatusObj + String[] patientArray = map.get(barcode); + if(!succUserecordIdList.contains(patientArray[0])){ + succUserecordIdList.add(patientArray[0]); + } + + depart = patientArray[14]; + if(StringUtils.isBlank(depart)){ + throw new RuntimeException("科室为空"); + } + OrgUnit orgUnit = + (OrgUnit)objectDao.getBySql(OrgUnit.class.getSimpleName(), "where name='" + depart + "'"); + if(orgUnit == null){ + throw new RuntimeException("科室" + depart + "在供应系统不存在"); + } + + //构造病人信息 + PatientInfoVO patientInfo = new PatientInfoVO(); + patientInfo.setOperationTime(patientArray[1]); + patientInfo.setClinicNumber(patientArray[2]); + patientInfo.setHospitalNumber(patientArray[3]); + patientInfo.setPatientName(patientArray[4]); + patientInfo.setPatientAge(patientArray[5]); + patientInfo.setPatientSex(patientArray[6]); + patientInfo.setPatientIDCard(patientArray[7]); + patientInfo.setDoctorName(patientArray[8]); + patientInfo.setOperation(patientArray[9]); + patientInfo.setPatientArea(patientArray[11]); + patientInfo.setRoomNumber(patientArray[12]); + patientInfo.setBedNumber(patientArray[13]); + patientInfo.setAscriptionDepartment(depart); + + /** + * 病人vo类增加洗手护士、巡回护士、特殊感染类型 + */ + patientInfo.setWashHandNurse(patientArray[15]); + patientInfo.setCircuitNurse(patientArray[16]); + patientInfo.setSpecialInfection(patientArray[17]); + patientInfo.setType(patientNumber.equals(patientInfo.getHospitalNumber()) ? PatientInfoVO.TYPE_HOSPITAL : PatientInfoVO.TYPE_CLINIC); + + departCoding = orgUnit.getOrgUnitCoding(); + if(!departCodingSet.contains(departCoding)){ + //1.新增使用记录(同一个部门只生成一个使用记录) + useRecord = new UseRecord(); + useRecord.setDepartCoding(departCoding); + useRecord.setDepart(depart); + useRecord.setDoctorName(patientArray[8]); + useRecord.setEnteringDate(now); + + //录入日期取中间表的日期 + try{ + useRecord.setEnteringDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(patientInfo.getOperationTime())); + }catch(Exception e){ + logger.debug("使用记录录入日期转换赋值失败"); + try{ + useRecord.setEnteringDate(new SimpleDateFormat("yyyy-MM-dd").parse(patientInfo.getOperationTime())); + }catch(Exception e1){ + useRecord.setEnteringDate(now); + logger.debug("使用记录录入日期转换赋值失败"); + } + } + + useRecord.setApplicationTime(now);//由于回调时生成使用记录立即转换为申请单,所以申请时间也是当前时间 + useRecord.setPatientName(patientInfo.getPatientName()); + useRecord.setHospitalNum(patientInfo.getHospitalNumber()); + useRecord.setTreatmentNum(patientInfo.getClinicNumber()); + useRecord.setStatus(UseRecord.STATUS_APPLIED); + + //病人年龄 + if(StringUtils.isNotBlank(patientInfo.getPatientAge())){ + useRecord.setPatientAge(patientInfo.getPatientAge()); + } + + //病人性别 + if(StringUtils.isNotBlank(patientInfo.getPatientSex())){ + useRecord.setPatientSex(patientInfo.getPatientSex()); + } + + //病人身份证号 + if(StringUtils.isNotBlank(patientInfo.getPatientIDCard())){ + useRecord.setPatientIDCard(patientInfo.getPatientIDCard()); + } + + //医生姓名 + if(StringUtils.isNotBlank(patientInfo.getDoctorName())){ + useRecord.setDoctorName(patientInfo.getDoctorName()); + } + + //手术名称 + if(StringUtils.isNotBlank(patientInfo.getOperation())){ + useRecord.setOperation(patientInfo.getOperation()); + } + + // 病室 + if(StringUtils.isNotBlank(patientInfo.getRoomNumber())){ + useRecord.setRoomNumber(patientInfo.getRoomNumber()); + } + + // 床位 + if(StringUtils.isNotBlank(patientInfo.getBedNumber())){ + useRecord.setBedNumber(patientInfo.getBedNumber()); + } + + // 洗手护士 + if(StringUtils.isNotBlank(patientInfo.getWashHandNurse())){ + useRecord.setWashHandNurse(patientInfo.getWashHandNurse()); + } + + // 巡回护士 + if(StringUtils.isNotBlank(patientInfo.getCircuitNurse())){ + useRecord.setCircuitNurse(patientInfo.getCircuitNurse()); + } + + // 特殊感染类型 + if(StringUtils.isNotBlank(patientInfo.getSpecialInfection())){ + useRecord.setSpecialInfection(patientInfo.getSpecialInfection()); + } + useRecordManager.saveOrUpdate(useRecord); + + idList.add(useRecord.getId()); + + departCodingSet.add(departCoding); + } + TousseInstance tousseInstance = tousseInstanceManager.getTousseInstanceByBarcode(barcode); + if(tousseInstance != null){ + tousseInstance.setUseRecord_id(useRecord.getId()); + tousseInstance.setStatus(TousseInstance.STATUS_USED); + tousseInstanceManager.saveOrUpdate(tousseInstance); + } + } + + //原有老的方式调用 + if(success){ + useRecordManager.createRecyclingApplicationByUseRecords(StringUtils.join(idList.toArray(), ";"), applicant, depart, departCoding); + + //如果调用成功则将对应的医嘱信息表的记录的状态置为1 + updateRecordStatus(true , succUserecordIdList); + }else{ + updateRecordStatus(false , succUserecordIdList); + } + + }else{ + //暂不处理 + } + return success; + } + + /** + * 回调记录更新为成功或失败 + * @param success 是否成功或失败 + * @param userecordIdList herp接口使用记录id集合 + */ + public void updateRecordStatus(boolean success , List userecordIdList){ + String userecordUpdateSql = "update UserecordMain set status = " + + (success ? 1 : 0) + " where 1=2"; + String userecordDetailUpdateSql = "update UserecordDetail set status = " + + (success ? 1 : 0) + " where 1=2"; + if(CollectionUtils.isNotEmpty(userecordIdList)){ + String userecordIds = ""; + for (int i = 0; i < userecordIdList.size(); i++) { + userecordIds += "'" + userecordIdList.get(i) + "'"; + if(i < userecordIdList.size() - 1){ + userecordIds += ","; + } + } + userecordUpdateSql += " or id in (" + userecordIds + ")"; + userecordDetailUpdateSql += " or userId in (" + userecordIds + ")"; + } + try { + pathJdbcTemplate.update(userecordUpdateSql); + if(success){ + pathJdbcTemplate.update(userecordDetailUpdateSql); + } + } catch (Exception e) { + e.printStackTrace(); + logger.error(e); + } + } + + /** + * 手麻回调失败的记录重新回调 + * 定时器定时调用 + */ + @Override + public void timingSubmitUseRecord(){ + logger.info("定时调用手麻回调失败的记录重新回调的方法:" + new Date()); + String applicant = "系统定时回写"; + final JSONObject jsonObject = new JSONObject(); + try { + //病人号不传,执行所有未成功的病人号 + submitUseRecordByPatientNumberAndApplicant(null, applicant); + } catch (Exception e) { + jsonObject.put("return", "0"); + jsonObject.put("errMsg", "定时调用手麻回调失败的记录-调用回调接口过程中出现异常."); + logger.error("定时调用手麻回调失败的记录-更新该病历的器械包使用记录异常", e); + } + } +}