/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.businessdata.service.impl;

import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.service.PointService;
import com.youchain.basicdata.service.StockService;
import com.youchain.businessdata.domain.*;
import com.youchain.businessdata.repository.*;
import com.youchain.businessdata.service.*;
import com.youchain.exception.BadRequestException;
import com.youchain.modules.system.domain.Dept;
import com.youchain.service.LogService;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.service.dto.TaskDto;
import com.youchain.businessdata.service.dto.TaskQueryCriteria;
import com.youchain.businessdata.service.mapstruct.TaskMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.servlet.http.HttpServletResponse;

/**
 * @author houjianlan
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-08-16
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class TaskServiceImpl implements TaskService {

    private final TaskRepository taskRepository;
    private final TaskMapper taskMapper;
    private final PointService pointService;
    private final StockService stockService;
    private final PickService pickService;
    private final LogService logService;
    private final PickDetailService pickDetailService;
    private final AgvTaskService agvTaskService;
    private final InventoryService inventoryService;
    private final InventoryLogService inventoryLogService;
    private final StockTypeToAreaMap stockTypeToAreaMap;
    // 存储日志信息
    private final ConcurrentHashMap<String, Boolean> lastPollLogMessages = new ConcurrentHashMap<>();
    // 存储日志添加顺序
    private final ConcurrentLinkedQueue<String> logOrderQueue = new ConcurrentLinkedQueue<>();
    // 集合最大容量
    private static final int MAX_LOG_CAPACITY = 135;

    @Override
    public Map<String, Object> queryAll(TaskQueryCriteria criteria, Pageable pageable) {
        Page<Task> page = taskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(taskMapper::toDto));
    }

    @Override
    public List<TaskDto> queryAll(TaskQueryCriteria criteria) {
        return taskMapper.toDto(taskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public Task findById(Long id) {
        return taskRepository.findById(id).orElseGet(Task::new);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TaskDto create(Task resources) {
        return taskMapper.toDto(taskRepository.save(resources));
    }

    @Override
    public void update(Task resources) {
        Task task = taskRepository.findById(resources.getId()).orElseGet(Task::new);
        ValidationUtil.isNull(task.getId(), "Task", "id", resources.getId());
        task.copy(resources);
        taskRepository.save(task);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            taskRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TaskDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TaskDto task : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("序号", task.getId());
            map.put("物料编码", task.getItemKey().getItem().getCode());
            map.put("物料名称", task.getItemKey().getItem().getName());
            map.put("容器编号", task.getSrcStock() == null ? "" : task.getSrcStock().getCode());
            map.put("原点位", task.getSrcPointCode());
            map.put("目标点位", task.getDstPointCode());
            map.put("任务状态", task.getTaskStatus());
            map.put("计划数量", task.getPlanQty());
            map.put("移位数量", task.getMoveQty());
            map.put("创建人", task.getCreateBy());
            map.put("创建时间", task.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Task> findTaskByAgvTask(Long agvTaskId) {
        return taskRepository.findTaskByAgvTask(agvTaskId);
    }

    @Override
    public List<Task> findTaskByCallAgvTask(Long callAgvTaskId) {
        return taskRepository.findTaskByCallAgvTask(callAgvTaskId);
    }

    @Override
    public List<Task> findByPickNotAllTask() {
        return taskRepository.findByPickNotAllTask();
    }

    @Override
    public List<Task> findByPickAllTask(Long pickId) {
        return taskRepository.findByPickAllTask(pickId);
    }

    @Override
    public List<Task> findByPickDetailNotAllTask(Long pickDetailId) {
        return taskRepository.findByPickDetailNotAllTask(pickDetailId);
    }

    @Override
    public List<Task> findByPickDetailAllTask(Long pickDetailId) {
        return taskRepository.findByPickDetailAllTask(pickDetailId);
    }

    @Override
    public List<Task> findTaskByContains(String taskType, String taskStatus, String srcStockCode) {
        return taskRepository.findTaskByContains(taskType, taskStatus, srcStockCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void allocatePick(Pick pick) {


        List<String> msgList = new ArrayList<>();//存放提示信息
        //查询出待分配的pickDetail
        List<PickDetail> pickDetails = pickDetailService.findByPickDetailNoAllocate(pick.getId());
        for (PickDetail pickDetail : pickDetails) {
            msgList.add(this.allocate(pickDetail.getId(), pickDetail.getOrderQty() - pickDetail.getAllocatedQty()));
        }

        /* 更新出库单状态*/
        pickService.refreshPickStatus(pick);

        //保存日志
        String logMessage = msgList.toString();
        if (!lastPollLogMessages.containsKey(logMessage)) {
            logService.saveLogInfo(pick.getCode(), pick.getCode(), "/pick/allocatePick", msgList.toString(), "出库单分配", 200, "info");
            addLog(logMessage);
        }

    }

    private void addLog(String logMessage) {
        if (lastPollLogMessages.size() >= MAX_LOG_CAPACITY) {
            String oldestLog = logOrderQueue.poll();
            if (oldestLog != null) {
                lastPollLogMessages.remove(oldestLog);
            }
        }
        lastPollLogMessages.put(logMessage, true);
        logOrderQueue.offer(logMessage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void materialPick() {
        AgvTask agvTask = null;
        List<Task> taskList = this.findByPickNotAllTask();
        for (Task task : taskList) {
            /*生成搬运任务*/
            List<AgvTask> agvTaskList = agvTaskService.findRepeat(task.getSrcStockCode(), task.getSrcPointCode(), task.getDstPointCode());
            if (agvTaskList.isEmpty()) {
                //创建任务
                agvTask = agvTaskService.createAgvTask(BizStatus.PICK, task.getSrcStock(), task.getSrcPoint().getCode(), task.getDstPoint().getCode(), "FORKLIFT_MOVE");

                //终点占用
                pointService.usedPoint(task.getDstPoint());
            } else {
                agvTask = agvTaskList.get(0);
            }
            task.setAgvTask(agvTask);
            task.setTaskStatus(agvTask.getStatus());
            taskRepository.save(task);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void callEmptyStock(Point point) {
        String dstAreaCode = AreaNameDic.XJJLQ.equals(point.getArea().getName()) ? AreaNameDic.XJFBCQ : AreaNameDic.DJFBCQ;

        //容器
        Stock stock = stockService.findByPointCode(point.getCode());
        if (stock == null) {
            return;
        }
        //起点
        Point srcPoint = stock.getPoint();

        //终点
        List<Point> pointList = pointService.queryPoints(null, BaseStatus.FREE, BaseStatus.BOX, dstAreaCode);
        if (pointList.isEmpty()) {
            return;
        }
        Point endPoint = pointList.get(0);

        //有任务则直接返回
        if (!agvTaskService.findByEndSlotCode(endPoint.getCode(), BizStatus.JL_Back, "RACK_MOVE")) {
            return;
        }

        //终点占用
        pointService.usedPoint(endPoint);

        //创建任务
        agvTaskService.createAgvTask(BizStatus.JL_Back, stock, srcPoint.getCode(), endPoint.getCode(), "RACK_MOVE");


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void callJlTask(Pick pick) {

        //绑定任务
        List<Task> taskList = this.findByPickAllTask(pick.getId());
        if (taskList.isEmpty()) {
            throw new BadRequestException(pick.getGdCode() + "工单没有进行翻包!");
        }
        //工单是否叫料
        if (StringUtils.isEmpty(pick.getCallPoint())) {
            throw new BadRequestException(pick.getGdCode() + "工单没有进行叫料!");
        }
        //终点
        Set<String> endPointList = new HashSet<>(Arrays.asList(pick.getCallPoint().split(",")));
        Map<String, Point> pointMap = pointService.findByCodes(endPointList);


     /*   AgvTask agvTask = agvTaskService.createAgvTask(BizStatus.CALL_PICK, stock, srcPoint.getCode(), endPoint.getCode(), "RACK_MOVE");
        agvTask.setLineSlotCode(pick.getCode());
        agvTaskService.update(agvTask);*/
        List<String> endPoints = getValuesFromMap(endPointList, pointMap);
        int taskCount = 0;
        for (Task task : taskList) {
            for (String endPointCode : endPoints) {
                if (agvTaskService.isEndPointAvailable(endPointCode, BizStatus.CALL_PICK, "RACK_MOVE")) {
                    AgvTask agvTask = agvTaskService.createAgvTask(BizStatus.CALL_PICK, task.getDstStock(), task.getDstPointCode(), endPointCode, "RACK_MOVE");
                    agvTask.setLineSlotCode(pick.getCode());
                    agvTaskService.update(agvTask);
                    task.setCallAgvTaskId(agvTask.getId());
                    taskRepository.save(task);
                }
                taskCount++;
                if (taskCount >= taskList.size()) {
                    break; // 当处理的任务数量达到 taskList 的长度时，跳出内层循环
                }
            }
            if (taskCount >= taskList.size()) {
                break; // 当处理的任务数量达到 taskList 的长度时，跳出外层循环
            }
        }


    }

    public static List<String> getValuesFromMap(Set<String> endPointList, Map<String, Point> map) {
        List<String> values = new ArrayList<>();
        int count = endPointList.size();
        int index = 0;

        // 遍历 map 的键值对
        for (Map.Entry<String, Point> entry : map.entrySet()) {
            if (index >= count) {
                break;
            }
            values.add(entry.getKey());
            index++;
        }
        return values;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String allocate(long id, double quantity) {
        //出库明细
        PickDetail pickDetail = pickDetailService.findById(id);

        //物料
        Item item = pickDetail.getItem();

        //出库单
        Pick pick = pickDetail.getPick();

        if (pickDetail.getAllocatedQty() >= pickDetail.getOrderQty()) {
            return pick.getCode() + "出库单;" + item.getCode() + "数量已分配！";
        }
        String boxAreaName;
        if (BaseStatus.GD_TYPE_CT.equals(pick.getOrderType())) {
            boxAreaName = AreaNameDic.XJFBQ;
        } else if (BaseStatus.GD_TYPE_DP.equals(pick.getOrderType())) {
            boxAreaName = AreaNameDic.DJFBQ;
        } else {
            throw new BadRequestException("出库单类型错误！");
        }

        return allocateInternal(pick, pickDetail, item, boxAreaName, quantity);
    }

    private String allocateInternal(Pick pick, PickDetail pickDetail, Item item, String boxAreaName, double quantity) {
        // 查询物料所在库区的库存
        String areaName = boxAreaName.equals(AreaNameDic.DJFBQ) ? AreaNameDic.DJQ : AreaNameDic.XJQ;
        List<Inventory> inventoryList = inventoryService.queryInventory(null, item.getCode(), null, areaName, item.getDept().getId());
        if (inventoryList.isEmpty()) {
            if (isReplaceableMaterial(item.getCode())) {
                // 定义互替物料关系
                String alternativeMaterial = getAlternativeMaterial(item.getCode());
                inventoryList = inventoryService.queryInventory(null, alternativeMaterial, null, areaName, item.getDept().getId());
                if (inventoryList.isEmpty()) {
                    return pick.getCode() + "出库单;" + item.getCode() + "的替换物料" + alternativeMaterial + "库存不足，请先补充库存！";
                }
            } else {
                return pick.getCode() + "出库单;" + item.getCode() + "物料库存不足，请先补充库存！";
            }
        }

        double allocateQty = 0;
        // 未分配数量
        double unQty = quantity;
        Set<String> tps = new HashSet<>();
        for (Inventory inv : inventoryList) {
            // 判断托盘是否有任务
            synchronized (inv.getId()) {
                if (this.existsByStock(pick.getCode(), inv.getStockCode()) >= 1) {
                    tps.add(inv.getStockCode());
                    continue;
                }

                //查询翻包区是否有空闲点位
                List<Point> pointList = pointService.queryPoints(null, BaseStatus.FREE, BaseStatus.BOX, boxAreaName);
                if (pointList.isEmpty()) {
                    String logMessage = pick.getCode() + "明细分配失败，" + (BaseStatus.GD_TYPE_CT.equals(pick.getOrderType()) ? "小件" : "大件") + "翻包区没有空闲点位!";
                    if (!lastPollLogMessages.containsKey(logMessage)) {
                        logService.saveLogInfo(pick.getCode(), pick.getCode(), "/pick/allocatePick", logMessage, "出库单分配", 200, "info");
                        addLog(logMessage);
                    }
                    return logMessage;
                }
                Point endPoint = pointList.get(0);

                // 起点
                Point startPoint = inv.getPoint();
                if (unQty == 0) {
                    break;
                }
                // 库存可用数量
                allocateQty = inv.getQuantity() - inv.getQueuedQty();
                if (allocateQty <= 0) {
                    continue;
                }

                if (unQty < allocateQty) {
                    allocateQty = unQty;
                }
                inv.setQueuedQty(inv.getQueuedQty() + allocateQty);
                inventoryService.update(inv);
                unQty -= allocateQty;
                /* 更新出库单明细状态*/
                pickDetail.setAllocatedQty(pickDetail.getAllocatedQty() + allocateQty);
                pickDetailService.update(pickDetail);

                // 生成Task任务
                Task task = this.createTask(item, allocateQty, inv.getItemKey(), null, BizStatus.PICK, pickDetail, inv.getId(), inv.getStock(), startPoint, endPoint, null);

                //占用点位
                pointService.usedPoint(endPoint);
            }
        }

        if (tps.isEmpty()) {
            return pick.getCode() + "出库单中" + item.getCode() + "物料分配成功！";
        }
        return tps + "托盘有任务！";

    }


    // 增加物料可替代性判断
    private boolean isReplaceableMaterial(String material) {
        return material.equals("17300653-00") || material.equals("17213913-00");
    }

    private String getAlternativeMaterial(String material) {
        switch (material) {
            case "17300653-00":
                return "17213913-00";
            case "17213913-00":
                return "17300653-00";
            default:
                return null; //非替代物料返回null
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelAllocate(long id, double quantity) {
        PickDetail pd = pickDetailService.findById(id);
        List<Task> tasks = this.findByPickDetailNotAllTask(id);
        for (Task task : tasks) {
            //根据task找到对应的库存
            Inventory inv = inventoryService.findById(task.getInvId());
            inv.setQueuedQty(inv.getQueuedQty() - pd.getAllocatedQty());
            inventoryService.update(inv);

            //删除Task
            taskRepository.delete(task);

            pd.setAllocatedQty(pd.getAllocatedQty() - quantity);
            if (pd.getAllocatedQty() == 0) {
                pd.setStatus(BizStatus.OPEN);
            }
            pickDetailService.update(pd);
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pickConfirm(Long taskId, double pickedQuantity) {
        Task task = this.findById(taskId);
        if (task.getInvId() == null) {
            throw new BadRequestException("未找到库存相应记录！");
        }
        if (task.getPlanQty() > task.getMoveQty()) {
            PickDetail pickDetail = task.getPickDetail();
            Inventory inv = inventoryService.findById(task.getInvId());
            synchronized (inv) {
                inv.setQuantity(inv.getQuantity() - pickedQuantity);
                inv.setQueuedQty(inv.getQueuedQty() - pickedQuantity);
                inventoryService.update(inv);

                //释放容器
                stockService.usedStock(inv.getStock(), inv.getPoint(), BaseStatus.FREE);

                if (inv.getQuantity() <= 0) {
                    inventoryService.deleteAll(new Long[]{inv.getId()});
                }

                task.setMoveQty(pickedQuantity);
                task.setTaskStatus(BizStatus.FINISH);
                taskRepository.save(task);

                //回写拣货数量
                pickDetail.setPickedQty(pickDetail.getPickedQty() + pickedQuantity);
                pickDetailService.update(pickDetail);

                //刷新状态
                pickService.refreshPickStatus(pickDetail.getPick());
            }

            //添加库存日志
            inventoryLogService.createInventoryLog(BizStatus.PICK_DOWN, BizStatus.REDUCE, task.getPickDetail().getPo(), task.getItemKey(), task.getSrcPoint(), task.getDstPoint(), task.getSrcStock(), task.getDstStock(), inv.getQuantity(), pickedQuantity, BizStatus.PICK, task.getId(), inv.getId(), "");

        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pickBarBack(long taskId, double pickedQuantity) {
        Task task = findById(taskId);
        PickDetail pickDetail = task.getPickDetail();
        if (task.getInvId() == null) {
            throw new BadRequestException("未找到库存相应记录！");
        }
        Inventory inv = inventoryService.findById(task.getInvId());

        //退回库存
        double srcQty = inv.getQueuedQty();
        synchronized (inv) {
            inv.setQueuedQty(inv.getQueuedQty() + pickedQuantity);
            inv.setQuantity(inv.getQuantity() + pickedQuantity);
            inventoryService.update(inv);


            //退回Task
            task.setMoveQty(task.getMoveQty() - pickedQuantity);
            taskRepository.save(task);

            //退回拣货数量
            pickDetail.setPickedQty(pickDetail.getPickedQty() - pickedQuantity);
            pickDetailService.update(pickDetail);

            //更新出库单状态
            pickService.refreshPickStatus(pickDetail.getPick());

        }
        //添加库存日志
        inventoryLogService.createInventoryLog(BizStatus.PICK_CANCEL, BizStatus.ADD, task.getPickDetail().getPo(), task.getItemKey(), task.getDstPoint(), task.getSrcPoint(), task.getSrcStock(), task.getDstStock(), srcQty, pickedQuantity, BizStatus.PICK, task.getId(), inv.getId(), "");

    }

    @Override
    public Task createTask(Item item, Double quantity, ItemKey itemKey, AsnDetail asnDetail, String taskType, PickDetail pickDetail, Long invId, Stock stock, Point srcPoint, Point dstPoint, AgvTask agvTask) {
        Task task = new Task();
        task.setItem(item);
        task.setItemKey(itemKey);
        task.setBillCode(asnDetail == null ? "" : asnDetail.getOrderNumber());
        task.setTaskType(taskType);
        task.setAsnDetail(asnDetail);
        task.setPickDetail(pickDetail);
        task.setInvId(invId);
        task.setSrcStock(stock);
        task.setDstStock(stock);
        task.setSrcPoint(srcPoint);
        task.setDstPoint(dstPoint);
        task.setSrcStockCode(stock == null ? null : stock.getCode());
        task.setDstStockCode(stock == null ? null : stock.getCode());
        task.setSrcPointCode(srcPoint == null ? null : srcPoint.getCode());
        task.setDstPointCode(dstPoint == null ? null : dstPoint.getCode());
        task.setTaskStatus(BizStatus.OPEN);
        task.setPlanQty(quantity);
        task.setDept(item.getDept());
        task.setAgvTask(agvTask);
        taskRepository.save(task);
        return task;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Task reassignTask(Task task, String orderNumber) {
        Inventory dstinventory = inventoryService.findByOrderNumber(orderNumber, task.getSrcStockCode());
        if (dstinventory == null) {
            throw new BadRequestException(orderNumber + "箱号无库存，请更换箱号！");
        }
        double kyQty = dstinventory.getQuantity() - dstinventory.getQueuedQty();
        if (kyQty < task.getPlanQty()) {
            throw new BadRequestException(orderNumber + "箱号库存数量不足，请更换箱号！");
        }
        Inventory srcinventory = inventoryService.findById(task.getInvId());
        srcinventory.setQueuedQty(srcinventory.getQueuedQty() - task.getPlanQty());
        inventoryService.update(srcinventory);
        dstinventory.setQueuedQty(dstinventory.getQueuedQty() + task.getPlanQty());
        inventoryService.update(dstinventory);
        task.setInvId(dstinventory.getId());
        task.setItemKey(dstinventory.getItemKey());
        taskRepository.save(task);

        return task;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void moveInventory(Task task, Stock dstStock, double moveQty) {
        //将占用数量移位，原库存回库内
        Inventory inventory = inventoryService.findById(task.getInvId());
        ItemKey itemKey = inventory.getItemKey();
        Dept dept = inventory.getDept();
        PickDetail pickDetail = task.getPickDetail();
        synchronized (inventory) {
            inventory.setQueuedQty(inventory.getQueuedQty() - moveQty);
            inventory.setQuantity(inventory.getQuantity() - moveQty);
            inventoryService.update(inventory);

            if (inventory.getQuantity() == 0) {
                inventoryService.deleteAll(new Long[]{inventory.getId()});
            }
        }

        //生成新库存
        Inventory newInventory = inventoryService.createInventory(itemKey, dstStock.getPoint(), dstStock, dept, moveQty);

        //更新Task
        double planQty = task.getPlanQty();
        String status = task.getTaskStatus();
        task.setInvId(inventory.getId());
        task.setNewInvId(newInventory.getId());
        task.setMoveQty(task.getMoveQty() + moveQty);
        task.setDstStock(dstStock);
        task.setDstStockCode(dstStock.getCode());
        task.setDstPoint(dstStock.getPoint());
        task.setDstPointCode(dstStock.getPoint() == null ? null : dstStock.getPoint().getCode());
        if (task.getPlanQty() - task.getMoveQty() == 0) {
            task.setTaskStatus(BizStatus.FINISH);
        } else {
            //部分拣货；部分数量生成新的Task；当前任务完成即可
            task.setPlanQty(moveQty);
            task.setMoveQty(moveQty);
            task.setTaskStatus(BizStatus.FINISH);
            // 生成Task任务
            Task newTask = this.createTask(task.getItemKey().getItem(), planQty - moveQty, task.getItemKey(), null, BizStatus.PICK, pickDetail, task.getInvId(), task.getSrcStock(), task.getSrcPoint(), task.getDstPoint(), task.getAgvTask());
            newTask.setDstStock(dstStock);
            newTask.setDstStockCode(dstStock.getCode());
            newTask.setTaskStatus(status);
            taskRepository.save(newTask);
        }
        taskRepository.save(task);

        pickDetail.setPickedQty(pickDetail.getPickedQty() + moveQty);
        pickDetailService.update(pickDetail);

        //容器占用
        stockService.usedStock(dstStock, dstStock.getPoint(), BaseStatus.USED);

        //库存日志
        inventoryLogService.createInventoryLog(BizStatus.MOVE, BizStatus.ADD, task.getPickDetail().getPo(), task.getItemKey(), task.getDstPoint(), dstStock.getPoint(), task.getSrcStock(), dstStock, 0d, moveQty, BizStatus.PICK, task.getId(), newInventory.getId(), "");

    }

    @Override
    public int existsByStock(String pickCode, String stockCode) {
        return taskRepository.existsByStock(pickCode, stockCode);
    }

}
