<?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.ruoyi.oa.mapper.CmcManageDeptMapper">
    
    <resultMap type="CmcManageDept" id="CmcManageDeptResult">
        <result property="userId"    column="user_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="partyAId"    column="party_a_id"    />
        <association property="user"    javaType="SysUser"         resultMap="SysUserResult" />
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <sql id="selectCmcManageDeptVo">
        select md.user_id, u.nick_name as nick_name, md.dept_id, md.party_a_id from cmc_manage_dept as md
        left join sys_user as u on u.user_id = md.user_id
    </sql>

    <select id="selectCmcManageDeptList" parameterType="CmcManageDept" resultMap="CmcManageDeptResult">
        <include refid="selectCmcManageDeptVo"/>
        <where>
            <if test="deptId != null  and deptId != ''"> and find_in_set(#{deptId}, md.dept_id)</if>
            <if test="partyAId != null  and partyAId != ''"> and find_in_set(#{partyAId}, md.party_a_id)</if>
        </where>
    </select>
    
    <select id="selectCmcManageDeptByUserId" parameterType="Long" resultMap="CmcManageDeptResult">
        <include refid="selectCmcManageDeptVo"/>
        where md.user_id = #{userId}
    </select>
        
    <insert id="insertCmcManageDept" parameterType="CmcManageDept">
        insert into cmc_manage_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="partyAId != null">party_a_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="partyAId != null">#{partyAId},</if>
         </trim>
    </insert>

    <update id="updateCmcManageDept" parameterType="CmcManageDept">
        update cmc_manage_dept
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null and deptId != ''">dept_id = #{deptId},</if>
            party_a_id = #{partyAId}
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteCmcManageDeptByUserId" parameterType="Long">
        delete from cmc_manage_dept where user_id = #{userId}
    </delete>

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