@Configuration
public class RestClientConfig {
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
package com.htf.damp.service.impl;
import com.htf.damp.mapper.HolidayMapper;
import com.htf.damp.model.DayHodilyDetail;
import com.htf.damp.model.MonthHodilyDetail;
import com.htf.damp.model.YearHodilyResponse;
import com.htf.damp.model.po.HolidayPO;
import com.htf.damp.service.HolidayService;
import com.htf.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Calendar;
import java.util.List;
@Service
@Slf4j
public class HolidayServiceImpl implements HolidayService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private HolidayMapper holidayMapper;
private final static String HOLIDAY_API_URL = "https://www.mxnzp.com/api/holiday/list/year/%s?app_id=fpkvnbgtxxwcjmml&app_secret=eFBocExXRnZySTY4dElTUWxQZWVCQT09";
@Override
public String syncHolidayData() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
String holidayApiUrl = String.format(HOLIDAY_API_URL,year);
YearHodilyResponse responce = restTemplate.getForObject(holidayApiUrl,YearHodilyResponse.class);
holidayDataMaintain(responce);
return null;
}
public void holidayDataMaintain(YearHodilyResponse response) {
if (null == response || CollectionUtils.isEmpty(response.getData())) {
log.warn("未获取到节假日数据或解析节假日数据失败");
return;
}
List dayHodilyDetails;
for (MonthHodilyDetail monthHodilyDetail : response.getData()) {
dayHodilyDetails = monthHodilyDetail.getDays();
if (CollectionUtils.isEmpty(dayHodilyDetails)) {
continue;
}
for (DayHodilyDetail dayHodilyDetail : dayHodilyDetails) {
log.info("初始化日期={}", dayHodilyDetail.getDate());
HolidayPO holidayPO = buildHolidayPO(monthHodilyDetail, dayHodilyDetail);
List existPO = holidayMapper.selectByDate(dayHodilyDetail.getDate());
if (CollectionUtils.isEmpty(existPO)) {
holidayMapper.insert(holidayPO);
} else {
holidayMapper.updateByDate(holidayPO);
}
}
}
}
private HolidayPO buildHolidayPO(MonthHodilyDetail monthHodilyDetail, DayHodilyDetail dayHodilyDetail) {
HolidayPO holidayPO = new HolidayPO();
holidayPO.setTheDate(dayHodilyDetail.getDate());
holidayPO.setTheYear(monthHodilyDetail.getYear());
holidayPO.setTheMonth(monthHodilyDetail.getMonth());
holidayPO.setDateType(dayHodilyDetail.getType());
holidayPO.setTypeDesc(dayHodilyDetail.getTypeDes());
holidayPO.setWeekday(dayHodilyDetail.getWeekDay());
holidayPO.setDayOfYear(dayHodilyDetail.getDayOfYear());
holidayPO.setWeekOfYear(dayHodilyDetail.getWeekOfYear());
return holidayPO;
}
}
package com.htf.damp.model.po;
import lombok.Data;
@Data
public class HolidayPO extends BasePo{
private String theDate;
private String theYear;
private String theMonth;
private Integer dateType;
private String typeDesc;
private Integer weekday;
private Integer dayOfYear;
private Integer weekOfYear;
private Integer isQuality;
private String qualityUserId;
private Integer isPublish;
private Integer status;
}
@Data
public class YearHodilyResponse {
private Integer code;
private String msg;
private List data;
}
@Data
public class MonthHodilyDetail implements Serializable {
private static final long serialVersionUID = 3451474789401838239L;
private String month;
private String year;
private List days;
}
package com.htf.damp.model;
import java.io.Serializable;
import lombok.Data;
@Data
public class DayHodilyDetail implements Serializable {
private static final long serialVersionUID = 853431153574675841L;
private String date;
private Integer weekDay;
private String yearTips;
private Integer type;
private String typeDes;
private String chineseZodiac;
private String solarTerms;
private String avoid;
private String lunarCalendar;
private String suit;
private Integer dayOfYear;
private Integer weekOfYear;
private String constellation;
}
package com.htf.damp.mapper;
import com.htf.damp.model.po.HolidayPO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.math.BigDecimal;
import java.util.List;
@Mapper
public interface HolidayMapper {
int deleteByPrimaryKey(BigDecimal id);
int insert(HolidayPO record);
int insertSelective(HolidayPO record);
HolidayPO selectByPrimaryKey(BigDecimal id);
int updateByPrimaryKeySelective(HolidayPO record);
int updateByPrimaryKey(HolidayPO record);
List selectByDate(@Param(value = "theDate") String theDate);
void updateByDate(HolidayPO holidayPO);
List selectByDateType(@Param(value = "dateType") Integer theDate);
@Select("select * from T_DAMP_QUALITY_HOLIDAY where DATE_TYPE != 0")
List selectNotWeekdays();
}
ID, THE_DATE, THE_YEAR, THE_MONTH, DATE_TYPE, TYPE_DESC, WEEKDAY, DAY_OF_YEAR, WEEK_OF_YEAR, IS_QUALITY, QUALITY_USER_ID, IS_PUBLISH, STATUS, CREATE_USER_ID, CREATE_USER_NAME, CREATE_TIME, UPDATE_USER_ID, UPDATE_USER_NAME, UPDATE_TIME, IS_DELETE
orcal表设计:
-- ----------------------------
-- Table structure for T_DAMP_QUALITY_HOLIDAY
-- ----------------------------
DROP TABLE "HTFUTURES"."T_DAMP_QUALITY_HOLIDAY";
CREATE TABLE "HTFUTURES"."T_DAMP_QUALITY_HOLIDAY" (
"ID" NVARCHAr2(32) VISIBLE NOT NULL,
"THE_DATE" NVARCHAr2(10) VISIBLE,
"THE_YEAR" NVARCHAr2(4) VISIBLE,
"THE_MONTH" NVARCHAr2(2) VISIBLE,
"DATE_TYPE" NUMBER(1,0) VISIBLE,
"TYPE_DESC" NVARCHAr2(50) VISIBLE,
"WEEKDAY" NUMBER(1,0) VISIBLE,
"DAY_OF_YEAR" NUMBER(3,0) VISIBLE,
"WEEK_OF_YEAR" NUMBER(2,0) VISIBLE,
"IS_QUALITY" NUMBER(1,0) VISIBLE,
"QUALITY_USER_ID" NVARCHAr2(50) VISIBLE,
"IS_PUBLISH" NUMBER(1,0) VISIBLE DEFAULT 1,
"STATUS" NUMBER(1,0) VISIBLE,
"CREATE_USER_ID" NVARCHAr2(50) VISIBLE,
"CREATE_USER_NAME" NVARCHAr2(50) VISIBLE,
"CREATE_TIME" DATE VISIBLE,
"UPDATE_USER_ID" NVARCHAr2(50) VISIBLE,
"UPDATE_USER_NAME" NVARCHAr2(50) VISIBLE,
"UPDATE_TIME" DATE VISIBLE,
"IS_DELETE" NUMBER(1,0) VISIBLE DEFAULT 1
)
LOGGING
NOCOMPRESS
PCTFREE 10
INITRANS 1
STORAGE (
INITIAL 163840
NEXT 1048576
MINEXTENTS 1
MAXEXTENTS 2147483645
BUFFER_POOL DEFAULT
)
PARALLEL 1
NOCACHE
DISABLE ROW MOVEMENT
;
-- ----------------------------
-- Primary Key structure for table T_DAMP_QUALITY_HOLIDAY
-- ----------------------------
ALTER TABLE "HTFUTURES"."T_DAMP_QUALITY_HOLIDAY" ADD CONSTRAINT "SYS_C00342809" PRIMARY KEY ("ID");
-- ----------------------------
-- Checks structure for table T_DAMP_QUALITY_HOLIDAY
-- ----------------------------
ALTER TABLE "HTFUTURES"."T_DAMP_QUALITY_HOLIDAY" ADD CONSTRAINT "SYS_C00342808" CHECK ("ID" IS NOT NULL) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;



