Exemplo n.º 1
0
        //[Invoke(Begin = "2020-11-11 00:00", Interval = 1000 * 3600, SkipWhileExecuting = true)]
        public void DoTask()
        {
            int totalCount = 0;
            int count      = 0;
            //获取AccessToken
            string accessToken = CallExternal.GetAccessToken();
            //获取外部接口日志
            DateTime time = DateTime.Today.AddDays(-13);//开始时间

            using (var db = new TestContext())
            {
                var query = db.ExternalLog.OrderByDescending(e => e.EndTime).FirstOrDefault();
                if (query != null)
                {
                    time = query.EndTime;
                }
            }
            long          startTime = TimeFormat.ToUnixTimestampByMilliseconds(time.AddMinutes(-1)); //开始时间
            long          endTime   = TimeFormat.ToUnixTimestampByMilliseconds(DateTime.Now);        //结束时间
            string        pageId    = null;                                                          //页码Id 上一页则传当前最小记录id,下一页则传当前最大id,首页或者最后页传null
            string        pageType  = "first";                                                       //页码类型 prev=上一页,next=下一页,first=第一页,last=最后一页
            var           pushLogs  = GetExternalLog(accessToken, startTime, endTime, pageId, pageType);
            List <string> insList   = new List <string>();

            pushLogs.ForEach(p =>
            {
                string ins = GetInstance(p.FormInstId, p.FormCodeId, accessToken);
                insList.Add(ins);
            });
            insList    = insList.Where(i => i.Contains("true")).ToList();
            totalCount = insList.Count();
            //获取实例
            try
            {
                insList.ForEach(p =>
                {
                    JObject jObject = JObject.Parse(p);
                    if (jObject["success"].ToString() == "True")
                    {
                        count += 1;
                        using (var db = new TestContext())
                        {
                            //解析数据存入数据库
                            //获取CGSqlist
                            ParseEntity PE = new ParseEntity();
                            CGSqlist cg    = PE.GetCGSQlist(jObject);
                            cg.FirstInput  = false;
                            //审批流
                            List <Reqlist> rlList = PE.GetReqlist(JObject.Parse(GetFlowRecord(cg.FormInstId, cg.FormCodeId)), cg.Fbillid);
                            rlList.ForEach(r =>
                            {
                                var list = db.Reqlist.AsNoTracking().Where(c => c.CreateTime == r.CreateTime)
                                           .Where(c => c.Fbillno == r.Fbillno).ToList();
                                if (list.Count() == 0)
                                {
                                    db.Reqlist.Add(r);
                                }
                            });
                            var query = db.CGSqlist.AsNoTracking()
                                        .Where(c => c.SerialNumber.Equals(cg.SerialNumber))
                                        .ToList();
                            if (query.Count == 0)
                            {
                                //获取CgsqListentry
                                List <CgsqListentry> cleList = PE.GetCgsqListentry(jObject, cg.Fbillid);
                                db.CGSqlist.Add(cg);
                                db.CgsqListentry.AddRange(cleList);
                            }
                            db.SaveChanges();
                        }
                    }
                });
                using (var DbContext = new TestContext())
                {
                    ExternalLog log = new ExternalLog
                    {
                        StartTime    = TimeFormat.ToLocalTimeTime(startTime),
                        EndTime      = TimeFormat.ToLocalTimeTime(endTime),
                        QueryState   = QueryState.成功,
                        FailTotalNum = totalCount,
                        TakeStatus   = TakeStatus.完全同步,
                        CatchNum     = count,
                        CreationTime = TimeFormat.ToLocalTimeTime(endTime),
                        TakeTime     = TimeFormat.ToLocalTimeTime(endTime)
                    };
                    DbContext.ExternalLog.Add(log);
                    DbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                using (var DbContext = new TestContext())
                {
                    ExternalLog log = new ExternalLog
                    {
                        StartTime    = TimeFormat.ToLocalTimeTime(startTime),
                        EndTime      = TimeFormat.ToLocalTimeTime(endTime),
                        QueryState   = QueryState.失败,
                        FailTotalNum = totalCount,
                        TakeStatus   = TakeStatus.部分同步,
                        CatchNum     = count,
                        CreationTime = TimeFormat.ToLocalTimeTime(endTime),
                        TakeTime     = TimeFormat.ToLocalTimeTime(endTime)
                    };
                    if (totalCount == 0 || count == 0)
                    {
                        log.TakeStatus = TakeStatus.未同步;
                    }
                    DbContext.ExternalLog.Add(log);
                    DbContext.SaveChanges();
                }
            }
        }
Exemplo n.º 2
0
        public void DoCheck()
        {
            int    count       = 0;
            int    totalCount  = 0;
            string accessToken = GetAccessToken();
            var    dbContext   = new TestContext();
            //查询失败的日志
            var query = dbContext.ExternalLog.Where(e => e.QueryState == QueryState.失败).ToList();

            //获取外部日志存入数据库
            query.ForEach(q =>
            {
                long startTime        = TimeFormat.ToUnixTimestampByMilliseconds(q.StartTime); //开始时间
                long endTime          = TimeFormat.ToUnixTimestampByMilliseconds(q.EndTime);   //结束时间
                string pageId         = null;                                                  //页码Id 上一页则传当前最小记录id,下一页则传当前最大id,首页或者最后页传null
                string pageType       = "first";                                               //页码类型 prev=上一页,next=下一页,first=第一页,last=最后一页
                var pushLogs          = GetExternalLog(accessToken, startTime, endTime, pageId, pageType);
                List <string> insList = new List <string>();
                pushLogs.ForEach(p =>
                {
                    string ins = GetInstance(p.FormInstId, p.FormCodeId, accessToken);
                    insList.Add(ins);
                });
                insList    = insList.Where(i => i.Contains("true")).ToList();
                totalCount = insList.Count();
                //获取实例
                try
                {
                    insList.ForEach(p =>
                    {
                        JObject jObject = JObject.Parse(p);
                        if (jObject["success"].ToString() == "True")
                        {
                            count += 1;
                            using (var db = new TestContext())
                            {
                                //解析数据存入数据库
                                //获取CGSqlist
                                ParseEntity PE = new ParseEntity();
                                CGSqlist cg    = PE.GetCGSQlist(jObject);
                                cg.FirstInput  = false;
                                //审批流
                                List <Reqlist> rlList = PE.GetReqlist(JObject.Parse(GetFlowRecord(cg.FormInstId, cg.FormCodeId)), cg.Fbillid);
                                rlList.ForEach(r =>
                                {
                                    var list = db.Reqlist.AsNoTracking().Where(c => c.CreateTime == r.CreateTime)
                                               .Where(c => c.Fbillno == r.Fbillno).ToList();
                                    if (list.Count() == 0)
                                    {
                                        db.Reqlist.Add(r);
                                    }
                                });
                                var query = db.CGSqlist.AsNoTracking()
                                            .Where(c => c.SerialNumber.Equals(cg.SerialNumber))
                                            .ToList();
                                if (query.Count == 0)
                                {
                                    //获取CgsqListentry
                                    List <CgsqListentry> cleList = PE.GetCgsqListentry(jObject, cg.Fbillid);
                                    db.CGSqlist.Add(cg);
                                    db.CgsqListentry.AddRange(cleList);
                                }
                                db.SaveChanges();
                            }
                        }
                    });
                    q.CatchNum   = count;
                    q.TakeStatus = TakeStatus.完全同步;
                    q.QueryState = QueryState.成功;
                    q.TakeTime   = DateTime.Now;
                    dbContext.Update(q);
                    dbContext.SaveChanges();
                }
                catch (Exception e)
                {
                    string content = startTime + "——" + endTime + "云之家外部日志接口同步失败";
                    //发送邮件
                    SendMail mailService = new SendMail();
                    bool fmailback       = mailService.Send("*****@*****.**", "*****@*****.**", "云之家外部日志接口问题", content, "*****@*****.**", "naduohua", "smtp.163.com", "", "");
                }
            });
        }