liding
5 小时以前 56424206be7af38a02be17f215654f3ca813ebcb
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.ruoyi.sales.controller;
 
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.sales.dto.WeighingRecordExcelDto;
import com.ruoyi.sales.pojo.WeighingRecord;
import com.ruoyi.sales.service.IWeighingRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 过磅记录(磅单台账)Controller
 *
 * @author ruoyi
 * @date 2026-04-07
 */
@Api(tags = "磅单台账管理")
@RestController
@RequestMapping("/sales/weighbridgeLedger")
public class WeighingRecordController extends BaseController {
 
    @Autowired
    private IWeighingRecordService weighingRecordService;
 
    /**
     * 分页查询磅单台账列表
     */
    @ApiOperation("分页查询磅单台账列表")
    @GetMapping("/listPage")
    public R<IPage<WeighingRecord>> listPage(Page<WeighingRecord> page, WeighingRecord weighingRecord) {
        IPage<WeighingRecord> list = weighingRecordService.listPage(page, weighingRecord);
        return R.ok(list);
    }
 
    /**
     * 导入 Excel(新磅单台账)
     */
    @ApiOperation("导入Excel")
    @Log(title = "磅单台账", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R importWeighbridgeLedgerExcel(@RequestParam("file") MultipartFile file) throws Exception {
        try {
            List<WeighingRecordExcelDto> excelDataList = EasyExcel.read(file.getInputStream())
                    .head(WeighingRecordExcelDto.class)
                    .sheet()
                    .doReadSync();
 
            if (CollectionUtils.isEmpty(excelDataList)) {
                return R.fail("模板错误或导入数据为空");
            }
 
            // 转换为实体对象
            List<WeighingRecord> weighingRecordList = new ArrayList<>();
            for (WeighingRecordExcelDto dto : excelDataList) {
                WeighingRecord record = new WeighingRecord();
                BeanUtils.copyProperties(dto, record);
                weighingRecordList.add(record);
            }
 
            // 去重检查
            List<WeighingRecord> newRecordList = new ArrayList<>();
            int duplicateCount = 0;
 
            for (WeighingRecord record : weighingRecordList) {
                LambdaQueryWrapper<WeighingRecord> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(WeighingRecord::getSerialNo, record.getSerialNo());
 
                long count = weighingRecordService.count(wrapper);
                if (count == 0) {
                    newRecordList.add(record);
                } else {
                    duplicateCount++;
                }
            }
 
            if (newRecordList.isEmpty()) {
                return R.fail("所有数据都已存在,共" + duplicateCount + "条重复数据");
            }
 
            // 批量插入新数据
            weighingRecordService.saveBatch(newRecordList);
 
            String message = String.format("导入成功:%d条,跳过重复:%d条", newRecordList.size(), duplicateCount);
            return R.ok(message);
 
        } catch (Exception e) {
            return R.fail("导入失败:" + e.getMessage());
        }
    }
 
    /**
     * 删除(新磅单台账)
     */
    @ApiOperation("删除磅单台账")
    @Log(title = "磅单台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public R delWeighbridgeLedger(@RequestBody List<Long> ids) {
        boolean success = weighingRecordService.removeByIds(ids);
        if (success) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }
 
}