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

    <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="projectLeader"    column="project_leader"    />
        <result property="isFinished"    column="is_finished"    />
        <result property="projectSource"    column="project_source"    />
        <result property="partyAId"    column="party_a_id"    />
        <result property="contactPerson"    column="contact_person"    />
        <result property="telephone"    column="telephone"    />
        <result property="projectType"    column="project_type"    />
        <result property="projectLevel"    column="project_level"    />
        <result property="projectRegistrant"    column="project_registrant"    />
        <result property="undertakingDept"    column="undertaking_dept"    />
        <result property="projectPlanner"    column="project_planner"    />
        <result property="qualityInspector"    column="quality_inspector"    />
        <result property="siteLeader"    column="site_leader"    />
        <result property="organizeDocument"    column="organize_document"    />
        <result property="organizeTime"    column="organize_time"    />
        <result property="registerTime"    column="register_time"    />
        <result property="planTime"    column="plan_time"    />
        <result property="entryTime"    column="entry_time"    />
        <result property="exitTime"    column="exit_time"    />
        <result property="remark"    column="remark"    />
        <result property="projectKml"    column="project_kml"    />
        <result property="projectCp"    column="project_cp"    />
        <result property="kmlLng"    column="kml_lng"    />
        <result property="kmlLat"    column="kml_lat"    />
        <result property="cars"    column="cars"    />
        <result property="drivers"    column="drivers"    />
        <result property="devices"    column="devices"    />
        <result property="participates"    column="participates"    />
        <result property="taskDocument"    column="task_document"    />
        <result property="projectRequest"    column="project_request"    />
        <association property="projectLeaderUser"    javaType="SysUser"         resultMap="ProjectLeaderResult" />
        <association property="projectRegistrantUser"    javaType="SysUser"         resultMap="ProjectRegistrantResult" />
        <association property="technicalDirectorUser"    javaType="SysUser"         resultMap="TechnicalDirectorResult" />
        <association property="partyA"    javaType="CmcPartyA"         resultMap="CmcPartyAResult" />
    </resultMap>

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

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

    <resultMap type="SysUser" id="TechnicalDirectorResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="director_nick_name"    />
    </resultMap>

    <resultMap type="CmcPartyA" id="CmcPartyAResult">
        <result property="partyAId"    column="party_a_id"    />
        <result property="partyAName"    column="party_a_name"    />
    </resultMap>

    <sql id="selectCmcProjectVo">
        select p.project_id, p.project_number, p.project_name, p.project_leader, u.nick_name as leader_nick_name, p.is_finished, p.project_source, p.party_a_id, pa.party_a_name, p.contact_person, p.telephone,
               p.project_type, p.project_level, p.project_registrant, u1.nick_name as registrant_nick_name, p.undertaking_dept, p.project_planner, p.site_leader, p.organize_document, p.organize_time,
               p.register_time, p.plan_time, p.entry_time, p.exit_time, p.remark, tp.technical_director, u2.nick_name as director_nick_name, tp.quality_inspector, p.project_kml, p.project_cp, p.kml_lng, p.kml_lat,
               p.cars, p.drivers, p.devices, p.participates, p.task_document, p.project_request from cmc_project as p
        left join sys_user as u on u.user_id = p.project_leader
        left join sys_user as u1 on u1.user_id = p.project_registrant
        left join cmc_technical_plan as tp on tp.project_id = p.project_id
        left join sys_user as u2 on u2.user_id = tp.technical_director
        left join cmc_party_a as pa on pa.party_a_id = p.party_a_id
    </sql>

    <select id="selectCmcProjectList" parameterType="CmcProject" resultMap="CmcProjectResult">
        <include refid="selectCmcProjectVo"/>
        <where>
            <if test="projectId != null  and projectId != ''"> and p.project_id = #{projectId}</if>
            <if test="projectNumber != null  and projectNumber != ''"> and p.project_number like concat('%', #{projectNumber}, '%')</if>
            <if test="projectName != null  and projectName != ''"> and p.project_name like concat('%', #{projectName}, '%')</if>
            <if test="projectLeader != null "> and p.project_leader = #{projectLeader}</if>
            <if test="isFinished != null  and isFinished != ''"> and p.is_finished = #{isFinished}</if>
            <if test="projectSource != null  and projectSource != ''"> and p.project_source = #{projectSource}</if>
            <if test="partyAId != null  and partyAId != ''"> and p.party_a_id = #{partyAId}</if>
            <if test="contactPerson != null  and contactPerson != ''"> and p.contact_person = #{contactPerson}</if>
            <if test="telephone != null  and telephone != ''"> and p.telephone = #{telephone}</if>
            <if test="projectType != null  and projectType != ''"> and p.project_type = #{projectType}</if>
            <if test="projectLevel != null  and projectLevel != ''"> and p.project_level = #{projectLevel}</if>
            <if test="projectRegistrant != null "> and p.project_registrant = #{projectRegistrant}</if>
            <if test="undertakingDept != null  and undertakingDept != ''"> and p.undertaking_dept like concat('%', #{undertakingDept}, '%')</if>
            <if test="projectPlanner != null "> and p.project_planner = #{projectPlanner}</if>
            <if test="siteLeader != null "> and p.site_leader = #{siteLeader}</if>
            <if test="planTime != null "> and p.plan_time = #{planTime}</if>
            <if test="entryTime != null "> and p.entry_time = #{entryTime}</if>
            <if test="exitTime != null "> and p.exit_time = #{exitTime}</if>
        </where>
        order by p.register_time desc
    </select>

    <select id="selectCmcProjectByProjectId" parameterType="String" resultMap="CmcProjectResult">
        <include refid="selectCmcProjectVo"/>
        where p.project_id = #{projectId}
    </select>

    <select id="selectCmcProjectByProjectNumber" parameterType="String" resultMap="CmcProjectResult">
        <include refid="selectCmcProjectVo"/>
        where p.project_number = #{projectNumber}
    </select>

    <insert id="insertCmcProject" parameterType="CmcProject">
        insert into cmc_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="projectNumber != null">project_number,</if>
            <if test="projectName != null">project_name,</if>
            <if test="projectLeader != null">project_leader,</if>
            <if test="isFinished != null">is_finished,</if>
            <if test="projectSource != null">project_source,</if>
            <if test="partyAId != null">party_a_id,</if>
            <if test="contactPerson != null">contact_person,</if>
            <if test="telephone != null">telephone,</if>
            <if test="projectType != null">project_type,</if>
            <if test="projectLevel != null">project_level,</if>
            <if test="projectRegistrant != null">project_registrant,</if>
            <if test="undertakingDept != null">undertaking_dept,</if>
            <if test="projectPlanner != null">project_planner,</if>
            <if test="siteLeader != null">site_leader,</if>
            <if test="organizeDocument != null">organize_document,</if>
            <if test="organizeTime != null">organize_time,</if>
            <if test="registerTime != null">register_time,</if>
            <if test="planTime != null">plan_time,</if>
            <if test="entryTime != null">entry_time,</if>
            <if test="exitTime != null">exit_time,</if>
            <if test="remark != null">remark,</if>
            <if test="projectKml != null">project_kml,</if>
            <if test="projectCp != null">project_cp,</if>
            <if test="kmlLng != null">kml_lng,</if>
            <if test="kmlLat != null">kml_lat,</if>
            <if test="cars != null">cars,</if>
            <if test="drivers != null">drivers,</if>
            <if test="devices != null">devices,</if>
            <if test="participates != null">participates,</if>
            <if test="taskDocument != null">task_document,</if>
            <if test="projectRequest != null">project_request,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="projectNumber != null">#{projectNumber},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="projectLeader != null">#{projectLeader},</if>
            <if test="isFinished != null">#{isFinished},</if>
            <if test="projectSource != null">#{projectSource},</if>
            <if test="partyAId != null">#{partyAId},</if>
            <if test="contactPerson != null">#{contactPerson},</if>
            <if test="telephone != null">#{telephone},</if>
            <if test="projectType != null">#{projectType},</if>
            <if test="projectLevel != null">#{projectLevel},</if>
            <if test="projectRegistrant != null">#{projectRegistrant},</if>
            <if test="undertakingDept != null">#{undertakingDept},</if>
            <if test="projectPlanner != null">#{projectPlanner},</if>
            <if test="siteLeader != null">#{siteLeader},</if>
            <if test="organizeDocument != null">#{organizeDocument},</if>
            <if test="organizeTime != null">#{organizeTime},</if>
            <if test="registerTime != null">#{registerTime},</if>
            <if test="planTime != null">#{planTime},</if>
            <if test="entryTime != null">#{entryTime},</if>
            <if test="exitTime != null">#{exitTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="projectKml != null">#{projectKml},</if>
            <if test="projectCp != null">#{projectCp},</if>
            <if test="kmlLng != null">#{kmlLng},</if>
            <if test="kmlLat != null">#{kmlLat},</if>
            <if test="cars != null">#{cars},</if>
            <if test="drivers != null">#{drivers},</if>
            <if test="devices != null">#{devices},</if>
            <if test="participates != null">#{participates},</if>
            <if test="taskDocument != null">#{taskDocument},</if>
            <if test="projectRequest != null">#{projectRequest},</if>
        </trim>
    </insert>

    <update id="updateCmcProject" parameterType="CmcProject">
        update cmc_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectNumber != null">project_number = #{projectNumber},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="projectLeader != null">project_leader = #{projectLeader},</if>
            <if test="isFinished != null">is_finished = #{isFinished},</if>
            <if test="projectSource != null">project_source = #{projectSource},</if>
            <if test="partyAId != null">party_a_id = #{partyAId},</if>
            <if test="contactPerson != null">contact_person = #{contactPerson},</if>
            <if test="telephone != null">telephone = #{telephone},</if>
            <if test="projectType != null">project_type = #{projectType},</if>
            <if test="projectLevel != null">project_level = #{projectLevel},</if>
            <if test="projectRegistrant != null">project_registrant = #{projectRegistrant},</if>
            <if test="undertakingDept != null">undertaking_dept = #{undertakingDept},</if>
            <if test="projectPlanner != null">project_planner = #{projectPlanner},</if>
            <if test="siteLeader != null">site_leader = #{siteLeader},</if>
            <if test="organizeDocument != null">organize_document = #{organizeDocument},</if>
            <if test="organizeTime != null">organize_time = #{organizeTime},</if>
            <if test="registerTime != null">register_time = #{registerTime},</if>
            <if test="planTime != null">plan_time = #{planTime},</if>
            <if test="entryTime != null">entry_time = #{entryTime},</if>
            <if test="exitTime != null">exit_time = #{exitTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="projectKml != null">project_kml = #{projectKml},</if>
            <if test="projectCp != null">project_cp = #{projectCp},</if>
            <if test="kmlLng != null">kml_lng = #{kmlLng},</if>
            <if test="kmlLat != null">kml_lat = #{kmlLat},</if>
            <if test="cars != null">cars = #{cars},</if>
            <if test="drivers != null">drivers = #{drivers},</if>
            <if test="devices != null">devices = #{devices},</if>
            <if test="participates != null">participates = #{participates},</if>
            <if test="taskDocument != null">task_document = #{taskDocument},</if>
            <if test="projectRequest != null">project_request = #{projectRequest},</if>
        </trim>
        where project_id = #{projectId}
    </update>

    <delete id="deleteCmcProjectByProjectId" parameterType="String">
        delete from cmc_project where project_id = #{projectId}
    </delete>

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