123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package service
-
- import (
- "IC/models"
- "IC/utils"
- "fmt"
- _"regexp"
- "strconv"
- "strings"
- "time"
- _"unicode"
- "github.com/jinzhu/gorm"
- )
-
-
-
- type SZBLJYJC struct {
- FName string `gorm:"column:name" json:"name" form:"name"`
- FCompoundItemName string `gorm:"column:DockingApplyItemName" json:"DockingApplyItemName" form:"DockingApplyItemName"`
- FReference string `gorm:"column:refrange" json:"refrange" form:"refrange"`
- FSingleItemName string `gorm:"column:itemname" json:"itemname" form:"itemname"`
- FRecordTime string `gorm:"column:recordTime" json:"recordTime" form:"recordTime"`
- FCheckTime string `gorm:"column:repdate" json:"repdate" form:"repdate"`
- FResult string `gorm:"column:result" json:"result" form:"result"`
- FUnit string `gorm:"column:unit" json:"unit" form:"unit"`
- }
-
- func (SZBLJYJC) TableName() string {
- return "v_cm_result"
- }
-
- //获取lis返回检验检查结果数据
- func GetSZBLResultDataInsertDB(org_id int64) (result []*SZBLJYJC) {
-
- org := &models.DataUploadConfig{
- OrgId: 10138,
- ProvinceId: 19,
- CityId: 291,
- DepartmentName: "深圳市百霖血液透析中心",
- HospitalId: "MA5DB4KB6",
- InstType: 1,
- DbHost: "localhost",
- DbPort: "1433",
- DbPass: "1Q2W3e4r!@#$",
- DbUser: "adapter",
- DbName: "hyDB",
- }
-
- if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
- orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
- if err != nil {
- utils.ErrorLog("创建数据库连接失败:%v", err)
- return
- }
-
- record, _ := GetLastSyncResultRecord(org_id)
- fmt.Println(record)
- var sync_time int64
- if record.ID > 0 {
- sync_time = record.Ctime
- } else {
- sync_time = 1627747200
- }
-
- sync_time_temp := time.Unix(sync_time,0)
- syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
-
- list, err := GetSZBLResultRecord(orgDb,syncTimeStr)
- if err == nil {
- // fmt.Println(list)
-
- //插入到系统检验检查数据
- for _, item := range list {
- printInfo, _ := GetPatientsInfoByIDCardNo(item.FName, 10138)
-
- if printInfo.ID == 0 {
- fmt.Println(item)
- fmt.Println(printInfo)
- fmt.Println("暂无该患者")
- continue
- }
-
- project_id := int64(0)
-
- project_id, _ = GetSzbljhProjectID(org_id, item.FCompoundItemName)
-
-
-
- item_id, _ := GetSzblItemID(org_id, item.FCompoundItemName, item.FSingleItemName, project_id)
-
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
-
- recordDateStr := ""
- //inspect_date := item.FRecordTime.Format("2006-01-02 15:04")
-
- timstr := strings.Split(item.FCheckTime, "T")
- timstem := strings.Split(timstr[1], "Z")
-
- recordDateStr = timstr[0] + " " + timstem[0]
- // recordDateStr = item.FCheckTime
- fmt.Println(recordDateStr)
- date ,_ := utils.ParseTimeStringToTime("2006-01-02 15:04:05",recordDateStr)
- fmt.Println(date)
- record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
- fmt.Println(record_date)
-
- // ftimstr := strings.Split(item.FCheckTime, "T")
- // ftimstem := strings.Split(timstr[1], "Z")
-
- // fdate := ftimstr[0] + " " + ftimstem[0]
- fdate := recordDateStr
-
- var total int
- var RangeOptions string
- var RangeMin string
- var RangeMax string
- // 判断检查类型
- var ItemType int
- if IsChineseChar(item.FReference) || strings.Contains(item.FReference, "|") || strings.Contains(item.FReference, "≤") {
- ItemType = 2
- } else {
- if strings.Contains(item.FReference, "-") {
- ItemType = 1
- } else {
- ItemType = 2
- }
- }
-
- if ItemType == 1 {
- Range := strings.Split(item.FReference, "-")
- RangeMin = Range[0]
- RangeMax = Range[1]
- } else {
- RangeOptions = item.FReference
- }
-
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error
- if inspection_reference.ID > 0 {
- ItemType = inspection_reference.RangeType
- }
- if total <= 0 {
- inspection_reference.OrgId = org_id
- inspection_reference.ProjectName = item.FCompoundItemName
- inspection_reference.Project = item.FCompoundItemName
- inspection_reference.ProjectId = project_id
- inspection_reference.ItemName = item.FSingleItemName
- inspection_reference.ItemNameAddition = ""
- inspection_reference.ItemId = item_id
- inspection_reference.RangeType = ItemType
- inspection_reference.RangeMin = RangeMin
- inspection_reference.RangeMax = RangeMax
- inspection_reference.RangeOptions = RangeOptions
- inspection_reference.Unit = item.FUnit
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
- inspection_reference.InspectDate = fdate
- inspection_reference.UTime = fdate
- err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- var itotal int
- err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, record_date.Unix(), printInfo.ID).Find(&inspection).Count(&itotal).Error
- if itotal <= 0 {
- inspection.PatientId = printInfo.ID
- inspection.OrgId = org_id
- inspection.ProjectId = project_id
- inspection.ItemName = inspection_reference.ItemName
- inspection.ProjectName = inspection_reference.ProjectName
- inspection.InspectType = ItemType
- inspection.ItemId = item_id
- inspection.InspectValue = item.FResult
- inspection.InspectDate = item.FCheckTime
- inspection.RecordDate = record_date.Unix()
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- inspection.UTime = item.FCheckTime
- inspection.HisUserId = strconv.FormatInt(printInfo.ID, 10)
- err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- tx.Commit()
-
- }
- //插入一条查询检验检查结果记录状态数据
- var info LisSyncResultStatusInfo
- info.Ctime = time.Now().Unix()
- info.Status = 1
- info.Mtime = time.Now().Unix()
- info.OrgId = org_id
- info.IsResult = 1
- info.LastId = int64(len(list))
- info.ResultDate = time.Now().Unix()
- writeMiddleDb.Save(&info)
- }
-
- // 第一步:跟进org_id 去中间库查出需要同步的数据
- inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
- inspections, _ := GetSyncInspectionByOrgId(org_id)
-
- // 第二步:将数据同步到业务库
- if len(inspection_references) > 0 {
- for _, inspection_reference := range inspection_references {
- SyncInspectionReference(&inspection_reference)
- }
- }
-
- if len(inspections) > 0 {
- for _, inspection := range inspections {
- SyncInspection(&inspection)
- }
- }
- }
- return
-
- }
-
- func GetSZBLResultRecord(rdb *gorm.DB,synctime string) (record []*SZBLJYJC, err error) {
- err = rdb.Model(&SZBLJYJC{}).Where("repdate >= ?",synctime).Find(&record).Error
- return
- }
-
- //判断是否包含中文
- // func IsChineseChar(str string) bool {
- // for _, r := range str {
- // if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
- // return true
- // }
- // }
- // return false
- // }
-
- // func GetResultRecordByTime(id int64) (record []*KMResult, err error) {
- // err = readDb.Model(&KMResult{}).Where("barcode > ? AND repdate >= 2021-08-12 00:00:00", id).Order("barcode asc").Find(&record).Error
- // return
- // }
|