cron.go 6.1KB

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