<?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.NoticeCorpMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.USERNAME,
		f.CREATOR,
		f.CREATTIME,
		f.SYNOPSIS,
		f.CONTENT,
		f.ISALL,
		f.ISDELETE,
		f.ISTOP,
		f.STATUS,
		f.CORP_INFO_ID,
		f.FILEPATH,
		f.DBALARM_ID,
		f.NOTICECORP_ID
	</sql>

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

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USERNAME},
		#{CREATOR},
		#{CREATTIME},
		#{SYNOPSIS},
		#{CONTENT},
		#{ISALL},
		#{ISDELETE},
		#{ISTOP},
		#{STATUS},
		#{CORP_INFO_ID},
		#{FILEPATH},
		#{DBALARM_ID},
		#{NOTICECORP_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">
		delete from
		<include refid="tableName"></include>
		where
		NOTICECORP_ID = #{NOTICECORP_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		USERNAME = #{USERNAME},
		CREATOR = #{CREATOR},
		CREATTIME = #{CREATTIME},
		SYNOPSIS = #{SYNOPSIS},
		CONTENT = #{CONTENT},
		ISALL = #{ISALL},
		ISTOP = #{ISTOP},
		STATUS = #{STATUS},
		<if test="FILEPATH != null and FILEPATH != ''"><!-- 关键词检索 -->
			FILEPATH = #{FILEPATH},
		</if>
		NOTICECORP_ID = NOTICECORP_ID
		where
		NOTICECORP_ID = #{NOTICECORP_ID}
	</update>

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

	<!-- 通过ID获取数据 -->
	<select id="findByUserId" parameterType="pd" resultType="pd">
	SELECT
	f.USERNAME,
	f.CREATOR,
	f.CREATTIME,
	f.SYNOPSIS,
	f.CONTENT,
	f.ISALL,
	f.ISDELETE,
	f.ISTOP,
	f.STATUS,
	f.CORP_INFO_ID,
	f.FILEPATH,
	f.DBALARM_ID,
	f.NOTICECORP_ID
FROM
	BUS_NOTICECORP f
	left join 	bus_noticecorpuserid u ON f.NOTICECORP_ID = u.NOTICECORP_ID
WHERE
	u.NOTICECORPUSERID_ID = #{NOTICECORPUSERID_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,u.NAME CREATOR_NAME
		from
		<include refid="tableName"></include> f
		left join  sys_user u on f.CREATOR = u.USER_ID
		where 1=1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			f.SYNOPSIS LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.CORP_INFO_ID !=null and pd.CORP_INFO_ID!=''">
			and f.CORP_INFO_ID=#{pd.CORP_INFO_ID}
		</if>
		<if test="pd.STATUS != null and pd.STATUS != ''"><!-- 关键词检索 -->
			and f.STATUS=#{pd.STATUS}
		</if>
		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 1=1 and f.STATUS='1'
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		delete from
		<include refid="tableName"></include>
		where
		NOTICECORP_ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>

	<!-- 列表 -->
	<select id="listByUserId" parameterType="page" resultType="pd">
		SELECT
			c.*,u.TYPE,u.NOTICECORPUSERID_ID
		FROM
			bus_noticecorpuserid u
				LEFT JOIN bus_noticecorp c ON c.NOTICECORP_ID = u.NOTICECORP_ID
		where u.USER_ID = #{pd.USER_ID} and u.ISDELETE is NULL and c.`STATUS` = '1'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			c.SYNOPSIS LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			c.CONTENT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		order by c.ISTOP desc, c.CREATTIME desc
	</select>
	<select id="listByIndex" parameterType="page" resultType="pd">
		SELECT
			u.*
		FROM
			bus_noticecorp u
				LEFT JOIN bus_noticecorpuserid c ON c.NOTICECORP_ID = u.NOTICECORP_ID
		where c.USER_ID = #{pd.USER_ID}
		  AND u.ISDELETE = 0
		  AND u.STATUS = 1
		order by u.ISTOP desc, u.CREATTIME desc
	</select>


	<select id="getRedPoint" parameterType="pd" resultType="int">

		SELECT
			count(*)
		FROM
			bus_noticecorpuserid u
				LEFT JOIN bus_noticecorp c ON c.NOTICECORP_ID = u.NOTICECORP_ID
		where u.USER_ID = #{USER_ID} and u.type ='0' and c.ISDELETE ='0' and c.`STATUS` = '1'

	</select>
	<!-- 获取平台公告（第一条） -->
	<select id="getPlaNewNotice" parameterType="pd" resultType="pd">
		SELECT
			u.*
		FROM
			bus_noticecorp u
				LEFT JOIN bus_noticecorpuserid c ON c.NOTICECORP_ID = u.NOTICECORP_ID
		where c.USER_ID = #{USER_ID}
		  AND u.ISDELETE = 0
		  AND u.STATUS = 1
		order by u.ISTOP desc, u.CREATTIME desc
			limit 1
	</select>
</mapper>
