<?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="org.springblade.system.mapper.ArchiveMapper">
	<insert id="saveArchivalCatalogue">
			INSERT INTO record_da_ky_files_contents
			<trim prefix="(" suffix=")" suffixOverrides=",">
				<if test="contentName != null and contentName != ''">content_name,</if>
				<if test="parentId != null">parent_id,</if>
				<if test="level != null">`level`,</if>
				<if test="sort != null">sort,</if>
				<if test="markCode != null and markCode != ''">mark_code,</if>
				<if test="term != null and term != ''">term,</if>
				<if test="unitName != null and unitName != ''">unit_name,</if>
				<if test="major != null and major != ''">major,</if>
				<if test="classifyMark != null and classifyMark != ''">classify_mark,</if>
				<if test="createUserId != null">create_user_id,</if>
				<if test="createUserName != null and createUserName != ''">create_user_name,</if>
				<if test="updateUserId != null">update_user_id,</if>
				<if test="updateUserName != null and updateUserName != ''">update_user_name,</if>
			</trim>
			VALUES
			<trim prefix="(" suffix=")" suffixOverrides=",">
				<if test="contentName != null and contentName != ''">#{contentName},</if>
				<if test="parentId != null">#{parentId},</if>
				<if test="level != null">#{level},</if>
				<if test="sort != null">#{sort},</if>
				<if test="markCode != null and markCode != ''">#{markCode},</if>
				<if test="term != null and term != ''">#{term},</if>
				<if test="unitName != null and unitName != ''">#{unitName},</if>
				<if test="major != null and major != ''">#{major},</if>
				<if test="classifyMark != null and classifyMark != ''">#{classifyMark},</if>
				<if test="createUserId != null">#{createUserId},</if>
				<if test="createUserName != null and createUserName != ''">#{createUserName},</if>
				<if test="updateUserId != null">#{updateUserId},</if>
				<if test="updateUserName != null and updateUserName != ''">#{updateUserName},</if>
			</trim>
	</insert>
	<update id="editArchivalCatalogue">
		UPDATE record_da_ky_files_contents
		<set>
			<if test="contentName != null and contentName != ''">content_name = #{contentName},</if>
			<if test="parentId != null">parent_id = #{parentId},</if>
			<if test="level != null">`level` = #{level},</if>
			<if test="sort != null">sort = #{sort},</if>
			<if test="markCode != null and markCode != ''">mark_code = #{markCode},</if>
			<if test="term != null and term != ''">term = #{term},</if>
			<if test="unitName != null and unitName != ''">unit_name = #{unitName},</if>
			<if test="major != null and major != ''">major = #{major},</if>
			<if test="classifyMark != null and classifyMark != ''">classify_mark = #{classifyMark},</if>
			<if test="createUserId != null">create_user_id = #{createUserId},</if>
			<if test="createUserName != null and createUserName != ''">create_user_name = #{createUserName},</if>
			<if test="updateUserId != null">update_user_id = #{updateUserId},</if>
			<if test="updateUserName != null and updateUserName != ''">update_user_name = #{updateUserName},</if>
		</set>
		WHERE id = #{id}
	</update>
	<delete id="delArchivalCatalogue">
		DELETE FROM record_da_ky_files_contents
		WHERE id = #{id}
	</delete>

	<select id="getArchivalCatalogueTree" resultType="org.springblade.system.domain.ArchivalCatalogueDto">
		SELECT
		t1.id,
		t1.content_name AS contentName,
		t1.parent_id AS parentId,
		t2.content_name AS parentName,
		t1.`level` AS LEVEL,
		t1.sort,
		t1.mark_code AS markCode,
		t1.term,
		t1.unit_name AS unitName,
		t1.major,
		t1.classify_mark AS classifyMark,
		t1.create_time AS createTime,
		t1.update_time AS updateTime,
		t1.create_user_id AS createUserId,
		t1.create_user_name AS createUserName,
		t1.update_user_id AS updateUserId,
		t1.update_user_name AS updateUserName,
		t1.del_flag AS delFlag
		FROM
        record_da_ky_files_contents t1
		LEFT JOIN record_da_ky_files_contents t2 ON t1.parent_id = t2.id
		WHERE
		t1.del_flag = '1'
		AND t1.LEVEL IN ( 0, 1, 2, 3 )
		<if test="contentName != null and contentName != ''">
			and INSTR(t1.content_name, #{contentName}) > 0
		</if>
		ORDER BY
		t1.`level`,t1.sort
	</select>
	<select id="getArchivalCatalogue" resultType="org.springblade.system.domain.ArchivalCatalogueDto">
		SELECT
		dkfc.id,
		dkfc.content_name AS contentName,
		concat(dkfc2.content_name, '/', dkfc1.content_name) AS path,
		dkfc.parent_id AS parentId,
		dkfc.`level` AS level,
		dkfc.sort,
		dkfc.mark_code AS markCode,
		dkfc.term,
		dkfc.unit_name AS unitName,
		dkfc.major,
		dkfcm.id AS classifyMarkId,
		dkfcm.classify_mark_name AS classifyMark,
		dkfc.create_time AS createTime,
		dkfc.update_time AS updateTime,
		dkfc.create_user_id AS createUserId,
		dkfc.create_user_name AS createUserName,
		dkfc.update_user_id AS updateUserId,
		dkfc.update_user_name AS updateUserName
		FROM
        record_da_ky_files_contents dkfc
		    left join record_da_ky_files_contents dkfc1 on dkfc.parent_id = dkfc1.id
		    left join record_da_ky_files_contents dkfc2 on dkfc1.parent_id = dkfc2.id
		left join record_da_ky_files_classify_mark dkfcm on dkfcm.id = dkfc.classify_mark
		WHERE
		dkfc.del_flag = '1' and dkfc.parent_id = #{parentId} and dkfc.level = 4
		<if test="contentName != null and contentName != ''">
			and INSTR(dkfc.content_name, #{contentName}) > 0
		</if>
		order by dkfc.create_time desc
	</select>
	<select id="getArchivalCatalogueById" resultType="org.springblade.system.domain.ArchivalCatalogueDto">
		SELECT
			t1.id,
			t1.content_name AS contentName,
			t1.parent_id AS parentId,
			t2.content_name as parentName,
			t1.`level` AS level,
			t1.sort,
			t1.mark_code AS markCode,
			t1.term,
			t1.unit_name AS unitName,
			t1.major,
			t1.classify_mark AS classifyMark,
			t1.create_time AS createTime,
			t1.update_time AS updateTime,
			t1.create_user_id AS createUserId,
			t1.create_user_name AS createUserName,
			t1.update_user_id AS updateUserId,
			t1.update_user_name AS updateUserName
		FROM
            record_da_ky_files_contents t1
		left join record_da_ky_files_contents t2 on t1.parent_id = t2.id
		WHERE
			t1.id = #{id}
	</select>
	<select id="getchild" resultType="java.lang.Integer">
		SELECT
			count(t1.id)
		FROM
            record_da_ky_files_contents t1
				left join record_da_ky_files_contents t2 on t1.parent_id = t2.id
		WHERE
			t2.id = #{id} and t1.del_flag = '1'
	</select>
	<select id="getFilesClassifyMarkSelect" resultType="org.springblade.system.domain.SelectDto">
		SELECT
		id,
		classify_mark_name AS name
		FROM
            record_da_ky_files_classify_mark
		WHERE
		del_flag = '1'
	</select>
	<select id="geMaxSort" resultType="java.lang.Integer">
		select max(sort) from record_da_ky_files_contents where parent_id = #{parentId}
	</select>
	<select id="getArchivalCatalogueByName" resultType="java.lang.Integer">
		select count(*) from record_da_ky_files_contents where content_name = #{contentName}
		and parent_id = #{parentId} and del_flag = '1'
		<if test="id != null">
			and id != #{id}
		</if>
	</select>
	<select id="getSortById" resultType="java.lang.Integer">
		select sort from record_da_ky_files_contents where id = #{id}
	</select>
	<select id="getClassifyMark" resultType="java.lang.String">
		select dkfcm.classify_mark_name from record_da_ky_files_contents dkfc
		   left join record_da_ky_files_classify_mark dkfcm on dkfc.classify_mark = dkfcm.id
		where dkfc.id = #{id}
	</select>
	<select id="getLevelById" resultType="java.lang.Integer">
		select `level` from record_da_ky_files_contents where id = #{id}
	</select>

</mapper>
