<?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.iformall.mapper.WxCouponPasswordMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCouponPassword">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
		<result column="coupon_short" jdbcType="VARCHAR" property="couponShort" />
		<result column="password" jdbcType="VARCHAR" property="password" />
		<result column="status" jdbcType="INTEGER" property="status" />
		<result column="sended_phone" jdbcType="VARCHAR" property="sendedPhone" />
		<result column="card_id" jdbcType="BIGINT" property="cardId" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="expire_date" jdbcType="TIMESTAMP" property="expireDate" />
		<result column="present_id" jdbcType="TIMESTAMP" property="presentId"/>
	
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`tenant_id`,`coupon_id`,`coupon_short`,`password`,`status`,`sended_phone`,`card_id`,`create_date`,`update_date`,`expire_date`,`present_id`
	</sql>

	<sql id="dynamicWhereConditions">
	        where 1 = 1
		<if test=" null != id ">
		    and `id` = #{id}
		</if>    
		<if test=" null != tenantId ">
		    and `tenant_id` = #{tenantId}
		</if>    
		<if test=" null != couponId ">
		    and `coupon_id` = #{couponId}
		</if>
		<if test=" null != couponShort ">
			and `coupon_short` = #{couponShort}
		</if>
		<if test=" null != password ">
		    and `password` = #{password}
		</if>    
		<if test=" null != status ">
		    and `status` = #{status}
		</if>    
		<if test=" null != sendedPhone ">
		    and `sended_phone` = #{sendedPhone}
		</if>    
		<if test=" null != cardId ">
		    and `card_id` = #{cardId}
		</if>    
		<if test=" null != createDate ">
		    and `create_date` = #{createDate}
		</if>    
		<if test=" null != updateDate ">
		    and `update_date` = #{updateDate}
		</if>
		<if test=" null != expireDate ">
			and `expire_date` > #{expireDate}
		</if>
		
		<if test=" null != presentId ">
			and `present_id` = #{presentId}
		</if>
		
		<if test=" null != ids ">
		 	and id in  
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
			#{idItem}
			</foreach>
		</if>
		
		<if test=" null != statusStr and ''!=statusStr">
			and status in (${statusStr})
		</if>
		
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</sql>
	
	<select id="findList" parameterType="com.iformall.domain.po.WxCouponPassword" resultMap="BaseResultMap">
		select <include refid="allColumns" />
		from wx_coupon_password
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="findAvaPasswordListByCouponId" parameterType="com.iformall.domain.po.WxCouponPassword" resultMap="BaseResultMap">
		select <include refid="allColumns" />
		from wx_coupon_password
		<where>
			<if test=" null != tenantId ">
				and `tenant_id` = #{tenantId}
			</if>
			<if test=" null != couponId ">
				and `coupon_id` = #{couponId}
			</if>
			<if test=" null != expireDate ">
				and `expire_date` > #{expireDate}
			</if>
		</where>
	</select>

	<insert id="insertCouponPasswds" parameterType="java.util.List">
		INSERT INTO wx_coupon_password (`id`,`coupon_id`,`coupon_short`,`password`,`status`,`create_date`,`update_date`)
		VALUES
		<foreach collection="list" item="item" index="index" separator=",">
			(
			#{item.id},#{item.couponId},#{item.couponShort},#{item.password},#{item.status},
			#{item.createDate},#{item.updateDate}
			)
		</foreach>
	</insert>

	<update id="disableByCouponId" parameterType="Long">
		update wx_coupon_password
		set status=4, update_date=now()
		where status != 3 and coupon_id=#{couponId}
	</update>

	<update id="downloadByCouponId" parameterType="Long">
		update wx_coupon_password
		set status=1, update_date=now(), expire_date=date_add(now(),interval 30 DAY)
		where status=0 and coupon_id=#{couponId}
	</update>

	<select id="findCouponGroupList" parameterType="com.iformall.domain.po.WxCouponPassword" resultMap="BaseResultMap">
		select `coupon_id`,`coupon_short`
		from wx_coupon_password
		where `tenant_id` = #{tenantId}
		group by `coupon_id`,`coupon_short`
	</select>
	
	
	<resultMap id="CouponCountInfoMap" type="com.iformall.domain.vo.WxCouponPasswordCountInfoVO">
		<result column="coupon_count" jdbcType="BIGINT" property="couponCount"/>
		<result column="title" jdbcType="VARCHAR" property="couponName"/>
		<result column="coupon_id" jdbcType="BIGINT" property="couponId"/>
		<result column="price" jdbcType="VARCHAR" property="price"/>
	</resultMap>
	
	<select id="findCouponCountInfo" parameterType="com.iformall.domain.po.WxCouponPassword"
	        resultMap="CouponCountInfoMap">
		select cp.coupon_id,c.title,c.price,cp.coupon_count from
		(select count(*) as coupon_count,coupon_id from wx_coupon_password
		where tenant_id=#{tenantId} and status=0 group by coupon_id) cp
		inner join wx_coupon c on cp.coupon_id = c.id
		where c.tenant_id=#{tenantId}
		and c.status=0 and c.put_apply_status in(0,2)
		<if test=" null != couponId ">
			and cp.`coupon_id` = #{couponId}
		</if>
		group by cp.coupon_id
		order by c.create_date desc
	</select>
	
	<update id="updateStatus" parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" open="" close="" separator=";">
			update wx_coupon_password set
			status=1,sended_phone=#{item.sendedPhone},update_date=now(),expire_date=date_add(now(),interval 30 DAY),
			present_id=#{item.presentId}
			where id = #{item.id}
		</foreach>
	</update>

</mapper>
