<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.rightsSetting.dao.impl.RightsNowItemDaoImpl">

    <resultMap  id="BaseResultMap" type="com.pcloud.book.rightsSetting.entity.RightsNowItem">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="rightsSettingId" column="rights_setting_id" jdbcType="BIGINT"/>
        <result property="serveId" column="serve_id" jdbcType="BIGINT"/>
        <result property="serveType" column="serve_type" jdbcType="VARCHAR"/>
        <result property="linkUrl" column="link_url" jdbcType="VARCHAR"/>
        <result property="type" column="type" jdbcType="BIGINT"/>
        <result property="giftCouponPackageId" column="gift_coupon_package_id" jdbcType="BIGINT"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="rightsItemGroupId" column="rights_item_group_id" jdbcType="BIGINT"/>
        <result property="rightsSettingTitleId" column="rights_setting_title_id" jdbcType="BIGINT"/>
        <result property="groupType" column="group_type" jdbcType="VARCHAR"/>
        <result property="isShow" column="is_show" jdbcType="TINYINT"/>
        <result property="showUrl" column="show_url" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, rights_setting_id, serve_id, serve_type, link_url, type, gift_coupon_package_id, create_time,
        rights_item_group_id,rights_setting_title_id,group_type, is_show, show_url
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from rights_now_item
        where id = #{id,jdbcType=BIGINT}
    </select>

    <delete id="deleteById" parameterType="long">
        delete from rights_now_item
        where id = #{id,jdbcType=BIGINT}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.rightsSetting.entity.RightsNowItem" useGeneratedKeys="true" keyProperty="id">
        insert into rights_now_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            rights_setting_id, serve_id, serve_type, link_url, type, gift_coupon_package_id, create_time, rights_item_group_id, is_show, show_url
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{rightsSettingId,jdbcType=BIGINT},
            #{serveId,jdbcType=BIGINT},
            #{serveType,jdbcType=VARCHAR},
            #{linkUrl,jdbcType=VARCHAR},
            #{type,jdbcType=INTEGER},
            #{giftCouponPackageId,jdbcType=BIGINT},
            NOW(),
            #{rightsItemGroupId,jdbcType=BIGINT},
            #{isShow},
            #{showUrl}
        </trim>
    </insert>

    <insert id="batchInsert"  parameterType="com.pcloud.book.rightsSetting.entity.RightsNowItem" useGeneratedKeys="true" keyProperty="id">
        insert into rights_now_item (
        rights_setting_id, serve_id, serve_type, link_url, type, gift_coupon_package_id, create_time, rights_item_group_id,
        rights_setting_title_id,group_type, is_show, show_url
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.rightsSettingId,jdbcType=BIGINT},
            #{item.serveId,jdbcType=BIGINT},
            #{item.serveType,jdbcType=VARCHAR},
            #{item.linkUrl,jdbcType=VARCHAR},
            #{item.type,jdbcType=INTEGER},
            #{item.giftCouponPackageId,jdbcType=BIGINT},
            NOW(),
            #{item.rightsItemGroupId,jdbcType=BIGINT},
            #{item.rightsSettingTitleId},
            #{item.groupType,jdbcType=VARCHAR},
            #{item.isShow},
            #{item.showUrl}
            )
        </foreach>
    </insert>

    <delete id="deleteByRightsSettingId" parameterType="long">
        delete from rights_now_item
        where rights_setting_id=#{rightsSettingId}
    </delete>

    <select id="getListByRightsSettingId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from
        rights_now_item
        where rights_setting_id=#{rightsSettingId}
        <if test="types!=null and types.size()>0">
            and type in
            <foreach collection="types" item="item" open="(" separator="," close=")">
                ${item}
            </foreach>
        </if>
    </select>

    <select id="getGiftCouponListByRightsSettingId" parameterType="map" resultType="com.pcloud.book.rightsSetting.entity.RightsNowItem">
        SELECT
        t.id,
        t.rights_setting_id rightsSettingId,
        t.serve_id serveId,
        t.serve_type serveType,
        t.link_url linkUrl,
        t.type,
        t.gift_coupon_package_id giftCouponPackageId,
        t.create_time createTime,
        t1.title giftCouponTitle,
        t1.cover_pic giftCouponCoverPic,
        t1.denomination giftCouponDenomination,
        t1.valid_date_begin giftCouponValidDateBegin,
        t1.valid_date_end giftCouponValidDateEnd,
        t1.coupon_type couponType,
        t1.instructions instructions,
        t1.use_type useType,
        t1.exchange_address exchangeAddress
        FROM
        rights_now_item t
        INNER JOIN gift_coupon_package t1 ON t.gift_coupon_package_id = t1.id
        WHERE
        t1.is_delete = 0
        AND t.rights_setting_id = #{rightsSettingId}
        and now() <![CDATA[ <= ]]> t1.valid_date_end
    </select>

    <select id="getListByGroupIds" parameterType="map" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        from rights_now_item t
        where rights_item_group_id in
        <foreach collection="groupIds" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>

    <select id="getListByRightsSettingTitleId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from
        rights_now_item
        where rights_setting_title_id=#{rightsSettingTitleId}
        <if test="types!=null and types.size()>0">
            and type in
            <foreach collection="types" item="item" open="(" separator="," close=")">
                ${item}
            </foreach>
        </if>
    </select>

    <select id="getGiftCouponListByRightsSettingTitleId" parameterType="map" resultType="com.pcloud.book.rightsSetting.entity.RightsNowItem">
    SELECT
    t.id,
    t.rights_setting_id rightsSettingId,
    t.serve_id serveId,
    t.serve_type serveType,
    t.link_url linkUrl,
    t.type,
    t.gift_coupon_package_id giftCouponPackageId,
    t.create_time createTime,
    t1.title giftCouponTitle,
    t1.cover_pic giftCouponCoverPic,
    t1.denomination giftCouponDenomination,
    t1.valid_date_begin giftCouponValidDateBegin,
    t1.valid_date_end giftCouponValidDateEnd,
    t1.coupon_type couponType,
    t1.instructions instructions,
    t1.use_type useType,
    t1.exchange_address exchangeAddress
    FROM
    rights_now_item t
    INNER JOIN gift_coupon_package t1 ON t.gift_coupon_package_id = t1.id
    WHERE
    t1.is_delete = 0
    AND t.rights_setting_title_id = #{rightsSettingTitleId}
    and now() <![CDATA[ <= ]]> t1.valid_date_end
    </select>

    <select id="getAdviserGroupIds" parameterType="list" resultType="long">
        select
        DISTINCT(serve_id)
        from
        rights_now_item
        where
        type = 6
        and
        serve_type = 'GROUP'
        and
        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_now_item
        where
        type = 6
        and
        serve_type = 'GROUP'
        and
        (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_now_item
        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_now_item
        where
        serve_type = "NEWS"
        and
        serve_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getByActivityGroupIds" parameterType="list" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        rights_now_item
        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_now_item
        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>
    
    <select id="getItemListByRightsSettingId" parameterType="map" resultMap="BaseResultMap">
        SELECT
        d.id, d.rights_setting_id, d.serve_id, d.serve_type, d.link_url, d.type, d.gift_coupon_package_id, d.create_time,
        d.rights_item_group_id,d.rights_setting_title_id,d.group_type, d.is_show, d.show_url
        FROM
        rights_now_item d
        LEFT JOIN rights_setting_title t ON d.rights_setting_title_id = t.id
        WHERE
        t.rights_setting_id = #{rightsSettingId}
        <if test="openState != null">
            AND t.open_state = #{openState}
        </if>
        <if test="rightsNowItemType != null">
            AND t.rights_setting_now_type = #{rightsNowItemType}
        </if>
        <choose>
            <when test="readType==1">
                and t.easy_read=1
            </when>
            <when test="readType==2">
                and it.efficient_read=1
            </when>
            <when test="readType==3">
                and it.deep_read=1
            </when>
        </choose>
        <if test="serveType != null">
            AND d.serve_type=#{serveType}
        </if>
        GROUP BY d.serve_id, d.serve_type
        ORDER BY d.id
    </select>


    <delete id="deletePackageIds" parameterType="list">
        delete
        from
        rights_now_item
        where
        serve_type = "GIFT"
        and
        gift_coupon_package_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
</mapper>