/*
*  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.Area;
import com.youchain.basicdata.domain.BigItem;
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.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @website https://eladmin.vip
* @author HJL
* @date 2023-08-14
**/
public interface AreaRepository extends JpaRepository<Area, Long>, JpaSpecificationExecutor<Area> {
    @Query(value = "SELECT * FROM base_area", nativeQuery = true)
    List<Area> getAreas();
    @Query(value = "SELECT * FROM base_area area where area.code =:code", nativeQuery = true)
    Area getByCode(@Param("code")String code);

    @Query(value = "SELECT * FROM base_area area where area.name =:name", nativeQuery = true)
    Area getByName(@Param("name")String name);

    @Query(value = "SELECT * FROM base_area area where area.code =:code or area.name=:name", nativeQuery = true)
    Area findByCodeOrName(@Param("code")String code,@Param("name")String name);

    @Query(value = "SELECT * FROM base_area area where area.code =:code and area.bexb=1", nativeQuery = true)
    Area getJsKq(@Param("code")String code);

    /**
     * 将working_station字段设置位null
     * @param id
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE `base_area` area SET area.working_station = NULL where id = :id", nativeQuery = true)
    void updateWorkingStationISNull(@Param("id") Long id);

    @Query(value = "SELECT * FROM base_area t where t.code in ('A仓','S仓')", nativeQuery = true)
    List<Area> getBomOutAreas();

    @Query("from Area i where (i.name in (:codes)  or i.code in (:codes) ) and i.enabled=true ")
    List<Area> queryByAreaCodes(List<String> codes);
}