huminmin
2026-05-28 13afb7eafeffac6ecccc7c66d36974c54c5172d4
src/main/java/com/ruoyi/stock/service/impl/ManufacturerServiceImpl.java
@@ -10,6 +10,10 @@
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.stock.dto.ManufacturerDto;
import com.ruoyi.stock.execl.ManufacturerExcelDto;
import com.ruoyi.stock.mapper.ManufacturerMapper;
@@ -20,10 +24,13 @@
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
/**
@@ -40,6 +47,7 @@
    private final StockInventoryMapper stockInventoryMapper;
    private final ManufacturerMapper manufacturerMapper;
    private final SysUserMapper sysUserMapper;
    /**
     * 厂商新增
@@ -50,7 +58,7 @@
        LambdaQueryWrapper<Manufacturer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manufacturer::getName,manufacturer.getName());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("供应商已存在");
            throw new RuntimeException("厂家已存在");
        }
        baseMapper.insert(manufacturer);
@@ -106,7 +114,7 @@
    public void manufacturerExport(HttpServletResponse response, ManufacturerDto manufacturerDto) {
        List<ManufacturerExcelDto> list = manufacturerMapper.manufacturerExportList(manufacturerDto);
        ExcelUtil<ManufacturerExcelDto> util = new ExcelUtil<>(ManufacturerExcelDto.class);
        util.exportExcel(response, list, "厂商数据");
        util.exportExcel(response, list, "厂家数据");
    }
    /**
@@ -114,17 +122,39 @@
     */
    @Override
    @Transactional
    public Boolean importData(MultipartFile file) {
    public R importData(MultipartFile file) {
        try {
            ExcelUtil<Manufacturer> util = new ExcelUtil<>(Manufacturer.class);
            List<Manufacturer> list = util.importExcel(file.getInputStream());
            if (list != null && !list.isEmpty()) {
                this.saveBatch(list);
            }
            return true;
            ExcelUtil<ManufacturerExcelDto> util = new ExcelUtil<>(ManufacturerExcelDto.class);
            List<ManufacturerExcelDto> list = util.importExcel(file.getInputStream());
            ArrayList<Manufacturer> manufacturers = new ArrayList<>();
            list.stream().forEach(dto -> {
                Manufacturer manufacturer = new Manufacturer();
                BeanUtils.copyProperties(dto, manufacturer);
                manufacturer.setMaintainTime(LocalDate.now());
                // 检查厂家名称是否重复
                LambdaQueryWrapper<Manufacturer> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Manufacturer::getName, manufacturer.getName());
                if (baseMapper.selectCount(queryWrapper) > 0) {
                    throw new RuntimeException("厂家名称已存在: " + manufacturer.getName());
                }
                if (StringUtils.isEmpty(dto.getMaintainUserName())) {
                    manufacturer.setMaintainUserId(SecurityUtils.getLoginUser().getUser().getUserId());
                } else {
                    SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getNickName, dto.getMaintainUserName()));
                    if (sysUser == null) {
                        throw new RuntimeException("维护人不存在: " + dto.getMaintainUserName());
                    }
                    manufacturer.setMaintainUserId(sysUser.getUserId());
                }
                manufacturers.add(manufacturer);
            });
            this.saveOrUpdateBatch(manufacturers);
            return R.ok("导入成功");
        } catch (Exception e) {
            log.error("导入失败", e);
            return false;
            return R.fail("导入失败: " + e.getMessage());
        }
    }
}