package service import ( "IC/models" "IC/utils" "fmt" "github.com/jinzhu/gorm" "regexp" "strconv" "strings" "time" "unicode" ) type KMResult struct { FRequestCode string `gorm:"column:F_RequestCode" json:"F_RequestCode" form:"F_RequestCode"` FHospSampleID string `gorm:"column:F_HospSampleID" json:"F_HospSampleID" form:"F_HospSampleID"` FHospSampleNumber string `gorm:"column:F_HospSampleNumber" json:"F_HospSampleNumber" form:"F_HospSampleNumber"` FInputDate string `gorm:"column:F_InputDate" json:"F_InputDate" form:"F_InputDate"` FSamplingDate string `gorm:"column:F_SamplingDate" json:"F_SamplingDate" form:"F_SamplingDate"` FName string `gorm:"column:F_Name" json:"F_Name" form:"F_Name"` FSex string `gorm:"column:F_Sex" json:"F_Sex" form:"F_Sex"` FAge string `gorm:"column:F_Age" json:"F_Age" form:"F_Age"` FAgeUnit string `gorm:"column:F_AgeUnit" json:"F_AgeUnit" form:"F_AgeUnit"` FPatientNumber string `gorm:"column:F_PatientNumber" json:"F_PatientNumber" form:"F_PatientNumber"` FItemType string `gorm:"column:F_ItemType" json:"F_ItemType" form:"F_ItemType"` FCompoundItem string `gorm:"column:F_CompoundItem" json:"F_CompoundItem" form:"F_CompoundItem"` FCompoundItemName string `gorm:"column:F_CompoundItemName" json:"F_CompoundItemName" form:"F_CompoundItemName"` FNaturalItem string `gorm:"column:F_NaturalItem" json:"F_NaturalItem" form:"F_NaturalItem"` FNaturalItemName string `gorm:"column:F_NaturalItemName" json:"F_NaturalItemName" form:"F_NaturalItemName"` FSingleItem string `gorm:"column:F_SingleItem" json:"F_SingleItem" form:"F_SingleItem"` FSingleItemName string `gorm:"column:F_SingleItemName" json:"F_SingleItemName" form:"F_SingleItemName"` FHospNaturalItem string `gorm:"column:F_HospNaturalItem" json:"F_HospNaturalItem" form:"F_HospNaturalItem"` FHospNaturalItemName string `gorm:"column:F_HospNaturalItemName" json:"F_HospNaturalItemName" form:"F_HospNaturalItemName"` FHospItem string `gorm:"column:F_HospItem" json:"F_HospItem" form:"F_HospItem"` FHospItemName string `gorm:"column:F_HospItemName" json:"F_HospItemName" form:"F_HospItemName"` FTestNO string `gorm:"column:F_TestNO" json:"F_TestNO" form:"F_TestNO"` FResult string `gorm:"column:F_Result" json:"F_Result" form:"F_Result"` FUnit string `gorm:"column:F_Unit" json:"F_Unit" form:"F_Unit"` FHint string `gorm:"column:F_Hint" json:"F_Hint" form:"F_Hint"` FSuggestion string `gorm:"column:F_Suggestion" json:"F_Suggestion" form:"F_Suggestion"` FReference string `gorm:"column:F_Reference" json:"F_Reference" form:"F_Reference"` FReference2 string `gorm:"column:F_Reference2" json:"F_Reference2" form:"F_Reference2"` FDepartmentName string `gorm:"column:F_DepartmentName" json:"F_DepartmentName" form:"F_DepartmentName"` FRecorderName string `gorm:"column: F_RecorderName" json:"F_RecorderName" form:"F_RecorderName"` FRecordTime string `gorm:"column:F_RecordTime" json:"F_RecordTime" form:"F_RecordTime"` FCheckerName string `gorm:"column:F_CheckerName" json:"F_CheckerName" form:"F_CheckerName"` FCheckTime string `gorm:"column:F_CheckTime" json:"F_CheckTime" form:"F_CheckTime"` FAuthorizeName string `gorm:"column:F_AuthorizeName" json:"F_AuthorizeName" form:"F_AuthorizeName"` FAuthorizeTime string `gorm:"column:F_AuthorizeTime" json:"F_AuthorizeTime" form:"F_AuthorizeTime"` FSubResults string `gorm:"column:F_SubResults" json:"F_SubResults" form:"F_SubResults"` FPathologyGrossingDesc string `gorm:"column:F_PathologyGrossingDesc" json:"F_PathologyGrossingDesc" form:"F_PathologyGrossingDesc"` FReportBarCode string `gorm:"column:F_ReportBarCode" json:"F_ReportBarCode" form:"F_ReportBarCode"` FReceiveTime string `gorm:"column:F_ReceiveTime" json:"F_ReceiveTime" form:"F_ReceiveTime"` FStatus string `gorm:"column:F_Status" json:"F_Status" form:"F_Status"` FRemark string `gorm:"column:F_Remark" json:"F_Remark" form:"F_Remark"` FTestMethodName string `gorm:"column:F_TestMethodName" json:"F_TestMethodName" form:"F_TestMethodName"` FMachineName string `gorm:"column:F_MachineName" json:"F_MachineName" form:"F_MachineName"` FItemDesc string `gorm:"column:F_ItemDesc" json:"F_ItemDesc" form:"F_ItemDesc"` FIsDelayed string `gorm:"column:F_IsDelayed" json:"F_IsDelayed" form:"F_IsDelayed"` FDelayeddesc string `gorm:"column:F_Delayeddesc" json:"F_Delayeddesc" form:"F_Delayeddesc"` FIsReimbu string `gorm:"column:F_IsReimbu" json:"F_IsReimbu" form:"F_IsReimbu"` FReimbuDesc string `gorm:"column:F_ReimbuDesc" json:"F_ReimbuDesc" form:"F_ReimbuDesc"` FDetailItemID string `gorm:"column:F_DetailItemID" json:"F_DetailItemID" form:"F_DetailItemID"` FIDCardNo string `gorm:"column:F_IDCardNo" json:"F_IDCardNo" form:"F_IDCardNo"` } func (KMResult) TableName() string { return "v_km_lis_result" } type KMSheet struct { FName string `gorm:"column:F_Name" json:"F_Name" form:"F_Name"` FCompoundItemName string `gorm:"column:F_CompoundItemName" json:"F_CompoundItemName" form:"F_CompoundItemName"` FReference string `gorm:"column:F_Reference" json:"F_Reference" form:"F_Reference"` FSingleItemName string `gorm:"column:F_SingleItemName" json:"F_SingleItemName" form:"F_SingleItemName"` FRecordTime string `gorm:"column:F_RecordTime" json:"F_RecordTime" form:"F_RecordTime"` FCheckTime string `gorm:"column:F_CheckTime" json:"F_CheckTime" form:"F_CheckTime"` FResult string `gorm:"column:F_Result" json:"F_Result" form:"F_Result"` FUnit string `gorm:"column:F_Unit" json:"F_Unit" form:"F_Unit"` } func (KMSheet) TableName() string { return "KM_LIS_Result" } func GetPatientsInfoByName(name string, org_id int64) (info models.Patients, err error) { err = readDb.Model(&models.Patients{}).Where("name = ? AND user_org_id = ?", name, org_id).First(&info).Error return } // 获取lis返回检验检查结果数据 func GetSHResultDataInsertDB(org_id int64) (result []*KMResult) { org := &models.DataUploadConfig{ OrgId: 10215, ProvinceId: 19, CityId: 291, DepartmentName: "肇庆三鹤血液透析中心", HospitalId: "MA5DB4KB6", InstType: 1, DbHost: "localhost", DbPort: "1433", DbPass: "KingMed", DbUser: "KingMed", DbName: "KMDB", } if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 { orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName) if err != nil { utils.ErrorLog("创建数据库连接失败:%v", err) return } record, _ := GetLastSyncResultRecord(org_id) fmt.Println(record) var sync_time int64 if record.ID > 0 { sync_time = record.Ctime } else { sync_time = 1627747200 } sync_time_temp := time.Unix(sync_time, 0) syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05") list, err := GetSHResultRecord(orgDb, syncTimeStr) if err == nil { // fmt.Println(list) //插入到系统检验检查数据 for _, item := range list { printInfo, _ := GetPatientsInfoByName(item.FName, 10215) if printInfo.ID == 0 { fmt.Println(item) fmt.Println(printInfo) fmt.Println("暂无该患者") continue } project_id := int64(0) project_id, _ = GetShjhProjectID(org_id, item.FCompoundItemName) item_id, _ := GetShItemID(org_id, item.FCompoundItemName, item.FSingleItemName, project_id) tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference recordDateStr := "" //inspect_date := item.FRecordTime.Format("2006-01-02 15:04") timstr := strings.Split(item.FRecordTime, "T") timstem := strings.Split(timstr[1], "Z") recordDateStr = timstr[0] + " " + timstem[0] // recordDateStr = item.FRecordTime date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr) record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02")) // ftimstr := strings.Split(item.FCheckTime, "T") // ftimstem := strings.Split(timstr[1], "Z") // fdate := ftimstr[0] + " " + ftimstem[0] fdate := item.FCheckTime var total int var RangeOptions string var RangeMin string var RangeMax string // 判断检查类型 var ItemType int if IsChineseChar(item.FReference) || strings.Contains(item.FReference, "|") || strings.Contains(item.FReference, "≤") { ItemType = 2 } else { if strings.Contains(item.FReference, "-") { ItemType = 1 } else { ItemType = 2 } } if ItemType == 1 { Range := strings.Split(item.FReference, "-") RangeMin = Range[0] RangeMax = Range[1] } else { RangeOptions = item.FReference } 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 = inspection_reference.RangeType } if total <= 0 { inspection_reference.OrgId = org_id inspection_reference.ProjectName = item.FCompoundItemName inspection_reference.Project = item.FCompoundItemName inspection_reference.ProjectId = project_id inspection_reference.ItemName = item.FSingleItemName inspection_reference.ItemNameAddition = "" inspection_reference.ItemId = item_id inspection_reference.RangeType = ItemType inspection_reference.RangeMin = RangeMin inspection_reference.RangeMax = RangeMax inspection_reference.RangeOptions = RangeOptions inspection_reference.Unit = item.FUnit inspection_reference.Status = 1 inspection_reference.CreatedTime = time.Now().Unix() inspection_reference.UpdatedTime = time.Now().Unix() inspection_reference.InspectDate = fdate inspection_reference.UTime = fdate 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.Unix(), printInfo.ID).Find(&inspection).Count(&itotal).Error if itotal <= 0 { inspection.PatientId = printInfo.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 = item.FResult inspection.InspectDate = item.FCheckTime inspection.RecordDate = record_date.Unix() inspection.Status = 1 inspection.CreatedTime = time.Now().Unix() inspection.UpdatedTime = time.Now().Unix() inspection.UTime = item.FCheckTime inspection.HisUserId = strconv.FormatInt(printInfo.ID, 10) err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error if err != nil { tx.Rollback() } } tx.Commit() } //插入一条查询检验检查结果记录状态数据 var info LisSyncResultStatusInfo info.Ctime = time.Now().Unix() info.Status = 1 info.Mtime = time.Now().Unix() info.OrgId = org_id info.IsResult = 1 info.LastId = int64(len(list)) info.ResultDate = time.Now().Unix() writeMiddleDb.Save(&info) } // 第一步:跟进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) } } } return } func GetSHResultRecord(rdb *gorm.DB, synctime string) (record []*KMSheet, err error) { err = rdb.Model(&KMSheet{}).Where("F_CheckTime >= ?", synctime).Find(&record).Error return } // 判断是否包含中文 func IsChineseChar(str string) bool { for _, r := range str { if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) { return true } } return false } func GetResultRecordByTime(id int64) (record []*KMResult, err error) { err = readDb.Model(&KMResult{}).Where("barcode > ? AND repdate >= 2021-08-12 00:00:00", id).Order("barcode asc").Find(&record).Error return }