<?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.WxTopicMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxTopic">
		<id column="id" property="id" />
		<result column="tenant_id" property="tenantId" />
		<result column="name" property="name" />
		<result column="title" property="title" />
		<result column="content" property="content" />
		<result column="cover" property="cover" />
        <result column="cover_picture" property="coverPicture" />
        <result column="detail_picture" property="detailPicture" />
		<result column="advert" property="advert" />
		<result column="begin_time" property="beginTime" />
		<result column="end_time" property="endTime" />
		<result column="status" property="status" />
		<result column="createtime" property="createtime" />
		<result column="updatetime" property="updatetime" />
		<result column="bg_color" property="bgColor" />
		<result column="couponChannelId" property="couponChannelId" />

	</resultMap>

	<sql id="allColumns">
		`id`,`tenant_id`,`name`,`title`,`content`,`cover`,`cover_picture`,`detail_picture`,`advert`,`begin_time`,`end_time`,`createtime`,`updatetime`,`bg_color`
	</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='statusStr =="0,2" or status==1'>
			<if test=" null != beginTime ">
				and `begin_time` >= #{beginTime}
			</if>
			<if test=" null != endTime ">
				and `begin_time` &lt;= #{endTime}
			</if>
		</if>
		<if test='status ==2 or status==3'>
			<if test=" null != beginTime ">
				and `end_time` >= #{beginTime}
			</if>
			<if test=" null != endTime ">
				and `end_time` &lt;= #{endTime}
			</if>
		</if>

		<if test=" null != status ">
			and `status` = #{status}
		</if>
		<if test=" null != name and ''!=name ">
			and `name` like concat('%',#{name},'%')
		</if>
		<if test=" null != statusList ">
			and status in
			<foreach collection="statusList" index="index" item="statusItem" open="(" separator="," close=")">
				#{statusItem}
			</foreach>
		</if>

		<if test=" null != sortColumns"> order by ${sortColumns} </if>
		<if test=" null == sortColumns"> order by begin_time desc,status asc </if>
	</sql>

	<select id="findList" parameterType="com.iformall.domain.po.WxTopic" resultMap="BaseResultMap">
		select * from wx_topic
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="findExist" parameterType="com.iformall.domain.po.WxTopic" resultMap="BaseResultMap">
		SELECT * FROM wx_topic WHERE wx_topic.tenant_id = #{tenantId}
		AND (`begin_time` >= #{beginTime} and `end_time` &lt;= #{endTime}) AND `status` = 1
	</select>

	<select id="show" parameterType="com.iformall.domain.po.WxTopic" resultMap="BaseResultMap">
		select t.*,cc.id as couponChannelId from wx_topic t left join wx_coupon_channel cc on(cc.sub_target_id = t.id)
		where t.status = 1
		and now() >= t.begin_time and  now() &lt;= t.end_time
		and t.tenant_id = #{tenantId}
		order by t.begin_time desc limit 1
	</select>

	<update id="updateStatus" parameterType="com.iformall.domain.po.WxTopic">
		update wx_topic set status = 3 where id in(
			select t.id from (
				select id from wx_topic where status !=3 and now() >= end_time and tenant_id = #{tenantId}
			) t
		)
	</update>

	<update id="couponDownLine" parameterType="com.iformall.domain.po.WxTopic">
		update wx_coupon_channel set status = 1 where status = 0 and target_ad = 8 and sub_target_id in(
			select id from wx_topic where status in(2,3) and tenant_id = #{tenantId}
		)
	</update>

</mapper>
