<?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.ym.mec.cms.dal.dao.SysNewsInformationDao">
	
	<resultMap type="com.ym.mec.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.ym.mec.common.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="tenant_id_" property="tenantId" />
		<result column="attribute1_" property="attribute1" />
		<result column="attribute2_" property="attribute2" />
		<result column="subject_id_list_" property="subjectIdList" />
		<result column="organ_id_list_" property="organIdList" />
		<result column="subject_name_" property="subjectName" />
		<result column="organ_name_list_" property="organNameList" />
	</resultMap>
	
	<resultMap type="com.ym.mec.cms.dto.SysNewsInformationDto" id="SysNewsInformationDto" extends="SysNewsInformation">
		<result column="typeName" property="typeName" />
		<result column="subTypeName" property="subTypeName" />
	</resultMap>
	
	<sql id="queryCondition">
		<where>
			sni.del_flag_=0 and sni.tenant_id_ = #{tenantId}
			<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.ym.mec.common.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>
			<if test="organId != null">
				and (find_in_set(#{organId},sni.organ_id_list_) or sni.organ_id_list_ is null)
			</if>
			<if test="organIdList != null">
				and INTE_ARRAY(#{organIdList},sni.organ_id_list_)
			</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="clientName != 'manage'">
				<choose>
					<when test="memo != null and memo != ''">
						and sni.memo_ = #{memo}
					</when>
					<otherwise>
						and (sni.memo_ is null or sni.memo_ = '')
					</otherwise>
				</choose>
			</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.ym.mec.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_,tenant_id_,attribute1_,attribute2_,subject_id_list_,organ_id_list_)
		VALUES(#{id},#{title},#{content},#{coverImage},#{videoCoverImage},#{type},#{onlineTime},#{offlineTime},#{subType},#{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},now(),now(),#{linkUrl},#{hrefTarget},#{order},0,#{memo},#{tenantId},#{attribute1},#{attribute2},#{subjectIdList},#{organIdList})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.cms.dal.entity.SysNewsInformation">
		UPDATE sys_news_information
		<set>
			<if test="status != null">
				status_ = #{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="title != null">
				title_ = #{title},
			</if>
			<if test="tenantId != null">
				tenant_id_ = #{tenantId},
			</if>
			<if test="updateTime != null">
				update_time_ = NOW(),
			</if>
			<if test="content != null">
				content_ = #{content},
			</if>
			<if test="coverImage != null">
				cover_image_ = #{coverImage},
			</if>
			<if test="videoCoverImage != null">
				video_cover_image_ = #{videoCoverImage},
			</if>
			<if test="type != null">
				type_ = #{type},
			</if>
				online_time_ = #{onlineTime},
				offline_time_ = #{offlineTime},
			<if test="subType != null">
				sub_type_ = #{subType},
			</if>
			<if test="linkUrl != null">
				link_url_ = #{linkUrl},
			</if>
			<if test="hrefTarget != null">
				href_target_ = #{hrefTarget},
			</if>
			<if test="order != null">
				order_ = #{order},
			</if>
			<if test="memo != null">
				memo_ = #{memo},
			</if>
			<if test="delFlag != null">
				del_flag_ = #{delFlag},
			</if>
			<if test="attribute1 != null">
				attribute1_ = #{attribute1},
			</if>
			<if test="attribute2 != null">
				attribute2_ = #{attribute2},
			</if>
			<if test="subjectIdList != null">
				subject_id_list_ = #{subjectIdList},
			</if>
			<if test="organIdList != null">
				organ_id_list_ = #{organIdList},
			</if>
		</set>
		WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM sys_news_information WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SysNewsInformation" parameterType="map">
		SELECT sni.*,GROUP_CONCAT(distinct s.name_) subject_name_,GROUP_CONCAT(distinct o.name_) organ_name_list_
		FROM sys_news_information sni left join subject s on find_in_set(s.id_,sni.subject_id_list_)
		left join organization o on find_in_set(o.id_,sni.organ_id_list_)
		<include refid="queryCondition" />
		group by sni.id_
		order by sni.status_ desc,sni.order_ desc,sni.update_time_ desc
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(sni.id_) FROM sys_news_information sni
		<include refid="queryCondition" />
	</select>

    <select id="queryBottomInfo" resultMap="SysNewsInformation" parameterType="java.lang.Integer">
        SELECT * FROM sys_news_information
        where del_flag_=0 and status_ = 1
                and sub_type_ = #{subType}
    </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.*,GROUP_CONCAT(s.name_) subject_name_ FROM sys_news_information sni left join subject s on find_in_set(s.id_,sni.subject_id_list_) where sni.del_flag_=0 and sni.tenant_id_ = #{tenantId}
		<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.ym.mec.common.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>
			<if test="organId != null">
				and (find_in_set(#{organId},sni.organ_id_list_) or sni.organ_id_list_ is null or sni.organ_id_list_ = '')
			</if>
		<if test="clientName != 'manage'">
			<choose>
				<when test="memo != null and memo != ''">
					and sni.memo_ = #{memo}
				</when>
				<otherwise>
					and (sni.memo_ is null or sni.memo_ = '')
				</otherwise>
			</choose>
		</if>
		group by sni.id_
		order by sni.status_ desc,sni.order_ desc,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 and sni.tenant_id_ = #{tenantId}
		<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.ym.mec.common.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>
			<if test="organId != null">
				and (find_in_set(#{organId},sni.organ_id_list_) or sni.organ_id_list_ is null or sni.organ_id_list_ = '')
			</if>
		<if test="clientName != 'manage'">
			<choose>
				<when test="memo != null and memo != ''">
					and sni.memo_ = #{memo}
				</when>
				<otherwise>
					and (sni.memo_ is null or sni.memo_ = '')
				</otherwise>
			</choose>
		</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.*,sntp.name_ typeName,snts.name_ subTypeName FROM sys_news_information sni 
		left join sys_news_type sntp on sni.type_ = sntp.id_
		left join sys_news_type snts on sni.sub_type_ = snts.id_
		where sni.id_ = #{id}
	</select>
	<select id="queryBySubType" resultMap="SysNewsInformation">
		SELECT * FROM sys_news_information
		WHERE del_flag_=0
		AND status_=1
		<if test="subType != null">
			AND sub_type_ = #{subType}
		</if>
		<if test="memo == null or memo == ''">
			AND (memo_ IS NULL OR memo_ = '')
		</if>
		<if test="memo != null and memo !=''">
			AND memo_ = #{memo}
		</if>
		ORDER BY order_ DESC
	</select>
</mapper>
