<?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.CmcProjectChangeMapper">
    
    <resultMap type="CmcProjectChange" id="CmcProjectChangeResult">
        <result property="changeId"    column="change_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="undertakingDept"    column="undertaking_dept"    />
        <result property="content"    column="content"    />
        <result property="registrant"    column="registrant"    />
        <result property="registerTime"    column="register_time"    />
        <association property="leaderUser"    javaType="SysUser"         resultMap="LeaderUserResult" />
        <association property="registrantUser"    javaType="SysUser"         resultMap="RegistrantUserResult" />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
    </resultMap>

    <resultMap type="SysUser" id="LeaderUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="leader_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="RegistrantUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="registrant_nick_name"    />
    </resultMap>

    <resultMap type="CmcProject" id="CmcProjectResult">
        <result property="projectId"    column="project_id"    />
        <result property="projectNumber"    column="project_number"    />
        <result property="projectName"    column="project_name"    />
        <result property="undertakingDept"    column="undertaking_dept"    />
    </resultMap>

    <sql id="selectCmcProjectChangeVo">
        select pc.change_id, pc.project_id, p.project_name, p.project_number, p.undertaking_dept, pc.content, pc.registrant, u1.nick_name as registrant_nick_name, pc.register_time,
        u.nick_name as leader_nick_name from cmc_project_change as pc
        left join sys_user as u1 on u1.user_id = pc.registrant
        left join cmc_project as p on p.project_id = pc.project_id
        left join sys_user as u on u.user_id = p.project_leader
    </sql>

    <select id="selectCmcProjectChangeList" parameterType="CmcProjectChange" resultMap="CmcProjectChangeResult">
        <include refid="selectCmcProjectChangeVo"/>
        <where>  
            <if test="projectId != null  and projectId != ''"> and pc.project_id = #{projectId}</if>
            <if test="content != null  and content != ''"> and pc.content = #{content}</if>
            <if test="registrant != null "> and pc.registrant = #{registrant}</if>
            <if test="registerTime != null "> and pc.register_time = #{registerTime}</if>
            <if test="undertakingDept != null "> and p.undertaking_dept like concat("%", #{undertakingDept} ,"%")</if>
        </where>
        order by pc.register_time desc
    </select>
    
    <select id="selectCmcProjectChangeByChangeId" parameterType="Long" resultMap="CmcProjectChangeResult">
        <include refid="selectCmcProjectChangeVo"/>
        where pc.change_id = #{changeId}
    </select>
        
    <insert id="insertCmcProjectChange" parameterType="CmcProjectChange" useGeneratedKeys="true" keyProperty="changeId">
        insert into cmc_project_change
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="changeId != null">change_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="content != null">content,</if>
            <if test="registrant != null">registrant,</if>
            <if test="registerTime != null">register_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="changeId != null">#{changeId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="content != null">#{content},</if>
            <if test="registrant != null">#{registrant},</if>
            <if test="registerTime != null">#{registerTime},</if>
         </trim>
    </insert>

    <update id="updateCmcProjectChange" parameterType="CmcProjectChange">
        update cmc_project_change
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="content != null">content = #{content},</if>
            <if test="registrant != null">registrant = #{registrant},</if>
            <if test="registerTime != null">register_time = #{registerTime},</if>
        </trim>
        where change_id = #{changeId}
    </update>

    <delete id="deleteCmcProjectChangeByChangeId" parameterType="Long">
        delete from cmc_project_change where change_id = #{changeId}
    </delete>

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