<?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.DepartmentMapper">

	<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">
		OA_DEPARTMENT
	</sql>

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

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{NAME},
		#{NAME_EN},
		#{BIANMA},
		#{PARENT_ID},
		#{HEADMAN},
		#{TEL},
		#{FUNCTIONS},
		#{ADDRESS},
		#{BZ},
		#{DEPARTMENT_ID},
		#{CORPINFO_ID},
		#{LEVEL},
		#{DEP_ORDER},
		#{ISSUPERVISE},
		#{deptId}
	</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},
			DEP_ORDER = #{DEP_ORDER},
			ISSUPERVISE = #{ISSUPERVISE},
			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
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where 1=1
		<if test="_parameter  != null and _parameter  != ''"><!-- 关键词检索 -->
			and 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 ISDELETE = 0
		<if test="PARENT_ID  != null and PARENT_ID  != ''"><!-- 关键词检索 -->
			and PARENT_ID = #{PARENT_ID}
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
				CORPINFO_ID = #{CORPINFO_ID}
		</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,
		f.DEP_ORDER,
		f.ISSUPERVISE,

		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>

	<insert id="batchUpdate" parameterType="pd">
        INSERT INTO
        <include refid="tableName"></include>
        (
        DEPARTMENT_ID,
        deptId
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.DEPARTMENT_ID},
            #{item.deptId}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE deptId = VALUES ( deptId )
    </insert>
</mapper>
