<?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.com.ctop.kuaishou.modules.channel.mapper.KuaishouChannelItemsMapper">


    <insert id="replaceBatch">
        replace into ctop_kuaishou_channel_items (
        id,
        project_id,
        project_name,
        state,
        item_name,
        keyword,
        user_id,
        state_date
        )
        values
        <foreach collection="itemList" item="item" separator=",">
            (
            #{item.id},
            #{item.projectId},
            #{item.projectName},
            #{item.state},
            #{item.itemName},
            #{item.keyword},
            #{item.userId},
            #{item.stateDate}
            )
        </foreach>
    </insert>

    <select id="queryChannelItemList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        id,
        project_id,
        project_name,
        item_name,
        keyword,
        state,
        create_time
        FROM ctop_kuaishou_channel_items
        <where>
            <if test="projectId != null">
                and project_id = #{projectId}
            </if>
            <if test="itemName != null">
                AND LOCATE(#{itemName},item_name)
            </if>
            <if test="keyword != null">
                AND LOCATE(#{keyword},keyword)
            </if>
            <if test="state != null">
                AND state = #{state}
            </if>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="queryAllChannelItemByProjectId"
            resultType="cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannelItems">
        SELECT id,
               item_name,
               keyword
        FROM ctop_kuaishou_channel_items
        WHERE project_id = #{projectId}
          AND state = 1
        ORDER BY create_time DESC
    </select>

    <select id="queryByProjectIdAndName" resultType="cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannelItems">
        SELECT item_name
        FROM ctop_kuaishou_channel_items
        WHERE project_id = #{projectId}
          AND item_name = #{itemName} limit 1
    </select>


</mapper>