api獲取國家休息日 並填寫到數據庫中

/最近要獲取節假日,並需要保存在數據中做考勤計算,爲了不手動操作就用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可以去百度查詢一下文獻,很好理解的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章