sh_service.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "unicode"
  11. "github.com/jinzhu/gorm"
  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 "Sheet"
  80. }
  81. //获取lis返回检验检查结果数据
  82. func GetSHResultDataInsertDB(org_id int64) (result []*KMResult) {
  83. org := &models.DataUploadConfig{
  84. OrgId: 10138,
  85. ProvinceId: 19,
  86. CityId: 291,
  87. DepartmentName: "深圳市百霖血液透析中心",
  88. HospitalId: "MA5DB4KB6",
  89. InstType: 1,
  90. DbHost: "localhost",
  91. DbPort: "1433",
  92. DbPass: "KingMed",
  93. DbUser: "KingMed",
  94. DbName: "KMDB",
  95. }
  96. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  97. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  98. if err != nil {
  99. utils.ErrorLog("创建数据库连接失败:%v", err)
  100. return
  101. }
  102. record, _ := GetLastSyncResultRecord(org_id)
  103. fmt.Println(record)
  104. var sync_time int64
  105. if record.ID > 0 {
  106. sync_time = record.Ctime
  107. } else {
  108. sync_time = 1627747200
  109. }
  110. sync_time_temp := time.Unix(sync_time,0)
  111. syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  112. list, err := GetSHResultRecord(orgDb,syncTimeStr)
  113. if err == nil {
  114. // fmt.Println(list)
  115. //插入到系统检验检查数据
  116. for _, item := range list {
  117. printInfo, _ := GetPatientsInfoByIDCardNo(item.FName, 10215)
  118. if printInfo.ID == 0 {
  119. fmt.Println(item)
  120. fmt.Println(printInfo)
  121. fmt.Println("暂无该患者")
  122. continue
  123. }
  124. project_id := int64(0)
  125. project_id, _ = GetShjhProjectID(org_id, item.FCompoundItemName)
  126. item_id, _ := GetShItemID(org_id, item.FCompoundItemName, item.FSingleItemName, project_id)
  127. tx := writeMiddleDb.Begin()
  128. var inspection models.MiddleInspection
  129. var inspection_reference models.MiddleInspectionReference
  130. recordDateStr := ""
  131. //inspect_date := item.FRecordTime.Format("2006-01-02 15:04")
  132. // timstr := strings.Split(item.FRecordTime, "T")
  133. // timstem := strings.Split(timstr[1], "Z")
  134. // recordDateStr = timstr[0] + " " + timstem[0]
  135. recordDateStr = item.FRecordTime
  136. date ,_ := utils.ParseTimeStringToTime("2006-01-02 15:04:05",recordDateStr)
  137. record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  138. // ftimstr := strings.Split(item.FCheckTime, "T")
  139. // ftimstem := strings.Split(timstr[1], "Z")
  140. // fdate := ftimstr[0] + " " + ftimstem[0]
  141. fdate := item.FCheckTime
  142. var total int
  143. var RangeOptions string
  144. var RangeMin string
  145. var RangeMax string
  146. // 判断检查类型
  147. var ItemType int
  148. if IsChineseChar(item.FReference) || strings.Contains(item.FReference, "|") || strings.Contains(item.FReference, "≤") {
  149. ItemType = 2
  150. } else {
  151. if strings.Contains(item.FReference, "-") {
  152. ItemType = 1
  153. } else {
  154. ItemType = 2
  155. }
  156. }
  157. if ItemType == 1 {
  158. Range := strings.Split(item.FReference, "-")
  159. RangeMin = Range[0]
  160. RangeMax = Range[1]
  161. } else {
  162. RangeOptions = item.FReference
  163. }
  164. 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
  165. if inspection_reference.ID > 0 {
  166. ItemType = inspection_reference.RangeType
  167. }
  168. if total <= 0 {
  169. inspection_reference.OrgId = org_id
  170. inspection_reference.ProjectName = item.FCompoundItemName
  171. inspection_reference.Project = item.FCompoundItemName
  172. inspection_reference.ProjectId = project_id
  173. inspection_reference.ItemName = item.FSingleItemName
  174. inspection_reference.ItemNameAddition = ""
  175. inspection_reference.ItemId = item_id
  176. inspection_reference.RangeType = ItemType
  177. inspection_reference.RangeMin = RangeMin
  178. inspection_reference.RangeMax = RangeMax
  179. inspection_reference.RangeOptions = RangeOptions
  180. inspection_reference.Unit = item.FUnit
  181. inspection_reference.Status = 1
  182. inspection_reference.CreatedTime = time.Now().Unix()
  183. inspection_reference.UpdatedTime = time.Now().Unix()
  184. inspection_reference.InspectDate = fdate
  185. inspection_reference.UTime = fdate
  186. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  187. if err != nil {
  188. tx.Rollback()
  189. }
  190. }
  191. var itotal int
  192. 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
  193. if itotal <= 0 {
  194. inspection.PatientId = printInfo.ID
  195. inspection.OrgId = org_id
  196. inspection.ProjectId = project_id
  197. inspection.ItemName = inspection_reference.ItemName
  198. inspection.ProjectName = inspection_reference.ProjectName
  199. inspection.InspectType = ItemType
  200. inspection.ItemId = item_id
  201. inspection.InspectValue = item.FResult
  202. inspection.InspectDate = item.FCheckTime
  203. inspection.RecordDate = record_date.Unix()
  204. inspection.Status = 1
  205. inspection.CreatedTime = time.Now().Unix()
  206. inspection.UpdatedTime = time.Now().Unix()
  207. inspection.UTime = item.FCheckTime
  208. inspection.HisUserId = strconv.FormatInt(printInfo.ID, 10)
  209. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  210. if err != nil {
  211. tx.Rollback()
  212. }
  213. }
  214. tx.Commit()
  215. }
  216. //插入一条查询检验检查结果记录状态数据
  217. var info LisSyncResultStatusInfo
  218. info.Ctime = time.Now().Unix()
  219. info.Status = 1
  220. info.Mtime = time.Now().Unix()
  221. info.OrgId = org_id
  222. info.IsResult = 1
  223. info.LastId = int64(len(list))
  224. info.ResultDate = time.Now().Unix()
  225. writeMiddleDb.Save(&info)
  226. }
  227. // 第一步:跟进org_id 去中间库查出需要同步的数据
  228. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  229. inspections, _ := GetSyncInspectionByOrgId(org_id)
  230. // 第二步:将数据同步到业务库
  231. if len(inspection_references) > 0 {
  232. for _, inspection_reference := range inspection_references {
  233. SyncInspectionReference(&inspection_reference)
  234. }
  235. }
  236. if len(inspections) > 0 {
  237. for _, inspection := range inspections {
  238. SyncInspection(&inspection)
  239. }
  240. }
  241. }
  242. return
  243. }
  244. func GetSHResultRecord(rdb *gorm.DB,synctime string) (record []*KMSheet, err error) {
  245. err = rdb.Model(&KMSheet{}).Where("F_CheckTime >= ?",synctime).Find(&record).Error
  246. return
  247. }
  248. //判断是否包含中文
  249. func IsChineseChar(str string) bool {
  250. for _, r := range str {
  251. if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
  252. return true
  253. }
  254. }
  255. return false
  256. }
  257. func GetResultRecordByTime(id int64) (record []*KMResult, err error) {
  258. err = readDb.Model(&KMResult{}).Where("barcode > ? AND repdate >= 2021-08-12 00:00:00", id).Order("barcode asc").Find(&record).Error
  259. return
  260. }