前言
业务需求,需要判断节假日。
简单来说,我们有一个节假日日历即可解决,但是市面上 百度 小米 华为都有自己的日历但是就是没有API,我的需求是找一个稳定,高效,准确的日历
以下调用均为CURL调用,官方支持SDK调用,考虑到成本,使用CURL
我是为了加班费计算(节假日、补班)需要用到节假日的,有相同需求的可以看这篇
预期
开发
日历类总链接,有三个API
根据自己的需求用对应的API,我要知道节假日及其放假安排,所以调用第二个接口
参考链接
https://www.kancloud.cn/topthink-doc/think-api/1861639
调用须知
调用前要登陆 获取登陆密钥 AppCode
https://docs.topthink.com/think-api/1835086
1、查询当天详细信息
接口
GET https://api.topthink.com/calendar/day
调用
https://api.topthink.com/calendar/day?appCode=写自己的&date=2021-5-1
响应
{
"code": 0,
"message": "Success",
"data": {
"animalsYear": "牛",
"weekday": "星期六",
"lunarYear": "辛丑年",
"lunar": "三月二十",
"year-month": "2021-5",
"date": "2021-5-1",
"suit": "结婚.领证.订婚.求嗣.修坟.赴任.祈福.祭祀.纳畜.启钻.捕捉.嫁娶.纳采.盖屋.栽种.斋醮.招赘.纳婿.藏宝",
"avoid": "搬家.装修.开业.入宅.开工.动土.出行.安葬.上梁.开张.旅游.破土.修造.开市.纳财.移徙.立券.竖柱.放水.分居.行丧.开仓.置产.筑堤.出货",
"holiday": "劳动节",
"desc": "5月1日至5日放假调休,共5天。4月25日(星期日)、5月8日(星期六)上班"
}
}
2、根据传入的月份返回近期的假期列表
接口
POST https://api.topthink.com/calendar/month
调用
{
"yearMonth":"2021-5"
}
响应
接口查询一个月会返回两个月数据
观察了下发现"status": "1"
是放假 "status": "2"
是工作日(补班)
{
"code": 0,
"message": "Success",
"data": {
"year": "2021",
"year-month": "2021-5",
"holiday": "[{\"desc\":\"4月3日至5日放假调休,共3天\",\"festival\":\"2021-4-4\",\"list\":[{\"date\":\"2021-4-3\",\"status\":\"1\"},{\"date\":\"2021-4-4\",\"status\":\"1\"},{\"date\":\"2021-4-5\",\"status\":\"1\"}],\"list#num#\":3,\"name\":\"清明节\",\"rest\":\"\"},{\"desc\":\"5月1日至5日放假调休,共5天。4月25日(星期日)、5月8日(星期六)上班\",\"festival\":\"2021-5-1\",\"list\":[{\"date\":\"2021-5-1\",\"status\":\"1\"},{\"date\":\"2021-5-2\",\"status\":\"1\"},{\"date\":\"2021-5-3\",\"status\":\"1\"},{\"date\":\"2021-5-4\",\"status\":\"1\"},{\"date\":\"2021-5-5\",\"status\":\"1\"},{\"date\":\"2021-4-25\",\"status\":\"2\"},{\"date\":\"2021-5-8\",\"status\":\"2\"}],\"list#num#\":7,\"name\":\"劳动节\",\"rest\":\"\"},{\"desc\":\"6月12日至14日放假,共3天\",\"festival\":\"2021-6-14\",\"list\":[{\"date\":\"2021-6-12\",\"status\":\"1\"},{\"date\":\"2021-6-13\",\"status\":\"1\"},{\"date\":\"2021-6-14\",\"status\":\"1\"}],\"list#num#\":3,\"name\":\"端午节\",\"rest\":\"\"}]",
"holiday_array": [
{
"desc": "4月3日至5日放假调休,共3天",
"festival": "2021-4-4",
"list": [
{
"date": "2021-4-3",
"status": "1"
},
{
"date": "2021-4-4",
"status": "1"
},
{
"date": "2021-4-5",
"status": "1"
}
],
"list#num#": 3,
"name": "清明节",
"rest": "",
"list_num": 3
},
{
"desc": "5月1日至5日放假调休,共5天。4月25日(星期日)、5月8日(星期六)上班",
"festival": "2021-5-1",
"list": [
{
"date": "2021-5-1",
"status": "1"
},
{
"date": "2021-5-2",
"status": "1"
},
{
"date": "2021-5-3",
"status": "1"
},
{
"date": "2021-5-4",
"status": "1"
},
{
"date": "2021-5-5",
"status": "1"
},
{
"date": "2021-4-25",
"status": "2"
},
{
"date": "2021-5-8",
"status": "2"
}
],
"list#num#": 7,
"name": "劳动节",
"rest": "",
"list_num": 7
},
{
"desc": "6月12日至14日放假,共3天",
"festival": "2021-6-14",
"list": [
{
"date": "2021-6-12",
"status": "1"
},
{
"date": "2021-6-13",
"status": "1"
},
{
"date": "2021-6-14",
"status": "1"
}
],
"list#num#": 3,
"name": "端午节",
"rest": "",
"list_num": 3
}
]
}
}
3、根据传入年份返回当年假期列表
接口
此接口只能查询节假日具体那一天,不可以查看具体放几天,以及补班
POST https://api.topthink.com/calendar/year
调用
{
"year":"2021"
}
响应
{
"code": 0,
"message": "Success",
"data": {
"year": "2021",
"holidaylist": "[{\"name\":\"元旦\",\"startday\":\"2021-1-1\"},{\"name\":\"除夕\",\"startday\":\"2021-2-11\"},{\"name\":\"春节\",\"startday\":\"2021-2-12\"},{\"name\":\"清明节\",\"startday\":\"2021-4-4\"},{\"name\":\"劳动节\",\"startday\":\"2021-5-1\"},{\"name\":\"端午节\",\"startday\":\"2021-6-14\"},{\"name\":\"中秋节\",\"startday\":\"2021-9-21\"},{\"name\":\"国庆节\",\"startday\":\"2021-10-1\"}]",
"holiday_list": [
{
"name": "元旦",
"startday": "2021-1-1"
},
{
"name": "除夕",
"startday": "2021-2-11"
},
{
"name": "春节",
"startday": "2021-2-12"
},
{
"name": "清明节",
"startday": "2021-4-4"
},
{
"name": "劳动节",
"startday": "2021-5-1"
},
{
"name": "端午节",
"startday": "2021-6-14"
},
{
"name": "中秋节",
"startday": "2021-9-21"
},
{
"name": "国庆节",
"startday": "2021-10-1"
}
]
}
}
解析
拿到数据,我们把需要的节假日 工作日可以存储在数据库中
1、SQL
create table holiday_array
(
holiday_date date not null comment '节假日准确日期' primary key,
`desc` varchar(256) null comment '节假日备注',
status int default 1 not null comment '是否为工作日(补班)'
)
comment '节假日数组';
2、Bean
@Table(name = "holiday_array")
public class Holiday {
@Id
private Date holidayDate;
private String desc;
private Integer status;
public Date getHolidayDate() {
return holidayDate;
}
public void setHolidayDate(Date holidayDate) {
this.holidayDate = holidayDate;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
3、mapper
@Repository
public interface HolidayArrayMapper extends Mapper<Holiday> {
//区间查询
@Select("select count(1) form holiday_array where holiday_date between #{start} and #{end} ")
int findHolidayByDate(@Param("start") String start,@Param("end") String end);
//单日查询
@Select("select count(1) from holiday_array where holiday_date =#{date} ")
int findHolidayByDate2(@Param("date") String date);
//单日查询 当天是否为节假日
@Select("select status from holiday_array where holiday_date =#{date}")
Integer findHolidayByDate3(@Param("date") String date);
//批量插入
@Insert({
"<script>",
"insert into holiday_array(holiday_date,`desc`,status) values ",
"<foreach collection='recordList' item='item' index='index' separator=','>",
"(#{item.holidayDate}, #{item.desc}, #{item.status})"+ "</foreach>",
"</script>"
})
int insertHoliday(@Param("recordList") List<Holiday> recordList );
}
4、响应体解析
//存储库
@Autowired
private HolidayArrayMapper holidayArrayMapper;
//使用spring自带模板发送 需要代理的可以看https://blog.csdn.net/HezhezhiyuLe/article/details/112177771
@Autowired
private RestTemplate restTemplate;
/**
* 每月更新节假日 每月一号执行
*/
@Scheduled(cron = "0 0 2 1 * ? *")
public void holiday(){
String ny = DateUtil.getNY();
Map<String, String> map = new HashMap<>();
map.put("yearMonth",ny);
List<Holiday> holidays = new ArrayList<>();
try {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", appCode);
HttpEntity<Map> requestEntity = new HttpEntity<>(map, headers);
String post =restTemplate.postForEntity(holidayUrl,requestEntity,String.class).getBody();
if(!IFNull.strNotNull(post)||!post.contains("\"code\":0")){
return;
}
map.clear();
JSONObject holidayJson = JSON.parseObject(post);
JSONObject data = holidayJson.getJSONObject("data");
List<JSONObject> holiday_array = JSON.parseArray(data.getString("holiday_array"), JSONObject.class);
for (JSONObject holiday : holiday_array) {
List<JSONObject> list = JSON.parseArray(holiday.getString("list"), JSONObject.class);
for (JSONObject jsonObject : list) {
String date = jsonObject.getString("date");
String status = jsonObject.getString("status");
Holiday holiday1 = new Holiday();
if("1".equals(status)){
//节假日日期:名称
holiday1.setHolidayDate(DateUtil.parseDate(date));
holiday1.setDesc(holiday.getString("name"));
holiday1.setStatus(1);
}else {
//调休工作日时间:备注
holiday1.setHolidayDate(DateUtil.parseDate(date));
holiday1.setDesc(holiday.getString("desc"));
holiday1.setStatus(2);
}
int holidayByDate = holidayArrayMapper.findHolidayByDate2(date);
if(holidayByDate==0){
holidays.add(holiday1);
}else {
System.out.println("重复节假日"+holiday1);
}
}
}
} catch (Exception e) {
e.printStackTrace();
return;
}
//存储
if(holidays.size()>0){
int i = holidayArrayMapper.insertHoliday(holidays);
System.out.println(i);
}
}
效果图
番外
感谢ThinkAPI
维护者答疑
有任何接口问题的可以留言,或者加官方答疑QQ群:375558052