<?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.jeeplus.modules.sg.managementcenter.activiti.mapper.ConstructionMapper">

    <sql id="constructionColumns">
        a.id AS "id",
		a.proc_ins_id AS "procInsId",
		a.reserveProjectName AS "reserveProjectName",
        a.ifSelf as "ifSelf",
        a.ifBorrow as "ifBorrow",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
    </sql>

    <sql id="basicColumns">
        b.reserveProjectName as `basicInformation.reserveProjectName`,
		b.municipalCompany AS `basicInformation.municipalCompany`,
		b.countyCompany AS `basicInformation.countyCompany`,
		b.projectNature AS `basicInformation.projectNature`,
		b.demandUnit AS `basicInformation.demandUnit`,
		b.operationUnit AS `basicInformation.operationUnit`,
		b.powerSupplyArea AS `basicInformation.powerSupplyArea`,
		b.gridNumber AS `basicInformation.gridNumber`,
		b.cellNumber AS `basicInformation.cellNumber`,
		b.projectReserveNo AS `basicInformation.projectReserveNo`,
		b.gridProjectNo AS `basicInformation.gridProjectNo`,
		b.erpNo AS `basicInformation.erpNo`,
		b.necessityContent AS `basicInformation.necessityContent`,
		b.subStationName AS `basicInformation.subStationName`,
		b.lineName AS `basicInformation.lineName`,
		b.projectType AS `basicInformation.projectType`,
		b.whetherSummer AS `basicInformation.whetherSummer`,
		b.basicDescription AS `basicInformation.basicDescription`,
		b.workAreaRemarks AS `basicInformation.workAreaRemarks`
    </sql>

    <sql id="projectColumns">
        c.id as `id`,
		c.reserveProjectName,
		c.projectId,
		c.projectName,
        c.status,
        c.acquisitionStatus,
        c.constructionStatus,
        c.whetherCloseStatus,
        c.settStatus,
        c.earlyMaterial,
        c.preConstruction
    </sql>

    <sql id="discloseColumns">
        d.hasImplemented as `disclose.hasImplemented`,
		d.sceneDescription as `disclose.sceneDescription`,
		d.isImplement as `disclose.isImplement`,
		d.noImplementation as `disclose.noImplementation`,
		d.whetherComplete as `disclose.whetherComplete`,
		d.drawingDescription as `disclose.drawingDescription`,
		d.whetherStandard as `disclose.whetherStandard`,
		d.normativeDrawings as `disclose.normativeDrawings`,
		d.whetherDrawing as `disclose.whetherDrawing`,
		d.problemDescription as `disclose.problemDescription`,
		d.whetherChannel as `disclose.whetherChannel`,
		d.constructionDescription as `disclose.constructionDescription`,
		d.civilAdjusted as `disclose.civilAdjusted`,
		d.descriptionPlans as `disclose.descriptionPlans`,
		d.descriptionAdjustment as `disclose.descriptionAdjustment`,
		d.electricalAdjusted as `disclose.electricalAdjusted`,
		d.adjustmentDescription as `disclose.adjustmentDescription`,
		d.doesChange as `disclose.doesChange`,
		d.automatedContent as `disclose.automatedContent`,
		d.automatedAdjustment as `disclose.automatedAdjustment`,
		d.otherAdjusted as `disclose.otherAdjusted`,
		d.descriptionReasons as `disclose.descriptionReasons`,
        d.difficultResolve  as `disclose.difficultResolve` ,
        d.designChanges  as `disclose.designChanges` ,
        d.needsClosed  as `disclose.needsClosed` ,
        d.affairsDescription  as `disclose.affairsDescription` ,
        d.planningRequired  as `disclose.planningRequired` ,
        d.highSpeed  as `disclose.highSpeed` ,
        d.affairsOffice  as `disclose.affairsOffice` ,
        d.preconditions  as `disclose.preconditions` ,
        d.preconditionDescription  as `disclose.preconditionDescription` ,
        d.customizedMaterials  as `disclose.customizedMaterials` ,
        d.customizedCan  as `disclose.customizedCan` ,
        d.reasonDescription  as `disclose.reasonDescription` ,
        d.materialDescription  as `disclose.materialDescription` ,
        d.demolitionMaterials  as `disclose.demolitionMaterials` ,
        d.modelDescription  as `disclose.modelDescription` ,
        d.quantityDescription  as `disclose.quantityDescription` ,
        d.drawingAnnotation  as `disclose.drawingAnnotation` ,
        d.drawingProblem  as `disclose.drawingProblem` ,
        d.powerOutage  as `disclose.powerOutage` ,
        d.powerProblem  as `disclose.powerProblem` ,
        d.nearLiveLine  as `disclose.nearLiveLine` ,
        d.highVoltageLine  as `disclose.highVoltageLine` ,
        d.complexPipeline  as `disclose.complexPipeline` ,
        d.complicatedPower  as `disclose.complicatedPower`
    </sql>

    <sql id="assignmentColumns">
        g.reserveProjectName as `assignment.reserveProjectName`,
            g.disclosureType as `assignment.disclosureType`,
            g.projectReleaseBatch as `assignment.projectReleaseBatch`,
            g.deliveryDate as `assignment.deliveryDate`,
            g.packageNo as `assignment.packageNo`,
            g.rate as `assignment.rate`,
            g.assignedUnit as `assignment.assignedUnit`,
            g.constructionUnit as `assignment.constructionUnit`,
            g.constructionSubcontractor as `assignment.constructionSubcontractor`,
            g.behalf as `assignment.behalf`,
            g.constructionFullTime as `assignment.constructionFullTime`,
            g.constructionTechnical as `assignment.constructionTechnical`,
            g.constructionManage as `assignment.constructionManage`,
            g.constructionLeader as `assignment.constructionLeader`,
            g.subcontractingContact as `assignment.subcontractingContact`,
            g.designer as `assignment.designer`,
            g.designLeader as `assignment.designLeader`,
            g.ownerManager as `assignment.ownerManager`,
            g.distribution as `assignment.distribution`,
            g.distributionFullTime as `assignment.distributionFullTime`,
            g.distributionSystem as `assignment.distributionSystem`,
            g.distributionPolicy as `assignment.distributionPolicy`,
            g.centerLeader as `assignment.centerLeader`,
            g.centerTechnical as `assignment.centerTechnical`,
            g.instituteRole as `assignment.instituteRole`,
            g.repUnit as `assignment.repUnit`,
            g.inspectionUnit as `assignment.inspectionUnit`
    </sql>
    <sql id="quantityColumns">
            q.id as `quantity.id`,
            q.reserveProjectName as `quantity.reserveProjectName`,
            q.includeAutomation as `quantity.includeAutomation`,
            q.wayOf as `quantity.wayOf`,
            q.assignmentStyle as `quantity.assignmentStyle`,
            q.operationSubstation as `quantity.operationSubstation`,
            q.namingNumber as `quantity.namingNumber`,
            q.engineeringAcceptance as `quantity.engineeringAcceptance`,
            q.electricalAcceptance as `quantity.electricalAcceptance`,
            q.blackoutLine as `quantity.blackoutLine`,
            q.outageRange as `quantity.outageRange`,
            q.policyIssues as `quantity.policyIssues`,
            q.processingType as `quantity.processingType`,
            q.processingDescription as `quantity.processingDescription`
    </sql>

    <sql id="constructionJoins">
        LEFT JOIN sys_user updateBy ON updateBy.id = a.update_by
    </sql>


    <resultMap type="Construction" id="ConstructionResult" autoMapping="true">
        <result column="reason" property="reason" typeHandler="com.jeeplus.core.mapper.ConvertBlobTypeHandler"/>
    </resultMap>

    <select id="get" resultMap="ConstructionResult">
        SELECT
        <include refid="constructionColumns"/>,
        <include refid="basicColumns"/>,
        c.projectId,
        c.projectName,
        <include refid="discloseColumns"/>,
       <include refid="quantityColumns"/>
        from xm_construction_clarificaiton a
        LEFT JOIN xm_project_basicinformation b
        on a.reserveProjectName = b.reserveProjectName
        LEFT JOIN xm_material_project c
        on a.reserveProjectName = c.reserveProjectName
        LEFT JOIN xm_disclose_conclusion d
        on a.proc_ins_id = d.proc_ins_id
        LEFT JOIN xm_project_quantity q
        on a.reserveProjectName = q.reserveProjectName
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultMap="ConstructionResult">
        SELECT
        <include refid="constructionColumns"/>,
        <include refid="discloseColumns"/>
        FROM xm_construction_clarificaiton a,xm_disclose_conclusion d
        <include refid="constructionJoins"/>
        <where>
            a.proc_ins_id=b.proc_ins_id and
            a.del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.update_date DESC
            </otherwise>
        </choose>
    </select>

    <select id="findAllList" resultMap="ConstructionResult">
        SELECT
        <include refid="constructionColumns"/>
        FROM xm_construction_clarificaiton a
        <include refid="constructionJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.update_date DESC
            </otherwise>
        </choose>
    </select>

    <insert id="insert">
        INSERT INTO xm_construction_clarificaiton(id,
                                                  proc_ins_id,
                                                  create_by,
                                                  create_date,
                                                  update_by,
                                                  update_date,
                                                  remarks,
                                                  del_flag)
        VALUES (#{id},
                #{procInsId},
                #{userName},
                #{projectId},
                #{projectName},
                #{acceptDate},
                #{examineDate},
                #{applyName},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{remarks},
                #{delFlag})
    </insert>

    <update id="update">
        UPDATE xm_construction_clarificaiton
        SET proc_ins_id = #{procInsId},

            update_by   = #{updateBy.id},
            update_date = #{updateDate},
            remarks     = #{remarks}
        WHERE id = #{id}
    </update>

    <update id="newUpdate">
        UPDATE xm_construction_clarificaiton
        SET reserveProjectName='1'
        WHERE id = #{id}
    </update>

    <!--物理删除-->
    <update id="delete">
        DELETE
        FROM xm_construction_clarificaiton
        WHERE id = #{id}
    </update>

    <!--逻辑删除-->
    <update id="deleteByLogic">
        UPDATE xm_construction_clarificaiton
        SET del_flag = #{DEL_FLAG_DELETE}
        WHERE id = #{id}
    </update>


    <!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
    <select id="findUniqueByProperty" resultType="OALeave" statementType="STATEMENT">
        select *
        FROM xm_construction_clarificaiton
        where ${propertyName} = '${value}'
    </select>

    <select id="queryPerson" resultType="com.jeeplus.modules.sys.entity.User">
        SELECT *
        FROM sys_user
        where name = #{loginName}
    </select>

    <select id="nameFindOffice" resultType="string">
        select name
        from sys_role
        where id in (select role_id
                     from sys_user_role
                     where user_id = (SELECT id from sys_user where login_name = #{loginName}))
    </select>

    <insert id="newSave">
        INSERT INTO xm_construction_clarificaiton(id,
                                                  proc_ins_id,
                                                  create_by,
                                                  create_date,
                                                  update_by,
                                                  update_date,
                                                  remarks,
                                                  del_flag,
                                                  reserveProjectName)
        VALUES (#{id},
                #{procInsId},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{remarks},
                #{delFlag},
                #{reserveProjectName})
    </insert>

    <update id="updateStatus">
        update xm_material_project
        set status = '1'
        where reserveProjectName = #{reserveProjectName}
    </update>
    <select id="demandUnit" resultType="string">
        select demand_unit
        from xm_project_details
        where require_name = (select project_name from xm_construction_clarificaiton where proc_ins_id = #{procInsId});
    </select>

    <insert id="replaceDisclose">
        replace into xm_disclose_conclusion
        (
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        remarks,
        del_flag,
        proc_ins_id,
        hasImplemented,
        sceneDescription,
        isImplement,
        noImplementation,
        whetherComplete,
        drawingDescription,
        whetherStandard,
        normativeDrawings,
        whetherDrawing,
        problemDescription,
        whetherChannel,
        constructionDescription,
        civilAdjusted,
        descriptionPlans,
        descriptionAdjustment,
        electricalAdjusted,
        adjustmentDescription,
        doesChange,
        automatedContent,
        automatedAdjustment,
        otherAdjusted,
        descriptionReasons,
        difficultResolve,
        designChanges,
        needsClosed,
        affairsDescription,
        planningRequired,
        highSpeed,
        affairsOffice,
        preconditions,
        preconditionDescription,
        customizedMaterials,
        customizedCan,
        reasonDescription,
        materialDescription,
        demolitionMaterials,
        modelDescription,
        quantityDescription,
        drawingAnnotation,
        drawingProblem,
        powerOutage,
        powerProblem,
        nearLiveLine,
        highVoltageLine,
        complexPipeline,
        complicatedPower
        )values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.remarks},
            #{item.delFlag},
            #{item.procInsId},
            #{item.disclose.hasImplemented},
            #{item.disclose.sceneDescription},
            #{item.disclose.isImplement},
            #{item.disclose.noImplementation},
            #{item.disclose.whetherComplete},
            #{item.disclose.drawingDescription},
            #{item.disclose.whetherStandard},
            #{item.disclose.normativeDrawings},
            #{item.disclose.whetherDrawing},
            #{item.disclose.problemDescription},
            #{item.disclose.whetherChannel},
            #{item.disclose.constructionDescription},
            #{item.disclose.civilAdjusted},
            #{item.disclose.descriptionPlans},
            #{item.disclose.descriptionAdjustment},
            #{item.disclose.electricalAdjusted},
            #{item.disclose.adjustmentDescription},
            #{item.disclose.doesChange},
            #{item.disclose.automatedContent},
            #{item.disclose.automatedAdjustment},
            #{item.disclose.otherAdjusted},
            #{item.disclose.descriptionReasons},
            #{item.disclose.difficultResolve},
            #{item.disclose.designChanges},
            #{item.disclose.needsClosed},
            #{item.disclose.affairsDescription},
            #{item.disclose.planningRequired},
            #{item.disclose.highSpeed},
            #{item.disclose.affairsOffice},
            #{item.disclose.preconditions},
            #{item.disclose.preconditionDescription},
            #{item.disclose.customizedMaterials},
            #{item.disclose.customizedCan},
            #{item.disclose.reasonDescription},
            #{item.disclose.materialDescription},
            #{item.disclose.demolitionMaterials},
            #{item.disclose.modelDescription},
            #{item.disclose.quantityDescription},
            #{item.disclose.drawingAnnotation},
            #{item.disclose.drawingProblem},
            #{item.disclose.powerOutage},
            #{item.disclose.powerProblem},
            #{item.disclose.nearLiveLine},
            #{item.disclose.highVoltageLine},
            #{item.disclose.complexPipeline},
            #{item.disclose.complicatedPower}
            )
        </foreach>
    </insert>
    <insert id="uploadSave">
        INSERT INTO xm_upload_images(id,
                                     create_by,
                                     create_date,
                                     update_by,
                                     update_date,
                                     remarks,
                                     del_flag,
                                     imgName,
                                     path,
                                     proc_ins_id,
                                     uid,
                                     reserveProjectName)
        VALUES (#{id},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{remarks},
                #{delFlag},
                #{imgName},
                #{path},
                #{procInsId},
                #{uid},
                #{reserveProjectName})
    </insert>

    <select id="findByProcInsId" resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.Construction">
        SELECT
        <include refid="constructionColumns"/>,
        <include refid="assignmentColumns"/>,
        <include refid="discloseColumns"/>
        from xm_construction_clarificaiton a
        LEFT JOIN xm_project_assignment g
        on a.reserveProjectName = g.reserveProjectName
        LEFT JOIN xm_disclose_conclusion d
        on a.proc_ins_id = d.proc_ins_id
        where a.proc_ins_id in
        <foreach collection="list" open="(" close=")" index="item" item="item" separator=",">
            #{item.procInsId}
        </foreach>
    </select>

    <!--根据流程定义号查询项目需求名称-施工交底流程-->
    <select id="idProjectName" resultType="string">
        select reserveProjectName
        from xm_construction_clarificaiton
        where proc_ins_id = #{procInsId};
    </select>
    <!--施工验收流程记录表根据流程定义号获取项目名称-->
    <select id="idProjectName3" resultType="string">
        select reserveProjectName
        from sg_construction_process
        where proc_ins_id = #{procInsId};
    </select>
    <!--结算验收流程根据流程定义号获取项目名称-->
    <select id="idProjectName4" resultType="java.lang.String">
        select reserveProjectName
        from xm_settlement_process
        where proc_ins_id = #{procInsId};
    </select>
    <select id="findNameByInsID" resultType="com.jeeplus.modules.sg.managementcenter.project.entity.Project1">
        select id, reserveProjectName
        from xm_material_project
        where id = (select project_id from md_picking_application where proc_ins_id = #{id})
    </select>
    <select id="idProjectName2" resultType="string">
        select reserveProjectName
        from xm_material_project
        where reserveProjectName in (SELECT reserveProjectName from md_acquisition_process where proc_ins_id = #{procInsId})
    </select>
    <select id="insIdByName" resultType="java.lang.String">
        select proc_ins_id
        from xm_construction_clarificaiton
        where reserveProjectName like concat('%', #{projectName}, '%')
    </select>
    <select id="insIdByName2" resultType="java.lang.String">
        select proc_ins_id
        from md_acquisition_process
        where reserveProjectName in (select reserveProjectName from xm_project_details where require_name like concat('%', #{projectName}, '%'))
    </select>
    <select id="insIdByName3" resultType="java.lang.String">
        select proc_ins_id
        from sg_construction_process
        where reserveProjectName like concat('%', #{projectName}, '%');
    </select>
    <select id="insIdByName4" resultType="java.lang.String">
        select proc_ins_id
        from xm_settlement_process
        where reserveProjectName like concat('%', #{projectName}, '%')
    </select>

    <select id="findDisclosePage" resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.Construction">
        SELECT
        <include refid="constructionColumns"/>,
        c.projectId,
        c.projectName,
        <include refid="discloseColumns"/>
        from xm_construction_clarificaiton a
        LEFT JOIN xm_material_project c
        on a.reserveProjectName = c.reserveProjectName
        LEFT JOIN xm_disclose_conclusion d
        on a.proc_ins_id = d.proc_ins_id
        <where>
            1=1
            and c.secondPower = '0'
            <!--<if test="projectId != null and projectId != ''">-->
                <!--and c.projectId = #{projectId}-->
            <!--</if>-->
            <!--<if test="reserveProjectName != null and reserveProjectName != ''">-->
                <!--and c.reserveProjectName = #{reserveProjectName}-->
            <!--</if>-->
            <if test="reserveList != null and  reserveList !='' ">
                and c.reserveProjectName in
                <foreach collection="reserveList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="projectId != null and projectId !=''">
                and c.projectId like concat('%', #{projectId}, '%')
            </if>
            <if test="projectName !=null and projectName !=''">
                and c.projectName like concat('%', #{projectName}, '%')
            </if>
        </where>
    </select>

    <select id="roleListByName" resultType="com.jeeplus.modules.sys.entity.Role">
        select *
        from sys_role
        where id in
              (select role_id
               from sys_user_role
               where user_id = (
                   select id
                   from sys_user
                   where name = #{name}
               )
              )
    </select>
    <select id="findUploadPage"
            resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.UploadImages">
        select *
        from xm_upload_images
        <where>
            <if test="dataScope != null and dataScope != ''">
                ${dataScope}
            </if>
            <if test="reserveProjectName != null and reserveProjectName != ''">
                and reserveProjectName = #{reserveProjectName}
            </if>
            <if test="uid != null and uid != ''">
                AND uid = #{uid}
            </if>
            <if test="imgName!=null and imgName!=''">
                AND imgName = #{imgName}
            </if>
            <if test="procInsId!=null and procInsId!=''">
                and proc_ins_id = #{procInsId}
            </if>
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY update_date DESC
            </otherwise>
        </choose>
    </select>
    <update id="updateIfSelf">
        update xm_construction_clarificaiton
        set ifSelf = #{ifSelf}
        where proc_ins_id = #{procInsId};
    </update>
    <update id="updateIfBorrow">
        update xm_construction_clarificaiton
        set ifBorrow = #{ifBorrow}
        where proc_ins_id = #{procInsId};
    </update>
    <update id="updateOIPI">
        update xm_disclose_conclusion
        set create_date = now()
        where proc_ins_id = #{procInsId};
    </update>
    <update id="updateCloseStatus">
        update xm_material_project
        set whetherCloseStatus = '1'
        where reserveProjectName =
              (select reserveProjectName from xm_construction_clarificaiton where proc_ins_id = #{procInsId});
    </update>
    <update id="updateAdvStatus">
        update xm_material_project
        set advanceStatus = '1'
        where id = #{id};
    </update>
    <update id="updateEndStatus">
        update xm_material_project
        set status = '2'
        where reserveProjectName = #{reserveProjectName}
    </update>
    <insert id="quantityInsert">
        replace into xm_project_quantity
        (
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        remarks,
        del_flag,
        reserveProjectName,
        includeAutomation,
        wayOf,
        assignmentStyle,
        operationSubstation,
        namingNumber,
        engineeringAcceptance,
        electricalAcceptance,
        blackoutLine,
        outageRange,
        policyIssues,
        processingType,
        processingDescription
        )values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.remarks},
            #{item.delFlag},
            #{item.quantity.reserveProjectName},
            #{item.quantity.includeAutomation},
            #{item.quantity.wayOf},
            #{item.quantity.assignmentStyle},
            #{item.quantity.operationSubstation},
            #{item.quantity.namingNumber},
            #{item.quantity.engineeringAcceptance},
            #{item.quantity.electricalAcceptance},
            #{item.quantity.blackoutLine},
            #{item.quantity.outageRange},
            #{item.quantity.policyIssues},
            #{item.quantity.processingType},
            #{item.quantity.processingDescription}
            )
        </foreach>
    </insert>
    <insert id="EngAmountSave">
        insert into
            xm_engineering_amount (
                id,
                create_by,
                create_date,
                update_by,
                update_date,
                del_flag,
                remarks,
                reserveProjectName,
                civilWorks,
                civilMaterials,
                civilUnit
            )
        values (
                #{id},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{delFlag},
                #{remarks},
                #{reserveProjectName},
                #{civilWorks},
                #{civilMaterials},
                #{civilUnit}
             )ON DUPLICATE KEY UPDATE
                update_by = #{updateBy.id},
                update_date = #{updateDate},
                reserveProjectName = #{reserveProjectName},
                civilWorks = #{civilWorks},
                civilMaterials = #{civilMaterials},
                civilUnit = #{civilUnit}
    </insert>
    <insert id="addOther">
        insert into
            xm_other_amount (
            id,
            create_by,
            create_date,
            update_by,
            update_date,
            del_flag,
            remarks,
            reserveProjectName,
            otherWorks,
            otherMaterials,
            otherUnit
        )
        values (
               #{id},
               #{createBy.id},
               #{createDate},
               #{updateBy.id},
               #{updateDate},
               #{delFlag},
               #{remarks},
               #{reserveProjectName},
               #{otherWorks},
               #{otherMaterials},
               #{otherUnit}
           )ON DUPLICATE KEY UPDATE
             update_by = #{updateBy.id},
             update_date = #{updateDate},
             reserveProjectName = #{reserveProjectName},
             otherWorks = #{otherWorks},
             otherMaterials = #{otherMaterials},
             otherUnit = #{otherUnit}
    </insert>
    <select id="engineeringTable"
            resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.EngineeringAmount">
        select id,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        remarks,
        reserveProjectName,
        civilWorks,
        civilmaterials,
        civilUnit
        from xm_engineering_amount
        <where>
            1=1
            <if test="reserveProjectName !=null and reserveProjectName !='' ">
                and reserveProjectName = #{reserveProjectName}
            </if>
        </where>
    </select>
    <select id="otherTable" resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.OtherAmount">
        select id,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        remarks,
        reserveProjectName,
        otherWorks,
        otherMaterials,
        otherUnit
        from xm_other_amount
        <where>
            1=1
            <if test="reserveProjectName !=null and reserveProjectName !='' ">
                and reserveProjectName = #{reserveProjectName}
            </if>
        </where>
    </select>
    <select id="getEngAmount"
            resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.EngineeringAmount">
        select id,
               create_by,
               create_date,
               update_by,
               update_date,
               del_flag,
               remarks,
               reserveProjectName,
               civilWorks,
               civilMaterials,
               civilUnit
        from xm_engineering_amount
        where id = #{id};
    </select>
    <select id="getOtherAmount"
            resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.OtherAmount">
        select id,
               create_by,
               create_date,
               update_by,
               update_date,
               del_flag,
               remarks,
               reserveProjectName,
               otherWorks,
               otherMaterials,
               otherUnit
        from xm_other_amount
        where id = #{id};
    </select>
    <select id="procInsIdAllName" resultType="com.jeeplus.modules.sg.managementcenter.project.entity.Project">
        SELECT
            xmp.id,
            xmp.reserveProjectName,
            xmp.projectId,
            xmp.projectName
        FROM
            xm_material_project xmp
                LEFT JOIN xm_construction_clarificaiton xcc ON xmp.reserveProjectName = xcc.reserveProjectName
                LEFT JOIN sg_construction_process scp ON xmp.reserveProjectName = scp.reserveProjectName
                LEFT JOIN xm_nonpower_process xnp ON xmp.reserveProjectName = xnp.reserveProjectName
                LEFT JOIN md_acquisition_process map ON xmp.reserveProjectName = map.reserveProjectName
                LEFT JOIN xm_fund_process xfp ON xmp.reserveProjectName = xfp.reserveProjectName
                LEFT JOIN md_material_report_application mmra ON xmp.reserveProjectName = mmra.project_name
                LEFT JOIN xm_settlement_process xsp ON xmp.reserveProjectName = xsp.reserveProjectName
                LEFT JOIN md_picking_application mpa ON xmp.projectId = mpa.project_id
        WHERE
            1=1
          <if test="procInsId !=null and procInsId !=''">
           and  xcc.proc_ins_id = #{procInsId}
           OR scp.proc_ins_id = #{procInsId}
           OR xnp.proc_ins_id = #{procInsId}
           OR map.proc_ins_id = #{procInsId}
           OR xfp.proc_ins_id = #{procInsId}
           OR mmra.proc_ins_id = #{procInsId}
           OR xsp.proc_ins_id = #{procInsId}
           OR mpa.proc_ins_id = #{procInsId}
          </if>
        group by reserveProjectName
    </select>
    <select id="findProcInsIDAll"
            resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.ReturnTodo">
        SELECT
            xcc.proc_ins_id as `xcProcInsId`,
            scp.proc_ins_id as `scProcInsId`,
            xnp.proc_ins_id as `xnProcInsId`,
            map.proc_ins_id as `maProcInsId`,
            xfp.proc_ins_id as `xfProcInsId`,
            mmra.proc_ins_id as `mmProcInsId`,
            xsp.proc_ins_id as `xsProcInsId`,
            mpa.proc_ins_id as `mpProcInsId`
        FROM
            xm_material_project xmp
                LEFT JOIN xm_construction_clarificaiton xcc ON xmp.reserveProjectName = xcc.reserveProjectName
                LEFT JOIN sg_construction_process scp ON xmp.reserveProjectName = scp.reserveProjectName
                LEFT JOIN xm_nonpower_process xnp ON xmp.reserveProjectName = xnp.reserveProjectName
                LEFT JOIN md_acquisition_process map ON xmp.reserveProjectName = map.reserveProjectName
                LEFT JOIN xm_fund_process xfp ON xmp.reserveProjectName = xfp.reserveProjectName
                LEFT JOIN md_material_report_application mmra ON xmp.reserveProjectName = mmra.project_name
                LEFT JOIN xm_settlement_process xsp ON xmp.reserveProjectName = xsp.reserveProjectName
                LEFT JOIN md_picking_application mpa ON xmp.projectId = mpa.project_id
        <where>
            1=1
            <if test="reserveProjectName != null and reserveProjectName != ''">
                and xmp.reserveProjectName = #{reserveProjectName}
            </if>
            <if test="projectId != null and projectId != ''">
                and xmp.projectId = #{projectId}
            </if>
            <if test="projectName != null and projectName != ''">
                and xmp.projectName = #{projectName}
            </if>
        </where>
    </select>
    <delete id="delEngAmount">
        delete
        from xm_engineering_amount
        where id = #{id}
    </delete>
    <delete id="delOtherAmount">
        delete
        from xm_other_amount
        where id = #{id}
    </delete>
    <delete id="delProcessList">
        delete
        from md_process_list
        where procInsId = #{procInsId};
    </delete>
    <update id="updatePolicyStatus">
        update xm_material_project
        set policyStatus = #{policyStatus}
        where reserveProjectName = #{reserveProjectName};
    </update>
</mapper>