<?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.sys.mapper.CompletionInformationMapper">

	<sql id="completionInformationColumns">
		a.id AS "id",
		a.projectyear AS "projectYear",
		a.itemcode AS "itemCode",
		a.projecttype AS "projectType",
		a.entryname AS "entryName",
		a.constructionunit AS "constructionUnit",
		a.contractamount AS "contractAmount",
		a.completiondate AS "completionDate",
		a.timelimit AS "timeLimit",
		a.expectedDate AS "expectedDate",
		a.submissiondate AS "submissionDate",
		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"
	</sql>

	<sql id="completionInformationJoins">

	</sql>


	<select id="get" resultType="CompletionInformation" >
		SELECT
			<include refid="completionInformationColumns"/>
		FROM lishu_item_list a
		<include refid="completionInformationJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findList" resultType="CompletionInformation" >
		SELECT
		<include refid="completionInformationColumns"/>,
		(case
		when a.submissiondate!='' and a.submissiondate is not null then ''
		else timestampdiff(day,date_format(now(),'%Y-%m-%d'),a.expectedDate) end
		) as "remainingDate",
		(case  when a.submissiondate!='' and a.submissiondate is not null then
			(case  when timestampdiff(day,a.submissiondate,expectedDate)&gt;=0  then '否' else '是' end)
		else
			(case  when timestampdiff(day,date_format(now(),'%Y-%m-%d'),a.expectedDate)&gt;=0 then '' else '是' end)
		end
		) as "overdue"
		FROM lishu_item_list a
		<include refid="completionInformationJoins"/>
		<where>
			a.del_flag = '0'
			${dataScope}
			<if test="attrs != null and attrs.size()>0 ">
				and a.itemCode in
				<foreach collection="attrs" item="item" index="index" open="(" separator="," close=")">
					${item}
				</foreach>
			</if>
			<if test="projectYear != null and projectYear != '' ">
				AND a.projectYear = #{projectYear}
			</if>
			<if test="projectType != null and projectType != ''">
				AND a.projecttype LIKE
				    <if test="_databaseId == 'postgre'">'%'||#{projectType}||'%'</if>
					<if test="_databaseId == 'oracle'">'%'||#{projectType}||'%'</if>
					<if test="_databaseId == 'mssql'">'%'+#{projectType}+'%'</if>
					<if test="_databaseId == 'mysql'">concat('%',#{projectType},'%')</if>
			</if>
			<if test="entryName != null and entryName != ''">
				AND a.entryname LIKE
				    <if test="_databaseId == 'postgre'">'%'||#{entryName}||'%'</if>
					<if test="_databaseId == 'oracle'">'%'||#{entryName}||'%'</if>
					<if test="_databaseId == 'mssql'">'%'+#{entryName}+'%'</if>
					<if test="_databaseId == 'mysql'">concat('%',#{entryName},'%')</if>
			</if>
			<if test="constructionUnit != null and constructionUnit != ''">
				AND a.constructionunit LIKE
				    <if test="_databaseId == 'postgre'">'%'||#{constructionUnit}||'%'</if>
					<if test="_databaseId == 'oracle'">'%'||#{constructionUnit}||'%'</if>
					<if test="_databaseId == 'mssql'">'%'+#{constructionUnit}+'%'</if>
					<if test="_databaseId == 'mysql'">concat('%',#{constructionUnit},'%')</if>
			</if>
			<if test="beginContractAmount != null and beginContractAmount != ''">
				AND a.contractamount &gt;= #{beginContractAmount}
			</if>
			<if test="endContractAmount != null and endContractAmount != ''">
				AND a.contractamount &lt;=#{endContractAmount}
			</if>
			<if test="beginCompletionDate != null and beginCompletionDate != ''">
				AND a.completiondate &gt;= #{beginCompletionDate}
			</if>
			<if test="endCompletionDate != null and endCompletionDate != '' ">
				AND a.completiondate &lt;=#{endCompletionDate}
			</if>
			<if test="beginRemainingDate != null and beginRemainingDate != ''">
				AND (case
				when a.submissiondate!='' and a.submissiondate is not null then null
				else timestampdiff(day,date_format(now(),'%Y-%m-%d'),a.expectedDate) end
				) &gt;= #{beginRemainingDate}
			</if>
			<if test="endRemainingDate != null and endRemainingDate != ''">
				AND (case
				when a.submissiondate!='' and a.submissiondate is not null then null
				else timestampdiff(day,date_format(now(),'%Y-%m-%d'),a.expectedDate) end
				) &lt;= #{endRemainingDate}
			</if>
			<if test="overdue!=null and overdue!=''">
				and (case  when a.submissiondate!='' and a.submissiondate is not null then
				(case  when timestampdiff(day,a.submissiondate,expectedDate)&gt;=0  then '否' else '是' end)
				else
				(case  when timestampdiff(day,date_format(now(),'%Y-%m-%d'),a.expectedDate)&lt;=0 then '' else '是' end)
				end
				) = #{overdue}
			</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>

	<select id="findAllList" resultType="CompletionInformation" >
		SELECT
			<include refid="completionInformationColumns"/>
		FROM lishu_item_list a
		<include refid="completionInformationJoins"/>
		<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 lishu_item_list(
			id,
			projectyear,
			itemcode,
			projecttype,
			entryname,
			constructionunit,
			contractamount,
			completiondate,
			timelimit,
			expectedDate,
			submissiondate,
			remainingdate,
			overdue,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{projectYear},
			#{itemCode},
			#{projectType},
			#{entryName},
			#{constructionUnit},
			#{contractAmount},
			#{completionDate},
			#{timeLimit},
			#{expectedDate},
			#{submissionDate},
			#{remainingDate},
			#{overdue},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	<insert id="insertList">
		INSERT INTO lishu_item_list(
			id,
			projectyear,
			itemcode,
			projecttype,
			entryname,
			constructionunit,
			contractamount,
			completiondate,
			timelimit,
			expectedDate,
			submissiondate,
			remainingdate,
			overdue,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES
		<foreach collection="list" item="item" index="index" separator=",">
			(
				#{item.id},
				#{item.projectYear},
				#{item.itemCode},
				#{item.projectType},
				#{item.entryName},
				#{item.constructionUnit},
				#{item.contractAmount},
				#{item.completionDate},
				#{item.timeLimit},
				#{item.expectedDate},
				#{item.submissionDate},
				#{item.remainingDate},
				#{item.overdue},
				#{item.createBy.id},
				#{item.createDate},
				#{item.updateBy.id},
				#{item.updateDate},
				#{item.remarks},
				#{item.delFlag}
			)
		</foreach>
	</insert>

	<update id="update">
		UPDATE lishu_item_list SET
			projectyear = #{projectYear},
			itemcode = #{itemCode},
			projecttype = #{projectType},
			entryname = #{entryName},
			constructionunit = #{constructionUnit},
			contractamount = #{contractAmount},
			completiondate = #{completionDate},
			timelimit = #{timeLimit},
			expectedDate = #{expectedDate},
			submissiondate = #{submissionDate},
			remainingdate = #{remainingDate},
			overdue = #{overdue},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>


	<!--物理删除-->
	<update id="delete">
		DELETE FROM lishu_item_list
		WHERE id = #{id}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE lishu_item_list SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>

	<update id="updateDelFlag">
		UPDATE lishu_item_list SET
			del_flag = '1'
		WHERE id = #{id}
	</update>
	<update id="updateSubmissionDate">
		<foreach collection="list" index="index" item="item" separator=",">
			update lishu_item_list
			set submissionDate = #{item.submissionDate}
			where id = #{item.id};
		</foreach>
	</update>

	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="CompletionInformation" statementType="STATEMENT">
		select * FROM lishu_item_list  where ${propertyName} = '${value}'
	</select>
	<select id="isSubmissionDate" resultType="CompletionInformation">
		select * from lishu_item_list
		<where>
			id in(
			<foreach collection="array" index="index" item="item" separator=",">
				#{item}
			</foreach>
			)
		</where>
	</select>

</mapper>