|  |  |  | 
|---|
|  |  |  | package com.vincent.rsf.server.manager.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.Query; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.vincent.rsf.framework.common.R; | 
|---|
|  |  |  | import com.vincent.rsf.framework.exception.CoolException; | 
|---|
|  |  |  | import com.vincent.rsf.server.common.domain.BaseParam; | 
|---|
|  |  |  | import com.vincent.rsf.server.common.domain.PageParam; | 
|---|
|  |  |  | import com.vincent.rsf.server.common.domain.PageResult; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.controller.params.OrgLocParams; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.controller.params.TransferItemParams; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.*; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.*; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Objects; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service("transferService") | 
|---|
|  |  |  | public class TransferServiceImpl extends ServiceImpl<TransferMapper, Transfer> implements TransferService { | 
|---|
|  |  |  | 
|---|
|  |  |  | private OutStockService outStockService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private OutStockItemService outStockItemService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private TransferOrderService transferOrderService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private TransferService transferService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private AsnOrderService asnOrderService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private AsnOrderItemService asnOrderItemService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocItemService locItemService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocService locService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author Ryan | 
|---|
|  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /***保存成功,自动生成出库单*/ | 
|---|
|  |  |  | genOutStock(transfer, loginUserId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new CoolException("数据错误:调拔单明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | WkOrder wkOrder = new WkOrder(); | 
|---|
|  |  |  | String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TRANSFER_ORDER_CODE, transfer); | 
|---|
|  |  |  | String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_OUT_STOCK_CODE, transfer); | 
|---|
|  |  |  | if (StringUtils.isBlank(ruleCode)) { | 
|---|
|  |  |  | throw new CoolException("调拔单号生成失败!!"); | 
|---|
|  |  |  | throw new CoolException("出库单生成失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Double anfmes = items.stream().mapToDouble(TransferItem::getAnfme).sum(); | 
|---|
|  |  |  | wkOrder.setCode(ruleCode) | 
|---|
|  |  |  | .setAnfme(anfmes) | 
|---|
|  |  |  | .setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_UN_EXCE.val) | 
|---|
|  |  |  | .setType(OrderType.ORDER_TRANSFER.type) | 
|---|
|  |  |  | .setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_INIT.val) | 
|---|
|  |  |  | .setType(OrderType.ORDER_OUT.type) | 
|---|
|  |  |  | .setWkType(OrderWorkType.ORDER_WORK_TYPE_STOCK_TERANSFER.type) | 
|---|
|  |  |  | .setCreateBy(loginUserId) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new CoolException("出库单明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | transfer.setExceStatus(WaveExceStatus.WAVE_EXCE_STATUS_EXCING.val); | 
|---|
|  |  |  | transfer.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_EXCE_ING.val); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!this.updateById(transfer)) { | 
|---|
|  |  |  | throw new CoolException("调拔单更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TransferOrder transferOrder = new TransferOrder(); | 
|---|
|  |  |  | transferOrder.setTransferId(transfer.getId()) | 
|---|
|  |  |  | .setOrderId(wkOrder.getId()) | 
|---|
|  |  |  | .setType(OrderType.ORDER_OUT.type); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!transferOrderService.save(transferOrder)) { | 
|---|
|  |  |  | throw new CoolException("单据关联失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | /**生成入库单据信息*/ | 
|---|
|  |  |  | genInStock(wkOrder, orderItems); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return transfer; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author Ryan | 
|---|
|  |  |  | * @date 2025/7/29 | 
|---|
|  |  |  | * @description: TODO | 
|---|
|  |  |  | * @version 1.0 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void genInStock(WkOrder wkOrder, List<WkOrderItem> orderItems) throws Exception { | 
|---|
|  |  |  | WkOrder order = new WkOrder(); | 
|---|
|  |  |  | BeanUtils.copyProperties(wkOrder, order); | 
|---|
|  |  |  | order.setType(OrderType.ORDER_IN.type) | 
|---|
|  |  |  | .setWkType(OrderWorkType.ORDER_WORK_TYPE_OTHER_TERANSFER.type) | 
|---|
|  |  |  | .setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_UN_EXCE.val) | 
|---|
|  |  |  | .setId(null); | 
|---|
|  |  |  | String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_ASN_ORDER, order); | 
|---|
|  |  |  | if (StringUtils.isBlank(ruleCode)) { | 
|---|
|  |  |  | throw new CoolException("入库单号生成失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | order.setCode(ruleCode); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!asnOrderService.save(order)) { | 
|---|
|  |  |  | throw new CoolException("单据保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | orderItems.forEach(item -> { | 
|---|
|  |  |  | item.setOrderId(order.getId()) | 
|---|
|  |  |  | .setOrderCode(order.getCode()) | 
|---|
|  |  |  | .setId(null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!asnOrderItemService.save(item)) { | 
|---|
|  |  |  | throw new CoolException("入库单明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TransferOrder transferOrder = new TransferOrder(); | 
|---|
|  |  |  | transferOrder.setTransferId(wkOrder.getPoId()) | 
|---|
|  |  |  | .setOrderId(order.getId()) | 
|---|
|  |  |  | .setType(OrderType.ORDER_IN.type); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!transferOrderService.save(transferOrder)) { | 
|---|
|  |  |  | throw new CoolException("单据关联失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author Ryan | 
|---|
|  |  |  | * @date 2025/7/29 | 
|---|
|  |  |  | * @description: 删除调拔单 | 
|---|
|  |  |  | * @version 1.0 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public boolean removeTransfer(List<Long> list) { | 
|---|
|  |  |  | List<WkOrder> wkOrders = outStockService.list(new LambdaQueryWrapper<WkOrder>().in(WkOrder::getPoId, list)); | 
|---|
|  |  |  | if (!wkOrders.isEmpty()) { | 
|---|
|  |  |  | Set<Long> longs = wkOrders.stream().map(WkOrder::getId).collect(Collectors.toSet()); | 
|---|
|  |  |  | if (!outStockService.remove(new LambdaQueryWrapper<WkOrder>() | 
|---|
|  |  |  | .in(WkOrder::getId, longs) | 
|---|
|  |  |  | .eq(WkOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_INIT.val))) { | 
|---|
|  |  |  | throw new CoolException("当前出库单已执行,不支持删除!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | outStockItemService.remove(new LambdaQueryWrapper<WkOrderItem>().in(WkOrderItem::getOrderId, longs)); | 
|---|
|  |  |  | transferOrderService.remove(new LambdaQueryWrapper<TransferOrder>().in(TransferOrder::getTransferId, list)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | transferService.removeByIds(list); | 
|---|
|  |  |  | transferItemService.remove(new LambdaQueryWrapper<TransferItem>().in(TransferItem::getTransferId, list)); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author Ryan | 
|---|
|  |  |  | * @date 2025/7/29 | 
|---|
|  |  |  | * @description: 获取原库区物料信息 | 
|---|
|  |  |  | * @version 1.0 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public IPage<LocItem> getLocsItems(OrgLocParams params, Long loginUserId) { | 
|---|
|  |  |  | List<Loc> locs = locService.list(new LambdaQueryWrapper<Loc>() | 
|---|
|  |  |  | .eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type) | 
|---|
|  |  |  | .eq(!Objects.isNull(params.getOrgAreaId()), Loc::getAreaId, params.getOrgAreaId())); | 
|---|
|  |  |  | if (!locs.isEmpty()) { | 
|---|
|  |  |  | Page<LocItem> page = new Page<>(params.getCurrent(), params.getPageSize()); | 
|---|
|  |  |  | QueryWrapper<LocItem> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.select("id, matnr_id, maktx, matnr_code, SUM(anfme) anfme, SUM(work_qty) work_qty, SUM(qty) qty, batch, unit, fields_index, model, spec, loc_id, loc_code ") | 
|---|
|  |  |  | .lambda() | 
|---|
|  |  |  | .eq(StringUtils.isNotBlank(params.getMatnrCode()), LocItem::getMatnrCode, params.getMatnrCode()) | 
|---|
|  |  |  | .like(StringUtils.isNotBlank(params.getMaktx()), LocItem::getMaktx, params.getMaktx()); | 
|---|
|  |  |  | Set<Long> locIds = locs.stream().map(Loc::getId).collect(Collectors.toSet()); | 
|---|
|  |  |  | wrapper.lambda().in(LocItem::getLocId, locIds) | 
|---|
|  |  |  | .groupBy(LocItem::getBatch, LocItem::getMatnrId); | 
|---|
|  |  |  | return locItemService.page(page, wrapper); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | IPage<LocItem> page = new Page<>(params.getCurrent(), params.getPageSize()); | 
|---|
|  |  |  | return page; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|