sgyhs_service.go 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. "github.com/jinzhu/gorm"
  6. "strings"
  7. "time"
  8. )
  9. type sgyhsLisReportResult struct {
  10. Itemcode string `gorm:"column:itemcode" json:"itemcode"`
  11. Itemname string `gorm:"column:itemname" json:"itemname"`
  12. Result string `gorm:"column:result" json:"result"`
  13. Unit string `gorm:"column:unit" json:"unit"`
  14. Refrange string `gorm:"column:refrange" json:"refrange"`
  15. Ts string `gorm:"column:ts" json:"ts"`
  16. Repdate string `gorm:"column:repdate" json:"repdate"`
  17. Name string `gorm:"column:name" json:"name"`
  18. DockingapplyitemName string `gorm:"column:DockingApplyItemName" json:"DockingApplyItemName"`
  19. Sex string `gorm:"column:sex" json:"sex"`
  20. Age string `gorm:"column:age" json:"age"`
  21. }
  22. func (sgyhsLisReportResult) TableName() string {
  23. return "v_cm_result"
  24. }
  25. func GetSgyhsLisReport(rdb *gorm.DB, synctime string) (record []*sgyhsLisReportResult, err error) {
  26. err = rdb.Model(&sgyhsLisReportResult{}).Where("repdate >= ?", synctime).Find(&record).Error
  27. return
  28. }
  29. // 韶关始兴源和圣血液透析中心Lis同步
  30. func SyncLisSgyhs() (err error) {
  31. org_id := int64(10571)
  32. org := &models.DataUploadConfig{
  33. OrgId: org_id,
  34. DbHost: "192.168.3.75",
  35. DbPort: "1433",
  36. DbPass: "hy@123456.",
  37. DbUser: "sa",
  38. DbName: "hyDB",
  39. }
  40. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  41. if err != nil {
  42. utils.ErrorLog("创建数据库连接失败:%v", err)
  43. return
  44. }
  45. // 第一步:获取上一次同步的时间点
  46. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  47. var sync_time int64
  48. if syncLastInfo.ID > 0 {
  49. sync_time = syncLastInfo.SyncTime
  50. } else {
  51. sync_time = 1698768000 // 2023-01-01
  52. }
  53. SyncTime := time.Unix(sync_time, 0)
  54. syncTimeStr := SyncTime.Format("2006-01-02 15:04:05")
  55. utils.InfoLog("syncTimeStr:%v", syncTimeStr)
  56. // 第二步:获取时间内所有检验结果
  57. sgyhsLisResult, _ := GetSgyhsLisReport(orgDb, syncTimeStr)
  58. if len(sgyhsLisResult) > 0 {
  59. for _, Result := range sgyhsLisResult {
  60. utils.InfoLog("Report:%v", Result)
  61. if Result.Name == "陈永中" {
  62. Result.Name = "陈永忠"
  63. }
  64. if Result.Name == "朱小飞" {
  65. Result.Name = "朱小妃"
  66. }
  67. patient, _ := GetUserInfoByName(org_id, Result.Name)
  68. if patient.ID > 0 {
  69. project_id, _ := GetSgyhsProjectID(org_id, Result.DockingapplyitemName)
  70. item_id, _ := GetSgyhsItemID(org_id, Result.DockingapplyitemName, Result.Itemname, project_id)
  71. tx := writeMiddleDb.Begin()
  72. var inspection models.MiddleInspection
  73. var inspection_reference models.MiddleInspectionReference
  74. tempTime := strings.Split(Result.Repdate, "T")
  75. tempTime1 := strings.Split(tempTime[1], "Z")
  76. tempRecord := tempTime[0] + " " + tempTime1[0]
  77. recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord)
  78. inspect_date := recordTime.Format("2006-01-02 15:04")
  79. recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
  80. recordTimeInt := recordTimeStr.Unix()
  81. var total int
  82. var RangeOptions string
  83. var RangeMin string
  84. var RangeMax string
  85. var ItemType int
  86. Range := strings.Split(Result.Refrange, "-")
  87. if len(Range) > 1 {
  88. Range1 := strings.Split(Range[0], "[")
  89. if len(Range1) > 1 {
  90. Range2 := strings.Split(Range[1], "]")
  91. RangeMin = Range1[1]
  92. RangeMax = Range2[0]
  93. ItemType = 1
  94. } else {
  95. ItemType = 2
  96. RangeMin = Result.Refrange
  97. }
  98. } else {
  99. ItemType = 2
  100. RangeMin = Result.Refrange
  101. }
  102. RangeOptions = Result.Ts
  103. 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
  104. if total <= 0 {
  105. inspection_reference.OrgId = org_id
  106. inspection_reference.ProjectName = Result.DockingapplyitemName
  107. inspection_reference.Project = Result.DockingapplyitemName
  108. inspection_reference.ProjectId = project_id
  109. inspection_reference.ItemName = Result.Itemname
  110. inspection_reference.ItemNameAddition = Result.Itemcode
  111. inspection_reference.ItemId = item_id
  112. inspection_reference.RangeType = ItemType
  113. inspection_reference.RangeMin = RangeMin
  114. inspection_reference.RangeMax = RangeMax
  115. inspection_reference.Unit = Result.Unit
  116. inspection_reference.Status = 1
  117. inspection_reference.CreatedTime = time.Now().Unix()
  118. inspection_reference.UpdatedTime = time.Now().Unix()
  119. inspection_reference.InspectDate = tempRecord
  120. inspection_reference.UTime = tempRecord
  121. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  122. if err != nil {
  123. tx.Rollback()
  124. }
  125. }
  126. var itotal int
  127. 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
  128. if itotal <= 0 {
  129. // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  130. // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  131. inspection.PatientId = patient.ID
  132. inspection.OrgId = org_id
  133. inspection.ProjectId = project_id
  134. inspection.ItemName = inspection_reference.ItemName
  135. inspection.ProjectName = inspection_reference.ProjectName
  136. inspection.InspectType = ItemType
  137. inspection.ItemId = item_id
  138. inspection.InspectValue = Result.Result
  139. inspection.InspectDate = inspect_date
  140. inspection.RecordDate = recordTimeInt
  141. inspection.InspectTips = RangeOptions
  142. inspection.Status = 1
  143. inspection.CreatedTime = time.Now().Unix()
  144. inspection.UpdatedTime = time.Now().Unix()
  145. // inspection.UTime = record_date.Format(timeLayout)
  146. // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  147. inspection.SysProjectId = inspection_reference.XtProjectId
  148. inspection.SysItemId = inspection_reference.XtItemId
  149. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  150. if err != nil {
  151. tx.Rollback()
  152. }
  153. }
  154. tx.Commit()
  155. } else {
  156. continue
  157. }
  158. }
  159. var syncInfo models.MiddleSyncInfo
  160. syncInfo.OrgId = org_id
  161. syncInfo.SyncTime = time.Now().Unix()
  162. syncInfo.SyncResultType = 1
  163. syncInfo.SyncRsultRemark = "同步成功"
  164. syncInfo.SyncTotalNum = 0
  165. syncInfo.SyncSuccessNum = 0
  166. syncInfo.SyncInfo = ""
  167. syncInfo.CreateTime = time.Now().Unix()
  168. syncInfo.UpdateTime = time.Now().Unix()
  169. cwderr := CreateSyncInfo(&syncInfo)
  170. if cwderr != nil {
  171. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  172. return
  173. }
  174. }
  175. SyncToSgyhs()
  176. return
  177. }
  178. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  179. func GetSgyhsProjectID(org_id int64, project_name string) (project_id int64, err error) {
  180. var inspection_reference models.MiddleInspectionReference
  181. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ? ", org_id, project_name).First(&inspection_reference).Error
  182. if inspection_reference.ID > 0 {
  183. return inspection_reference.ProjectId, err
  184. } else {
  185. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  186. if inspection_reference.ProjectId > 0 {
  187. return inspection_reference.ProjectId + 1, err
  188. } else {
  189. return 1057101, err
  190. }
  191. }
  192. }
  193. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  194. func GetSgyhsItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  195. var inspection_reference models.MiddleInspectionReference
  196. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  197. if inspection_reference.ID > 0 {
  198. return inspection_reference.ItemId, err
  199. } else {
  200. 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
  201. if inspection_reference.ItemId > 0 {
  202. return inspection_reference.ItemId + 1, err
  203. } else {
  204. return project_id*100 + 1, err
  205. }
  206. }
  207. }
  208. func SyncToSgyhs() {
  209. utils.TraceLog("检验检查同步任务开始执行")
  210. org_id := int64(10571)
  211. // 第一步:跟进org_id 去中间库查出需要同步的数据
  212. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  213. inspections, _ := GetSyncInspectionByOrgId(org_id)
  214. // 第二步:将数据同步到业务库
  215. if len(inspection_references) > 0 {
  216. for _, inspection_reference := range inspection_references {
  217. SyncInspectionReference(&inspection_reference)
  218. }
  219. }
  220. if len(inspections) > 0 {
  221. for _, inspection := range inspections {
  222. SyncInspection(&inspection)
  223. }
  224. }
  225. utils.SuccessLog("检验检查同步任务完成")
  226. }