src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -4,16 +4,17 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.StockQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockUnQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.EnumUtil;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.stock.dto.StockInRecordDto;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.dto.StockUninventoryDto;
import com.ruoyi.stock.execl.StockInRecordExportData;
import com.ruoyi.stock.mapper.StockInRecordMapper;
import com.ruoyi.stock.mapper.StockInventoryMapper;
@@ -22,43 +23,50 @@
import com.ruoyi.stock.pojo.StockInventory;
import com.ruoyi.stock.pojo.StockUninventory;
import com.ruoyi.stock.service.StockInRecordService;
import lombok.AllArgsConstructor;
import com.ruoyi.stock.service.StockInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@Service
@AllArgsConstructor
public class StockInRecordServiceImpl extends ServiceImpl<StockInRecordMapper, StockInRecord> implements StockInRecordService {
    @Autowired
    private StockInRecordMapper stockInRecordMapper;
    @Autowired
    private StockInventoryMapper stockInventoryMapper;
    @Autowired
    private StockUninventoryMapper stockUninventoryMapper;
    @Autowired
    private StockInventoryService stockInventoryService;
    @Override
    public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
        return stockInRecordMapper.listPage(page, stockInRecordDto);
    }
    // 新增入库
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(StockInRecordDto stockInRecordDto) {
    public Long add(StockInRecordDto stockInRecordDto) {
        String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK");
        stockInRecordDto.setInboundBatches(no);
        StockInRecord stockInRecord = new StockInRecord();
        BeanUtils.copyProperties(stockInRecordDto, stockInRecord);
        return stockInRecordMapper.insert(stockInRecord);
        int insertRows = stockInRecordMapper.insert(stockInRecord);
        Long insertId = stockInRecord.getId();
        return insertRows > 0 ? insertId : null;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(Long id, StockInRecordDto stockInRecordDto) {
        // 判断对象是否存在
        StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
        if (stockInRecord == null){
        if (stockInRecord == null) {
            throw new BaseException("该入库记录不存在,无法更新!!!");
        }
@@ -72,26 +80,28 @@
    public int batchDelete(List<Long> ids) {
        for (Long id : ids) {
            StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
            if (stockInRecord.getType().equals("0")) {
                StockInventory stockInventory = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>().eq(StockInventory::getProductModelId, stockInRecord.getProductModelId()));
            if ("0".equals(stockInRecord.getType())) {
                StockInventory stockInventory = findQualifiedInventory(stockInRecord);
                if (stockInventory == null) {
                    throw new BaseException("库存记录中没有对应的产品,无法删除!!!");
                }else {
                    StockInventoryDto stockInRecordDto = new StockInventoryDto();
                    stockInRecordDto.setProductModelId(stockInventory.getProductModelId());
                    stockInRecordDto.setQualitity(stockInRecord.getStockInNum());
                    stockInventoryMapper.updateAddStockInventory(stockInRecordDto);
                    throw new BaseException("库存记录中没有对应的产品,无法删除!!!");
                }
            }else if (stockInRecord.getType().equals("1")) {
                StockUninventory stockUninventory = stockUninventoryMapper.selectOne(new LambdaQueryWrapper<StockUninventory>().eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()));
                stockInventory.setQualitity(defaultDecimal(stockInventory.getQualitity()).subtract(defaultDecimal(stockInRecord.getStockInNum())));
                stockInventory.setVersion(stockInventory.getVersion() == null ? 1 : stockInventory.getVersion() + 1);
                stockInventory.setUpdateTime(LocalDateTime.now());
                stockInventoryMapper.updateById(stockInventory);
            } else if ("1".equals(stockInRecord.getType())) {
                StockUninventory stockUninventory = stockUninventoryMapper.selectOne(
                        new LambdaQueryWrapper<StockUninventory>()
                                .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId())
                                .eq(StockUninventory::getBatchNo, stockInRecord.getBatchNo())
                );
                if (stockUninventory == null) {
                    throw new BaseException("库存记录中没有对应的产品,无法删除!!!");
                }else {
                    StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
                    stockUninventoryDto.setProductModelId(stockUninventory.getProductModelId());
                    stockUninventoryDto.setQualitity(stockInRecord.getStockInNum());
                    stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
                    throw new BaseException("库存记录中没有对应的产品,无法删除!!!");
                }
                stockUninventory.setQualitity(defaultDecimal(stockUninventory.getQualitity()).subtract(defaultDecimal(stockInRecord.getStockInNum())));
                stockUninventory.setVersion(stockUninventory.getVersion() == null ? 1 : stockUninventory.getVersion() + 1);
                stockUninventory.setUpdateTime(LocalDateTime.now());
                stockUninventoryMapper.updateById(stockUninventory);
            }
        }
        return stockInRecordMapper.deleteBatchIds(ids);
@@ -101,13 +111,53 @@
    public void exportStockInRecord(HttpServletResponse response, StockInRecordDto stockInRecordDto) {
        List<StockInRecordExportData> list = stockInRecordMapper.listStockInRecordExportData(stockInRecordDto);
        for (StockInRecordExportData stockInRecordExportData : list) {
            if (stockInRecordExportData.getType().equals("0")) {
                stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue());
            }else {
                stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockUnQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue());
            if ("0".equals(stockInRecordExportData.getType())) {
                stockInRecordExportData.setRecordType(
                        EnumUtil.fromCode(StockInQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue()
                );
            } else {
                stockInRecordExportData.setRecordType(
                        EnumUtil.fromCode(StockInUnQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue()
                );
            }
        }
        ExcelUtil<StockInRecordExportData> util = new ExcelUtil<>(StockInRecordExportData.class);
        util.exportExcel(response,list, "入库记录信息");
        util.exportExcel(response, list, "入库记录信息");
    }
    @Override
    public StockInRecordDto selectByRecord(Long id) {
        return stockInRecordMapper.selectByRecord(id);
    }
    @Override
    public int updateStockInRecord(StockInRecordDto stockInRecordDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        try {
            stockInventoryService.addApproveByPurchase(loginUser, stockInRecordDto, stockInRecordDto.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        stockInRecordDto.setApproveStatus(1);
        return stockInRecordMapper.updateById(stockInRecordDto);
    }
    // 合格入库回退按与库存合并一致的唯一键查找。
    private StockInventory findQualifiedInventory(StockInRecord stockInRecord) {
        LambdaQueryWrapper<StockInventory> queryWrapper = new LambdaQueryWrapper<StockInventory>()
                .eq(StockInventory::getProductModelId, stockInRecord.getProductModelId())
                .orderByAsc(StockInventory::getId);
        String processCategory = StringUtils.trimToEmpty(stockInRecord.getProcessCategory());
        String voltage = StringUtils.trimToEmpty(stockInRecord.getVoltage());
        if (StringUtils.isNotBlank(processCategory) || StringUtils.isNotBlank(voltage)) {
            queryWrapper.eq(StockInventory::getProcessCategory, processCategory);
            queryWrapper.eq(StockInventory::getVoltage, voltage);
        }
        List<StockInventory> inventories = stockInventoryMapper.selectList(queryWrapper);
        return inventories.isEmpty() ? null : inventories.get(0);
    }
    private BigDecimal defaultDecimal(BigDecimal value) {
        return value == null ? BigDecimal.ZERO : value;
    }
}