<?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.materialmodule.activiti.mapper.MaterialModuleMapper">

    <sql id="materialColumns">
        a.id AS "id",
		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",
		a.proc_ins_id AS "procInsId",
		a.user_name AS "userName",
		a.project_id AS "projectId",
		a.project_name AS "projectName",
		a.project_type AS "projectType"
    </sql>

    <select id="get" resultType="MaterialModule">
        select a.id,
               a.projectId,
               a.projectName,
               a.reserveProjectName,
               a.severalTimes,
               b.municipalCompany   as buildingUnit,
               b.erpNo              as projectNumber,
               xpa.constructionUnit as constructionUnit,
               a.plannedStart,
               a.plannedEnd,
               a.projectManager,
               a.actualStart,
               a.actualEnd,
               a.projectManager,
               a.personInCharge,
               b.lineName,
               a.workPlace,
               a.operationMethod,
               a.newWorkload,
               a.demolitionWorkload,
               a.wayOfWorking,
               a.startConstruction,
               a.startLoad,
               a.startCrossover,
               a.completedWork,
               a.endConstruction,
               a.endLoad,
               a.endCrossover,
               xpq.engineeringAcceptance,
               a.create_date
        from sg_construction_process a
                 LEFT JOIN xm_material_project p
                            ON a.reserveProjectName = p.reserveProjectName
                 LEFT JOIN xm_project_basicinformation b
                           on p.reserveProjectName = b.reserveProjectName
                 left join xm_project_assignment xpa
                           on a.reserveProjectName = xpa.reserveProjectName
                 left join xm_project_quantity xpq
                           on a.reserveProjectName = xpq.reserveProjectName
        where a.id = #{id}
    </select>

    <select id="findList" resultType="MaterialModule">
        SELECT
        <include refid="materialColumns"/>
        FROM sg_construction_process a
        <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>

    <!--	修改提前施工状态0未提前施工，1提前施工
        <update id="updatePreConstruction">
            update xm_project_details set preConstruction = #{preConstruction} where id = #{id};
        </update>-->

    <insert id="insert">
        insert into sg_construction_process (id,
                                             create_by,
                                             create_date,
                                             update_by,
                                             update_date,
                                             del_flag,
                                             remarks,
                                             projectId,
                                             projectName,
                                             reserveProjectName)
        values (#{id},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{delFlag},
                #{remarks},
                #{projectId},
                #{projectName},
                #{reserveProjectName});
    </insert>

    <update id="updateConstructionStatus">
        update xm_material_project
        set constructionStatus = #{status}
        where reserveProjectName = #{reserveProjectName}
    </update>

    <select id="findProjectName" resultType="string">
        select projectName
        from sg_construction_process
        where proc_ins_id = #{procInsId};
    </select>

    <select id="findSpecificTime" resultType="java.util.Date">
        select specificTime
        from ms_construction_project
        where projectName = #{projectName};
    </select>

    <select id="findUserId" parameterType="java.util.List" resultType="java.lang.String">
        select id from sys_user
        where name in
        (
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
        )
    </select>

    <select id="findProject"
            resultType="com.jeeplus.modules.sg.managementcenter.constructionProject.entity.ConstructionProject">
        select id
             , create_by
             , create_date
             , update_by
             , update_date
             , del_flag
             , remarks
             , cityCompany
             , countyCompany
             , projectNature
             , powerSupplyArea
             , gridNumber
             , cellNumber
             , reserveNumber
             , projectName
             , projectNumber
             , substationName
             , lineName
             , projectType
             , instructions
             , workAreaNote
             , contructiveContent
             , costEstimate
             , whetherGraceProject
             , projectNote
             , constructionUnit
             , arrangeTime
             , specificTime
             , projectStatus
        from ms_construction_project
        where projectName = #{projectName}
    </select>

    <insert id="conProject">
        INSERT INTO ms_construction_project(id,
                                            create_by,
                                            create_date,
                                            update_by,
                                            update_date,
                                            remarks,
                                            del_flag,
                                            cityCompany,
                                            countyCompany,
                                            projectNature,
                                            powerSupplyArea,
                                            gridNumber,
                                            cellNumber,
                                            reserveNumber,
                                            projectName,
                                            projectNumber,
                                            substationName,
                                            lineName,
                                            projectType,
                                            instructions,
                                            workAreaNote,
                                            contructiveContent,
                                            costEstimate,
                                            whetherGraceProject,
                                            projectNote,
                                            constructionUnit,
                                            arrangeTime,
                                            specificTime,
                                            projectStatus,
                                            endFlag)
        VALUES (#{id},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{remarks},
                #{delFlag},
                #{cityCompany},
                #{countyCompany},
                #{projectNature},
                #{powerSupplyArea},
                #{gridNumber},
                #{cellNumber},
                #{reserveNumber},
                #{projectName},
                #{projectNumber},
                #{substationName},
                #{lineName},
                #{projectType},
                #{instructions},
                #{workAreaNote},
                #{contructiveContent},
                #{costEstimate},
                #{whetherGraceProject},
                #{projectNote},
                #{constructionUnit},
                #{arrangeTime},
                #{specificTime},
                #{projectStatus},
                0)
    </insert>

    <select id="findMonthProject"
            resultType="com.jeeplus.modules.sg.managementcenter.constructionProject.entity.ConstructionProject">
        select projectName, endFlag
        from ms_construction_project
        where DATE_FORMAT(arrangeTime, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')
    </select>


    <update id="updateEndFlag">
        update ms_construction_project
        set endFlag = 1
        where projectName = #{projectName};
    </update>

    <update id="updateElectricalStart">
        update sg_construction_process
        set plannedStart      = #{plannedStart},
            plannedEnd        = #{plannedEnd},
            projectManager    = #{projectManager},
            personInCharge    = #{personInCharge},
            workPlace         = #{workPlace},
            wayOfWorking      = #{wayOfWorking},
            startConstruction = #{startConstruction},
            startLoad         = #{startLoad},
            startCrossover    = #{startCrossover}
        where id = #{id}
    </update>

    <update id="updateElectricalEnd">
        update sg_construction_process
        set actualStart      = #{actualStart},
            actualEnd        = #{actualEnd},
            completedWork    = #{completedWork},
            endConstruction    = #{endConstruction},
            endLoad         = #{endLoad},
            endCrossover      = #{endCrossover}
        where id = #{id}
    </update>
    <update id="updateSeveralTimes">
        update sg_construction_process set severalTimes = #{number}
        where proc_ins_id = #{procInsId}
    </update>

    <select id="findPlanDay" resultType="java.lang.String">
        select projectPowerOut
        from xm_project_plan_daily
        where projectName = #{reserveProjectName}
    </select>
    <select id="findProcessList"
            resultType="com.jeeplus.modules.sg.managementcenter.activiti.entity.EngineeringAmount">
        select materialDescription as `civilMaterials` ,
               mdUnits as `civilUnit`
        from md_process_list
        where procInsId in
              (select proc_ins_id
               from md_acquisition_process
               where canEnter = '2'
                 and reserveProjectName = #{reserveProjectName});
    </select>
    <select id="findById" resultType="com.jeeplus.modules.sg.materialmodule.activiti.entity.MaterialModule">
        SELECT
            a.id,
            a.projectId,
            a.projectName,
            a.reserveProjectName,
            b.municipalCompany AS buildingUnit,
            b.erpNo AS projectNumber,
            xpa.constructionUnit AS constructionUnit,
            a.plannedStart,
            a.plannedEnd,
            a.actualStart,
            a.actualEnd,
            a.projectManager,
            b.lineName,
            a.personInCharge,
            a.workPlace,
            a.wayOfWorking,
            a.startConstruction,
            a.startLoad,
            a.startCrossover,
            a.completedWork,
            a.endConstruction,
            a.endLoad,
            a.endCrossover,
            xpq.engineeringAcceptance,
            a.create_date
        FROM
            sg_construction_process a
                LEFT JOIN xm_material_project p ON a.projectId = p.projectId
                LEFT JOIN xm_project_basicinformation b ON p.reserveProjectName = b.reserveProjectName
                LEFT JOIN xm_project_assignment xpa ON a.reserveProjectName = xpa.reserveProjectName
                LEFT JOIN xm_project_quantity xpq ON a.reserveProjectName = xpq.reserveProjectName
        WHERE
            a.id = #{id}
    </select>
    <select id="findListPage"
            resultType="com.jeeplus.modules.sg.materialmodule.activiti.entity.MaterialModule">
        select
        scp.id,
        scp.create_by,
        scp.create_date,
        scp.update_by,
        scp.update_date,
        scp.del_flag,
        scp.remarks,
        scp.proc_ins_id,
        scp.projectId,
        scp.plannedStart,
        scp.plannedEnd,
        scp.actualStart,
        scp.actualEnd,
        scp.projectManager,
        scp.personInCharge,
        scp.workPlace,
        scp.projectName,
        scp.reserveProjectName
        from sg_construction_process scp
        left join xm_project_assignment xpa
        on scp.reserveProjectName = xpa.reserveProjectName
        <where>
            1=1
            <if test="projectId!=null and projectId != ''">
                and projectId = #{projectId}
            </if>
            <if test="projectName!=null and projectName != ''">
                and projectName = #{projectName}
            </if>
            <if test="reserveProjectName!=null and reserveProjectName != ''">
                and reserveProjectName = #{reserveProjectName}
            </if>
            <if test="dataScope != null and dataScope != ''">
                ${dataScope}
            </if>
        </where>
    </select>
</mapper>