sh_service.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "unicode"
  11. )
  12. type KMResult struct {
  13. FRequestCode string `gorm:"column:F_RequestCode" json:"F_RequestCode" form:"F_RequestCode"`
  14. FHospSampleID string `gorm:"column:F_HospSampleID" json:"F_HospSampleID" form:"F_HospSampleID"`
  15. FHospSampleNumber string `gorm:"column:F_HospSampleNumber" json:"F_HospSampleNumber" form:"F_HospSampleNumber"`
  16. FInputDate string `gorm:"column:F_InputDate" json:"F_InputDate" form:"F_InputDate"`
  17. FSamplingDate string `gorm:"column:F_SamplingDate" json:"F_SamplingDate" form:"F_SamplingDate"`
  18. FName string `gorm:"column:F_Name" json:"F_Name" form:"F_Name"`
  19. FSex string `gorm:"column:F_Sex" json:"F_Sex" form:"F_Sex"`
  20. FAge string `gorm:"column:F_Age" json:"F_Age" form:"F_Age"`
  21. FAgeUnit string `gorm:"column:F_AgeUnit" json:"F_AgeUnit" form:"F_AgeUnit"`
  22. FPatientNumber string `gorm:"column:F_PatientNumber" json:"F_PatientNumber" form:"F_PatientNumber"`
  23. FItemType string `gorm:"column:F_ItemType" json:"F_ItemType" form:"F_ItemType"`
  24. FCompoundItem string `gorm:"column:F_CompoundItem" json:"F_CompoundItem" form:"F_CompoundItem"`
  25. FCompoundItemName string `gorm:"column:F_CompoundItemName" json:"F_CompoundItemName" form:"F_CompoundItemName"`
  26. FNaturalItem string `gorm:"column:F_NaturalItem" json:"F_NaturalItem" form:"F_NaturalItem"`
  27. FNaturalItemName string `gorm:"column:F_NaturalItemName" json:"F_NaturalItemName" form:"F_NaturalItemName"`
  28. FSingleItem string `gorm:"column:F_SingleItem" json:"F_SingleItem" form:"F_SingleItem"`
  29. FSingleItemName string `gorm:"column:F_SingleItemName" json:"F_SingleItemName" form:"F_SingleItemName"`
  30. FHospNaturalItem string `gorm:"column:F_HospNaturalItem" json:"F_HospNaturalItem" form:"F_HospNaturalItem"`
  31. FHospNaturalItemName string `gorm:"column:F_HospNaturalItemName" json:"F_HospNaturalItemName" form:"F_HospNaturalItemName"`
  32. FHospItem string `gorm:"column:F_HospItem" json:"F_HospItem" form:"F_HospItem"`
  33. FHospItemName string `gorm:"column:F_HospItemName" json:"F_HospItemName" form:"F_HospItemName"`
  34. FTestNO string `gorm:"column:F_TestNO" json:"F_TestNO" form:"F_TestNO"`
  35. FResult string `gorm:"column:F_Result" json:"F_Result" form:"F_Result"`
  36. FUnit string `gorm:"column:F_Unit" json:"F_Unit" form:"F_Unit"`
  37. FHint string `gorm:"column:F_Hint" json:"F_Hint" form:"F_Hint"`
  38. FSuggestion string `gorm:"column:F_Suggestion" json:"F_Suggestion" form:"F_Suggestion"`
  39. FReference string `gorm:"column:F_Reference" json:"F_Reference" form:"F_Reference"`
  40. FReference2 string `gorm:"column:F_Reference2" json:"F_Reference2" form:"F_Reference2"`
  41. FDepartmentName string `gorm:"column:F_DepartmentName" json:"F_DepartmentName" form:"F_DepartmentName"`
  42. FRecorderName string `gorm:"column: F_RecorderName" json:"F_RecorderName" form:"F_RecorderName"`
  43. FRecordTime string `gorm:"column:F_RecordTime" json:"F_RecordTime" form:"F_RecordTime"`
  44. FCheckerName string `gorm:"column:F_CheckerName" json:"F_CheckerName" form:"F_CheckerName"`
  45. FCheckTime string `gorm:"column:F_CheckTime" json:"F_CheckTime" form:"F_CheckTime"`
  46. FAuthorizeName string `gorm:"column:F_AuthorizeName" json:"F_AuthorizeName" form:"F_AuthorizeName"`
  47. FAuthorizeTime string `gorm:"column:F_AuthorizeTime" json:"F_AuthorizeTime" form:"F_AuthorizeTime"`
  48. FSubResults string `gorm:"column:F_SubResults" json:"F_SubResults" form:"F_SubResults"`
  49. FPathologyGrossingDesc string `gorm:"column:F_PathologyGrossingDesc" json:"F_PathologyGrossingDesc" form:"F_PathologyGrossingDesc"`
  50. FReportBarCode string `gorm:"column:F_ReportBarCode" json:"F_ReportBarCode" form:"F_ReportBarCode"`
  51. FReceiveTime string `gorm:"column:F_ReceiveTime" json:"F_ReceiveTime" form:"F_ReceiveTime"`
  52. FStatus string `gorm:"column:F_Status" json:"F_Status" form:"F_Status"`
  53. FRemark string `gorm:"column:F_Remark" json:"F_Remark" form:"F_Remark"`
  54. FTestMethodName string `gorm:"column:F_TestMethodName" json:"F_TestMethodName" form:"F_TestMethodName"`
  55. FMachineName string `gorm:"column:F_MachineName" json:"F_MachineName" form:"F_MachineName"`
  56. FItemDesc string `gorm:"column:F_ItemDesc" json:"F_ItemDesc" form:"F_ItemDesc"`
  57. FIsDelayed string `gorm:"column:F_IsDelayed" json:"F_IsDelayed" form:"F_IsDelayed"`
  58. FDelayeddesc string `gorm:"column:F_Delayeddesc" json:"F_Delayeddesc" form:"F_Delayeddesc"`
  59. FIsReimbu string `gorm:"column:F_IsReimbu" json:"F_IsReimbu" form:"F_IsReimbu"`
  60. FReimbuDesc string `gorm:"column:F_ReimbuDesc" json:"F_ReimbuDesc" form:"F_ReimbuDesc"`
  61. FDetailItemID string `gorm:"column:F_DetailItemID" json:"F_DetailItemID" form:"F_DetailItemID"`
  62. FIDCardNo string `gorm:"column:F_IDCardNo" json:"F_IDCardNo" form:"F_IDCardNo"`
  63. }
  64. func (KMResult) TableName() string {
  65. return "v_km_lis_result"
  66. }
  67. //获取lis返回检验检查结果数据
  68. func GetSHResultDataInsertDB(org_id int64) (result []*KMResult) {
  69. record, _ := GetLastSyncResultRecord(org_id)
  70. fmt.Println(record)
  71. if record.ID == 0 {
  72. list, err := GetSHResultRecord()
  73. if err == nil {
  74. fmt.Println(list)
  75. //插入到系统检验检查数据
  76. for _, item := range list {
  77. project_id := int64(0)
  78. if len(item.FIDCardNo) > 0 {
  79. project_id, _ = GetShjhProjectID(org_id, item.FCompoundItemName)
  80. } else {
  81. continue
  82. }
  83. printInfo, _ := GetPatientsInfoByIDCardNo(item.FIDCardNo, 10215)
  84. if printInfo.ID == 0 {
  85. fmt.Println(item)
  86. fmt.Println(printInfo)
  87. fmt.Println("暂无该患者")
  88. continue
  89. }
  90. item_id, _ := GetShItemID(org_id, item.FCompoundItemName, item.FSingleItemName, project_id)
  91. tx := writeMiddleDb.Begin()
  92. var inspection models.MiddleInspection
  93. var inspection_reference models.MiddleInspectionReference
  94. recordDateStr := ""
  95. //inspect_date := item.FRecordTime.Format("2006-01-02 15:04")
  96. recordDateStr = item.FRecordTime
  97. date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  98. record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  99. var total int
  100. var RangeOptions string
  101. var RangeMin string
  102. var RangeMax string
  103. // 判断检查类型
  104. var ItemType int
  105. if IsChineseChar(item.FReference) || strings.Contains(item.FReference, "|") || strings.Contains(item.FReference, "≤") {
  106. ItemType = 2
  107. } else {
  108. if strings.Contains(item.FReference, "-") {
  109. ItemType = 1
  110. } else {
  111. ItemType = 2
  112. }
  113. }
  114. if ItemType == 1 {
  115. Range := strings.Split(item.FReference, "-")
  116. RangeMin = Range[0]
  117. RangeMax = Range[1]
  118. } else {
  119. RangeOptions = item.FReference
  120. }
  121. 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
  122. if inspection_reference.ID > 0 {
  123. ItemType = inspection_reference.RangeType
  124. }
  125. if total <= 0 {
  126. inspection_reference.OrgId = org_id
  127. inspection_reference.ProjectName = item.FCompoundItemName
  128. inspection_reference.Project = item.FCompoundItemName
  129. inspection_reference.ProjectId = project_id
  130. inspection_reference.ItemName = item.FSingleItemName
  131. inspection_reference.ItemNameAddition = ""
  132. inspection_reference.ItemId = item_id
  133. inspection_reference.RangeType = ItemType
  134. inspection_reference.RangeMin = RangeMin
  135. inspection_reference.RangeMax = RangeMax
  136. inspection_reference.RangeOptions = RangeOptions
  137. inspection_reference.Unit = item.FUnit
  138. inspection_reference.Status = 1
  139. inspection_reference.CreatedTime = time.Now().Unix()
  140. inspection_reference.UpdatedTime = time.Now().Unix()
  141. inspection_reference.InspectDate = item.FCheckTime
  142. inspection_reference.UTime = item.FCheckTime
  143. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  144. if err != nil {
  145. tx.Rollback()
  146. }
  147. }
  148. var itotal int
  149. 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
  150. if itotal <= 0 {
  151. inspection.PatientId = printInfo.ID
  152. inspection.OrgId = org_id
  153. inspection.ProjectId = project_id
  154. inspection.ItemName = inspection_reference.ItemName
  155. inspection.ProjectName = inspection_reference.ProjectName
  156. inspection.InspectType = ItemType
  157. inspection.ItemId = item_id
  158. inspection.InspectValue = item.FResult
  159. inspection.InspectDate = item.FCheckTime
  160. inspection.RecordDate = record_date.Unix()
  161. inspection.Status = 1
  162. inspection.CreatedTime = time.Now().Unix()
  163. inspection.UpdatedTime = time.Now().Unix()
  164. inspection.UTime = item.FCheckTime
  165. inspection.HisUserId = strconv.FormatInt(printInfo.ID, 10)
  166. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  167. if err != nil {
  168. tx.Rollback()
  169. }
  170. }
  171. tx.Commit()
  172. }
  173. //插入一条查询检验检查结果记录状态数据
  174. var info LisSyncResultStatusInfo
  175. info.Ctime = time.Now().Unix()
  176. info.Status = 1
  177. info.Mtime = time.Now().Unix()
  178. info.OrgId = org_id
  179. info.IsResult = 1
  180. info.LastId = int64(len(list))
  181. info.ResultDate = time.Now().Unix()
  182. writeMiddleDb.Save(&info)
  183. }
  184. }
  185. //else
  186. //{
  187. // if record.LastId != 0 {
  188. // list, err := GetResultRecordByID(record.LastId)
  189. // fmt.Println(list)
  190. // //插入中间库中
  191. // //插入一条插入中间库记录数据
  192. // if err == nil {
  193. //
  194. // //插入到系统检验检查数据
  195. // for _, item := range list {
  196. // project_id := int64(0)
  197. // if len(item.Barcode) > 0 {
  198. // project_id, _ = GetBljhProjectID(org_id, item.Groupname)
  199. // } else {
  200. // continue
  201. // }
  202. // printInfo, _ := GetHisLabelPrintInfoById(item.Barcode)
  203. // item_id, _ := GetBlItemID(org_id, item.Groupname, item.Itemname, project_id)
  204. //
  205. // tx := writeMiddleDb.Begin()
  206. // var inspection models.MiddleInspection
  207. // var inspection_reference models.MiddleInspectionReference
  208. //
  209. // recordDateStr := ""
  210. // inspect_date := item.Repdate.Format("2006-01-02 15:04")
  211. // if item.Repdate.Unix() == 0 {
  212. // recordDateStr = time.Now().Format("2006-01-02 15:04")
  213. // } else {
  214. //
  215. // recordDateStr = inspect_date
  216. // }
  217. //
  218. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  219. // record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  220. // var total int
  221. // var RangeOptions string
  222. // var RangeMin string
  223. // var RangeMax string
  224. // // 判断检查类型
  225. // var ItemType int
  226. // if strings.Contains(item.Refrange, "-") {
  227. // ItemType = 1
  228. //
  229. // } else {
  230. // ItemType = 2
  231. //
  232. // }
  233. //
  234. // if ItemType == 1 {
  235. // Range := strings.Split(item.Refrange, "-")
  236. // RangeMin = Range[0]
  237. // RangeMax = Range[1]
  238. // } else {
  239. // RangeOptions = item.Refrange
  240. // }
  241. //
  242. // 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
  243. // if inspection_reference.ID > 0 {
  244. // ItemType = inspection_reference.RangeType
  245. // }
  246. // if total <= 0 {
  247. // inspection_reference.OrgId = org_id
  248. // inspection_reference.ProjectName = item.Groupname
  249. // inspection_reference.Project = item.Groupname
  250. // inspection_reference.ProjectId = project_id
  251. // inspection_reference.ItemName = item.Itemname
  252. // inspection_reference.ItemNameAddition = item.Barcode
  253. // inspection_reference.ItemId = item_id
  254. // inspection_reference.RangeType = ItemType
  255. // inspection_reference.RangeMin = RangeMin
  256. // inspection_reference.RangeMax = RangeMax
  257. // inspection_reference.RangeOptions = RangeOptions
  258. // inspection_reference.Unit = item.Unit
  259. // inspection_reference.Status = 1
  260. // inspection_reference.CreatedTime = time.Now().Unix()
  261. // inspection_reference.UpdatedTime = time.Now().Unix()
  262. // inspection_reference.InspectDate = inspect_date
  263. // inspection_reference.UTime = inspect_date
  264. // err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  265. // if err != nil {
  266. // tx.Rollback()
  267. // }
  268. // }
  269. //
  270. // var itotal int
  271. // 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
  272. // if itotal <= 0 {
  273. // inspection.PatientId = printInfo.PatientId
  274. // inspection.OrgId = org_id
  275. // inspection.ProjectId = project_id
  276. // inspection.ItemName = inspection_reference.ItemName
  277. // inspection.ProjectName = inspection_reference.ProjectName
  278. // inspection.InspectType = ItemType
  279. // inspection.ItemId = item_id
  280. // inspection.InspectValue = item.Result
  281. // inspection.InspectDate = inspect_date
  282. // inspection.RecordDate = record_date.Unix()
  283. // inspection.Status = 1
  284. // inspection.CreatedTime = time.Now().Unix()
  285. // inspection.UpdatedTime = time.Now().Unix()
  286. // inspection.UTime = inspect_date
  287. // inspection.HisUserId = strconv.FormatInt(printInfo.PatientId, 10)
  288. // err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  289. // if err != nil {
  290. // tx.Rollback()
  291. // }
  292. // }
  293. //
  294. // tx.Commit()
  295. //
  296. // }
  297. // //插入一条查询检验检查结果记录状态数据
  298. // var info LisSyncResultStatusInfo
  299. // info.Ctime = time.Now().Unix()
  300. // info.Status = 1
  301. // info.Mtime = time.Now().Unix()
  302. // info.OrgId = org_id
  303. // info.IsResult = 1
  304. // code, _ := strconv.ParseInt(list[0].Barcode, 10, 64)
  305. // info.LastId = code
  306. // info.ResultDate = time.Now().Unix()
  307. // writeMiddleDb.Save(&info)
  308. //
  309. // }
  310. //
  311. // }
  312. //
  313. //}
  314. //
  315. // 第一步:跟进org_id 去中间库查出需要同步的数据
  316. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  317. inspections, _ := GetSyncInspectionByOrgId(org_id)
  318. // 第二步:将数据同步到业务库
  319. if len(inspection_references) > 0 {
  320. for _, inspection_reference := range inspection_references {
  321. SyncInspectionReference(&inspection_reference)
  322. }
  323. }
  324. if len(inspections) > 0 {
  325. for _, inspection := range inspections {
  326. SyncInspection(&inspection)
  327. }
  328. }
  329. return
  330. }
  331. func GetSHResultRecord() (record []*KMResult, err error) {
  332. err = dataBase.Model(&KMResult{}).Order("barcode asc").Find(&record).Error
  333. return
  334. }
  335. //判断是否包含中文
  336. func IsChineseChar(str string) bool {
  337. for _, r := range str {
  338. if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
  339. return true
  340. }
  341. }
  342. return false
  343. }
  344. func GetResultRecordByTime(id int64) (record []*KMResult, err error) {
  345. err = readDb.Model(&KMResult{}).Where("barcode > ? AND repdate >= 2021-08-12 00:00:00", id).Order("barcode asc").Find(&record).Error
  346. return
  347. }