123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- package service
-
- import (
- "IC/models"
- "IC/utils"
- _ "encoding/json"
- _ "fmt"
- oracle "github.com/dzwvip/gorm-oracle"
- "gorm.io/gorm"
- "strconv"
- "strings"
- "time"
- )
-
- // 四川乐山友谊医院 HIS医嘱同步+LIS检验结果同步
-
- type scyyyylis struct {
- HisUserId string `gorm:"column:HIS_USER_ID" json:"HIS_USER_ID"`
- PatientName string `gorm:"column:PATIENT_NAME" json:"PATIENT_NAME"`
- ProjectId string `gorm:"column:PROJECT_ID" json:"PROJECT_ID"`
- ProjectName string `gorm:"column:PROJECT_NAME" json:"PROJECT_NAME"`
- InspectDate string `gorm:"column:INSPECT_DATE" json:"INSPECT_DATE"`
- ItemId string `gorm:"column:ITEM_ID" json:"ITEM_ID"`
- ItemName string `gorm:"column:ITEM_NAME" json:"ITEM_NAME"`
- ItemType int64 `gorm:"column:ITEM_TYPE" json:"ITEM_TYPE"`
- ItemValue string `gorm:"column:ITEM_VALUE" json:"ITEM_VALUE"`
- RangeMin string `gorm:"column:RANGE_MIN" json:"RANGE_MIN"`
- RangeMax string `gorm:"column:RANGE_MAX" json:"RANGE_MAX"`
- RangeValue string `gorm:"column:RANGE_VALUE" json:"RANGE_VALUE"`
- RangeOptions string `gorm:"column:RANGE_OPTIONS" json:"RANGE_OPTIONS"`
- Unit string `gorm:"column:UNIT" json:"UNIT"`
- State int64 `gorm:"column:STATE" json:"STATE"`
- CTime string `gorm:"column:C_TIME" json:"C_TIME"`
- UTime string `gorm:"column:U_TIME" json:"U_TIME"`
- }
-
- func (scyyyylis) TableName() string {
- return "yyyyxthy"
- }
-
- func GetSclsyyyyLis(rdb *gorm.DB, c_time string) (record []*scyyyylis, err error) {
- err = rdb.Model(&scyyyylis{}).Where("C_TIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')", c_time).Find(&record).Error
- return
- }
-
- // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
- func GetScyyyyProjectID(org_id int64, project_name string, item_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 = ? and item_name = ? ", org_id, project_name, item_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ProjectId, err
- } else {
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? ", org_id, project_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ProjectId, err
- } else {
- err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
- if inspection_reference.ProjectId > 0 {
- return inspection_reference.ProjectId + 1, err
- } else {
- return 1067701, err
- }
- }
- }
- }
-
- // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
- func GetScyyyyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
- var inspection_reference models.MiddleInspectionReference
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ItemId, err
- } else {
- err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and project_id = ? ", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_reference).Error
- if inspection_reference.ItemId > 0 {
- return inspection_reference.ItemId + 1, err
- } else {
- return project_id*100 + 1, err
- }
- }
- }
-
- // 四川乐山友谊医院Lis同步
- func SyncScyyyyLis() (err error) {
- org_id := int64(10677)
- orgDb, err := gorm.Open(oracle.Open("oracle://zjybase:zjybase@10.0.0.248:1521/orcl"), &gorm.Config{})
-
- if err != nil {
- utils.InfoLog("创建数据库连接失败:%v", err)
- }
-
- // 第一步:获取上一次同步的时间点
- syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
- var sync_time int64
- if syncLastInfo.ID > 0 {
- sync_time = syncLastInfo.SyncTime
- } else {
- sync_time = 1704038400 // 2024-01-01
- }
-
- // 第二步:获取时间内所有检验结果
- sync_time_temp := time.Unix(sync_time, 0)
- startTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
-
- yyyyLis, _ := GetSclsyyyyLis(orgDb, startTimeStr)
- if len(yyyyLis) > 0 {
- for _, LisInfo := range yyyyLis {
- var patientInfo models.Patients
- // 根据身份证号获取患者信息
- if len(LisInfo.HisUserId) > 0 {
- patientInfo, _ = GetPatientsInfoByIDCardNo(LisInfo.HisUserId, org_id)
- if patientInfo.ID <= 0 {
- continue
- }
- } else {
- patientInfo, _ = GetPatientsInfoByName(LisInfo.PatientName, org_id)
- if patientInfo.ID <= 0 {
- continue
- }
- }
-
- timeLayout := "2006-01-02 15:04"
- loc, _ := time.LoadLocation("Local")
-
- timstr := strings.Split(LisInfo.InspectDate, "T")
- timstem := strings.Split(timstr[1], "+")
- tempRecord := timstr[0] + " " + timstem[0]
- recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc)
- recordTimeInt := recordTime.Unix()
- recordTimeStr := time.Unix(recordTimeInt, 0).Format(timeLayout)
-
- project_id, _ := GetScyyyyProjectID(org_id, LisInfo.ProjectName, LisInfo.ItemName)
- item_id, _ := GetScyyyyItemID(org_id, LisInfo.ProjectName, LisInfo.ItemName, project_id)
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
-
- var total int
- var RangeOptions string
- var RangeMin string
- var RangeMax string
- var ItemType int
- Range := strings.Split(LisInfo.RangeValue, "--")
- if len(Range) > 1 {
- RangeMin = Range[0]
- RangeMax = Range[1]
- ItemType = 1
- } else {
- ItemType = 2
- RangeOptions = LisInfo.RangeValue
- }
-
- 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 = LisInfo.ProjectName
- inspection_reference.Project = LisInfo.ProjectName
- inspection_reference.ProjectId = project_id
- inspection_reference.ItemName = LisInfo.ItemName
- // inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
- inspection_reference.ItemId = item_id
- inspection_reference.RangeType = ItemType
- inspection_reference.RangeMin = RangeMin
- inspection_reference.RangeMax = RangeMax
- // inspection_reference.RangeValue = RangeValue
- inspection_reference.RangeOptions = RangeOptions
- inspection_reference.Unit = LisInfo.Unit
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
- inspection_reference.InspectDate = recordTimeStr
- inspection_reference.UTime = recordTimeStr
- 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, recordTimeStr, patientInfo.ID).Find(&inspection).Count(&itotal).Error
- if itotal <= 0 {
- // inspection.InspectValue = strings.Replace(LisInfo.Testresult, ">", ">", -1)
- // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "<", "<", -1)
- inspection.PatientId = patientInfo.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 = LisInfo.ItemValue
- inspection.InspectDate = recordTimeStr
- inspection.RecordDate = recordTimeInt
- // inspection.InspectTips = report.Resultstate
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- // inspection.UTime = record_date.Format(timeLayout)
- // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
- err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
- if err != nil {
- tx.Rollback()
- }
- }
- tx.Commit()
- }
- 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
- }
- }
-
- SyncToSclsyyyy()
- return
- }
-
- func SyncToSclsyyyy() {
- utils.TraceLog("检验检查同步任务开始执行")
- org_id := int64(10677)
-
- // 第一步:跟进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("检验检查同步任务完成")
- }
-
- type yyyyxtyz struct {
- HisUserId string `gorm:"column:HIS_USER_ID" json:"HIS_USER_ID" form:"HIS_USER_ID"`
- UserName string `gorm:"column:USER_NAME" json:"USER_NAME" form:"USER_NAME"`
- DoctorId string `gorm:"column:DOCTOR_ID" json:"DOCTOR_ID" form:"DOCTOR_ID"`
- DoctorName string `gorm:"column:DOCTOR_NAME" json:"DOCTOR_NAME" form:"DOCTOR_NAME"`
- AdviceId int64 `gorm:"column:ADVICE_ID" json:"ADVICE_ID" form:"ADVICE_ID"`
- MedListCodg string `gorm:"column:MED_LIST_CODG" json:"MED_LIST_CODG" form:"MED_LIST_CODG"`
- StartTime string `gorm:"column:START_TIME" json:"START_TIME" form:"START_TIME"`
- AdviceName string `gorm:"column:ADVICE_NAME" json:"ADVICE_NAME" form:"ADVICE_NAME"`
- AdviceDesc string `gorm:"column:ADVICE_DESC" json:"ADVICE_DESC" form:"ADVICE_DESC"`
- SingleDose string `gorm:"column:SINGLE_DOSE" json:"SINGLE_DOSE" form:"SINGLE_DOSE"`
- SingleDoseUnit string `gorm:"column:SINGLE_DOSE_UNIT" json:"SINGLE_DOSE_UNIT" form:"SINGLE_DOSE_UNIT"`
- PrescribingNumber string `gorm:"column:PRESCRIBING_NUMBER" json:"PRESCRIBING_NUMBER" form:"PRESCRIBING_NUMBER"`
- PrescribingNumberUnit string `gorm:"column:PRESCRIBING_NUMBER_UNIT" json:"PRESCRIBING_NUMBER_UNIT" form:"PRESCRIBING_NUMBER_UNIT"`
- DeliveryWay string `gorm:"column:DELIVERY_WAY" json:"DELIVERY_WAY" form:"DELIVERY_WAY"`
- ExecutionFrequency string `gorm:"column:EXECUTION_FREQUENCY" json:"EXECUTION_FREQUENCY" form:"EXECUTION_FREQUENCY"`
- State int64 `gorm:"column:STATE" json:"STATE" form:"STATE"`
- CTime string `gorm:"column:CREATE_TIME" json:"CREATE_TIME" form:"CREATE_TIME"`
- UTime string `gorm:"column:UPDATE_TIME" json:"UPDATE_TIME" form:"UPDATE_TIME"`
- }
-
- func (yyyyxtyz) TableName() string {
- return "yyyyxtyz"
- }
-
- func GetSclsyyyyyz(rdb *gorm.DB, update_time string) (record []*yyyyxtyz, err error) {
- err = rdb.Model(&yyyyxtyz{}).Where("update_time >= ?", update_time).Find(&record).Error
- return
- }
-
- func GetAdviceBySyncAdviceIdForScyyyy(orgId int64, sync_advice_id int64, advice_date int64, patient_id int64) (advice models.XtDoctorAdvice, err error) {
- err = readDb.Model(&models.XtDoctorAdvice{}).Where(" user_org_id = ? AND advice_date = ? and patient_id = ? AND sync_advice_id = ? AND status = 1 ", orgId, advice_date, patient_id, sync_advice_id).First(&advice).Error
- return
- }
-
- // 根据姓名获取patient_id
- func GetSchedulesByPatientID(org_id int64, schedulesTime int64, patient_id int64) (schedules models.DialysisSchedule, err error) {
- err = readDb.Model(&models.DialysisSchedule{}).Where("user_org_id = ? and schedule_date = ? and patient_id = ? and status = 1", org_id, schedulesTime, patient_id).First(&schedules).Error
- return
- }
-
- // 四川乐山友谊医院医嘱同步
- func SyncSclsyyyyyz(orgDb *gorm.DB) (err error) {
- // 第一步:获取上次同步时间
- org_id := int64(10677)
-
- redisClient := RedisClient()
- defer redisClient.Close()
- sync_time_tt, _ := redisClient.Get("sclsyyyy_sync_time").Result()
-
- sync_time, _ := strconv.ParseInt(sync_time_tt, 10, 64)
- if sync_time == 0 {
- sync_time = time.Now().Unix() - 60*60*10
- }
- // 需要把时间戳转换为日期格式
- syncTimeStr := time.Unix(sync_time, 0).Format("2006-01-02 15:04:05")
- new_sync_time := time.Now().Unix() - 60*3
- schedulesDateStr := time.Now().Format("2006-01-02")
- schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
- schedulesTime := schedulesDate.Unix()
- utils.InfoLog("syncTimeStr:%v", syncTimeStr)
- utils.InfoLog("schedulesTime:%v", schedulesTime)
-
- // 切换为当前时间
- redisClient.Set("sclsyyyy_sync_time", new_sync_time, time.Minute*60*24)
- // 第二步: 获取该时间段内有更新的医嘱
- yzList, _ := GetSclsyyyyyz(orgDb, syncTimeStr)
- if len(yzList) > 0 {
- loc, _ := time.LoadLocation("Local") //重要:获取时区
- for _, yz := range yzList {
- if yz.DeliveryWay == "口服" {
- continue
- }
- var dflag int64
- dflag = 0
- utils.InfoLog("AdviceName:%v", yz.AdviceName)
- if strings.Contains(yz.AdviceName, "注射") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "血液") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "低分子肝素") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "左卡") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "促红素") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "奈莫司他") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "葡萄糖") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "吸氧") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "抢救") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "电脑血糖") {
- dflag = 1
- }
-
- if strings.Contains(yz.AdviceName, "血液透析") {
- yz.SingleDose = ""
- yz.SingleDoseUnit = ""
- yz.PrescribingNumber = "1"
- yz.PrescribingNumberUnit = "次"
- }
-
- utils.InfoLog("dflag:%v", dflag)
-
- if dflag == 0 {
- continue
- }
-
- var patientInfo models.Patients
- // 根据身份证号获取患者信息
- if len(yz.HisUserId) > 0 {
- patientInfo, _ = GetPatientsInfoByIDCardNo(yz.HisUserId, org_id)
- if patientInfo.ID <= 0 {
- continue
- }
- } else {
- patientInfo, _ = GetPatientsInfoByName(yz.UserName, org_id)
- if patientInfo.ID <= 0 {
- continue
- }
- }
- // 查询该患者当天是否有排班信息
- theTime, _ := time.ParseInLocation("2006-01-02 15:04", yz.StartTime, loc) //使用模板在对应时区转化为time.time类型
- start_time := theTime.Unix()
-
- schedulesInfo, _ := GetSchedulesByPatientID(org_id, schedulesTime, patientInfo.ID)
- if schedulesInfo.ID > 0 {
- start_time = theTime.Unix()
- } else {
- continue
- }
-
- // 根据同步来的 advice_id 来查询库里是否已经存在
- advice_info, _ := GetAdviceBySyncAdviceIdForScyyyy(org_id, yz.AdviceId, schedulesTime, patientInfo.ID)
- if advice_info.ID > 0 {
- continue
- } else {
- // 根据姓名获取医生ID
- var doctor_id int64
- temp_doctor_id, _ := redisClient.Get("sclsyyyy_doctor_id_" + yz.DoctorId).Result()
- if len(temp_doctor_id) == 0 {
- doctor, _ := GetAdminUserId(org_id, yz.DoctorName)
- doctor_id = doctor.AdminUserId
- if doctor_id > 0 {
- redisClient.Set("sclsyyyy_doctor_id_"+yz.DoctorId, doctor_id, time.Minute*60*2)
- } else {
- continue
- }
- } else {
- doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
- }
-
- // 根据排班班次,固定医嘱时间
- SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
- PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
- advice := models.XtDoctorAdvice{
- UserOrgId: org_id,
- PatientId: patientInfo.ID,
- AdviceType: 2,
- AdviceDate: schedulesTime,
- StartTime: start_time,
- AdviceName: yz.AdviceName,
- AdviceDesc: yz.AdviceDesc,
- RecordDate: schedulesTime,
- SingleDose: SingleDose,
- SingleDoseUnit: yz.SingleDoseUnit,
- PrescribingNumber: PrescribingNumber,
- PrescribingNumberUnit: yz.PrescribingNumberUnit,
- DeliveryWay: yz.DeliveryWay,
- ExecutionFrequency: yz.ExecutionFrequency,
- AdviceDoctor: doctor_id,
- CreatedTime: time.Now().Unix(),
- UpdatedTime: time.Now().Unix(),
- SyncAdviceId: yz.AdviceId,
- ExecutionState: 2,
- Status: 1,
- IsSync: 1,
- }
- utils.InfoLog("advice:%v", advice)
-
- if len(yz.AdviceName) > 0 {
- handleerr := CreateDoctorAdviceHandle(&advice)
- if handleerr != nil {
- utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
- continue
- }
- } else {
- continue
- }
- }
- }
- }
- return
- }
|