<?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="content_category_id" property="contentCategoryId" /><!--所属栏目 -->
				<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="app_id" property="appId" /><!--文章管理的应用id -->
				<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.ContentBean">
		<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="category_manager_id" property="categoryManagerId" /><!--发布用户id -->
		<result column="app_id" property="appId" /><!--应用编号 -->
		<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="contentCategoryId != null and contentCategoryId != ''">content_category_id,</if>
				<if test="contentType != null and contentType != ''">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="appId != null">app_id,</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="contentCategoryId != null and contentCategoryId != ''">#{contentCategoryId},</if>
				<if test="contentType != null and contentType != ''">#{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="appId != null">#{appId},</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="contentCategoryId != null and contentCategoryId != ''">content_category_id=#{contentCategoryId},</if>
				<if test="contentType != null and contentType != ''">content_type=#{contentType},</if>
				<if test="contentDisplay != null and contentDisplay != ''">content_display=#{contentDisplay},</if>
				<if test="contentAuthor != null and contentAuthor != ''">content_author=#{contentAuthor},</if>
				<if test="contentSource != null and contentSource != ''">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 and contentDescription != ''">content_description=#{contentDescription},</if>
				<if test="contentKeyword != null and contentKeyword != ''">content_keyword=#{contentKeyword},</if>
				<if test="contentDetails != null and contentDetails != ''">content_details=#{contentDetails},</if>
				<if test="contentUrl != null and contentUrl != ''">content_url=#{contentUrl},</if>
				<if test="contentHit != null">content_hit=#{contentHit},</if>
				<if test="appId != null">app_id=#{appId},</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}
		</select>

		<!--根据实体获取-->
		<select id="getByEntity" resultMap="resultMap" parameterType="net.mingsoft.cms.entity.ContentEntity">
			select * from cms_content
			<where>
				<if test="contentTitle != null and contentTitle != ''">and  content_title like CONCAT('%',#{contentTitle},'%')</if>
				<if test="contentCategoryId != null and contentCategoryId != ''">and content_category_id=#{contentCategoryId}</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="appId != null"> and app_id=#{appId} </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>
				<if test="del != null"> and del=#{del} </if>
			</where>
			limit 0,1
		</select>


		<!--删除-->
		<delete id="deleteEntity" parameterType="int">
			delete from cms_content  where id=#{id}
		</delete>

		<!--批量删除-->
		<delete id="delete" >
			delete from cms_content
			<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 order by id desc
		</select>
	<!--条件查询-->
	<select id="query" resultMap="resultMap">
		select * from cms_content
		<where>
			<if test="contentTitle != null and contentTitle != ''"> and  content_title like CONCAT('%',#{contentTitle},'%')</if>
			<if test="contentCategoryId != null and contentCategoryId != ''"> and content_category_id=#{contentCategoryId}</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="appId != null"> and app_id=#{appId} </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>
			<if test="del != null"> and del=#{del} </if>
			<include refid="net.mingsoft.base.dao.IBaseDao.sqlWhere"></include>
		</where>
		order by id 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
			cms_content.id article_id,c.*
			FROM cms_content
			LEFT JOIN cms_category c ON content_category_id = c.id
			where
			<if test="appId &gt; 0">
				cms_content.app_id = #{appId}
			</if>
			<!-- 查询子栏目数据 -->
			<if test="categoryId &gt; 0">
				and (content_category_id=#{categoryId} or content_category_id in
				(select id FROM cms_category where <include refid="queryWhereCategoryId"></include>))
			</if>
			<if test="beginTime!=null and beginTime!=''">
				and content_updatetime &gt;= #{beginTime}
			</if>
			<if test="endTime!=null and endTime!=''">
				and content_updatetime &gt;= #{endTime}
			</if>
			<if test="orderBy!=null and order!=null and orderBy!='' and order!=''">
				ORDER BY `${orderBy}` ${order}
			</if>

	</select>
	<select id="getSearchCount" resultType="int">
		select count(*) from
		cms_content a
		left join cms_category c
		ON a.content_category_id
		= c.id
		<if test="tableName!=null and tableName!=''">left join ${tableName} d on d.link_id=a.id
		</if>
		where a.app_id = #{websiteId}
		<if test="ids!=null and ids!=''">
			and FIND_IN_SET(content_category_id,'${ids}')
		</if>
		<foreach item="item" index="key" collection="map" open=""
				 separator="" close="">
			<if test=" item[0] == false">
				and ${key}
			</if>
			<if test="item[0]== true">
				and d.${key}
			</if>
			<!-- 数字类型的查找 item[1]:字段是否为数字类型false:数字 -->
			<if test="item[1] == false">
				<!--item[2]： 是采用等值还是区间查询。false:区间 itme[3][0]第一个值item[3][1]的二个值 -->
				<if test="item[2] == true">
					=
					<foreach item="val" index="index" collection="item[3]">#{val}
					</foreach>
				</if>
				<if test="item[2] == false">
					between
					<foreach item="val" index="index" collection="item[3]"
							 separator="and">
						#{val}
					</foreach>
				</if>
			</if>
			<!-- 字符型数据的查找 item[1]:字段是否为数字类型true:字符串 -->
			<if test="item[1] == true">
				<!--item[2]： 是采用模糊查询。false:不采用模糊 -->
				<if test="item[2] == true">
					<include refid="queryLike"></include>
				</if>
				<if test="item[2] == false">
					=
					<foreach item="val" index="index" collection="item[3]">#{val}
					</foreach>
				</if>
			</if>
		</foreach>
	</select>
	<!-- 根据字段条件查找文章实体开始 -->
	<sql id="queryLike" databaseId="mysql">
		like CONCAT("%",
		<foreach item="val" index="index" collection="item[3]">
			<if test="index==0">#{val}</if>
		</foreach>
		,"%")
	</sql>
	<sql id="queryLike" databaseId="oracle">
		like '%'||
		<foreach item="val" index="index" collection="item[3]">
			<if test="index==0">#{val}</if>
		</foreach>
		||'%'
	</sql>
	<sql id="queryLike" databaseId="sqlServer">
		like '%'+
		<foreach item="val" index="index" collection="item[3]">
			<if test="index==0">#{val}</if>
		</foreach>
		+'%'
	</sql>

</mapper>