package com.mes.quantity.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mes.utils.Result; import com.mes.quantity.entity.Quantity; import com.mes.quantity.service.QuantityService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; @Api(tags = "在制品数量接口") @RestController @RequestMapping("/quantity") public class QuantityController { @Autowired private QuantityService quantityService; @ApiOperation("获取在制品数量数据列表") @PostMapping("/listQuantity") @ResponseBody public Result listQuantity(@RequestBody Map params) { Integer page = params.get("page") == null ? 1 : (Integer) params.get("page"); Integer pageSize = params.get("pageSize") == null ? 10 : (Integer) params.get("pageSize"); String locationCode = (String) params.get("locationCode"); QueryWrapper queryWrapper = new QueryWrapper().orderByDesc("record_time"); if (StringUtils.hasText(locationCode)) { queryWrapper.eq("location_code", locationCode); } Page pageData = quantityService.page( new Page<>(page, pageSize), queryWrapper ); return Result.build(200, "查询成功", pageData); } @ApiOperation("添加在制品数量记录") @PostMapping("/addQuantity") @ResponseBody public Result addQuantity(@RequestBody Quantity quantity) { quantity.setCreateTime(new Date()); quantity.setUpdateTime(new Date()); boolean success = quantityService.save(quantity); int count = success ? 1 : 0; String message = count > 0 ? "添加成功:" + count : "添加失败!"; return Result.build(200, message, count); } @ApiOperation("修改在制品数量记录") @PostMapping("/updateQuantity") @ResponseBody public Result updateQuantity(@RequestBody Quantity quantity) { quantity.setUpdateTime(new Date()); boolean success = quantityService.updateById(quantity); int count = success ? 1 : 0; String message = count > 0 ? "修改成功:" + count : "修改失败!"; return Result.build(200, message, count); } @ApiOperation("删除在制品数量记录") @PostMapping("/deleteQuantity") @ResponseBody public Result deleteQuantity(@RequestBody Quantity quantity) { boolean success = quantityService.removeById(quantity.getId()); int count = success ? 1 : 0; String message = count > 0 ? "删除成功:" + count : "删除失败!"; return Result.build(200, message, count); } @ApiOperation("获取图表数据") @PostMapping("/chartQuantity") @ResponseBody public Result getChartData(@RequestBody(required = false) Map params) { String locationCode = params != null ? (String) params.get("locationCode") : null; Integer dayCount = params != null && params.get("dayCount") != null ? (Integer) params.get("dayCount") : null; QueryWrapper queryWrapper = new QueryWrapper().orderByAsc("record_time"); if (StringUtils.hasText(locationCode)) { queryWrapper.eq("location_code", locationCode); } if (dayCount != null && dayCount > 0) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, -dayCount); Date startDate = calendar.getTime(); queryWrapper.ge("record_time", startDate); } List data = quantityService.list(queryWrapper); return Result.build(200, "查询成功", data); } }