tszhra_service.go 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "encoding/json"
  6. _ "fmt"
  7. "github.com/jinzhu/gorm"
  8. "strings"
  9. "time"
  10. )
  11. type zhraLis struct {
  12. UserCardId string `json:"user_card_id"`
  13. PatientName string `json:"patient_name"`
  14. ProjectId string `json:"project_id"`
  15. ProjectName string `json:"project_name"`
  16. InspectDate string `json:"inspect_date"`
  17. ItemId string `json:"item_id"`
  18. ItemName string `json:"item_name"`
  19. ItemType string `json:"item_type"`
  20. ItemValue string `json:"item_value"`
  21. RangeMin string `json:"range_min"`
  22. RangeMax string `json:"range_max"`
  23. RangeValue string `json:"range_value"`
  24. RangeOptions string `json:"range_options"`
  25. Unit string `json:"unit"`
  26. State string `json:"state"`
  27. CTime string `json:"c_time"`
  28. UTime string `json:"u_time"`
  29. }
  30. func (zhraLis) TableName() string {
  31. return "V_LIS_XTJK"
  32. }
  33. func GetTszhraLis(rdb *gorm.DB, synctime string, user_card_id string) (record []*zhraLis, err error) {
  34. err = rdb.Model(&zhraLis{}).Where("c_time >= ? and user_card_id = ?", synctime, user_card_id).Find(&record).Error
  35. return
  36. }
  37. // 根据机构ID和患者身份证号码获取患者信息
  38. func GetUserInIdcardNoByOrg(org_id int64) (patients []*models.Patients, err error) {
  39. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 ", org_id).Find(&patients).Error
  40. return
  41. }
  42. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  43. func GetZhraProjectID(org_id int64, project_name string, item_name string) (project_id int64, err error) {
  44. var inspection_reference models.MiddleInspectionReference
  45. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? and item_name = ? ", org_id, project_name, item_name).First(&inspection_reference).Error
  46. if inspection_reference.ID > 0 {
  47. return inspection_reference.ProjectId, err
  48. } else {
  49. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? ", org_id, project_name).First(&inspection_reference).Error
  50. if inspection_reference.ID > 0 {
  51. return inspection_reference.ProjectId, err
  52. } else {
  53. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  54. if inspection_reference.ProjectId > 0 {
  55. return inspection_reference.ProjectId + 1, err
  56. } else {
  57. return 1058501, err
  58. }
  59. }
  60. }
  61. }
  62. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  63. func GetZhraItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  64. var inspection_reference models.MiddleInspectionReference
  65. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  66. if inspection_reference.ID > 0 {
  67. return inspection_reference.ItemId, err
  68. } else {
  69. err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and project_id = ? ", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_reference).Error
  70. if inspection_reference.ItemId > 0 {
  71. return inspection_reference.ItemId + 1, err
  72. } else {
  73. return project_id*100 + 1, err
  74. }
  75. }
  76. }
  77. // 唐山遵化仁爱医院Lis同步
  78. func SyncTszhraLis() (err error) {
  79. org_id := int64(10585)
  80. org := &models.DataUploadConfig{
  81. OrgId: org_id,
  82. DbHost: "192.168.100.100",
  83. DbPort: "1433",
  84. DbPass: "xytx",
  85. DbUser: "xytx",
  86. DbName: "BLis",
  87. }
  88. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  89. if err != nil {
  90. utils.ErrorLog("创建数据库连接失败:%v", err)
  91. return
  92. }
  93. // 第一步:获取上一次同步的时间点
  94. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  95. var sync_time int64
  96. if syncLastInfo.ID > 0 {
  97. sync_time = syncLastInfo.SyncTime + 86400
  98. } else {
  99. sync_time = 1672502400 // 2023-01-01
  100. }
  101. // 第二步:获取时间内所有检验结果
  102. sync_time_temp := time.Unix(sync_time, 0)
  103. startTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  104. patients, _ := GetUserInIdcardNoByOrg(org_id)
  105. if len(patients) > 0 {
  106. for _, patient := range patients {
  107. userCardId := patient.IdCardNo
  108. // 根据姓名获取患者ID
  109. zhraLis, _ := GetTszhraLis(orgDb, startTimeStr, userCardId)
  110. if len(zhraLis) > 0 {
  111. for _, LisInfo := range zhraLis {
  112. project_id, _ := GetZhraProjectID(org_id, LisInfo.ProjectName, LisInfo.ItemName)
  113. item_id, _ := GetZhraItemID(org_id, LisInfo.ProjectName, LisInfo.ItemName, project_id)
  114. tx := writeMiddleDb.Begin()
  115. var inspection models.MiddleInspection
  116. var inspection_reference models.MiddleInspectionReference
  117. timeLayout := "2006-01-02 15:04"
  118. loc, _ := time.LoadLocation("Local")
  119. tempRecord := LisInfo.CTime
  120. recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc)
  121. recordTimeInt := recordTime.Unix()
  122. recordTimeStr := time.Unix(recordTimeInt, 0).Format(timeLayout)
  123. var total int
  124. var RangeOptions string
  125. var RangeMin string
  126. var RangeMax string
  127. var ItemType int
  128. Range := strings.Split(LisInfo.RangeValue, "--")
  129. if len(Range) > 1 {
  130. RangeMin = Range[0]
  131. RangeMax = Range[1]
  132. ItemType = 1
  133. } else {
  134. ItemType = 2
  135. RangeOptions = LisInfo.RangeValue
  136. }
  137. 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
  138. if total <= 0 {
  139. inspection_reference.OrgId = org_id
  140. inspection_reference.ProjectName = LisInfo.ProjectName
  141. inspection_reference.Project = LisInfo.ProjectName
  142. inspection_reference.ProjectId = project_id
  143. inspection_reference.ItemName = LisInfo.ItemName
  144. // inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  145. inspection_reference.ItemId = item_id
  146. inspection_reference.RangeType = ItemType
  147. inspection_reference.RangeMin = RangeMin
  148. inspection_reference.RangeMax = RangeMax
  149. // inspection_reference.RangeValue = RangeValue
  150. inspection_reference.RangeOptions = RangeOptions
  151. inspection_reference.Unit = LisInfo.Unit
  152. inspection_reference.Status = 1
  153. inspection_reference.CreatedTime = time.Now().Unix()
  154. inspection_reference.UpdatedTime = time.Now().Unix()
  155. inspection_reference.InspectDate = recordTimeStr
  156. inspection_reference.UTime = recordTimeStr
  157. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  158. if err != nil {
  159. tx.Rollback()
  160. }
  161. }
  162. var itotal int
  163. 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, recordTimeStr, patient.ID).Find(&inspection).Count(&itotal).Error
  164. if itotal <= 0 {
  165. // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  166. // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  167. inspection.PatientId = patient.ID
  168. inspection.OrgId = org_id
  169. inspection.ProjectId = project_id
  170. inspection.ItemName = inspection_reference.ItemName
  171. inspection.ProjectName = inspection_reference.ProjectName
  172. inspection.InspectType = ItemType
  173. inspection.ItemId = item_id
  174. inspection.InspectValue = LisInfo.ItemValue
  175. inspection.InspectDate = recordTimeStr
  176. inspection.RecordDate = recordTimeInt
  177. // inspection.InspectTips = report.Resultstate
  178. inspection.Status = 1
  179. inspection.CreatedTime = time.Now().Unix()
  180. inspection.UpdatedTime = time.Now().Unix()
  181. // inspection.UTime = record_date.Format(timeLayout)
  182. // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  183. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  184. if err != nil {
  185. tx.Rollback()
  186. }
  187. }
  188. tx.Commit()
  189. }
  190. } else {
  191. continue
  192. }
  193. }
  194. var syncInfo models.MiddleSyncInfo
  195. syncInfo.OrgId = org_id
  196. syncInfo.SyncTime = time.Now().Unix()
  197. syncInfo.SyncResultType = 1
  198. syncInfo.SyncRsultRemark = "同步成功"
  199. syncInfo.SyncTotalNum = 0
  200. syncInfo.SyncSuccessNum = 0
  201. syncInfo.SyncInfo = ""
  202. syncInfo.CreateTime = time.Now().Unix()
  203. syncInfo.UpdateTime = time.Now().Unix()
  204. cwderr := CreateSyncInfo(&syncInfo)
  205. if cwderr != nil {
  206. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  207. return
  208. }
  209. }
  210. SyncTotszhrayy()
  211. return
  212. }
  213. func SyncTotszhrayy() {
  214. utils.TraceLog("检验检查同步任务开始执行")
  215. org_id := int64(10585)
  216. // 第一步:跟进org_id 去中间库查出需要同步的数据
  217. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  218. inspections, _ := GetSyncInspectionByOrgId(org_id)
  219. // 第二步:将数据同步到业务库
  220. if len(inspection_references) > 0 {
  221. for _, inspection_reference := range inspection_references {
  222. SyncInspectionReference(&inspection_reference)
  223. }
  224. }
  225. if len(inspections) > 0 {
  226. for _, inspection := range inspections {
  227. SyncInspection(&inspection)
  228. }
  229. }
  230. utils.SuccessLog("检验检查同步任务完成")
  231. }