<?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.qw.mapper.QwDeptMapper">
    
    <resultMap type="QwDept" id="QwDeptResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="parentid"    column="parentid"    />
        <result property="companyId"    column="company_id"    />
        <result property="corpId"    column="corp_id"    />
    </resultMap>

    <sql id="selectQwDeptVo">
        select id, dept_id, dept_name, parentid, company_id, corp_id from qw_dept
    </sql>

    <select id="selectQwDeptList" parameterType="QwDept" resultMap="QwDeptResult">
        <include refid="selectQwDeptVo"/>
        <where>  
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="parentid != null "> and parentid = #{parentid}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="corpId != null "> and corp_id = #{corpId}</if>
        </where>
    </select>
    
    <select id="selectQwDeptById" parameterType="Long" resultMap="QwDeptResult">
        <include refid="selectQwDeptVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertQwDept" parameterType="QwDept">
        insert into qw_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="parentid != null">parentid,</if>
            <if test="companyId != null">company_id,</if>
            <if test="corpId != null">corp_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="parentid != null">#{parentid},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="corpId != null">#{corpId},</if>
         </trim>
    </insert>

    <insert id="insertOrUpdateQwDept" parameterType="QwDept">
        insert into qw_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="parentid != null">parentid,</if>
            <if test="companyId != null">company_id,</if>
            <if test="corpId != null">corp_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="parentid != null">#{parentid},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="corpId != null">#{corpId},</if>
        </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="parentid != null">parentid = #{parentid},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
        </trim>
    </insert>


    <update id="updateQwDept" parameterType="QwDept">
        update qw_dept
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="parentid != null">parentid = #{parentid},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwDeptById" parameterType="Long">
        delete from qw_dept where id = #{id}
    </delete>

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