package service import ( "IC/models" "IC/utils" _ "IC/utils" "encoding/json" _ "encoding/json" "encoding/xml" _ "fmt" "github.com/astaxie/beego/httplib" _ "github.com/jinzhu/gorm" "net/url" "strconv" "strings" "time" ) type XmlKeyChyd struct { XMLName xml.Name `xml:"string"` StringName string `xml:"name,attr"` InnerText string `xml:",innerxml"` } // 获取艾迪康的key func GetAdKeyChyd(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) apiurl = Domain + apiurl XmlKeyChyd := XmlKeyChyd{} httplib.Get(apiurl).ToXML(&XmlKeyChyd) return XmlKeyChyd.InnerText } type ADStringChyd struct { XMLName xml.Name `xml:"string" json:"string"` NewDataSetChyd rpnewDataSetChyd `xml:"NewDataSet" json:"NewDataSet"` } type rpnewDataSetChyd struct { ListTableChyd []rplisttableChyd `xml:"listtable" json:"listtable"` } type rplisttableChyd struct { Id string `xml:"Id" json:"Id"` ReportType string `xml:"ReportType" json:"ReportType"` AdiconBarcode string `xml:"AdiconBarcode" json:"AdiconBarcode"` 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"` CustomerBarcode string `xml:"CustomerBarcode" json:"CustomerBarcode"` Repno string `xml:"Repno" json:"Repno"` // PdfFileName string `xml:"PdfFileName" json:"PdfFileName"` // DownFlag string `xml:"DownFlag" json:"DownFlag"` // DownDate string `xml:"DownDate" json:"DownDate"` Sjrq string `xml:"Sjrq" json:"Sjrq"` // Sjys string `xml:"Sjys" json:"Sjys"` // Brnl string `xml:"Brnl" json:"Brnl"` // Brxb string `xml:"Brxb" json:"Brxb"` Bbzl string `xml:"bbzl" json:"bbzl"` // Bzlbid string `xml:"Bzlbid" json:"Bzlbid"` // PDFFileType string `xml:"PDFFileType" json:"PDFFileType"` // PageType string `xml:"PageType" json:"PageType"` PatientNo string `xml:"PatientNo" json:"PatientNo"` // SfzId string `xml:"SfzId" json:"SfzId"` // CertificatesType string `xml:"CertificatesType" json:"CertificatesType"` // Nationality string `xml:"Nationality" json:"Nationality"` // SerialNumber string `xml:"SerialNumber" json:"SerialNumber"` // Str1 string `xml:"Str1" json:"Str1"` // Str2 string `xml:"Str2" json:"Str2"` // Str3 string `xml:"Str3" json:"Str3"` // Str4 string `xml:"Str4" json:"Str4"` // Str5 string `xml:"Str5" json:"Str5"` // TWBG string `xml:"TWBG" json:"TWBG"` } // 获取机构一段时间内检验检查列表 func GetAdGetReportListChyd(Key string, BeginDateTime string, EndDateTime string, TypeDateTime string, AgainFlag string) (key ADStringChyd) { Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetReportList" //apiurl := "Key=" + Key + "&BeginDateTime=" + BeginDateTime + "&EndDateTime=" + EndDateTime + "&TypeDateTime=" + TypeDateTime + "&AgainFlag=" + AgainFlag //apiurl = url.PathEscape(apiurl) //apiurl = Domain + apiurl //utils.InfoLog("url0: %v", apiurl) var xmlKey ADStringChyd req := httplib.Post(Domain) req.Param("Key", Key) req.Param("BeginDateTime", BeginDateTime) req.Param("EndDateTime", EndDateTime) req.Param("TypeDateTime", TypeDateTime) req.Param("AgainFlag", AgainFlag) xmlString, _ := req.String() xmlString = strings.Replace(xmlString, "<", "<", -1) xmlString = strings.Replace(xmlString, ">", ">", -1) xml.Unmarshal([]byte(xmlString), &xmlKey) return xmlKey } type AdkCheckChyd struct { Cgbg AdkSResult `json:"常规报告"` } type AdkSResult struct { Item []AdkItem `json:"item"` } type AdkItem struct { Adiconbarcode string `json:"AdiconBarcode"` Patientname string `json:"PatientName"` Sex string `json:"Sex"` Age string `json:"Age"` Reportdate string `json:"ReportDate"` Itemcode string `json:"ItemCode"` ItemnameCn string `json:"ItemName_CN"` ItemnameEn string `json:"ItemName_EN"` Result string `json:"Result"` Resulthint string `json:"ResultHint"` Resultreference string `json:"ResultReference"` Resultunit string `json:"ResultUnit"` Zhmc string `json:"zhmc"` } func GetAdkInfoChyd(key string, id string) (str AdkCheckChyd, checkStr string) { Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetJSONReportItemListByAdiconBarocde?" //apiurl := "key=" + key + "&AdiconBarocde=" + id //apiurl = url.PathEscape(apiurl) //apiurl = Domain + apiurl //utils.InfoLog("url1: %v", apiurl) xmlKey := XmlKeyChyd{} rep := httplib.Post(Domain) rep.Param("Key", key) rep.Param("AdiconBarcode", id) rep.ToXML(&xmlKey) utils.InfoLog("InnerText: %v", xmlKey.InnerText) var checkJSON AdkCheckChyd json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON) return checkJSON, xmlKey.InnerText } // 从化意达透析中心Lis同步 func SyncLisChyd(flag int64) (err error) { // 第一步:获取艾迪康下发的Key org_id := int64(10318) login := "RH0222" password := "Abc123123" key := GetAdKeyChyd(login, password) utils.InfoLog("key:%v", key) // 第二步:获取上一次同步的时间点 syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 1704038400 } if flag > 0 { sync_time = 1704038400 } 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") adString := GetAdGetReportListChyd(key, tempsyncTimes, tempsyncTimes, "2", "1") listtable := adString.NewDataSetChyd.ListTableChyd if len(listtable) >= 1 { // 第三步:根据检查列表获取的ID来获取检验检查详情 for _, list := range listtable { listId := list.AdiconBarcode checkInfo, _ := GetAdkInfoChyd(key, listId) //utils.InfoLog("checkStr: %v", checkStr) for _, info := range checkInfo.Cgbg.Item { // 根据姓名 去查询对应的patient_id patient, _ := GetPatientID(org_id, info.Patientname) utils.InfoLog("patient: %v", patient) if patient.ID > 0 { ChydReportInfoHandle(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 } } } } SyncToChyd() return } func SyncToChyd() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10318) // 第一步:跟进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("检验检查同步任务完成") } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetChydItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) { var inspection_reference models.MiddleInspectionReference err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error if inspection_reference.ID > 0 { return inspection_reference.ItemId, err } else { err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and project_id = ? ", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_reference).Error utils.InfoLog("inspection_reference: %v", inspection_reference) if inspection_reference.ItemId > 0 { return inspection_reference.ItemId + 1, err } else { return project_id*100 + 1, err } } } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetChydProjectID(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 1031801, err } } } func ChydReportInfoHandle(CheckInfo AdkItem, org_id int64, patientId int64) { tx := writeMiddleDb.Begin() var total int var RangeMin string var RangeMax string timstr := strings.Split(CheckInfo.Reportdate, ".") recordDateStr := timstr[0] if len(recordDateStr) == 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") // // 判断检查类型 ItemType := 1 Range := strings.Split(CheckInfo.Resultreference, "-") if len(Range) > 1 { RangeMin = Range[0] RangeMax = Range[1] ItemType = 1 } else { ItemType = 2 } var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project = ? and item_code = ? and status = 1", org_id, CheckInfo.Zhmc, CheckInfo.Itemcode).Find(&inspection_reference).Count(&total).Error if inspection_reference.ID > 0 { ItemType = inspection_reference.RangeType } if total <= 0 { // 如果配置表里没有相关配置,则创建project_id,item_id和相关配置信息 project_id, _ := GetChydProjectID(org_id, CheckInfo.Zhmc) item_id, _ := GetChydItemID(org_id, CheckInfo.Zhmc, CheckInfo.ItemnameCn, project_id) inspection_reference.OrgId = org_id inspection_reference.ProjectName = CheckInfo.Zhmc inspection_reference.Project = CheckInfo.Zhmc inspection_reference.ProjectId = project_id inspection_reference.ItemName = CheckInfo.ItemnameCn inspection_reference.ItemNameAddition = CheckInfo.ItemnameEn inspection_reference.ItemId = item_id inspection_reference.ItemCode = CheckInfo.Itemcode inspection_reference.RangeType = ItemType inspection_reference.RangeMin = RangeMin inspection_reference.RangeMax = RangeMax // inspection_reference.RangeValue = RangeValue //inspection_reference.RangeOptions = RangeOptions inspection_reference.Unit = CheckInfo.Resultunit 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 inspection_reference.Adiconbarcode = CheckInfo.Adiconbarcode 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, inspection_reference.ProjectId, inspection_reference.ItemId, record_date, patientId).Find(&inspection).Count(&itotal).Error if itotal <= 0 { inspection.PatientId = patientId inspection.OrgId = org_id inspection.ProjectId = inspection_reference.ProjectId inspection.ItemName = inspection_reference.ItemName inspection.ProjectName = inspection_reference.ProjectName inspection.InspectType = ItemType inspection.ItemId = inspection_reference.ItemId inspection.InspectValue = CheckInfo.Result inspection.InspectDate = inspect_date inspection.RecordDate = record_date //inspection.InspectTips = result.AntiCode inspection.Status = 1 inspection.CreatedTime = time.Now().Unix() inspection.UpdatedTime = time.Now().Unix() inspection.UTime = inspect_date inspection.HisUserId = strconv.FormatInt(patientId, 10) inspection.Adiconbarcode = CheckInfo.Adiconbarcode err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error if err != nil { tx.Rollback() } } tx.Commit() }