欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

api获取国家休息日 并填写到数据库中

程序员文章站 2022-05-16 21:25:52
...

/最近要获取节假日,并需要保存在数据中做考勤计算,为了不手动操作就用hangfire定时操作;

先看一下获取节假日的代码:

            int Yeahs = DateTime.Now.Year;//当前年份
            var model = LTDB.OffDay.Where(o => o.OffDate.Year == Yeahs).ToList();//通过数据查询到当前年份的数据,以前是手动添加的现在就获取一下,为了不添加重复的
            
            WebClient client = new WebClient();//  初始化 System.Net.WebClient 类的新实例。
            client.Encoding = Encoding.UTF8;
           //apiURL格式是  http://www.easybots.cn/api/holiday.php?m=201902
            for (int i = 1; i <= 12; i++)//每个月取一次api ,也可以取多个月份,可以去他们的官网去查询 http://www.easybots.cn/holiday_api.net
            {
                string str_i = "";
                if (i < 10)
                {
                    str_i = "0" + i.ToString();
                }
                else
                {
                    str_i = i.ToString();
                }
               
                var url = "http://www.easybots.cn/api/holiday.php?m=" + Yeahs.ToString() + str_i + "";
                var jsondata = client.DownloadString(url);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(jsondata);
//json格式是 {"201902":{"04":"1","05":"2","06":"2","07":"2","08":"1","09":"1","10":"1","16":"1","17":"1","23":"1","24":"1"}}
                string firstKey = json.ElementAt(0).Key;//201902
                object a = json.ElementAt(0).Value;
//再次去解析josn
//把object再次转换成json格式
                JavaScriptSerializer jsonser = new JavaScriptSerializer();
                string myJson = jsonser.Serialize(a);
                Dictionary<string, object> json1 = (Dictionary<string, object>)serializer.DeserializeObject(myJson);
//添加到数据库中
                foreach (var item in json1)
                {
                    string date_str = firstKey + item.Key;
                    DateTime dt1 = Convert.ToDateTime(date_str.Substring(0, 4) + "-" + date_str.Substring(4, 2) + "-" + date_str.Substring(6, 2));
                    var existData = model.FirstOrDefault(o => o.OffDate == dt1);
                    if (existData==null)
                    {
                        OffDay one = new OffDay();
                        one.OffDayId = Guid.NewGuid();
                        one.OffDate = dt1;
                        LTDB.OffDay.InsertOnSubmit(one);
                        LTDB.SubmitChanges();
                    }
                }


            }

 

可以把这个封装成一个函数,通过hangfire定时调用,添加owin类

 public void Configuration(IAppBuilder app)
        {
            // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
            //运用SqlServer存储,对应web.config中的connectionStrings中的name
            GlobalConfiguration.Configuration.UseSqlServerStorage("LTHRConnectionString");

            //RecurringJob.AddOrUpdate<TaskRun>(x => x.RiLi(), "* 00 * * *", TimeZoneInfo.Local);//  每分钟执行一次
            RecurringJob.AddOrUpdate<TaskRun>(x => x.RiLi(),Cron.Monthly);//  每月执行一次

            
           

            app.UseHangfireDashboard("/hangfire", new DashboardOptions
            {
                Authorization = new[] { new MyAuthorizationFilter() }
            });
            app.UseHangfireServer();//开始使用Hangfire服务
        }
public class MyAuthorizationFilter : IDashboardAuthorizationFilter
    {
        public bool Authorize(DashboardContext context)
        {
            return true;
        }
    }

hangfire调用的地址是:http://localhost:8340/hangfire可以看到你定时计划,hangfire可以去百度查询一下文献,很好理解的。