<?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.ylcm.sys.mapper.AdProductMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ylcm.sys.domain.AdProduct">
        <id column="id" property="id" />
        <result column="product_name" property="productName" />
        <result column="name_img_url" property="nameImgUrl" />
        <result column="brand_name" property="brandName" />
        <result column="brand_img_url" property="brandImgUrl" />
        <result column="first_ind" property="firstInd" />
        <result column="second_ind" property="secondInd" />
        <result column="status" property="status" />
        <result column="crt_time" property="crtTime" />
        <result column="upd_time" property="updTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, product_name, name_img_url, brand_name, brand_img_url, first_ind, second_ind, status, crt_time, upd_time
    </sql>

    <!-- 根据产品名获取信息 -->
	<select id="getByName" resultMap="BaseResultMap">
		SELECT 
        <include refid="Base_Column_List" />
        FROM ad_product
		WHERE status = 0 AND product_name = #{productName}
		<choose>
			<when test="brandName != null and brandName !='' ">
				 AND brand_name = #{brandName}
			</when>
			<otherwise>
				 AND (brand_name is null OR brand_name = '')
			</otherwise>
        </choose>
        LIMIT 1
	</select>
	
	<!-- 获取产品下拉并去重条数 -->
	<select id="getSelectProductNameTotal" resultType="java.lang.Integer">
		SELECT
		COUNT(DISTINCT product_name) as total FROM ad_product
		WHERE status = 0
		<if test="productName != null and productName !=null ">
			 AND product_name LIKE CONCAT('%',#{productName},'%')
		</if>
	</select>
	
    <!-- 获取产品下拉并去重列表 -->
	<select id="getSelectProductNameList" resultType="java.lang.String">
		SELECT
		DISTINCT product_name FROM ad_product
		WHERE status = 0
		<if test="productName != null and productName !=null ">
			 AND product_name LIKE CONCAT('%',#{productName},'%')
		</if>
		ORDER BY id DESC
		<if test="pageNo != null">
		    LIMIT #{pageNo},#{pageSize}
		</if>
	</select>
</mapper>
