<?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.ruoyi.ss.articleClassify.mapper.SmArticleClassifyMapper">

	<resultMap type="SmArticleClassify" id="SmArticleClassifyResult" autoMapping="true"/>

	<sql id="selectClassifyVo">
        select d.classify_id,
               d.parent_id,
               d.ancestors,
               d.classify_name,
               d.order_num,
               d.status,
               d.del_flag,
               d.create_by,
               d.create_time,
               d.built_system,
               d.picture
        from sm_article_classify d
    </sql>

	<select id="selectClassifyList" parameterType="SmArticleClassifyQuery" resultMap="SmArticleClassifyResult">
        <include refid="selectClassifyVo"/>
        where d.del_flag = '0'
		<if test="classifyId != null and classifyId != 0">
			AND classify_id = #{classifyId}
		</if>
        <if test="parentId != null and parentId != 0">
			AND parent_id = #{parentId}
		</if>
		<if test="classifyName != null and classifyName != ''">
			AND classify_name like concat('%', #{classifyName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND status = #{status}
		</if>
		<if test="keyword != null">
			AND (
            	d.classify_name LIKE concat('%', #{keyword}, '%')
            	OR d.classify_id IN (
            		SELECT DISTINCT classify_id
            		FROM sm_article sa
            		WHERE sa.title LIKE concat('%', #{keyword}, '%')
            		OR sa.introduction LIKE concat('%', #{keyword}, '%')
					OR sa.content LIKE concat('%', #{keyword}, '%')
				)
            )
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		order by d.parent_id, d.order_num
    </select>


    <select id="selectClassifyById" parameterType="Long" resultMap="SmArticleClassifyResult">
		select
		    d.classify_id,
		       d.parent_id,
		       d.ancestors,
		       d.classify_name,
		       d.order_num,
		       d.status,
		       d.built_system,
				(select classify_name from sm_article_classify where classify_id = d.parent_id) parent_name
		from sm_article_classify d
		where d.classify_id = #{classifyId}
	</select>

    <select id="checkClassifyExistArticle" parameterType="Long" resultType="int">
		select count(1) from sm_article where classify_id = #{classifyId} and del_flag = '0'
	</select>

	<select id="hasChildByClassifyId" parameterType="Long" resultType="int">
		select count(1) from sm_article_classify
		where del_flag = '0' and parent_id = #{classifyId} limit 1
	</select>

	<select id="selectChildrenClassifyById" parameterType="Long" resultMap="SmArticleClassifyResult">
		select * from sm_article_classify where find_in_set(#{classifyId}, ancestors)
	</select>

	<select id="selectNormalChildrenClassifyById" parameterType="Long" resultType="int">
		select count(*) from sm_article_classify where status = 0 and del_flag = '0' and find_in_set(#{classifyId}, ancestors)
	</select>

	<select id="checkClassifyNameUnique" resultMap="SmArticleClassifyResult">
	    <include refid="selectClassifyVo"/>
		where classify_name=#{classifyName} and parent_id = #{parentId} and del_flag = '0' limit 1
	</select>

    <insert id="insertClassify" parameterType="SysDept">
 		insert into sm_article_classify(
 			<if test="classifyId != null and classifyId != 0">classify_id,</if>
 			<if test="parentId != null and parentId != 0">parent_id,</if>
 			<if test="classifyName != null and classifyName != ''">classify_name,</if>
 			<if test="ancestors != null and ancestors != ''">ancestors,</if>
 			<if test="orderNum != null">order_num,</if>
 			<if test="status != null">status,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
			<if test="builtSystem != null">built_system,</if>
			<if test="picture != null">picture,</if>
 			create_time
 		)values(
 			<if test="classifyId != null and classifyId != 0">#{classifyId},</if>
 			<if test="parentId != null and parentId != 0">#{parentId},</if>
 			<if test="classifyName != null and classifyName != ''">#{classifyName},</if>
 			<if test="ancestors != null and ancestors != ''">#{ancestors},</if>
 			<if test="orderNum != null">#{orderNum},</if>
 			<if test="status != null">#{status},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
			<if test="builtSystem != null">#{builtSystem},</if>
			<if test="picture != null">#{picture},</if>
 			sysdate()
 		)
	</insert>

	<update id="updateClassify" parameterType="SysDept">
 		update sm_article_classify
 		<set>
 			<if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
 			<if test="classifyName != null and classifyName != ''">classify_name = #{classifyName},</if>
 			<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
 			<if test="orderNum != null">order_num = #{orderNum},</if>
 			<if test="status != null and status != ''">`status` = #{status},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
			<if test="builtSystem != null">built_system = #{builtSystem},</if>
			<if test="picture != null">picture = #{picture},</if>
 			update_time = sysdate()
 		</set>
 		where classify_id = #{classifyId}
	</update>

	<update id="updateClassifyChildren" parameterType="java.util.List">
	    update sm_article_classify set ancestors =
	    <foreach collection="classifyIds" item="item" index="index"
	        separator=" " open="case classify_id" close="end">
	        when #{item.classifyId} then #{item.ancestors}
	    </foreach>
	    where classify_id in
	    <foreach collection="classifyIds" item="item" index="index"
	        separator="," open="(" close=")">
	        #{item.classifyId}
	    </foreach>
	</update>

	<update id="updateClassifyStatusNormal" parameterType="Long">
 	    update sm_article_classify set status = '0' where classify_id in
 	    <foreach collection="array" item="classifyId" open="(" separator="," close=")">
        	#{classifyId}
        </foreach>
	</update>

	<delete id="deleteClassifyById" parameterType="Long">
		update sm_article_classify set del_flag = '2' where classify_id = #{classifyId}
	</delete>

</mapper>
