<?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.giftcoupon.dao.impl.GiftReceiveDaoImpl">

    <resultMap type="com.pcloud.book.giftcoupon.entity.GiftReceive" id="GiftReceiveMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="wechatUserId" column="wechat_user_id" jdbcType="INTEGER"/>
        <result property="giftPackageId" column="gift_package_id" jdbcType="INTEGER"/>
        <result property="rightsSettingId" column="rights_setting_id" jdbcType="INTEGER"/>
        <result property="useTime" column="use_time" jdbcType="TIMESTAMP"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, wechat_user_id, gift_package_id, rights_setting_id, use_time, create_time, update_time
    </sql>

    <!--查询单个-->
    <select id="getById" resultMap="GiftReceiveMap">
        select
          id, wechat_user_id, gift_package_id, rights_setting_id, use_time, create_time, update_time
        from book.gift_receive
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="GiftReceiveMap">
        select
          id, wechat_user_id, gift_package_id, rights_setting_id, use_time, create_time, update_time
        from book.gift_receive
        limit #{offset}, #{limit}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="GiftReceiveMap">
        select
          id, wechat_user_id, gift_package_id, rights_setting_id, use_time, create_time, update_time
        from book.gift_receive
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="wechatUserId != null">
                and wechat_user_id = #{wechatUserId}
            </if>
            <if test="giftPackageId != null">
                and gift_package_id = #{giftPackageId}
            </if>
            <if test="rightsSettingId != null">
                and rights_setting_id = #{rightsSettingId}
            </if>
            <if test="useTime != null">
                and use_time = #{useTime}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" parameterType="com.pcloud.book.giftcoupon.entity.GiftReceive" keyProperty="id" useGeneratedKeys="true">
        insert into book.gift_receive(wechat_user_id, gift_package_id, rights_setting_id, use_time, create_time, update_time)
        values (#{wechatUserId}, #{giftPackageId}, #{rightsSettingId}, #{useTime}, now(), now())
    </insert>

    <insert id="batchInsert" parameterType="com.pcloud.book.giftcoupon.entity.GiftReceive" keyProperty="id" useGeneratedKeys="true">
        insert into book.gift_receive(wechat_user_id, gift_package_id, rights_setting_id, use_time, create_time, update_time)
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (#{item.wechatUserId}, #{item.giftPackageId}, #{item.rightsSettingId}, #{item.useTime}, now(), now())
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.gift_receive
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="giftPackageId != null">
                gift_package_id = #{giftPackageId},
            </if>
            <if test="useTime != null">
                use_time = #{useTime},
            </if>
            <if test="rightsSettingId != null">
                rights_setting_id = #{rightsSettingId},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
                update_time = #{updateTime},
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from book.gift_receive where id = #{id}
    </delete>

    <select id="getGiftReceive" parameterType="map" resultMap="GiftReceiveMap">
        select
           id, wechat_user_id, gift_package_id, rights_setting_id, use_time, create_time, update_time
        from book.gift_receive
        where wechat_user_id=#{wechatUserId}
        and gift_package_id=#{giftPackageId}
        limit 1
    </select>

    <select id="getGiftReceiveCount" parameterType="map" resultType="Integer">
        select
           ifnull(count(1),0)
        from gift_receive
        where wechat_user_id=#{wechatUserId}
        and gift_package_id=#{giftPackageId}
    </select>

    <select id="useGiftCoupon" parameterType="map">
        update gift_receive
        set use_time=now(),update_time=now()
        where id=#{receiveId}
    </select>

    <select id="list4MyGiftPackage" parameterType="map" resultType="com.pcloud.book.giftcoupon.dto.GiftPackageDTO">
        select
        b.id receiveId,
        b.use_time useTime,
        a.title title,
        a.cover_pic coverPic,
        a.denomination denomination,
        a.valid_date_begin validDateBegin,
        a.valid_date_end validDateEnd,
        a.id id,
        a.coupon_type couponType,
        a.instructions instructions,
        a.use_type useType,
        a.resource_desc resourceDesc,
        a.price price,
        a.crossed_price crossedPrice,
        a.exchange_address exchangeAddress,
        <if test="state!=null and state==2">
            2 failureState,
        </if>
        <if test="state!=null and state==3">
            1 failureState,
        </if>
        a.serve_id serveId,
        a.serve_type serveType,
        a.link_url linkUrl,
        a.third_resource_id thirdResourceId,
        a.on_shelves onShelves
        from
        gift_receive b LEFT JOIN gift_coupon_package a
        on a.id=b.gift_package_id
        where b.wechat_user_id=#{wechatUserId}
        <if test="state!=null and state==1">
            and now() <![CDATA[ <= ]]> a.valid_date_end and use_time is null
        </if>
        <if test="state!=null and state==2">
            and now() > a.valid_date_end and use_time is null
        </if>
        <if test="state!=null and state==3">
            and use_time is not null
        </if>
            order by b.create_time desc
    </select>

    <select id="getGiftReceiveNumList" parameterType="map" resultType="com.pcloud.book.giftcoupon.dto.GiftReceiveDTO">
        select gift_package_id giftPackageId,
        ifnull(count(1),0) receiveNum
        from gift_receive
        where gift_package_id in
        <foreach collection="giftPackageIds" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
        group by gift_package_id
    </select>

    <select id="getUserReceiveGiftId" parameterType="map" resultType="long">
        select distinct gift_package_id
        from gift_receive
        where  wechat_user_id=#{wechatUserId}
        and rights_setting_id=#{rightsSettingId}
    </select>

    <select id="getMapByIds" parameterType="map" resultMap="GiftReceiveMap">
        select <include refid="Base_Column_List"/>
        from gift_receive
        where wechat_user_id=#{wechatUserId}
        and gift_package_id in
        <foreach collection="giftPackageIds" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>

    <select id="getOverLimitCoupon" parameterType="map" resultType="com.pcloud.book.giftcoupon.entity.GiftCouponPackage">
       select b.receive_limit receiveLimit ,a.gift_package_id id,b.title title
       from gift_receive a
       left join gift_coupon_package b
       on a.gift_package_id=b.id
       where
       a.wechat_user_id=#{wechatUserId}
       and a.gift_package_id in
        <foreach collection="giftPackageIds" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
       GROUP BY a.gift_package_id HAVING count(a.id)>=b.receive_limit
    </select>

    <select id="getGiftAllReceiveCount" parameterType="map" resultType="Integer">
        select ifnull(count(1),0)
        from gift_receive a
         left join gift_coupon_package b
         on a.gift_package_id=b.id
        where a.wechat_user_id=#{wechatUserId}
        and a.use_time is null
        and b.valid_date_end > now()
    </select>

    <select id="getReceivedIds" parameterType="map" resultType="Long">
        select distinct  gift_package_id
        from gift_receive
        where wechat_user_id=#{wechatUserId}
        and gift_package_id in
        <foreach collection="giftPackageIds" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>

    <select id="getReceivedCount" parameterType="map" resultType="com.pcloud.book.giftcoupon.dto.GiftReceiveDTO">
            select
             gift_package_id giftPackageId,
             count(1) receiveNum,
             sum(if((a.use_time is not null or now() > b.valid_date_end),1,0)) failureNum
            from gift_receive a
            LEFT JOIN gift_coupon_package b
            on a.gift_package_id=b.id
            where
            a.wechat_user_id=#{wechatUserId}
            and a.gift_package_id in
            <foreach collection="giftPackageIds" item="item" index="index" open="(" separator="," close=")">
            ${item}
            </foreach>
            GROUP BY a.gift_package_id;
    </select>

</mapper>