<?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.zcloud.mapper.datasource.safetyInvestment.SafetyInvestmentPlanMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_SAFETYINVESTMENTPLAN
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.CORPINFO_ID,
		f.PLAN_NAME,
		f.PLAN_YEAR,
		f.PLAN_PERIOD,
		f.TOTAL_AMOUNT,
		f.STATUS,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.SAFETYINVESTMENTPLAN_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CORPINFO_ID,
		PLAN_NAME,
		PLAN_YEAR,
		PLAN_PERIOD,
		TOTAL_AMOUNT,
		STATUS,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		SAFETYINVESTMENTPLAN_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CORPINFO_ID},
		#{PLAN_NAME},
		#{PLAN_YEAR},
		#{PLAN_PERIOD},
		#{TOTAL_AMOUNT},
		#{STATUS},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{SAFETYINVESTMENTPLAN_ID}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			SAFETYINVESTMENTPLAN_ID = #{SAFETYINVESTMENTPLAN_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		    PLAN_NAME = #{PLAN_NAME},
			PLAN_YEAR = #{PLAN_YEAR},
			PLAN_PERIOD = #{PLAN_PERIOD},
			TOTAL_AMOUNT = #{TOTAL_AMOUNT},
		    STATUS = #{STATUS},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			SAFETYINVESTMENTPLAN_ID = SAFETYINVESTMENTPLAN_ID
		where
			SAFETYINVESTMENTPLAN_ID = #{SAFETYINVESTMENTPLAN_ID}
	</update>

	<!-- 修改 -->
	<update id="editStatus" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		STATUS = #{STATUS},
		OPERATOR = #{OPERATOR}
		where
		SAFETYINVESTMENTPLAN_ID = #{SAFETYINVESTMENTPLAN_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.SAFETYINVESTMENTPLAN_ID = #{SAFETYINVESTMENTPLAN_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		(select count(1) from BUS_SAFETYINVESTMENTPLANINFO i where i.SAFETYINVESTMENTPLAN_ID = f.SAFETYINVESTMENTPLAN_ID) as INFO_COUNT
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.PLAN_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.PLAN_YEAR != null and pd.PLAN_YEAR != ''"><!-- 关键词检索 -->
			and f.PLAN_YEAR = #{pd.PLAN_YEAR}
		</if>
		<if test="pd.PLAN_PERIOD != null and pd.PLAN_PERIOD != ''"><!-- 关键词检索 -->
			and f.PLAN_PERIOD = #{pd.PLAN_PERIOD}
		</if>
		<if test="pd.STATUS != null and pd.STATUS != ''"><!-- 关键词检索 -->
			and f.STATUS = #{pd.STATUS}
		</if>
		order by f.PLAN_YEAR DESC, f.PLAN_PERIOD, f.STATUS = 0 desc, f.CREATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			SAFETYINVESTMENTPLAN_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

</mapper>
