package com.ywt.domain.entity.glasses;

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;

public interface DictTypeRepository extends JpaRepository<DictType, Integer>, JpaSpecificationExecutor<DictType> {

    @Query(value = "select * from dict_type where deleted = 0 and type_code = ? order by weight desc, id desc;", nativeQuery = true)
    List<DictType> getDictType(String typeCode);


    @Query(value = "select * from dict_type where deleted = 0 and type_code = ? and shop_id = ? order by weight desc, id desc;", nativeQuery = true)
    List<DictType> getDictType(String typeCode, int shopId);

    @Query(value = "select * from dict_type where deleted = 0 and  id =? and type_code = ? and shop_id = ? order by weight desc, id desc;", nativeQuery = true)
    public DictType checkTypeById(int id, String typeCode, int shopId);

    @Query(value = "select * from dict_type where deleted = 0 and id =? and type_code = ? and shop_id = 0 order by weight desc, id desc;", nativeQuery = true)
    public DictType checkDictTypeById(int id, String typeCode);

    @Query(value = "select * from dict_type where deleted = 0 and type_code = 'TYPE_COMMON' and shop_id = ? order by weight desc, id desc;", nativeQuery = true)
    public List<DictType> showDictTypeList (int shopId);

    @Query(value = "SELECT COUNT(1) FROM  dict_type WHERE  item_name = ? AND  id != ? and  shop_id = ? and deleted = 0 ", nativeQuery = true)
    public int checkItemNameById(String itemName, int id, int shopId);

    @Query(value = "SELECT COUNT(1) FROM  dict_type WHERE  item_name = ? AND  id != ? and  shop_id = 0 and deleted = 0 ", nativeQuery = true)
    public int checkItemNameById(String itemName, int id);

    @Query(value = "SELECT COUNT(1) FROM  dict_type WHERE  item_name = ? and shop_id = ? and deleted = 0 ", nativeQuery = true)
    public int checkItemName(String itemName, int shopId);

    @Query(value = "SELECT COUNT(1) FROM  dict_type WHERE  item_name = ? and shop_id = 0 and deleted = 0 ", nativeQuery = true)
    public int checkItemName(String itemName);
}