<?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.SceneDao">

    <select id="queryObject" resultType="com.platform.entity.SceneEntity">
		select *  from nideshop_scene
		where id = #{id}
	</select>

    <select id="findByName" resultType="com.platform.entity.SceneEntity">
        select * from nideshop_scene where name = #{brandName}
    </select>


    <select id="queryBySceneNum" resultType="com.platform.entity.SceneEntity">
        select * from nideshop_scene where scene_num = #{sceneNum} limit 1
    </select>

    <select id="selectByDeptIdsAndExceptSceneNums" resultType="com.platform.entity.SceneEntity">
        select * from nideshop_scene scene
        <if test="deptIds != null and deptIds.size() > 0">
            join sys_user user on scene.create_user_id = user.user_id
        </if>
        <where>
            <if test="sceneNums != null and sceneNums.size() > 0">
                AND scene.scene_num not in
                <foreach item="num" collection="sceneNums" open="(" separator="," close=")">
                    #{num}
                </foreach>
            </if>
            <if test="deptIds != null and deptIds.size() > 0">
                AND user.dept_id in
                <foreach item="deptId" collection="deptIds" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryList" resultType="com.platform.entity.SceneEntity">
        select * from nideshop_scene scene
        <if test="deptIdList != null and deptIdList.size() > 0">
            join sys_user user on scene.create_user_id = user.user_id
        </if>
        <where>
            <if test="name != null and name.trim() != ''">
                scene.name LIKE concat('%',#{name},'%')
            </if>
            <if test="idList != null and idList.size() > 0">
                AND scene.id in
                <foreach item="id" collection="idList" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="deptIdList != null and deptIdList.size() > 0">
                AND user.dept_id in
                <foreach item="id" collection="deptIdList" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="createUserId != null">
                scene.create_user_id LIKE concat('%',#{createUserId},'%')
            </if>
        </where>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by create_time desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from nideshop_scene
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
        <if test="idList != null and idList.size() > 0">
            AND id in
            <foreach item="id" collection="idList" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <insert id="save" parameterType="com.platform.entity.SceneEntity" useGeneratedKeys="true" keyProperty="id">
        insert into nideshop_scene(`name`,`is_show`,`app_list_pic_url`,`scene_url`,`scene_num`,`create_user_id`,
        `update_user_id`,`create_time`,`update_time`,deleted
        )
        values(#{name},#{isShow},#{appListPicUrl},#{sceneUrl},#{sceneNum},#{createUserId}, #{updateUserId}, now(),
        now(),0)
	</insert>

    <update id="update" parameterType="com.platform.entity.SceneEntity">
        update nideshop_scene
        <set>
            <if test="name != null">`name` = #{name},</if>
            <if test="isShow != null">`is_show` = #{isShow},</if>
            <if test="appListPicUrl != null">`app_list_pic_url` = #{appListPicUrl},</if>
            <if test="sceneUrl != null">`scene_url` = #{sceneUrl},</if>
            <if test="sceneNum != null">`scene_num` = #{sceneNum},</if>
            `update_user_id` = #{updateUserId},
            `create_user_id` = #{createUserId},`update_time` = now()
        </set>
        where id = #{id}
    </update>

    <update id="updateSceneUser">
        update nideshop_scene set create_user_id = #{toUserId},`update_time` = now(),update_user_id=#{updateUserId} where create_user_id = #{fromUserId}
    </update>

    <delete id="delete">
		update nideshop_scene set deleted = 1 where id = #{value}
	</delete>

    <select id="queryByUserId" resultType="com.platform.entity.SceneEntity">
        select * from nideshop_scene where create_user_id = #{value}
    </select>

    <delete id="deleteBatch">
        update nideshop_scene set deleted = 1
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>