xyc
7 小时以前 fbacc121df810f01e5b6ed43e6431dea6df270fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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<HolidayCalendar> listHolidays(Integer year, String holidayType) {
        LambdaQueryWrapper<HolidayCalendar> wrapper = new LambdaQueryWrapper<HolidayCalendar>()
                .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<HolidayCalendar> findHolidayDays(int year, String holidayType) {
        return holidayCalendarMapper.selectList(new LambdaQueryWrapper<HolidayCalendar>()
                .eq(HolidayCalendar::getYear, year)
                .eq(HolidayCalendar::getHolidayType, holidayType)
                .orderByAsc(HolidayCalendar::getDayIndex));
    }
 
    public HolidayCalendar findHolidayByDate(LocalDate date) {
        return holidayCalendarMapper.selectOne(new LambdaQueryWrapper<HolidayCalendar>()
                .eq(HolidayCalendar::getHolidayDate, date)
                .orderByAsc(HolidayCalendar::getDayIndex)
                .last("limit 1"));
    }
 
    @Transactional
    public HolidayCalendar saveHoliday(Map<String, Object> 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<HolidayCalendar> duplicate = new LambdaQueryWrapper<HolidayCalendar>()
                .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<HolidayPassengerCoefficient> listCoefficients(Integer year) {
        return coefficientMapper.selectList(new LambdaQueryWrapper<HolidayPassengerCoefficient>()
                .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<HolidayPassengerCoefficient>()
                        .eq(HolidayPassengerCoefficient::getYear, year)
                        .eq(HolidayPassengerCoefficient::getDateCategory, dateCategory)
                        .last("limit 1"));
        return row == null ? null : row.getCoefficient();
    }
 
    @Transactional
    public HolidayPassengerCoefficient saveCoefficient(Map<String, Object> 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<HolidayPassengerCoefficient> duplicate = new LambdaQueryWrapper<HolidayPassengerCoefficient>()
                .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());
    }
}