package service import ( "IC/models" "IC/utils" oracle "github.com/dzwvip/gorm-oracle" "gorm.io/gorm" "strings" "time" ) type wzLisReport struct { Repno string `gorm:"column:REPNO" json:"REPNO"` Repname string `gorm:"column:REPNAME" json:"REPNAME"` NAME string `gorm:"column:NAME" json:"NAME"` Appdeptname string `gorm:"column:APPDEPTNAME" json:"APPDEPTNAME"` Firstaudittime string `gorm:"column:FIRSTAUDITTIME" json:"FIRSTAUDITTIME"` } func (wzLisReport) TableName() string { return "V_LIS_REPORT" } type wzLisReportResult struct { Repno string `gorm:"column:REPNO" json:"REPNO"` Itemid string `gorm:"column:ITEMID" json:"ITEMID"` Itemcname string `gorm:"column:ITEMCNAME" json:"ITEMCNAME"` Itemename string `gorm:"column:ITEMENAME" json:"ITEMENAME"` Result string `gorm:"column:RESULT" json:"RESULT"` Unit string `gorm:"column:UNIT" json:"UNIT"` Prompt string `gorm:"column:PROMPT" json:"PROMPT"` Refmax string `gorm:"column:REFMAX" json:"REFMAX"` Refmin string `gorm:"column:REFMIN" json:"REFMIN"` Time string `gorm:"column:TIME" json:"TIME"` } func (wzLisReportResult) TableName() string { return "V_LIS_REPORT_RESULT" } func GetSzwzyLisReport(rdb *gorm.DB, synctime string) (record []*wzLisReport, err error) { err = rdb.Model(&wzLisReport{}).Where("FIRSTAUDITTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') and APPDEPTNAME = '肾病科'", synctime).Find(&record).Error return } func GetSzwzyLisReportResule(rdb *gorm.DB, repno string) (record []*wzLisReportResult, err error) { err = rdb.Model(&wzLisReportResult{}).Where("REPNO = ? ", repno).Find(&record).Error return } // 深圳五洲中医院Lis同步 func SyncLisSzwz() (err error) { org_id := int64(10580) orgDb, err := gorm.Open(oracle.Open("oracle://BSLIS52:zhsoft@172.9.200.3:1521/JYK"), &gorm.Config{}) if err != nil { utils.InfoLog("syncTimeStr:%v", err) } // 第一步:获取上一次同步的时间点 syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 1672502400 // 2023-01-01 } SyncTime := time.Unix(sync_time, 0) syncTimeStr := SyncTime.Format("2006-01-02 15:04:05") utils.InfoLog("syncTimeStr:%v", syncTimeStr) // 第二步:获取时间内所有检验结果 szwzLisReport, _ := GetSzwzyLisReport(orgDb, syncTimeStr) if len(szwzLisReport) > 0 { for _, Report := range szwzLisReport { utils.InfoLog("Report:%v", Report) if len(Report.Repname) <= 0 { Report.Repname = "未知" } patient, _ := GetUserInfoByName(org_id, Report.NAME) if patient.ID > 0 { // 第三步,根据REPNO查询对应的检查小项 szwzLisReportResult, _ := GetSzwzyLisReportResule(orgDb, Report.Repno) if len(szwzLisReportResult) > 0 { for _, Result := range szwzLisReportResult { project_id, _ := GetSzwzProjectID(org_id, Report.Repname) item_id, _ := GetSzwzItemID(org_id, Report.Repname, Result.Itemcname, project_id) tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference 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 RangeMin = Result.Refmin RangeMax = Result.Refmax ItemType = 1 RangeOptions = Result.Prompt 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 = Report.Repname inspection_reference.Project = Report.Repname inspection_reference.ProjectId = project_id inspection_reference.ItemName = Result.Itemcname inspection_reference.ItemNameAddition = Result.Itemename 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 } SyncToSzwz() return } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetSzwzProjectID(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 1058001, err } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetSzwzItemID(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 SyncToSzwz() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10580) inspections, _ := GetSyncInspectionByOrgId(org_id) if len(inspections) > 0 { for _, inspection := range inspections { if inspection.SysProjectId > 0 && inspection.SysItemId > 0 { SyncSzwzInspection(&inspection) } } } utils.SuccessLog("检验检查同步任务完成") } // 从机构将数据同步到中间库 func SyncSzwzInspection(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 }