<?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.audit.information.mapper.InformationMapper">

    <sql id="informationColumns">
		id,
		project_id,
		project_name,
		project_type,
		project_grade,
		project_year,
		project_total,
		project_start,
		project_stop,
		department,
		project_firm,
		subDate,
		releaseDate,
		endDate,
		resultDate,
		approvalDate,
		second_units,
		first_units,
		firstFinish,
		firstFinishDate,
		secondFinish,
		secondFinishDate,
		finishDate,
		overdue,
		underCentralized
	</sql>

	<sql id="newInformationColumns">
		a.id,
		a.project_id,
		a.project_name,
		a.project_type,
		a.project_grade,
		a.project_year,
		a.project_total,
		a.project_start,
		a.project_stop,
		a.department,
		a.project_firm,
		a.subDate,
		a.releaseDate,
		a.endDate,
		a.resultDate,
		a.approvalDate,
		a.second_units,
		a.first_units,
		a.firstFinish,
		a.firstFinishDate,
		a.secondFinish,
		a.secondFinishDate,
		a.finishDate,
		a.overdue,
		a.underCentralized,
		a.suggestedAmount
	</sql>
    <select id="findAllInformationList" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
		select project_id,project_name from sj_project_information
	</select>

    <insert id="insert">
		INSERT INTO sj_project_information(
			id,
			project_id,
			project_name,
			project_type,
			project_grade,
			project_year,
			project_total,
			project_start,
			project_stop,
			department,
			project_firm,
			subDate,
			releaseDate,
			endDate,
			resultDate,
			approvalDate,
			second_units,
			first_units,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag
		) VALUES (
			 #{id},
			 #{projectId},
			 #{projectName},
			 #{projectType},
			 #{projectGrade},
			 #{projectYear},
			 #{projectTotal},
			 #{projectStart},
			 #{projectStop},
			 #{department},
			 #{projectFirm},
			 #{subDate},
			 #{releaseDate},
			 #{endDate},
			 #{resultDate},
			 #{approvalDate},
			 #{secondUnits},
			 #{firstUnits},
			 #{createBy.id},
			 #{createDate},
			 #{updateBy.id},
			 #{updateDate},
			 #{delFlag}
		)on duplicate key update
		id=           #{id},
		project_id=   #{projectId},
		project_name= #{projectName},
		project_type= #{projectType},
		project_grade=#{projectGrade},
		project_year= #{projectYear},
		project_total=#{projectTotal},
		project_start=#{projectStart},
		project_stop= #{projectStop},
		department=   #{department},
		project_firm= #{projectFirm},
		subDate=      #{subDate},
		releaseDate=  #{releaseDate},
		endDate=      #{endDate},
		resultDate=   #{resultDate},
		approvalDate= #{approvalDate},
		second_units= #{secondUnits},
		first_units=  #{firstUnits},
		create_by = #{createBy.id},
		create_date = #{createDate},
		update_by = #{updateBy.id},
		update_date = #{updateDate},
		del_flag = #{delFlag}
	</insert>



	<insert id="insertInfor">
	INSERT INTO sj_project_information(
		id,
		project_id,
		project_name,
		project_type,
		project_grade,
		project_year,
		project_total,
		project_start,
		project_stop,
		department,
		project_firm,
		subDate,
		releaseDate,
		endDate,
		resultDate,
		approvalDate,
		second_units,
		first_units,
		create_by,
		create_date,
		update_by,
		update_date,
		del_flag
	) VALUES (
		 #{id},
		 #{projectId},
		 #{projectName},
		 #{projectType},
		 #{projectGrade},
		 #{projectYear},
		 #{projectTotal},
		 #{projectStart},
		 #{projectStop},
		 #{department},
		 #{projectFirm},
		 #{subDate},
		 #{releaseDate},
		 #{endDate},
		 #{resultDate},
		 #{approvalDate},
		 #{secondUnits},
		 #{firstUnits},
		 #{createBy.id},
		 #{createDate},
		 #{updateBy.id},
		 #{updateDate},
		 #{delFlag}
	)on duplicate key update
		id=           #{id},
		project_id=   #{projectId},
		project_name= #{projectName},
		project_type= #{projectType},
		project_grade=#{projectGrade},
		project_year= #{projectYear},
		project_total=#{projectTotal},
		project_start=#{projectStart},
		project_stop= #{projectStop},
		department=   #{department},
		project_firm= #{projectFirm},
		subDate=      #{subDate},
		releaseDate=  #{releaseDate},
		endDate=      #{endDate},
		resultDate=   #{resultDate},
		approvalDate= #{approvalDate},
		second_units= #{secondUnits},
		first_units=  #{firstUnits},
		create_by = #{createBy.id},
		create_date = #{createDate},
		update_by = #{updateBy.id},
		update_date = #{updateDate},
		del_flag = #{delFlag}
	</insert>

    <update id="updateInformationDate" parameterType="com.jeeplus.modules.sg.audit.information.entity.Information">
		update sj_project_information set
		approvalDate = #{approvalDate}
		where project_Id = #{projectId} and project_Name = #{projectName}
	</update>
	
	<update id="updateDate">
		update sj_project_information set firstFinishDate = CURDATE(),firstFinish = '是'
		where project_Id in(
		<foreach collection="list" item="item" separator=",">
			#{item.projectId}
		</foreach>
		)
		and firstFinishDate is null
	</update>

    <select id="findInformationByID" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
		select * from sj_project_information where project_Id = #{projectId} and project_Name = #{projectName}
	</select>

    <select id="findList" resultType="Information">
        SELECT
		<include refid="newInformationColumns"/>,b.trial,b.appendStatus,
		datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) as newdate
        FROM sj_project_information as a
		left JOIN sj_project_append as b on a.project_id = b.project_id
        <where>
			1=1
            <if test="projectName != null and projectName != ''">
                AND a.project_name LIKE
                <if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
                <if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
                <if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
            </if>
            <if test="department != null and department != ''">
                AND a.department = #{department}
            </if>
            <if test="secondUnits != null and secondUnits != '' ">
                AND a.second_units=#{secondUnits}
            </if>
            <if test="firstUnits != null and firstUnits != '' ">
                AND a.first_units=#{firstUnits}
            </if>
			<if test="projectYear !=null and projectYear != ''">
				and a.project_year = #{projectYear}
			</if>
        	<if test="projectType !=null and projectType != ''">
				and a.project_type = #{projectType}
			</if>
			<if test="firstFinish!=null and firstFinish!=''">
				<if test="firstFinish == '是'.toString()">
					and a.firstFinish='是' and (a.secondFinish != '否' or a.secondFinish is null)
				</if>
				<if test="firstFinish == '否'.toString()">
					and (a.firstFinish='否' or a.secondFinish = '否')
				</if>
			</if>
			<if test="attrs != null and  attrs !='' ">
			and a.project_id in
				<foreach collection="attrs" item="item" index="index" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
			<if test="fileList != null and  fileList !='' ">
				and a.project_id in
					<foreach collection="fileList" item="item" index="index" open="(" separator="," close=")">
						#{item.projectId}
					</foreach>
			</if>
			<if test="extendedStatus !=null and extendedStatus!=''">
				<if test="extendedTime !=null and extendedTime!=''">
					<if test="extendedStatus == '大于等于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) &gt;= ${extendedTime}
					</if>
					<if test="extendedStatus == '大于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) &gt; ${extendedTime}
					</if>
					<if test="extendedStatus == '等于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) = ${extendedTime}
					</if>
					<if test="extendedStatus =='小于等于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) &lt;= ${extendedTime}
					</if>
					<if test="extendedStatus =='小于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) &lt; ${extendedTime}
					</if>
				</if>
			</if>
			<if test="dataScope != null and dataScope != ''">
				${dataScope}
			</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>

    <select id="findAllList" resultType="Information">
        SELECT
        <include refid="informationColumns"/>
        FROM sj_project_information 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>

    <insert id="saveInfo" parameterType="java.util.List">
		replace into  sj_project_information(
		id,
		project_id,
		project_name,
		project_type,
		project_grade,
		project_year,
		project_total,
		project_start,
		project_stop,
		department,
		project_firm,
		subDate,
		releaseDate,
		endDate,
		resultDate,
		approvalDate,
		second_units,
		first_units,
		firstFinish,
		create_by,
		create_date,
		update_by,
		update_date,
		del_flag,
		firstFinishDate,
		secondFinish,
		secondFinishDate,
		underCentralized,
		settlementPrice,
	 	suggestedAmount
		) VALUES
		<foreach collection="information" item="item" index="index" separator="," >
		(
			#{item.id},
			#{item.projectId},
			#{item.projectName},
			#{item.projectType},
			#{item.projectGrade},
			#{item.projectYear},
			#{item.projectTotal},
			#{item.projectStart},
			#{item.projectStop},
			#{item.department},
			#{item.projectFirm},
			#{item.subDate},
			#{item.releaseDate},
			#{item.endDate},
			#{item.resultDate},
			#{item.approvalDate},
			#{item.secondUnits},
			#{item.firstUnits},
		 	#{item.firstFinish},
			#{item.createBy.id},
			#{item.createDate},
			#{item.updateBy.id},
			#{item.updateDate},
			#{item.delFlag},
			#{item.firstFinishDate},
			#{item.secondFinish},
			#{item.secondFinishDate},
			#{item.underCentralized},
			#{item.settlementPrice},
			#{item.suggestedAmount}
            )
		</foreach>
	</insert>

	<select id="findListBy" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
			select
			<include refid="informationColumns"/>
			from sj_project_information
			where project_Id in
			<foreach collection="list" item="information" index="index" open="(" separator="," close=")">
				#{information.projectId}
			</foreach>
			<foreach collection="list" item="information" index="index">
				<if test="information.projectName != null and information.projectName !='' ">
				or
				project_Name in (
				SELECT project_Name from sj_project_information where
				<foreach collection="list" item="information" index="index" separator="or" >
					<if test="information.projectName != null and information.projectName !='' ">
					project_Name LIKE concat('%',#{information.projectName},'%')
					</if>
				</foreach>
					)
				</if>
			</foreach>
	</select>

	<select id="findListById" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
		select project_id,project_name,firstFinish,firstFinishDate,secondFinish,secondFinishDate from sj_project_information where project_Id=#{projectId}
	</select>

	<insert id="updateAppend">
		replace into sj_project_append(
			project_id,
			trial,
			appendStatus
		)
		VALUES
		<foreach collection="list" item="item" separator="," >
			(
			#{item.projectId},
			#{item.trial},
			#{item.appendStatus}
			)
		</foreach>
	</insert>

	<select id="allNoBack" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
		select *
		from sj_material_information
		where project_Id in(
		<foreach collection="list" item="item" index="index" separator=",">
			#{item.projectId}
		</foreach>
		    )
	</select>

	<select id="allNo" resultType="com.jeeplus.modules.sg.audit.material.entity.Material">
		SELECT
		a.*, b.project_Name,
		b.project_Type,
		c.appendStatus
		FROM
		sj_material_information AS a
		LEFT JOIN sj_project_information AS b ON a.project_id = b.project_id
		LEFT JOIN sj_project_append AS c ON a.project_id = c.project_id
		where a.project_Id in(
		<foreach collection="list" item="items" index="index"  separator=",">
			#{items.projectId}
		</foreach>
		)
	</select>

	<select id="exportInfor" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
		SELECT
		a.*,b.trial,b.construction,b.outConstruction,b.settlement
		FROM sj_project_information as a
		left JOIN sj_project_append as b on a.project_id = b.project_id
		where a.project_Id in(
		    <foreach collection="list" item="items" index="index" separator=",">
				#{items.projectId}
			</foreach>
			)
	</select>
	
	<select id="export" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
		SELECT
		a.*,b.trial,b.construction,b.outConstruction,b.settlement,b.underCentralized
		FROM sj_project_information as a
		left JOIN sj_project_append as b on a.project_id = b.project_id
		where 1=1
		<foreach collection="list" item="items" index="index">
			<if test="items.projectId != null and items.projectId != ''">
				AND a.project_Id = #{items.projectId}
			</if>
			<if test="items.projectName != null and items.projectName != ''">
				AND a.project_Name LIKE
				concat('%',#{items.projectName},'%')
			</if>
			<if test="items.department != null and items.department != ''">
				AND a.department = #{items.department}
			</if>
			<if test="items.secondUnits != null and items.secondUnits != '' ">
				AND a.second_Units=#{items.secondUnits}
			</if>
			<if test="items.firstUnits != null and items.firstUnits != '' ">
				AND a.first_Units=#{items.firstUnits}
			</if>
			<if test="items.projectType !=null and items.projectType != ''">
				and a.project_type = #{items.projectType}
			</if>
		</foreach>

	</select>

	<select id="findIdByList" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
		SELECT
		a.project_Id
		FROM sj_project_information as a
		<where>
			1=1
			<foreach collection="list" item="items" index="index">
				<if test="items.projectId != null and items.projectId != ''">
					AND a.project_Id = #{items.projectId}
				</if>
				<if test="items.projectName != null and items.projectName != ''">
					AND a.project_Name LIKE
					concat('%',#{items.projectName},'%')
				</if>
				<if test="items.department != null and items.department != ''">
					AND a.department = #{items.department}
				</if>
				<if test="items.secondUnits != null and items.secondUnits != '' ">
					AND a.second_Units=#{items.secondUnits}
				</if>
				<if test="items.firstUnits != null and items.firstUnits != '' ">
					AND a.first_Units=#{items.firstUnits}
				</if>
				<if test="items.projectType !=null and items.projectType != ''">
					and a.project_type = #{items.projectType}
				</if>
			</foreach>
		</where>
	</select>

	<select id="findOverTime" resultType="string">
		select overTime from sj_project_constant where overType = #{overType} ;
	</select>

	<update id="updateConstant" parameterType="com.jeeplus.modules.sg.audit.information.entity.constant">
		update sj_project_constant set overTime = #{overTime} where overType = 'overTime';
	</update>


	<select id="findListWord" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
		SELECT
		<include refid="newInformationColumns"/>,b.trial,b.construction,b.outConstruction,b.settlement,b.underCentralized,
		datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) as newdate
		FROM sj_project_information as a
		left JOIN sj_project_append as b on a.project_id = b.project_id
		<where>
			1=1
			and a.project_Id in (
				select project_Id
				from sj_material_information
				where (status is not null
				or status != '') group by project_id
			)
			<if test="projectName != null and projectName != ''">
				AND a.project_name LIKE
				<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
			</if>
			<if test="department != null and department != ''">
				AND a.department = #{department}
			</if>
			<if test="secondUnits != null and secondUnits != '' ">
				AND a.second_units=#{secondUnits}
			</if>
			<if test="firstUnits != null and firstUnits != '' ">
				AND a.first_units=#{firstUnits}
			</if>
			<if test="projectType !=null and projectType != ''">
				and a.project_type = #{projectType}
			</if>
			<if test="firstFinish!=null and firstFinish!=''">
				<if test="firstFinish == '是'.toString()">
					and a.firstFinish='是' and (a.secondFinish != '否' or a.secondFinish is null)
				</if>
				<if test="firstFinish == '否'.toString()">
					and (a.firstFinish='否' or a.secondFinish = '否')
				</if>
			</if>
			<if test="attrs != null and  attrs !='' ">
				and a.project_id in
				<foreach collection="attrs" item="item" index="index" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
			<if test="fileList != null and  fileList !='' ">
				and a.project_id in
				<foreach collection="fileList" item="item" index="index" open="(" separator="," close=")">
					#{item.projectId}
				</foreach>
			</if>
			<if test="extendedStatus !=null and extendedStatus!=''">
				<if test="extendedTime !=null and extendedTime!=''">
					<if test="extendedStatus == '大于等于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) &gt;= ${extendedTime}
					</if>
					<if test="extendedStatus == '大于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) &gt; ${extendedTime}
					</if>
					<if test="extendedStatus == '等于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) = ${extendedTime}
					</if>
					<if test="extendedStatus =='小于等于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) &lt;= ${extendedTime}
					</if>
					<if test="extendedStatus =='小于'.toString()">
						and  datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) &lt; ${extendedTime}
					</if>
				</if>
			</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>

	<select id="findNoFinishList" resultType="com.jeeplus.modules.sg.audit.information.entity.Information">
		select * from sj_project_information where(firstFinish ='否' or secondFinish = '否')
		and project_id in(
		<foreach collection="list" item="item" index="index"  separator=",">
			#{item}
		</foreach>
		)
	</select>

	<select id="findListMain" parameterType="java.util.List" resultType="Information">
		select
		*
		FROM sj_project_information a
		WHERE a.del_flag = '0'
		and a.project_id in
		<if test="list != null">
			<foreach collection="list" index="index" item="item" open="("
					 separator="," close=")">
				#{item}
			</foreach>
		</if>
	</select>
</mapper>