/*
 *  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.Point;
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;
import java.util.Set;

/**
 * @author liuxue
 * @website https://eladmin.vip
 * @date 2023-07-26
 **/
public interface PointRepository extends JpaRepository<Point, Long>, JpaSpecificationExecutor<Point> {

    @Query(" from Point p " +
            "where " +
            "p.enabled=true " +
            "and p.status='FREE' " +
            "and p.type = :type " +
            "and (:areaCode is null or p.area.code = :areaCode)")
    List<Point> findByTypeAndAreaCode(String type, String areaCode);

    @Query(" from Point p " +
            "where p.enabled=true " +
            "and p.status='FREE' " +
            "and p.type = :type " +
            "and (:areaCode is null or p.area.code = :areaCode)")
    Page<Point> findByTypeAndAreaCode(String type, String areaCode, Pageable pageable);

    @Query(" from Point point where point.enabled = true " +
            "and (:code IS NULL or point.code = :code) " +
            "and (:status IS NULL or point.status = :status) " +
            "and (:type IS NULL or point.type = :type) " +
            "and (:goodType IS NULL or point.description = :goodType) " +
            "and (:areaCode IS NULL or point.area.code = :areaCode) " +
            "and (:itemHeight IS NULL or point.itemHeight = :itemHeight)" +
            "order by point.posX desc,point.posY asc,point.posZ asc ")
    List<Point> findByCode(String code, String status, String type, String areaCode, String goodType, Double itemHeight);

    @Query(" from Point p where p.code in :pointCodes")
    List<Point> findByCodes(Set pointCodes);

    @Query(" from Point p where p.beatCode = :beatCode")
    List<Point> findByByBeatCode(String beatCode);
}
