<?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="net.mingsoft.cms.dao.IContentDao">

	<resultMap id="resultMap" type="net.mingsoft.cms.entity.ContentEntity">
			<id column="id" property="id" /><!--编号 -->
				<result column="content_title" property="contentTitle" /><!--文章标题 -->
				<result column="category_id" property="categoryId" /><!--所属栏目 -->
				<result column="content_type" property="contentType" /><!--文章类型 -->
				<result column="content_display" property="contentDisplay" /><!--是否显示 -->
				<result column="content_author" property="contentAuthor" /><!--文章作者 -->
				<result column="content_source" property="contentSource" /><!--文章来源 -->
				<result column="content_datetime" property="contentDatetime" /><!--发布时间 -->
				<result column="content_sort" property="contentSort" /><!--自定义顺序 -->
				<result column="content_img" property="contentImg" /><!--文章缩略图 -->
				<result column="content_description" property="contentDescription" /><!--描述 -->
				<result column="content_keyword" property="contentKeyword" /><!--关键字 -->
				<result column="content_details" property="contentDetails" /><!--文章内容 -->
				<result column="content_url" property="contentUrl" /><!--文章跳转链接地址 -->
				<result column="content_hit" property="contentHit" /><!--点击次数 -->
				<result column="create_by" property="createBy" /><!--创建人 -->
				<result column="create_date" property="createDate" /><!--创建时间 -->
				<result column="update_by" property="updateBy" /><!--修改人 -->
				<result column="update_date" property="updateDate" /><!--修改时间 -->
				<result column="del" property="del" /><!--删除标记 -->
	</resultMap>

	<resultMap id="resultContentMap" type="net.mingsoft.cms.bean.ContentBean">
		<id column="id" property="id" /><!--编号 -->
		<result column="content_title" property="contentTitle" /><!--文章标题 -->
		<result column="category_id" property="categoryId" /><!--所属栏目 -->
		<result column="content_type" property="contentType" /><!--文章类型 -->
		<result column="content_display" property="contentDisplay" /><!--是否显示 -->
		<result column="content_author" property="contentAuthor" /><!--文章作者 -->
		<result column="content_source" property="contentSource" /><!--文章来源 -->
		<result column="content_datetime" property="contentDatetime" /><!--发布时间 -->
		<result column="content_sort" property="contentSort" /><!--自定义顺序 -->
		<result column="content_img" property="contentImg" /><!--文章缩略图 -->
		<result column="content_description" property="contentDescription" /><!--描述 -->
		<result column="content_keyword" property="contentKeyword" /><!--关键字 -->
		<result column="content_details" property="contentDetails" /><!--文章内容 -->
		<result column="content_url" property="contentUrl" /><!--文章跳转链接地址 -->
		<result column="static_url" property="staticUrl" /><!--静态地址 -->
		<result column="content_hit" property="contentHit" /><!--点击次数 -->
		<result column="create_by" property="createBy" /><!--创建人 -->
		<result column="create_date" property="createDate" /><!--创建时间 -->
		<result column="update_by" property="updateBy" /><!--修改人 -->
		<result column="update_date" property="updateDate" /><!--修改时间 -->
		<result column="del" property="del" /><!--删除标记 -->
	</resultMap>

	<resultMap id="resultBean" type="net.mingsoft.cms.bean.CategoryBean">
		<id column="id" property="id" /><!--编号 -->
		<id column="article_Id" property="articleId" /><!--编号 -->
		<result column="category_title" property="categoryTitle" /><!--栏目管理名称 -->
		<result column="category_id" property="categoryId" /><!--所属栏目 -->
		<result column="category_type" property="categoryType" /><!--栏目管理属性 -->
		<result column="category_sort" property="categorySort" /><!--自定义顺序 -->
		<result column="category_list_url" property="categoryListUrl" /><!--列表模板 -->
		<result column="category_url" property="categoryUrl" /><!--内容模板 -->
		<result column="category_keyword" property="categoryKeyword" /><!--栏目管理关键字 -->
		<result column="category_descrip" property="categoryDescrip" /><!--栏目管理描述 -->
		<result column="category_img" property="categoryImg" /><!--缩略图 -->
		<result column="category_diy_url" property="categoryDiyUrl" /><!--自定义链接 -->
		<result column="mdiy_model_id" property="mdiyModelId" /><!--栏目管理的内容模型id -->
		<result column="category_datetime" property="categoryDatetime" /><!--类别发布时间 -->
		<result column="dict_id" property="dictId" /><!--字典对应编号 -->
		<result column="category_flag" property="categoryFlag" /><!--栏目属性 -->
		<result column="category_path" property="categoryPath" /><!--栏目路径 -->
		<result column="category_parent_id" property="categoryParentId" /><!--父类型编号 -->
		<result column="create_by" property="createBy" /><!--创建人 -->
		<result column="create_date" property="createDate" /><!--创建时间 -->
		<result column="update_by" property="updateBy" /><!--修改人 -->
		<result column="update_date" property="updateDate" /><!--修改时间 -->
		<result column="del" property="del" /><!--删除标记 -->
	</resultMap>

	<!--保存-->
	<insert id="saveEntity" useGeneratedKeys="true" keyProperty="id"
			parameterType="net.mingsoft.cms.entity.ContentEntity" >
		insert into cms_content
		<trim prefix="(" suffix=")" suffixOverrides=",">
				<if test="contentTitle != null and contentTitle != ''">content_title,</if>
				<if test="categoryId != null and categoryId != ''">category_id,</if>
				<if test="contentType != null ">content_type,</if>
				<if test="contentDisplay != null and contentDisplay != ''">content_display,</if>
				<if test="contentAuthor != null and contentAuthor != ''">content_author,</if>
				<if test="contentSource != null and contentSource != ''">content_source,</if>
				<if test="contentDatetime != null">content_datetime,</if>
				<if test="contentSort != null">content_sort,</if>
				<if test="contentImg != null and contentImg != ''">content_img,</if>
				<if test="contentDescription != null and contentDescription != ''">content_description,</if>
				<if test="contentKeyword != null and contentKeyword != ''">content_keyword,</if>
				<if test="contentDetails != null and contentDetails != ''">content_details,</if>
				<if test="contentUrl != null and contentUrl != ''">content_url,</if>
				<if test="contentHit != null">content_hit,</if>
				<if test="createBy &gt; 0">create_by,</if>
				<if test="createDate != null">create_date,</if>
				<if test="updateBy &gt; 0">update_by,</if>
				<if test="updateDate != null">update_date,</if>
				<if test="del != null">del,</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
				<if test="contentTitle != null and contentTitle != ''">#{contentTitle},</if>
				<if test="categoryId != null and categoryId != ''">#{categoryId},</if>
				<if test="contentType != null ">#{contentType},</if>
				<if test="contentDisplay != null and contentDisplay != ''">#{contentDisplay},</if>
				<if test="contentAuthor != null and contentAuthor != ''">#{contentAuthor},</if>
				<if test="contentSource != null and contentSource != ''">#{contentSource},</if>
				<if test="contentDatetime != null">#{contentDatetime},</if>
				<if test="contentSort != null">#{contentSort},</if>
				<if test="contentImg != null and contentImg != ''">#{contentImg},</if>
				<if test="contentDescription != null and contentDescription != ''">#{contentDescription},</if>
				<if test="contentKeyword != null and contentKeyword != ''">#{contentKeyword},</if>
				<if test="contentDetails != null and contentDetails != ''">#{contentDetails},</if>
				<if test="contentUrl != null and contentUrl != ''">#{contentUrl},</if>
				<if test="contentHit != null">#{contentHit},</if>
				<if test="createBy &gt; 0">#{createBy},</if>
				<if test="createDate != null">#{createDate},</if>
				<if test="updateBy &gt; 0">#{updateBy},</if>
				<if test="updateDate != null">#{updateDate},</if>
				<if test="del != null">#{del},</if>
		</trim>
	</insert>

		<!--更新-->
		<update id="updateEntity" parameterType="net.mingsoft.cms.entity.ContentEntity">
			update cms_content
			<set>
				<if test="contentTitle != null and contentTitle != ''">content_title=#{contentTitle},</if>
				<if test="categoryId != null and categoryId != ''">category_id=#{categoryId},</if>
				<if test="contentType != null ">content_type=#{contentType},</if>
				<if test="contentDisplay != null and contentDisplay != ''">content_display=#{contentDisplay},</if>
				<if test="contentAuthor != null ">content_author=#{contentAuthor},</if>
				<if test="contentSource != null ">content_source=#{contentSource},</if>
				<if test="contentDatetime != null">content_datetime=#{contentDatetime},</if>
				<if test="contentSort != null">content_sort=#{contentSort},</if>
				<if test="contentImg != null and contentImg != ''">content_img=#{contentImg},</if>
				<if test="contentDescription != null ">content_description=#{contentDescription},</if>
				<if test="contentKeyword != null ">content_keyword=#{contentKeyword},</if>
				<if test="contentDetails != null ">content_details=#{contentDetails},</if>
				<if test="contentUrl != null and contentUrl != ''">content_url=#{contentUrl},</if>
				<if test="contentHit != null">content_hit=#{contentHit},</if>
				<if test="createBy &gt; 0">create_by=#{createBy},</if>
				<if test="createDate != null">create_date=#{createDate},</if>
				<if test="updateBy &gt; 0">update_by=#{updateBy},</if>
				<if test="updateDate != null">update_date=#{updateDate},</if>
				<if test="del != null">del=#{del},</if>
			</set>
			where id = #{id}
		</update>

		<!--根据id获取-->
		<select id="getEntity" resultMap="resultMap" parameterType="int">
			select * from cms_content where id=#{id} and del=0
		</select>

		<!--根据实体获取-->
		<select id="getByEntity" resultMap="resultMap" parameterType="net.mingsoft.cms.entity.ContentEntity">
			select * from cms_content
			<where>
				del=0
				<if test="contentTitle != null and contentTitle != ''">and  content_title like CONCAT('%',#{contentTitle},'%')</if>
				<if test="categoryId != null and categoryId != ''">and category_id=#{categoryId}</if>
				<if test="contentType != null and contentType != ''">and content_type=#{contentType}</if>
				<if test="contentDisplay != null and contentDisplay != ''">and content_display=#{contentDisplay}</if>
				<if test="contentAuthor != null and contentAuthor != ''">and content_author=#{contentAuthor}</if>
				<if test="contentSource != null and contentSource != ''">and content_source=#{contentSource}</if>
				<if test="contentDatetime != null"> and content_datetime=#{contentDatetime} </if>
				<if test="contentSort != null"> and content_sort=#{contentSort} </if>
				<if test="contentImg != null and contentImg != ''">and content_img=#{contentImg}</if>
				<if test="contentDescription != null and contentDescription != ''">and content_description=#{contentDescription}</if>
				<if test="contentKeyword != null and contentKeyword != ''">and content_keyword=#{contentKeyword}</if>
				<if test="contentDetails != null and contentDetails != ''">and content_details=#{contentDetails}</if>
				<if test="contentUrl != null and contentUrl != ''">and content_url=#{contentUrl}</if>
				<if test="contentHit != null">and content_hit=#{contentHit}</if>
				<if test="createBy &gt; 0"> and create_by=#{createBy} </if>
				<if test="createDate != null"> and create_date=#{createDate} </if>
				<if test="updateBy &gt; 0"> and update_by=#{updateBy} </if>
				<if test="updateDate != null"> and update_date=#{updateDate} </if>
			</where>
			limit 0,1
		</select>


		<!--删除-->
		<delete id="deleteEntity" parameterType="int">
			update  cms_content set del=1  where id=#{id}
		</delete>

		<!--删除-->
		<delete id="deleteEntityByCategoryIds" >
			update  cms_content set del=1
			<where>
				category_id  in <foreach collection="ids" item="item" index="index"
							open="(" separator="," close=")">#{item}</foreach>
			</where>
		</delete>

		<!--批量删除-->
		<delete id="delete" >
			update  cms_content set del=1
			<where>
				id  in <foreach collection="ids" item="item" index="index"
										 open="(" separator="," close=")">#{item}</foreach>
			</where>
		</delete>
		<!--查询全部-->
		<select id="queryAll" resultMap="resultMap">
			select * from cms_content where del=0 order by id desc
		</select>
	<!--条件查询-->
	<select id="query" resultMap="resultContentMap">
		select ct.*,CONCAT('/html/',ct.app_id,category_path,'/',ct.id,'.html') AS static_url from (
		select ct.*,cc.category_path from cms_content ct
		join cms_category cc on ct.category_id=cc.id
		<where>
			ct.del=0
			<if test="contentTitle != null and contentTitle != ''"> and  content_title like CONCAT('%',#{contentTitle},'%')</if>
			<if test="categoryId != null and categoryId != ''"> 	and (ct.category_id=#{categoryId} or ct.category_id in
				(select id FROM cms_category where <include refid="queryWhereCategoryId"></include>))</if>
			<if test="contentType != null and contentType != ''"> and content_type LIKE CONCAT('%',#{contentType},'%')</if>
			<if test="contentDisplay != null and contentDisplay != ''"> and content_display=#{contentDisplay}</if>
			<if test="contentAuthor != null and contentAuthor != ''"> and content_author=#{contentAuthor}</if>
			<if test="contentSource != null and contentSource != ''"> and content_source=#{contentSource}</if>
			<if test="contentDatetime != null"> and content_datetime=#{contentDatetime} </if>
			<if test="contentSort != null"> and content_sort=#{contentSort} </if>
			<if test="contentImg != null and contentImg != ''"> and content_img=#{contentImg}</if>
			<if test="contentDescription != null and contentDescription != ''"> and content_description=#{contentDescription}</if>
			<if test="contentKeyword != null and contentKeyword != ''"> and content_keyword=#{contentKeyword}</if>
			<if test="contentDetails != null and contentDetails != ''"> and content_details=#{contentDetails}</if>
			<if test="contentUrl != null and contentUrl != ''"> and content_url=#{contentUrl}</if>
			<if test="contentHit != null"> and content_hit=#{contentHit}</if>
			<if test="createBy &gt; 0"> and ct.create_by=#{createBy} </if>
			<if test="createDate != null"> and ct.create_date=#{createDate} </if>
			<if test="updateBy &gt; 0"> and ct.update_by=#{updateBy} </if>
			<if test="updateDate != null"> and update_date=#{updateDate} </if>
		</where>
		)ct ORDER BY ct.content_datetime desc,content_sort desc
	</select>

	<sql id="queryWhereCategoryId" databaseId="mysql">
		find_in_set('${categoryId}',CATEGORY_PARENT_ID)
	</sql>
	<sql id="queryWhereCategoryId" databaseId="oracle" >
		instr(','||'${categoryId}'||',', ','||CATEGORY_PARENT_ID||',')>0
	</sql>
	<sql id="queryWhereCategoryId" databaseId="sqlServer">
		CHARINDEX(','+'${categoryId}'+',' , ','+CATEGORY_PARENT_ID +',')>0
	</sql>

	<!-- 根据站点编号、开始、结束时间和栏目编号查询文章编号集合 -->
	<select id="queryIdsByCategoryIdForParser" resultMap="resultBean" >
			select
			ct.id article_id,c.*
			FROM cms_content ct
			LEFT JOIN cms_category c ON ct.category_id = c.id
			where ct.del=0

			<!-- 查询子栏目数据 -->
			<if test="categoryId &gt; 0">
				and (ct.category_id=#{categoryId} or ct.category_id in
				(select id FROM cms_category where <include refid="queryWhereCategoryId"></include>))
			</if>
			<if test="beginTime!=null and beginTime!=''">
				and content_datetime &gt;= #{beginTime}
			</if>
			<if test="endTime!=null and endTime!=''">
				and content_datetime &gt;= #{endTime}
			</if>
			<if test="flag!=null and flag!=''">
			and ct.content_type in ( #{flag})
			</if>
			<if  test="noflag!=null and noflag!=''">
			and (ct.content_type not in ( #{noflag}  ) or ct.content_type is null)
			</if>
			<if test="orderBy!=null  and orderBy!='' ">
				<if test="orderBy=='date'">ORDER BY content_datetime</if>
				<if test="orderBy=='hit'">ORDER BY content_hit</if>
				<if test="orderBy=='sort'">ORDER BY content_sort</if>
				<if  test="orderBy!='date' and orderBy!='hit' and orderBy!='sort'">
					ORDER BY ct.id
				</if>
				<choose>
					<when test="order!=null and order!=''">
						${order}
					</when>
					<otherwise>
						desc
					</otherwise>
				</choose>
			</if>

	</select>
	<select id="getSearchCount" resultType="int">
		select count(*) from
		cms_content a
		left join cms_category c
		ON a.category_id
		= c.id
		<if test="tableName!=null and tableName!='' and diyMap!=null">left join ${tableName} d on d.link_id=a.id
		</if>
		<where>
			a.del=0
			<if test="ids!=null and ids!=''">
				and FIND_IN_SET(a.category_id,#{ids})
			</if>
			<if test="map.content_title!=null">
			and a.content_title like CONCAT("%",#{map.content_title},"%")
			</if>
			<if test="map.content_author!=null">
			and a.content_author like CONCAT("%",#{map.content_author},"%")
			</if>
			<if test="map.content_source!=null">
			and a.content_source like CONCAT("%",#{map.content_source},"%")
			</if>
			<if test="map.content_type!=null">
				and <foreach item="item" index="index" collection="map.content_type.split(',')"  open="(" separator="or" close=")">
					FIND_IN_SET('${item}',a.content_type)
				</foreach>
			</if>
			<if test="map.content_description!=null">
			and a.content_description like CONCAT("%",#{map.content_description},"%")
			</if>
			<if test="map.content_keyword!=null">
			and a.content_keyword like CONCAT("%",#{map.content_keyword},"%")
			</if>
			<if test="map.content_details!=null">
			and a.content_details like CONCAT("%",#{map.content_details},"%")
			</if>
			<if test="map.content_datetime_start!=null and map.content_datetime_end!=null">
				and a.content_datetime between #{map.content_datetime_start} and #{map.content_datetime_end}
			</if>
			<if test="tableName!=null and tableName!='' and diyMap!=null">
				<foreach item="item" index="index" collection="diyList" open=""
						 separator="" close="">
					and d.${field.key} like CONCAT("%",#{item.value},"%")
				</foreach>
			</if>
		</where>

	</select>


</mapper>
