package service // 广州康允血液透析中心 检验检查对接 import ( "IC/models" "IC/utils" _ "encoding/json" _ "encoding/xml" "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm" _ "strconv" "strings" "time" ) type gzkylis struct { Finputdate string `gorm:"column:F_InputDate" json:"F_InputDate"` Fname string `gorm:"column:F_Name" json:"F_Name"` Fsex string `gorm:"column:F_Sex" json:"F_Sex"` Fage string `gorm:"column:F_Age" json:"F_Age"` Fhospnaturalitem string `gorm:"column:F_HospNaturalItem" json:"F_HospNaturalItem"` Fhospnaturalitemname string `gorm:"column:F_HospNaturalItemName" json:"F_HospNaturalItemName"` Fhospitem string `gorm:"column:F_HospItem" json:"F_HospItem"` Fhospitemname string `gorm:"column:F_HospItemName" json:"F_HospItemName"` Fresult string `gorm:"column:F_Result" json:"F_Result"` Funit string `gorm:"column:F_Unit" json:"F_Unit"` Fhint string `gorm:"column:F_Hint" json:"F_Hint"` Freference string `gorm:"column:F_Reference" json:"F_Reference"` Frecordtime string `gorm:"column:F_RecordTime" json:"F_RecordTime"` } func (gzkylis) TableName() string { return "V_KM_LIS_Result" } func GetGzkyLis(rdb *gorm.DB, synctime string) (record []*gzkylis, err error) { err = rdb.Model(&gzkylis{}).Where("F_RecordTime >= ? ", synctime).Find(&record).Error return } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetGzkyProjectID(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 1056701, err } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetGzkyItemID(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 if inspection_reference.ItemId > 0 { return inspection_reference.ItemId + 1, err } else { return project_id*100 + 1, err } } } // 汕头三优血液透析中心 func SyncGzkyLis() (err error) { org_id := int64(10567) org := &models.DataUploadConfig{ OrgId: org_id, DbHost: "127.0.0.1", DbPort: "1433", DbPass: "1Q2W3e4r!@#$", DbUser: "kyy", DbName: "kmdb", } orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName) if err != nil { utils.ErrorLog("创建数据库连接失败:%v", err) return } // var sync_time int64 // sync_time = 1627747200 // scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682") // utils.InfoLog("IdCardNo:%v",scpaLis) // 第一步:获取上一次同步的时间点 syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 1685548800 } SyncTime := time.Unix(sync_time, 0) syncTimeStr := SyncTime.Format("2006-01-02 15:04:05") fmt.Println(SyncTime) // 第二步:获取时间内所有检验结果 gzkyLis, _ := GetGzkyLis(orgDb, syncTimeStr) if len(gzkyLis) > 0 { for _, LisInfo := range gzkyLis { utils.InfoLog("LisInfo:%v", LisInfo) utils.InfoLog("患者姓名:%v", LisInfo.Fname) brxm := LisInfo.Fname // 根据姓名获取患者ID patient, _ := GetUserInfoByName(org_id, brxm) if patient.ID > 0 { project_id, _ := GetGzkyProjectID(org_id, LisInfo.Fhospnaturalitemname) item_id, _ := GetGzkyItemID(org_id, LisInfo.Fhospnaturalitemname, LisInfo.Fhospitemname, project_id) tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference tempTime := strings.Split(LisInfo.Frecordtime, "T") utils.InfoLog("tempTime1:%v", tempTime[0]) utils.InfoLog("tempTime2:%v", tempTime[1]) tempTime1 := strings.Split(tempTime[1], "Z") tempRecord := tempTime[0] + " " + tempTime1[0] utils.InfoLog("tempTime3:%v", tempRecord) recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord) utils.InfoLog("tempTime4:%v", recordTime) inspect_date := recordTime.Format("2006-01-02 15:04") recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date) recordTimeInt := recordTimeStr.Unix() utils.InfoLog("tempTime5:%v", recordTimeInt) utils.InfoLog("tempTime6:%v", recordTimeStr) var total int var RangeOptions string var RangeMin string var RangeMax string var ItemType int typeFlag := strings.Contains(LisInfo.Freference, "阴性") if typeFlag { ItemType = 2 RangeMin = LisInfo.Freference RangeOptions = LisInfo.Fhint } else { Range := strings.Split(LisInfo.Freference, "-") if len(Range) > 1 { RangeMin = Range[0] RangeMax = Range[1] ItemType = 1 } else { ItemType = 2 RangeMin = LisInfo.Freference } RangeOptions = LisInfo.Fhint } 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 total <= 0 { inspection_reference.OrgId = org_id inspection_reference.ProjectName = LisInfo.Fhospnaturalitemname inspection_reference.Project = LisInfo.Fhospnaturalitem inspection_reference.ProjectId = project_id inspection_reference.ItemName = LisInfo.Fhospitemname inspection_reference.ItemNameAddition = LisInfo.Fhospitem inspection_reference.ItemId = item_id inspection_reference.RangeType = ItemType inspection_reference.RangeMin = RangeMin inspection_reference.RangeMax = RangeMax inspection_reference.RangeValue = LisInfo.Freference inspection_reference.RangeOptions = RangeOptions inspection_reference.Unit = LisInfo.Funit inspection_reference.Status = 1 inspection_reference.CreatedTime = time.Now().Unix() inspection_reference.UpdatedTime = time.Now().Unix() inspection_reference.InspectDate = tempRecord inspection_reference.UTime = tempRecord 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, recordTimeStr, patient.ID).Find(&inspection).Count(&itotal).Error if itotal <= 0 { // inspection.InspectValue = strings.Replace(LisInfo.Testresult, ">", ">", -1) // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "<", "<", -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 = LisInfo.Fresult inspection.InspectDate = inspect_date inspection.RecordDate = recordTimeInt inspection.InspectTips = LisInfo.Fhint inspection.Status = 1 inspection.CreatedTime = time.Now().Unix() inspection.UpdatedTime = time.Now().Unix() // inspection.UTime = record_date.Format(timeLayout) // inspection.HisUserId = strconv.FormatInt(patient_id, 10) inspection.SysProjectId = inspection_reference.XtProjectId inspection.SysItemId = inspection_reference.XtItemId err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error if err != nil { tx.Rollback() } } tx.Commit() } else { continue } } } var syncInfo models.MiddleSyncInfo syncInfo.OrgId = org_id syncInfo.SyncTime = time.Now().Unix() 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 } SyncToGzkytx() return } // func deleteMid(tx *gorm.DB) (err error) { // err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," + // "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," + // "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + // "WHERE t1.user_org_id = t2.orgid)tmp " + // "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + // "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error // return // } func SyncToGzkytx() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10567) // 第一步:跟进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 { if inspection.SysProjectId > 0 && inspection.SysItemId > 0 { SyncGzkyInspection(&inspection) } } } utils.SuccessLog("检验检查同步任务完成") } // 从机构将数据同步到中间库 func SyncGzkyInspection(data *models.MiddleInspection) error { tx := writeDb.Begin() var inspection models.Inspection var total int err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and item_id = ? and patient_id =? and inspect_date=? and status = 1", data.OrgId, data.ProjectId, data.ItemId, data.PatientId, data.RecordDate).Find(&inspection).Count(&total).Error if total <= 0 { inspection.OrgId = data.OrgId inspection.PatientId = data.PatientId inspection.ProjectName = data.ProjectName // inspection.Project = data.ProjectName inspection.ProjectId = data.SysProjectId inspection.ItemName = data.ItemName inspection.ItemId = data.SysItemId inspection.InspectType = int64(data.InspectType) inspection.InspectValue = data.InspectValue inspection.InspectTips = data.InspectTips inspection.InspectDate = data.RecordDate inspection.Status = 1 inspection.CreatedTime = time.Now().Unix() inspection.UpdatedTime = time.Now().Unix() err := tx.Model(&models.Inspection{}).Create(&inspection).Error if err != nil { tx.Rollback() } data.IsSync = 1 data.SyncId = inspection.ID data.UpdatedTime = time.Now().Unix() ierr := writeMiddleDb.Save(&data).Error if ierr != nil { tx.Rollback() } } tx.Commit() return err }