/*
 *  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.repository;


import com.youchain.businessdata.domain.Inventory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author huojin
 * @website https://eladmin.vip
 * @date 2023-08-22
 **/
public interface InventoryRepository extends JpaRepository<Inventory, Long>, JpaSpecificationExecutor<Inventory> {
    /**
     * 查询库存信息
     *
     * @param stockId  容器
     * @param itemCode   物料
     * @param pointId  点位
     * @param areaName 库区
     * @param deptId   仓库
     */
    @Query(" from Inventory inv where inv.quantity-inv.queuedQty>0 " +
            "and (:stockId is null or inv.stock.id=:stockId) " +
            "and (:itemCode is null or inv.itemKey.item.code=:itemCode)" +
            "and (:pointId is null or inv.point.id=:pointId)" +
            "and (:areaName is null or inv.point.area.name=:areaName)" +
            "and (:deptId is null or inv.dept.id=:deptId)" +
            "order by inv.createTime ")
    List<Inventory> queryInventory(Long stockId, String itemCode, Long pointId, String areaName, Long deptId);

    /**
     * 根据箱号和容器查询库存信息
     *
     * @param orderNumber 箱号
     * @param stockCode   容器
     */
    @Query(" from Inventory inv where inv.itemKey.orderNumber=:orderNumber and inv.stockCode=:stockCode and inv.quantity-inv.queuedQty>0 ")
    Inventory findByOrderNumber(String orderNumber, String stockCode);

    /**
     * 根据容器查询所有库存信息
     *
     * @param stockCode 容器
     */
    @Query(" from Inventory inv where inv.stockCode=:stockCode and inv.quantity>0 ")
    List<Inventory> findByStockCode(String stockCode);

    @Query(" select " +
            " inv.stock.code," +
            " inv.point.code," +
            " function('group_concat', inv.itemKey.orderNumber) as  orderNumber," +
            " inv.itemKey.item.code," +
            " sum(inv.quantity)  " +
            " from Inventory inv " +
            " where inv.quantity>0 " +
            " group by inv.stock.code,inv.point.code,inv.itemKey.item.code " +
            " order by inv.point.code ")
    Page<List<Object[]>> queryBigScreenPointInfo(Pageable pageable);

    @Query(" from Inventory inv where inv.itemKey.id in (select ik.id from ItemKey ik  where ik.orderNumber in :orderNumbers ) and inv.quantity>0 ")
    List<Inventory> findByOrderNumbers(List<String> orderNumbers);
}
