package service // 三优 检验检查对接 import ( _ "encoding/json" _ "encoding/xml" "fmt" _ "strconv" "strings" "time" "IC/models" "IC/utils" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm" ) type stsylis struct { Testno string `gorm:"column:testno " json:"testno "` Seqno string `gorm:"column:seqno " json:"seqno "` Itemno string `gorm:"column:itemno " json:"itemno "` Itemname string `gorm:"column:itemname" json:"itemname"` Testresult string `gorm:"column:testresult" json:"testresult"` Result1 string `gorm:"column:result1" json:"result1"` Result2 string `gorm:"column:result2" json:"result2"` Resultflag string `gorm:"column:resultflag" json:"resultflag"` Units string `gorm:"column:units" json:"units"` Ranges string `gorm:"column:ranges" json:"ranges"` Resulttime string `gorm:"column:resulttime" json:"resulttime"` Checkman string `gorm:"column:checkman" json:"checkman"` Testman string `gorm:"column:testman" json:"testman"` Lastmodify string `gorm:"column:lastmodify" json:"lastmodify"` Instrument string `gorm:"column:instrument" json:"instrument"` Sampleno string `gorm:"column:sampleno" json:"sampleno"` Brdh string `gorm:"column:brdh" json:"brdh"` Brxm string `gorm:"column:brxm" json:"brxm"` Sfxmdh string `gorm:"column:sfxmdh" json:"sfxmdh"` Sendflag string `gorm:"column:sendflag" json:"sendflag"` Myid string `gorm:"column:myid" json:"myid"` Uploadflag string `gorm:"column:uploadflag" json:"uploadflag"` } func (stsylis) TableName() string { return "lis_reqresult" } func GetStsyLis(rdb *gorm.DB, synctime time.Time) (record []*stsylis, err error) { err = rdb.Model(&stsylis{}).Where("lastmodify >= ? ", synctime).Find(&record).Error return } // 根据机构ID和患者姓名获取患者信息 func GetUserInfoByName(org_id int64, name string) (patients models.Patients, err error) { err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 and name = ?", org_id, name).First(&patients).Error return } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetStsyProjectID(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.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 1022301, err } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetStsyItemID(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 SyncShuHeLis() (err error) { org_id := int64(10644) org := &models.DataUploadConfig{ OrgId: org_id, DbHost: "192.168.2.4", DbPort: "1433", DbPass: "a123456..", DbUser: "sa", DbName: "BI", } orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName) if err != nil { utils.ErrorLog("创建数据库连接失败:%v", err) return } fmt.Println(orgDb) infoList, _ := GetHisLabelPrintStatusInfo(org_id) if len(infoList) > 0 { for _, item := range infoList { p_ids_arr := strings.Split(item.Ids, ",") list, _ := GetHisLabelPrintInfo(org_id, p_ids_arr) fmt.Println("list==============", list) patients, _ := GetPatientByIDOne(org_id, item.PatientId) fmt.Println("patinets", patients.Name) fmt.Println("patinets", patients.IdCardNo) if len(list) > 0 { for _, it := range list { printCode := models.DataPrintCode{ HisUserId: patients.IdCardNo, Name: patients.Name, RecordDate: it.RecordDate, ProjectName: it.ProjectName, Ctime: time.Now().Unix(), Mtime: time.Now().Unix(), Status: 1, PrintId: item.ID, PatientId: it.PatientId, } err = CratePrintCode(orgDb, printCode) fmt.Println("errr-000000000000000", err) } } } } return } // 汕头三优血液透析中心 func SyncStsyLis() (err error) { org_id := int64(10223) org := &models.DataUploadConfig{ OrgId: org_id, DbHost: "192.168.1.209", DbPort: "1433", DbPass: "Aa123456", DbUser: "kyys", DbName: "lis2022", } 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) // 第一步:获取上一次同步的时间点 syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 1685548800 } SyncTime := time.Unix(sync_time, 0) fmt.Println(SyncTime) // 第二步:获取时间内所有检验结果 stsyLis, _ := GetStsyLis(orgDb, SyncTime) if len(stsyLis) > 0 { var syncTimeEnd int64 syncTimeEnd = time.Now().Unix() for _, LisInfo := range stsyLis { brxm := LisInfo.Brxm // 根据姓名获取患者ID patient, _ := GetUserInfoByName(org_id, brxm) if patient.ID > 0 { project_id, _ := GetStsyProjectID(org_id, LisInfo.Instrument, LisInfo.Itemname) item_id, _ := GetStsyItemID(org_id, LisInfo.Instrument, 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") tempTime := strings.Split(LisInfo.Lastmodify, "T") tempTime1 := strings.Split(tempTime[1], ".") tempRecord := tempTime[0] + " " + tempTime1[0] 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.Ranges, "--") if len(Range) > 1 { RangeMin = Range[0] RangeMax = Range[1] ItemType = 1 } else { ItemType = 2 RangeOptions = LisInfo.Ranges } 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.Instrument inspection_reference.Project = LisInfo.Instrument 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.Units 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, recordTimeInt, 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.Testresult 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 = syncTimeEnd 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 } } // 第四步:关闭数据库连接 CloseDB(orgDb) SyncToStsytx() return } // func deleteMid(tx *gorm.DB) (err error) { // err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," + // "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," + // "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + // "WHERE t1.user_org_id = t2.orgid)tmp " + // "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + // "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error // return // } func SyncToStsytx() { org_id := int64(10223) utils.TraceLog("检验检查同步任务开始执行") // 第一步:跟进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 CratePrintCode(rdb *gorm.DB, code models.DataPrintCode) error { err := rdb.Create(&code).Error return err } func GetHisLabelPrintStatusInfo(user_org_id int64) (info []*models.XtHisLabelPrintStatusInfo, err error) { err = XTReadDB().Where("user_org_id = ? and status=1 and record_date ='2024-09-04'", user_org_id).Find(&info).Error return info, err } func GetHisLabelPrintInfo(user_org_id int64, p_ids_arr []string) (list []*models.HisLabelPrintInfo, err error) { err = XTReadDB().Where("user_org_id = ? and status=1 and id in(?)", user_org_id, p_ids_arr).Find(&list).Error return list, err } func GetPatientByIDOne(orgID int64, patientID int64) (models.Patients, error) { var patient models.Patients err := readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", patientID, orgID).First(&patient).Error return patient, err }