<?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="com.pcloud.book.rightsSetting.dao.impl.RightsSettingItemDetailDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.rightsSetting.entity.RightsSettingItemDetail">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="rights_setting_item_id" property="rightsSettingItemId" jdbcType="BIGINT"/>
        <result column="serve_id" property="serveId" jdbcType="BIGINT"/>
        <result column="serve_type" property="serveType" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="group_type" property="groupType" jdbcType="TINYINT"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, rights_setting_item_id, serve_id, serve_type, create_time, group_type
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM rights_setting_item_detail
        WHERE id = #{id}
    </select>

    <select id="getByItemIds" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM rights_setting_item_detail
        WHERE rights_setting_item_id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM rights_setting_item_detail
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO rights_setting_item_detail(
            rights_setting_item_id, 
            serve_id, 
            serve_type, 
            create_time,
            group_type
        ) VALUES (
            #{rightsSettingItemId, jdbcType=BIGINT}, 
            #{serveId, jdbcType=BIGINT}, 
            #{serveType, jdbcType=VARCHAR}, 
            NOW(),
            group_type
        )
    </insert>
    
    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO rights_setting_item_detail (
            rights_setting_item_id, 
            serve_id, 
            serve_type, 
            create_time,
        group_type
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.rightsSettingItemId, jdbcType=BIGINT}, 
            #{item.serveId, jdbcType=BIGINT}, 
            #{item.serveType, jdbcType=VARCHAR}, 
            NOW(),
            #{item.groupType}
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE rights_setting_item_detail
        <set>
            <if test="rightsSettingItemId != null">
                rights_setting_item_id = #{rightsSettingItemId},
            </if>
            <if test="serveId != null">
                serve_id = #{serveId},
            </if>
            <if test="serveType != null and serveType != ''">
                serve_type = #{serveType},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="group_type != null">
                group_type = #{groupType}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM rights_setting_item_detail where id = #{id}
    </delete>

    <delete id="deleteByItemIds">
        DELETE FROM rights_setting_item_detail where
        rights_setting_item_id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </delete>

    <select id="getAdviserGroupIds" parameterType="list" resultType="long">
        select
        serve_id
        from
        rights_setting_item_detail
        where
        group_type = 2
        and
        serve_id in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="getActivityGroupIds" parameterType="list" resultType="long">
        select
        serve_id
        from
        rights_setting_item_detail
        where
        group_type &lt;&gt; 2 or group_type is null
        and
        serve_id in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <delete id="deleteInvalidNews" parameterType="list">
        delete
        from
        rights_setting_item_detail
        where
        serve_type = "NEWS"
        and
        serve_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <select id="getByNewsIds" parameterType="list" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        rights_setting_item_detail
        where
        serve_type = "NEWS"
        and
        serve_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getByActivityIds" parameterType="list" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        rights_setting_item_detail
        where
        serve_type = "GROUP"
        and
        (
        group_type is null or
        group_type &lt;&gt; 2
        )
        and
        serve_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <delete id="deleteInvalidActivityGroups" parameterType="list">
        delete
        from
        rights_setting_item_detail
        where
        serve_type = "GROUP"
        and
        (
        group_type is null or
        group_type &lt;&gt; 2
        )
        and
        serve_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

</mapper>