/*
 *  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.BillType;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.ProductionPlan;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author JiangKun
 * @website https://eladmin.vip
 * @date 2024-01-04
 **/
public interface ProductionPlanRepository extends JpaRepository<ProductionPlan, Long>, JpaSpecificationExecutor<ProductionPlan> {
    /**
     * 根据制造线+IDNO查找ProductionPlan是否存在记录
     * @param idno
     * @return
     */
    @Query(value = "SELECT * FROM base_production_plan p WHERE  p.idno = :idno", nativeQuery = true)
    List<ProductionPlan> findByIDNO( @Param("idno") String idno);
    /**查找目前最大的序号*/
    @Query(value = "SELECT max(order_no) FROM `base_production_plan` bpp ", nativeQuery = true)
    Long getMaxOrderNo();



    /**
     * @param idno
     * @return
     */
    @Query(value = "SELECT * FROM base_production_plan p WHERE   p.make_line=:make_line and p.idno >= :idno and p.working_station in :workingStation", nativeQuery = true)
    List<ProductionPlan> findByGwIdnoList( @Param("make_line") String make_line,@Param("idno") String idno,@Param("workingStation") String[] workingStation);
    @Query(value = "SELECT * FROM base_production_plan p WHERE  p.make_line=:make_line and p.IDNO >= :IDNO", nativeQuery = true)
    List<ProductionPlan> findByIdnoList( @Param("make_line") String make_line,@Param("IDNO") String IDNO);
    @Query(value = "SELECT count(id) FROM base_production_plan p WHERE   p.make_line=:make_line and p.IDNO >= :IDNO and p.working_station in :workingStation and p.statue!='UN_CONSUME'", nativeQuery = true)
    int existsGwHyIdno( @Param("make_line") String make_line, @Param("IDNO") String IDNO,@Param("workingStation") String[] workingStation);

    @Query(value = "SELECT count(id) FROM base_production_plan p WHERE p.make_line=:make_line and  p.IDNO >= :IDNO and p.statue!='UN_CONSUME'", nativeQuery = true)
    int existsHyIdno( @Param("make_line") String make_line, @Param("IDNO") String IDNO);


    /**
     * 查询可以生成的数据
     */
    @Query(value = "SELECT * FROM `base_production_plan` bpp where bpp.working_station = :workingStation and bpp.statue='UN_CONSUME'  AND bpp.`IDNO`>= :IDNO  order by bpp.`IDNO` ASC", nativeQuery = true)
    List<ProductionPlan> getAllData(@Param("workingStation") String workingStation, @Param("IDNO")String IDNO);

    @Query(value = "SELECT * FROM `base_production_plan` bpp where bpp.working_station = :workingStation and bpp.statue='UN_CONSUME'  AND bpp.`IDNO`<= :IDNO  order by bpp.`IDNO` ASC", nativeQuery = true)
    List<ProductionPlan> getIdNoAllData(@Param("workingStation") String workingStation, @Param("IDNO")String IDNO);

    @Query(value = "SELECT bpp.working_station FROM `base_production_plan` bpp where bpp.`IDNO`<= :IDNO  group by bpp.working_station", nativeQuery = true)
    List<String> getIdNoWhyGw( @Param("IDNO")String IDNO);

    @Query(value = "SELECT * FROM `base_production_plan` bpp where bpp.working_station = ?1 and bpp.statue='UN_CONSUME'  LIMIT ?2  ", nativeQuery = true)
    List<ProductionPlan> getLimitPlan(String workingStation,int num);

    @Query(value = "SELECT * FROM `base_production_plan` bpp where bpp.working_station = :workingStation and bpp.statue='UN_CONSUME' AND bpp.`IDNO`>= :IDNO1   AND bpp.`IDNO`<= :IDNO2  order by bpp.`IDNO` ASC", nativeQuery = true)
    List<ProductionPlan> getList(@Param("workingStation") String workingStation, @Param("IDNO1")String IDNO1, @Param("IDNO2")String IDNO2);

    @Query(value = "SELECT max(bpp.IDNO) FROM `base_production_plan` bpp where bpp.working_station = :workingStation and bpp.statue='UN_CONSUME'", nativeQuery = true)
    String getIdNoWhyScGw( @Param("workingStation") String workingStation);

}