<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.roadm.mapper.ChannelReturnBlackAutoCacheMapper">

    <resultMap id="channelReturnBlackAutoCacheResultMap" type="cn.roadm.bo.ChannelReturnBlackAutoCache">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="VARCHAR"/>
        <result column="error_code" property="errorCode" jdbcType="VARCHAR"/>
        <result column="cache_black" property="cacheBlack" jdbcType="VARCHAR"/>
        <result column="match_type" property="matchType" jdbcType="TINYINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="status" property="status" jdbcType="TINYINT"/>
        <result column="channel_name" property="channelName" jdbcType="VARCHAR"/>
        <result column="cache_black_name" property="cacheBlackName" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        c.id, c.channel_id, c.error_code, c.cache_black, c.match_type, c.create_time, c.status
    </sql>

    <select id="listPageChannelReturnBlackAutoCache" parameterType="cn.roadm.bo.ChannelReturnBlackAutoCache" 
            resultMap="channelReturnBlackAutoCacheResultMap">
        SELECT 
            <include refid="Base_Column_List"/>,
            COALESCE(ch.channel_name, '未知通道') as channel_name
        FROM channel_return_black_auto_cache c
        LEFT JOIN sms_send_channel_info ch ON c.channel_id = ch.id
        <where>
            AND c.status = 0
            <if test="channelId != null and channelId != ''">
                AND c.channel_id = #{channelId}
            </if>
            <if test="errorCode != null and errorCode != ''">
                AND c.error_code LIKE CONCAT('%', #{errorCode}, '%')
            </if>
            <if test="cacheBlack != null and cacheBlack != ''">
                AND c.cache_black = #{cacheBlack}
            </if>
            <if test="matchType != null">
                AND c.match_type = #{matchType}
            </if>
            <if test="channelName != null and channelName != ''">
                AND ch.channel_name LIKE CONCAT('%', #{channelName}, '%')
            </if>
        </where>
        ORDER BY c.create_time DESC, c.id DESC
    </select>

    <select id="getChannelReturnBlackAutoCacheById" parameterType="long" resultMap="channelReturnBlackAutoCacheResultMap">
        SELECT 
            <include refid="Base_Column_List"/>
        FROM channel_return_black_auto_cache c
        WHERE c.id = #{id} AND c.status = 0
    </select>

    <insert id="insertChannelReturnBlackAutoCache" parameterType="cn.roadm.bo.ChannelReturnBlackAutoCache" 
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO channel_return_black_auto_cache (
            channel_id,
            error_code,
            cache_black,
            match_type,
            status
        ) VALUES (
            #{channelId},
            #{errorCode},
            #{cacheBlack},
            #{matchType},
            0
        )
    </insert>

    <update id="updateChannelReturnBlackAutoCache" parameterType="cn.roadm.bo.ChannelReturnBlackAutoCache">
        UPDATE channel_return_black_auto_cache 
        SET 
            channel_id = #{channelId},
            error_code = #{errorCode},
            cache_black = #{cacheBlack},
            match_type = #{matchType}
        WHERE id = #{id}
    </update>

    <update id="deleteChannelReturnBlackAutoCache" parameterType="long">
        UPDATE channel_return_black_auto_cache SET status = 1 WHERE id = #{id}
    </update>

    <select id="getChannelList" resultType="map">
        SELECT
            id as channelId,
            channel_name as channelName
        FROM sms_send_channel_info
        WHERE status = 1
    </select>

    <select id="getBlacklistTypeList" resultType="map">
        SELECT 
            bt.id as blacklistTypeId,
            bt.blacklist_group_name as blacklistTypeName
        FROM sms_black_phone_type bt 
        WHERE 1=1
          AND bt.type IN (1,2)
        ORDER BY bt.id
    </select>

    <select id="getChannelReturnBlackAutoCacheByChannelAndErrorCode" 
            parameterType="cn.roadm.bo.ChannelReturnBlackAutoCache" 
            resultMap="channelReturnBlackAutoCacheResultMap">
        SELECT 
            <include refid="Base_Column_List"/>
        FROM channel_return_black_auto_cache c
        WHERE c.channel_id = #{channelId}
          AND c.status = 0
        <if test="id != null">
            AND c.id != #{id}
        </if>
        LIMIT 1
    </select>

</mapper> 