<?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.TbCfToiCouponDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfToicouponEntity" id="tbCfToiCouponMap">
        <result property="toitableId" column="toitable_id"/>
        <result property="couponId" column="coupon_id"/>
        <result property="userId" column="user_id"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="identification" column="identification"/>
        <result property="enableFlag" column="enable_flag"/>
    </resultMap>

    <select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfToicouponEntity">
		select
			`toitable_id`,
			`coupon_id`,
			`user_id`,
			`start_time`,
			`end_time`,
			`identification`,
			`enable_flag`
		from tb_cf_toicoupon
		where toitable_id = #{id}
	</select>

    <select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfToicouponEntity">
        select
        `toitable_id`,
        `coupon_id`,
        `user_id`,
        `start_time`,
        `end_time`,
        `identification`,
        `enable_flag`
        from tb_cf_toicoupon
        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 toitable_id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from tb_cf_toicoupon
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
    </select>
    <!--查询用户有效的优惠券-->
    <select id="queryCouponByUserId" resultType="com.diaoyun.zion.chinafrica.entity.TbCfToiCouponEntityExtends">
           	select
            a.toitable_id,
            a.coupon_id,
            b.coupon_category_id,
            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.create_time,
			a.identification,
            a.enable_flag
            from tb_cf_toicoupon a LEFT  join  tb_cf_coupon b ON a.coupon_id=b.coupon_id
            WHERE a.user_id = #{userId} and a.enable_flag=1 and b.status=1
    </select>
    <!--修改优惠券为已使用-->
    <update id="changeCoupnStatus" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfToicouponEntity">
      update  tb_cf_toicoupon t set  enable_flag = 0  where  t.user_id=#{userId} and  t.coupon_id=#{couponId}
    </update>
    <!--修改优惠券领取发放记录-->
    <update id="updateEnableFlag" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfToicouponEntity">
        update tb_cf_toicoupon set enable_flag=#{enableFlag} where user_id=#{userId} and coupon_id=#{couponId}
	</update>
    <insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfToicouponEntity">
		insert into tb_cf_toicoupon(
			`toitable_id`,
			`coupon_id`,
			`user_id`,
			`start_time`,
			`end_time`,
			`identification`,
			`enable_flag`)
		values(
			#{toitableId},
			#{couponId},
			#{userId},
			#{startTime},
			#{endTime},
			#{identification},
			#{enableFlag})
	</insert>

    <update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfToicouponEntity">
        update tb_cf_toicoupon
        <set>
            <if test="couponId != null">`coupon_id` = #{couponId},</if>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="startTime != null">`start_time` = #{startTime},</if>
            <if test="endTime != null">`end_time` = #{endTime},</if>
            <if test="identification != null">`identification` = #{identification},</if>
            <if test="enableFlag != null">`enable_flag` = #{enableFlag}</if>
        </set>
        where toitable_id = #{toitableId}
    </update>

    <delete id="delete">
		delete from tb_cf_toicoupon where toitable_id = #{value}
	</delete>

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

</mapper>