package com.trafficaudit.holiday.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.trafficaudit.holiday.HolidayConstants; import com.trafficaudit.holiday.entity.HolidayCalendar; import com.trafficaudit.holiday.entity.HolidayPassengerCoefficient; import com.trafficaudit.holiday.mapper.HolidayCalendarMapper; import com.trafficaudit.holiday.mapper.HolidayPassengerCoefficientMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; import java.util.Map; @Service public class HolidayConfigService { @Resource private HolidayCalendarMapper holidayCalendarMapper; @Resource private HolidayPassengerCoefficientMapper coefficientMapper; public List listHolidays(Integer year, String holidayType) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper() .eq(year != null, HolidayCalendar::getYear, year) .eq(holidayType != null && !holidayType.trim().isEmpty(), HolidayCalendar::getHolidayType, holidayType) .orderByAsc(HolidayCalendar::getYear) .orderByAsc(HolidayCalendar::getHolidayType) .orderByAsc(HolidayCalendar::getDayIndex); return holidayCalendarMapper.selectList(wrapper); } public List findHolidayDays(int year, String holidayType) { return holidayCalendarMapper.selectList(new LambdaQueryWrapper() .eq(HolidayCalendar::getYear, year) .eq(HolidayCalendar::getHolidayType, holidayType) .orderByAsc(HolidayCalendar::getDayIndex)); } public HolidayCalendar findHolidayByDate(LocalDate date) { return holidayCalendarMapper.selectOne(new LambdaQueryWrapper() .eq(HolidayCalendar::getHolidayDate, date) .orderByAsc(HolidayCalendar::getDayIndex) .last("limit 1")); } @Transactional public HolidayCalendar saveHoliday(Map params) { Long id = longValue(params.get("id")); Integer year = intValue(params.get("year")); String holidayType = stringValue(params.get("holidayType")); LocalDate holidayDate = dateValue(params.get("holidayDate")); Integer dayIndex = intValue(params.get("dayIndex")); if (year == null || year < 2000 || year > 2100) { throw new IllegalArgumentException("年份不正确"); } if (!HolidayConstants.isValidHolidayType(holidayType)) { throw new IllegalArgumentException("节假日分类不正确"); } if (holidayDate == null || holidayDate.getYear() != year) { throw new IllegalArgumentException("日期必须属于所选年份"); } if (dayIndex == null || dayIndex < 1) { throw new IllegalArgumentException("第几日必须从 1 开始"); } LambdaQueryWrapper duplicate = new LambdaQueryWrapper() .eq(HolidayCalendar::getYear, year) .eq(HolidayCalendar::getHolidayType, holidayType) .and(w -> w.eq(HolidayCalendar::getHolidayDate, holidayDate) .or().eq(HolidayCalendar::getDayIndex, dayIndex)); if (id != null) { duplicate.ne(HolidayCalendar::getId, id); } if (holidayCalendarMapper.selectCount(duplicate) > 0) { throw new IllegalArgumentException("同一节假日下的日期或第几日已存在"); } HolidayCalendar entity = id == null ? new HolidayCalendar() : holidayCalendarMapper.selectById(id); if (entity == null) { throw new IllegalArgumentException("节假日记录不存在"); } if (id == null) { entity.setCreatedAt(LocalDateTime.now()); } entity.setYear(year); entity.setHolidayType(holidayType); entity.setHolidayDate(holidayDate); entity.setDayIndex(dayIndex); entity.setUpdatedAt(LocalDateTime.now()); if (id == null) { holidayCalendarMapper.insert(entity); } else { holidayCalendarMapper.updateById(entity); } return entity; } @Transactional public void deleteHoliday(Long id) { if (id == null) { throw new IllegalArgumentException("缺少记录 id"); } holidayCalendarMapper.deleteById(id); } public List listCoefficients(Integer year) { return coefficientMapper.selectList(new LambdaQueryWrapper() .eq(year != null, HolidayPassengerCoefficient::getYear, year) .orderByAsc(HolidayPassengerCoefficient::getYear) .orderByAsc(HolidayPassengerCoefficient::getDateCategory)); } public BigDecimal coefficientOf(int year, String dateCategory) { HolidayPassengerCoefficient row = coefficientMapper.selectOne( new LambdaQueryWrapper() .eq(HolidayPassengerCoefficient::getYear, year) .eq(HolidayPassengerCoefficient::getDateCategory, dateCategory) .last("limit 1")); return row == null ? null : row.getCoefficient(); } @Transactional public HolidayPassengerCoefficient saveCoefficient(Map params) { Long id = longValue(params.get("id")); Integer year = intValue(params.get("year")); String dateCategory = stringValue(params.get("dateCategory")); BigDecimal coefficient = decimalValue(params.get("coefficient")); if (year == null || year < 2000 || year > 2100) { throw new IllegalArgumentException("年份不正确"); } if (!HolidayConstants.isValidDateCategory(dateCategory)) { throw new IllegalArgumentException("日期分类不正确"); } if (coefficient == null || coefficient.compareTo(BigDecimal.ZERO) <= 0) { throw new IllegalArgumentException("客运系数必须大于 0"); } LambdaQueryWrapper duplicate = new LambdaQueryWrapper() .eq(HolidayPassengerCoefficient::getYear, year) .eq(HolidayPassengerCoefficient::getDateCategory, dateCategory); if (id != null) { duplicate.ne(HolidayPassengerCoefficient::getId, id); } if (coefficientMapper.selectCount(duplicate) > 0) { throw new IllegalArgumentException("该年份和日期分类的客运系数已存在"); } HolidayPassengerCoefficient entity = id == null ? new HolidayPassengerCoefficient() : coefficientMapper.selectById(id); if (entity == null) { throw new IllegalArgumentException("客运系数记录不存在"); } if (id == null) { entity.setCreatedAt(LocalDateTime.now()); } entity.setYear(year); entity.setDateCategory(dateCategory); entity.setCoefficient(coefficient); entity.setUpdatedAt(LocalDateTime.now()); if (id == null) { coefficientMapper.insert(entity); } else { coefficientMapper.updateById(entity); } return entity; } @Transactional public void deleteCoefficient(Long id) { if (id == null) { throw new IllegalArgumentException("缺少记录 id"); } coefficientMapper.deleteById(id); } private Long longValue(Object value) { if (value == null || value.toString().trim().isEmpty()) return null; return Long.valueOf(value.toString()); } private Integer intValue(Object value) { if (value == null || value.toString().trim().isEmpty()) return null; return Integer.valueOf(value.toString()); } private BigDecimal decimalValue(Object value) { if (value == null || value.toString().trim().isEmpty()) return null; return com.trafficaudit.holiday.HolidayNumberUtils.decimalValue(value); } private String stringValue(Object value) { return value == null ? null : value.toString().trim(); } private LocalDate dateValue(Object value) { if (value == null || value.toString().trim().isEmpty()) return null; return LocalDate.parse(value.toString()); } }