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

import com.youchain.basicdata.domain.BomAccount;
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;

/**
* @website https://eladmin.vip
* @author baobinglin
* @date 2024-01-02
**/
public interface BomAccountRepository extends JpaRepository<BomAccount, Long>, JpaSpecificationExecutor<BomAccount> {
    /**
     * 唯一 查询
     * @param big_item_id
     * @param item_id
     * @return
     */
    @Query(value = "SELECT * FROM base_bom_account ba WHERE ba.big_item_id=?1 and ba.item_id=?2 and ba.r_area_id=?3 and ba.z_point_id=?4", nativeQuery = true)
    BomAccount findByOnly(Long big_item_id,Long item_id,Long r_area_id,Long z_point_id);

    /**
     * 查询说有的 接收仓库 为true的 bigitem
     * @return
     */
    @Query(value = "SELECT * FROM `base_bom_account` b where b.r_area_id in (select a.id from base_area a where a.bexb=1)  GROUP BY b.big_item_id", nativeQuery = true)
    List<BomAccount> queryBomAccountsBIidOne();

    @Query(value = "SELECT * FROM `base_bom_account` b where b.r_area_id=?1 and b.item_id=?2 and b.z_point_id=?3", nativeQuery = true)
    List<BomAccount> queryBomUnique(Long area_id, Long item_id, Long poin_id);
    @Query(value = "SELECT IFNULL(sum(pc_qty),0) FROM `base_bom_account` b where b.r_area_id=?1 and b.item_id=?2 and b.z_point_id=?3", nativeQuery = true)
    Double getPcQty(Long area_id, Long item_id, Long point_id);

    @Query(value = "SELECT * FROM `base_bom_account` b where b.r_area_id=?1 and b.item_id=?2", nativeQuery = true)
    List<BomAccount> getBomList(Long area_id, Long item_id);

    @Query(value = "SELECT * FROM `base_bom_account` b where  b.item_id=?1", nativeQuery = true)
    List<BomAccount> getItemBomList( Long item_id);
    @Query(value = "SELECT IFNULL(max(b.c_area_id),0) FROM `base_bom_account` b where  b.item_id=?1", nativeQuery = true)
    Long getItemAreaId( Long item_id);

}