/*
*  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.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @author liuxue
* @date 2023-07-26
**/
public interface PointRepository extends JpaRepository<Point, Long>, JpaSpecificationExecutor<Point> {
    @Query(value = "SELECT u.id,u.code FROM base_point p WHERE p.enabled=1 and p.status='FREE'", nativeQuery = true)
    public List<Map<String, String>> getKyPointList();
    @Query(value = "SELECT p FROM Point p WHERE p.code=?1", nativeQuery = false)
    Point findByCode(String code);
    @Query(value = "SELECT p FROM Point p WHERE p.area.name=?1 and p.status=?2 and p.enabled=true", nativeQuery = false)
    List<Point> findByAreaAndStatus(String areaName,String status);

    /** 库位信息化看板*/
    @Query(value = " select p.`code` 'pointCode',i.`code` 'itemCode' from base_point p  "
            +" left join base_area a on p.area_id=a.id "
            +" left join data_inventory inv on inv.point_id=p.id "
            +" left join data_item_key ik on ik.id=inv.item_key_id  "
            +" left join base_item i on i.id=ik.item_id "
            +" where a.`code`='存储区' and p.type='STORAGE' ", nativeQuery = true)
    List<Point> getDataPoint();

    /** 库位信息化看板 查询货位组*/
    @Query(value =" select p.pos_x,p.pos_y from base_point p  "
            +" left join base_area a on p.area_id=a.id "
            +" left join data_inventory inv on inv.point_id=p.id "
            +" left join data_item_key ik on ik.id=inv.item_key_id  "
            +" left join base_item i on i.id=ik.item_id "
            +" where a.`code`='存储区' and p.type='STORAGE' "
            +" group by p.pos_x,p.pos_y " , nativeQuery = true)
    List<Map<String, Object>> getDataPointTotal();
    /** 库位信息化看板 查询货架信息*/
    @Query(value =" select p.pos_x,p.pos_y from base_point p  "
            +" left join base_area a on p.area_id=a.id "
            +" left join data_inventory inv on inv.point_id=p.id "
            +" left join data_item_key ik on ik.id=inv.item_key_id  "
            +" left join base_item i on i.id=ik.item_id "
            +" where a.`code`='存储区' and p.type='STORAGE' "
            +" group by p.pos_x,p.pos_y  "
            +" order by p.pos_x,p.pos_y asc "
            +" LIMIT ?1,?2 ", nativeQuery = true)
    List<Map<String, Object>> getDataPointLimit(int currentPage, int pageSize);
        @Query(value =" select p.`code` 'pointCode',i.`code` 'itemCode' from base_point p  "
            +" left join base_area a on p.area_id=a.id "
            +" left join data_inventory inv on inv.point_id=p.id "
            +" left join data_item_key ik on ik.id=inv.item_key_id  "
            +" left join base_item i on i.id=ik.item_id "
            +" where a.`code`='存储区' and p.type='STORAGE' "
            +" and p.pos_x=?1 and p.pos_y=?2 ", nativeQuery = true)
    List<Map<String, Object>> getDataPointList(double x, double y);
    @Query(value = "SELECT p FROM Point p WHERE p.type=?1 and p.enabled=true", nativeQuery = false)
    List<Point> findByType(String type);

    List<Point> findByAreaCode(String areaCode);
}
