|  |  |  | 
|---|
|  |  |  | package com.vincent.rsf.server.manager.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.vincent.rsf.framework.common.Cools; | 
|---|
|  |  |  | import com.vincent.rsf.framework.common.R; | 
|---|
|  |  |  | import com.vincent.rsf.framework.exception.CoolException; | 
|---|
|  |  |  | import com.vincent.rsf.server.api.utils.LocUtils; | 
|---|
|  |  |  | import com.vincent.rsf.server.common.constant.Constants; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.controller.dto.ExistDto; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.controller.dto.OrderOutItemDto; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.controller.params.LocToTaskParams; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.controller.params.OrderOutTaskParam; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.controller.params.OutStockToTaskParams; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.*; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.controller.params.AsnOrderAndItemsParams; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.*; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.vincent.rsf.server.system.constant.SerialRuleCode; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.utils.SerialRuleUtils; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | import org.springframework.beans.BeanUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.WaveRuleType; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Service("outStockServiceImpl") | 
|---|
|  |  |  | public class OutStockServiceImpl extends ServiceImpl<AsnOrderMapper, AsnOrder> implements OutStockService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Logger logger = LoggerFactory.getLogger(this.getClass()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private AsnOrderItemService asnOrderItemService; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @param | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public R getOrderOutTaskItem(OrderOutTaskParam param) { | 
|---|
|  |  |  | if (Cools.isEmpty(param.getWaveId())){ | 
|---|
|  |  |  | if (Cools.isEmpty(param.getWaveId())) { | 
|---|
|  |  |  | throw new CoolException("策略参数为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Cools.isEmpty(param.getOrderId())){ | 
|---|
|  |  |  | throw new CoolException("单据id为空"); | 
|---|
|  |  |  | if (Cools.isEmpty(param.getOrderId())) { | 
|---|
|  |  |  | throw new CoolException("单据ID为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | WaveRule waveRule = waveRuleService.getOne(new LambdaQueryWrapper<WaveRule>().eq(WaveRule::getId, param.getWaveId())); | 
|---|
|  |  |  | if (Cools.isEmpty(waveRule)){ | 
|---|
|  |  |  | if (Cools.isEmpty(waveRule)) { | 
|---|
|  |  |  | throw new CoolException("未找到当前策略"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<OrderOutItemDto> locItems = null; | 
|---|
|  |  |  | if (WaveRuleType.Efficiency_First.type.equals(waveRule.getType())) { | 
|---|
|  |  |  | locItems =  efficiencyFirst(param.getOrderId()); | 
|---|
|  |  |  | } else if (WaveRuleType.First_In_First_Out.type.equals(waveRule.getType())) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locItems = getOutOrderList(param.getOrderId(), waveRule); | 
|---|
|  |  |  | return R.ok(locItems); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private List<LocItem> getEfficiencyFirstItemList(AsnOrderItem asnOrderItem){ | 
|---|
|  |  |  | QueryWrapper<LocItem> locItemQueryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | locItemQueryWrapper.eq("matnr_code", asnOrderItem.getMatnrCode()); | 
|---|
|  |  |  | locItemQueryWrapper.eq("batch", asnOrderItem.getSplrBatch()); | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 生成出库任务 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param params | 
|---|
|  |  |  | * @param outId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public R genOutStockTask(List<OutStockToTaskParams> params, Long loginUserId, Long outId) { | 
|---|
|  |  |  | if (params.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("参数不能为空!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //优先生成浅库位任务 | 
|---|
|  |  |  | List<OutStockToTaskParams> Items = params.stream().sorted(Comparator.comparing(OutStockToTaskParams::getLocCode).thenComparing(item -> { | 
|---|
|  |  |  | return LocUtils.isShallowLoc(item.getLocCode()) ? 1 : 0; | 
|---|
|  |  |  | }).reversed()).collect(Collectors.toList()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (OutStockToTaskParams param : Items) { | 
|---|
|  |  |  | if (Objects.isNull(param)) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getBarcode, param.getBarcode())); | 
|---|
|  |  |  | if (!Objects.isNull(loc)) { | 
|---|
|  |  |  | List<LocItem> locItems = new ArrayList<>(); | 
|---|
|  |  |  | LocItem locItem = locItemService.getById(param.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | AsnOrderItem orderItem = outStockItemService.getOne(new LambdaQueryWrapper<AsnOrderItem>() | 
|---|
|  |  |  | .eq(AsnOrderItem::getAsnId, outId) | 
|---|
|  |  |  | .eq(StringUtils.isNotBlank(locItem.getBatch()), AsnOrderItem::getSplrBatch, locItem.getBatch()) | 
|---|
|  |  |  | .eq(StringUtils.isNotBlank(locItem.getFieldsIndex()), AsnOrderItem::getFieldsIndex, locItem.getFieldsIndex()) | 
|---|
|  |  |  | .eq(AsnOrderItem::getMatnrId, locItem.getMatnrId())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (Objects.isNull(orderItem)) { | 
|---|
|  |  |  | throw new CoolException("单据明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | locItem.setOutQty(param.getOutQty()) | 
|---|
|  |  |  | .setBatch(param.getBatch()) | 
|---|
|  |  |  | .setSourceId(outId) | 
|---|
|  |  |  | .setSourceCode(orderItem.getAsnCode()) | 
|---|
|  |  |  | .setSource(orderItem.getId()); | 
|---|
|  |  |  | locItems.add(locItem); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LocToTaskParams taskParams = new LocToTaskParams(); | 
|---|
|  |  |  | taskParams.setType(Constants.TASK_TYPE_ORDER_OUT_STOCK) | 
|---|
|  |  |  | .setOrgLoc(loc.getCode()) | 
|---|
|  |  |  | .setItems(locItems) | 
|---|
|  |  |  | .setSourceId(outId) | 
|---|
|  |  |  | .setSiteNo(param.getSiteNo()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //生成出库任务 | 
|---|
|  |  |  | locItemService.generateTask(TaskResouceType.TASK_RESOUCE_ORDER_TYPE.val, taskParams, loginUserId); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | logger.error("UNK", e); | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Double workQty = Math.round((orderItem.getWorkQty() + locItem.getOutQty()) * 10000) / 10000.0; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | orderItem.setUpdateBy(loginUserId).setUpdateTime(new Date()).setWorkQty(workQty); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!outStockItemService.updateById(orderItem)) { | 
|---|
|  |  |  | throw new CoolException("单据明细修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Double sum = Items.stream().mapToDouble(OutStockToTaskParams::getOutQty).sum(); | 
|---|
|  |  |  | //更新出库单明细及主单 | 
|---|
|  |  |  | AsnOrder outOrder = outStockService.getById(outId); | 
|---|
|  |  |  | if (Objects.isNull(outOrder)) { | 
|---|
|  |  |  | throw new CoolException("出库单据不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Double workQty = Math.round((outOrder.getWorkQty() + sum) * 10000) / 10000.0; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | outOrder.setWorkQty(workQty).setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_CREATE.val); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!outStockService.updateById(outOrder)) { | 
|---|
|  |  |  | throw new CoolException("出库单状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取出库站点 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public R getSiteNos() { | 
|---|
|  |  |  | List<Integer> list = Arrays.asList(TaskType.TASK_TYPE_MERGE_OUT.type, | 
|---|
|  |  |  | TaskType.TASK_TYPE_OUT.type, | 
|---|
|  |  |  | TaskType.TASK_TYPE_MERGE_OUT.type, | 
|---|
|  |  |  | TaskType.TASK_TYPE_PICK_AGAIN_OUT.type); | 
|---|
|  |  |  | List<DeviceSite> sites = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>().in(DeviceSite::getType, list).groupBy(DeviceSite::getSite)); | 
|---|
|  |  |  | return R.ok(sites); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private List<LocItem> getEfficiencyFirstItemList(AsnOrderItem asnOrderItem) { | 
|---|
|  |  |  | LambdaQueryWrapper<LocItem> locItemQueryWrapper = new LambdaQueryWrapper<>(); | 
|---|
|  |  |  | locItemQueryWrapper.eq(LocItem::getMatnrCode, asnOrderItem.getMatnrCode()); | 
|---|
|  |  |  | locItemQueryWrapper.eq(LocItem::getBatch, asnOrderItem.getSplrBatch()); | 
|---|
|  |  |  | String applySql = String.format( | 
|---|
|  |  |  | "EXISTS (SELECT 1 FROM man_loc ml " + | 
|---|
|  |  |  | "WHERE ml.use_status = '%s'" + | 
|---|
|  |  |  | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | locItemQueryWrapper.apply(applySql); | 
|---|
|  |  |  | List<LocItem> locItems = locItemService.list(locItemQueryWrapper); | 
|---|
|  |  |  | locItems.sort((s1,s2)-> LocUtils.isShallowLoc(s1.getLocCode())?-1:0); | 
|---|
|  |  |  | locItems.sort(Comparator | 
|---|
|  |  |  | .comparing((LocItem item) -> !LocUtils.isShallowLoc(item.getLocCode())) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | return locItems; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private List<OrderOutItemDto> efficiencyFirst(Long orderId){ | 
|---|
|  |  |  | List<AsnOrderItem> asnOrderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>() | 
|---|
|  |  |  | .eq(AsnOrderItem::getAsnId, orderId) | 
|---|
|  |  |  | private List<LocItem> getFirstInFirstOutItemList(AsnOrderItem asnOrderItem) { | 
|---|
|  |  |  | LambdaQueryWrapper<LocItem> locItemQueryWrapper = new LambdaQueryWrapper<>(); | 
|---|
|  |  |  | locItemQueryWrapper.eq(LocItem::getMatnrCode, asnOrderItem.getMatnrCode()); | 
|---|
|  |  |  | locItemQueryWrapper.eq(LocItem::getBatch, asnOrderItem.getSplrBatch()); | 
|---|
|  |  |  | locItemQueryWrapper.orderByAsc(LocItem::getCreateTime); | 
|---|
|  |  |  | String applySql = String.format( | 
|---|
|  |  |  | "EXISTS (SELECT 1 FROM man_loc ml " + | 
|---|
|  |  |  | "WHERE ml.use_status = '%s'" + | 
|---|
|  |  |  | "AND ml.id = man_loc_item.loc_id " + | 
|---|
|  |  |  | ")", | 
|---|
|  |  |  | LocStsType.LOC_STS_TYPE_F.type | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | locItemQueryWrapper.apply(applySql); | 
|---|
|  |  |  | List<LocItem> locItems = locItemService.list(locItemQueryWrapper); | 
|---|
|  |  |  | return locItems; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private List<OrderOutItemDto> getOutOrderList(Long orderId, WaveRule waveRule) { | 
|---|
|  |  |  | List<AsnOrderItem> asnOrderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>() | 
|---|
|  |  |  | .eq(AsnOrderItem::getAsnId, orderId)); | 
|---|
|  |  |  | List<OrderOutItemDto> list = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Set<String> exist = new HashSet<>(); | 
|---|
|  |  |  | Set<ExistDto> existDtos = new HashSet<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (AsnOrderItem asnOrderItem : asnOrderItems) { | 
|---|
|  |  |  | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<LocItem> locItems = null; | 
|---|
|  |  |  | locItems = getEfficiencyFirstItemList(asnOrderItem); | 
|---|
|  |  |  | if (WaveRuleType.Efficiency_First.type.equals(waveRule.getType())) { | 
|---|
|  |  |  | locItems = getEfficiencyFirstItemList(asnOrderItem); | 
|---|
|  |  |  | } else if (WaveRuleType.First_In_First_Out.type.equals(waveRule.getType())) { | 
|---|
|  |  |  | locItems = getFirstInFirstOutItemList(asnOrderItem); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | locItems = getFirstInFirstOutItemList(asnOrderItem); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (LocItem locItem : locItems) { | 
|---|
|  |  |  | Loc loc = locService.getById(locItem.getLocId()); | 
|---|
|  |  |  | List<LocItem> itemList = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocCode, locItem.getLocCode())); | 
|---|
|  |  |  | if (issued.doubleValue() > 0){ | 
|---|
|  |  |  | if (issued.doubleValue() > 0) { | 
|---|
|  |  |  | ExistDto existDto = new ExistDto().setBatch(locItem.getBatch()).setMatnr(locItem.getMatnrCode()).setLocNo(locItem.getLocCode()); | 
|---|
|  |  |  | if (existDtos.add(existDto)){ | 
|---|
|  |  |  | if (existDtos.add(existDto)) { | 
|---|
|  |  |  | locItem.setOutQty(issued.doubleValue() >= locItem.getAnfme() ? locItem.getAnfme() : issued.doubleValue()); | 
|---|
|  |  |  | locItem.setBarcode(loc.getBarcode()); | 
|---|
|  |  |  | OrderOutItemDto orderOutItemDto = new OrderOutItemDto(); | 
|---|
|  |  |  | orderOutItemDto.sync(locItem); | 
|---|
|  |  |  | orderOutItemDto.setLocItem(locItem); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<DeviceSite> deviceSites = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>() | 
|---|
|  |  |  | .eq(DeviceSite::getChannel, loc.getChannel()) | 
|---|
|  |  |  | .eq(DeviceSite::getType, issued.doubleValue() >= locItem.getAnfme() && itemList.size() == 1 ? TaskType.TASK_TYPE_OUT.type : TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | List<OrderOutItemDto.staListDto> maps = new ArrayList<>(); | 
|---|
|  |  |  | for (DeviceSite sta : deviceSites) { | 
|---|
|  |  |  | OrderOutItemDto.staListDto staListDto = new OrderOutItemDto.staListDto(); | 
|---|
|  |  |  | staListDto.setStaNo(sta.getSite()); | 
|---|
|  |  |  | staListDto.setStaName(sta.getSite()); | 
|---|
|  |  |  | maps.add(staListDto); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!deviceSites.isEmpty()) { | 
|---|
|  |  |  | List<OrderOutItemDto.staListDto> maps = new ArrayList<>(); | 
|---|
|  |  |  | for (DeviceSite sta : deviceSites) { | 
|---|
|  |  |  | OrderOutItemDto.staListDto staListDto = new OrderOutItemDto.staListDto(); | 
|---|
|  |  |  | staListDto.setStaNo(sta.getSite()); | 
|---|
|  |  |  | staListDto.setStaName(sta.getSite()); | 
|---|
|  |  |  | maps.add(staListDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | orderOutItemDto.setStaNos(maps); | 
|---|
|  |  |  | //默认获取第一站点 | 
|---|
|  |  |  | DeviceSite deviceSite = deviceSites.stream().findFirst().get(); | 
|---|
|  |  |  | orderOutItemDto.setSiteNo(deviceSite.getSite()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | orderOutItemDto.setStaNos(maps); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | list.add(orderOutItemDto); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | issued = issued.subtract(new BigDecimal(locItem.getAnfme().toString())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (issued.doubleValue() > 0) { | 
|---|
|  |  |  | LocItem locItem = new LocItem() | 
|---|
|  |  |  | .setId(new Random().nextLong()) | 
|---|
|  |  |  | .setMatnrCode(asnOrderItem.getMatnrCode()) | 
|---|
|  |  |  | .setMaktx(asnOrderItem.getMaktx()) | 
|---|
|  |  |  | .setAnfme(0.00) | 
|---|
|  |  |  | .setWorkQty(issued.doubleValue()) | 
|---|
|  |  |  | .setOutQty(issued.doubleValue()) | 
|---|
|  |  |  | .setUnit(asnOrderItem.getStockUnit()) | 
|---|
|  |  |  | .setBatch(asnOrderItem.getSplrBatch()); | 
|---|
|  |  |  | OrderOutItemDto orderOutItemDto = new OrderOutItemDto(); | 
|---|
|  |  |  | orderOutItemDto.setLocItem(locItem); | 
|---|
|  |  |  | list.add(orderOutItemDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|