liyong
11 小时以前 74a29854bb8ff92d2ff2fcb55fa5028609ec6a33
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.ArrayList;
import java.util.List;
 
import static com.ruoyi.framework.datasource.DynamicDataSourceContextHolder.log;
 
/**
 * 过磅记录(磅单台账)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;
            int matchedCount = 0;
            int unmatchedCount = 0;
 
            for (WeighingRecord record : weighingRecordList) {
                LambdaQueryWrapper<WeighingRecord> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(WeighingRecord::getSerialNo, record.getSerialNo());
                if (ObjectUtils.isEmpty(record.getSpecification())) {
                    record.setSpecification("/");
                }
                long count = weighingRecordService.count(wrapper);
                if (count == 0) {
                    newRecordList.add(record);
                } else {
                    duplicateCount++;
                }
            }
 
            if (newRecordList.isEmpty()) {
                return R.fail("所有数据都已存在,共" + duplicateCount + "条重复数据");
            }
 
            // 批量插入新数据并执行出库操作
            for (WeighingRecord record : newRecordList) {
                // 先保存磅单记录,获取ID
                weighingRecordService.save(record);
 
 
                // 根据客户、产品名称、规格匹配销售台账并出库
                Long productId = weighingRecordService.matchAndOutStock(
                        record.getReceiveUnit(),      // 收货单位=客户
                        record.getGoodsName(),        // 货名=产品名称
                        record.getSpecification(),    // 规格型号
                        record.getNetWeight(),       // 净重=数量
                        record.getId()               // 磅单ID作为关联记录ID
                );
                if (productId != null) {
                    matchedCount++;
                } else {
                    unmatchedCount++;
                }
            }
 
            String message = String.format("导入成功:%d条,跳过重复:%d条,匹配出库:%d条,未匹配:%d条",
                    newRecordList.size(), duplicateCount, matchedCount, unmatchedCount);
            return R.ok(message);
 
        } catch (Exception e) {
            log.error("磅单导入失败:{}", e.getMessage());
            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("删除失败");
        }
    }
 
}