<#assign _typeid=""/>
<#assign _size="20"/>
<#if column?? && column.id?? && column.id?number gt 0>
  <#assign  _typeid="${column.id}">
</#if>
<#if typeid??>
  <#assign  _typeid="${typeid}">
</#if>
<#if size??>
  <#assign  _size="${size}">
</#if>
<#if orderby?? >
      <#if orderby=="date"> 
	   <#assign  _orderby="content_datetime">
      <#elseif orderby=="updatedate">
 <#assign  _orderby="content_updatetime">
      <#elseif orderby=="hit"> 
	  <#assign  _orderby="content_hit">
      <#elseif orderby=="sort">
	   <#assign  _orderby="content_sort">
      <#else><#assign  _orderby="cms_content.id"></#if>
   <#else>
   <#assign  _orderby="cms_content.id">
  </#if>
SELECT
  cms_content.id AS id,
  @rownum := @rownum + 1 AS `index`,
  LEFT (content_title, ${titlelen ?default(40)}) AS title,
  content_title AS fulltitle,
  content_author AS author,
  content_source AS source,
  content_details AS content,
  category.category_title AS typename,
  category.id AS typeid,
  ( CASE category.category_img WHEN '' THEN category.category_img ELSE CONVERT ( JSON_UNQUOTE( JSON_EXTRACT( category.category_img -> '$[0]', '$.path' ) ) USING utf8 ) END ) AS typelitpic,
  <#--列表页动态链接-->
  <#if isDo?? && isDo>
  CONCAT("/${modelName}/list.do?typeid=", category.category_id) as typelink,
  <#else>
  (SELECT "index.html") AS typelink,
  </#if>
  ( CASE content_img WHEN '' THEN content_img ELSE CONVERT ( JSON_UNQUOTE( JSON_EXTRACT( content_img -> '$[0]', '$.path' ) ) USING utf8 ) END ) AS litpic,
  <#--内容页动态链接-->
  <#if isDo?? && isDo>
   CONCAT("/${modelName}/view.do?id=", cms_content.id,"&orderby=${_orderby}","&order=${order!'ASC'}","&typeid=${typeid}") as link,
  <#else>

  CONCAT(category.category_path,"/",cms_content.id,".html") AS link,
  </#if>
  content_datetime AS date,<#if tableName??>${tableName}.*,</#if>
  content_description AS descrip,
  content_hit AS hit,
  content_type AS flag,
  category_title AS typetitle,
  cms_content.content_keyword AS keyword 
FROM
  (SELECT @rownum := 0) r,
  cms_content
  LEFT JOIN cms_category as category ON content_category_id = category.id
  <#--判断是否有自定义模型表-->
  <#if tableName??>LEFT JOIN ${tableName} ON ${tableName}.link_id=cms_content.id </#if>
WHERE  
  content_display=0 
	and cms_content.del=0 
  <#--根据站点编号查询-->
  <#if appId?? >
    and cms_content.app_id=${appId}
	and cms_content.id>0
  </#if>
  <#--判断是否有搜索分类集合-->
  <#if search??>
		<#if search.categoryIds??>and FIND_IN_SET(category.id,'${search.categoryIds}')</#if>
			<#--标题-->
			<#if search.content_title??> and content_title like CONCAT("%",'${search.content_title}',"%")</#if>
			<#--作者-->
			<#if search.content_author??> and content_author like CONCAT("%",'${search.content_author}',"%")</#if>
			<#--来源-->
			<#if search.content_source??> and content_source like CONCAT("%",'${search.content_source}',"%")</#if>
			<#--属性-->
			<#if search.content_type??> and  (
			<#list search.content_type?split(',') as item>
			<#if item?index gt 0> or</#if>
			FIND_IN_SET('${item}',cms_content.content_type)
			</#list>)</#if>
			<#--描述-->
			<#if search.content_description??> and content_description like CONCAT("%",'${search.content_description}',"%")</#if>
			<#--关键字-->
			<#if search.content_keyword??> and content_keyword like CONCAT("%",'${search.content_keyword}',"%")</#if>
			<#--内容-->
			<#if search.content_details??> and content_details like CONCAT("%",'${search.content_details}',"%")</#if>
			
<#--自定义顺序-->
			<#if search.content_sort??> and content_sort=${search.content_sort}</#if>		
<#--时间范围-->
			<#if search.content_datetime_start??&&search.content_datetime_end??> and content_datetime between '${search.content_datetime_start}' and '${search.content_datetime_end}'</#if>
  <#else><#--查询栏目-->
    <#if _typeid?has_content> and (content_category_id=${_typeid} or content_category_id in 
  (select id FROM cms_category where cms_category.del=0 and find_in_set(${_typeid},CATEGORY_PARENT_ID))) </#if>
 </#if>
  <#--标题-->
  <#if content_title??> and content_title like CONCAT("%",'${content_title}',"%")</#if>
  <#--作者-->
  <#if content_author??> and content_author like CONCAT("%",'${content_author}',"%")</#if>
  <#--来源-->
  <#if content_source??> and content_source like CONCAT("%",'${content_source}',"%")</#if>
  <#--属性-->
  <#if content_type??> and content_type like CONCAT("%",'${content_type}',"%")</#if>
  <#--描述-->
  <#if content_description??> and content_description like CONCAT("%",'${content_description}',"%")</#if>
  <#--关键字-->
  <#if content_keyword??> and content_keyword like CONCAT("%",'${content_keyword}',"%")</#if>
  <#--内容-->
  <#if content_details??> and content_details like CONCAT("%",'${content_details}',"%")</#if>
  <#--自定义顺序-->
  <#if content_sort??> and content_sort=${content_sort}</#if>
  <#--自定义模型-->
  <#if diyModel??> 
    <#list diyModel as dm>
      and ${tableName}.${dm.key} like CONCAT("%",'${dm.value}',"%") 
    </#list>
  </#if>
  <#--文章属性-->
  <#if flag?? >
			and(
			<#list flag?split(',') as item>
			<#if item?index gt 0> or</#if>
			FIND_IN_SET('${item}',cms_content.content_type)
			</#list>)
  </#if>
  <#if noflag??>
			and(
			<#list noflag?split(',') as item>
			<#if item?index gt 0> and</#if>
			FIND_IN_SET('${item}',cms_content.content_type)=0
			</#list> or cms_content.content_type is null)
  </#if>
  <#--字段排序-->
  <#if orderby?? >
    ORDER BY 
      <#if orderby=="date"> content_datetime
      <#elseif orderby=="updatedate"> content_updatetime
      <#elseif orderby=="hit"> content_hit
      <#elseif orderby=="sort"> content_sort
      <#else>cms_content.id</#if>
 <#else>
    ORDER BY   cms_content.id
  </#if>
  <#if order?? >
      <#if order=="desc"> desc</#if>
      <#if order=="asc"> asc</#if>
  </#if>
   LIMIT 
    <#--判断是否分页-->
  <#if ispaging?? && (pageTag.pageNo)??>${((pageTag.pageNo-1)*_size?eval)?c},${_size?default(20)}
  <#else>${_size?default(20)}</#if>