package com.ywt.mg.domain.ywtDrugEntities;

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;

public interface PharmacyRepository extends JpaRepository<Pharmacy, Integer>, JpaSpecificationExecutor<Pharmacy> {

    /**
     * 得到药房list（不包括删除和不显示的）
     * @return {@link List <Pharmacy>}
     */
    @Query(value = "select * from pharmacy where deleted = 0 and display = 0;", nativeQuery = true)
    public List<Pharmacy> getPharmacyList();

    /**
     * 获取该药房及子药房的列表
     *
     * @param pharmacyId 药房 id
     * @return {@link Pharmacy}
     */
    @Query(value = "select * from pharmacy where deleted = 0 and (id = :pId or tree_code like concat(:pId, '|%') or tree_code like concat('%|', :pId, '|%'));", nativeQuery = true)
    List<Pharmacy> getDecendantPharmacyList(@Param("pId") int pharmacyId);

    /**
     * 判断是否存在药房名称相同的药房
     *
     * @param id   药房ID
     * @param name 药房名称
     * @return 数量
     */
    @Query(value = "select count(1) from pharmacy where deleted = 0 and id != ? and name = ? ;", nativeQuery = true)
    public int checkExistName(int id, String name);

    /**
     * 根据药房ID的药房对象
     *
     * @param id 药房ID
     * @return {@link Pharmacy}
     */
    @Query(value = "select * from pharmacy where id = ? and deleted = 0 limit 1;", nativeQuery = true)
    Pharmacy getPharmacyById(int id);

    /**
     * 根据父类ID得到对象
     *
     * @param id 父类ID
     * @return {@link Pharmacy}
     */
    @Query(value = "select * from pharmacy where parent_id = ? and deleted = 0 limit 1;", nativeQuery = true)
    Pharmacy getPharmacyByParentId(int id);

    /**
     * 根据药房ID得到药房以及自药房(不包括删除和不显示的)
     *
     * @param id 药房ID
     * @param id 父类ID
     * @return {@link  List<Pharmacy>}
     */
    @Query(value = "select * from pharmacy where id = ? or parent_id = ? and deleted = 0;", nativeQuery = true)
    List<Pharmacy> getPharmacyAndSonList(int id, int parentId);

    /**
     * 得到所有药房list（不包括删除的）
     * @return {@link List<Pharmacy>}
     */
    @Query(value = "select * from pharmacy where deleted = 0;", nativeQuery = true)
    public List<Pharmacy> getAllPharmacyList();

}