cron.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. "encoding/json"
  6. _ "fmt"
  7. _ "github.com/jinzhu/gorm"
  8. "github.com/robfig/cron"
  9. _ "strings"
  10. "time"
  11. )
  12. // cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html
  13. var createLisSyncCronJob *cron.Cron
  14. func init() {
  15. utils.InfoLog("开启自动检验检查同步定时任务")
  16. createLisSyncCronJob = cron.New()
  17. spec := "0 0 1 * * ?" // 每天凌晨1点同步
  18. createLisSyncCronJob.AddFunc(spec, func() {
  19. AutoSyncLis()
  20. })
  21. //speclg := "0 0 2 * * ?" // 每天凌晨2点同步 龙岗二院
  22. //createLisSyncCronJob.AddFunc(speclg, func() {
  23. // SyncLisLgty()
  24. //})
  25. //
  26. //specys := "0 0 3 * * ?" // 每天凌晨3点同步 沅胜血透
  27. //createLisSyncCronJob.AddFunc(specys, func() {
  28. // SyncHbdyLis()
  29. //})
  30. // specgzjh := "0 */1 7-20 * * " // 每2分钟同步 广州暨华HIS医嘱
  31. // createLisSyncCronJob.AddFunc(specgzjh, func() {
  32. // SyncGzjhyz()
  33. // })
  34. //specgzjhlis := "0 30 3 * * ?" // 每天凌晨3点30同步 广州暨华
  35. //createLisSyncCronJob.AddFunc(specgzjhlis, func() {
  36. // SyncGzjhLis()
  37. //})
  38. //
  39. specszbllis := "0 0 4 * * ?" // 每天凌晨4点00同步 深圳百霖
  40. createLisSyncCronJob.AddFunc(specszbllis, func() {
  41. SyncSZblLis()
  42. })
  43. specszbllisResult := "0 30 4 * * ?" // 每天凌晨4点30同步 深圳百霖
  44. createLisSyncCronJob.AddFunc(specszbllisResult, func() {
  45. SyncSZblLisResult()
  46. })
  47. }
  48. func BeginAutoSyncLis() {
  49. createLisSyncCronJob.Start()
  50. }
  51. func AutoSyncLis() {
  52. utils.TraceLog("检验检查任务开始执行")
  53. org_id := int64(9538)
  54. // 第一步:根据机构id获取对应机构所有病人的his_user_id
  55. patients, patientError := GetHisUserIdByOrgID(org_id)
  56. if patientError != nil {
  57. utils.ErrorLog("获取用户His_user_id信息失败:%v", patientError)
  58. return
  59. }
  60. // patientStr:= make([]interface{},0)
  61. if len(patients) > 0 {
  62. // 第二步:根据病人的his_user_id来查询上次同步时间
  63. for _, patient := range patients {
  64. if len(patient.HisUserId) <= 0 {
  65. continue
  66. }
  67. syncLastInfo, _ := GetSyncTimeByOrgID(org_id, patient.HisUserId)
  68. var sync_time int64
  69. if syncLastInfo.ID > 0 {
  70. sync_time = syncLastInfo.SyncTime
  71. } else {
  72. inspectionLastInfo, _ := GetInspectionLastInfoByOrgID(org_id, patient.HisUserId)
  73. if inspectionLastInfo.ID > 0 {
  74. sync_time = inspectionLastInfo.CreatedTime
  75. } else {
  76. sync_time = 0
  77. }
  78. }
  79. syncTimes := time.Unix(sync_time, 0).Format("2006-01-02 15:04")
  80. utils.InfoLog("syncTimes:%v", syncTimes)
  81. // 第三步:跟进上次同步时间和指定的his_user_id,获取上次同步到现在指定人的检验检查记录
  82. resultList, _ := GetListByHisUserID(patient.HisUserId, syncTimes, patient.SelectType)
  83. var syncInfo models.MiddleSyncInfo
  84. if len(resultList) > 0 {
  85. // 如果有数据,则把同步过来的详情插入一条数据到同步详情表中
  86. syncInfo.OrgId = org_id
  87. syncInfo.HisUserId = patient.HisUserId
  88. syncInfo.SyncTime = time.Now().Unix()
  89. syncInfo.SyncResultType = 1
  90. syncInfo.SyncRsultRemark = "同步成功"
  91. syncInfo.SyncTotalNum = int64(len(resultList))
  92. syncInfo.SyncSuccessNum = 0
  93. SyncInfos, _ := json.Marshal(resultList)
  94. syncInfo.SyncInfo = string(SyncInfos)
  95. syncInfo.CreateTime = time.Now().Unix()
  96. syncInfo.UpdateTime = time.Now().Unix()
  97. err := CreateSyncInfo(&syncInfo)
  98. if err != nil {
  99. utils.ErrorLog("创建同步信息失败:%v", err)
  100. return
  101. }
  102. } else {
  103. // 如果没有数据,则插入一条数据到同步详情表中
  104. syncInfo.OrgId = org_id
  105. syncInfo.HisUserId = patient.HisUserId
  106. syncInfo.SyncTime = time.Now().Unix()
  107. syncInfo.SyncResultType = 1
  108. syncInfo.SyncRsultRemark = "该患者这个时间段没有任何检验检查数据"
  109. syncInfo.CreateTime = time.Now().Unix()
  110. syncInfo.UpdateTime = time.Now().Unix()
  111. err := CreateSyncInfo(&syncInfo)
  112. if err != nil {
  113. utils.ErrorLog("创建同步信息失败:%v", err)
  114. return
  115. }
  116. // utils.InfoLog("同步信息详情:%v",syncInfo)
  117. continue
  118. }
  119. // 第四部:将同步过来的数据插入到中间库对应的表
  120. syncSuccessNum := 0
  121. for _, result := range resultList {
  122. if (result.HisUserId == patient.HisUserId) || (result.PatientName == patient.HisUserId) {
  123. patientId := patient.XtPatientId
  124. if patientId > 0 {
  125. inspection_reference, inspection, _ := Sync(result, org_id, patientId)
  126. if inspection_reference.ID > 0 || inspection.ID > 0 {
  127. syncSuccessNum++
  128. }
  129. }
  130. }
  131. }
  132. syncInfo.SyncSuccessNum = int64(syncSuccessNum)
  133. UpdateSyncInfo(&syncInfo)
  134. // patientStr = append(patientStr,patient.HisUserId)
  135. // utils.InfoLog("patientStr:%v",patientStr)
  136. }
  137. } else {
  138. var syncInfo models.MiddleSyncInfo
  139. // 如果没有数据,则插入一条数据到同步详情表中
  140. syncInfo.OrgId = org_id
  141. syncInfo.SyncTime = time.Now().Unix()
  142. syncInfo.SyncResultType = 0
  143. syncInfo.SyncRsultRemark = "该机构没有对应的his_user_id"
  144. syncInfo.CreateTime = time.Now().Unix()
  145. syncInfo.UpdateTime = time.Now().Unix()
  146. err := CreateSyncInfo(&syncInfo)
  147. if err != nil {
  148. utils.ErrorLog("创建同步信息失败:%v", err)
  149. return
  150. }
  151. utils.InfoLog("同步信息详情:%v", syncInfo)
  152. return
  153. }
  154. SyncToBase()
  155. utils.SuccessLog("检验检查任务完成")
  156. }
  157. func SyncToBase() {
  158. utils.TraceLog("检验检查同步任务开始执行")
  159. org_id := int64(9538)
  160. // 第一步:跟进org_id 去中间库查出需要同步的数据
  161. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  162. inspections, _ := GetSyncInspectionByOrgId(org_id)
  163. // 第二步:将数据同步到业务库
  164. if len(inspection_references) > 0 {
  165. for _, inspection_reference := range inspection_references {
  166. SyncInspectionReference(&inspection_reference)
  167. }
  168. }
  169. if len(inspections) > 0 {
  170. for _, inspection := range inspections {
  171. SyncInspection(&inspection)
  172. }
  173. }
  174. utils.SuccessLog("检验检查同步任务完成")
  175. }
  176. func SyncSZblLis() {
  177. utils.TraceLog("检验检查同步任务开始执行")
  178. org_id := int64(10138)
  179. GetDataInsertDB(org_id)
  180. }
  181. func SyncSZblLisResult() {
  182. utils.TraceLog("获取检验检查结果任务开始执行")
  183. org_id := int64(10138)
  184. GetResultDataInsertDB(org_id)
  185. }