package service import ( "IC/models" "IC/utils" _ "encoding/json" _ "encoding/xml" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm" "strconv" "strings" "time" ) type scpalis struct { HisUserId string `gorm:"column:his_user_id" json:"his_user_id"` PatientName string `gorm:"column:patient_name" json:"patient_name"` ProjectId int64 `gorm:"column:project_id" json:"project_id"` ProjectName string `gorm:"column:project_name" json:"project_name"` InspectDate int64 `gorm:"column:inspect_date" json:"inspect_date"` ItemId int64 `gorm:"column:item_id" json:"item_id"` ItemName string `gorm:"column:item_name" json:"item_name"` ItemType int64 `gorm:"column:item_type" json:"item_type"` ItemValue string `gorm:"column:item_value" json:"item_value"` RangeMin string `gorm:"column:range_min" json:"range_min"` RangeMax string `gorm:"column:range_max" json:"range_max"` RangeValue string `gorm:"column:range_value" json:"range_value"` RangeOptions string `gorm:"column:range_options" json:"range_options"` Unit string `gorm:"column:unit" json:"unit"` State int64 `gorm:"column:state" json:"state"` CTime int64 `gorm:"column:c_time" json:"c_time"` UTime int64 `gorm:"column:u_time" json:"u_time"` } func (scpalis) TableName() string { return "检验检查信息" } func GetScpaLis(rdb *gorm.DB, synctime int64, hisuserid string) (record []*scpalis, err error) { err = rdb.Model(&scpalis{}).Where("inspect_date >= ? and his_user_id = ?", synctime, hisuserid).Find(&record).Error return } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetScpaProjectID(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 = ?", 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 9829001, err } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetScpaItemID(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 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 } } } // 四川蓬安济民透析中心Lis同步 func SyncScpaLis() (err error) { org_id := int64(9829) org := &models.DataUploadConfig{ OrgId: org_id, DbHost: "192.168.0.254", DbPort: "1433", DbPass: "999999", DbUser: "sa", DbName: "nfjiminhis", } 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 = 1627747200 } // 第二步:获取所有患者的病历号 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) scpaLis, _ := GetScpaLis(orgDb, sync_time, patient.IdCardNo) if len(scpaLis) > 0 { for _, info := range scpaLis { utils.InfoLog("Result:%v", info) // 根据project_id和item_id 判断该项是否存在 project_id := int64(0) if info.ProjectId > 0 { project_id, _ = GetScpaProjectID(org_id, info.ProjectName) } else { continue } item_id, _ := GetScpaItemID(org_id, info.ProjectName, info.ItemName, project_id) tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference record_date := info.InspectDate if info.InspectDate == 0 { record_date = time.Now().Unix() } inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04") var total int var RangeOptions string var RangeMin string var RangeMax string // // 判断检查类型 ItemType := info.ItemType RangeMin = info.RangeMin RangeMax = info.RangeMax RangeOptions = info.RangeValue // Range := strings.Split(info.RangeValue, "-") // if len(Range) > 1 { // RangeMin = Range[0] // RangeMax = Range[1] // ItemType = 1 // } else { // ItemType = 2 // RangeOptions = info.RangeOptions // } 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 = info.ProjectName inspection_reference.Project = info.ProjectName inspection_reference.ProjectId = project_id inspection_reference.ItemName = info.ItemName inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10) inspection_reference.ItemId = item_id inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10)) inspection_reference.RangeMin = RangeMin inspection_reference.RangeMax = RangeMax // inspection_reference.RangeValue = RangeValue inspection_reference.RangeOptions = RangeOptions inspection_reference.Unit = info.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, project_id, item_id, record_date, patient_id).Find(&inspection).Count(&itotal).Error if itotal <= 0 { info.ItemValue = strings.Replace(info.ItemValue, ">", ">", -1) info.ItemValue = strings.Replace(info.ItemValue, "<", "<", -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, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10)) inspection.ItemId = item_id inspection.InspectValue = info.ItemValue inspection.InspectDate = inspect_date inspection.RecordDate = record_date // inspection.InspectTips = report.Resultstate 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() } } } 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 } SyncToScpatx() return } func SyncToScpatx() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(9829) // 第一步:跟进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("检验检查同步任务完成") }