<?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.CmcTechnicalPlanMapper">

    <resultMap type="CmcTechnicalPlan" id="CmcTechnicalPlanResult">
        <result property="technicalPlanId"    column="technical_plan_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="technicalDirector"    column="technical_director"    />
        <result property="technicalDesigner"    column="technical_designer"    />
        <result property="technicalPlanner"    column="technical_planner"    />
        <result property="technicalAuditor"    column="technical_auditor"    />
        <result property="planComment"    column="plan_comment"    />
        <result property="qualityInspector"    column="quality_inspector"    />
        <result property="technicalTime"    column="technical_time"    />
        <result property="technicalDocument"    column="technical_document"    />
        <result property="modifyDocument"    column="modify_document"    />
        <result property="finalDocument"    column="final_document"    />
        <result property="designDescription"    column="design_description"    />
        <result property="technicalComment"    column="technical_comment"    />
        <result property="manageComment"    column="manage_comment"    />
        <result property="technicalUploadTime"    column="technical_upload_time"    />
        <result property="techApprovalTime"    column="tech_approval_time"    />
        <result property="manageApprovalTime"    column="manage_approval_time"    />
        <association property="directUser"    javaType="SysUser"         resultMap="DirectUserResult" />
        <association property="planUser"    javaType="SysUser"         resultMap="PlanUserResult" />
        <association property="designUser"    javaType="SysUser"         resultMap="DesignUserResult" />
        <association property="auditUser"    javaType="SysUser"         resultMap="AuditUserResult" />
        <association property="managerUser"    javaType="SysUser"         resultMap="ManagerUserResult" />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
    </resultMap>

    <resultMap type="SysUser" id="DirectUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="direct_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="PlanUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="plan_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="DesignUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="design_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="AuditUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="audit_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="ManagerUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="manager_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="selectCmcTechnicalPlanVo">
        select t.technical_plan_id, t.project_id, p.project_number, p.project_name, t.technical_director, u.nick_name as direct_nick_name, t.technical_designer, u1.nick_name as design_nick_name,
               t.technical_planner, u2.nick_name as plan_nick_name, t.plan_comment, t.quality_inspector, t.technical_time, t.technical_document, t.modify_document, t.final_document,
               t.design_description, t.technical_comment, t.technical_auditor, u3.nick_name as audit_nick_name, t.manager_user_id, u4.nick_name as manager_nick_name, t.manage_comment,
               t.technical_upload_time, t.tech_approval_time, t.manage_approval_time from cmc_technical_plan as t
        left join sys_user as u on u.user_id = t.technical_director
        left join sys_user as u1 on u1.user_id = t.technical_designer
        left join sys_user as u2 on u2.user_id = t.technical_planner
        left join sys_user as u3 on u3.user_id = t.technical_auditor
        left join sys_user as u4 on u4.user_id = t.manager_user_id
        left join cmc_project as p on p.project_id = t.project_id
    </sql>

    <select id="selectCmcTechnicalPlanList" parameterType="CmcTechnicalPlan" resultMap="CmcTechnicalPlanResult">
        <include refid="selectCmcTechnicalPlanVo"/>
        <where>
            <if test="projectId != null  and projectId != ''"> and t.project_id = #{projectId}</if>
            <if test="technicalDirector != null "> and t.technical_director = #{technicalDirector}</if>
            <if test="technicalDesigner != null "> and t.technical_designer = #{technicalDesigner}</if>
            <if test="technicalPlanner != null "> and t.technical_planner = #{technicalPlanner}</if>
            <if test="technicalAuditor != null "> and t.technical_auditor = #{technicalAuditor}</if>
            <if test="planComment != null  and planComment != ''"> and t.plan_comment = #{planComment}</if>
            <if test="qualityInspector != null  and qualityInspector != ''"> and t.quality_inspector = #{qualityInspector}</if>
            <if test="technicalTime != null "> and t.technical_time = #{technicalTime}</if>
            <if test="technicalDocument != null  and technicalDocument != ''"> and t.technical_document = #{technicalDocument}</if>
            <if test="designDescription != null  and designDescription != ''"> and t.design_description = #{designDescription}</if>
            <if test="technicalComment != null  and technicalComment != ''"> and t.technical_comment = #{technicalComment}</if>
            <if test="managerUserId != null "> and t.manager_user_id = #{managerUserId}</if>
            <if test="manageComment != null  and manageComment != ''"> and t.manage_comment = #{manageComment}</if>
            <if test="technicalUploadTime != null "> and t.technical_upload_time = #{technicalUploadTime}</if>
            <if test="techApprovalTime != null "> and t.tech_approval_time = #{techApprovalTime}</if>
            <if test="manageApprovalTime != null "> and t.manage_approval_time = #{manageApprovalTime}</if>
        </where>
        order by t.technical_time desc
    </select>

    <select id="selectCmcTechnicalPlanByTechnicalPlanId" parameterType="String" resultMap="CmcTechnicalPlanResult">
        <include refid="selectCmcTechnicalPlanVo"/>
        where t.technical_plan_id = #{technicalPlanId}
    </select>

    <insert id="insertCmcTechnicalPlan" parameterType="CmcTechnicalPlan">
        insert into cmc_technical_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="technicalPlanId != null">technical_plan_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="technicalDirector != null">technical_director,</if>
            <if test="technicalDesigner != null">technical_designer,</if>
            <if test="technicalPlanner != null">technical_planner,</if>
            <if test="technicalAuditor != null">technical_auditor,</if>
            <if test="planComment != null">plan_comment,</if>
            <if test="qualityInspector != null">quality_inspector,</if>
            <if test="technicalTime != null">technical_time,</if>
            <if test="technicalDocument != null">technical_document,</if>
            <if test="modifyDocument != null">modify_document,</if>
            <if test="finalDocument != null">final_document,</if>
            <if test="designDescription != null">design_description,</if>
            <if test="technicalComment != null">technical_comment,</if>
            <if test="managerUserId != null">manager_user_id,</if>
            <if test="manageComment != null">manage_comment,</if>
            <if test="technicalUploadTime != null">technical_upload_time,</if>
            <if test="techApprovalTime != null">tech_approval_time,</if>
            <if test="manageApprovalTime != null">manage_approval_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="technicalPlanId != null">#{technicalPlanId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="technicalDirector != null">#{technicalDirector},</if>
            <if test="technicalDesigner != null">#{technicalDesigner},</if>
            <if test="technicalPlanner != null">#{technicalPlanner},</if>
            <if test="technicalAuditor != null">#{technicalAuditor},</if>
            <if test="planComment != null">#{planComment},</if>
            <if test="qualityInspector != null">#{qualityInspector},</if>
            <if test="technicalTime != null">#{technicalTime},</if>
            <if test="technicalDocument != null">#{technicalDocument},</if>
            <if test="modifyDocument != null">#{modifyDocument},</if>
            <if test="finalDocument != null">#{finalDocument},</if>
            <if test="designDescription != null">#{designDescription},</if>
            <if test="technicalComment != null">#{technicalComment},</if>
            <if test="managerUserId != null">#{managerUserId},</if>
            <if test="manageComment != null">#{manageComment},</if>
            <if test="technicalUploadTime != null">#{technicalUploadTime},</if>
            <if test="techApprovalTime != null">#{techApprovalTime},</if>
            <if test="manageApprovalTime != null">#{manageApprovalTime},</if>
        </trim>
    </insert>

    <update id="updateCmcTechnicalPlan" parameterType="CmcTechnicalPlan">
        update cmc_technical_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="technicalDirector != null">technical_director = #{technicalDirector},</if>
            <if test="technicalDesigner != null">technical_designer = #{technicalDesigner},</if>
            <if test="technicalPlanner != null">technical_planner = #{technicalPlanner},</if>
            <if test="technicalAuditor != null">technical_auditor = #{technicalAuditor},</if>
            <if test="planComment != null">plan_comment = #{planComment},</if>
            <if test="qualityInspector != null">quality_inspector = #{qualityInspector},</if>
            <if test="technicalTime != null">technical_time = #{technicalTime},</if>
            <if test="technicalDocument != null">technical_document = #{technicalDocument},</if>
            <if test="modifyDocument != null">modify_document = #{modifyDocument},</if>
            <if test="finalDocument != null">final_document = #{finalDocument},</if>
            <if test="designDescription != null">design_description = #{designDescription},</if>
            <if test="technicalComment != null">technical_comment = #{technicalComment},</if>
            <if test="managerUserId != null">manager_user_id = #{managerUserId},</if>
            <if test="manageComment != null">manage_comment = #{manageComment},</if>
            <if test="technicalUploadTime != null">technical_upload_time = #{technicalUploadTime},</if>
            <if test="techApprovalTime != null">tech_approval_time = #{techApprovalTime},</if>
            <if test="manageApprovalTime != null">manage_approval_time = #{manageApprovalTime},</if>
        </trim>
        where technical_plan_id = #{technicalPlanId}
    </update>

    <delete id="deleteCmcTechnicalPlanByTechnicalPlanId" parameterType="String">
        delete from cmc_technical_plan where technical_plan_id = #{technicalPlanId}
    </delete>

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