<?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.diaoyun.zion.chinafrica.dao.TbCfIssueCouponDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntity" id="tbCfIssueCouponMap">
        <result property="couponIssuetableId" column="coupon_issuetable_id"/>
        <result property="couponId" column="coupon_id"/>
        <result property="couponTitle" column="coupon_Title"/>
        <result property="couponCategoryName" column="coupon_category_name"/>
        <result property="userId" column="user_id"/>
        <result property="nick" column="nick"/>
        <result property="createTime" column="create_time"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="issueId" column="issue_id"/>
    </resultMap>
    <update id="changeCoupnStatus" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntity">
             update  tb_cf_issue_coupon t set  enable_flag = 0  where  t.user_id=#{userId} and  t.coupon_id=#{couponId}
    </update>
    <select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntity">
		select
			`coupon_issuetable_id`,
			`coupon_id`,
			`user_id`,
			`nick`,
			`create_time`,
			`enable_flag`,
			`issue_id`
		from tb_cf_issue_coupon
		where coupon_issuetable_id = #{id}
	</select>

    <select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntity">
        select
        a.coupon_issuetable_id,
        a.coupon_id,
        b.coupon_category_id,
        c.coupon_category_name,
        b.coupon_title,
        a.user_id,
        a.nick,
        a.create_time,
        a.enable_flag,
        a.issue_id
        from tb_cf_issue_coupon a LEFT join tb_cf_coupon b ON a.coupon_id=b.coupon_id
        LEFT JOIN tb_cf_coupon_category c on b.coupon_category_id=c.coupon_category_id
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by coupon_issuetable_id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>
    <!--查询用户有效的优惠券-->
    <select id="queryCouponByUserId" resultType="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntityExtends">
            select
            a.coupon_issuetable_id,
            a.coupon_id,
            b.coupon_category_id,
            c.coupon_category_name,
            b.coupon_title,
            b.with_amount amount,
            b.deduct_amount money,
            b.valid_start_time start,
            b.valid_end_time end,
            a.enable_flag status,
            a.user_id,
            a.nick,
            a.create_time,
            a.enable_flag,
            a.issue_id
            from tb_cf_issue_coupon a LEFT  join  tb_cf_coupon b ON a.coupon_id=b.coupon_id
            LEFT JOIN tb_cf_coupon_category c on b.coupon_category_id=c.coupon_category_id
            WHERE a.user_id = #{user_id} and a.enable_flag=1 and b.status=1
    </select>

    <!--查询用户已使用的优惠券-->
    <select id="queryCouponByUserIdAndUsed"
            resultType="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntityExtends">
            select
            a.coupon_issuetable_id,
            a.coupon_id,
            b.coupon_category_id,
            c.coupon_category_name,
            b.coupon_title,
            b.with_amount amount,
            b.deduct_amount money,
            b.valid_start_time start,
            b.valid_end_time end,
            a.enable_flag status,
            a.user_id,
            a.nick,
            a.create_time,
            a.enable_flag,
            a.issue_id
            from tb_cf_issue_coupon a LEFT  join  tb_cf_coupon b ON a.coupon_id=b.coupon_id
            LEFT JOIN tb_cf_coupon_category c on b.coupon_category_id=c.coupon_category_id
            WHERE a.user_id = #{user_id} and a.enable_flag=0 or b.status=0
    </select>
    <select id="queryTotal" resultType="int">
        select count(*) from tb_cf_issue_coupon
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
    </select>

    <insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntity">
		insert into tb_cf_issue_coupon(
			`coupon_issuetable_id`,
			`coupon_id`,
			`user_id`,
			`nick`,
			`create_time`,
			`enable_flag`,
			`issue_id`)
		values(
			#{couponIssuetableId},
			#{couponId},
			#{userId},
			#{nick},
			#{createTime},
			#{enableFlag},
			#{issueId})
	</insert>

    <update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntity">
        update tb_cf_issue_coupon
        <set>
            <if test="couponId != null">`coupon_id` = #{couponId},</if>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="nick != null">`nick` = #{nick},</if>
            <if test="createTime != null">`create_time` = #{createTime},</if>
            <if test="enableFlag != null">`enable_flag` = #{enableFlag},</if>
            <if test="issueId != null">`issue_id` = #{issueId}</if>
        </set>
        where coupon_issuetable_id = #{couponIssuetableId}
    </update>

    <update id="changeCouponStatus" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfIssueCouponEntity">
        UPDATE tb_cf_issue_coupon
        SET enable_flag = 0
        WHERE
	    user_id = #{userId}
	    AND coupon_id = #{couponId}
    </update>
    <delete id="delete">
		delete from tb_cf_issue_coupon where coupon_issuetable_id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from tb_cf_issue_coupon where coupon_issuetable_id in
        <foreach item="couponIssuetableId" collection="array" open="(" separator="," close=")">
            #{couponIssuetableId}
        </foreach>
    </delete>

</mapper>