<?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.platform.dao.TbCfCouponIssueDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfCouponIssueEntity" id="tbCfCouponIssueMap">
        <result property="couponIssueId" column="coupon_issue_id"/>
        <result property="couponId" column="coupon_id"/>
        <result property="userStatus" column="user_status"/> 
        <result property="userLevel" column="user_level"/>
    </resultMap>

	<select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfCouponIssueEntity">
		select
			`coupon_issue_id`,
			`coupon_id`,
			`user_status`,
			`user_level`
		from tb_cf_coupon_issue
		where coupon_issue_id = #{id}
	</select>
	<select id="queryStatus" resultType="com.diaoyun.zion.chinafrica.entity.TbCfCouponIssueEntity">
		select DISTINCT
			  a.user_id,a.nick
		 from  tb_cf_coupon_issue b ,
   		where user_status = #{userStatus}
	</select>
	<select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfCouponIssueEntity">
		select
    		`coupon_issue_id`,
    		`coupon_id`,
    		`user_status`,
    		`user_level`
		from tb_cf_coupon_issue
		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_issue_id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	<select id="queryByUserStatus" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfCouponIssueEntity" resultType="com.diaoyun.zion.chinafrica.entity.TbCfUserInfoEntity" >
		select DISTINCT
			a.user_id,a.nick
		from  tb_cf_user_info a
		<choose>
			<when test="status != null and status == '10'" >
				INNER JOIN tb_cf_order b ON a.user_id=b.user_id
			</when>
			<when test="status != null and status == '20'">
				where
				(unix_timestamp(now()) - unix_timestamp(a.create_time))/(24*60*60) &lt; '1'
			</when>
			<when test="status != null and status == '30'">
				LEFT JOIN tb_cf_order b ON a.user_id=b.user_id
				where b.user_id  IS NULL
			</when>
			<when test="status != null and status == '40'">
				LEFT JOIN tb_cf_cart_record_r r ON  a.user_id= r.user_id
			</when>
		</choose>
	</select>

 	<select id="queryTotal" resultType="int">
		select count(*) from tb_cf_coupon_issue
		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.TbCfCouponIssueEntity">
		insert into tb_cf_coupon_issue(
			`coupon_issue_id`,
			`coupon_id`,
			`user_status`,
			`user_level`)
		values(
			#{couponIssueId},
			#{couponId},
			#{userStatus},
			#{userLevel})
	</insert>
	<select id="queryByStatus" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfCouponIssueEntity" resultType="int">
		select  `user_status` from  tb_cf_coupon_issue where coupon_issue_id= #{couponIssueId}
	</select>
	<update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfCouponIssueEntity">
		update tb_cf_coupon_issue 
		<set>
			<if test="couponId != null">`coupon_id` = #{couponId}, </if>
			<if test="userStatus != null">`user_status` = #{userStatus}, </if>
			<if test="userLevel != null">`user_level` = #{userLevel}</if>
		</set>
		where coupon_issue_id = #{couponIssueId}
	</update>
	
	<delete id="delete">
		delete from tb_cf_coupon_issue where coupon_issue_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from tb_cf_coupon_issue where coupon_issue_id in 
		<foreach item="couponIssueId" collection="array" open="(" separator="," close=")">
			#{couponIssueId}
		</foreach>
	</delete>

</mapper>