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

	<resultMap type="ArticleClassify" id="AsArticleClassifyResult">
		<id     property="classifyId"     column="classify_id"     />
		<result property="parentId"   column="parent_id"   />
		<result property="deptId"   column="dept_id"   />
		<result property="deptName"   column="dept_name"   />
		<result property="ancestors"  column="ancestors"   />
		<result property="classifyName"   column="classify_name"   />
		<result property="orderNum"   column="order_num"   />
		<result property="status"     column="status"      />
		<result property="delFlag"    column="del_flag"    />
		<result property="parentName" column="parent_name" />
		<result property="createBy"   column="create_by"   />
		<result property="createTime" column="create_time" />
		<result property="updateBy"   column="update_by"   />
		<result property="updateTime" column="update_time" />
		<result property="agentId" column="agent_id" />
	</resultMap>

	<sql id="selectClassifyVo">
        select ac.classify_id, ac.dept_id, ac.parent_id, ac.ancestors, ac.classify_name, ac.order_num, ac.status, ac.del_flag, ac.create_by, ac.create_time,ac.agent_id
        from ss_article_classify ac
    </sql>

	<select id="selectClassifyList" parameterType="ArticleClassify" resultMap="AsArticleClassifyResult">
		select
		    ac.classify_id,
		    ac.dept_id,
		    ac.parent_id,
		    ac.ancestors,
		    ac.classify_name,
		    ac.order_num,
		    ac.status,
		    ac.del_flag,
		    ac.create_by,
		    ac.create_time,
		    ac.agent_id
		from ss_article_classify ac
        where (1 = 1
		<if test="classifyId != null and classifyId != 0">
			AND ac.classify_id = #{classifyId}
		</if>
        <if test="parentId != null and parentId != 0">
			AND ac.parent_id = #{parentId}
		</if>
		<if test="deptId != null and deptId != 0">
			AND ac.dept_id = #{deptId}
		</if>
		<if test="deptName != null and deptName != ''">
			AND ac.dept_name = #{deptName}
		</if>
		<if test="classifyName != null and classifyName != ''">
			AND ac.classify_name like concat('%', #{classifyName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND ac.status = #{status}
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		or (ac.classify_id = 111 or ac.parent_id = 111))
		and ac.del_flag = '0'
		order by ac.parent_id, ac.order_num
    </select>


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

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

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

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

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

	<select id="checkClassifyNameUnique" resultMap="AsArticleClassifyResult">
	    <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 ss_article_classify(
 			<if test="classifyId != null and classifyId != 0">classify_id,</if>
 			<if test="parentId != null and parentId != 0">parent_id,</if>
 			<if test="deptId != null and deptId != 0">dept_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="agentId != null">agent_id,</if>
 			create_time
 		)values(
 			<if test="classifyId != null and classifyId != 0">#{classifyId},</if>
 			<if test="parentId != null and parentId != 0">#{parentId},</if>
 			<if test="deptId != null and deptId != 0">#{deptId},</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="agentId != null">#{agentId},</if>
 			sysdate()
 		)
	</insert>

	<update id="updateClassify" parameterType="SysDept">
 		update ss_article_classify
 		<set>
 			<if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
 			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</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="agentId != null">agent_id = #{agentId},</if>
 			update_time = sysdate()
 		</set>
 		where classify_id = #{classifyId}
	</update>

	<update id="updateClassifyChildren" parameterType="java.util.List">
	    update ss_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 ss_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 ss_article_classify set del_flag = '2' where classify_id = #{classifyId}
	</delete>

</mapper>
