<?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.platform.dao.ApiBrandMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.platform.entity.BrandVo" id="brandMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="listPicUrl" column="list_pic_url"/>
        <result property="simpleDesc" column="simple_desc"/>
        <result property="picUrl" column="pic_url"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="isShow" column="is_show"/>
        <result property="floorPrice" column="floor_price"/>
        <result property="appListPicUrl" column="app_list_pic_url"/>
        <result property="isNew" column="is_new"/>
        <result property="newPicUrl" column="new_pic_url"/>
        <result property="newSortOrder" column="new_sort_order"/>
        <result property="type" column="type"/>
        <result property="sceneUrl" column="scene_url"/>
        <result property="sceneNum" column="scene_num"/>
        <result property="shareWxQrCode" column="share_wx_qr_code"/>
        <result property="address" column="address"/>
        <result property="retailPrice" column="retail_price"/>
        <result property="averagePrice" column="average_price"/>
        <result property="pvTotalNum" column="pv_total_num"/>
        <result property="liveRoomUrl" column="live_room_url"/>
        <result property="sceneName" column="scene_name"/>
    </resultMap>

    <select id="queryIdByScene" resultType="java.lang.Integer">
        select
        `id`
        from nideshop_brand
        where scene_num = #{sceneNum}
    </select>

    <select id="queryByScene" resultMap="brandMap">
        select
        *
        from nideshop_brand
        where scene_num = #{sceneNum}
    </select>

    <select id="getBrandTypeList" resultType="java.lang.Integer">
		select type from nideshop_brand group by type
	</select>

    <select id="queryObject" resultMap="brandMap">
		select * from nideshop_brand where id = #{value}
	</select>

    <select id="queryList" resultMap="brandMap">
        select
        <if test="fields != null and fields != ''">
            ${fields}
        </if>
        <if test="fields == null or fields == ''">
            nideshop_brand.*
        </if>
        from nideshop_brand
        <if test="userId != null">
            join sys_user_brand userBrand on ( userBrand.brand_id = nideshop_brand.id and userBrand.enable = 1 and userBrand.user_id = #{userId})
        </if>
        where 1=1
        <if test="isShow != null">
            and is_show = #{isShow}
        </if>
        <if test="isShow != null and  userId != null">
            and nideshop_brand.create_user_id = #{userId}
        </if>
        <if test="livestreamStatus != null">
            and livestream_status = #{livestreamStatus}
        </if>
        <if test="type != null">
            and type = #{type}
        </if>
        <if test="city != address and address!=''">
            and address like concat('%',#{address},'%')
        </if>
        <if test="name != null and name!=''">
            and name like concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by nideshop_brand.sort_order asc,nideshop_brand.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>

    </select>

    <select id="queryTotal" resultType="int">
		select count(*) from nideshop_brand
        where is_show = 1
        <if test="type != null">
            and type = #{type}
        </if>
        <if test="name != null">
            and name like concat('%',#{name},'%')
        </if>
	</select>

    <insert id="save" parameterType="com.platform.entity.BrandVo" useGeneratedKeys="true" keyProperty="id">
		insert into nideshop_brand
		(
			`name`, 
			`list_pic_url`,
			`simple_desc`,
			`pic_url`,
			`sort_order`, 
			`is_show`, 
			`floor_price`, 
			`app_list_pic_url`, 
			`is_new`, 
			`new_pic_url`, 
			`new_sort_order`,
			`type`,
			`scene_url`,
			`scene_num`,
			`share_wx_qr_code`
		)
		values
		(
			#{name}, 
			#{listPicUrl},
			#{simpleDesc},
			#{picUrl},
			#{sortOrder},
			#{isShow},
			#{floorPrice},
			#{appListPicUrl},
			#{isNew},
			#{newPicUrl},
			#{newSortOrder},
			#{type},
			#{sceneUrl},
			#{sceneNum},
			#{shareWxQrCode}
		)
	</insert>

    <update id="update" parameterType="com.platform.entity.BrandVo">
        update nideshop_brand
        <set>
            <if test="name != null">`name` = #{name},</if>
            <if test="list_pic_url != null">`list_pic_url` = #{list_pic_url},</if>
            <if test="simple_desc != null">`simple_desc` = #{simple_desc},</if>
            <if test="pic_url != null">`pic_url` = #{pic_url},</if>
            <if test="sort_order != null">`sort_order` = #{sort_order},</if>
            <if test="is_show != null">`is_show` = #{is_show},</if>
            <if test="floor_price != null">`floor_price` = #{floor_price},</if>
            <if test="app_list_pic_url != null">`app_list_pic_url` = #{app_list_pic_url},</if>
            <if test="is_new != null">`is_new` = #{is_new},</if>
            <if test="new_pic_url != null">`new_pic_url` = #{new_pic_url},</if>
            <if test="new_sort_order != null">`new_sort_order` = #{new_sort_order},</if>
            <if test="type != null">`type` = #{type},</if>
            <if test="sceneNum != null">`scene_num` = #{sceneNum},</if>
            <if test="shareWxQrCode != null">`share_wx_qr_code` = #{shareWxQrCode},</if>
            <if test="sceneUrl != null">`scene_url` = #{sceneUrl}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from nideshop_brand where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from nideshop_brand where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getRecommondDeptBrand" resultType="com.platform.entity.BrandVo">
        select dept.logo_url appListPicUrl,dept.dept_id id,dept.name from sys_dept dept
        join nideshop_brand brand on dept.dept_id = brand.create_user_dept_id
        group by dept.dept_id order by count(*) desc limit 0,3
    </select>

    <select id="increaseViewCount">
        update nideshop_brand set pv_total_num = pv_total_num + 1 where id= #{id}
    </select>

    <select id="getTopPvNumByDeptId" resultType="java.lang.Integer">
        select id from nideshop_brand brand where create_user_dept_id = #{deptId} order by pv_total_num desc limit 1
    </select>

</mapper>