szbl_service.go 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. _ "regexp"
  8. "strconv"
  9. "strings"
  10. "time"
  11. _ "unicode"
  12. )
  13. type SZBLJYJC struct {
  14. FName string `gorm:"column:name" json:"name" form:"name"`
  15. FCompoundItemName string `gorm:"column:DockingApplyItemName" json:"DockingApplyItemName" form:"DockingApplyItemName"`
  16. FReference string `gorm:"column:refrange" json:"refrange" form:"refrange"`
  17. FSingleItemName string `gorm:"column:itemname" json:"itemname" form:"itemname"`
  18. FRecordTime string `gorm:"column:recordTime" json:"recordTime" form:"recordTime"`
  19. FCheckTime string `gorm:"column:repdate" json:"repdate" form:"repdate"`
  20. FResult string `gorm:"column:result" json:"result" form:"result"`
  21. FUnit string `gorm:"column:unit" json:"unit" form:"unit"`
  22. }
  23. func (SZBLJYJC) TableName() string {
  24. return "v_cm_result"
  25. }
  26. // 获取lis返回检验检查结果数据
  27. func GetSZBLResultDataInsertDB(org_id int64) (result []*SZBLJYJC) {
  28. org := &models.DataUploadConfig{
  29. OrgId: 10278,
  30. ProvinceId: 19,
  31. CityId: 291,
  32. DepartmentName: "深圳市百霖血液透析中心",
  33. HospitalId: "MA5DB4KB6",
  34. InstType: 1,
  35. DbHost: "localhost",
  36. DbPort: "1433",
  37. DbPass: "1Q2W3e4r!@#$",
  38. DbUser: "adapter",
  39. DbName: "hyDB",
  40. }
  41. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  42. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  43. if err != nil {
  44. utils.ErrorLog("创建数据库连接失败:%v", err)
  45. return
  46. }
  47. record, _ := GetLastSyncResultRecord(org_id)
  48. fmt.Println(record)
  49. var sync_time int64
  50. if record.ID > 0 {
  51. sync_time = record.Ctime
  52. } else {
  53. sync_time = 1627747200
  54. }
  55. sync_time_temp := time.Unix(sync_time, 0)
  56. syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  57. list, err := GetSZBLResultRecord(orgDb, syncTimeStr)
  58. if err == nil {
  59. // fmt.Println(list)
  60. //插入到系统检验检查数据
  61. for _, item := range list {
  62. printInfo, _ := GetPatientsInfoByName(item.FName, 10278)
  63. if printInfo.ID == 0 {
  64. fmt.Println(item)
  65. fmt.Println(printInfo)
  66. fmt.Println("暂无该患者")
  67. continue
  68. }
  69. project_id := int64(0)
  70. project_id, _ = GetSzbljhProjectID(org_id, item.FCompoundItemName)
  71. item_id, _ := GetSzblItemID(org_id, item.FCompoundItemName, item.FSingleItemName, project_id)
  72. tx := writeMiddleDb.Begin()
  73. var inspection models.MiddleInspection
  74. var inspection_reference models.MiddleInspectionReference
  75. recordDateStr := ""
  76. //inspect_date := item.FRecordTime.Format("2006-01-02 15:04")
  77. timstr := strings.Split(item.FCheckTime, "T")
  78. timstem := strings.Split(timstr[1], "Z")
  79. recordDateStr = timstr[0] + " " + timstem[0]
  80. // recordDateStr = item.FCheckTime
  81. fmt.Println(recordDateStr)
  82. date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  83. fmt.Println(date)
  84. record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  85. fmt.Println(record_date)
  86. // ftimstr := strings.Split(item.FCheckTime, "T")
  87. // ftimstem := strings.Split(timstr[1], "Z")
  88. // fdate := ftimstr[0] + " " + ftimstem[0]
  89. fdate := recordDateStr
  90. var total int
  91. var RangeOptions string
  92. var RangeMin string
  93. var RangeMax string
  94. // 判断检查类型
  95. var ItemType int
  96. if IsChineseChar(item.FReference) || strings.Contains(item.FReference, "|") || strings.Contains(item.FReference, "≤") {
  97. ItemType = 2
  98. } else {
  99. if strings.Contains(item.FReference, "-") {
  100. ItemType = 1
  101. } else {
  102. ItemType = 2
  103. }
  104. }
  105. if ItemType == 1 {
  106. Range := strings.Split(item.FReference, "-")
  107. RangeMin = Range[0]
  108. RangeMax = Range[1]
  109. } else {
  110. RangeOptions = item.FReference
  111. }
  112. 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
  113. if inspection_reference.ID > 0 {
  114. ItemType = inspection_reference.RangeType
  115. }
  116. if total <= 0 {
  117. inspection_reference.OrgId = org_id
  118. inspection_reference.ProjectName = item.FCompoundItemName
  119. inspection_reference.Project = item.FCompoundItemName
  120. inspection_reference.ProjectId = project_id
  121. inspection_reference.ItemName = item.FSingleItemName
  122. inspection_reference.ItemNameAddition = ""
  123. inspection_reference.ItemId = item_id
  124. inspection_reference.RangeType = ItemType
  125. inspection_reference.RangeMin = RangeMin
  126. inspection_reference.RangeMax = RangeMax
  127. inspection_reference.RangeOptions = RangeOptions
  128. inspection_reference.Unit = item.FUnit
  129. inspection_reference.Status = 1
  130. inspection_reference.CreatedTime = time.Now().Unix()
  131. inspection_reference.UpdatedTime = time.Now().Unix()
  132. inspection_reference.InspectDate = fdate
  133. inspection_reference.UTime = fdate
  134. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  135. if err != nil {
  136. tx.Rollback()
  137. }
  138. }
  139. var itotal int
  140. 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
  141. if itotal <= 0 {
  142. inspection.PatientId = printInfo.ID
  143. inspection.OrgId = org_id
  144. inspection.ProjectId = project_id
  145. inspection.ItemName = inspection_reference.ItemName
  146. inspection.ProjectName = inspection_reference.ProjectName
  147. inspection.InspectType = ItemType
  148. inspection.ItemId = item_id
  149. inspection.InspectValue = item.FResult
  150. inspection.InspectDate = item.FCheckTime
  151. inspection.RecordDate = record_date.Unix()
  152. inspection.Status = 1
  153. inspection.CreatedTime = time.Now().Unix()
  154. inspection.UpdatedTime = time.Now().Unix()
  155. inspection.UTime = item.FCheckTime
  156. inspection.HisUserId = strconv.FormatInt(printInfo.ID, 10)
  157. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  158. if err != nil {
  159. tx.Rollback()
  160. }
  161. }
  162. tx.Commit()
  163. }
  164. //插入一条查询检验检查结果记录状态数据
  165. var info LisSyncResultStatusInfo
  166. info.Ctime = time.Now().Unix()
  167. info.Status = 1
  168. info.Mtime = time.Now().Unix()
  169. info.OrgId = org_id
  170. info.IsResult = 1
  171. info.LastId = int64(len(list))
  172. info.ResultDate = time.Now().Unix()
  173. writeMiddleDb.Save(&info)
  174. }
  175. // 第一步:跟进org_id 去中间库查出需要同步的数据
  176. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  177. inspections, _ := GetSyncInspectionByOrgId(org_id)
  178. // 第二步:将数据同步到业务库
  179. if len(inspection_references) > 0 {
  180. for _, inspection_reference := range inspection_references {
  181. SyncInspectionReference(&inspection_reference)
  182. }
  183. }
  184. if len(inspections) > 0 {
  185. for _, inspection := range inspections {
  186. SyncInspection(&inspection)
  187. }
  188. }
  189. }
  190. return
  191. }
  192. func GetSZBLResultRecord(rdb *gorm.DB, synctime string) (record []*SZBLJYJC, err error) {
  193. err = rdb.Model(&SZBLJYJC{}).Where("repdate >= ?", synctime).Find(&record).Error
  194. return
  195. }
  196. //判断是否包含中文
  197. // func IsChineseChar(str string) bool {
  198. // for _, r := range str {
  199. // if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
  200. // return true
  201. // }
  202. // }
  203. // return false
  204. // }
  205. // func GetResultRecordByTime(id int64) (record []*KMResult, err error) {
  206. // err = readDb.Model(&KMResult{}).Where("barcode > ? AND repdate >= 2021-08-12 00:00:00", id).Order("barcode asc").Find(&record).Error
  207. // return
  208. // }