<?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.thinkgem.jeesite.modules.cms.dao.ArticleDao">
    
	<sql id="cmsArticleColumns">
		a.id AS "id",
		a.category_id AS "category.id",
		a.title AS "title",
		a.link AS "link",
		a.color AS "color",
		a.image AS "image",
		a.keywords AS "keywords",
		a.description AS "description",
		a.weight AS "weight",
		a.weight_date AS "weightDate",
		a.hits AS "hits",
		a.posid AS "posid",
		a.custom_content_view AS "customContentView",
		a.view_config AS "viewConfig",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		c.name AS "category.name",
		u.name AS "user.name"
	</sql>
	
	<sql id="cmsArticleJoins">
		JOIN cms_category c ON c.id = a.category_id
		JOIN sys_office o ON o.id = c.office_id
		JOIN sys_user u ON u.id = a.create_by
	</sql>
    
	<select id="get" resultType="Article">
		SELECT 
			<include refid="cmsArticleColumns"/>
		FROM cms_article a
		<include refid="cmsArticleJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="Article">
		SELECT 
			<include refid="cmsArticleColumns"/>
		FROM cms_article a
		<include refid="cmsArticleJoins"/>
		<where>
			a.del_flag = #{delFlag}
			<if test="title != null and title != ''">
				AND a.title LIKE 
					<if test="dbName == 'oracle'">'%'||#{title}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{title}+'%'</if>
					<if test="dbName == 'mysql'">CONCAT('%', #{title}, '%')</if>
			</if>
			<if test="posid != null and posid != ''">
				AND a.posid LIKE 
					<if test="dbName == 'oracle'">'%'||#{posid}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{posid}+'%'</if>
					<if test="dbName == 'mysql'">CONCAT('%', #{posid}, '%')</if>
			</if>
			<if test="category.id != null and category.id != ''">
				AND (a.category_id = #{category.id}
				<if test="category.parentIds != null and category.parentIds != ''">
					or c.parent_ids like 
						<if test="dbName == 'oracle'">'%'||#{category.id}||'%'</if>
						<if test="dbName == 'mssql'">'%,'+#{category.id}+',%'</if>
						<if test="dbName == 'mysql'">CONCAT('%,', #{category.id}, ',%')</if>
				</if>)
			</if>
			<if test="image != null and image != ''">
				AND a.image  = #{image}
			</if>
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by  = #{createBy.id}
			</if>
			<!-- ${sqlMap.dsf}-->
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.weight DESC, a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="Article">
		SELECT 
			<include refid="cmsArticleColumns"/>
		FROM cms_article a
		<include refid="cmsArticleJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.weight DESC, a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO cms_article(
			id,
			category_id,
			title,
			link,
			color,
			image,
			keywords,
			description,
			weight,
			weight_date,
			hits,
			posid,
			custom_content_view,
			view_config,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{category.id},
			#{title},
			#{link},
			#{color},
			#{image},
			#{keywords},
			#{description},
			#{weight},
			#{weightDate},
			#{hits},
			#{posid},
			#{customContentView},
			#{viewConfig},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE cms_article SET 	
			category_id = #{category.id},
			title = #{title},
			link = #{link},
			color = #{color},
			image = #{image},
			keywords = #{keywords},
			description = #{description},
			weight = #{weight},
			weight_date = #{weightDate},
			hits = #{hits},
			posid = #{posid},
			custom_content_view = #{customContentView},
			view_config = #{viewConfig},
			create_date = #{createDate},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			del_flag = #{delFlag}
		WHERE id = #{id}
	</update>
	
	<update id="delete">
		UPDATE cms_article SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<select id="findByIdIn" resultType="Article">
		SELECT 
			<include refid="cmsArticleColumns"/>
		from cms_article a where 
		<where>
			id in (${id});
		</where>		
	</select>
 
	<update id="updateExpiredWeight">
		update cms_article SET
			 weight = 0
		WHERE weight &gt; 0 AND weight_date &lt; 
			<if test="dbName == 'oracle'">sysdate</if>
			<if test="dbName == 'mssql'">FLOOR(CONVERT(FLOAT,GETDATE()))</if>
			<if test="dbName == 'mysql'">CURDATE()</if>
	</update>
	
	<update id="updateHitsAddOne">
		update cms_article set
			 hits = hits+1
		 WHERE id = #{id}
	</update>
	
	<select id="findStats" resultType="Category">
		select max(c.id) AS "id",
		       max(c.name) AS "name",
		       max(cp.id) AS "parent.id",
		       max(cp.name) AS "parent.name",
		       count(*) AS "cnt",
		       sum(a.hits) AS "hits",
		       max(a.update_date) as "updateDate",
		       max(o.id) AS "office.id",
		       max(o.name) AS "office.name"
		  FROM cms_article a
		  RIGHT JOIN cms_category c ON c.id = a.category_id
		  JOIN cms_category cp ON c.parent_id = cp.id
		  JOIN sys_office o ON o.id = c.office_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} 
			AND c.site_id = #{site.id}
			<if test="office.id != null and office.id != ''">
				AND (c.office_id = #{office.id} OR o.parent_ids like 
					<if test="dbName == 'oracle'">'%'||#{office.id}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{office.id}+'%'</if>
					<if test="dbName == 'mysql'">CONCAT('%', #{office.id}, '%')</if>)
			</if>
			<if test="beginDate != null and beginDate != ''">
				AND a.update_date <![CDATA[ >= #{beginDate} ]]>
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.update_date <![CDATA[ <= #{endDate} ]]>
			</if>
			<if test="id != null and id != ''">
				AND (c.id = #{id} OR c.parent_ids LIKE 
					<if test="dbName == 'oracle'">'%'||#{id}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{id}+'%'</if>
					<if test="dbName == 'mysql'">CONCAT('%', #{id}, '%')</if>)
			</if>
			group by cp.sort, cp.id, c.sort, c.id
			order by cp.sort, cp.id, c.sort, c.id
		</where>		
	</select>
</mapper>