package com.crm.pay.dao.repository;

import com.crm.rely.backend.dao.repository.BaseRepository;
import com.crm.rely.backend.model.pojo.table.SysChannelBankTable;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;
import java.util.List;

/**
 * @author: houn
 */
@Repository
public interface SysChannelBankRepository extends BaseRepository<SysChannelBankTable> {


    /**
     * 根据通道code获取银行列表信息(根据subIndex排序)
     *
     * @param channelCode 通道code
     * @param valid       是否启用
     * @return
     */
    List<SysChannelBankTable> getAllByChannelCodeAndValidOrderBySubIndex(String channelCode, Integer valid);

    List<SysChannelBankTable> getAllByChannelCodeAndCodeInAndValidOrderBySubIndex(String channelCode,
                                                                                  List<String> codes, Integer valid);

    @Query(value = "SELECT * FROM sys_channel_bank " +
            "WHERE channel_code = :channelCode " +
            "AND valid = :valid " +
            "AND (countrys IS NULL OR countrys = '' OR CONCAT(',', countrys, ',') LIKE CONCAT('%,', :country, ',%')) " +
            "ORDER BY sub_index",
            nativeQuery = true)
    List<SysChannelBankTable> getAllByChannelCodeAndValidAndCountry(
            @Param("channelCode") String channelCode,
            @Param("valid") Integer valid,
            @Param("country") String country

    );

    @Query(value = "SELECT * FROM sys_channel_bank " +
            "WHERE channel_code = :channelCode " +
            "AND valid = :valid " +
            "AND ( `code` IN :bankCodes) " +
            "AND (countrys IS NULL OR countrys = '' OR CONCAT(',', countrys, ',') LIKE CONCAT('%,', :country, ',%')) " +
            "ORDER BY sub_index",
            nativeQuery = true)
    List<SysChannelBankTable> getAllByChannelCodeAndValidAndCountry(
            @Param("channelCode") String channelCode,
            @Param("valid") Integer valid,
            @Param("country") String country,
            @Param("bankCodes") List<String> bankCodes

    );


    /**
     * 根据银行code获取银行信息
     *
     * @param code  银行code
     * @param valid 是否启用
     * @return
     */
    SysChannelBankTable getFirstByCodeAndChannelCodeAndValid(String code, String channelCode, Integer valid);

    List<SysChannelBankTable> getAllByCode(String code);

    List<SysChannelBankTable> getAllByChannelCode(String channelCode);

    List<SysChannelBankTable> getAllByChannelCodeIn(List<String> channelCodes);

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    List<SysChannelBankTable> findAllByChannelCode(String channelCode);

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    List<SysChannelBankTable> findAllByIdIn(List<Long> ids);

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    List<SysChannelBankTable> findAllByChannelCodeIn(List<String> channelCodes);

    void deleteByIdNotIn(List<Long> ids);
}
