<?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.yonge.cooleshow.cms.dal.dao.SysNewsTypeDao">

	<resultMap type="com.yonge.cooleshow.cms.dal.entity.SysNewsType" id="SysNewsType">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="parent_id_" property="parentId" />
		<result column="del_flag_" property="delFlag" />
		<result column="order_" property="order" />
		<result column="url_" property="url" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	
	<resultMap type="com.yonge.cooleshow.cms.dal.entity.SysNewsTypeTree" extends="SysNewsType" id="SysNewsTypeTree">
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="SysNewsType">
		SELECT * FROM
		sys_news_type WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="SysNewsType">
		SELECT * FROM sys_news_type ORDER
		BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.yonge.cooleshow.cms.dal.entity.SysNewsType"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO sys_news_type
		(id_,name_,parent_id_,del_flag_,create_time_,update_time_,order_,url_)
		VALUES(#{id},#{name},#{parentId},#{delFlag},#{createTime},#{updateTime},#{order},#{url})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.yonge.cooleshow.cms.dal.entity.SysNewsType">
		UPDATE sys_news_type
		<set>
			<if test="parentId != null">
				parent_id_ = #{parentId},
			</if>
			<if test="delFlag != null">
				del_flag_ = #{delFlag},
			</if>
			<if test="id != null">
				id_ = #{id},
			</if>
			<if test="updateTime != null">
				update_time_ = #{updateTime},
			</if>
			<if test="name != null">
				name_ = #{name},
			</if>
			<if test="createTime != null">
				create_time_ = #{createTime},
			</if>
			<if test="order != null">
				order_ = #{order},
			</if>
			<if test="url != null">
				url_ = #{url}
			</if>
		</set>
		WHERE id_ = #{id}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete">
		update sys_news_type set del_flag_ = 1 WHERE id_ = #{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SysNewsType" parameterType="map">
		SELECT * FROM sys_news_type

		<include refid="queryCondition" />
		ORDER BY order_ , id_
		<include refid="global.limit" />
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM sys_news_type
		<include refid="queryCondition" />
	</select>
	
	<select id="queryByParentId" resultMap="SysNewsTypeTree">
		SELECT * FROM sys_news_type where parent_id_ = #{parentId}
	</select>

	<sql id="queryCondition">
		<where>
			del_flag_ = 0
			<if test="search != null and searcer != ''">
				and (id_ like concat('%',#{search},'%') or name_ like concat('%',#{search},'%'))
			</if>
			<if test="parentId != null">
				and parent_id_ = #{parentId}
			</if>
		</where>
	</sql>

	<select id="queryAppPage" resultMap="SysNewsTypeDto">
		select a.*,sni.* from (
		SELECT distinct snt.id_ as sntId
		,snt.name_ as name,
		snt.parent_id_ as parentId,
		snt.order_ as sntOrder,
		snt.del_flag_ as sntDelFlag,
		snt.create_time_ as createTime,
		snt.update_time_ as updateTime,
		snt.url_ as url
		FROM sys_news_type snt
		left join sys_news_information sni on sni.sub_type_ = snt.id_
		where snt.del_flag_ = 0 and snt.parent_id_ = 6 and sni.del_flag_ = 0 and sni.status_ = 1
		<if test="search != null and search != ''">
			and (sni.title_ like concat('%',#{search},'%') or snt.name_ like concat('%',#{search},'%'))
		</if>
		ORDER BY snt.order_ , sni.order_
		<include refid="global.limit"/>
		) a

		left join sys_news_information sni on sni.sub_type_ = a.sntId
		where sni.del_flag_ = 0 and sni.status_ = 1

		ORDER BY a.sntOrder , sni.order_

	</select>

	<resultMap type="com.yonge.cooleshow.cms.dto.SysNewsTypeDto" id="SysNewsTypeDto">

		<result column="sntId" property="id" />
		<result column="name" property="name" />
		<result column="parentId" property="parentId" />
		<result column="sntOrder" property="order" />
		<result column="sntDelFlag" property="delFlag" />
		<result column="createTime" property="createTime" />
		<result column="url" property="url" />
		<result column="updateTime" property="updateTime" />

		<collection property="newsInformationList"  ofType="com.yonge.cooleshow.cms.dal.entity.SysNewsInformation">
			<result column="id_" property="id" />
			<result column="title_" property="title" />
			<result column="content_" property="content" />
			<result column="cover_image_" property="coverImage" />
			<result column="video_cover_image_" property="videoCoverImage" />
			<result column="link_url_" property="linkUrl"/>
			<result column="type_" property="type"/>
			<result column="online_time_" property="onlineTime"/>
			<result column="offline_time_" property="offlineTime"/>
			<result column="sub_type_" property="subType"/>
			<result column="status_" property="status" typeHandler="com.yonge.toolset.mybatis.dal.CustomEnumTypeHandler" />
			<result column="create_time_" property="createTime" />
			<result column="update_time_" property="updateTime" />
			<result column="del_flag_" property="delFlag" />
			<result column="href_target_" property="hrefTarget" />
			<result column="order_" property="order" />
			<result column="memo_" property="memo" />
			<result column="attribute1_" property="attribute1" />
			<result column="attribute2_" property="attribute2" />
			<result column="show_time_" property="showTime" />
			<result column="update_by_" property="updateBy" />
			<result column="create_by_" property="createBy" />
		</collection>
	</resultMap>

	<select id="queryAppCount" resultType="int">
		select count(1) from (
		SELECT snt.id_
		FROM sys_news_type snt
		left join sys_news_information sni on sni.sub_type_ = snt.id_
		where snt.del_flag_ = 0 and snt.parent_id_ = 6 and sni.del_flag_ = 0 and sni.status_ = 1
		<if test="search != null and search != ''">
			and  (sni.title_ like concat('%',#{search},'%') or snt.name_ like concat('%',#{search},'%'))
		</if>
		group by snt.id_ ) a
	</select>
</mapper>
