<#assign _typeid="0"/>
<#if column?? && column.id?? && column.id?number gt 0>
	<#assign  _typeid="${column.id}">
	<#assign  selfid="${column.id}">
</#if>
<#if typeid??>
	<#assign  _typeid="${typeid}">
</#if>
select 
	@rownum := @rownum + 1 AS typeindex,
	id,
	id as typeid,
	category_title as typetitle,
	<#--返回父id集合-->
	category_parent_id as pids,
	<#--栏目选中的样式-->
	IF(<#if selfid?has_content>${selfid}<#else>${_typeid}</#if> = id ,"${class!''}","") as class,
	<#--动态链接-->
	<#if isDo?? && isDo>
	CONCAT("/${modelName}/list.do?typeid=", id) as typelink,
	<#else>
		<#--栏目类型为链接-->
		IF("3" = category_type,category_diy_url,CONCAT(category_path,"/index.html")) as typelink,
	</#if>
	category_keyword as typekeyword,
	category_diy_url as typeurl,
	category_flag as flag,
 category_parent_id as parentid,
category_descrip as typedescrip,
	( CASE category_img WHEN '' THEN category_img ELSE CONVERT ( JSON_UNQUOTE( JSON_EXTRACT( category_img -> '$[0]', '$.path' ) ) USING utf8 ) END ) as typelitpic ,
(select count(*) from cms_category c where c.category_id=typeid and c.del=0) as childsize
	from (SELECT @rownum := 0) r,cms_category  
	where 
	cms_category.del=0 
	<#--根据站点编号查询-->
	<#if appId?? >
		and cms_category.app_id=${appId}
	</#if>
	<#--栏目属性-->
	  <#if flag?? >
   and
	(		<#list flag?split(',') as item>
			<#if item?index gt 0> or</#if>
			FIND_IN_SET('${item}',category_flag)
			</#list>)
  </#if>

	<#if noflag?? >
      and
			(
			<#list noflag?split(',') as item>
			<#if item?index gt 0> and</#if>
			FIND_IN_SET('${item}',category_flag)=0
			</#list> or category_flag is null)
	</#if>
	<#--type默认son-->
<#if !type??||!type?has_content>
<#assign type="son"/>
</#if>
<#if type?has_content>
	<#--顶级栏目（单个）-->
	<#if type=="top">
	<#if _typeid != "0">
		and id=(SELECT IF(IF(LEFT ( category_parent_id, LOCATE( ",", category_parent_id ) - 1 ),LEFT ( category_parent_id, LOCATE( ",", category_parent_id ) - 1 ),category_parent_id),IF(LEFT ( category_parent_id, LOCATE( ",", category_parent_id ) - 1 ),LEFT ( category_parent_id, LOCATE( ",", category_parent_id ) - 1 ),category_parent_id),id)FROM cms_category WHERE id = ${_typeid})
		</#if>
	<#elseif type=="nav">
		and(category_id=0 or category_id is null)
	<#--同级栏目（多个）-->
	<#elseif type=="level">
		and
		<#if _typeid?has_content>
		 category_id=(select category_id from cms_category where id=${_typeid})
		<#else>
		 1=1
		</#if>
  	<#--当前栏目（单个）-->
	<#elseif type=="self">
		 and 
		 <#if _typeid?has_content>
		  id=${_typeid}
		 <#else>
		 1=1
		 </#if>
	<#--当前栏目的所属栏目（多个）-->
	<#elseif type=="path">
			and 
		 <#if _typeid?has_content>
		   id in (<#if column?? && column.categoryParentId??>${column.categoryParentId},</#if>${_typeid})
		 <#else>
		  1=1
		 </#if>
	<#--子栏目（多个）-->
	<#elseif type=="son">
			and 
		 <#if _typeid?has_content>
		  category_id=${_typeid}
		 <#else>
		  1=1
		 </#if>
		 <#--上一级栏目没有则取当前栏目（单个）-->
	<#elseif type=="parent">
			and 
		 <#if _typeid?has_content>
		   <#if column?? && column.categoryId??>
				id=${column.categoryId}
			 <#else>
			  id=${_typeid}
			 </#if>
		 <#else>
		  1=1
	</#if>
	<#--子栏目或同级栏目（多个）-->
	<#elseif type=="sonOrLevel">
		 and 
		 <#if _typeid?has_content>
	 	 category_id= if((SELECT count(*) FROM cms_category
		 WHERE category_id=${_typeid})>0,${_typeid},(select category_id from cms_category where id=${_typeid}))
		 <#else>
		 1=1
		 </#if>
	</#if>
<#else> <#--默认顶级栏目-->
	 and
	<#if _typeid?has_content>
	 id=${_typeid}
	<#else>
	 (category_id=0 or category_id is null)
	</#if>
</#if>
<#--字段排序-->
  <#if orderby?? >
    ORDER BY 
      <#if orderby=="date"> category_datetime
      <#elseif orderby=="sort"> category_sort
      <#else>cms_content.id</#if>
 <#else>
    ORDER BY   id
  </#if>
  <#if order?? >
      <#if order=="desc"> desc</#if>
      <#if order=="asc"> asc</#if>
  </#if>