package service import ( "IC/models" "IC/utils" "encoding/json" _ "fmt" _ "github.com/jinzhu/gorm" "github.com/robfig/cron" _ "strings" "time" ) // cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html var createLisSyncCronJob *cron.Cron func init() { utils.InfoLog("开启自动检验检查同步定时任务") createLisSyncCronJob = cron.New() spec := "0 0 1 * * ?" // 每天凌晨1点同步 createLisSyncCronJob.AddFunc(spec, func() { AutoSyncLis() }) speclg := "0 0 2 * * ?" // 每天凌晨2点同步 龙岗二院 createLisSyncCronJob.AddFunc(speclg, func() { SyncLisLgty() }) specys := "0 0 3 * * ?" // 每天凌晨3点同步 沅胜血透 createLisSyncCronJob.AddFunc(specys, func() { SyncHbdyLis() }) // specgzjh := "0 */1 7-20 * * " // 每2分钟同步 广州暨华HIS医嘱 // createLisSyncCronJob.AddFunc(specgzjh, func() { // SyncGzjhyz() // }) specgzjhlis := "0 30 3 * * ?" // 每天凌晨3点30同步 广州暨华 createLisSyncCronJob.AddFunc(specgzjhlis, func() { SyncGzjhLis() }) specszbllis := "0 30 4 * * ?" // 每天凌晨4点30同步 广州暨华 createLisSyncCronJob.AddFunc(specszbllis, func() { SyncSZblLis() }) specszbllisResult := "0 0 2 * * ?" // 每天凌晨3点00同步 广州暨华 createLisSyncCronJob.AddFunc(specszbllisResult, func() { SyncSZblLisResult() }) } func BeginAutoSyncLis() { createLisSyncCronJob.Start() } func AutoSyncLis() { utils.TraceLog("检验检查任务开始执行") org_id := int64(9538) // 第一步:根据机构id获取对应机构所有病人的his_user_id patients, patientError := GetHisUserIdByOrgID(org_id) if patientError != nil { utils.ErrorLog("获取用户His_user_id信息失败:%v", patientError) return } // patientStr:= make([]interface{},0) if len(patients) > 0 { // 第二步:根据病人的his_user_id来查询上次同步时间 for _, patient := range patients { if len(patient.HisUserId) <= 0 { continue } syncLastInfo, _ := GetSyncTimeByOrgID(org_id, patient.HisUserId) var sync_time int64 if syncLastInfo.ID > 0 { sync_time = syncLastInfo.SyncTime } else { inspectionLastInfo, _ := GetInspectionLastInfoByOrgID(org_id, patient.HisUserId) if inspectionLastInfo.ID > 0 { sync_time = inspectionLastInfo.CreatedTime } else { sync_time = 0 } } syncTimes := time.Unix(sync_time, 0).Format("2006-01-02 15:04") utils.InfoLog("syncTimes:%v", syncTimes) // 第三步:跟进上次同步时间和指定的his_user_id,获取上次同步到现在指定人的检验检查记录 resultList, _ := GetListByHisUserID(patient.HisUserId, syncTimes, patient.SelectType) var syncInfo models.MiddleSyncInfo if len(resultList) > 0 { // 如果有数据,则把同步过来的详情插入一条数据到同步详情表中 syncInfo.OrgId = org_id syncInfo.HisUserId = patient.HisUserId syncInfo.SyncTime = time.Now().Unix() syncInfo.SyncResultType = 1 syncInfo.SyncRsultRemark = "同步成功" syncInfo.SyncTotalNum = int64(len(resultList)) syncInfo.SyncSuccessNum = 0 SyncInfos, _ := json.Marshal(resultList) syncInfo.SyncInfo = string(SyncInfos) syncInfo.CreateTime = time.Now().Unix() syncInfo.UpdateTime = time.Now().Unix() err := CreateSyncInfo(&syncInfo) if err != nil { utils.ErrorLog("创建同步信息失败:%v", err) return } } else { // 如果没有数据,则插入一条数据到同步详情表中 syncInfo.OrgId = org_id syncInfo.HisUserId = patient.HisUserId syncInfo.SyncTime = time.Now().Unix() syncInfo.SyncResultType = 1 syncInfo.SyncRsultRemark = "该患者这个时间段没有任何检验检查数据" syncInfo.CreateTime = time.Now().Unix() syncInfo.UpdateTime = time.Now().Unix() err := CreateSyncInfo(&syncInfo) if err != nil { utils.ErrorLog("创建同步信息失败:%v", err) return } // utils.InfoLog("同步信息详情:%v",syncInfo) continue } // 第四部:将同步过来的数据插入到中间库对应的表 syncSuccessNum := 0 for _, result := range resultList { if (result.HisUserId == patient.HisUserId) || (result.PatientName == patient.HisUserId) { patientId := patient.XtPatientId if patientId > 0 { inspection_reference, inspection, _ := Sync(result, org_id, patientId) if inspection_reference.ID > 0 || inspection.ID > 0 { syncSuccessNum++ } } } } syncInfo.SyncSuccessNum = int64(syncSuccessNum) UpdateSyncInfo(&syncInfo) // patientStr = append(patientStr,patient.HisUserId) // utils.InfoLog("patientStr:%v",patientStr) } } else { var syncInfo models.MiddleSyncInfo // 如果没有数据,则插入一条数据到同步详情表中 syncInfo.OrgId = org_id syncInfo.SyncTime = time.Now().Unix() syncInfo.SyncResultType = 0 syncInfo.SyncRsultRemark = "该机构没有对应的his_user_id" syncInfo.CreateTime = time.Now().Unix() syncInfo.UpdateTime = time.Now().Unix() err := CreateSyncInfo(&syncInfo) if err != nil { utils.ErrorLog("创建同步信息失败:%v", err) return } utils.InfoLog("同步信息详情:%v", syncInfo) return } SyncToBase() utils.SuccessLog("检验检查任务完成") } func SyncToBase() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(9538) // 第一步:跟进org_id 去中间库查出需要同步的数据 inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id) inspections, _ := GetSyncInspectionByOrgId(org_id) // 第二步:将数据同步到业务库 if len(inspection_references) > 0 { for _, inspection_reference := range inspection_references { SyncInspectionReference(&inspection_reference) } } if len(inspections) > 0 { for _, inspection := range inspections { SyncInspection(&inspection) } } utils.SuccessLog("检验检查同步任务完成") } func SyncSZblLis() { utils.TraceLog("检验检查同步任务开始执行") org_id := int64(10138) GetDataInsertDB(org_id) } func SyncSZblLisResult() { utils.TraceLog("获取检验检查结果任务开始执行") org_id := int64(10138) GetResultDataInsertDB(org_id) }