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

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfTakeCouponEntity" id="tbCfTakeCouponMap">
        <result property="takeId" column="take_id"/>
        <result property="userId" column="user_id"/>
        <result property="couponId" column="coupon_id"/>
        <result property="createTime" column="create_time"/>
        <result property="enableFlag" column="enable_flag"/>
    </resultMap>

	<select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfTakeCouponEntity">
		select
			`take_id`,
			`user_id`,
			`coupon_id`,
			`create_time`,
			`enable_flag`
		from tb_cf_take_coupon
		where take_id = #{id}
	</select>

	<select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfTakeCouponEntity">
		select
    		`take_id`,
    		`user_id`,
    		`coupon_id`,
    		`create_time`,
		    `enable_flag`
		from tb_cf_take_coupon
		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 take_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_take_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.TbCfTakeCouponEntity">
		insert into tb_cf_take_coupon(
			`take_id`,
			`user_id`,
			`coupon_id`,
			`create_time`,
			`enable_flag`)
		values(
			#{takeId},
			#{userId},
			#{couponId},
			#{createTime},
			#{enableFlag}
			)
	</insert>

	<update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfTakeCouponEntity">
		update tb_cf_take_coupon
		<set>
			<if test="userId != null">`user_id` = #{userId}, </if>
			<if test="couponId != null">`coupon_id` = #{couponId}, </if>
			<if test="createTime != null">`create_time` = #{createTime}</if>
			<if test="enableFlag != null">`enable_flag` = #{enableFlag}</if>
		</set>
		where take_id = #{takeId}
	</update>

	<delete id="delete">
		delete from tb_cf_take_coupon where take_id = #{value}
	</delete>

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

	<!--判断用户是否已经领取优惠券-->
	<select id="queryTakeByCouponId" resultType="java.lang.String">
		select take_id from tb_cf_take_coupon where coupon_id=#{couponId} and user_id=#{userId}
	</select>

	<!--统计已领取优惠券数量-->
	<select id="queryTakeCount" resultType="java.lang.Integer">
		select count(*) from tb_cf_take_coupon where coupon_id=#{couponId}
	</select>

    <!--更新是否已经使用-->
	<update id="updateEnableFlag">
        update tb_cf_issue_coupon set enable_flag=#{enableFlag} where user_id=#{userId} and coupon_id=#{couponId}
    </update>

</mapper>