<?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.user_name AS "userName",
		a.project_id AS "projectId",
		a.project_name AS "projectName",
		a.accept_date AS "acceptDate",
		a.examine_date AS "examineDate",
		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.applyName
	</sql>

    <sql id="disclose">
		d.conId AS "disclose.conId",
		d.channel AS "disclose.channel",
		d.capacity AS "disclose.capacity",
		d.sceneSize AS "disclose.sceneSize",
		d.capacitySize AS "disclose.capacitySize",
		d.hiddenDanger AS "disclose.hiddenDanger",
		d.influenceConstruction AS "disclose.influenceConstruction",
		d.projectRepetition AS "disclose.projectRepetition",
		d.implemented AS "disclose.implemented",
		d.transformer AS "disclose.transformer",
		d.lineName AS "disclose.lineName",
		d.quantities AS "disclose.quantities",
		d.rodNumber AS "disclose.rodNumber",
		d.administrationType AS "disclose.administrationType",
		d.precondition AS "disclose.precondition",
		d.placementMode AS "disclose.placementMode",
		d.productionDate AS "disclose.productionDate",
		d.IntegrateModule AS "disclose.IntegrateModule",
		d.needReplace AS "disclose.needReplace",
		d.overallTransformation AS "disclose.overallTransformation",
		d.explanationOfNecessity AS "disclose.explanationOfNecessity",
		d.inconsistentDescription AS "disclose.inconsistentDescription",
		d.policyHandlingInstructions AS "disclose.policyHandlingInstructions",
		d.otherProblem AS "disclose.otherProblem"
	</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"/>,
        b.demand_unit AS "demandUnit",
		b.power_line AS "powerLine",
		b.power_scope AS "blackoutArea",
		b.project_type AS "projectType",
		b.power_flag_package AS "powerFailure",
		b.preliminary_judgment AS "preliminaryJudgment",
		b.flag_civil_engineering AS "civilEngineering",
		b.steel_tube_pole AS "baseType",
		b.flag_approval_planning AS "planApproval",
		b.equ_create_type AS "transformationType",
		b.equ_create_size AS "transformationEquipment",
		b.flag_new_equ_name AS "newNamed",
		b.include_bdz_news AS "putInto",
		b.flag_no_electrical AS "electricalConstruction",
		b.drawings_work AS "theAssignment",
		b.flag_automation AS "automation",
		b.two_and_three_remote AS "remote",
		b.phone_style AS "communicationMode",
		b.stay_open_area AS "inSitu",
		b.ring_network_cabinet AS "networkCabinet",
		b.station_dtu AS "station",
		b.status As "status",
		<include refid="disclose"/>
		FROM xm_construction_clarificaiton a
		LEFT JOIN xm_project_details b on a.project_id=b.id
		LEFT JOIN xm_disclose_conclusion d on a.proc_ins_id = d.proc_ins_id
		WHERE a.id = #{id}
    </select>

    <select id="findList" resultMap="ConstructionResult">
        SELECT
        <include refid="constructionColumns"/>,
        <include refid="disclose"/>
        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,
			user_name,
			project_id,
			project_name,
			accept_date,
			examine_date,
			applyName,
			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},
			user_name = #{userName},
			project_id = #{projectId},
			project_name = #{projectName},
			applyName = #{applyName},
			accept_date = #{acceptDate},
			examine_date = #{examineDate},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>

    <update id="newUpdate">
		UPDATE xm_construction_clarificaiton SET
			powerLine = #{powerLine},
			blackoutArea = #{blackoutArea},
			projectType = #{projectType},
			powerFailure = #{powerFailure},
			preliminaryJudgment = #{preliminaryJudgment},
			civilEngineering = #{civilEngineering},
			baseType = #{baseType},
			planApproval = #{planApproval},
			transformationType = #{transformationType},
			transformationEquipment = #{transformationEquipment},
			newNamed = #{newNamed},
			putInto = #{putInto},
			electricalConstruction = #{electricalConstruction},
			theAssignment = #{theAssignment},
			automation = #{automation},
			remote = #{remote},
			communicationMode = #{communicationMode},
			inSitu = #{inSitu},
			networkCabinet = #{networkCabinet},
			station = #{station}
		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 =(select role_id from sys_user_role where user_id = (SELECT id from sys_user where name = #{loginName}))
	</select>

	<insert id="newSave">
		INSERT INTO xm_construction_clarificaiton(
			id,
			proc_ins_id,
			user_name,
			project_id,
			project_name,
			accept_date,
			examine_date,
			applyName,
			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="updateExamine">
		UPDATE xm_construction_clarificaiton SET
			examine_date = #{examineDate}
		WHERE id = #{id}
	</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>
	<!--根据流程定义号查询项目需求名称-->
	<select id="idProjectName" resultType="string">
		select project_name from xm_construction_clarificaiton where proc_ins_id = #{procInsId};
	</select>
	<select id="idProjectName3" resultType="string">
		select project_name from sg_construction_process where proc_ins_id = #{procInsId};
	</select>
	<select id="findNameByInsID" resultType="com.jeeplus.modules.sg.managementcenter.project.entity.Project">
		select id,require_name from xm_project_details where id =  (select project_id from md_picking_application where proc_ins_id  = #{id})
	</select>
	<select id="idProjectName2" resultType="string">
		select require_name from xm_project_details where id  in(SELECT project_id 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 project_name like concat('%',#{projectName},'%')
	</select>
	<select id="insIdByName2" resultType="java.lang.String">
		select proc_ins_id from md_acquisition_process where project_id  in(select id 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 project_name like concat('%',#{projectName},'%')
	</select>
	<insert id="insertDisclose">
		replace into xm_disclose_conclusion
		(
		id,
		create_by,
		create_date,
		update_by,
		update_date,
		remarks,
		del_flag,
		proc_ins_id,
		projectId,
		channel,
		capacity,
		sceneSize,
		capacitySize,
		hiddenDanger,
		influenceConstruction,
		projectRepetition,
		implemented,
		transformer,
		lineName,
		quantities,
		rodNumber,
		administrationType,
		precondition,
		placementMode,
		productionDate,
		IntegrateModule,
		needReplace,
		overallTransformation,
		explanationOfNecessity,
		inconsistentDescription,
		policyHandlingInstructions,
		otherProblem
		) values (
		#{id},
		#{createBy.id},
		#{createDate},
		#{updateBy.id},
		#{updateDate},
		#{remarks},
		#{delFlag},
		#{procInsId},
		#{projectId},
		#{disclose.channel},
		#{disclose.capacity},
		#{disclose.sceneSize},
		#{disclose.capacitySize},
		#{disclose.hiddenDanger},
		#{disclose.influenceConstruction},
		#{disclose.projectRepetition},
		#{disclose.implemented},
		#{disclose.transformer},
		#{disclose.lineName},
		#{disclose.quantities},
		#{disclose.rodNumber},
		#{disclose.administrationType},
		#{disclose.precondition},
		#{disclose.placementMode},
		#{disclose.productionDate},
		#{disclose.IntegrateModule},
		#{disclose.needReplace},
		#{disclose.overallTransformation},
		#{disclose.explanationOfNecessity},
		#{disclose.inconsistentDescription},
		#{disclose.policyHandlingInstructions},
		#{disclose.otherProblem}
		);
	</insert>
</mapper>