<?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.emergency.EmergencyTeamMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.TEAM_NAME,
		f.TEAM_CATEGORY,
		f.TEAM_PROPERTY,
		f.TEAM_LEVEL,
		f.TEAM_GRADE,
		f.TEAM_CREATE_TIME,
		f.ADDRESS,
		f.SERVICE_PLACE,
		f.TEAM_LEADER_NAME,
		f.TEAM_LEADER_PHONE,
		f.DUTY_TELEPHONE,
		f.TOTLE_PERSON_NUM,
		f.COMMANDER_NUM,
		f.CAR_NUM,
		f.LOGISTICS_PERSON_NUM,
		f.TEAM_MEMBER_INFO,
		f.SPECIAL_TYPE,
		f.SPECIAL_DESC,
		f.TEAM_DESC,
		f.ISDELETE,
		f.OPERATOR,
		f.OPERATTIME,
		f.CREATTIME,
		f.CREATOR,
		f.CORPINFO_ID,
		f.EMERGENCYTEAM_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		TEAM_NAME,
		TEAM_CATEGORY,
		TEAM_PROPERTY,
		TEAM_LEVEL,
		TEAM_GRADE,
		TEAM_CREATE_TIME,
		ADDRESS,
		SERVICE_PLACE,
		TEAM_LEADER_NAME,
		TEAM_LEADER_PHONE,
		DUTY_TELEPHONE,
		TOTLE_PERSON_NUM,
		COMMANDER_NUM,
		CAR_NUM,
		LOGISTICS_PERSON_NUM,
		TEAM_MEMBER_INFO,
		SPECIAL_TYPE,
		SPECIAL_DESC,
		TEAM_DESC,
		ISDELETE,
		OPERATOR,
		OPERATTIME,
		CREATTIME,
		CREATOR,
		CORPINFO_ID,
		EMERGENCYTEAM_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{TEAM_NAME},
		#{TEAM_CATEGORY},
		#{TEAM_PROPERTY},
		#{TEAM_LEVEL},
		#{TEAM_GRADE},
		#{TEAM_CREATE_TIME},
		#{ADDRESS},
		#{SERVICE_PLACE},
		#{TEAM_LEADER_NAME},
		#{TEAM_LEADER_PHONE},
		#{DUTY_TELEPHONE},
		#{TOTLE_PERSON_NUM},
		#{COMMANDER_NUM},
		#{CAR_NUM},
		#{LOGISTICS_PERSON_NUM},
		#{TEAM_MEMBER_INFO},
		#{SPECIAL_TYPE},
		#{SPECIAL_DESC},
		#{TEAM_DESC},
		#{ISDELETE},
		#{OPERATOR},
		#{OPERATTIME},
		#{CREATTIME},
		#{CREATOR},
		#{CORPINFO_ID},
		#{EMERGENCYTEAM_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
			EMERGENCYTEAM_ID = #{EMERGENCYTEAM_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			TEAM_NAME = #{TEAM_NAME},
			TEAM_CATEGORY = #{TEAM_CATEGORY},
			TEAM_PROPERTY = #{TEAM_PROPERTY},
			TEAM_LEVEL = #{TEAM_LEVEL},
			TEAM_GRADE = #{TEAM_GRADE},
			TEAM_CREATE_TIME = #{TEAM_CREATE_TIME},
			ADDRESS = #{ADDRESS},
			SERVICE_PLACE = #{SERVICE_PLACE},
			TEAM_LEADER_NAME = #{TEAM_LEADER_NAME},
			TEAM_LEADER_PHONE = #{TEAM_LEADER_PHONE},
			DUTY_TELEPHONE = #{DUTY_TELEPHONE},
			TOTLE_PERSON_NUM = #{TOTLE_PERSON_NUM},
			COMMANDER_NUM = #{COMMANDER_NUM},
			CAR_NUM = #{CAR_NUM},
			LOGISTICS_PERSON_NUM = #{LOGISTICS_PERSON_NUM},
			TEAM_MEMBER_INFO = #{TEAM_MEMBER_INFO},
			SPECIAL_TYPE = #{SPECIAL_TYPE},
			SPECIAL_DESC = #{SPECIAL_DESC},
			TEAM_DESC = #{TEAM_DESC},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			EMERGENCYTEAM_ID = EMERGENCYTEAM_ID
		where
			EMERGENCYTEAM_ID = #{EMERGENCYTEAM_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		,d1.NAME as 'TEAM_CATEGORY_NAME'
		,d2.NAME as 'TEAM_PROPERTY_NAME'
		,d3.NAME as 'TEAM_LEVEL_NAME'
		,d4.NAME as 'TEAM_GRADE_NAME'
		,d5.NAME as 'SPECIAL_TYPE_NAME'
		from
		<include refid="tableName"></include> f
		left join SYS_DICTIONARIES d1 on f.TEAM_CATEGORY = d1.BIANMA and d1.PARENT_ID = 'ef2f9a53723b4531968279fe4816be3d'
		left join SYS_DICTIONARIES d2 on f.TEAM_PROPERTY = d2.BIANMA and d2.PARENT_ID = 'afde28dd39c841d984fb331116781ff3'
		left join SYS_DICTIONARIES d3 on f.TEAM_LEVEL = d3.BIANMA and d3.PARENT_ID = 'bfd057e59c124b42ac93e13c6a30b85e'
		left join SYS_DICTIONARIES d4 on f.TEAM_GRADE = d4.BIANMA and d4.PARENT_ID = '4dca6d39490d4845ba7b02df2ab68aa4'
		left join SYS_DICTIONARIES d5 on f.SPECIAL_TYPE = d5.BIANMA and d5.PARENT_ID = '6a724d36c3ad416fad22049d932c329e'
		where
			f.EMERGENCYTEAM_ID = #{EMERGENCYTEAM_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		,d1.NAME as 'TEAM_CATEGORY_NAME'
		,d2.NAME as 'TEAM_PROPERTY_NAME'
		,d3.NAME as 'TEAM_LEVEL_NAME'
		,d4.NAME as 'TEAM_GRADE_NAME'
		,d5.NAME as 'SPECIAL_TYPE_NAME'
		from
		<include refid="tableName"></include> f
		left join SYS_DICTIONARIES d1 on f.TEAM_CATEGORY = d1.BIANMA and d1.PARENT_ID = 'ef2f9a53723b4531968279fe4816be3d'
		left join SYS_DICTIONARIES d2 on f.TEAM_PROPERTY = d2.BIANMA and d2.PARENT_ID = 'afde28dd39c841d984fb331116781ff3'
		left join SYS_DICTIONARIES d3 on f.TEAM_LEVEL = d3.BIANMA and d3.PARENT_ID = 'bfd057e59c124b42ac93e13c6a30b85e'
		left join SYS_DICTIONARIES d4 on f.TEAM_GRADE = d4.BIANMA and d4.PARENT_ID = '4dca6d39490d4845ba7b02df2ab68aa4'
		left join SYS_DICTIONARIES d5 on f.SPECIAL_TYPE = d5.BIANMA and d5.PARENT_ID = '6a724d36c3ad416fad22049d932c329e'
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
				f.TEAM_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				<!--	根据需求自己加检索条件
					字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				-->
				)
		</if>
		ORDER BY f.OPERATTIME DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
	</select>
	<select id="emergencyIndex" resultType="com.zcloud.entity.PageData">
		SELECT
			COUNT( 1 ) emergencyplan,
			( SELECT COUNT( 1 ) emergencyequipment FROM bus_emergencyequipment WHERE CORPINFO_ID = #{CORPINFO_ID} AND ISDELETE = 0 ) emergencyequipment,
			( SELECT COUNT( 1 ) emergencydrillcontent FROM bus_emergencydrillcontent WHERE CORPINFO_ID = #{CORPINFO_ID} AND ISDELETE = 0 ) emergencydrillcontent,
			( SELECT COUNT( 1 ) emergencyteam FROM bus_emergencyteam WHERE CORPINFO_ID = #{CORPINFO_ID} AND ISDELETE = 0 ) emergencyteam
		FROM
			bus_emergencyplan
		WHERE
			CORPINFO_ID = #{CORPINFO_ID}
		  AND ISDELETE = 0
	</select>

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

</mapper>
