/*
|
* Copyright (c) 2018-2025, ztt All rights reserved.
|
*
|
* Redistribution and use in source and binary forms, with or without
|
* modification, are permitted provided that the following conditions are met:
|
*
|
* Redistributions of source code must retain the above copyright notice,
|
* this list of conditions and the following disclaimer.
|
* Redistributions in binary form must reproduce the above copyright
|
* notice, this list of conditions and the following disclaimer in the
|
* documentation and/or other materials provided with the distribution.
|
* Neither the name of the pig4cloud.com developer nor the names of its
|
* contributors may be used to endorse or promote products derived from
|
* this software without specific prior written permission.
|
* Author: ztt
|
*/
|
package com.chinaztt.mes.warehouse.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
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.chinaztt.mes.common.numgen.NumberGenerator;
|
import com.chinaztt.mes.warehouse.dto.AdjustDetailDTO;
|
import com.chinaztt.mes.warehouse.dto.StockAddDTO;
|
import com.chinaztt.mes.warehouse.entity.AdjustDetail;
|
import com.chinaztt.mes.warehouse.entity.Stock;
|
import com.chinaztt.mes.warehouse.mapper.AdjustDetailMapper;
|
import com.chinaztt.mes.warehouse.mapper.StockMapper;
|
import com.chinaztt.mes.warehouse.service.AdjustDetailService;
|
import com.chinaztt.mes.warehouse.util.StockUtils;
|
import lombok.AllArgsConstructor;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.time.LocalDateTime;
|
import java.util.List;
|
|
/**
|
* 库存调整表
|
*
|
* @author sunxl
|
* @date 2020-11-03 14:33:19
|
*/
|
@Service
|
@AllArgsConstructor
|
public class AdjustDetailServiceImpl extends ServiceImpl<AdjustDetailMapper, AdjustDetail> implements AdjustDetailService {
|
private NumberGenerator<AdjustDetail> numberGenerator;
|
private StockMapper stockMapper;
|
private StockUtils stockUtils;
|
|
@Override
|
public boolean fullSave(AdjustDetail adjustDetail) {
|
numberMonitoring(adjustDetail);
|
adjustDetail.setAdjNo(numberGenerator.generateNumberWithPrefix(AdjustDetail.DIGIT, AdjustDetail.PREFIX, AdjustDetail::getAdjNo));
|
adjustDetail.setAdjTime(LocalDateTime.now());
|
baseMapper.insert(adjustDetail);
|
return true;
|
}
|
|
@Override
|
public IPage<List<AdjustDetailDTO>> getAdjust(Page page, QueryWrapper<AdjustDetailDTO> gen) {
|
return baseMapper.getAdjust(page, gen);
|
}
|
|
@Override
|
public AdjustDetailDTO getAdjustById(Long id) {
|
return baseMapper.getAdjustById(id);
|
}
|
|
@Override
|
public boolean updateByAdjust(AdjustDetail adjustDetail) {
|
numberMonitoring(adjustDetail);
|
baseMapper.updateById(adjustDetail);
|
return true;
|
}
|
|
@Override
|
public boolean updateAdjust(List<Long> ids) {
|
for (Long id : ids) {
|
AdjustDetail adjustDetail = baseMapper.selectById(id);
|
adjustDetail.setStatus("已下发");
|
baseMapper.updateById(adjustDetail);
|
//原零件的实时库存
|
Stock stock = stockMapper.selectOne(Wrappers.<Stock>lambdaQuery().eq(Stock::getPartId, adjustDetail.getFromPartId())
|
.eq(Stock::getLocationId, adjustDetail.getFromLocId()).eq(Stock::getPartBatchNo, adjustDetail.getFromPartBatchNo()).eq(Stock::getSystemNo, adjustDetail.getSystemNo()));
|
stockUtils.updateById(stock.getId(), adjustDetail.getAdjNum().negate(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, adjustDetail.getAdjNo(), "ADJUST");
|
//新零件的实时库存
|
Stock newStock = stockMapper.selectOne(Wrappers.<Stock>lambdaQuery().eq(Stock::getPartId, adjustDetail.getToPartId())
|
.eq(Stock::getLocationId, adjustDetail.getToLocId()).eq(Stock::getPartBatchNo, adjustDetail.getToPartBatchNo()).eq(Stock::getSystemNo, adjustDetail.getSystemNo()));
|
//如果存在,加上调整数量
|
if (newStock != null) {
|
stockUtils.updateById(newStock.getId(), adjustDetail.getAdjNum(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, adjustDetail.getAdjNo(), "ADJUST");
|
}
|
if (newStock == null) {
|
StockAddDTO stockAdd = new StockAddDTO();
|
stockAdd.setPartsId(adjustDetail.getToPartId());
|
stockAdd.setNewLocationId(adjustDetail.getToLocId());
|
stockAdd.setNewPartBatchNo(adjustDetail.getToPartBatchNo());
|
stockAdd.setNewSystemNo(adjustDetail.getSystemNo());
|
|
Stock stock1 = stockUtils.query(stockAdd);
|
stockUtils.updateById(stock1.getId(), adjustDetail.getAdjNum(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO,adjustDetail.getAdjNo(), "ADJUST");
|
}
|
}
|
return false;
|
}
|
|
public void numberMonitoring(AdjustDetail adjustDetail) {
|
Stock stock = stockMapper.selectOne(Wrappers.<Stock>lambdaQuery().eq(Stock::getPartId, adjustDetail.getFromPartId())
|
.eq(Stock::getLocationId, adjustDetail.getFromLocId()).eq(Stock::getPartBatchNo, adjustDetail.getFromPartBatchNo()));
|
if (stock.getAvailableStockQuantity().compareTo(adjustDetail.getAdjNum()) == -1) {
|
throw new RuntimeException("调整数量超过了可用库存数量,请重新填写");
|
}
|
}
|
}
|