CREATE TABLE `activity_holiday_info` (
`holiday_id` int NOT NULL AUTO_INCREMENT,
`holiday_name` varchar(20) DEFAULT NULL COMMENT '节假日名称',
`holiday_time` date DEFAULT NULL COMMENT '节假日时间',
`remark` varchar(50) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`holiday_id`)
) ENGINE=InnoDB COMMENT='节假日';
/**
* @author qy
* 非工作日维护
*/
@RestController
@RequestMapping("/holidays")
@Slf4j
@Api(tags = "非工作日维护")
public class ActivitityHolidayController {
@Autowired
private ActivityHolidayInfoService activityHolidayInfoService;
/**
* 非工作日维护,周六周日按年份需初始化,特殊、节假日页面维护
*/
@ApiOperation(value = "按年份初始化周末")
@GetMapping(value = "/{year}/initDate")
@ApiImplicitParams({
@ApiImplicitParam(name = "year", dataType = "String", paramType = "path")
})
public Result<Boolean> initHolidays(@PathVariable String year) {
activityHolidayInfoService.initHolidays(year);
return Result.success(Boolean.TRUE);
}
/**
* 查询已录入系统的非工作日给前端,标记在日历📅中展示。
*/
@ApiOperation(value = "查询已录入系统的非工作日")
@GetMapping(value = "/{year}/list")
@ApiImplicitParams({
@ApiImplicitParam(name = "year", dataType = "String", paramType = "path")
})
public Result<List<ActivityHolidayInfo>> queryHolidays(@PathVariable String year) {
return Result.success(activityHolidayInfoService.queryHolidays(year));
}
/**
* 更新该年份的非工作日()
*/
@ApiOperation(value = "更新该年份的非工作日")
@PutMapping(value = "/{year}/list")
@ApiImplicitParams({
@ApiImplicitParam(name = "year", dataType = "String", paramType = "path")
})
public Result<Boolean> mergeHolidays(@RequestBody List<ActivityHolidayInfo> holidayInfos, @PathVariable String year) {
activityHolidayInfoService.mergeHolidays(holidayInfos, year);
return Result.success(Boolean.TRUE);
}
}
public interface ActivityHolidayInfoService extends IService<ActivityHolidayInfo> {
/**
* 初始化当年周六、周末
*/
void initHolidays(String year);
/**
* 查询已录入系统的非工作日
*/
List<ActivityHolidayInfo> queryHolidays(String year);
/**
* 更新该年份的非工作日
*/
void mergeHolidays(List<ActivityHolidayInfo> holidayInfos, String year);
}
@Service
public class ActivityHolidayInfoServiceImpl extends ServiceImpl<ActivityHolidayInfoMapper, ActivityHolidayInfo> implements ActivityHolidayInfoService {
/**
* 查询改年份已录入系统的非工作日
*/
@Override
public List<ActivityHolidayInfo> queryHolidays(String year) {
QueryWrapper<ActivityHolidayInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().likeRight(ActivityHolidayInfo::getHolidayTime, year);
return baseMapper.selectList(queryWrapper);
}
/**
* 更新该年份的非工作日
*/
@Override
public void mergeHolidays(List<ActivityHolidayInfo> holidayInfos, String year) {
remove(new QueryWrapper<ActivityHolidayInfo>()
.lambda()
.likeRight(ActivityHolidayInfo::getHolidayTime, year));
saveBatch(holidayInfos);
}
// 根据年份和月份获取当月的所有日期
public static List<String> getDayByMonth(int month, String year) {
List<String> data = new ArrayList<>();
try {
Calendar c = Calendar.getInstance();
// 获取当前的年份
// int year = c.get(Calendar.YEAR);
// 获取本月的总天数
int dayCount = c.getActualMaximum(Calendar.DAY_OF_MONTH);
// 定义时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 开始日期为当前年月拼接1号
Date startDate = sdf.parse(year + "-" + month + "-01");
// 结束日期为当前年月拼接该月最大天数
Date endDate = sdf.parse(year + "-" + month + "-" + dayCount);
// 设置calendar的开始日期
c.setTime(startDate);
// 当前时间小于等于设定的结束时间
while (c.getTime().compareTo(endDate) <= 0) {
String time = sdf.format(c.getTime());
data.add(time);
// 当前日期加1
c.add(Calendar.DATE, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return data;
}
/**
* 初始化当年周六、周末
*/
@Override
public void initHolidays(String year) {
try {
// 拿到当年中的所有日期
List<String> dateList = new ArrayList<>();
for (int i = 1; i <= 12; i++) {
dateList.addAll(getDayByMonth(i, year));
}
dateList = dateList.stream().distinct().collect(Collectors.toList());
// set 日期
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<ActivityHolidayInfo> activityHolidayInfo = new ArrayList<>();
// 添加当年所有日期数据
for (String date : dateList) {
ActivityHolidayInfo serviceDate = new ActivityHolidayInfo();
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse(date));
// index 值为 7 时 是周六 值为 1 时是末, 美国周六是一周的最后一天,周日是一周的最后一天
int index = calendar.get(Calendar.DAY_OF_WEEK);
if (index == 7) {
serviceDate.setHolidayName("周六");
serviceDate.setRemark("周末");
serviceDate.setHolidayTime(simpleDateFormat.parse(date));
activityHolidayInfo.add(serviceDate);
} else if (index == 1) {
serviceDate.setHolidayName("周日");
serviceDate.setRemark("周末");
serviceDate.setHolidayTime(simpleDateFormat.parse(date));
activityHolidayInfo.add(serviceDate);
}
}
if (!CollectionUtils.isEmpty(activityHolidayInfo)) {
//先删除该年份数据
remove(new QueryWrapper<ActivityHolidayInfo>()
.lambda()
.likeRight(ActivityHolidayInfo::getHolidayTime, year));
//再批量插入
saveBatch(activityHolidayInfo);
}
} catch (Exception e) {
log.error("日期初始化错误");
e.printStackTrace();
}
}
}
postman请求:http://x.x.x.x:8080/holidays/年份/initDate
数据库情况: