<?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.ProjectMapper">
	<insert id="insertDakyProFilesContents">
		INSERT INTO da_ky_pro_files_contents
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null and id != ''">id,</if>
			<if test="proId != null and proId != ''">pro_id,</if>
			<if test="contentName != null and contentName != ''">content_name,</if>
			<if test="parentId != null and parentId != ''">parent_id,</if>
			<if test="level != null and level != ''">`level`,</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="dataSource != null and dataSource != ''">data_source,</if>
			<if test="isUnique != null and isUnique != ''">is_unique,</if>
			<if test="integrityStatus != null and integrityStatus != ''">integrity_status,</if>
			<if test="createUserName != null and createUserName != ''">create_user_name,</if>
			<if test="updateUserName != null and updateUserName != ''">update_user_name,</if>
			<if test="delFlag != null and delFlag != ''">del_flag,</if>
			<if test="sort != null">sort,</if>
			<if test="createUserId != null">create_user_id,</if>
			<if test="updateUserId != null">update_user_id,</if>
		    create_time,
		</trim>
		<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
			<if test="id != null and id != ''">#{id},</if>
			<if test="proId != null and proId != ''">#{proId},</if>
			<if test="contentName != null and contentName != ''">#{contentName},</if>
			<if test="parentId != null and parentId != ''">#{parentId},</if>
			<if test="level != null and level != ''">#{level},</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="dataSource != null and dataSource != ''">#{dataSource},</if>
			<if test="isUnique != null and isUnique != ''">#{isUnique},</if>
			<if test="integrityStatus != null and integrityStatus != ''">#{integrityStatus},</if>
			<if test="createUserName != null and createUserName != ''">#{createUserName},</if>
			<if test="updateUserName != null and updateUserName != ''">#{updateUserName},</if>
			<if test="delFlag != null and delFlag != ''">#{delFlag},</if>
			<if test="sort != null">#{sort},</if>
			<if test="createUserId != null">#{createUserId},</if>
			<if test="updateUserId != null">#{updateUserId},</if>
			now()
		</trim>
	</insert>
	<update id="updateContentsName">
		UPDATE da_ky_project
		SET
			contents_name = #{contentsName}
		WHERE
			id = #{proId}
	</update>
	<update id="updateFileStatus">
		UPDATE da_ky_project
		SET
			file_status = '1'
		WHERE
			id = #{proId}
	</update>

	<select id="list" resultType="org.springblade.system.domain.ProjectDto">
		SELECT
			dkp.*,
			dksdd.dict_label proTypeName,
			dksdd2.dict_label voltageLevelName,
			dksdd3.dict_label proStatusName
		FROM
			da_ky_project dkp
				LEFT JOIN da_ky_sys_dict_data dksdd ON dkp.pro_type = dksdd.dict_value and dksdd.dict_type = 'pro_type'
				LEFT JOIN da_ky_sys_dict_data dksdd2 ON dkp.voltage_level = dksdd2.dict_value and dksdd2.dict_type = 'voltage_level'
				LEFT JOIN da_ky_sys_dict_data dksdd3 ON dkp.pro_status = dksdd3.dict_value and dksdd3.dict_type = 'pro_status'
		WHERE
			dkp.del_flag = '1'
		<if test="singleProName != null and singleProName != ''">
			AND INSTR(dkp.single_pro_name, #{singleProName}) > 0
		</if>
		<if test="proType != null and proType != ''">
			AND dkp.pro_type = #{proType}
		</if>
		<if test="voltageLevel != null and voltageLevel != ''">
			AND dkp.voltage_level = #{voltageLevel}
		</if>
		<if test="proStatus != null and proStatus != ''">
			AND dkp.pro_status =  #{proStatus}
		</if>

	</select>
	<select id="getFileCatalogSelect" resultType="org.springblade.system.domain.SelectDto">
		SELECT
			id,
			content_name AS name
		FROM
			da_ky_files_contents
		WHERE
			del_flag = '1' and level = 1
	</select>
	<select id="getfilesContentsById" resultType="org.springblade.system.domain.ArchivalCatalogueDto">
		WITH RECURSIVE child_nodes AS (
			SELECT
				id AS id,
				content_name AS contentName,
				parent_id AS parentId,
				level AS level,
				sort AS sort,
				mark_code AS markCode,
				term AS term,
				unit_name AS unitName,
				major AS major,
				classify_mark AS classifyMark,
				create_time AS createTime,
				update_time AS updateTime,
				create_user_id AS createUserId,
				create_user_name AS createUserName,
				update_user_id AS updateUserId,
				update_user_name AS updateUserName,
				del_flag AS delFlag
			FROM da_ky_files_contents
			WHERE id = #{id}

			UNION ALL

			SELECT
				t.id AS id,
				t.content_name AS contentName,
				t.parent_id AS parentId,
				t.level AS level,
				t.sort AS sort,
				t.mark_code AS markCode,
				t.term AS term,
				t.unit_name AS unitName,
				t.major AS major,
				t.classify_mark AS classifyMark,
				t.create_time AS createTime,
				t.update_time AS updateTime,
				t.create_user_id AS createUserId,
				t.create_user_name AS createUserName,
				t.update_user_id AS updateUserId,
				t.update_user_name AS updateUserName,
				t.del_flag AS delFlag
			FROM da_ky_files_contents t
					 INNER JOIN child_nodes c ON t.parent_id = c.id
			WHERE t.del_flag = '1'
		)
		SELECT * FROM child_nodes ORDER BY id asc
	</select>
</mapper>
