package service import ( "IC/models" "IC/utils" "github.com/jinzhu/gorm" "strconv" "strings" "time" ) // 四川城东康复医院Lis同步 func SyncSccdkfyyLis() (err error) { org_id := int64(10551) org := &models.DataUploadConfig{ OrgId: org_id, DbHost: "192.168.1.5", DbPort: "1433", DbPass: "XTXT_User", DbUser: "XTXT_User", DbName: "ndlmis15", } 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) // 第一步:获取上一次同步的plug_plugcase的ID syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 0 } sync_time_temp := time.Unix(sync_time, 0) syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05") // 第二步:获取时间内所有检验结果 sccdLis, _ := GetScdckfyyLis(orgDb, syncTimeStr) if len(sccdLis) > 0 { for _, item := range sccdLis { printInfo, _ := GetPatientsInfoBySfzh(item.Sfzh, 10551) if printInfo.ID == 0 { continue } // result 里查询 antiRresult,_ := GetScCdkfyyAnti(orgDb,item.TestOrderNo) if len(antiRresult) > 0 { for _,anti := range antiRresult { LgthScdckfyySync(anti,item, org_id, printInfo.ID) } } } } // 第二步:获取所有患者的病历号 // patientList, _ := GetPatientGzjhByOrgId(org_id) // if len(patientList) > 0 { // for _, patient := range patientList { // if len(patient.IdCardNo) > 0 { // patient_id := patient.ID // utils.InfoLog("IdCardNo:%v", patient.IdCardNo) // scdcCaseList, _ := GetScCdkfyyCase(orgDb, sync_time,lastId, patient.IdCardNo) // // 如果存在纪录,再在plug_plugCheckitem里根据信息查询检验信息 // if len(scdcCaseList) > 0 { // for _, info := range scdcCaseList { // utils.InfoLog("Result:%v", info) // scdcItemList,_ := GetScCdkfyyCheckItem(orgDb,info.Serialno) // //根据item里的数据,查询检验结果 // if len(scdcItemList) > 0 { // for _,item := range scdcItemList { // resultList,_ := GetScCdkfyyResult(orgDb,item.Serialno,item.Itemcode) // if len(resultList) > 0 { // for _ ,resultinfo := range resultList{ // LgthScdckfyySync(resultinfo,item, org_id, patient_id) // } // } // } // } // } // } // } // } // } 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 } SyncToSccdkfyy() return } func SyncToSccdkfyy() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10551) // 第一步:跟进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("检验检查同步任务完成") } func GetScCdkfyyAnti(rdb *gorm.DB, test_order_no string) (record []*models.VYunjingPatientLabAnti, err error) { err = rdb.Model(&models.VYunjingPatientLabAnti{}).Where("TEST_ORDER_NO = ?", test_order_no).Find(&record).Error return } func GetPatientsInfoBySfzh(sfzh string, org_id int64) (info models.Patients, err error) { err = readDb.Model(&models.Patients{}).Where("id_card_no = ? AND user_org_id = ?", sfzh, org_id).First(&info).Error return } func GetScdckfyyLis(rdb *gorm.DB, synctime string) (record []*models.VYunjingPatientLabItems, err error) { err = rdb.Model(&models.VYunjingPatientLabItems{}).Where("CREATE_AT >= ? ", synctime).Find(&record).Error return } func SyncToScdckfyytx() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10551) // 第一步:跟进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("检验检查同步任务完成") } // 从机构将数据同步到中间库 func LgthScdckfyySync(result *models.VYunjingPatientLabAnti, checkItem *models.VYunjingPatientLabItems, org_id int64, patient_id int64) (*models.MiddleInspectionReference, error) { tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference timstr := strings.Split(checkItem.CreateAt, "T") timstem := strings.Split(timstr[1], "Z") recordDateStr := timstr[0] + " " + timstem[0] utils.InfoLog("recordDateStr: %v", recordDateStr) if len(recordDateStr) == 0 { recordDateStr = time.Now().Format("2006-01-02 15:04") } record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr) utils.InfoLog("record_date_str: %v", record_date_str) record_date := record_date_str.Unix() utils.InfoLog("record_date: %v", record_date) inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04") utils.InfoLog("inspect_date: %v", inspect_date) var total int var RangeOptions string var RangeMin string var RangeMax string // // 判断检查类型 ItemType := 1 Range := strings.Split(result.ReferRange, "-") if len(Range) > 1 { RangeMin = Range[0] RangeMax = Range[1] ItemType = 1 } else { ItemType = 2 RangeOptions = result.ReferRange } err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_name = ? and item_name = ? and status = 1", org_id,checkItem.ItemTypeName,result.AntiName).Find(&inspection_reference).Count(&total).Error if inspection_reference.ID > 0 { ItemType = inspection_reference.RangeType } if total <= 0 { // 如果配置表里没有相关配置,则创建project_id,item_id和相关配置信息 project_id, _ := GetSccdkfyyProjectID(org_id,checkItem.ItemTypeName) item_id,_ := GetSccdkfyyItemID(org_id, checkItem.ItemTypeName, result.AntiName, project_id) inspection_reference.OrgId = org_id inspection_reference.ProjectName = checkItem.ItemTypeName inspection_reference.Project = checkItem.ItemType inspection_reference.ProjectId = project_id inspection_reference.ItemName = result.AntiName inspection_reference.ItemNameAddition = result.AntiName inspection_reference.ItemId = item_id inspection_reference.ItemCode = result.AntiName inspection_reference.RangeType = ItemType inspection_reference.RangeMin = RangeMin inspection_reference.RangeMax = RangeMax // inspection_reference.RangeValue = RangeValue inspection_reference.RangeOptions = RangeOptions inspection_reference.Unit = result.Unit inspection_reference.Status = 1 inspection_reference.CreatedTime = time.Now().Unix() inspection_reference.UpdatedTime = time.Now().Unix() inspection_reference.InspectDate = inspect_date inspection_reference.UTime = inspect_date 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, inspection_reference.ProjectId, inspection_reference.ItemId, record_date, patient_id).Find(&inspection).Count(&itotal).Error if itotal <= 0 { result.TestResult = strings.Replace(result.TestResult, ">", ">", -1) result.TestResult = strings.Replace(result.TestResult, "<", "<", -1) inspection.PatientId = patient_id inspection.OrgId = org_id inspection.ProjectId = inspection_reference.ProjectId inspection.ItemName = inspection_reference.ItemName inspection.ProjectName = inspection_reference.ProjectName inspection.InspectType = ItemType inspection.ItemId = inspection_reference.ItemId inspection.InspectValue = result.TestResult inspection.InspectDate = inspect_date inspection.RecordDate = record_date inspection.InspectTips = result.AntiCode inspection.Status = 1 inspection.CreatedTime = time.Now().Unix() inspection.UpdatedTime = time.Now().Unix() inspection.UTime = inspect_date inspection.HisUserId = strconv.FormatInt(patient_id, 10) err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error if err != nil { tx.Rollback() } } tx.Commit() return nil, err } func GetProjectInfoSccdkfyy(item_id int64, org_id int64) (project_info models.MiddleInspectionReference, err error) { err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_id = ?", org_id, item_id).First(&project_info).Error return } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetSccdkfyyItemID(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 utils.InfoLog("inspection_reference: %v", inspection_reference) if inspection_reference.ItemId > 0 { return inspection_reference.ItemId + 1, err } else { return project_id*100 + 1, err } } } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetSccdkfyyProjectID(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 1055101, err } } } func GetScCdkfyyResult(rdb *gorm.DB, serialNo string,parItemNo string) (record []*models.PlugPlugresult, err error) { err = rdb.Model(&models.PlugPlugresult{}).Where("serialNo = ? and parItemNo = ?", serialNo,parItemNo).Find(&record).Error return } func GetScCdkfyyCheckItem(rdb *gorm.DB, serialNo string) (record []*models.PlugPlugcheckitem, err error) { err = rdb.Model(&models.PlugPlugcheckitem{}).Where("serialNo = ? ", serialNo).Find(&record).Error return } func GetScCdkfyyCase(rdb *gorm.DB, startId int64,endId int64, hisuserid string) (record []*models.PlugPlugcase, err error) { err = rdb.Model(&models.PlugPlugcase{}).Where("ID >= ? and ID <= ? and mainIndex = ?", startId,endId, hisuserid).Find(&record).Error return } func GetScCdkfyyCaseMaxID(rdb *gorm.DB) (record models.PlugPlugcase, err error) { err = rdb.Model(&models.PlugPlugcase{}).Select("TOP 1 *").Order("ID desc").Find(&record).Error return }