xyc
昨天 e92968f85531df814268b6afbe62f6e062f5a07f
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
package com.trafficaudit.holiday.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.trafficaudit.holiday.HolidayNumberUtils;
import com.trafficaudit.holiday.entity.HolidayVehicleAdjustment;
import com.trafficaudit.holiday.mapper.HolidayVehicleAdjustmentMapper;
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 HolidayVehicleAdjustmentService {
 
    private static final int REASON_MAX_LENGTH = 500;
 
    @Resource
    private HolidayVehicleAdjustmentMapper adjustmentMapper;
 
    public List<HolidayVehicleAdjustment> list(Integer year) {
        return adjustmentMapper.selectList(new LambdaQueryWrapper<HolidayVehicleAdjustment>()
                .eq(year != null, HolidayVehicleAdjustment::getYear, year)
                .orderByDesc(HolidayVehicleAdjustment::getYear)
                .orderByAsc(HolidayVehicleAdjustment::getFlowDate));
    }
 
    @Transactional
    public HolidayVehicleAdjustment save(Map<String, Object> params) {
        Long id = longValue(params.get("id"));
        Integer year = intValue(params.get("year"));
        LocalDate flowDate = dateValue(params.get("flowDate"));
        BigDecimal adjustedValue = decimalValue(params.get("adjustedValue"));
        String reason = textValue(params.get("reason"));
 
        if (year == null || year < 2000 || year > 2100) {
            throw new IllegalArgumentException("年份不正确");
        }
        if (year >= LocalDate.now().getYear()) {
            throw new IllegalArgumentException("往年国省道数据调整仅维护历史年份");
        }
        if (flowDate == null || flowDate.getYear() != year) {
            throw new IllegalArgumentException("日期必须属于所选年份");
        }
        if (adjustedValue == null || adjustedValue.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("调整后的国省道车流量不能小于 0");
        }
        if (reason.isEmpty()) {
            throw new IllegalArgumentException("调整原因不能为空");
        }
        if (reason.length() > REASON_MAX_LENGTH) {
            throw new IllegalArgumentException("调整原因不能超过 " + REASON_MAX_LENGTH + " 个字符");
        }
 
        HolidayVehicleAdjustment sameKey = adjustmentMapper.selectOne(
                new LambdaQueryWrapper<HolidayVehicleAdjustment>()
                        .eq(HolidayVehicleAdjustment::getYear, year)
                        .eq(HolidayVehicleAdjustment::getFlowDate, flowDate)
                        .last("limit 1"));
        HolidayVehicleAdjustment entity;
        if (id == null) {
            entity = sameKey == null ? new HolidayVehicleAdjustment() : sameKey;
        } else {
            entity = adjustmentMapper.selectById(id);
            if (entity == null) {
                throw new IllegalArgumentException("调整记录不存在");
            }
            if (sameKey != null && !sameKey.getId().equals(id)) {
                throw new IllegalArgumentException("该年份和日期已存在调整记录");
            }
        }
 
        if (entity.getId() == null) {
            entity.setCreatedAt(LocalDateTime.now());
        }
        entity.setYear(year);
        entity.setFlowDate(flowDate);
        entity.setAdjustedValue(HolidayNumberUtils.normalize(adjustedValue));
        entity.setReason(reason);
        entity.setUpdatedAt(LocalDateTime.now());
        if (entity.getId() == null) {
            adjustmentMapper.insert(entity);
        } else {
            adjustmentMapper.updateById(entity);
        }
        return entity;
    }
 
    @Transactional
    public void delete(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("缺少记录 id");
        }
        adjustmentMapper.deleteById(id);
    }
 
    private Long longValue(Object value) {
        if (value == null || value.toString().trim().isEmpty()) return null;
        return Long.valueOf(value.toString().trim());
    }
 
    private Integer intValue(Object value) {
        if (value == null || value.toString().trim().isEmpty()) return null;
        return Integer.valueOf(value.toString().trim());
    }
 
    private BigDecimal decimalValue(Object value) {
        if (value == null || value.toString().trim().isEmpty()) return null;
        return HolidayNumberUtils.decimalValue(value);
    }
 
    private LocalDate dateValue(Object value) {
        if (value == null || value.toString().trim().isEmpty()) return null;
        return LocalDate.parse(value.toString().trim());
    }
 
    private String textValue(Object value) {
        return value == null ? "" : value.toString().trim();
    }
}