1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009 |
- 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 := "2023-01-01"
- end_time := "2023-11-22"
- record, _ := GetLastSyncResultRecord(org_id)
- if record.ID > 0 {
- start_time = record.GpSyncEndDate
- end_time = time.Now().Format("2006-01-02")
- }
- 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)
- 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 item_name = ? and status = 1", org_id, 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
- code, _ := strconv.ParseInt(list[0].Barcode, 10, 64)
- info.LastId = code
- info.ResultDate = time.Now().Unix()
- info.GpSyncEndDate = end_time
- writeMiddleDb.Save(&info)
- }
- }
- SyncToSssytx()
- }
-
- 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 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
- }
|