严智鑫
2025-03-21 f1fdf0c1e332227dd41c8d85411b2edc9744b65e
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
package com.mes.energy.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mes.energy.entity.EnergyConsumption;
import com.mes.energy.service.EnergyConsumptionService;
import com.mes.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Api(tags = "能源管理")
@RestController
@RequestMapping("/energy/consumption")
public class EnergyConsumptionController {
 
    @Autowired
    private EnergyConsumptionService energyService;
 
    @ApiOperation("获取能耗数据列表")
    @PostMapping("/listEnergy")
    @ResponseBody
    public Result list(@RequestBody Map<String, Object> params) {
        Integer page = params.get("page") == null ? 1 : (Integer) params.get("page");
        Integer pageSize = params.get("pageSize") == null ? 10 : (Integer) params.get("pageSize");
        
        Page<EnergyConsumption> pageData = energyService.page(
            new Page<>(page, pageSize),
            new QueryWrapper<EnergyConsumption>().orderByDesc("record_date")
        );
        
        return Result.build(200, "查询成功", pageData);
    }
 
    @ApiOperation("添加能耗数据")
    @PostMapping("/addEnergy")
    @ResponseBody
    public Result add(@RequestBody EnergyConsumption consumption) {
        consumption.setCreatedAt(new Date());
        consumption.setUpdatedAt(new Date());
        int count = energyService.save(consumption) ? 1 : 0;
        String message = count > 0 ? "添加成功:" + count : "添加失败!";
        return Result.build(200, message, count);
    }
 
    @ApiOperation("修改能耗数据")
    @PostMapping("/updateEnergy")
    @ResponseBody
    public Result update(@RequestBody EnergyConsumption consumption) {
        consumption.setUpdatedAt(new Date());
        int count = energyService.updateById(consumption) ? 1 : 0;
        String message = count > 0 ? "修改成功:" + count : "修改失败!";
        energyService.notifyEnergyUpdate(consumption);
        return Result.build(200, message, count);
    }
 
    @ApiOperation("删除能耗数据")
    @PostMapping("/deleteEnergy")
    @ResponseBody
    public Result delete(@RequestBody EnergyConsumption consumption) {
        int count = energyService.removeById(consumption.getId()) ? 1 : 0;
        String message = count > 0 ? "删除成功:" + count : "删除失败!";
        return Result.build(200, message, count);
    }
 
    @ApiOperation("获取图表数据")
    @PostMapping("/chartEnergy")
    @ResponseBody
    public Result getChartData() {
        List<EnergyConsumption> data = energyService.list(
            new QueryWrapper<EnergyConsumption>()
                .orderByAsc("record_date")
                .last("limit 30")
        );
 
        Map<String, Object> result = new HashMap<>();
        result.put("actual", data);
        result.put("planned", null);
 
        return Result.build(200, "查询成功", result);
    }