/*
*  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 me.zhengjie.modules.dm.dmCard.repository;

import me.zhengjie.modules.dm.dmCard.domain.DmCard;
import me.zhengjie.modules.dm.dmCard.service.dto.DmCardDto;
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;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author zhirong
* @date 2021-11-10
**/
public interface DmCardRepository extends JpaRepository<DmCard, Long>, JpaSpecificationExecutor<DmCard> {
    @Query(value = "SELECT u.* FROM dm_card u WHERE u.user_number = :userNumber AND if(:cardId != '',u.id != :cardId,1=1) ",nativeQuery = true)
    List<DmCard> findByDmUserId(@Param("userNumber")String userNumber, @Param("cardId") Long cardId);

    //DmCard findByCardNumber(String cardNumber);

    @Query(value = "SELECT id,card_number cardNumber,user_number FROM dm_card ",nativeQuery = true)
    List<Map<String,Object>> getAllByCardNumber();

    @Query(value = "SELECT * FROM dm_card WHERE card_number = :cardNumber AND status = '00' ",nativeQuery = true)
    DmCard findByCardNumber(@Param("cardNumber")String cardNumber);

    @Query(value = "SELECT * FROM dm_card WHERE user_number = :userNumber AND status = '00' ",nativeQuery = true)
    DmCard findByUserNumber(@Param("userNumber")String userNumber);
}