<?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.WxScreenAdMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxScreenAd">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="sub_type" jdbcType="INTEGER" property="subType"/>
        <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="cover_img" jdbcType="VARCHAR" property="coverImg"/>
        <result column="target_id" jdbcType="BIGINT" property="targetId"/>
        <result column="ext_info" jdbcType="VARCHAR" property="extInfo"/>
    </resultMap>

    <sql id="allColumns">
	    `id`, `tenant_id`, `title`, `type`, `sub_type`, `status`,  `create_date`, `update_date`, `cover_img`, `target_id`, `ext_info`
	</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 != title and ''!=title">
            and `title` like concat('%', #{title},'%')
        </if>

        <if test=" null != type ">
            and `type` = #{type}
        </if>

        <if test=" null != subType ">
            and `sub_type` = #{subType}
        </if>

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

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

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

        <if test=" null != coverImg ">
            and `cover_img` = #{coverImg}
        </if>

        <if test=" null != targetId ">
            and `target_id` = #{targetId}
        </if>


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

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

    <update id="updateStatusByCouponChannelId" parameterType="com.iformall.domain.po.WxScreenAd">
        update wx_screen_ad sa
        set sa.status = #{status}, sa.update_date = #{updateDate}
        where sa.target_id = #{targetId}
        <if test=" null != tenantId ">
            and sa.tenant_id = #{tenantId}
        </if>
    </update>

    <update id="offScreeAdByCouponChannelStatus">
		update wx_screen_ad sa, wx_coupon_channel cc
		SET sa.status = 1, sa.update_date = now()
		where sa.status = 0
		and sa.target_id = cc.id
		and cc.status = 1
	</update>

</mapper>
