xyc
昨天 2f3c2d2851f7693357feb9e62a56a64670755c6a
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
package com.trafficaudit.holiday.controller;
 
import com.trafficaudit.common.Result;
import com.trafficaudit.holiday.entity.HolidayVehicleAdjustment;
import com.trafficaudit.holiday.service.HolidayVehicleAdjustmentService;
import com.trafficaudit.security.utils.JwtUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
 
@RestController
@RequestMapping("/api/holiday/adjustments")
public class HolidayVehicleAdjustmentController {
 
    @Resource
    private HolidayVehicleAdjustmentService adjustmentService;
    @Resource
    private JwtUtils jwtUtils;
 
    @GetMapping
    public Result<List<HolidayVehicleAdjustment>> list(@RequestParam(value = "year", required = false) Integer year) {
        return Result.ok(adjustmentService.list(year));
    }
 
    @GetMapping("/source-value")
    public Result<BigDecimal> sourceValue(@RequestParam("year") Integer year,
                                         @RequestParam("flowDate") String flowDate) {
        try {
            return Result.ok(adjustmentService.findOriginalValue(year, LocalDate.parse(flowDate)));
        } catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
    }
 
    @PostMapping("/save")
    public Result<HolidayVehicleAdjustment> save(@RequestHeader(value = "Authorization", required = false) String auth,
                                                 @RequestBody Map<String, Object> params) {
        if (!isAdmin(auth)) return Result.error(403, "仅 admin 可以维护往年国省道数据调整");
        try {
            return Result.ok(adjustmentService.save(params));
        } catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
    }
 
    @PostMapping("/delete")
    public Result<Void> delete(@RequestHeader(value = "Authorization", required = false) String auth,
                               @RequestParam("id") Long id) {
        if (!isAdmin(auth)) return Result.error(403, "仅 admin 可以维护往年国省道数据调整");
        try {
            adjustmentService.delete(id);
            return Result.ok();
        } catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
    }
 
    private boolean isAdmin(String auth) {
        return "admin".equals(jwtUtils.resolveUsername(auth));
    }
}