cron.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. }
  16. // 返回一个支持至 秒 级别的 cron
  17. func newWithSeconds() *cron.Cron {
  18. secondParser := cron.NewParser(cron.Second | cron.Minute |
  19. cron.Hour | cron.Dom | cron.Month | cron.DowOptional | cron.Descriptor)
  20. return cron.New(cron.WithParser(secondParser), cron.WithChain())
  21. }
  22. func BeginAutoSyncLis() {
  23. utils.InfoLog("开启自动检验检查同步定时任务")
  24. createLisSyncCronJob := newWithSeconds()
  25. specys := "0 0 */1 * * ?" // 每天凌晨3点同步 唐山朝阳医院
  26. createLisSyncCronJob.AddFunc(specys, func() {
  27. SyncTscyLis()
  28. })
  29. specys1 := "0 10 */2 * * ?" // 每两个小时同步长沙南雅医院
  30. createLisSyncCronJob.AddFunc(specys1, func() {
  31. SyncCsnyLis()
  32. })
  33. spec := "0 50 1 * * ?" // 每天凌晨1点同步 监利大院的
  34. createLisSyncCronJob.AddFunc(spec, func() {
  35. SyncHbdyLis()
  36. })
  37. spec2 := "0 50 2 * * ?" // 每天凌晨2点同步 龙岗二院
  38. createLisSyncCronJob.AddFunc(spec2, func() {
  39. SyncLisLgty()
  40. })
  41. spec3 := "0 50 3 * * ?" // 每天凌晨2点同步 广州暨华血液透析中心
  42. createLisSyncCronJob.AddFunc(spec3, func() {
  43. SyncLisGzjhtx()
  44. })
  45. spec4 := "0 20 3 * * ?" // 每天凌晨同步 石狮盛誉血液透析中心
  46. createLisSyncCronJob.AddFunc(spec4, func() {
  47. SyncSYData2()
  48. })
  49. spec5 := "0 20 4 * * ?" // 每天凌晨同步 石狮盛誉血液透析中心
  50. createLisSyncCronJob.AddFunc(spec5, func() {
  51. SyncTscyVipLis()
  52. })
  53. //spec6 := "0 20 1 * * ?" // 每天凌晨同步 广州康允透析中心LIS对接,本地部署的
  54. //createLisSyncCronJob.AddFunc(spec6, func() {
  55. // SyncGzkyLis()
  56. //})
  57. createLisSyncCronJob.Start()
  58. }
  59. func AutoSyncLis() {
  60. utils.TraceLog("检验检查任务开始执行")
  61. org_id := int64(9538)
  62. // 第一步:根据机构id获取对应机构所有病人的his_user_id
  63. patients, patientError := GetHisUserIdByOrgID(org_id)
  64. if patientError != nil {
  65. utils.ErrorLog("获取用户His_user_id信息失败:%v", patientError)
  66. return
  67. }
  68. // patientStr:= make([]interface{},0)
  69. if len(patients) > 0 {
  70. // 第二步:根据病人的his_user_id来查询上次同步时间
  71. for _, patient := range patients {
  72. if len(patient.HisUserId) <= 0 {
  73. continue
  74. }
  75. syncLastInfo, _ := GetSyncTimeByOrgID(org_id, patient.HisUserId)
  76. var sync_time int64
  77. if syncLastInfo.ID > 0 {
  78. sync_time = syncLastInfo.SyncTime
  79. } else {
  80. inspectionLastInfo, _ := GetInspectionLastInfoByOrgID(org_id, patient.HisUserId)
  81. if inspectionLastInfo.ID > 0 {
  82. sync_time = inspectionLastInfo.CreatedTime
  83. } else {
  84. sync_time = 0
  85. }
  86. }
  87. syncTimes := time.Unix(sync_time, 0).Format("2006-01-02 15:04")
  88. utils.InfoLog("syncTimes:%v", syncTimes)
  89. // 第三步:跟进上次同步时间和指定的his_user_id,获取上次同步到现在指定人的检验检查记录
  90. resultList, _ := GetListByHisUserID(patient.HisUserId, syncTimes, patient.SelectType)
  91. var syncInfo models.MiddleSyncInfo
  92. if len(resultList) > 0 {
  93. // 如果有数据,则把同步过来的详情插入一条数据到同步详情表中
  94. syncInfo.OrgId = org_id
  95. syncInfo.HisUserId = patient.HisUserId
  96. syncInfo.SyncTime = time.Now().Unix()
  97. syncInfo.SyncResultType = 1
  98. syncInfo.SyncRsultRemark = "同步成功"
  99. syncInfo.SyncTotalNum = int64(len(resultList))
  100. syncInfo.SyncSuccessNum = 0
  101. SyncInfos, _ := json.Marshal(resultList)
  102. syncInfo.SyncInfo = string(SyncInfos)
  103. syncInfo.CreateTime = time.Now().Unix()
  104. syncInfo.UpdateTime = time.Now().Unix()
  105. err := CreateSyncInfo(&syncInfo)
  106. if err != nil {
  107. utils.ErrorLog("创建同步信息失败:%v", err)
  108. return
  109. }
  110. } else {
  111. // 如果没有数据,则插入一条数据到同步详情表中
  112. syncInfo.OrgId = org_id
  113. syncInfo.HisUserId = patient.HisUserId
  114. syncInfo.SyncTime = time.Now().Unix()
  115. syncInfo.SyncResultType = 1
  116. syncInfo.SyncRsultRemark = "该患者这个时间段没有任何检验检查数据"
  117. syncInfo.CreateTime = time.Now().Unix()
  118. syncInfo.UpdateTime = time.Now().Unix()
  119. err := CreateSyncInfo(&syncInfo)
  120. if err != nil {
  121. utils.ErrorLog("创建同步信息失败:%v", err)
  122. return
  123. }
  124. // utils.InfoLog("同步信息详情:%v",syncInfo)
  125. continue
  126. }
  127. // 第四部:将同步过来的数据插入到中间库对应的表
  128. syncSuccessNum := 0
  129. for _, result := range resultList {
  130. if (result.HisUserId == patient.HisUserId) || (result.PatientName == patient.HisUserId) {
  131. patientId := patient.XtPatientId
  132. if patientId > 0 {
  133. inspection_reference, inspection, _ := Sync(result, org_id, patientId)
  134. if inspection_reference.ID > 0 || inspection.ID > 0 {
  135. syncSuccessNum++
  136. }
  137. }
  138. }
  139. }
  140. syncInfo.SyncSuccessNum = int64(syncSuccessNum)
  141. UpdateSyncInfo(&syncInfo)
  142. // patientStr = append(patientStr,patient.HisUserId)
  143. // utils.InfoLog("patientStr:%v",patientStr)
  144. }
  145. } else {
  146. var syncInfo models.MiddleSyncInfo
  147. // 如果没有数据,则插入一条数据到同步详情表中
  148. syncInfo.OrgId = org_id
  149. syncInfo.SyncTime = time.Now().Unix()
  150. syncInfo.SyncResultType = 0
  151. syncInfo.SyncRsultRemark = "该机构没有对应的his_user_id"
  152. syncInfo.CreateTime = time.Now().Unix()
  153. syncInfo.UpdateTime = time.Now().Unix()
  154. err := CreateSyncInfo(&syncInfo)
  155. if err != nil {
  156. utils.ErrorLog("创建同步信息失败:%v", err)
  157. return
  158. }
  159. utils.InfoLog("同步信息详情:%v", syncInfo)
  160. return
  161. }
  162. SyncToBase()
  163. utils.SuccessLog("检验检查任务完成")
  164. }
  165. func SyncToBase() {
  166. utils.TraceLog("检验检查同步任务开始执行")
  167. org_id := int64(9538)
  168. // 第一步:跟进org_id 去中间库查出需要同步的数据
  169. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  170. inspections, _ := GetSyncInspectionByOrgId(org_id)
  171. // 第二步:将数据同步到业务库
  172. if len(inspection_references) > 0 {
  173. for _, inspection_reference := range inspection_references {
  174. SyncInspectionReference(&inspection_reference)
  175. }
  176. }
  177. if len(inspections) > 0 {
  178. for _, inspection := range inspections {
  179. SyncInspection(&inspection)
  180. }
  181. }
  182. utils.SuccessLog("检验检查同步任务完成")
  183. }
  184. // func SyncSZblLis() {
  185. // utils.TraceLog("检验检查同步任务开始执行")
  186. // org_id := int64(10138)
  187. // GetDataInsertDB(org_id)
  188. // }
  189. // func SyncSZblLisResult() {
  190. // utils.TraceLog("获取检验检查结果任务开始执行")
  191. // org_id := int64(10138)
  192. // GetResultDataInsertDB(org_id)
  193. // }