<?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.dgtly.system.mapper.SysUserOrderAuthorMapper">
    
    <resultMap type="SysUserOrderAuthor" id="SysUserOrderAuthorResult">
        <result property="authorId"    column="author_id"    />
        <result property="authorName"    column="author_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="code"    column="code"    />
        <result property="orderNum"    column="order_num"    />
        <result property="authorType"    column="author_type"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="parentName" column="parent_name" />
    </resultMap>

    <sql id="selectSysUserOrderAuthorVo">
        select author_id, author_name, parent_id, code, order_num, author_type, create_by, create_time, update_by, update_time, remark from sys_user_order_author
    </sql>

    <select id="selectSysUserOrderAuthorList" parameterType="SysUserOrderAuthor" resultMap="SysUserOrderAuthorResult">
        <include refid="selectSysUserOrderAuthorVo"/>
        <where>  
            <if test="authorName != null  and authorName != ''"> and author_name like concat('%', #{authorName}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="authorType != null  and authorType != ''"> and author_type = #{authorType}</if>
        </where>
        order by parent_id,order_num
    </select>

    <select id="selectAuthorTree" parameterType="Long" resultType="Long">
		select m.author_id
		from sys_user_order_author m
			left join sys_user_author_rel rm on m.author_id = rm.author_id
		where rm.user_id = #{userId}
		order by m.parent_id, m.order_num
	</select>

    <select id="selectSysUserOrderAuthorById" parameterType="Long" resultMap="SysUserOrderAuthorResult">
        select t.author_id, t.author_name, t.parent_id, t.code, t.order_num, t.author_type, t.create_by, t.create_time, t.update_by, t.update_time, t.remark, p.author_name as parent_name
        from sys_user_order_author t
        left join sys_user_order_author p on p.author_id = t.parent_id
        where t.author_id = #{authorId}
    </select>
        
    <insert id="insertSysUserOrderAuthor" parameterType="SysUserOrderAuthor" useGeneratedKeys="true" keyProperty="authorId">
        insert into sys_user_order_author
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="authorName != null  and authorName != ''">author_name,</if>
            <if test="parentId != null ">parent_id,</if>
            <if test="code != null  and code != ''">code,</if>
            <if test="orderNum != null ">order_num,</if>
            <if test="authorType != null  and authorType != ''">author_type,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="remark != null  and remark != ''">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="authorName != null  and authorName != ''">#{authorName},</if>
            <if test="parentId != null ">#{parentId},</if>
            <if test="code != null  and code != ''">#{code},</if>
            <if test="orderNum != null ">#{orderNum},</if>
            <if test="authorType != null  and authorType != ''">#{authorType},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="remark != null  and remark != ''">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSysUserOrderAuthor" parameterType="SysUserOrderAuthor">
        update sys_user_order_author
        <trim prefix="SET" suffixOverrides=",">
            <if test="authorName != null  and authorName != ''">author_name = #{authorName},</if>
            <if test="parentId != null ">parent_id = #{parentId},</if>
            <if test="code != null  and code != ''">code = #{code},</if>
            <if test="orderNum != null ">order_num = #{orderNum},</if>
            <if test="authorType != null  and authorType != ''">author_type = #{authorType},</if>
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="remark != null  and remark != ''">remark = #{remark},</if>
        </trim>
        where author_id = #{authorId}
    </update>

    <delete id="deleteSysUserOrderAuthorById" parameterType="Long">
        delete from sys_user_order_author where author_id = #{authorId}
    </delete>

    <delete id="deleteSysUserOrderAuthorByIds" parameterType="String">
        delete from sys_user_order_author where author_id in 
        <foreach item="authorId" collection="array" open="(" separator="," close=")">
            #{authorId}
        </foreach>
    </delete>

    <select id="selectAuthorIdByCode" parameterType="SysUserOrderAuthor" resultMap="SysUserOrderAuthorResult">
        SELECT
            parent_id,
            author_id
        FROM
            sys_user_order_author
        WHERE
                parent_id = (
                SELECT
                    author_id
                FROM
                    sys_user_order_author
                WHERE
                      1=1
                      <if test="code !=null and code !='' ">
                          and  code =#{code}
                      </if>
            )<if test="divisionCode !=null and divisionCode !='' "> and  code =#{divisionCode}</if>
        <if test="divisionCode ==null  ">and  code not in ('NVB2023T05','NVB2023T01','NVB2023T03','NVB2023T02','NVB2023T04','其中-公装事业部','其中-家装事业部')</if>
    </select>
    <select id="selectByParentId"  parameterType="SysUserOrderAuthor" resultMap="SysUserOrderAuthorResult">
        SELECT
            parent_id,
            author_id
        FROM
            sys_user_order_author
        WHERE 1=1
        <if test=" authorList != null and authorList.size > 0  ">
             and    parent_id in
         <foreach collection="authorList" item="authId" index="index" open="(" close=")" separator=",">
            #{authId}
        </foreach>
        </if>
    </select>

    <select id="selectAuthorIdByParentId" parameterType="SysUserOrderAuthor" resultMap="SysUserOrderAuthorResult">
    SELECT
    parent_id,
    author_id
    FROM
    sys_user_order_author
    WHERE
    parent_id = #{parentId}
    </select>
    <select id="selectAuthorIdByParentIdList" parameterType="SysUserOrderAuthor" resultMap="SysUserOrderAuthorResult">
        SELECT
            parent_id,
            author_id
        FROM
            sys_user_order_author
        WHERE
            parent_id = #{parentId}
    </select>

</mapper>