<?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
        <include refid="materialColumns"/>
        FROM sg_construction_process a
        WHERE a.proc_ins_id = #{procInsId}
    </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,
			proc_ins_id,
			user_name,
			project_id,
			project_name,
			project_type,
			frequency,
			nonPower,
			electrical,
			power
		)values
			(
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{remarks},
			#{procInsId},
			#{userName},
			#{projectId},
			#{projectName},
			#{projectType},
			#{frequency},
			#{nonPower},
			#{electrical},
			#{power}
			)
	</insert>

    <update id="updateConstructionStatus">
		update xm_project_details set construction_status = '1' where id=#{projectId}
	</update>

    <select id="findProjectName" resultType="string">
		select project_name 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>

    <select id="findMaterialProject" resultType="com.jeeplus.modules.sg.materialmodule.activiti.entity.MaterialModule">
        select id,proc_ins_id, user_name, project_id, project_name, project_type from sg_construction_process where
        project_name
        (
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item.projectName}
        </foreach>
        )
    </select>

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

    <select id="getModuleById" resultType="com.jeeplus.modules.sg.materialmodule.activiti.entity.MaterialModule">
		select project_name,proc_ins_id,now_project_name,frequency from sg_construction_process where proc_ins_id = #{procInsId};
	</select>

    <update id="updateModuleById">
		update sg_construction_process set frequency = #{frequency},now_project_name=#{nowProjectName} where proc_ins_id = #{procInsId};
	</update>

    <update id="updateNonPower">
		update sg_construction_process set nonPower = 0 where proc_ins_id  = #{procInsId}
	</update>

    <update id="updateElectrical">
		update sg_construction_process set electrical = 0 where proc_ins_id  = #{procInsId}
	</update>

    <update id="updatePower">
		update sg_construction_process set power = 0 where proc_ins_id  = #{procInsId}
	</update>
	
	<select id="findByProcInsId" resultType="com.jeeplus.modules.sg.materialmodule.activiti.entity.MaterialModule">
		select project_name,proc_ins_id,nonPower,electrical,power from sg_construction_process where proc_ins_id  = #{procInsId};
	</select>
</mapper>