huang
2025-05-20 2c2413760b6467bf62402dba7338bd3bbcbd7341
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
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<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");
        String locationCode = (String) params.get("locationCode");
        
        QueryWrapper<Quantity> queryWrapper = new QueryWrapper<Quantity>().orderByDesc("record_time");
        if (StringUtils.hasText(locationCode)) {
            queryWrapper.eq("location_code", locationCode);
        }
        
        Page<Quantity> 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<String, Object> params) {
        String locationCode = params != null ? (String) params.get("locationCode") : null;
        Integer dayCount = params != null && params.get("dayCount") != null ? (Integer) params.get("dayCount") : null;
        
        QueryWrapper<Quantity> queryWrapper = new QueryWrapper<Quantity>().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<Quantity> data = quantityService.list(queryWrapper);
        
        return Result.build(200, "查询成功", data);
    }