package com.ruoyi.stock.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.excel.SupplierManageExcelDto;
|
import com.ruoyi.basic.pojo.SupplierManage;
|
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;
|
import com.ruoyi.stock.mapper.StockInventoryMapper;
|
import com.ruoyi.stock.pojo.Manufacturer;
|
import com.ruoyi.stock.pojo.StockInventory;
|
import com.ruoyi.stock.service.ManufacturerService;
|
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;
|
|
/**
|
* <p>
|
* 厂商服务实现类
|
* </p>
|
*
|
* @author 芯导软件(江苏)有限公司
|
* @since 2026-05-28 09:52:35
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class ManufacturerServiceImpl extends ServiceImpl<ManufacturerMapper, Manufacturer> implements ManufacturerService {
|
|
private final StockInventoryMapper stockInventoryMapper;
|
private final ManufacturerMapper manufacturerMapper;
|
private final SysUserMapper sysUserMapper;
|
|
/**
|
* 厂商新增
|
*/
|
@Override
|
@Transactional
|
public void saveManufacturer(Manufacturer manufacturer) {
|
LambdaQueryWrapper<Manufacturer> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.eq(Manufacturer::getName,manufacturer.getName());
|
if (baseMapper.selectCount(queryWrapper) > 0) {
|
throw new RuntimeException("厂家已存在");
|
}
|
|
baseMapper.insert(manufacturer);
|
}
|
|
/**
|
* 厂商删除
|
*/
|
@Override
|
@Transactional
|
public int delManufacturer(List<Long> ids) {
|
// 批量检查是否关联库存
|
LambdaQueryWrapper<StockInventory> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.in(StockInventory::getManufacturerId, ids);
|
List<StockInventory> inventoryList = stockInventoryMapper.selectList(queryWrapper);
|
|
if (!inventoryList.isEmpty()) {
|
throw new RuntimeException("厂商已关联库存,无法删除");
|
}
|
|
return baseMapper.deleteBatchIds(ids);
|
}
|
|
/**
|
* 厂商详情
|
*/
|
@Override
|
public Manufacturer manufacturerDetail(Long id) {
|
return baseMapper.selectById(id);
|
}
|
|
/**
|
* 厂商修改
|
*/
|
@Override
|
@Transactional
|
public int manufacturerUpdate(Manufacturer manufacturer) {
|
return baseMapper.updateById(manufacturer);
|
}
|
|
/**
|
* 厂商分页查询
|
*/
|
@Override
|
public IPage<Manufacturer> manufacturerListPage(Page page, ManufacturerDto manufacturerDto) {
|
return manufacturerMapper.manufacturerListPage(page,manufacturerDto);
|
}
|
|
/**
|
* 厂商导出
|
*/
|
@Override
|
public void manufacturerExport(HttpServletResponse response, ManufacturerDto manufacturerDto) {
|
List<ManufacturerExcelDto> list = manufacturerMapper.manufacturerExportList(manufacturerDto);
|
ExcelUtil<ManufacturerExcelDto> util = new ExcelUtil<>(ManufacturerExcelDto.class);
|
util.exportExcel(response, list, "厂家数据");
|
}
|
|
/**
|
* 厂商导入
|
*/
|
@Override
|
@Transactional
|
public R importData(MultipartFile file) {
|
try {
|
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 R.fail("导入失败: " + e.getMessage());
|
}
|
}
|
}
|