<?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.dsno1.system.AdminDepartmentMapper">

	<resultMap type="Department" id="departmentResultMap">
		<id column="DEPARTMENT_ID" property="DEPARTMENT_ID"/>
		<result column="NAME" property="NAME"/>
		<result column="PARENT_ID" property="PARENT_ID"/>
	</resultMap>

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

	<!-- 字段 -->
	<sql id="Field">
		NAME,
		NAME_EN,
		BIANMA,
		PARENT_ID,
		HEADMAN,
		TEL,
		FUNCTIONS,
		ADDRESS,
		BZ,
		DEPARTMENT_ID,
		CORPINFO_ID,
		LEVEL
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{NAME},
		#{NAME_EN},
		#{BIANMA},
		#{PARENT_ID},
		#{HEADMAN},
		#{TEL},
		#{FUNCTIONS},
		#{ADDRESS},
		#{BZ},
		#{DEPARTMENT_ID},
		#{CORPINFO_ID},
		#{LEVEL}
	</sql>

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

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
		DEPARTMENT_ID = #{DEPARTMENT_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		NAME = #{NAME},
		NAME_EN = #{NAME_EN},
		HEADMAN = #{HEADMAN},
		BZ = #{BZ},
		TEL = #{TEL},
		FUNCTIONS = #{FUNCTIONS},
		ADDRESS = #{ADDRESS},
		LEVEL = #{LEVEL},
		DEPARTMENT_ID = DEPARTMENT_ID
		where
		DEPARTMENT_ID = #{DEPARTMENT_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		(select NAME from OA_DEPARTMENT o where t.PARENT_ID=o.DEPARTMENT_ID) as PARENT_NAME
		from
		<include refid="tableName"></include> t
		where
		DEPARTMENT_ID = #{DEPARTMENT_ID}
	</select>
	<select id="findByName" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		(select NAME from OA_DEPARTMENT o where t.PARENT_ID=o.DEPARTMENT_ID) as PARENT_NAME
		from
		<include refid="tableName"></include> t
		where
		t.NAME = #{NAME} and t.CORPINFO_ID = #{CORPINFO_ID}
	</select>

	<!--通过条件获取全部 -->
	<select id="findByCorpId" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		f.CORPINFO_ID = #{CORPINFO_ID} and f.PARENT_ID='0'
	</select>

	<!-- 通过编码获取数据 -->
	<select id="findByBianma" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where
		BIANMA = #{BIANMA}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where
		1=1
		<if test="pd.DEPARTMENT_ID!= null and pd.DEPARTMENT_ID != ''"><!-- 检索 -->
			and PARENT_ID = #{pd.DEPARTMENT_ID}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
			CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>

		<if test="pd.keywords != null and pd.keywords != ''"><!-- 关键词检索 -->
			and
			(
			NAME LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
			or
			NAME_EN LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
			or
			BIANMA LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
			)
		</if>
		order by NAME
	</select>

	<!-- 通过ID获取其子级列表 -->
	<select id="listSubDepartmentByParentId" parameterType="String" resultMap="departmentResultMap">
		select
		f.DEPARTMENT_ID,
		f.PARENT_ID,
		(case when county.NAME != '' then
			CONCAT(
				CASE
				f.TYPE
				WHEN 1 THEN
				'安委会'
				WHEN 2 THEN
				'行业'
				WHEN 3 THEN
				'属地'
				WHEN 4 THEN
				'其他'
			END,
			'--',
			f.NAME,
			CONCAT( '(', county.NAME ),
			')'
			)
		else
			CONCAT(
				CASE
				f.TYPE
				WHEN 1 THEN
				'安委会'
				WHEN 2 THEN
				'行业'
				WHEN 3 THEN
				'属地'
				WHEN 4 THEN
				'其他'
			END,
			'--',
			f.NAME
			)
		end
		)AS NAME
		from
		<include refid="tableName"></include> f
		left join SYS_DICTIONARIES county on county.BIANMA = f.county
		where 1=1
		<if test="_parameter  != null and _parameter  != ''"><!-- 关键词检索 -->
			and f.PARENT_ID = #{parentId}
		</if>
		order by NAME
	</select>

	<!-- 通过ID获取其子级列表 -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where 1=1
		<if test="parentId  != null and parentId  != ''"><!-- 关键词检索 -->
			and PARENT_ID = #{parentId}
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
			CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="POSSESSION != null and POSSESSION != '' and POSSESSIONLEVEL != null and POSSESSIONLEVEL != '' ">

			<choose>
				<when test='POSSESSIONLEVEL=="1"'>
					and PROVINCE =
				</when>
				<when test='POSSESSIONLEVEL=="2"'>
					and CITY =
				</when>
				<when test='POSSESSIONLEVEL=="3"'>
					and COUNTY =
				</when>
				<when test='POSSESSIONLEVEL=="4"'>
					and VILLAGE =
				</when>
				<when test='POSSESSIONLEVEL=="5"'>
					and STREET =
				</when>

			</choose>
			#{POSSESSION}
		</if>
		<if test="TYPE != null and TYPE != ''"><!-- 关键词检索 -->
			AND
			TYPE = #{TYPE}
		</if>
		order by NAME
	</select>

	<!-- 通过ID获取其子级列表 -->
	<select id="listAllByType" parameterType="pd" resultMap="departmentResultMap">
		select
		f.DEPARTMENT_ID,
		f.PARENT_ID,
		CONCAT(f.NAME,CONCAT('(', county.NAME),')') AS NAME
		from
		<include refid="tableName"></include> f
		left join SYS_DICTIONARIES county on county.BIANMA = f.county
		where 1=1
		<if test="PARENT_ID  != null and PARENT_ID  != ''"><!-- 关键词检索 -->
			and f.PARENT_ID = #{PARENT_ID}
		</if>
		<if test="TYPE != null and TYPE != ''"><!-- 关键词检索 -->
			AND
			f.TYPE = #{TYPE}
		</if>
		order by NAME
	</select>

	<!-- 列表关联部门级别 -->
	<select id="forLevelNamedatalistPage" parameterType="page" resultType="pd">
		select
		f.NAME,
		f.NAME_EN,
		f.BIANMA,
		f.PARENT_ID,
		f.HEADMAN,
		f.TEL,
		f.FUNCTIONS,
		f.ADDRESS,
		f.BZ,
		f.DEPARTMENT_ID,
		f.CORPINFO_ID,
		f.LEVEL,
		s.name as leName
		from  OA_DEPARTMENT f
		LEFT JOIN SYS_DICTIONARIES s on s.BIANMA = f.LEVEL
		where
		1=1
		<if test="pd.DEPARTMENT_ID!= null and pd.DEPARTMENT_ID != ''"><!-- 检索 -->
			and f.PARENT_ID = #{pd.DEPARTMENT_ID}
		</if>
		<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.NAME LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
			or
			f.NAME_EN LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
			or
			f.BIANMA LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
			)
		</if>
		order by NAME
	</select>

	<!-- 修改企业时，同步修改跟部门名称 -->
	<update id="editCornDept" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		NAME = #{NAME}
		where
		NAME = #{OLDNAME}
		and
		PARENT_ID = #{PARENT_ID}
		and
		CORPINFO_ID = #{CORPINFO_ID}
	</update>
</mapper>
