123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- package service
-
- import (
- "IC/models"
- "IC/utils"
- _ "encoding/json"
- _ "fmt"
- "github.com/jinzhu/gorm"
- "strings"
- "time"
- )
-
- type jhhjyyLis struct {
- HisUserId string `json:"his_user_id"`
- PatientName string `json:"patient_name"`
- ProjectId string `json:"project_id"`
- ProjectName string `json:"project_name"`
- InspectDate string `json:"inspect_date"`
- ItemId string `json:"item_id"`
- ItemName string `json:"item_name"`
- ItemType string `json:"item_type"`
- ItemValue string `json:"item_value"`
- RangeMin string `json:"range_min"`
- RangeMax string `json:"range_max"`
- RangeValue string `json:"range_value"`
- RangeOptions string `json:"range_options"`
- Unit string `json:"unit"`
- State string `json:"state"`
- CTime string `json:"c_time"`
- UTime string `json:"u_time"`
- }
-
- func (jhhjyyLis) TableName() string {
- return "GetJianYanXX"
- }
-
- func GetJhhjyyLis(rdb *gorm.DB, synctime string, his_user_id string, patient_name string) (record []*jhhjyyLis, err error) {
- err = rdb.Model(&jhhjyyLis{}).Where("c_time >= ? and (his_user_id = ? or patient_name = ?)", synctime, his_user_id, patient_name).Find(&record).Error
- return
- }
-
- //// 根据机构ID和患者身份证号码获取患者信息
- //func GetUserInIdcardNoByOrg(org_id int64) (patients []*models.Patients, err error) {
- // err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 ", org_id).Find(&patients).Error
- // return
- //}
-
- // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
- func GetJhhjyyProjectID(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 1062901, err
- }
- }
- }
- }
-
- // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
- func GetJhhjyyItemID(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 SyncJhhjyyLis() (err error) {
- org_id := int64(10629)
-
- org := &models.DataUploadConfig{
- OrgId: org_id,
- DbHost: "192.168.5.253",
- DbPort: "1433",
- DbPass: "XtUser",
- DbUser: "XtUser",
- DbName: "HealthOne",
- }
-
- orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
- if err != nil {
- utils.ErrorLog("创建数据库连接失败:%v", err)
- return
- }
-
- // 第一步:获取上一次同步的时间点
- syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
- var sync_time int64
- if syncLastInfo.ID > 0 {
- sync_time = syncLastInfo.SyncTime
- } else {
- sync_time = 1704038400 // 2023-01-01
- }
- // 第二步:获取时间内所有检验结果
- sync_time_temp := time.Unix(sync_time, 0)
- startTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
-
- patients, _ := GetUserInIdcardNoByOrg(org_id)
-
- if len(patients) > 0 {
- for _, patient := range patients {
- userCardId := patient.IdCardNo
- // 根据姓名获取患者ID
- jhhyyyLis, _ := GetJhhjyyLis(orgDb, startTimeStr, userCardId, patient.Name)
- if len(jhhyyyLis) > 0 {
- for _, LisInfo := range jhhyyyLis {
- project_id, _ := GetJhhjyyProjectID(org_id, LisInfo.ProjectName, LisInfo.ItemName)
- item_id, _ := GetJhhjyyItemID(org_id, LisInfo.ProjectName, LisInfo.ItemName, project_id)
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
-
- timeLayout := "2006-01-02 15:04"
- loc, _ := time.LoadLocation("Local")
-
- utils.InfoLog("1111111---%v", LisInfo.InspectDate)
- timstr := strings.Split(LisInfo.InspectDate, "T")
- timstem := strings.Split(timstr[1], "Z")
- tempRecord := timstr[0] + " " + timstem[0]
- utils.InfoLog("2222222---%v", tempRecord)
-
- recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc)
- recordTimeInt := recordTime.Unix()
- recordTimeStr := time.Unix(recordTimeInt, 0).Format(timeLayout)
-
- 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, patient.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 = 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 = 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()
- }
- } else {
- continue
- }
- }
-
- var syncInfo models.MiddleSyncInfo
- syncInfo.OrgId = org_id
- syncInfo.SyncTime = time.Now().Unix()
- syncInfo.SyncResultType = 1
- syncInfo.SyncRsultRemark = "同步成功"
- syncInfo.SyncTotalNum = 0
- syncInfo.SyncSuccessNum = 0
- syncInfo.SyncInfo = ""
- syncInfo.CreateTime = time.Now().Unix()
- syncInfo.UpdateTime = time.Now().Unix()
-
- cwderr := CreateSyncInfo(&syncInfo)
- if cwderr != nil {
- utils.ErrorLog("创建同步信息失败:%v", cwderr)
- return
- }
- }
- SyncToJhhjyy()
- return
- }
-
- func SyncToJhhjyy() {
-
- utils.TraceLog("检验检查同步任务开始执行")
- org_id := int64(10629)
-
- // 第一步:跟进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("检验检查同步任务完成")
- }
|