<?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.CmcArchiveMapper">
    
    <resultMap type="CmcArchive" id="CmcArchiveResult">
        <result property="archiveId"    column="archive_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectLeader"    column="project_leader"    />
        <result property="submitTime"    column="submit_time"    />
        <result property="submitSituation"    column="submit_situation"    />
        <result property="produceInspector"    column="produce_inspector"    />
        <result property="qualityInspector"    column="quality_inspector"    />
        <result property="deptLeader"    column="dept_leader"    />
        <result property="archiver"    column="archiver"    />
        <result property="receiveTime"    column="receive_time"    />
        <result property="qualityTime"    column="quality_time"    />
        <result property="produceTime"    column="produce_time"    />
        <result property="deptTime"    column="dept_time"    />
        <result property="archiveTime"    column="archive_time"    />
        <result property="produceComment"    column="produce_comment"    />
        <result property="qualityComment"    column="quality_comment"    />
        <result property="deptComment"    column="dept_comment"    />
        <result property="archiveComment"    column="archive_comment"    />
        <association property="produceUser"    javaType="SysUser"         resultMap="ProduceUserResult" />
        <association property="qualityUser"    javaType="SysUser"         resultMap="QualityUserResult" />
        <association property="deptUser"    javaType="SysUser"         resultMap="DeptUserResult" />
        <association property="archiveUser"    javaType="SysUser"         resultMap="ArchiveUserResult" />
        <association property="leaderUser"    javaType="SysUser"         resultMap="LeaderUserResult" />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
    </resultMap>

    <resultMap type="SysUser" id="ProduceUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="produce_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="QualityUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="quality_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="DeptUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="dept_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="ArchiveUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="archive_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="LeaderUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="leader_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"    />
    </resultMap>

    <sql id="selectCmcArchiveVo">
        select a.archive_id, a.project_id, p.project_number, p.project_name, a.project_leader, u4.nick_name as leader_nick_name, a.submit_time, a.submit_situation, a.produce_inspector,
               u.nick_name as produce_nick_name, a.quality_inspector, u1.nick_name as quality_nick_name, a.dept_leader, u2.nick_name as dept_nick_name, a.archiver,
               u3.nick_name as archive_nick_name, a.receive_time, a.quality_time, a.produce_time, a.dept_time, a.archive_time, a.produce_comment, a.quality_comment, a.dept_comment,
               a.archive_comment from cmc_archive as a
        left join sys_user as u on u.user_id = a.produce_inspector
        left join sys_user as u1 on u1.user_id = a.quality_inspector
        left join sys_user as u2 on u2.user_id = a.dept_leader
        left join sys_user as u3 on u3.user_id = a.archiver
        left join sys_user as u4 on u4.user_id = a.project_leader
        left join cmc_project as p on p.project_id = a.project_id
    </sql>

    <select id="selectCmcArchiveList" parameterType="CmcArchive" resultMap="CmcArchiveResult">
        <include refid="selectCmcArchiveVo"/>
        <where>

            <if test="projectId != null  and projectId != ''"> and a.project_id = #{projectId}</if>
            <if test="projectNumber != null  and projectNumber != ''"> and p.project_number like concat('%', #{projectNumber}, '%')</if>
            <if test="projectLeader != null  and projectLeader != ''"> and a.project_leader = #{projectLeader}</if>
            <if test="submitTime != null "> and a.submit_time = #{submitTime}</if>
            <if test="submitSituation != null  and submitSituation != ''"> and a.submit_situation = #{submitSituation}</if>
            <if test="produceInspector != null "> and a.produce_inspector = #{produceInspector}</if>
            <if test="qualityInspector != null "> and a.quality_inspector = #{qualityInspector}</if>
            <if test="deptLeader != null "> and a.dept_leader = #{deptLeader}</if>
            <if test="archiver != null "> and a.archiver = #{archiver}</if>
            <if test="receiveTime != null "> and a.receive_time = #{receiveTime}</if>
            <if test="qualityTime != null "> and a.quality_time = #{qualityTime}</if>
            <if test="produceTime != null "> and a.produce_time = #{produceTime}</if>
            <if test="deptTime != null "> and a.dept_time = #{deptTime}</if>
            <if test="archiveTime != null and @com.ruoyi.common.utils.DateUtils@parseDateToStr('yyyy', archiveTime) == '2000' "> and a.archive_time is not null</if>
            <if test="produceComment != null  and produceComment != ''"> and a.produce_comment = #{produceComment}</if>
            <if test="qualityComment != null  and qualityComment != ''"> and a.quality_comment = #{qualityComment}</if>
            <if test="deptComment != null  and deptComment != ''"> and a.dept_comment = #{deptComment}</if>
            <if test="archiveComment != null  and archiveComment != ''"> and a.archive_comment = #{archiveComment}</if>
        </where>
        order by a.submit_time desc
    </select>
    
    <select id="selectCmcArchiveByArchiveId" parameterType="String" resultMap="CmcArchiveResult">
        <include refid="selectCmcArchiveVo"/>
        where a.archive_id = #{archiveId}
    </select>
        
    <insert id="insertCmcArchive" parameterType="CmcArchive">
        insert into cmc_archive
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="archiveId != null">archive_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="projectLeader != null">project_leader,</if>
            <if test="submitTime != null">submit_time,</if>
            <if test="submitSituation != null">submit_situation,</if>
            <if test="produceInspector != null">produce_inspector,</if>
            <if test="qualityInspector != null">quality_inspector,</if>
            <if test="deptLeader != null">dept_leader,</if>
            <if test="archiver != null">archiver,</if>
            <if test="receiveTime != null">receive_time,</if>
            <if test="qualityTime != null">quality_time,</if>
            <if test="produceTime != null">produce_time,</if>
            <if test="deptTime != null">dept_time,</if>
            <if test="archiveTime != null">archive_time,</if>
            <if test="produceComment != null">produce_comment,</if>
            <if test="qualityComment != null">quality_comment,</if>
            <if test="deptComment != null">dept_comment,</if>
            <if test="archiveComment != null">archive_comment,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="archiveId != null">#{archiveId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="projectLeader != null">#{projectLeader},</if>
            <if test="submitTime != null">#{submitTime},</if>
            <if test="submitSituation != null">#{submitSituation},</if>
            <if test="produceInspector != null">#{produceInspector},</if>
            <if test="qualityInspector != null">#{qualityInspector},</if>
            <if test="deptLeader != null">#{deptLeader},</if>
            <if test="archiver != null">#{archiver},</if>
            <if test="receiveTime != null">#{receiveTime},</if>
            <if test="qualityTime != null">#{qualityTime},</if>
            <if test="produceTime != null">#{produceTime},</if>
            <if test="deptTime != null">#{deptTime},</if>
            <if test="archiveTime != null">#{archiveTime},</if>
            <if test="produceComment != null">#{produceComment},</if>
            <if test="qualityComment != null">#{qualityComment},</if>
            <if test="deptComment != null">#{deptComment},</if>
            <if test="archiveComment != null">#{archiveComment},</if>
         </trim>
    </insert>

    <update id="updateCmcArchive" parameterType="CmcArchive">
        update cmc_archive
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="projectLeader != null">project_leader = #{projectLeader},</if>
            <if test="submitTime != null">submit_time = #{submitTime},</if>
            <if test="submitSituation != null">submit_situation = #{submitSituation},</if>
            <if test="produceInspector != null">produce_inspector = #{produceInspector},</if>
            <if test="qualityInspector != null">quality_inspector = #{qualityInspector},</if>
            <if test="deptLeader != null">dept_leader = #{deptLeader},</if>
            <if test="archiver != null">archiver = #{archiver},</if>
            <if test="receiveTime != null">receive_time = #{receiveTime},</if>
            <if test="qualityTime != null">quality_time = #{qualityTime},</if>
            <if test="produceTime != null">produce_time = #{produceTime},</if>
            <if test="deptTime != null">dept_time = #{deptTime},</if>
            <if test="archiveTime != null">archive_time = #{archiveTime},</if>
            <if test="produceComment != null">produce_comment = #{produceComment},</if>
            <if test="qualityComment != null">quality_comment = #{qualityComment},</if>
            <if test="deptComment != null">dept_comment = #{deptComment},</if>
            <if test="archiveComment != null">archive_comment = #{archiveComment},</if>
        </trim>
        where archive_id = #{archiveId}
    </update>

    <delete id="deleteCmcArchiveByArchiveId" parameterType="String">
        delete from cmc_archive where archive_id = #{archiveId}
    </delete>

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