/* * 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 cn.hutool.core.collection.CollectionUtil; import com.alibaba.druid.sql.visitor.functions.Insert; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; import com.chinaztt.mes.common.numgen.NumberGenerator; import com.chinaztt.mes.warehouse.dto.StockAddDTO; import com.chinaztt.mes.warehouse.entity.ReturnDetail; import com.chinaztt.mes.warehouse.entity.ReturnMain; import com.chinaztt.mes.warehouse.entity.Stock; import com.chinaztt.mes.warehouse.entity.StockInCode; import com.chinaztt.mes.warehouse.mapper.ReturnDetailMapper; import com.chinaztt.mes.warehouse.mapper.ReturnMainMapper; import com.chinaztt.mes.warehouse.mapper.StockInCodeMapper; import com.chinaztt.mes.warehouse.mapper.StockMapper; import com.chinaztt.mes.warehouse.service.ReturnMainService; import com.chinaztt.mes.warehouse.util.StockUtils; import lombok.AllArgsConstructor; import org.springframework.security.core.parameters.P; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.List; /** * 退库申请主表 * * @author cxf * @date 2020-10-21 10:46:42 */ @Service @AllArgsConstructor @Transactional(rollbackFor = Exception.class) public class ReturnMainServiceImpl extends ServiceImpl implements ReturnMainService { private NumberGenerator numberGenerator; private ReturnDetailMapper returnDetailMapper; private StockMapper stockMapper; private StockUtils stockUtils; private ReturnMainMapper returnMainMapper; private StockInCodeMapper stockInCodeMapper; @Override public boolean save(ReturnMain returnMain) { returnMain.setReturnNo(numberGenerator.generateNumberWithPrefix(ReturnMain.DIGIT, ReturnMain.PREFIX, ReturnMain::getReturnNo)); return SqlHelper.retBool(baseMapper.insert(returnMain)); } @Override public boolean submit(Long mainId) { // 1.修改主表状态为已下发(状态机之后再调整) ReturnMain returnMain = new ReturnMain(); returnMain.setId(mainId); returnMain.setStatus("已下发"); baseMapper.updateById(returnMain); // 2.退库明细的确认数量默认为退库数量 returnDetailMapper.updateDefaultConfirmQtyByMainId(mainId); return true; } @Override public boolean accomplish(Long mainId) { // 1.修改主表状态为已处理(状态机之后再调整) ReturnMain returnMain = new ReturnMain(); returnMain.setId(mainId); returnMain.setStatus("已处理"); baseMapper.updateById(returnMain); // 2.产生退库接收事务,库存数量增加 List returnDetails = returnDetailMapper.selectList(Wrappers.query().lambda().eq(ReturnDetail::getReturnMainId, mainId)); if (CollectionUtil.isNotEmpty(returnDetails)) { for (ReturnDetail returnDetail : returnDetails) { ReturnMain newReturnMain = returnMainMapper.selectById(returnDetail.getReturnMainId()); StockInCode stockInCode = stockInCodeMapper.selectById(newReturnMain.getReturnType()); Stock stock = stockMapper.selectById(returnDetail.getStockId()); // 如果退库的零件不在原来的库位 StockAddDTO stockAdd = new StockAddDTO(); stockAdd.setPartsId(stock.getPartId()); stockAdd.setNewLocationId(returnDetail.getLocationId()); stockAdd.setNewPartBatchNo(stock.getPartBatchNo()); stockAdd.setNewSystemNo(stock.getSystemNo()); stockAdd.setReelNumber(stock.getReelNumber()); Stock newStock = stockUtils.query(stockAdd); stockUtils.updateById(newStock.getId(), returnDetail.getConfirmQty(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, newReturnMain.getReturnNo(), stockInCode.getStockCodeName()); } } return true; } }