<?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.SysNewsInformationDao">
	
	<resultMap type="com.yonge.cooleshow.cms.dal.entity.SysNewsInformation" id="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="link_type_" property="linkType" />
		<result column="create_by_" property="createBy" />
		<result column="client_type_" property="clientType" />
	</resultMap>
	
	<resultMap type="com.yonge.cooleshow.cms.dto.SysNewsInformationDto" id="SysNewsInformationDto" extends="SysNewsInformation">
		<result column="updateName" property="updateName" />
		<result column="subTypeName" property="subTypeName" />
	</resultMap>
	
	<sql id="queryCondition">
		<where>
			sni.del_flag_=0 
			<if test="type != null">
				and sni.type_ = #{type}
			</if>
			<if test="subType != null">
				and sni.sub_type_ = #{subType}
			</if>
			<if test="status != null">
				and sni.status_ = #{status}
			</if>
			<if test="title != null">
				and (sni.title_ like '%' #{title} '%'
				or sni.id_ like concat('%',#{title},'%')
				)
			</if>
			<if test="search != null">
				and sni.title_ like '%' #{search} '%'
			</if>
			<if test="subjectId != null">
				and find_in_set(#{subjectId},sni.subject_id_list_)
			</if>
			<if test="platformType != null and platformType != ''">
				<if test="platformType != 'ADMIN'" >
					and sni.status_ = 1
					<if test="type != 5">
						and if(sni.online_time_ is not null,sni.online_time_ &lt;= now(),1=1)
						and if(sni.offline_time_ is not null,sni.offline_time_ &gt;= now(),1=1)
					</if>
					<if test="type == 4 or type == 5">
						<choose>
							<when test="platformType == 'WEBSITE'">
								and sni.client_type_ = #{platformType}
							</when>
							<when test="platformType == 'STUDENT'">
								and sni.client_type_ = #{platformType}
							</when>
							<when test="platformType == 'TEACHER'">
								and sni.client_type_ = #{platformType}
							</when>
						</choose>
					</if>
				</if>
				<if test="platformType == 'ADMIN'" >
					<if test="clientType != null and clientType != ''">
						and sni.client_type_ = #{clientType}
					</if>
				</if>
			</if>
			<if test="excludeIds!=null and excludeIds.size()>0">
				AND sni.id_ NOT IN
				<foreach collection="excludeIds" item="excludeId" open="(" close=")" separator=",">
					#{excludeId}
				</foreach>
			</if>
			<if test="version != null and version != ''">
				and sni.attribute1_ = #{version}
			</if>
			<if test="attribute2 != null and attribute2 != ''">
				and sni.attribute2_ = #{attribute2}
			</if>
		</where>
	</sql>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="SysNewsInformation" >
		SELECT * FROM sys_news_information WHERE id_ = #{id} AND del_flag_=0
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="SysNewsInformation">
		SELECT * FROM sys_news_information WHERE del_flag_=0 ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.yonge.cooleshow.cms.dal.entity.SysNewsInformation" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO sys_news_information (id_,title_,content_,cover_image_,video_cover_image_,type_,online_time_,offline_time_,sub_type_,status_,create_time_,update_time_,link_url_,href_target_,order_,del_flag_,memo_,attribute1_,attribute2_,subject_id_list_,create_by_,update_by_,show_time_,link_type_,client_type_)
		VALUES(#{id},#{title},#{content},#{coverImage},#{videoCoverImage},#{type},#{onlineTime},#{offlineTime},#{subType},#{status, typeHandler=com.yonge.toolset.mybatis.dal.CustomEnumTypeHandler},now(),now(),#{linkUrl},#{hrefTarget},#{order},0,#{memo},#{attribute1},#{attribute2},#{subjectIdList},#{createBy},#{updateBy},#{showTime},#{linkType},#{clientType})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.yonge.cooleshow.cms.dal.entity.SysNewsInformation">
		UPDATE sys_news_information
		<set>
			<if test="status != null">
				status_ = #{status, typeHandler=com.yonge.toolset.mybatis.dal.CustomEnumTypeHandler},
			</if>
				title_ = #{title},
				update_time_ = NOW(),
				content_ = #{content},
				cover_image_ = #{coverImage},
				video_cover_image_ = #{videoCoverImage},
				type_ = #{type},
				online_time_ = #{onlineTime},
				offline_time_ = #{offlineTime},
				sub_type_ = #{subType},
				link_url_ = #{linkUrl},
				href_target_ = #{hrefTarget},
				order_ = #{order},
				memo_ = #{memo},
				del_flag_ = #{delFlag},
				attribute1_ = #{attribute1},
				attribute2_ = #{attribute2},
				subject_id_list_ = #{subjectIdList},
				update_by_ = #{updateBy},
				link_type_ = #{linkType},
				client_type_ = #{clientType},
				show_time_ = #{showTime}
		</set>
		WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM sys_news_information WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="selectPage" resultMap="SysNewsInformationDto" parameterType="map">
		SELECT sni.*
		, su.username_ as updateName
		, snt.name_ as subTypeName
		FROM sys_news_information sni
		left join sys_news_type snt on snt.id_ = sni.sub_type_
		left join sys_user su on sni.update_by_ = su.id_
		<include refid="queryCondition" />
		order by sni.status_ desc,sni.order_,sni.update_time_ desc
		<include refid="global.limit"/>
	</select>

	<select id="selectAppVersionInfo" resultType="java.lang.String">

		select avi.status_ from app_version_info avi where avi.platform_ = #{platform} and avi.version_ = #{version}

	</select>
	<!-- 查询当前表的总记录数 -->
	<select id="selectPageCount" resultType="int">
		SELECT COUNT(sni.id_) FROM sys_news_information sni
		<include refid="queryCondition" />
	</select>
	
	<select id="queryByType" resultMap="SysNewsInformation" parameterType="java.lang.Integer">
		SELECT * FROM sys_news_information 
		<where>
			del_flag_=0
			<if test="type != null">
				and type_ = #{type}
			</if>
		</where>
	</select>
	
	<update id="deleteWithLogical">
		UPDATE sys_news_information SET del_flag_ = 1,update_time_ = NOW() WHERE id_ = #{id}
	</update>
	
	<!-- 分页查询 -->
	<select id="queryHomePage" resultMap="SysNewsInformation"
		parameterType="map">
		SELECT sni.*
		FROM sys_news_information sni
		where sni.del_flag_=0
		<if test="type != null">
			and sni.type_ = #{type}
		</if>
		<if test="subType != null and subType != ''">
			and sni.sub_type_ = #{subType}
		</if>
		<if test="status != null">
			and sni.status_ = #{status,
			typeHandler=com.yonge.toolset.mybatis.dal.CustomEnumTypeHandler}
		</if>
		<if test="title != null">
			and sni.title_ like '%' #{title} '%'
		</if>
		<if test="search != null">
			and sni.title_ like '%' #{search} '%'
		</if>
		<if test="excludeIds!=null and excludeIds.size()>0">
			AND sni.id_ NOT IN
			<foreach collection="excludeIds" item="excludeId" open="(" close=")" separator=",">
				#{excludeId}
			</foreach>
		</if>
			<if test="subjectId != null">
				and find_in_set(#{subjectId},sni.subject_id_list_)
			</if>
		group by sni.id_
		order by sni.status_ desc,sni.order_,sni.update_time_ desc
		<include refid="global.limit" />
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryHomeCount" resultType="int">
		SELECT COUNT(sni.id_) FROM sys_news_information sni where sni.del_flag_=0
		<if test="type != null">
			and sni.type_ = #{type}
		</if>
		<if test="subType != null">
			and sni.sub_type_ = #{subType}
		</if>
		<if test="status != null">
			and sni.status_ = #{status,
			typeHandler=com.yonge.toolset.mybatis.dal.CustomEnumTypeHandler}
		</if>
		<if test="title != null">
			and sni.title_ like '%' #{title} '%'
		</if>
		<if test="search != null">
			and sni.title_ like '%' #{search} '%'
		</if>
			<if test="subjectId != null">
				and find_in_set(#{subjectId},sni.subject_id_list_)
			</if>
	</select>
	
	<select id="queryNeedUpdateStatusList" resultMap="SysNewsInformation" >
		SELECT * FROM sys_news_information WHERE del_flag_ = 0 and (
		(online_time_ is not null and now() between online_time_ and offline_time_ and status_ = 0) 
		or (offline_time_ is not null and offline_time_ &lt;= now() and status_ = 1)
		or (online_time_ is not null and online_time_ &gt;= now() and status_ = 1)
		)
	</select>
	
	<select id="queryById" resultMap="SysNewsInformationDto" >
		SELECT sni.*,su.username_ as updateName
		FROM sys_news_information sni
		left join sys_user su on sni.update_by_ = su.id_
		where sni.id_ = #{id}
	</select>

	<update id="updateStatus">
		UPDATE sys_news_information SET status_ = if(status_ = 0,1,0),update_time_ = NOW() WHERE id_ = #{id}
    </update>

	<select id="selectByVersionAndStatus" resultType="java.lang.Integer">
		select count(1)
		from sys_news_information sni
		<where>
		sni.del_flag_ = 0 and sni.type_ = 2
			<if test="status != null">
				and sni.status_ = #{status}
			</if>
		</where>
	</select>
</mapper>
