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);
|
}
|
}
|