<?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.WxCouponChannelMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCouponChannel">
		<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="type" jdbcType="INTEGER" property="type" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="target_ad" jdbcType="INTEGER" property="targetAd" />
		<result column="business" jdbcType="INTEGER" property="business" />
		<result column="sub_business" jdbcType="INTEGER" property="subBusiness" />
		<result column="begin_time" jdbcType="TIMESTAMP" property="beginTime" />
		<result column="end_time" jdbcType="TIMESTAMP" property="endTime" />
		<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="sub_target_id" jdbcType="BIGINT" property="subTargetId" />
	</resultMap>

	<sql id="allColumns">
	    cc.`id`,cc.`tenant_id`,cc.`coupon_id`,cc.`coupon_status`,cc.`type`,cc.`title`,cc.`target_ad`,cc.`business`,cc.`sub_business`,cc.`begin_time`,cc.`end_time`,cc.`status`,cc.`create_date`,cc.`update_date`,cc.`sub_target_id`
	</sql>

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

		<if test=" null != id ">
			and cc.`id` = #{id}
		</if>

		<if test=" null != tenantId ">
			and cc.`tenant_id` = #{tenantId}
		</if>

		<if test=" null != couponId ">
			and cc.`coupon_id` = #{couponId}
		</if>

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

		<if test=" null != targetAd ">
			and cc.`target_ad` = #{targetAd}
		</if>

		<if test=" null != business ">
			and cc.`business` = #{business}
		</if>

		<if test=" null != subBusiness ">
			and cc.`sub_business` = #{subBusiness}
		</if>

		<if test=" null != startdate and  null!=enddate">
			and cc.`create_date` between #{startdate} and #{enddate}
		</if>

		<if test=" null != status ">
			and cc.`status` = #{status}
		</if>

		<if test=" null != createDate ">
			and cc.`create_date` = #{createDate}
		</if>

		<if test=" null != updateDate ">
			and cc.`update_date` = #{updateDate}
		</if>

		<if test=" null != subTargetId ">
			and cc.`sub_target_id` = #{subTargetId}
		</if>
		
		<if test=" null != supportTransfer ">
			and c.`support_transfer` = #{supportTransfer}
		</if>
		
		<if test=" null != ids ">
			and cc.id in
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test=" null != couponIds ">
			and cc.coupon_id in
			<foreach collection="couponIds" index="index" item="couponIdsItem" open="(" separator="," close=")">
				#{couponIdsItem}
			</foreach>
		</if>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
	</sql>

	<select id="findList" parameterType="com.iformall.domain.po.WxCouponChannel" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_coupon_channel cc
		<include refid="dynamicWhereConditions" />
	</select>

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

	<sql id="CouponChannelVoColumns">
		<include refid="allColumns" />,
		c.remain_inventory,c.inventory,c.cover_img,c.title,c.sub_title,c.sale_price,
		c.use_price,c.price,c.unit,c.use_limit_quantity,c.send_type,c.support_transfer,c.press_limit_num,c.auto_refund,
		c.valid_type,c.valid_start_date,c.valid_end_date,c.valid_days,c.credit_price
	</sql>

	<resultMap id="CouponChannelVoMap" type="com.iformall.domain.vo.WxCouponChannelVo">
		<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="type" jdbcType="INTEGER" property="type" />
		<result column="title" jdbcType="VARCHAR" property="title" />
		<result column="target_ad" jdbcType="INTEGER" property="targetAd" />
		<result column="business" jdbcType="VARCHAR" property="business" />
		<result column="sub_business" jdbcType="INTEGER" property="subBusiness" />
		<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
		<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
		<result column="sub_target_id" jdbcType="BIGINT" property="subTargetId" />

		<result column="remain_inventory" jdbcType="INTEGER" property="remainInventory"/>
		<result column="inventory" jdbcType="INTEGER" property="inventory"/>
		<result column="cover_img" jdbcType="VARCHAR" property="coverImg"/>
		<result column="title" jdbcType="VARCHAR" property="title"/>
		<result column="sub_title" jdbcType="VARCHAR" property="subTitle"/>
		<result column="sale_price" jdbcType="INTEGER" property="salePrice"/>
		<result column="use_price" jdbcType="INTEGER" property="usePrice"/>
		<result column="price" jdbcType="INTEGER" property="price"/>
		<result column="unit" jdbcType="INTEGER" property="unit"/>
		<result column="use_limit_quantity" jdbcType="INTEGER" property="useLimitQuantity"/>
		<result column="send_type" jdbcType="INTEGER" property="sendType"/>
		<result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>
		<result column="press_limit_num" jdbcType="INTEGER" property="pressLimitNum"/>
		<result column="auto_refund" jdbcType="INTEGER" property="autoRefund"/>
		<result column="valid_type" jdbcType="INTEGER" property="validType"/>
		<result column="valid_start_date" jdbcType="TIMESTAMP" property="validStartDate"/>
		<result column="valid_end_date" jdbcType="TIMESTAMP" property="validEndDate"/>
		<result column="valid_days" jdbcType="INTEGER" property="validDays"/>
		
		<collection column="{productId=coupon_id}" property="merchantVoList"
			ofType="com.iformall.domain.vo.WxMerchantVo"
			javaType="java.util.List"
			select="com.iformall.mapper.WxCouponMerchantMapper.findMerchantNameList" >
		</collection>
	</resultMap>

	<select id="findVoList" parameterType="com.iformall.domain.po.WxCouponChannel" resultMap="CouponChannelVoMap">
		select <include refid="CouponChannelVoColumns" />
		from wx_coupon_channel cc
		left join wx_coupon c on cc.coupon_id = c.id
		<include refid="dynamicWhereConditions" />
	</select>

	<sql id="WxCouponCVoColumn">
		cc.id,cc.coupon_id,cc.target_ad,cc.begin_time,cc.end_time,
	    c.tenant_id,c.type,c.cover_img,c.title,c.sub_title,c.sale_price,c.use_price,
	    c.use_limit_quantity,c.send_type,c.valid_type,c.valid_start_date,c.valid_end_date,
	    c.valid_days,c.detail,c.price,c.unit,c.remain_inventory,c.inventory,c.remark,c.status,
	    c.create_date,c.update_date,c.business,c.sub_business,c.support_transfer,c.press_limit_num, c.auto_refund,c.credit_price
	</sql>

	<resultMap id="WxCouponCVoMap" type="com.iformall.domain.vo.WxCouponCVo">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="coupon_id" jdbcType="BIGINT" property="couponId" />
        <result column="type" jdbcType="INTEGER" property="type" />
		<result column="target_ad" jdbcType="INTEGER" property="targetAd" />
		<result column="begin_time" jdbcType="TIMESTAMP" property="beginTime"/>
		<result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>

		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="type" jdbcType="INTEGER" property="type"/>
		<result column="cover_img" jdbcType="VARCHAR" property="coverImg"/>
		<result column="title" jdbcType="VARCHAR" property="title"/>
		<result column="sub_title" jdbcType="VARCHAR" property="subTitle"/>
		<result column="sale_price" jdbcType="INTEGER" property="salePrice"/>
		<result column="use_price" jdbcType="INTEGER" property="usePrice"/>
		<result column="use_limit_quantity" jdbcType="INTEGER" property="useLimitQuantity"/>
		<result column="send_type" jdbcType="INTEGER" property="sendType"/>
		<result column="valid_type" jdbcType="INTEGER" property="validType"/>
		<result column="valid_start_date" jdbcType="TIMESTAMP" property="validStartDate"/>
		<result column="valid_end_date" jdbcType="TIMESTAMP" property="validEndDate"/>
		<result column="valid_days" jdbcType="INTEGER" property="validDays"/>
		<result column="detail" jdbcType="VARCHAR" property="detail"/>
		<result column="price" jdbcType="INTEGER" property="price"/>
		<result column="unit" jdbcType="INTEGER" property="unit"/>
		<result column="remain_inventory" jdbcType="INTEGER" property="remainInventory"/>
		<result column="inventory" jdbcType="INTEGER" property="inventory"/>
		<result column="remark" jdbcType="VARCHAR" property="remark"/>
		<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="business" jdbcType="INTEGER" property="business"/>
		<result column="subBusiness" jdbcType="INTEGER" property="subBusiness"/>
		<result column="support_transfer" jdbcType="INTEGER" property="supportTransfer"/>
		<result column="press_limit_num" jdbcType="INTEGER" property="pressLimitNum"/>
		<result column="auto_refund" jdbcType="INTEGER" property="autoRefund"/>

		<collection column="{productId=coupon_id}" property="merchantVoList"
					ofType="com.iformall.domain.vo.WxMerchantVo"
					javaType="java.util.List"
					select="com.iformall.mapper.WxCouponMerchantMapper.findMerchantVoList" >
		</collection>
	</resultMap>

	<select id="findVoDetail" parameterType="java.lang.Long" resultMap="WxCouponCVoMap">
		select
		<include refid="WxCouponCVoColumn"/>
		from wx_coupon_channel cc
		left join wx_coupon c on cc.coupon_id = c.id
		where cc.id = #{id}
	</select>


	<select id="countCoupon" parameterType="com.iformall.domain.po.WxCouponChannel" resultType="java.lang.Integer">
		select count(distinct coupon_id) from wx_coupon_channel cc
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="countCouponChannel" parameterType="com.iformall.domain.po.WxCouponChannel" resultType="java.lang.Integer">
		select count(*) from wx_coupon_channel cc
		<include refid="dynamicWhereConditions" />
	</select>

	<update id="offExpiriedCouponChannelByEndTime">
		update wx_coupon_channel SET status = 1, update_date = now()
		where status = 0 and end_time &lt; now()
	</update>

	<update id="offExpiriedCouponChannelByValidDate">
		update wx_coupon_channel cc, wx_coupon c SET cc.status = 1, cc.update_date = now()
		where cc.status = 0 and cc.coupon_id = c.id and c.valid_type = 1 and c.valid_end_date &lt; now()
	</update>

	<update id="offExpiriedCouponChannelByCouponStatus">
		update wx_coupon_channel cc, wx_coupon c SET cc.status = 1, cc.update_date = now()
		where cc.status = 0 and cc.coupon_id = c.id and c.status = 1
	</update>

	<update id="offExpiriedCouponChannel2ByEndTime">
		update wx_coupon_channel SET status = 1, update_date = now()
		where status = 0 and target_ad = 2 and end_time &lt; now()
	</update>

	<delete id="delByTopic" parameterType="com.iformall.domain.po.WxTopic">
		delete from wx_coupon_channel where sub_target_id = #{id}
	</delete>


</mapper>
