package service import ( "IC/models" "IC/utils" "github.com/jinzhu/gorm" "strings" "time" ) type pljxLisReportResult 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 (pljxLisReportResult) TableName() string { return "v_cm_result" } func GetPljxLisReport(rdb *gorm.DB, synctime string) (record []*pljxLisReportResult, err error) { err = rdb.Model(&pljxLisReportResult{}).Where("repdate >= ?", synctime).Find(&record).Error return } // 普宁锦熹血液透析中心Lis同步 func SyncLisPljx(org_id int64) (err error) { org := &models.DataUploadConfig{ OrgId: org_id, DbHost: "localhost", DbPort: "1433", DbPass: "1Q2W3e4r!@#$", DbUser: "kyy", DbName: "hyDB", } if org_id == 10599 { org.DbName = "hy_pndb" } 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") // 第二步:获取时间内所有检验结果 pljxLisResult, _ := GetPljxLisReport(orgDb, syncTimeStr) if len(pljxLisResult) > 0 { for _, Result := range pljxLisResult { utils.InfoLog("Report:%v", Result) patient, _ := GetUserInfoByName(org_id, Result.Name) if patient.ID > 0 { project_id, _ := GetPljxProjectID(org_id, Result.DockingapplyitemName, Result.Itemname) item_id, _ := GetPljxItemID(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() //tempTime := strings.Split(Result.Time, "T") //tempTime1 := strings.Split(tempTime[1], "+") //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 } } SyncToPljx(org_id) return } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetPljxProjectID(org_id int64, project_name string, item_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 = ? and item_name = ? ", org_id, project_name, item_name).First(&inspection_reference).Error if inspection_reference.ID > 0 { return inspection_reference.ProjectId, err } else { err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? ", 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 { if org_id == 10597 { return 1059901, err } else { return 1059701, err } } } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetPljxItemID(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 = ? 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 SyncToPljx(org_id int64) { utils.TraceLog("检验检查同步任务开始执行") 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) } } //if len(inspections) > 0 { // for _, inspection := range inspections { // if inspection.SysProjectId > 0 && inspection.SysItemId > 0 { // SyncPljxInspection(&inspection) // } // } //} utils.SuccessLog("检验检查同步任务完成") } // 从机构将数据同步到中间库 func SyncPljxInspection(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 }