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> listPage(Page page, WeighingRecord weighingRecord) { IPage 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 excelDataList = EasyExcel.read(file.getInputStream()) .head(WeighingRecordExcelDto.class) .sheet() .doReadSync(); if (CollectionUtils.isEmpty(excelDataList)) { return R.fail("模板错误或导入数据为空"); } // 转换为实体对象 List weighingRecordList = new ArrayList<>(); for (WeighingRecordExcelDto dto : excelDataList) { WeighingRecord record = new WeighingRecord(); BeanUtils.copyProperties(dto, record); weighingRecordList.add(record); } // 去重检查 List newRecordList = new ArrayList<>(); int duplicateCount = 0; for (WeighingRecord record : weighingRecordList) { LambdaQueryWrapper 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 ids) { boolean success = weighingRecordService.removeByIds(ids); if (success) { return R.ok("删除成功"); } else { return R.fail("删除失败"); } } }