sh_service.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 KMResult struct {
  14. FRequestCode string `gorm:"column:F_RequestCode" json:"F_RequestCode" form:"F_RequestCode"`
  15. FHospSampleID string `gorm:"column:F_HospSampleID" json:"F_HospSampleID" form:"F_HospSampleID"`
  16. FHospSampleNumber string `gorm:"column:F_HospSampleNumber" json:"F_HospSampleNumber" form:"F_HospSampleNumber"`
  17. FInputDate string `gorm:"column:F_InputDate" json:"F_InputDate" form:"F_InputDate"`
  18. FSamplingDate string `gorm:"column:F_SamplingDate" json:"F_SamplingDate" form:"F_SamplingDate"`
  19. FName string `gorm:"column:F_Name" json:"F_Name" form:"F_Name"`
  20. FSex string `gorm:"column:F_Sex" json:"F_Sex" form:"F_Sex"`
  21. FAge string `gorm:"column:F_Age" json:"F_Age" form:"F_Age"`
  22. FAgeUnit string `gorm:"column:F_AgeUnit" json:"F_AgeUnit" form:"F_AgeUnit"`
  23. FPatientNumber string `gorm:"column:F_PatientNumber" json:"F_PatientNumber" form:"F_PatientNumber"`
  24. FItemType string `gorm:"column:F_ItemType" json:"F_ItemType" form:"F_ItemType"`
  25. FCompoundItem string `gorm:"column:F_CompoundItem" json:"F_CompoundItem" form:"F_CompoundItem"`
  26. FCompoundItemName string `gorm:"column:F_CompoundItemName" json:"F_CompoundItemName" form:"F_CompoundItemName"`
  27. FNaturalItem string `gorm:"column:F_NaturalItem" json:"F_NaturalItem" form:"F_NaturalItem"`
  28. FNaturalItemName string `gorm:"column:F_NaturalItemName" json:"F_NaturalItemName" form:"F_NaturalItemName"`
  29. FSingleItem string `gorm:"column:F_SingleItem" json:"F_SingleItem" form:"F_SingleItem"`
  30. FSingleItemName string `gorm:"column:F_SingleItemName" json:"F_SingleItemName" form:"F_SingleItemName"`
  31. FHospNaturalItem string `gorm:"column:F_HospNaturalItem" json:"F_HospNaturalItem" form:"F_HospNaturalItem"`
  32. FHospNaturalItemName string `gorm:"column:F_HospNaturalItemName" json:"F_HospNaturalItemName" form:"F_HospNaturalItemName"`
  33. FHospItem string `gorm:"column:F_HospItem" json:"F_HospItem" form:"F_HospItem"`
  34. FHospItemName string `gorm:"column:F_HospItemName" json:"F_HospItemName" form:"F_HospItemName"`
  35. FTestNO string `gorm:"column:F_TestNO" json:"F_TestNO" form:"F_TestNO"`
  36. FResult string `gorm:"column:F_Result" json:"F_Result" form:"F_Result"`
  37. FUnit string `gorm:"column:F_Unit" json:"F_Unit" form:"F_Unit"`
  38. FHint string `gorm:"column:F_Hint" json:"F_Hint" form:"F_Hint"`
  39. FSuggestion string `gorm:"column:F_Suggestion" json:"F_Suggestion" form:"F_Suggestion"`
  40. FReference string `gorm:"column:F_Reference" json:"F_Reference" form:"F_Reference"`
  41. FReference2 string `gorm:"column:F_Reference2" json:"F_Reference2" form:"F_Reference2"`
  42. FDepartmentName string `gorm:"column:F_DepartmentName" json:"F_DepartmentName" form:"F_DepartmentName"`
  43. FRecorderName string `gorm:"column: F_RecorderName" json:"F_RecorderName" form:"F_RecorderName"`
  44. FRecordTime string `gorm:"column:F_RecordTime" json:"F_RecordTime" form:"F_RecordTime"`
  45. FCheckerName string `gorm:"column:F_CheckerName" json:"F_CheckerName" form:"F_CheckerName"`
  46. FCheckTime string `gorm:"column:F_CheckTime" json:"F_CheckTime" form:"F_CheckTime"`
  47. FAuthorizeName string `gorm:"column:F_AuthorizeName" json:"F_AuthorizeName" form:"F_AuthorizeName"`
  48. FAuthorizeTime string `gorm:"column:F_AuthorizeTime" json:"F_AuthorizeTime" form:"F_AuthorizeTime"`
  49. FSubResults string `gorm:"column:F_SubResults" json:"F_SubResults" form:"F_SubResults"`
  50. FPathologyGrossingDesc string `gorm:"column:F_PathologyGrossingDesc" json:"F_PathologyGrossingDesc" form:"F_PathologyGrossingDesc"`
  51. FReportBarCode string `gorm:"column:F_ReportBarCode" json:"F_ReportBarCode" form:"F_ReportBarCode"`
  52. FReceiveTime string `gorm:"column:F_ReceiveTime" json:"F_ReceiveTime" form:"F_ReceiveTime"`
  53. FStatus string `gorm:"column:F_Status" json:"F_Status" form:"F_Status"`
  54. FRemark string `gorm:"column:F_Remark" json:"F_Remark" form:"F_Remark"`
  55. FTestMethodName string `gorm:"column:F_TestMethodName" json:"F_TestMethodName" form:"F_TestMethodName"`
  56. FMachineName string `gorm:"column:F_MachineName" json:"F_MachineName" form:"F_MachineName"`
  57. FItemDesc string `gorm:"column:F_ItemDesc" json:"F_ItemDesc" form:"F_ItemDesc"`
  58. FIsDelayed string `gorm:"column:F_IsDelayed" json:"F_IsDelayed" form:"F_IsDelayed"`
  59. FDelayeddesc string `gorm:"column:F_Delayeddesc" json:"F_Delayeddesc" form:"F_Delayeddesc"`
  60. FIsReimbu string `gorm:"column:F_IsReimbu" json:"F_IsReimbu" form:"F_IsReimbu"`
  61. FReimbuDesc string `gorm:"column:F_ReimbuDesc" json:"F_ReimbuDesc" form:"F_ReimbuDesc"`
  62. FDetailItemID string `gorm:"column:F_DetailItemID" json:"F_DetailItemID" form:"F_DetailItemID"`
  63. FIDCardNo string `gorm:"column:F_IDCardNo" json:"F_IDCardNo" form:"F_IDCardNo"`
  64. }
  65. func (KMResult) TableName() string {
  66. return "v_km_lis_result"
  67. }
  68. type KMSheet struct {
  69. FName string `gorm:"column:F_Name" json:"F_Name" form:"F_Name"`
  70. FCompoundItemName string `gorm:"column:F_CompoundItemName" json:"F_CompoundItemName" form:"F_CompoundItemName"`
  71. FReference string `gorm:"column:F_Reference" json:"F_Reference" form:"F_Reference"`
  72. FSingleItemName string `gorm:"column:F_SingleItemName" json:"F_SingleItemName" form:"F_SingleItemName"`
  73. FRecordTime string `gorm:"column:F_RecordTime" json:"F_RecordTime" form:"F_RecordTime"`
  74. FCheckTime string `gorm:"column:F_CheckTime" json:"F_CheckTime" form:"F_CheckTime"`
  75. FResult string `gorm:"column:F_Result" json:"F_Result" form:"F_Result"`
  76. FUnit string `gorm:"column:F_Unit" json:"F_Unit" form:"F_Unit"`
  77. }
  78. func (KMSheet) TableName() string {
  79. return "KM_LIS_Result"
  80. }
  81. func GetPatientsInfoByName(name string, org_id int64) (info models.Patients, err error) {
  82. err = readDb.Model(&models.Patients{}).Where("name = ? AND user_org_id = ?", name, org_id).First(&info).Error
  83. return
  84. }
  85. // 获取lis返回检验检查结果数据
  86. func GetSHResultDataInsertDB(org_id int64) (result []*KMResult) {
  87. org := &models.DataUploadConfig{
  88. OrgId: 10215,
  89. ProvinceId: 19,
  90. CityId: 291,
  91. DepartmentName: "肇庆三鹤血液透析中心",
  92. HospitalId: "MA5DB4KB6",
  93. InstType: 1,
  94. DbHost: "localhost",
  95. DbPort: "1433",
  96. DbPass: "KingMed",
  97. DbUser: "KingMed",
  98. DbName: "KMDB",
  99. }
  100. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  101. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  102. if err != nil {
  103. utils.ErrorLog("创建数据库连接失败:%v", err)
  104. return
  105. }
  106. record, _ := GetLastSyncResultRecord(org_id)
  107. fmt.Println(record)
  108. var sync_time int64
  109. if record.ID > 0 {
  110. sync_time = record.Ctime
  111. } else {
  112. sync_time = 1627747200
  113. }
  114. sync_time_temp := time.Unix(sync_time, 0)
  115. syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  116. list, err := GetSHResultRecord(orgDb, syncTimeStr)
  117. if err == nil {
  118. // fmt.Println(list)
  119. //插入到系统检验检查数据
  120. for _, item := range list {
  121. printInfo, _ := GetPatientsInfoByName(item.FName, 10215)
  122. if printInfo.ID == 0 {
  123. fmt.Println(item)
  124. fmt.Println(printInfo)
  125. fmt.Println("暂无该患者")
  126. continue
  127. }
  128. project_id := int64(0)
  129. project_id, _ = GetShjhProjectID(org_id, item.FCompoundItemName)
  130. item_id, _ := GetShItemID(org_id, item.FCompoundItemName, item.FSingleItemName, project_id)
  131. tx := writeMiddleDb.Begin()
  132. var inspection models.MiddleInspection
  133. var inspection_reference models.MiddleInspectionReference
  134. recordDateStr := ""
  135. //inspect_date := item.FRecordTime.Format("2006-01-02 15:04")
  136. timstr := strings.Split(item.FRecordTime, "T")
  137. timstem := strings.Split(timstr[1], "Z")
  138. recordDateStr = timstr[0] + " " + timstem[0]
  139. // recordDateStr = item.FRecordTime
  140. date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  141. record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  142. // ftimstr := strings.Split(item.FCheckTime, "T")
  143. // ftimstem := strings.Split(timstr[1], "Z")
  144. // fdate := ftimstr[0] + " " + ftimstem[0]
  145. fdate := item.FCheckTime
  146. var total int
  147. var RangeOptions string
  148. var RangeMin string
  149. var RangeMax string
  150. // 判断检查类型
  151. var ItemType int
  152. if IsChineseChar(item.FReference) || strings.Contains(item.FReference, "|") || strings.Contains(item.FReference, "≤") {
  153. ItemType = 2
  154. } else {
  155. if strings.Contains(item.FReference, "-") {
  156. ItemType = 1
  157. } else {
  158. ItemType = 2
  159. }
  160. }
  161. if ItemType == 1 {
  162. Range := strings.Split(item.FReference, "-")
  163. RangeMin = Range[0]
  164. RangeMax = Range[1]
  165. } else {
  166. RangeOptions = item.FReference
  167. }
  168. 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
  169. if inspection_reference.ID > 0 {
  170. ItemType = inspection_reference.RangeType
  171. }
  172. if total <= 0 {
  173. inspection_reference.OrgId = org_id
  174. inspection_reference.ProjectName = item.FCompoundItemName
  175. inspection_reference.Project = item.FCompoundItemName
  176. inspection_reference.ProjectId = project_id
  177. inspection_reference.ItemName = item.FSingleItemName
  178. inspection_reference.ItemNameAddition = ""
  179. inspection_reference.ItemId = item_id
  180. inspection_reference.RangeType = ItemType
  181. inspection_reference.RangeMin = RangeMin
  182. inspection_reference.RangeMax = RangeMax
  183. inspection_reference.RangeOptions = RangeOptions
  184. inspection_reference.Unit = item.FUnit
  185. inspection_reference.Status = 1
  186. inspection_reference.CreatedTime = time.Now().Unix()
  187. inspection_reference.UpdatedTime = time.Now().Unix()
  188. inspection_reference.InspectDate = fdate
  189. inspection_reference.UTime = fdate
  190. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  191. if err != nil {
  192. tx.Rollback()
  193. }
  194. }
  195. var itotal int
  196. 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
  197. if itotal <= 0 {
  198. inspection.PatientId = printInfo.ID
  199. inspection.OrgId = org_id
  200. inspection.ProjectId = project_id
  201. inspection.ItemName = inspection_reference.ItemName
  202. inspection.ProjectName = inspection_reference.ProjectName
  203. inspection.InspectType = ItemType
  204. inspection.ItemId = item_id
  205. inspection.InspectValue = item.FResult
  206. inspection.InspectDate = item.FCheckTime
  207. inspection.RecordDate = record_date.Unix()
  208. inspection.Status = 1
  209. inspection.CreatedTime = time.Now().Unix()
  210. inspection.UpdatedTime = time.Now().Unix()
  211. inspection.UTime = item.FCheckTime
  212. inspection.HisUserId = strconv.FormatInt(printInfo.ID, 10)
  213. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  214. if err != nil {
  215. tx.Rollback()
  216. }
  217. }
  218. tx.Commit()
  219. }
  220. //插入一条查询检验检查结果记录状态数据
  221. var info LisSyncResultStatusInfo
  222. info.Ctime = time.Now().Unix()
  223. info.Status = 1
  224. info.Mtime = time.Now().Unix()
  225. info.OrgId = org_id
  226. info.IsResult = 1
  227. info.LastId = int64(len(list))
  228. info.ResultDate = time.Now().Unix()
  229. writeMiddleDb.Save(&info)
  230. }
  231. // 第一步:跟进org_id 去中间库查出需要同步的数据
  232. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  233. inspections, _ := GetSyncInspectionByOrgId(org_id)
  234. // 第二步:将数据同步到业务库
  235. if len(inspection_references) > 0 {
  236. for _, inspection_reference := range inspection_references {
  237. SyncInspectionReference(&inspection_reference)
  238. }
  239. }
  240. if len(inspections) > 0 {
  241. for _, inspection := range inspections {
  242. SyncInspection(&inspection)
  243. }
  244. }
  245. }
  246. return
  247. }
  248. func GetSHResultRecord(rdb *gorm.DB, synctime string) (record []*KMSheet, err error) {
  249. err = rdb.Model(&KMSheet{}).Where("F_CheckTime >= ?", synctime).Find(&record).Error
  250. return
  251. }
  252. // 判断是否包含中文
  253. func IsChineseChar(str string) bool {
  254. for _, r := range str {
  255. if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
  256. return true
  257. }
  258. }
  259. return false
  260. }
  261. func GetResultRecordByTime(id int64) (record []*KMResult, err error) {
  262. err = readDb.Model(&KMResult{}).Where("barcode > ? AND repdate >= 2021-08-12 00:00:00", id).Order("barcode asc").Find(&record).Error
  263. return
  264. }