<?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.zcloud.mapper.datasource.notice.ServiceNoticeMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_NOTICE
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.USERNAME,
		f.CREATOR,
		f.CREATTIME,
		f.SYNOPSIS,
		f.CONTENT,
		f.ISALL,
		f.ISTOP,
		f.STATUS,
		f.NOTICE_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		USERNAME,
		CREATOR,
		CREATTIME,
		SYNOPSIS,
		CONTENT,
		ISALL,
		ISTOP,
		STATUS,
		ISDELETE,
		NOTICE_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USERNAME},
		#{CREATOR},
		#{CREATTIME},
		#{SYNOPSIS},
		#{CONTENT},
		#{ISALL},
		#{ISTOP},
		#{STATUS},
		#{ISDELETE},
		#{NOTICE_ID}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			NOTICE_ID = #{NOTICE_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			SYNOPSIS = #{SYNOPSIS},
			CONTENT = #{CONTENT},
			ISTOP = #{ISTOP},
			STATUS = #{STATUS},
			NOTICE_ID = NOTICE_ID
		where
			NOTICE_ID = #{NOTICE_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.NOTICE_ID = #{NOTICE_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.SYNOPSIS LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.USERNAME!=null and pd.USERNAME!=''"><!-- 根据用户名过滤 -->
			and f.USERNAME = #{pd.USERNAME}
		</if>
		<if test="pd.ISALL != null and pd.ISALL != ''"><!-- 是否全显 -->
			AND
				f.ISALL = #{pd.ISALL}
		</if>
		<if test="pd.STATUS != null and pd.STATUS != ''"><!-- 是否发布，0否1是 -->
			AND
				f.STATUS = #{pd.STATUS}
		</if>
<!-- 		or f.ISALL = 'yes' -->
		order by f.ISTOP desc, f.CREATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0' and f.STATUS = 1
		order by f.ISTOP desc, f.CREATTIME desc
	</select>


	<select id="applistAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		(select BASICINFO_ID from bus_noticerecord t where  t.NOTICE_ID=f.NOTICE_ID and t.BASICINFO_ID=#{BASICINFO_ID}) as isread
		from
		<include refid="tableName"></include> f
	</select>
	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			NOTICE_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

	<select id="listCountBYBasicinfoID" parameterType="pd" resultType="pd">
		select count(1) as noRead from oa_notice f where f.NOTICE_ID not in( select NOTICE_ID from bus_noticerecord t where t.NOTICE_ID=f.NOTICE_ID and t.BASICINFO_ID=#{BASICINFO_ID})
	</select>


	<!-- 新增公告和企业关系信息 -->
	<insert id="saveNotAndCorp" parameterType="pd">
		insert into  bus_notice_corp
		( NOTICE_ID ,CORP_INFO_ID,TYPE,ISDELETE,NOTICE_CORP_ID
		) values
		(#{NOTICE_ID},#{CORP_INFO_ID},#{TYPE},'0',#{NOTICE_CORP_ID})
<!--		<foreach item="item" index="index"	 collection="list" separator=",">-->
<!--			(  #{item.noticeId},    #{item.corpId}	, #{item.TYPE}	,'0'		)-->
<!--		</foreach>-->
	</insert>


	<select id="findNoticeCorpInfoById" parameterType="page" resultType="pd">
		 	   SELECT  * FROM bus_notice_corp c
			where
			c.CORP_INFO_ID = #{CORP_INFO_ID}
			and c.NOTICE_ID = #{NOTICE_ID}
	</select>

	<!-- 根据公告id获取关系信息-->
	<select id="getNotAndCorpByNotId" parameterType="pd" resultType="pd">
		   SELECT
				f.NOTICE_ID,
				f.CORP_INFO_ID,
				i.CORP_NAME
			FROM
				bus_notice_corp f
			LEFT JOIN bus_corp_info i on  i.corpinfo_id = f.corp_info_id
		where
			f.NOTICE_ID = #{NOTICE_ID}
	</select>
	<!-- 根据公告id获取关系信息 -->
	<select id="getNotAndCorpByCorpIddatalistPage" parameterType="page" resultType="pd">
    	SELECT
   			<include refid="Field"></include>
		FROM
			BUS_NOTICE f
		WHERE
			(
				isall = 'yes'
				OR NOTICE_ID IN (
					SELECT
						NOTICE_ID
					FROM
						bus_notice_corp
					WHERE
						CORP_INFO_ID = #{pd.CORP_INFO_ID}
				)
			)
			AND f.ISDELETE = 0
			AND f.STATUS = 1
			<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
				and
				(
					f.SYNOPSIS LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				or
					f.CONTENT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
			</if>
		ORDER BY
			f.ISTOP DESC,
			f.CREATTIME DESC
	</select>
	<delete id="delteNotAndCorpByNotId" parameterType="pd">
		delete from bus_notice_corp   where  NOTICE_ID = #{NOTICE_ID}
	</delete>

	<!-- 列表(全部) -->
	<select id="getTypeCountByNotId" parameterType="page" resultType="pd">
				SELECT
					count(*) AS count,
					CORP_INFO_ID,
					NOTICE_ID,
					type
				FROM
					bus_notice_corp
				WHERE
					NOTICE_ID IN
				<foreach item="item" index="index" collection="pd.notIdsVal" open="(" separator="," close=")">
		                 '${item}'
				</foreach>
				GROUP BY
					type,
					NOTICE_ID
	</select>
		<!-- 列表 显示企业阅读情况-->
	<select id="getCorpReadTypelistPage" parameterType="page" resultType="pd">
				SELECT
					f.*,
					c.CORP_NAME
				FROM
					bus_notice_corp f
				LEFT JOIN bus_corp_info c ON c.CORPINFO_ID = f.CORP_INFO_ID
				WHERE
					f.NOTICE_ID = #{pd.NOTICE_ID}
	</select>

		<!-- 修改阅读状态 -->
	<update id="editCorpInfo" parameterType="pd">
		update bus_notice_corp
		set
			<if test = "TYPE!= null and TYPE!= ''">
				TYPE =#{TYPE},
			</if>
			<if test = "CREATOR!= null and CREATOR!= ''">
				CREATOR =#{CREATOR}
			</if>
		where  	NOTICE_ID = #{NOTICE_ID}
			<if test = "CORP_INFO_ID!= null and CORP_INFO_ID != ''">
				and CORP_INFO_ID =#{  CORP_INFO_ID}
			</if>
	</update>


	<select id="getRedPoint" parameterType="pd" resultType="int">
		select count(*) from bus_notice f
		left join bus_noticelog l on f.NOTICE_ID = l.NOTICE_ID
		where  f.ISDELETE = '0' and f.STATUS = '1' and l.USER_ID = #{USER_ID} and l.STATUS='0' and l.ISDELETE = '0'


	</select>

	<!--获取最新公告 -->
	<select id="getNewNotice" parameterType="pd" resultType="pd">
    	SELECT
   			<include refid="Field"></include>
		FROM
			BUS_NOTICE f
		WHERE
			(
				isall = 'yes'
				OR NOTICE_ID IN (
					SELECT
						NOTICE_ID
					FROM
						bus_notice_corp
					WHERE
						CORP_INFO_ID = #{CORP_INFO_ID}
				)
			)
			AND f.ISDELETE = 0
			AND f.STATUS = 1
		ORDER BY
			f.ISTOP DESC,
			f.CREATTIME DESC
			limit 1
	</select>


</mapper>
