package service import ( "IC/models" "IC/utils" _ "IC/utils" _ "bytes" "encoding/json" _ "encoding/json" _ "encoding/xml" _ "fmt" "github.com/astaxie/beego/httplib" _ "github.com/jinzhu/gorm" "strconv" "strings" "time" ) type tscylis struct { HisUserId string `json:"his_user_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"` } type tscyLisResult struct { ErrorCode int64 `json:"error_code"` Result []tscylis `json:"data"` } func GetTscyLis(sync_time string, his_user_id string) (str tscyLisResult, checkStr string) { apiurl := "http://60.2.56.98:8797/tscy/lis_report.do" rep := httplib.Get(apiurl) rep.Param("sync_time", sync_time) rep.Param("his_user_id", his_user_id) strs, err := rep.String() if err != nil { utils.InfoLog("err:%v", err) } var checkJSON tscyLisResult json.Unmarshal([]byte(strs), &checkJSON) // utils.InfoLog("checkJSON:%v", checkJSON) return checkJSON, strs } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetTscyProjectID(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 92001, err } } } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetTscyVipProjectID(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 1056201, err } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetTscyItemID(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 } } } // 唐山朝阳医院透析中心Lis同步 func SyncTscyLis() (err error) { org_id := int64(9882) // 第一步:获取上一次同步的时间点 syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 1651334400 } // 第二步:获取所有患者的病历号 patientList, _ := GetPatientGzjhByOrgId(org_id) if len(patientList) > 0 { for _, patient := range patientList { if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 { // 上次同步时间转换为接口需要的格式 patient_id := patient.ID // sync_time_temp := time.Unix(sync_time,0) // syncTimeStr := sync_time_temp.Format("2006010215:04:05") utils.InfoLog("IdCardNo:%v", patient.IdCardNo) // 获取会员号 // utils.InfoLog("VipStr:%v", VipStr) gzjhLis, _ := GetTscyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo) // if len(gzjhLis.Result) == 0 { // gzjhLis,_ = GethbdyLis(strconv.FormatInt(sync_time,10),VipStr) // } // utils.InfoLog("yzstr:%v", gzjhLis) if len(gzjhLis.Result) > 0 { // timeLayout := "2006010215:04:05" // 转化所需模板 // loc, _ := time.LoadLocation("Local") // 重要:获取时区 for _, info := range gzjhLis.Result { utils.InfoLog("Result:%v", info) // 根据project_id和item_id 判断该项是否存在 project_id := int64(0) // if info.ProjectId > 0 { project_id, _ = GetTscyProjectID(org_id, info.ProjectName) // } else { // continue // } item_id, _ := GetTscyItemID(org_id, info.ProjectName, info.ItemName, project_id) tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference record_date, _ := strconv.ParseInt(info.InspectDate, 10, 64) if record_date == 0 { record_date = time.Now().Unix() } // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr) // record_date := record_date_str.Unix() inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04") // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr) // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02")) var total int var RangeOptions string var RangeMin string var RangeMax string // // 判断检查类型 ItemType := info.ItemType // Range := strings.Split(info.RangeValue, "-") // if len(Range) > 1 { RangeMin = info.RangeMin RangeMax = info.RangeMax // 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 inspection_reference.ID > 0 { // ItemType = int64(inspection_reference.RangeType) // } 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 = info.ItemId inspection_reference.ItemId = item_id inspection_reference.RangeType, _ = strconv.Atoi(ItemType) 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(ItemType) 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 } SyncToTscytx() return } func SyncToTscytx() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(9882) // 第一步:跟进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("检验检查同步任务完成") } // 唐山朝阳医院Vip区透析中心Lis同步 func SyncTscyVipLis() (err error) { org_id := int64(10562) // 第一步:获取上一次同步的时间点 syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { sync_time = 1651334400 } // 第二步:获取所有患者的病历号 patientList, _ := GetPatientGzjhByOrgId(org_id) if len(patientList) > 0 { for _, patient := range patientList { if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 { // 上次同步时间转换为接口需要的格式 patient_id := patient.ID utils.InfoLog("IdCardNo:%v", patient.IdCardNo) gzjhLis, _ := GetTscyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo) if len(gzjhLis.Result) > 0 { for _, info := range gzjhLis.Result { // 根据project_id和item_id 判断该项是否存在 project_id := int64(0) project_id, _ = GetTscyVipProjectID(org_id, info.ProjectName) item_id, _ := GetTscyItemID(org_id, info.ProjectName, info.ItemName, project_id) tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference record_date, _ := strconv.ParseInt(info.InspectDate, 10, 64) if record_date == 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.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 = info.ItemId inspection_reference.ItemId = item_id inspection_reference.RangeType, _ = strconv.Atoi(ItemType) 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(ItemType) 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 } SyncToTscyVip() return } func SyncToTscyVip() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10562) // 第一步:跟进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("检验检查同步任务完成") }