package com.ruoyi.consumables.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumables.dto.ConsumablesInRecordDto;
import com.ruoyi.consumables.dto.ConsumablesInventoryDto;
import com.ruoyi.consumables.dto.ConsumablesOutRecordDto;
import com.ruoyi.consumables.dto.ConsumablesUnInventoryDto;
import com.ruoyi.consumables.execl.ConsumablesUnInventoryExportData;
import com.ruoyi.consumables.mapper.ConsumablesUnInventoryMapper;
import com.ruoyi.consumables.pojo.ConsumablesUnInventory;
import com.ruoyi.consumables.service.ConsumablesInRecordService;
import com.ruoyi.consumables.service.ConsumablesOutRecordService;
import com.ruoyi.consumables.service.ConsumablesUnInventoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
*
* 不合格库存表 服务实现类
*
*
* @author 芯导软件(江苏)有限公司
* @since 2026-01-22 10:17:45
*/
@Service
@AllArgsConstructor
public class ConsumablesUnInventoryServiceImpl extends ServiceImpl implements ConsumablesUnInventoryService {
private ConsumablesUnInventoryMapper consumablesUnInventoryMapper;
private ConsumablesOutRecordService ConsumablesOutRecordService;
private ConsumablesInRecordService ConsumablesInRecordService;
@Override
public IPage pageConsumablesUnInventory(Page page, ConsumablesUnInventoryDto consumablesUnInventoryDto) {
return consumablesUnInventoryMapper.pageConsumablesUnInventory(page, consumablesUnInventoryDto);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer addConsumablesUnInventory(ConsumablesUnInventoryDto consumablesUnInventoryDto) {
//新增入库记录再添加库存
ConsumablesInRecordDto consumablesInRecordDto = new ConsumablesInRecordDto();
consumablesInRecordDto.setRecordId(consumablesUnInventoryDto.getRecordId());
consumablesInRecordDto.setRecordType(consumablesUnInventoryDto.getRecordType());
consumablesInRecordDto.setStockInNum(consumablesUnInventoryDto.getQualitity());
consumablesInRecordDto.setProductModelId(consumablesUnInventoryDto.getProductModelId());
consumablesInRecordDto.setType("1");
ConsumablesInRecordService.add(consumablesInRecordDto);
//再进行新增库存数量库存
//先查询库存表中的产品是否存在,不存在新增,存在更新
ConsumablesUnInventory oldConsumablesUnInventory = consumablesUnInventoryMapper.selectOne(new QueryWrapper().lambda().eq(ConsumablesUnInventory::getProductModelId, consumablesUnInventoryDto.getProductModelId()));
if (ObjectUtils.isEmpty(oldConsumablesUnInventory)) {
ConsumablesUnInventory newConsumablesUnInventory = new ConsumablesUnInventory();
newConsumablesUnInventory.setProductModelId(consumablesUnInventoryDto.getProductModelId());
newConsumablesUnInventory.setQualitity(consumablesUnInventoryDto.getQualitity());
newConsumablesUnInventory.setVersion(1);
newConsumablesUnInventory.setRemark(consumablesUnInventoryDto.getRemark());
consumablesUnInventoryMapper.insert(newConsumablesUnInventory);
}else {
consumablesUnInventoryMapper.updateAddConsumablesUnInventory(consumablesUnInventoryDto);
}
return 1;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer subtractConsumablesUnInventory(ConsumablesUnInventoryDto consumablesUnInventoryDto) {
// 新增出库记录
ConsumablesOutRecordDto consumablesOutRecordDto = new ConsumablesOutRecordDto();
consumablesOutRecordDto.setRecordId(consumablesUnInventoryDto.getRecordId());
consumablesOutRecordDto.setRecordType(consumablesUnInventoryDto.getRecordType());
consumablesOutRecordDto.setStockOutNum(consumablesUnInventoryDto.getQualitity());
consumablesOutRecordDto.setProductModelId(consumablesUnInventoryDto.getProductModelId());
consumablesOutRecordDto.setType("1");
ConsumablesOutRecordService.add(consumablesOutRecordDto);
ConsumablesUnInventory oldConsumablesInventory = consumablesUnInventoryMapper.selectOne(new QueryWrapper().lambda().eq(ConsumablesUnInventory::getProductModelId, consumablesUnInventoryDto.getProductModelId()));
if (ObjectUtils.isEmpty(oldConsumablesInventory)) {
throw new RuntimeException("产品库存不存在");
}else {
consumablesUnInventoryMapper.updateSubtractConsumablesUnInventory(consumablesUnInventoryDto);
}
return 1;
}
@Override
public void exportConsumablesUnInventory(HttpServletResponse response, ConsumablesUnInventoryDto consumablesUnInventoryDto) {
List list = consumablesUnInventoryMapper.listConsumablesInventoryExportData(consumablesUnInventoryDto);
ExcelUtil util = new ExcelUtil<>(ConsumablesUnInventoryExportData.class);
util.exportExcel(response,list, "不合格库存信息");
}
@Override
public Boolean frozenConsumables(ConsumablesInventoryDto consumablesInventoryDto) {
ConsumablesUnInventory consumablesUnInventory = consumablesUnInventoryMapper.selectById(consumablesInventoryDto.getId());
if (consumablesUnInventory.getQualitity().compareTo(consumablesInventoryDto.getLockedQuantity())<0) {
throw new RuntimeException("冻结数量不能超过库存数量");
}
if (ObjectUtils.isEmpty(consumablesUnInventory.getLockedQuantity())) {
consumablesUnInventory.setLockedQuantity(consumablesInventoryDto.getLockedQuantity());
}else {
consumablesUnInventory.setLockedQuantity(consumablesUnInventory.getLockedQuantity().add(consumablesInventoryDto.getLockedQuantity()));
}
return this.updateById(consumablesUnInventory);
}
@Override
public Boolean thawConsumables(ConsumablesInventoryDto consumablesInventoryDto) {
ConsumablesUnInventory consumablesUnInventory = consumablesUnInventoryMapper.selectById(consumablesInventoryDto.getId());
if (consumablesUnInventory.getLockedQuantity().compareTo(consumablesInventoryDto.getLockedQuantity())<0) {
throw new RuntimeException("解冻数量不能超过冻结数量");
}
consumablesUnInventory.setLockedQuantity(consumablesUnInventory.getLockedQuantity().subtract(consumablesInventoryDto.getLockedQuantity()));
return this.updateById(consumablesUnInventory);
}
}