<?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.accident.AccidentMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CORPINFO_ID,
		f.ACCIDENT_NUM,
		f.ACCIDENT_NAME,
		f.ACCIDENT_DEPT,
		f.INVOLVING_DEPTS,
		f.ACCIDENT_LEVEL,
		f.ACCIDENT_TYPE,
		f.ACCIDENT_PLACE,
		f.ACCIDENT_TIME,
		f.ECONOMIC_LOSSES,
		f.INJURED_NUM,
		f.DEAD_NUM,
		f.SERIOUSLY_INJURED_NUM,
		f.ACCIDENT_BECAUSE,
		f.IS_PUNISHMENT,
		f.PUNISHMENT_AMOUNT,
		f.IMMEDIATE_CAUSE,
		f.REMOTE_CAUSE,
		f.ACCIDENT_DESCR,
		f.CREATOR,
		f.CREATTIME,
		f.ISDELETE,
		f.ACCIDENT_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CORPINFO_ID,
		ACCIDENT_NUM,
		ACCIDENT_NAME,
		ACCIDENT_DEPT,
		INVOLVING_DEPTS,
		ACCIDENT_LEVEL,
		ACCIDENT_TYPE,
		ACCIDENT_PLACE,
		ACCIDENT_TIME,
		ECONOMIC_LOSSES,
		INJURED_NUM,
		DEAD_NUM,
		SERIOUSLY_INJURED_NUM,
		ACCIDENT_BECAUSE,
		IS_PUNISHMENT,
		PUNISHMENT_AMOUNT,
		IMMEDIATE_CAUSE,
		REMOTE_CAUSE,
		ACCIDENT_DESCR,
		CREATOR,
		CREATTIME,
		ISDELETE,
		ACCIDENT_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CORPINFO_ID},
		#{ACCIDENT_NUM},
		#{ACCIDENT_NAME},
		#{ACCIDENT_DEPT},
		#{INVOLVING_DEPTS},
		#{ACCIDENT_LEVEL},
		#{ACCIDENT_TYPE},
		#{ACCIDENT_PLACE},
		#{ACCIDENT_TIME},
		#{ECONOMIC_LOSSES},
		#{INJURED_NUM},
		#{DEAD_NUM},
		#{SERIOUSLY_INJURED_NUM},
		#{ACCIDENT_BECAUSE},
		#{IS_PUNISHMENT},
		#{PUNISHMENT_AMOUNT},
		#{IMMEDIATE_CAUSE},
		#{REMOTE_CAUSE},
		#{ACCIDENT_DESCR},
		#{CREATOR},
		#{CREATTIME},
		#{ISDELETE},
		#{ACCIDENT_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
			ACCIDENT_ID = #{ACCIDENT_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			CORPINFO_ID = #{CORPINFO_ID},
			ACCIDENT_NUM = #{ACCIDENT_NUM},
			ACCIDENT_NAME = #{ACCIDENT_NAME},
			ACCIDENT_DEPT = #{ACCIDENT_DEPT},
			INVOLVING_DEPTS = #{INVOLVING_DEPTS},
			ACCIDENT_LEVEL = #{ACCIDENT_LEVEL},
			ACCIDENT_TYPE = #{ACCIDENT_TYPE},
			ACCIDENT_PLACE = #{ACCIDENT_PLACE},
			ACCIDENT_TIME = #{ACCIDENT_TIME},
			ECONOMIC_LOSSES = #{ECONOMIC_LOSSES},
			INJURED_NUM = #{INJURED_NUM},
			DEAD_NUM = #{DEAD_NUM},
			SERIOUSLY_INJURED_NUM = #{SERIOUSLY_INJURED_NUM},
			ACCIDENT_BECAUSE = #{ACCIDENT_BECAUSE},
			IS_PUNISHMENT = #{IS_PUNISHMENT},
			PUNISHMENT_AMOUNT = #{PUNISHMENT_AMOUNT},
			IMMEDIATE_CAUSE = #{IMMEDIATE_CAUSE},
			REMOTE_CAUSE = #{REMOTE_CAUSE},
			ACCIDENT_DESCR = #{ACCIDENT_DESCR},
			CREATOR = #{CREATOR},
			CREATTIME = #{CREATTIME},
			ISDELETE = #{ISDELETE},
			ACCIDENT_ID = ACCIDENT_ID
		where
			ACCIDENT_ID = #{ACCIDENT_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		l.NAME as ACCIDENT_LEVEL_NAME,
		d.NAME as ACCIDENT_DEPT_NAME,
		(select GROUP_CONCAT(t.NAME) from SYS_DICTIONARIES t where FIND_IN_SET(t.BIANMA, f.ACCIDENT_TYPE)) as ACCIDENT_TYPE_NAME,
		(select GROUP_CONCAT(ids.NAME) from OA_DEPARTMENT ids where FIND_IN_SET(ids.DEPARTMENT_ID, f.INVOLVING_DEPTS)) as INVOLVING_DEPTS_NAME
		from
		<include refid="tableName"></include> f
		left join SYS_DICTIONARIES l on l.BIANMA = f.ACCIDENT_LEVEL
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID = f.ACCIDENT_DEPT
		where
			f.ACCIDENT_ID = #{ACCIDENT_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		l.NAME as ACCIDENT_LEVEL_NAME
		from
		<include refid="tableName"></include> f
		 left join SYS_DICTIONARIES l on l.BIANMA = f.ACCIDENT_LEVEL
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.ACCIDENT_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					f.ACCIDENT_PLACE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.ACCIDENT_LEVEL != null and pd.ACCIDENT_LEVEL != ''"><!-- 关键词检索 -->
			and f.ACCIDENT_LEVEL = #{pd.ACCIDENT_LEVEL}
		</if>
		<if test="pd.ACCIDENT_DEPT != null and pd.ACCIDENT_DEPT != ''"><!-- 关键词检索 -->
			and f.ACCIDENT_DEPT = #{pd.ACCIDENT_DEPT}
		</if>
		<if test="pd.ACCIDENT_TYPE != null and pd.ACCIDENT_TYPE != ''"><!-- 关键词检索 -->
			and f.ACCIDENT_TYPE LIKE CONCAT(CONCAT('%', #{pd.ACCIDENT_TYPE}),'%')
		</if>
		order by 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="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			ACCIDENT_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

</mapper>
