using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using HZQR.Common; using Newtonsoft.Json; using SuperMap.RealEstate.ServiceModel; using Business = SuperMap.RealEstate.Personnel.Storage.Business; namespace Personnel.WebSite.Shanxi { public class HolidayDateHelper { /// /// 请求接口查询节假日 /// /// 按日期范围查询(type=3&date=2020-11-1~2020-11-10) /// 按月查询(type=2&date=2020-10) /// public static Models.HolidayDate GetWorkDateByApi(int type, string date) { string key = "ce9ac01a3105e7babff09452fbd564c5"; //请求接口,写入数据库,2:按月模式 string url = "https://api.tianapi.com/txapi/jiejiari/index?key=" + key + "&mode=1&type=" + type + "&date=" + date; string jsonstr = HttpUtil.HttpUrlGet(url, 500); JsonSerializer serializer = new JsonSerializer(); StringReader sr = new StringReader(jsonstr); object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(Models.HolidayDate)); Models.HolidayDate holidayDate = obj as Models.HolidayDate; return holidayDate; } public static bool IsNeedSeachApi(Transaction _Transaction, string startDate, string endDate, ref int workDay, ref string searchMonth) { //先查询数据库 string sqlWhere = string.Format(@" WHERE TO_DATE(DATE_NAME,'yyyy-mm-dd')>=TO_DATE('{0}','yyyy-mm-dd') AND TO_DATE(DATE_NAME,'yyyy-mm-dd')<=TO_DATE('{1}','yyyy-mm-dd')", startDate, endDate); Business.DATE dateModel = new Business.DATE(_Transaction); List list = dateModel.FillCollection(sqlWhere); bool isSearchApi = false; string startMonth = Convert.ToDateTime(startDate).ToString("yyyy-MM"); string endMonth = Convert.ToDateTime(endDate).ToString("yyyy-MM"); if (list.Count == 0) { isSearchApi = true; if (startMonth == endMonth)//开始月份等于结束月份,请求一次接口即可 { searchMonth = startMonth;//查询1个月份 } return isSearchApi; } else { int diffMonth = Convert.ToDateTime(endDate).Month - Convert.ToDateTime(startDate).Month; for (int i = 0; i <= diffMonth; i++) { startMonth = Convert.ToDateTime(startDate).AddMonths(i).ToString("yyyy-MM"); if (list.Where(o => Convert.ToDateTime(o.DATE_NAME).ToString("yyyy-MM") == startMonth).Count() == 0) { isSearchApi = true; searchMonth += startMonth + ","; return isSearchApi; } searchMonth = searchMonth.TrimEnd(','); } workDay = list.Where(o => o.DATE_ISNOTWORK == 0).Count(); } return isSearchApi; } } }