package service import ( "IC/models" "IC/utils" "encoding/json" "fmt" "github.com/astaxie/beego" "os" "strconv" "strings" "time" ) const ( key = "e7a96b253a9e45e78eda2a849ae5e200" ) func saveLog1(result string, request string, infno string, desc string) { org_id, _ := beego.AppConfig.Int64("org_id") //miConfig, _ := service.FindMedicalInsuranceInfo(org_id) dir := "下载数据日志" utils.Mkdir(dir) month := time.Unix(1557042972, 0).Format("1") year := time.Now().Format("2006") month = time.Now().Format("01") day := time.Now().Format("02") hour := time.Now().Format("15") min := time.Now().Format("04") sec := time.Now().Format("05") result_time := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec file := strconv.FormatInt(org_id, 10) + "_" + year + month + day + "_log" file_name := file + ".txt" file_path := "下载数据日志" + "/" + file_name exist, _ := utils.PathExists(file_path) if exist { //存在 fmt.Println("存在") f, err := os.OpenFile(file_path, os.O_WRONLY, 0644) if err != nil { fmt.Println("read fail") } content := "\r\n" + "\r\n" + "\r\n" + result_time + " " + "【 " + desc + infno + "入参" + " 】:" + "\r\n" + request + "\r\n" + result_time + " " + "【 " + desc + infno + "出参" + " 】:" + "\r\n" + result n, _ := f.Seek(0, 2) _, err = f.WriteAt([]byte(content), n) } else { //不存在 fmt.Println("文件不存在,创建文件") f, err := os.Create("下载数据日志" + "/" + file_name) defer f.Close() if err != nil { } else { _, err = f.Write([]byte("记录日志")) } } } func SaveErrorLog(result string, request string, infno string, desc string) { //org_id, _ := beego.AppConfig.Int64("org_id") //miConfig, _ := service.FindMedicalInsuranceInfo(org_id) dir := "faild" utils.Mkdir(dir) month := time.Unix(1557042972, 0).Format("1") year := time.Now().Format("2006") month = time.Now().Format("01") day := time.Now().Format("02") hour := time.Now().Format("15") min := time.Now().Format("04") sec := time.Now().Format("05") result_time := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec file := "failed" + "_" + year + month + day + "_log" file_name := file + ".txt" file_path := "faild" + "/" + file_name exist, _ := utils.PathExists(file_path) if exist { //存在 fmt.Println("存在") f, err := os.OpenFile(file_path, os.O_WRONLY, 0644) if err != nil { fmt.Println("read fail") } content := "\r\n" + "\r\n" + "\r\n" + result_time + " " + "【 " + desc + infno + "入参" + " 】:" + "\r\n" + request + "\r\n" + result_time + " " + "【 " + desc + infno + "出参" + " 】:" + "\r\n" + result n, _ := f.Seek(0, 2) _, err = f.WriteAt([]byte(content), n) } else { //不存在 fmt.Println("文件不存在,创建文件") f, err := os.Create("faild" + "/" + file_name) defer f.Close() if err != nil { } else { _, err = f.Write([]byte("记录日志")) } } } func saveSuccessLog(result string, request string, infno string, desc string) { //org_id, _ := beego.AppConfig.Int64("org_id") //miConfig, _ := service.FindMedicalInsuranceInfo(org_id) //dir := "同步成功日志" //utils.Mkdir(dir) month := time.Unix(1557042972, 0).Format("1") year := time.Now().Format("2006") month = time.Now().Format("01") day := time.Now().Format("02") hour := time.Now().Format("15") min := time.Now().Format("04") sec := time.Now().Format("05") result_time := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec file := "success" + "_" + year + month + day + "_log" file_name := file + ".txt" file_path := file_name exist, _ := utils.PathExists(file_path) if exist { //存在 fmt.Println("存在") f, err := os.OpenFile(file_path, os.O_WRONLY, 0644) if err != nil { fmt.Println("read fail") } content := "\r\n" + "\r\n" + "\r\n" + result_time + " " + "【 " + desc + infno + "入参" + " 】:" + "\r\n" + request + "\r\n" + result_time + " " + "【 " + desc + infno + "出参" + " 】:" + "\r\n" + result n, _ := f.Seek(0, 2) _, err = f.WriteAt([]byte(content), n) } else { //不存在 fmt.Println("文件不存在,创建文件") f, err := os.Create(file_name) defer f.Close() if err != nil { } else { _, err = f.Write([]byte("记录日志")) } } } // 发送申请单逻辑 func UploadSYData(org_id int64) { //record, _ := GetLastSyncRecord(org_id) //if record.ID == 0 { list, _ := GetAllLisDataFor10191(org_id) if len(list) > 0 { //插入中间库中 for _, item := range list { if len(item.ApplyCode) == 0 { var apply models.JYApply apply.BedCode = "" apply.PatientName = item.Patient.Name idCard := item.Patient.IdCardNo // 身份证号码示例 age, err := GetAge(idCard) if err != nil { age = int(item.Patient.Age) fmt.Println("计算年龄时出错:", err) } apply.Age = strconv.FormatInt(int64(age), 10) apply.AgeUnit = "岁" if item.Patient.Gender == 1 { apply.Sex = "男" } else if item.Patient.Gender == 2 { apply.Sex = "女" } else { apply.Sex = "不详" } apply.RegisterCode = "" apply.Diagnosis = "" apply.Ward = "" role, _ := GetAdminUserInfoByID(org_id, 2398) apply.SendDoctor = role.UserName apply.PatientType = 1 apply.PatientPhone = item.Patient.Phone //applys = append(applys, apply) var jYApplyDetails []models.CustomerApplyDetail if len(item.ItemIds) > 0 { team_ids := strings.Split(item.ItemIds, ",") for index, subItem := range team_ids { //判断一些需要拆分的组套 血透体检(定量):需要拆分出传染病检查(定性),丙型肝炎抗体测试,血透体检(定量) if subItem == "267" { //血透体检(定量) var jYApplyDetail models.CustomerApplyDetail id, _ := strconv.ParseInt(subItem, 10, 64) jYApplyDetail.CustomerChargeItemCode = strconv.FormatInt(id, 10) jYApplyDetail.RowIndex = int64(index + 1) jYApplyDetail.Barcode = "" jYApplyDetail.CustomerBarcode = strconv.FormatInt(item.ID, 10) jYApplyDetail.Comment = "" jYApplyDetail.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05") jYApplyDetails = append(jYApplyDetails, jYApplyDetail) //丙型肝炎抗体测定 var jYApplyDetail2 models.CustomerApplyDetail id2, _ := strconv.ParseInt("26701", 10, 64) jYApplyDetail2.CustomerChargeItemCode = strconv.FormatInt(id2, 10) jYApplyDetail2.RowIndex = int64(index + 1) jYApplyDetail2.Barcode = "" jYApplyDetail2.CustomerBarcode = strconv.FormatInt(item.ID, 10) jYApplyDetail2.Comment = "" jYApplyDetail2.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05") jYApplyDetails = append(jYApplyDetails, jYApplyDetail2) //传染病检查(定性) var jYApplyDetail3 models.CustomerApplyDetail id3, _ := strconv.ParseInt("26700", 10, 64) jYApplyDetail3.CustomerChargeItemCode = strconv.FormatInt(id3, 10) jYApplyDetail3.RowIndex = int64(index + 1) jYApplyDetail3.Barcode = "" jYApplyDetail3.CustomerBarcode = strconv.FormatInt(item.ID, 10) jYApplyDetail3.Comment = "" jYApplyDetail3.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05") jYApplyDetails = append(jYApplyDetails, jYApplyDetail3) } //判断一些需要拆分的组套 血透体检(定性):需要拆分出传染病检查(定性),丙型肝炎抗体测试,血透体检(定性) if subItem == "424" { //血透体检(定性) var jYApplyDetail models.CustomerApplyDetail id, _ := strconv.ParseInt(subItem, 10, 64) jYApplyDetail.CustomerChargeItemCode = strconv.FormatInt(id, 10) jYApplyDetail.RowIndex = int64(index + 1) jYApplyDetail.Barcode = "" jYApplyDetail.CustomerBarcode = strconv.FormatInt(item.ID, 10) jYApplyDetail.Comment = "" jYApplyDetail.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05") jYApplyDetails = append(jYApplyDetails, jYApplyDetail) //丙型肝炎抗体测定 var jYApplyDetail2 models.CustomerApplyDetail id2, _ := strconv.ParseInt("42401", 10, 64) jYApplyDetail2.CustomerChargeItemCode = strconv.FormatInt(id2, 10) jYApplyDetail2.RowIndex = int64(index + 1) jYApplyDetail2.Barcode = "" jYApplyDetail2.CustomerBarcode = strconv.FormatInt(item.ID, 10) jYApplyDetail2.Comment = "" jYApplyDetail2.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05") jYApplyDetails = append(jYApplyDetails, jYApplyDetail2) //传染病检查(定性) var jYApplyDetail3 models.CustomerApplyDetail id3, _ := strconv.ParseInt("42400", 10, 64) jYApplyDetail3.CustomerChargeItemCode = strconv.FormatInt(id3, 10) jYApplyDetail3.RowIndex = int64(index + 1) jYApplyDetail3.Barcode = "" jYApplyDetail3.CustomerBarcode = strconv.FormatInt(item.ID, 10) jYApplyDetail3.Comment = "" jYApplyDetail3.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05") jYApplyDetails = append(jYApplyDetails, jYApplyDetail3) } var jYApplyDetail models.CustomerApplyDetail id, _ := strconv.ParseInt(subItem, 10, 64) jYApplyDetail.CustomerChargeItemCode = strconv.FormatInt(id, 10) jYApplyDetail.RowIndex = int64(index + 1) jYApplyDetail.Barcode = "" jYApplyDetail.CustomerBarcode = strconv.FormatInt(item.ID, 10) jYApplyDetail.Comment = "" jYApplyDetail.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05") jYApplyDetails = append(jYApplyDetails, jYApplyDetail) } } if len(item.ProjectIds) > 0 { team_ids := strings.Split(item.ProjectIds, ",") for index, subItem := range team_ids { var jYApplyDetail models.CustomerApplyDetail id, _ := strconv.ParseInt(subItem, 10, 64) jYApplyDetail.CustomerChargeItemCode = strconv.FormatInt(id, 10) jYApplyDetail.RowIndex = int64(index) jYApplyDetail.Barcode = "" jYApplyDetail.CustomerBarcode = strconv.FormatInt(item.ID, 10) jYApplyDetail.Comment = "" jYApplyDetail.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05") jYApplyDetails = append(jYApplyDetails, jYApplyDetail) } } result := PostInspectApply(key, apply, jYApplyDetails) if result.Res == 0 { item.ApplyCode = result.JYApplyResult.ApplyCode writeDb.Save(&item) jsonData, err := json.Marshal(apply) if err != nil { fmt.Println("转换为JSON时发生错误:", err) } jsonStr := string(jsonData) jsonData2, err2 := json.Marshal(jYApplyDetails) if err2 != nil { fmt.Println("转换为JSON时发生错误:", err2) } jsonStr2 := string(jsonData2) jsonData3, err3 := json.Marshal(result) if err3 != nil { fmt.Println("转换为JSON时发生错误:", err3) } jsonStr3 := string(jsonData3) saveSuccessLog(jsonStr3, jsonStr+"\r\n"+jsonStr2, "申清单成功日志", "申清单") } else { jsonData, err := json.Marshal(apply) if err != nil { fmt.Println("转换为JSON时发生错误:", err) } jsonStr := string(jsonData) jsonData2, err2 := json.Marshal(jYApplyDetails) if err2 != nil { fmt.Println("转换为JSON时发生错误:", err2) } jsonStr2 := string(jsonData2) jsonData3, err3 := json.Marshal(result) if err3 != nil { fmt.Println("转换为JSON时发生错误:", err3) } jsonStr3 := string(jsonData3) SaveErrorLog(jsonStr3, jsonStr+"\r\n"+jsonStr2, "申清单错误日志", "申清单") } } } //插入一条插入中间库记录数据 var info LisSyncProcessStatusInfo info.Ctime = time.Now().Unix() info.Status = 1 info.Mtime = time.Now().Unix() info.OrgId = org_id info.IsInsert = 1 info.IsResult = 0 //info.LastId = list[len(list)-1].ID info.ResultDate = 0 writeMiddleDb.Save(&info) } } // 同步逻辑 //func SyncSYData(org_id int64) { // record, _ := GetLastSyncResultRecord(org_id) // if record.ID == 0 { // //通过接口从高品那里获取检验数据 // start_time := "2023-06-01" // end_time := "2023-06-30" // result := GetGPReportListData(start_time, end_time, key) // list := GetGPResultDataTwo(result, key) // jsonData, err := json.Marshal(result) // if err != nil { // fmt.Println("转换为JSON时发生错误:", err) // return // } // jsonStr := string(jsonData) // jsonData1, err1 := json.Marshal(list) // if err1 != nil { // fmt.Println("转换为JSON时发生错误:", err1) // return // } // jsonStr1 := string(jsonData1) // saveLog1(jsonStr, "", "列表数据", "列表数据") // saveLog1(jsonStr1, "", "结果数据", "结果数据") // // if err == nil { // fmt.Println(list) // //插入到系统检验检查数据 // if len(list) > 0 { // for _, item := range list { // project_id := int64(0) // //xt_project_id := int64(0) // if len(item.ListResult.Barcode) > 0 { // project_id, _, _ = GetSyjhProjectID(org_id, item.ChargeItemName) // } else { // continue // } // //var patient models.Patients // printInfo, _ := GetHisLabelPrintStatusInfoById(item.ListResult.CustomerBarcode) // if printInfo.ID == 0 { // patient, _ := GetPatientID(org_id, item.ListResult.PatientName) // printInfo.PatientId = patient.ID // // } // if printInfo.PatientId == 0 { // continue // } // item_id, _ := GetSyItemID(org_id, item.ChargeItemName, item.InspectionName, project_id) // tx := writeMiddleDb.Begin() // var inspection models.MiddleInspection // var inspection_reference models.MiddleInspectionReference // // recordDateStr := "" // inspect_date := "" // if len(item.ListResult.InspectDate) > 0 { // // inspect_date = item.ListResult.InspectDate // // } else { // inspect_date = strings.Split(item.ListResult.ReceiveDateTime, " ")[0] // // } // // recordDateStr = inspect_date // // date, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr) // record_date := date // var total int // var RangeOptions string // var RangeMin string // var RangeMax string // // 判断检查类型 // var ItemType int // if strings.Contains(item.Reference, "--") { // ItemType = 1 // } else { // ItemType = 2 // // } // // if ItemType == 1 { // Range := strings.Split(item.Reference, "--") // RangeMin = Range[0] // RangeMax = Range[1] // } else { // RangeOptions = item.Reference // } // err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_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.ChargeItemName // inspection_reference.Project = item.ChargeItemName // inspection_reference.ProjectId = project_id // inspection_reference.ItemName = item.InspectionName // inspection_reference.ItemNameAddition = item.Barcode // 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.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.Unix(), printInfo.PatientId).Find(&inspection).Count(&itotal).Error // if itotal <= 0 { // //item.Result = strings.Replace(item.Result, ">", ">", -1) // //item.Result = strings.Replace(item.Result, "<", "<", -1) // inspection.PatientId = printInfo.PatientId // 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.Result // inspection.InspectDate = inspect_date // inspection.RecordDate = record_date.Unix() // inspection.Status = 1 // inspection.CreatedTime = time.Now().Unix() // inspection.UpdatedTime = time.Now().Unix() // inspection.UTime = inspect_date // inspection.HisUserId = strconv.FormatInt(printInfo.PatientId, 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 // code, _ := strconv.ParseInt(list[0].Barcode, 10, 64) // info.LastId = code // info.ResultDate = time.Now().Unix() // info.GpSyncEndDate = end_time // writeMiddleDb.Save(&info) // } // } // // } else { // //start_time := record.GpSyncEndDate // //end_time := time.Now().Format("2006-01-02") // // // //result := GetGPReportListData(start_time, end_time, key) // //list := GetGPResultDataTwo(result, key) // // // ////插入一条插入中间库记录数据 // //if err == nil { // // if len(list) > 0 { // // //插入到系统检验检查数据 // // for _, item := range list { // // project_id := int64(0) // // if len(item.ListResult.CustomerBarcode) > 0 { // // project_id, _, _ = GetSyjhProjectID(org_id, item.ChargeItemName) // // } else { // // continue // // } // // var patient models.Patients // // printInfo, _ := GetHisLabelPrintStatusInfoById(item.ListResult.CustomerBarcode) // // if printInfo.ID == 0 { // // patient, _ = GetPatientID(org_id, item.ListResult.PatientName) // // } // // if patient.ID > 0 { // // printInfo.PatientId = patient.ID // // } // // if printInfo.PatientId == 0 { // // continue // // } // // // // item_id, _ := GetSyItemID(org_id, item.ChargeItemName, item.InspectionName, project_id) // // tx := writeMiddleDb.Begin() // // var inspection models.MiddleInspection // // var inspection_reference models.MiddleInspectionReference // // // // recordDateStr := "" // // inspect_date := "" // // if len(item.ListResult.InspectDate) > 0 { // // // // inspect_date = item.ListResult.InspectDate // // } else { // // inspect_date = strings.Split(item.ListResult.ReceiveDateTime, " ")[0] // // // // } // // recordDateStr = inspect_date // // // // date, _ := utils.ParseTimeStringToTime("2006-01-02", 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 // // // 判断检查类型 // // var ItemType int // // if strings.Contains(item.Reference, "--") { // // ItemType = 1 // // } else { // // ItemType = 2 // // // // } // // // // if ItemType == 1 { // // Range := strings.Split(item.Reference, "--") // // RangeMin = Range[0] // // RangeMax = Range[1] // // } else { // // RangeOptions = item.Reference // // } // // // // 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.ChargeItemName // // inspection_reference.Project = item.ChargeItemName // // inspection_reference.ProjectId = project_id // // inspection_reference.ItemName = item.InspectionName // // inspection_reference.ItemNameAddition = item.Barcode // // 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.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.Unix(), printInfo.PatientId).Find(&inspection).Count(&itotal).Error // // if itotal <= 0 { // // //item.Result = strings.Replace(item.Result, ">", ">", -1) // // //item.Result = strings.Replace(item.Result, "<", "<", -1) // // inspection.PatientId = printInfo.PatientId // // 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.Result // // inspection.InspectDate = inspect_date // // inspection.RecordDate = record_date.Unix() // // inspection.Status = 1 // // inspection.CreatedTime = time.Now().Unix() // // inspection.UpdatedTime = time.Now().Unix() // // inspection.UTime = inspect_date // // inspection.HisUserId = strconv.FormatInt(printInfo.PatientId, 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.ResultDate = time.Now().Unix() // // info.GpSyncEndDate = end_time // // 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 SyncSYData2() { org_id := int64(10191) start_time := "2024-04-20" end_time := "2024-04-26" record, _ := GetLastSyncResultRecord(org_id) if record.ID > 0 { start_time = record.GpSyncEndDate end_time = time.Now().Format("2006-01-02") } loc, _ := time.LoadLocation("Local") theTime, _ := time.ParseInLocation("2006-01-02", start_time, loc) newtheTime := theTime.Unix() - 60*60*24*3 SyncTime := time.Unix(newtheTime, 0) syncTimeStr := SyncTime.Format("2006-01-02") result := GetGPReportListData(syncTimeStr, end_time, key) if len(result) > 0 { for _, item := range result { list := GetGPResultDataTwo(item, key) //插入到系统检验检查数据 if len(list) > 0 { for _, item := range list { project_id := int64(0) patient, _ := GetPatientID(org_id, item.ListResult.PatientName) if patient.ID == 0 { continue } tx := writeMiddleDb.Begin() var inspection models.MiddleInspection var inspection_reference models.MiddleInspectionReference recordDateStr := item.ListResult.ReceiveDateTime recordDate, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr) inspect_date := recordDate.Format("2006-01-02 15:04") date, _ := time.Parse("2006-01-02 15:04:05", recordDateStr) utils.InfoLog("date2: %v", date.Unix()) record_date_time, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date) record_date := record_date_time.Unix() var total int var RangeOptions string var RangeMin string var RangeMax string // 判断检查类型 var ItemType int if strings.Contains(item.Reference, "--") { ItemType = 1 } else { ItemType = 2 } if ItemType == 1 { Range := strings.Split(item.Reference, "--") RangeMin = Range[0] RangeMax = Range[1] } else { RangeOptions = item.Reference } err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project = ? and item_name = ? and status = 1", org_id, item.ChargeItemName, item.InspectionName).Find(&inspection_reference).Count(&total).Error if inspection_reference.ID > 0 { ItemType = inspection_reference.RangeType project_id = inspection_reference.ProjectId } if total <= 0 { var inspection_referenceTwo models.MiddleInspectionReference err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, item.ChargeItemName).First(&inspection_referenceTwo).Error if inspection_referenceTwo.ID > 0 { project_id = inspection_referenceTwo.ProjectId } else { err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_referenceTwo).Error if inspection_referenceTwo.ProjectId > 0 { project_id = inspection_referenceTwo.ProjectId + 1 } else { project_id = 1019101 } } var item_id int64 var inspection_referenceThree models.MiddleInspectionReference 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_referenceThree) utils.InfoLog("inspection_reference: %v", inspection_reference) if inspection_referenceThree.ItemId > 0 { item_id = inspection_referenceThree.ItemId + 1 } else { item_id = project_id*100 + 1 } inspection_reference.OrgId = org_id inspection_reference.ProjectName = item.ChargeItemName inspection_reference.Project = item.ChargeItemName inspection_reference.ProjectId = project_id inspection_reference.ItemName = item.InspectionName inspection_reference.ItemNameAddition = item.Barcode 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.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, inspection_reference.ItemId, record_date, patient.ID).Find(&inspection).Count(&itotal).Error if itotal <= 0 { //item.Result = strings.Replace(item.Result, ">", ">", -1) //item.Result = strings.Replace(item.Result, "<", "<", -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 = inspection_reference.ItemId inspection.InspectValue = item.Result inspection.InspectDate = inspect_date inspection.RecordDate = record_date inspection.Status = 1 inspection.CreatedTime = time.Now().Unix() inspection.UpdatedTime = time.Now().Unix() inspection.UTime = inspect_date inspection.HisUserId = strconv.FormatInt(patient.ID, 10) inspection.SysProjectId = inspection_reference.XtProjectId inspection.SysItemId = inspection_reference.XtItemId 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.ResultDate = time.Now().Unix() info.GpSyncEndDate = end_time writeMiddleDb.Save(&info) } SyncToSssytx() UpdateJg() } func SyncToSssytx() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10191) // 第一步:跟进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 { if inspection.SysProjectId > 0 && inspection.SysItemId > 0 { SyncSssyInspection(&inspection) } } } utils.SuccessLog("检验检查同步任务完成") } func UpdateJg() error { var inspections []models.Inspection err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and inspect_date >= ? and status = 1", 10191, 15, 1704038400).Group("patient_id,inspect_date").Find(&inspections).Error for _, info := range inspections { var inspectionByOne []models.Inspection err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and inspect_date = ? and patient_id = ? and status = 1", 10191, 15, info.InspectDate, info.PatientId).Find(&inspectionByOne).Error if len(inspectionByOne) == 2 { for _, tinfo := range inspectionByOne { utils.InfoLog("date1: %v", tinfo.ID) if tinfo.ItemId != 115 && tinfo.ItemId != 37 { // 报告单里只有肌酐和尿素这两个项目要上传到“透后肾功能”里 continue } if tinfo.ItemId == 115 { tinfo.ProjectId = 8060 tinfo.ItemId = 8060 } if tinfo.ItemId == 37 { tinfo.ProjectId = 8060 tinfo.ItemId = 8061 } writeDb.Save(&tinfo) } } } return err } // 从机构将数据同步到中间库 func SyncSssyInspection(data *models.MiddleInspection) error { tx := writeDb.Begin() var inspection models.Inspection var total int err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and item_id = ? and patient_id =? and inspect_date=? and status = 1", data.OrgId, data.ProjectId, data.ItemId, data.PatientId, data.RecordDate).Find(&inspection).Count(&total).Error if total <= 0 { inspection.OrgId = data.OrgId inspection.PatientId = data.PatientId inspection.ProjectName = data.ProjectName // inspection.Project = data.ProjectName inspection.ProjectId = data.SysProjectId inspection.ItemName = data.ItemName inspection.ItemId = data.SysItemId inspection.InspectType = int64(data.InspectType) inspection.InspectValue = data.InspectValue inspection.InspectTips = data.InspectTips inspection.InspectDate = data.RecordDate inspection.Status = 1 inspection.CreatedTime = time.Now().Unix() inspection.UpdatedTime = time.Now().Unix() err := tx.Model(&models.Inspection{}).Create(&inspection).Error if err != nil { tx.Rollback() } data.IsSync = 1 data.SyncId = inspection.ID data.UpdatedTime = time.Now().Unix() ierr := writeMiddleDb.Save(&data).Error if ierr != nil { tx.Rollback() } } tx.Commit() return err } func GetAge(idCard string) (int, error) { birthDate, err := getBirthDateFromIDCard(idCard) if err != nil { return 0, err } age := calculateAge(birthDate) return age, nil } func getBirthDateFromIDCard(idCard string) (time.Time, error) { birthDateStr := idCard[6:14] // 身份证号码中生日部分的字符 birthDate, err := time.Parse("20060102", birthDateStr) if err != nil { return time.Time{}, err } return birthDate, nil } func calculateAge(birthDate time.Time) int { now := time.Now() age := now.Year() - birthDate.Year() // 如果当前月份小于出生月份,或者当前月份等于出生月份但当前日期小于出生日期,则年龄减一 if now.Month() < birthDate.Month() || (now.Month() == birthDate.Month() && now.Day() < birthDate.Day()) { age-- } return age } func GetHisLabelPrintStatusInfoById(id string) (info models.HisLabelPrintStatusInfo, err error) { err = readDb.Model(&models.HisLabelPrintStatusInfo{}).Where("id = ?", id).First(&info).Error return } // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个 func GetSyjhProjectID(org_id int64, project_name string) (project_id int64, sys_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, inspection_reference.XtProjectId, 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, 0, err } else { return 10191000, 0, err } } } // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个 func GetSyItemID(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 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*10 + 1, err } } } // 从机构将数据同步到中间库 func SyncInspectionReferenceThree(data *models.MiddleInspectionReference) error { tx := writeDb.Begin() var inspection_reference models.InspectionReference var total int err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? and item_id = ? and status = 1", data.OrgId, data.ItemId).Find(&inspection_reference).Count(&total).Error if total <= 0 { inspection_reference.OrgId = data.OrgId inspection_reference.ProjectName = data.ProjectName inspection_reference.Project = data.ProjectName inspection_reference.ProjectId = data.ProjectId inspection_reference.ItemName = data.ItemName inspection_reference.ItemId = data.ItemId inspection_reference.RangeType = data.RangeType inspection_reference.RangeMin = data.RangeMin inspection_reference.RangeMax = data.RangeMax inspection_reference.RangeValue = data.RangeValue inspection_reference.RangeOptions = data.RangeOptions inspection_reference.Unit = data.Unit inspection_reference.Status = 1 inspection_reference.CreatedTime = time.Now().Unix() inspection_reference.UpdatedTime = time.Now().Unix() err := tx.Model(&models.InspectionReference{}).Create(&inspection_reference).Error if err != nil { tx.Rollback() } data.IsSync = 1 data.SyncId = inspection_reference.ID data.UpdatedTime = time.Now().Unix() ierr := writeMiddleDb.Save(&data).Error if ierr != nil { tx.Rollback() } } tx.Commit() return err }