package service import ( "IC/models" "IC/utils" "encoding/json" "encoding/xml" "github.com/astaxie/beego/httplib" "net/url" "strconv" "strings" "time" ) type RequestResultGzjh struct { Msg string `json:"msg"` ResultGzjh []struct { AdviceId int64 `json:"医嘱ID"` OtherId int64 `json:"相关ID"` Source int `json:"病人来源"` PatientId int64 `json:"病人ID"` JzId int64 `json:"就诊ID"` BabyId int `json:"婴儿ID"` PatientDepartmentId int `json:"病人科室ID"` OrderId int `json:"次序"` AdviceStatus int `json:"医嘱状态"` AdviceType int `json:"医嘱类型"` DiagnosisType string `json:"诊疗类型"` DiagnosisItemId int `json:"诊疗项目ID"` CheckMethd string `json:"检查方法"` ChargeItemId int `json:"收费项目ID"` Day int `json:"天数"` JiLiang float64 `json:"剂量"` YongLiang string `json:"用量"` DanLiang float64 `json:"单量"` Shuliang float64 `json:"数量"` Advice string `json:"医嘱"` GeiYaoTuJing string `json:"给药途径"` ShuoMing string `json:"说明"` ZhiXingKeShiId int `json:"执行科室ID"` ZhiXingKeShi string `json:"执行科室"` PN string `json:"P(+)或N(-)表示阳性或阴性"` ZhiXingPinCi string `json:"执行频次"` PinLvCiShu int `json:"频率次数"` PinLvJianGe int `json:"频率间隔"` JianGeDanWei string `json:"间隔单位"` ZhiXingShiJianFangAn string `json:"执行时间方案"` JiJiaTeXing int `json:"计价特性"` ZhiXingXingZhi int `json:"执行性质"` ZhiXingBiaoZhi int `json:"执行标记"` KeFouFenLing int `json:"可否分零"` JinJiBiaoZhi int `json:"紧急标志"` KaiShiZhiXingShiJian int64 `json:"开始执行时间"` ZhiXingZhongZhiShiJian int64 `json:"执行终止时间"` ShangCiZhiXingShiJian int64 `json:"上次执行时间"` KaiZhuKeShiId int `json:"开嘱科室ID"` DoctorCode string `json:"医师编码"` Doctor string `json:"开嘱医师"` AdviceTime int64 `json:"开嘱时间"` StopTime int64 `json:"停嘱时间"` ApplyId int `json:"申请ID"` UploadLable int `json:"上传标志"` CheckResult int `json:"审查结果"` NoPrint int `json:"忽略打印"` Summary string `json:"摘要"` AdviceOrderId int64 `json:"医嘱单id"` GeiYaoKeShi int `json:"给药科室"` GeiYaoXingZhi int `json:"给药性质"` GroupNo int `json:"组号"` Speed string `json:"滴速"` ShouRiCiShu float64 `json:"首日次数"` MoRiCiShu float64 `json:"末日次数"` AdviceDoctorId int `json:"开嘱医师ID"` AdviceNurseId int `json:"开嘱护士ID"` OperaId int `json:"操作员ID"` Opera string `json:"操作员"` CheckTime int64 `json:"审核时间"` ReplyAdviceTime int64 `json:"重整医嘱时间"` } `json:"result"` Code string `json:"code"` } type XmlKeyGzjh struct { XMLName xml.Name `xml:"string"` StringName string `xml:"name,attr"` InnerText string `xml:",innerxml"` } // 获取艾迪康的key func GetGzjhAdKey(logid string, password string) (key string) { Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/Login?" apiurl := "logid=" + logid + "&password=" + password apiurl = url.PathEscape(apiurl) utils.InfoLog("apiUrl: %v", apiurl) apiurl = Domain + apiurl xmlKey := XmlKeyGzjh{} httplib.Get(apiurl).ToXML(&xmlKey) utils.InfoLog("xmlKey: %v", xmlKey) return xmlKey.InnerText } type ADStringGzjh struct { XMLName xml.Name `xml:"string" json:"string"` NewDataSet rpnewDataSetGzjh `xml:"NewDataSet" json:"NewDataSetGzjh"` } type rpnewDataSetGzjh struct { ListTable []rplisttableGzjh `xml:"listtable" json:"listtableGzjh"` } type rplisttableGzjh struct { Id string `xml:"Id" json:"Id"` PatientName string `xml:"PatientName" json:"PatientName"` Bgrq string `xml:"Bgrq" json:"Bgrq"` Instrument string `xml:"Instrument" json:"Instrument"` LisDate string `xml:"LisDate" json:"LisDate"` Sampleid string `xml:"Sampleid" json:"Sampleid"` } type ReportListXml struct { XMLName xml.Name `xml:"GetReportList"` Key string `xml:"Key"` BeginDateTime string `xml:"BeginDateTime"` EndDateTime string `xml:"EndDateTime"` TypeDateTime string `xml:"TypeDateTime"` AgainFlag string `xml:"AgainFlag"` } // 获取机构一段时间内检验检查列表 func GetAdGetReportListGzjh(Key string, BeginDateTime string, EndDateTime string, TypeDateTime string, AgainFlag string) (key ADStringGzjh) { Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetReportList" var xmlKey ADStringGzjh reportPost := httplib.Post(Domain) reportPost.Param("Key",Key) reportPost.Param("BeginDateTime",BeginDateTime) reportPost.Param("EndDateTime",EndDateTime) reportPost.Param("TypeDateTime",TypeDateTime) reportPost.Param("AgainFlag",AgainFlag) reportPost.Header("Content-Type", "application/xml") xmlString,_ := reportPost.String() xmlString = strings.Replace(xmlString, "<", "<", -1) xmlString = strings.Replace(xmlString, ">", ">", -1) xml.Unmarshal([]byte(xmlString), &xmlKey) return xmlKey } type adkCheckGzjh struct { ApplyId string `json:"applyid"` ReportNo string `json:"reportno"` PatName string `json:"patname"` Sex string `json:"sex"` Age string `json:"age"` DptName string `json:"dptname"` Testtype string `json:"testtype"` Testcode string `json:"testcode"` ExecuteTime string `json:"receivetime"` Checktime string `json:"checktime"` Checkcode string `json:"checkcode"` Result []adkResultGzjh `json:"result"` } func GetAdkInfoGzjh(key string, id string) (str []adkCheckGzjh, checkStr string) { Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetJSONReportItemListById" reportPost := httplib.Post(Domain) reportPost.Param("Key",key) reportPost.Param("id",id) reportPost.Header("Content-Type", "application/xml") xmlKey := XmlKey{} reportPost.ToXML(&xmlKey) var checkJSON []adkCheckGzjh json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON) return checkJSON, xmlKey.InnerText } // 广州暨华血液透析中心Lis同步 func SyncLisGzjhtx() (err error) { // 第一步:获取艾迪康下发的Key org_id := int64(10015) login := "RH0046" password := "Abc123123" key := GetGzjhAdKey(login, password) utils.InfoLog("key: %v", key) // 第二步:获e取上一次同步的时间点 syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime + 86400 } else { sync_time = 1672502400 } nowTimes := time.Now().Unix() // syncTimes := time.Unix(sync_time,0).Format("2006-01-02") if sync_time < nowTimes { for i := sync_time; i < nowTimes; i = i + 86400 { tempsyncTimes := time.Unix(i, 0).Format("2006-01-02") utils.InfoLog("tempsyncTimes:%v", tempsyncTimes) adString := GetAdGetReportListGzjh(key, tempsyncTimes, tempsyncTimes, "2", "2") listtable := adString.NewDataSet.ListTable utils.InfoLog("listtable: %v", listtable) if len(listtable) > 3 { // 第三步:根据检查列表获取的ID来获取检验检查详情 for _, list := range listtable { listId := list.Id utils.InfoLog("listId: %v", listId) checkInfo, checkStr := GetAdkInfoGzjh(key, listId) utils.InfoLog("checkInfo: %v", checkInfo) for _, info := range checkInfo { // 根据姓名 去查询对应的patient_id patient, _ := GetPatientIDGzjh(org_id, info.PatName) utils.InfoLog("patient: %v", patient) if patient.ID > 0 { middleReportHandle := models.MiddleReportHandle{ OrgId: org_id, HisUserId: strconv.FormatInt(patient.ID, 10), ApplyNo: info.ApplyId, ReportId: info.ReportNo, Name: info.PatName, Sex: info.Sex, Age: info.Age, Dept: info.DptName, PatientId: strconv.FormatInt(patient.ID, 10), RequestTime: info.ExecuteTime, ExecuteTime: info.ExecuteTime, ReceiveTime: info.ExecuteTime, CheckTime: info.ExecuteTime, ReportInfo: checkStr, CreatedTime: time.Now().Unix(), UpdatedTime: time.Now().Unix(), } handleerr := CreateReportHandle(&middleReportHandle) if handleerr != nil { utils.ErrorLog("创建检验检查操作数据失败:%v", handleerr) return } GzjhReportInfoHandle(info, org_id, patient.ID) } } } var syncInfo models.MiddleSyncInfo syncInfo.OrgId = org_id syncInfo.SyncTime = i syncInfo.SyncResultType = 1 syncInfo.SyncRsultRemark = "同步成功" syncInfo.SyncTotalNum = 0 syncInfo.SyncSuccessNum = 0 syncInfo.SyncInfo = "" syncInfo.CreateTime = time.Now().Unix() syncInfo.UpdateTime = time.Now().Unix() cwderr := CreateSyncInfo(&syncInfo) if cwderr != nil { utils.ErrorLog("创建同步信息失败:%v", cwderr) return } } } } SyncToGzczjhtx() return } // 根据姓名获取patient_id func GetPatientIDGzjh(org_id int64, patient_name string) (patient models.Patients, err error) { err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 and name = ? ", org_id, patient_name).First(&patient).Error return } func GetProjectInfoGzjh(item_id int64, org_id int64) (project_info models.MiddleInspectionReference, err error) { err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_id = ?", org_id, item_id).First(&project_info).Error return } // 修改配置信息 // func UpdateInspectionReference(InspectionReference *models.MiddleInspectionReference) error { // err := readMiddleDb.Model(&models.MiddleInspectionReference{}).Updates(&InspectionReference).Error // return err // } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetGzzcjhProjectID(org_id int64, project_name string) (project_id int64, err error) { var inspection_reference models.MiddleInspectionReference err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error if inspection_reference.ID > 0 { return inspection_reference.ProjectId, err } else { err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error utils.InfoLog("inspection_reference: %v", inspection_reference) if inspection_reference.ProjectId > 0 { return inspection_reference.ProjectId + 1, err } else { return 1001501, err } } } // // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 // func GetGzjhProjectID(org_id int64, project_name string) (project_id int64, err error) { // var inspection_reference models.MiddleInspectionReference // err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error // if inspection_reference.ID > 0 { // return inspection_reference.ProjectId, err // } else { // err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error // if inspection_reference.ProjectId > 0 { // return inspection_reference.ProjectId + 1, err // } else { // return 116001, err // } // } // } func GzjhReportInfoHandle(CheckInfo adkCheckGzjh, org_id int64, patientId int64) { // 1.先把所有的检查项弄出来 for _, info := range CheckInfo.Result { // 根据project_id和item_id 判断该项是否存在 curitemcode, _ := strconv.ParseInt(info.Itemcode, 10, 64) reference, _ := GetProjectInfoGzjh(curitemcode, org_id) utils.InfoLog("info: %v", info) utils.InfoLog("reference: %v", info) if reference.ID > 0 { if reference.ProjectId > 0 { LgthYsSyncGzjh(info, reference.ProjectId, curitemcode, org_id, patientId, CheckInfo.ExecuteTime) } else { project_id := int64(0) if len(info.Groupitemcode) > 0 { // 对没有project信息的项目进行修改 project_id, _ = GetGzzcjhProjectID(org_id, info.Groupitemname) reference.ProjectId = project_id reference.ProjectName = info.Groupitemcode reference.Project = info.Groupitemcode // 修改配置信息 UpdateInspectionReference(&reference) } LgthYsSyncGzjh(info, project_id, curitemcode, org_id, patientId, CheckInfo.ExecuteTime) } } else { project_id := int64(0) if len(info.Groupitemcode) > 0 { project_id, _ = GetGzzcjhProjectID(org_id, info.Groupitemname) } LgthYsSyncGzjh(info, project_id, curitemcode, org_id, patientId, CheckInfo.ExecuteTime) } } // SyncLisGzjhtx() } type adkResultGzjh struct { Testno string `json:"testno"` Reportno string `json:"reportno"` Itemord string `json:"itemord"` Itemcode string `json:"itemcode"` Itemname string `json:"itemname"` ItemEname string `json:"itemename"` Result string `json:"result"` Units string `json:"units"` Testdate string `json:"testdate"` Eqpname string `json:"eqpname"` Groupitemcode string `json:"groupitemcode"` Groupitemname string `json:"groupitemname"` Limit string `json:"limit"` Resultstate string `json:"resultstate"` Criticalflag string `json:"criticalflag"` Itemename string `json:"itemename"` } // 从机构将数据同步到中间库 func LgthYsSyncGzjh(report adkResultGzjh, project_id int64, item_id int64, org_id int64, patient_id int64, receivetime string) (*models.MiddleInspectionReference, error) { tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference recordDateStr := receivetime if len(receivetime) == 0 { recordDateStr = time.Now().Format("2006-01-02 15:04") } record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr) record_date := record_date_str.Unix() inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04") // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr) // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02")) var total int var RangeOptions string var RangeMin string var RangeMax string // // 判断检查类型 ItemType := 1 Range := strings.Split(report.Limit, "-") if len(Range) > 1 { RangeMin = Range[0] RangeMax = Range[1] ItemType = 1 } else { ItemType = 2 RangeOptions = report.Limit } err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error if inspection_reference.ID > 0 { ItemType = inspection_reference.RangeType } if total <= 0 { inspection_reference.OrgId = org_id inspection_reference.ProjectName = report.Groupitemname inspection_reference.Project = report.Groupitemcode inspection_reference.ProjectId = project_id inspection_reference.ItemName = report.Itemname inspection_reference.ItemNameAddition = report.ItemEname inspection_reference.ItemId = item_id inspection_reference.RangeType = ItemType inspection_reference.RangeMin = RangeMin inspection_reference.RangeMax = RangeMax // inspection_reference.RangeValue = RangeValue inspection_reference.RangeOptions = RangeOptions inspection_reference.Unit = report.Units inspection_reference.Status = 1 inspection_reference.CreatedTime = time.Now().Unix() inspection_reference.UpdatedTime = time.Now().Unix() inspection_reference.InspectDate = inspect_date inspection_reference.UTime = inspect_date err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error if err != nil { tx.Rollback() } } var itotal int err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, record_date, patient_id).Find(&inspection).Count(&itotal).Error if itotal <= 0 { report.Result = strings.Replace(report.Result, ">", ">", -1) report.Result = strings.Replace(report.Result, "<", "<", -1) inspection.PatientId = patient_id inspection.OrgId = org_id inspection.ProjectId = project_id inspection.ItemName = inspection_reference.ItemName inspection.ProjectName = inspection_reference.ProjectName inspection.InspectType = ItemType inspection.ItemId = item_id inspection.InspectValue = report.Result inspection.InspectDate = inspect_date inspection.RecordDate = record_date inspection.InspectTips = report.Resultstate inspection.Status = 1 inspection.CreatedTime = time.Now().Unix() inspection.UpdatedTime = time.Now().Unix() inspection.UTime = inspect_date inspection.HisUserId = strconv.FormatInt(patient_id, 10) err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error if err != nil { tx.Rollback() } } // //处理检验检查操作表里同步条数和状态 // cur_num := Head.ReportInfoItemHandleNum + 1 // Head.ReportInfoItemHandleNum = cur_num // handleType := 2 // if cur_num == Head.ReportInfoItemNum { // handleType = 1 // } // middleReportHandle := models.MiddleReportHandle{ // ID: Head.ID, // ReportInfoItemHandleNum: cur_num, // HandleType: int64(handleType), // UpdatedTime: time.Now().Unix(), // } // err = UpdateReportHandleListyOrgID(&middleReportHandle) // if err != nil { // tx.Rollback() // } // } tx.Commit() return nil, err } // 根据姓名获取patient_id func GetPatientGzjhListGzjh(org_id int64, schedulesTime int64) (schedules []*models.DialysisSchedule, err error) { err = readDb.Model(&models.DialysisSchedule{}).Where("user_org_id = ? and status = 1 and schedule_date = ?", org_id, schedulesTime).Preload("Patients", "status = 1").Find(&schedules).Error return } // 根据姓名获取admin_user_id func GetAdminUserIdGzjh(org_id int64, doctor_name string) (doctor models.UserAdminRole, err error) { err = writeUserDb.Model(&models.UserAdminRole{}).Where("org_id = ? and status = 1 and user_name = ?", org_id, doctor_name).First(&doctor).Error return } func GetPatientGzjhByOrgIdGzjh(org_id int64) (patientList []*models.Patients, err error) { err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patientList).Error return } func SyncToGzczjhtx() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10015) // 第一步:跟进org_id 去中间库查出需要同步的数据 inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id) inspections, _ := GetSyncInspectionByOrgId(org_id) // 第二步:将数据同步到业务库 if len(inspection_references) > 0 { for _, inspection_reference := range inspection_references { SyncInspectionReference(&inspection_reference) } } if len(inspections) > 0 { for _, inspection := range inspections { SyncInspection(&inspection) } } utils.SuccessLog("检验检查同步任务完成") }