<?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.projectPlanArrage.mapper.ProjectPlanActMapper">
	<sql id="proclnsColumns">
		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.proc_ins_id AS "procInsId",
		a.custom_id AS "customId"
	</sql>

	<sql id="assColumns">
		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.proc_ins_id AS "procInsId",
		a.custom_id AS "customId",
		a.projectName AS "projectName",
		a.projectNumber AS "projectNumber",
		a.projectLine AS "projectLine",
		a.projectPowerRange AS "projectPowerRange",
		a.projectSwitch AS "projectSwitch",
		a.projectMonthDate AS "projectMonthDate",
		a.projectDate AS "projectDate",
		a.projectPowerOut AS "projectPowerOut",
		a.status AS "status",
		a.distributionNetwork AS "distributionNetwork",
		a.constructionUnit AS "constructionUnit",
		a.operationUnit AS "operationUnit",
		a.backToStauts AS "backToStauts",
		a.backToText AS "backToText",
		a.associatedBag AS "associatedBag",
		a.powerPackage AS "powerPackage",
		a.associatedSort AS "associatedSort",
		a.powerPackageSort AS "powerPackageSort",
		a.oanotityId AS "oanotityId"
	</sql>

	<!--<select id="findListAssList">-->
		<!--SELECT-->
		<!--<include refid="assColumns"/>-->
		<!--FROM xm_project_plan_ass a-->
		<!--where a.proc_ins_id in-->
		<!--<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">-->
			<!--#{item.procInsId}-->
		<!--</foreach>-->
	<!--</select>-->

	<select id="findListAss" resultType="com.jeeplus.modules.sg.managementcenter.projectPlanArrage.entity.ProjectActProclnsAss">
		SELECT
		<include refid="assColumns"/>
		FROM xm_project_plan_ass a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="projectName != null and projectName != ''">
				AND a.projectName = #{projectName}
			</if>
			<if test="projectNumber != null and projectNumber != ''">
				AND a.projectNumber = #{projectNumber}
			</if>
			<if test="procInsId != null and procInsId != ''">
				AND a.proc_ins_id = #{procInsId}
			</if>
			<if test="operationUnit!=null and operationUnit != ''">
				AND a.operationUnit = #{operationUnit}
			</if>
			<if test="oanotityId!=null and oanotityId != ''">
				AND a.oanotityId = #{oanotityId}
			</if>
		</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="processAct">
        INSERT INTO xm_project_arrange_procIns(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			proc_ins_id,
			custom_id
		) VALUES
		(
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{procInsId},
			#{customId}
		)
    </insert>


	<insert id="insertProcInsList">
		replace INTO xm_project_arrange_procIns(
		id,
		create_by,
		create_date,
		update_by,
		update_date,
		remarks,
		del_flag,
		proc_ins_id,
		custom_id
		) 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.customId}
			)
		</foreach>
	</insert>

	<insert id="insertProcInsAssList">
		replace INTO xm_project_plan_ass(
		id,
		create_by,
		create_date,
		update_by,
		update_date,
		remarks,
		del_flag,
		proc_ins_id,
		custom_id,
		projectName,
		projectNumber,
		projectLine,
		projectPowerRange,
		projectSwitch,
		projectMonthDate,
		projectDate,
		projectPowerOut,
		status,
		distributionNetwork,
		constructionUnit,
		operationUnit,
		backToStauts,
		backToText,
		associatedBag,
		powerPackage,
		associatedSort,
		powerPackageSort,
		oanotityId
		) 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.customId},
			#{item.projectName},
			#{item.projectNumber},
			#{item.projectLine},
			#{item.projectPowerRange},
			#{item.projectSwitch},
			#{item.projectMonthDate},
			#{item.projectDate},
			#{item.projectPowerOut},
			#{item.status},
			#{item.distributionNetwork},
			#{item.constructionUnit},
			#{item.operationUnit},
			#{item.backToStauts},
			#{item.backToText},
			#{item.associatedBag},
			#{item.powerPackage},
			#{item.associatedSort},
			#{item.powerPackageSort},
			#{item.oanotityId}
			)
		</foreach>
	</insert>

	<!-- 根据流程key获取最大版本号的id -->
	<select id="actModelId" resultType="java.lang.String">
		select ID_ FROM act_re_procdef where KEY_ = #{key} and  VERSION_ =
		(select MAX(VERSION_) FROM act_re_procdef where KEY_ = #{key})
	</select>

	<delete id="deleteConsAss">
		delete from xm_project_plan_ass
		where constructionUnit = #{constructionUnit}  and proc_ins_id = #{procInsId}
	</delete>

	<delete id="deleteNetAss">
		delete from xm_project_plan_ass
		where proc_ins_id = #{procInsId}
	</delete>

	<update id="updateMaterialProject">
		UPDATE xm_material_project SET
		arrageProject = '2'
		WHERE reserveProjectName in
		<foreach collection="list" open="(" close=")" item="item" separator=",">
			#{item}
		</foreach>
	</update>

	<update id="updateMaterialNetProject">
		UPDATE xm_material_project SET
		arrageProject = '1'
		WHERE reserveProjectName in
		<foreach collection="list" open="(" close=")" item="item" separator=",">
			#{item}
		</foreach>
	</update>

</mapper>