<?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.fs.store.mapper.FsDepartmentMapper">

    <resultMap type="FsDepartment" id="FsDepartmentResult">
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="deptCode"    column="dept_code"    />
        <result property="iconUrl"    column="icon_url"    />
        <result property="sort"    column="sort"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="status"    column="status"    />
        <result property="isTui"    column="is_tui"    />
    </resultMap>

    <sql id="selectFsDepartmentVo">
        select dept_id,is_tui, dept_name, dept_code, icon_url, sort, create_time, create_by, update_time, update_by, status from fs_department
    </sql>

    <select id="selectFsDepartmentList" parameterType="FsDepartment" resultMap="FsDepartmentResult">
        <include refid="selectFsDepartmentVo"/>
        <where>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="deptCode != null  and deptCode != ''"> and dept_code = #{deptCode}</if>
            <if test="iconUrl != null  and iconUrl != ''"> and icon_url = #{iconUrl}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="isTui != null "> and is_tui = #{isTui}</if>
        </where>
        order by sort,dept_id desc
    </select>

    <select id="selectFsDepartmentByDeptId" parameterType="Long" resultMap="FsDepartmentResult">
        <include refid="selectFsDepartmentVo"/>
        where dept_id = #{deptId}
    </select>

    <insert id="insertFsDepartment" parameterType="FsDepartment" useGeneratedKeys="true" keyProperty="deptId">
        insert into fs_department
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptName != null">dept_name,</if>
            <if test="deptCode != null">dept_code,</if>
            <if test="iconUrl != null">icon_url,</if>
            <if test="sort != null">sort,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="status != null">status,</if>
            <if test="isTui != null">is_tui,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptName != null">#{deptName},</if>
            <if test="deptCode != null">#{deptCode},</if>
            <if test="iconUrl != null">#{iconUrl},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="status != null">#{status},</if>
            <if test="isTui != null">#{isTui},</if>
         </trim>
    </insert>

    <update id="updateFsDepartment" parameterType="FsDepartment">
        update fs_department
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="deptCode != null">dept_code = #{deptCode},</if>
            <if test="iconUrl != null">icon_url = #{iconUrl},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isTui != null">is_tui = #{isTui},</if>
        </trim>
        where dept_id = #{deptId}
    </update>

    <delete id="deleteFsDepartmentByDeptId" parameterType="Long">
        delete from fs_department where dept_id = #{deptId}
    </delete>

    <delete id="deleteFsDepartmentByDeptIds" parameterType="String">
        delete from fs_department where dept_id in
        <foreach item="deptId" collection="array" open="(" separator="," close=")">
            #{deptId}
        </foreach>
    </delete>
</mapper>
