package service import ( "IC/models" "IC/utils" "github.com/jinzhu/gorm" "strings" "time" ) type sgyhsLisReportResult struct { Itemcode string `gorm:"column:itemcode" json:"itemcode"` Itemname string `gorm:"column:itemname" json:"itemname"` Result string `gorm:"column:result" json:"result"` Unit string `gorm:"column:unit" json:"unit"` Refrange string `gorm:"column:refrange" json:"refrange"` Ts string `gorm:"column:ts" json:"ts"` Repdate string `gorm:"column:repdate" json:"repdate"` Name string `gorm:"column:name" json:"name"` DockingapplyitemName string `gorm:"column:DockingApplyItemName" json:"DockingApplyItemName"` Sex string `gorm:"column:sex" json:"sex"` Age string `gorm:"column:age" json:"age"` } func (sgyhsLisReportResult) TableName() string { return "v_cm_result" } func GetSgyhsLisReport(rdb *gorm.DB, synctime string) (record []*sgyhsLisReportResult, err error) { err = rdb.Model(&sgyhsLisReportResult{}).Where("repdate >= ?", synctime).Find(&record).Error return } // 韶关始兴源和圣血液透析中心Lis同步 func SyncLisSgyhs() (err error) { org_id := int64(10571) org := &models.DataUploadConfig{ OrgId: org_id, DbHost: "192.168.3.75", DbPort: "1433", DbPass: "hy@123456.", DbUser: "sa", DbName: "hyDB", } orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName) if err != nil { utils.ErrorLog("创建数据库连接失败:%v", err) return } // 第一步:获取上一次同步的时间点 syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 1698768000 // 2023-01-01 } SyncTime := time.Unix(sync_time, 0) syncTimeStr := SyncTime.Format("2006-01-02 15:04:05") utils.InfoLog("syncTimeStr:%v", syncTimeStr) // 第二步:获取时间内所有检验结果 sgyhsLisResult, _ := GetSgyhsLisReport(orgDb, syncTimeStr) if len(sgyhsLisResult) > 0 { for _, Result := range sgyhsLisResult { utils.InfoLog("Report:%v", Result) if Result.Name == "陈永中" { Result.Name = "陈永忠" } if Result.Name == "朱小飞" { Result.Name = "朱小妃" } patient, _ := GetUserInfoByName(org_id, Result.Name) if patient.ID > 0 { project_id, _ := GetSgyhsProjectID(org_id, Result.DockingapplyitemName) item_id, _ := GetSgyhsItemID(org_id, Result.DockingapplyitemName, Result.Itemname, project_id) tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference tempTime := strings.Split(Result.Repdate, "T") tempTime1 := strings.Split(tempTime[1], "Z") tempRecord := tempTime[0] + " " + tempTime1[0] recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord) inspect_date := recordTime.Format("2006-01-02 15:04") recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date) recordTimeInt := recordTimeStr.Unix() var total int var RangeOptions string var RangeMin string var RangeMax string var ItemType int Range := strings.Split(Result.Refrange, "-") if len(Range) > 1 { Range1 := strings.Split(Range[0], "[") if len(Range1) > 1 { Range2 := strings.Split(Range[1], "]") RangeMin = Range1[1] RangeMax = Range2[0] ItemType = 1 } else { ItemType = 2 RangeMin = Result.Refrange } } else { ItemType = 2 RangeMin = Result.Refrange } RangeOptions = Result.Ts 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 = Result.DockingapplyitemName inspection_reference.Project = Result.DockingapplyitemName inspection_reference.ProjectId = project_id inspection_reference.ItemName = Result.Itemname inspection_reference.ItemNameAddition = Result.Itemcode inspection_reference.ItemId = item_id inspection_reference.RangeType = ItemType inspection_reference.RangeMin = RangeMin inspection_reference.RangeMax = RangeMax inspection_reference.Unit = Result.Unit 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 = Result.Result inspection.InspectDate = inspect_date inspection.RecordDate = recordTimeInt inspection.InspectTips = RangeOptions 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 } } SyncToSgyhs() return } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetSgyhsProjectID(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 1057101, err } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetSgyhsItemID(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 SyncToSgyhs() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10571) // 第一步:跟进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("检验检查同步任务完成") }