<?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="MdeAdImagesMapper">
    <resultMap type="com.wechat.model.dbEntity.MdeAdImages" id="MdeAdImages">
    	<id column="ad_id" property="adId"/>
		<result column="AD_POSITION" property="adPosition"/>
		<result column="ad_title" property="adTitle"/>
		<result column="ad_hyperlink" property="adHyperlink"/>
		<result column="image_url" property="imageUrl"/>
		<result column="sort_key" property="sortKey"/>
		<result column="start_time" property="startTime"/>
		<result column="stop_time" property="stopTime"/>
		<result column="valid_flag" property="validFlag"/>
		<result column="version_no" property="versionNo"/>
		<result column="create_by" property="createBy"/>
		<result column="create_date" property="createDate"/>
		<result column="update_by" property="updateBy"/>
		<result column="update_date" property="updateDate"/>
		<result column="remarks" property="remarks"/>
		<result column="del_flag" property="delFlag"/>
    </resultMap>
    
	<sql id="mdeAdImagesColumns">
		a.ad_id AS "adId",
		a.AD_POSITION AS "adPosition",
		a.ad_title AS "adTitle",
		a.ad_hyperlink AS "adHyperlink",
		a.image_url AS "imageUrl",
		a.sort_key AS "sortKey",
		a.start_time AS "startTime",
		a.stop_time AS "stopTime",
		a.valid_flag AS "validFlag",
		a.version_no AS "versionNo",
		a.create_by AS "createBy",
		a.create_date AS "createDate",
		a.update_by AS "updateBy",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
	</sql>
	
	<sql id="mdeAdImagesJoins">
	</sql>
    
	<select id="get" resultMap="MdeAdImages">
		SELECT 
			<include refid="mdeAdImagesColumns"/>
		FROM mde_ad_images a
		<include refid="mdeAdImagesJoins"/>
		WHERE a.ad_id = #{adId}
	</select>
	
    <!-- 根据类型获取 广告 -->
	<select id="getAdImagesByadPosition" resultType="com.wechat.model.dto.AdImageDto">
		SELECT 
			a.ad_id AS "adId",
			a.ad_hyperlink AS "adHyperlink",
			a.image_url AS "imageUrl",
			a.update_date AS "updateDate",
			a.sort_key AS "sortKey"
		FROM mde_ad_images a
		<include refid="mdeAdImagesJoins"/>
		WHERE  (a.valid_flag=0 or (a.valid_flag=1 and DATE_FORMAT(a.start_time,'%Y-%m-%d') &lt;= DATE_FORMAT(now(),'%Y-%m-%d') 
			and DATE_FORMAT(a.stop_time,'%Y-%m-%d') &gt;= DATE_FORMAT(now(),'%Y-%m-%d')))
			and a.del_flag=0 and a.AD_POSITION=#{adPosition}  and a.status = 1 
			order by SORT_KEY, UPDATE_DATE desc
	</select>
	
	<!-- 获取登录页初始化图片地址 -->
	<select id = "getLoginInitImage" resultMap="MdeAdImages">
		SELECT
			<include refid="mdeAdImagesColumns"/>
		FROM
			mde_ad_images a
		WHERE
			a.AD_POSITION =#{adPosition}
		AND a.`STATUS` = 1
		AND a.DEL_FLAG = 0
	</select>
	
	<select id="findList" resultMap="MdeAdImages">
		SELECT 
			<include refid="mdeAdImagesColumns"/>
		FROM mde_ad_images a
		<include refid="mdeAdImagesJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultMap="MdeAdImages">
		SELECT 
			<include refid="mdeAdImagesColumns"/>
		FROM mde_ad_images a
		<include refid="mdeAdImagesJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO mde_ad_images(
			AD_POSITION,
			ad_title,
			ad_hyperlink,
			image_url,
			sort_key,
			start_time,
			stop_time,
			valid_flag,
			version_no,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{adPosition},
			#{adTitle},
			#{adHyperlink},
			#{imageUrl},
			#{sortKey},
			#{startTime},
			#{stopTime},
			#{validFlag},
			#{versionNo},
			#{createBy},
			#{createDate},
			#{updateBy},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE mde_ad_images SET 	
			<if test="adPosition != null">AD_POSITION = #{adPosition},            </if>
			<if test="adTitle != null">ad_title = #{adTitle},          </if>
			<if test="adHyperlink != null">ad_hyperlink = #{adHyperlink},  </if>
			<if test="imageUrl != null">image_url = #{imageUrl},        </if>
			<if test="sortKey != null">sort_key = #{sortKey},          </if>
			<if test="startTime != null">start_time = #{startTime},      </if>
			<if test="stopTime != null">stop_time = #{stopTime},        </if>
			<if test="validFlag != null">valid_flag = #{validFlag},      </if>
			<if test="updateBy != null">update_by = #{updateBy},     </if>
			<if test="updateDate != null">update_date = #{updateDate},    </if>
			<if test="remarks != null">remarks = #{remarks}, 			</if>
			version_no = version_no +1
		WHERE ad_id = #{adId}
		<if test="versionNo != null"> and version_no = #{versionNo} </if>
	</update>
	
	<update id="delete">
		UPDATE mde_ad_images SET 
			del_flag = #{DEL_FLAG_DELETE},
			version_no = version_no +1
		WHERE ad_id = #{adId} and version_no = #{versionNo}
	</update>
	
</mapper>