<?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.eightwork.LimitSpaceMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.NUMBER,
		f.NAME,
		f.LIMITSPACETYPE,
		f.POSITIONSCOPE,
		f.PRIMARYHAZARD,
		f.RISKGRADE,
		f.MAXPERSON,
		f.DESCR,
		f.INFORMANT,
		f.PRINCIPAL,
		f.COMPILETIME,
		f.DEVICENAME,
		f.ACCIDENTTYPE,
		f.FREQUENCYNUM,
		f.WARNINGSIGNS_NAME,
		f.WARNINGSIGNS_PATH,
		f.CORPINFO_ID,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.HASINSTRUCTOR_PATH,
		f.HASINSTRUCTOR_NAME,
		f.LIMITSPACE_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		NUMBER,
		NAME,
		LIMITSPACETYPE,
		POSITIONSCOPE,
		PRIMARYHAZARD,
		RISKGRADE,
		MAXPERSON,
		DESCR,
		INFORMANT,
		PRINCIPAL,
		COMPILETIME,
		DEVICENAME,
		ACCIDENTTYPE,
		FREQUENCYNUM,
		WARNINGSIGNS_NAME,
		WARNINGSIGNS_PATH,
		HASINSTRUCTOR_NAME,
		HASINSTRUCTOR_PATH,
		CORPINFO_ID,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		LIMITSPACE_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{NUMBER},
		#{NAME},
		#{LIMITSPACETYPE},
		#{POSITIONSCOPE},
		#{PRIMARYHAZARD},
		#{RISKGRADE},
		#{MAXPERSON},
		#{DESCR},
		#{INFORMANT},
		#{PRINCIPAL},
		#{COMPILETIME},
		#{DEVICENAME},
		#{ACCIDENTTYPE},
		#{FREQUENCYNUM},
		#{WARNINGSIGNS_NAME},
		#{WARNINGSIGNS_PATH},
		#{HASINSTRUCTOR_NAME},
		#{HASINSTRUCTOR_PATH},
		#{CORPINFO_ID},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{LIMITSPACE_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',
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME}
		where
			LIMITSPACE_ID = #{LIMITSPACE_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			NUMBER = #{NUMBER},
			NAME = #{NAME},
			LIMITSPACETYPE = #{LIMITSPACETYPE},
			POSITIONSCOPE = #{POSITIONSCOPE},
			PRIMARYHAZARD = #{PRIMARYHAZARD},
			RISKGRADE = #{RISKGRADE},
			MAXPERSON = #{MAXPERSON},
			DESCR = #{DESCR},
		    <if test="WARNINGSIGNS_NAME != null and WARNINGSIGNS_NAME != ''">
				WARNINGSIGNS_NAME = #{WARNINGSIGNS_NAME},
			</if>
		    <if test="WARNINGSIGNS_PATH != null and WARNINGSIGNS_PATH != ''">
				WARNINGSIGNS_PATH = #{WARNINGSIGNS_PATH},
			</if>
		    <if test="HASINSTRUCTOR_NAME != null and HASINSTRUCTOR_NAME != ''">
				HASINSTRUCTOR_NAME = #{HASINSTRUCTOR_NAME},
			</if>
		    <if test="HASINSTRUCTOR_PATH != null and HASINSTRUCTOR_PATH != ''">
				HASINSTRUCTOR_PATH = #{HASINSTRUCTOR_PATH},
			</if>
		 	INFORMANT = #{INFORMANT},
			PRINCIPAL = #{PRINCIPAL},
			ACCIDENTTYPE = #{ACCIDENTTYPE},
			COMPILETIME = #{COMPILETIME},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			LIMITSPACE_ID = LIMITSPACE_ID
		where
			LIMITSPACE_ID = #{LIMITSPACE_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		f.*,
		lst.NAME LIMITSPACETYPENAME,
		rg.NAME RISKGRADENAME,
		i.NAME INFORMANTNAME,
		p.NAME PRINCIPALNAME
		from
		<include refid="tableName"></include> f
		left join sys_dictionaries lst on f.LIMITSPACETYPE = lst.BIANMA and lst.YNDEL = 'no'
		left join sys_dictionaries rg on f.RISKGRADE = rg.BIANMA and rg.YNDEL = 'no'
		left join sys_user i on i.USER_ID = f.INFORMANT
		left join sys_user p on p.USER_ID = f.PRINCIPAL
		where
			f.LIMITSPACE_ID = #{LIMITSPACE_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		lst.NAME LIMITSPACETYPENAME,
		rg.NAME RISKGRADENAME,
		i.NAME INFORMANTNAME,
		p.NAME PRINCIPALNAME
		from
		<include refid="tableName"></include> f
		left join sys_dictionaries lst on f.LIMITSPACETYPE = lst.BIANMA and lst.YNDEL = 'no'
		left join sys_dictionaries rg on f.RISKGRADE = rg.BIANMA and rg.YNDEL = 'no'
		left join sys_user i on i.USER_ID = f.INFORMANT
		left join sys_user p on p.USER_ID = f.PRINCIPAL
		where f.ISDELETE = '0'
		and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					f.NUMBER LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<choose>
			<when test='pd.ISMAIN != null and pd.ISMAIN != "" and pd.ISMAIN == "1"'>
				<if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
					and
					i.DEPARTMENT_ID in
					<foreach item="item" index="index"
							 collection="pd.DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
				</if>
			</when>
			<otherwise>
				<choose>
					<when test='pd.ISSUPERVISE != null and pd.ISSUPERVISE != "" and pd.ISSUPERVISE == "1"'>
						<choose>
							<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
								and
								i.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								i.DEPARTMENT_ID in (
								select
								SUB_DEPARTMENT_ID
								from
								oa_supervision_department osd
								where
								osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
								and osd.ISDELETE = '0'
								and osd.CORPINFO_ID  = #{pd.CORPINFO_ID}
								)
							</otherwise>
						</choose>
					</when>
					<when test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
						and i.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
					</when>
					<otherwise>
					</otherwise>
				</choose>
			</otherwise>
		</choose>
		ORDER BY f.COMPILETIME DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		WHERE
		f.ISDELETE = '0'
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		order by f.OPERATTIME desc

	</select>

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

</mapper>
