<?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.WxCouponSendMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.vo.WxCouponSendVo">
		<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="title" jdbcType="VARCHAR" property="title" />
		<result column="send_type" jdbcType="INTEGER" property="sendType" />
		<result column="conditions" jdbcType="VARCHAR" property="conditions" />
		<result column="status" jdbcType="INTEGER" property="status" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />

		<result column="merchant_name" jdbcType="VARCHAR" property="merchantName" />
		<result column="remain_inventory" jdbcType="INTEGER" property="remainInventory"/>
		<result column="inventory" jdbcType="INTEGER" property="inventory"/>
		<result column="use_limit_quantity" jdbcType="INTEGER" property="useLimitQuantity"/>
		<result column="valid_start_date" jdbcType="TIMESTAMP" property="validStartDate"/>
		<result column="valid_end_date" jdbcType="TIMESTAMP" property="validEndDate"/>
		
		<result column="send_sms" jdbcType="INTEGER" property="sendSms"/>
	
	</resultMap>

	<sql id="allColumns">
		cs.id,cs.tenant_id,cs.coupon_id,cs.title,cs.send_type,cs.conditions,cs.status,cs.create_date,cs.update_date,
		(case when
				(select count(*) from wx_coupon_merchant xcm where xcm.product_id = cs.coupon_id and xcm.status = 0)=1
			then
				(select m.name from wx_merchant m, wx_coupon_merchant cm where m.id = cm.merchant_id and cm.product_id=cs.coupon_id)
			else
				'[多商户通用]' 
			end) as merchant_name,
		c.remain_inventory, c.use_limit_quantity, c.inventory, c.valid_start_date, c.valid_end_date,cs.send_sms
	</sql>

	<sql id="dynamicWhereConditions">
		where 1 = 1

		<if test=" null != id ">
			and cs.id = #{id}

		</if>

		<if test=" null != tenantId ">
			and cs.tenant_id  = #{tenantId}

		</if>

		<if test=" null != couponId ">
			and cs.coupon_id = #{couponId}

		</if>

		<if test=" null != title ">
			and cs.title  like concat('%', #{title},'%')

		</if>

		<if test=" null != sendType ">
			and cs.send_type = #{sendType}

		</if>

		<if test=" null != status ">
			and cs.status = #{status}

		</if>

		<if test=" null != createDate ">
			and cs.create_date = #{createDate}

		</if>

		<if test=" null != updateDate ">
			and cs.update_date = #{updateDate}

		</if>
		<if test=" null != ids ">
			and id in
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test=" null != sendSms ">
			and cs.send_sms = #{sendSms}
		
		</if>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</sql>

	<select id="findListVo" parameterType="com.iformall.domain.vo.WxCouponSendVo" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_coupon_send cs left join wx_coupon c on c.id = cs.coupon_id
		<include refid="dynamicWhereConditions" />
	</select>

	<update id="updateStatusByCouponId" parameterType="com.iformall.domain.po.WxCouponChannel">
		update wx_coupon_send set status=#{status}
		where coupon_id=#{couponId}
		<if test=" null != tenantId ">
			and tenant_id  = #{tenantId}
		</if>
	</update>

	<update id="offExpiriedCouponSendByCouponStatus">
		update wx_coupon_send cs, wx_coupon c
		set cs.status=1, cs.update_date = now()
		where c.id = cs.coupon_id and cs.status = 0 and
		((c.status = 1) or (c.valid_type = 1 and c.valid_end_date &lt; now()))
	</update>



</mapper>
