package service import ( "IC/models" "IC/utils" _ "encoding/json" _ "fmt" "github.com/jinzhu/gorm" "strings" "time" ) type zhraLis struct { UserCardId string `json:"user_card_id"` PatientName string `json:"patient_name"` ProjectId string `json:"project_id"` ProjectName string `json:"project_name"` InspectDate string `json:"inspect_date"` ItemId string `json:"item_id"` ItemName string `json:"item_name"` ItemType string `json:"item_type"` ItemValue string `json:"item_value"` RangeMin string `json:"range_min"` RangeMax string `json:"range_max"` RangeValue string `json:"range_value"` RangeOptions string `json:"range_options"` Unit string `json:"unit"` State string `json:"state"` CTime string `json:"c_time"` UTime string `json:"u_time"` } func (zhraLis) TableName() string { return "V_LIS_XTJK" } func GetTszhraLis(rdb *gorm.DB, synctime string, user_card_id string) (record []*zhraLis, err error) { err = rdb.Model(&zhraLis{}).Where("c_time >= ? and user_card_id = ?", synctime, user_card_id).Find(&record).Error return } // 根据机构ID和患者身份证号码获取患者信息 func GetUserInIdcardNoByOrg(org_id int64) (patients []*models.Patients, err error) { err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 ", org_id).Find(&patients).Error return } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetZhraProjectID(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 { return 1058501, err } } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetZhraItemID(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 } } } // 唐山遵化仁爱医院Lis同步 func SyncTszhraLis() (err error) { org_id := int64(10585) org := &models.DataUploadConfig{ OrgId: org_id, DbHost: "192.168.100.100", DbPort: "1433", DbPass: "xytx", DbUser: "xytx", DbName: "BLis", } 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 + 86400 } else { sync_time = 1672502400 // 2023-01-01 } // 第二步:获取时间内所有检验结果 sync_time_temp := time.Unix(sync_time, 0) startTimeStr := sync_time_temp.Format("2006-01-02 15:04:05") patients, _ := GetUserInIdcardNoByOrg(org_id) if len(patients) > 0 { for _, patient := range patients { userCardId := patient.IdCardNo // 根据姓名获取患者ID zhraLis, _ := GetTszhraLis(orgDb, startTimeStr, userCardId) if len(zhraLis) > 0 { for _, LisInfo := range zhraLis { project_id, _ := GetZhraProjectID(org_id, LisInfo.ProjectName, LisInfo.ItemName) item_id, _ := GetZhraItemID(org_id, LisInfo.ProjectName, LisInfo.ItemName, project_id) tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference timeLayout := "2006-01-02 15:04" loc, _ := time.LoadLocation("Local") tempRecord := LisInfo.CTime recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc) recordTimeInt := recordTime.Unix() recordTimeStr := time.Unix(recordTimeInt, 0).Format(timeLayout) var total int var RangeOptions string var RangeMin string var RangeMax string var ItemType int Range := strings.Split(LisInfo.RangeValue, "--") if len(Range) > 1 { RangeMin = Range[0] RangeMax = Range[1] ItemType = 1 } else { ItemType = 2 RangeOptions = LisInfo.RangeValue } 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.ProjectName inspection_reference.Project = LisInfo.ProjectName inspection_reference.ProjectId = project_id inspection_reference.ItemName = LisInfo.ItemName // inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10) inspection_reference.ItemId = item_id inspection_reference.RangeType = ItemType inspection_reference.RangeMin = RangeMin inspection_reference.RangeMax = RangeMax // inspection_reference.RangeValue = RangeValue inspection_reference.RangeOptions = RangeOptions inspection_reference.Unit = LisInfo.Unit inspection_reference.Status = 1 inspection_reference.CreatedTime = time.Now().Unix() inspection_reference.UpdatedTime = time.Now().Unix() inspection_reference.InspectDate = recordTimeStr inspection_reference.UTime = recordTimeStr 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.ItemValue inspection.InspectDate = recordTimeStr inspection.RecordDate = recordTimeInt // inspection.InspectTips = report.Resultstate 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) 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 } } SyncTotszhrayy() return } func SyncTotszhrayy() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10585) // 第一步:跟进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("检验检查同步任务完成") }