<?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.jeeplus.modules.oa.dao.OaNotifyDao">
    
	<sql id="oaNotifyColumns">
		a.ID AS "id",
		a.TYPE AS "type",
		a.TITLE AS "title",
		a.CONTENT AS "content",
		CONCAT(a.CONTENTS,<![CDATA['<style>img{max-width:100%!important;}</style>']]>) AS "contents",
		a.FILES AS "files",
		a.STATUS AS "status",
		a.CREATE_BY AS "createBy.id",
		a.CREATE_DATE AS "createDate",
		a.UPDATE_BY AS "updateBy.id",
		a.UPDATE_DATE AS "updateDate",
		a.REMARKS AS "remarks",
		a.DEL_FLAG AS "delFlag",
		a.company_id AS "company.id",
		a.office_id AS "office.id",
		a.number AS "number",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.process_instance_id AS "processInstanceId",
	    b.read_num ,
	    b.un_read_num,
	    u.name AS "createBy.name",
	    o.top_company AS "office.name"
	</sql>
	
	<sql id="oaNotifyJoins">
		<!-- 查询已读和未读条数 -->
		LEFT JOIN (
			SELECT r.oa_notify_id,
				sum(case when r.read_flag = '1' then 1 else 0 end) read_num,
				sum(case when r.read_flag != '1' then 1 else 0 end) un_read_num
			FROM oa_notify_record r WHERE r.user_id is not null and r.read_flag != '' GROUP BY r.oa_notify_id
		) b ON b.oa_notify_id = a.id
		LEFT JOIN sys_user u ON u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
	</sql>
    
	<select id="get" resultType="OaNotify">
		SELECT 
			<include refid="oaNotifyColumns"/>
		FROM oa_notify a
		<include refid="oaNotifyJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="OaNotify">
		SELECT 
			<include refid="oaNotifyColumns"/>
			<if test="isSelf">,
                    r.read_flag
			</if>
		FROM oa_notify a
		<include refid="oaNotifyJoins"/>
		<!-- 我的通告 -->
		<if test="isSelf">
			JOIN oa_notify_record r ON r.oa_notify_id = a.id AND r.user_id = #{currentUser.id,jdbcType=VARCHAR}
		</if>
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
		<!-- 未读0，已读1 -->
		<if test="readFlag != null and readFlag != ''">
			AND r.read_flag = #{readFlag}
		</if>
		<if test="title != null and title != ''">
			AND a.TITLE LIKE 
					<if test="dbName == 'oracle'">'%'||#{title}||'%'</if>
					<if test="dbName == 'mysql'">CONCAT('%', #{title}, '%')</if>
		</if>
		<if test="type != null and type != ''">
			AND a.TYPE = #{type}
		</if>
		<if test="number != null and number != ''">
			AND a.number = #{number}
		</if>
		<if test="status != null and status != ''">
			AND a.STATUS = #{status}
		</if>
		<!-- 我的通告：只查询"发布"状态的，不要"草稿"状态的 -->
		<if test="isSelf">
			AND a.STATUS = '5'
		</if>
		<if test="title != null and title != ''">
			AND a.title like
			<if test="dbName == 'oracle'">'%'||#{ctitle}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{title}, '%')</if>
		</if>
		<if test="content != null and content != ''">
			AND a.content like
			<if test="dbName == 'oracle'">'%'||#{content}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{content}, '%')</if>
		</if>
		<if test="company != null and company.id !=null and company.id != ''">
			AND a.company_id = #{company.id}
		</if>
		<if test="office != null and office.id !=null and office.id != ''">
			AND a.office_id = #{office.id}
		</if>
        <if test="createBy != null and createBy.id !=null and createBy.id != ''">
            AND a.CREATE_BY = #{createBy.id}
        </if>
        <if test="createBy != null and createBy.name !=null and createBy.name != ''">
            AND u.name like
			<if test="dbName == 'oracle'">'%'||#{createBy.name}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{createBy.name}, '%')</if>
        </if>
		<if test="createStartDate != null and createStartDate != ''">
			AND a.create_date &gt;= #{createStartDate}
		</if>
		<if test="createEndDate != null and createEndDate != ''">
			AND a.create_date &lt;= #{createEndDate}
		</if>
		<if test="nowDate != null and nowDate != ''">
			AND a.start_date &lt;= #{nowDate} AND a.end_date &gt;= #{nowDate}
		</if>

		ORDER BY a.update_date DESC
	</select>

	<select id="findListByPc" resultType="OaNotify">
		SELECT
		a.ID AS "id",
		a.TYPE AS "type",
		a.TITLE AS "title",
		a.CONTENT AS "content",
		CONCAT(a.CONTENTS,<![CDATA['<style>img{max-width:100%!important;}</style>']]>) AS "contents",
		a.FILES AS "files",
		a.STATUS AS "status",
		a.CREATE_BY AS "createBy.id",
		a.CREATE_DATE AS "createDate",
		a.UPDATE_BY AS "updateBy.id",
		a.UPDATE_DATE AS "updateDate",
		a.REMARKS AS "remarks",
		a.DEL_FLAG AS "delFlag",
		a.company_id AS "company.id",
		a.office_id AS "office.id",
		u.name AS "createBy.name",
		o.top_company AS "office.name",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.number AS "number",
        a.process_instance_id AS "processInstanceId"
		FROM oa_notify a
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = a.create_by
		<!-- 我的通告 -->
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
		<if test="title != null and title != ''">
			AND a.TITLE LIKE
			<if test="dbName == 'oracle'">'%'||#{title}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{title}, '%')</if>
		</if>
		<if test="type != null and type != ''">
			AND a.TYPE = #{type}
		</if>
		<if test="number != null and number != ''">
			AND a.number = #{number}
		</if>
		<if test="status != null and status != ''">
			AND a.STATUS = #{status}
		</if>
		<if test="title != null and title != ''">
			AND a.title like
			<if test="dbName == 'oracle'">'%'||#{ctitle}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{title}, '%')</if>
		</if>
		<if test="content != null and content != ''">
			AND a.content like
			<if test="dbName == 'oracle'">'%'||#{content}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{content}, '%')</if>
		</if>
		<if test="office != null and office.id !=null and office.id != ''">
			AND a.office_id = #{office.id}
		</if>
		<if test="createStartDate != null and createStartDate != ''">
			AND a.create_date &gt;= #{createStartDate}
		</if>
		<if test="createEndDate != null and createEndDate != ''">
			AND a.create_date &lt;= #{createEndDate}
		</if>
		<if test="createBy != null and createBy.name !=null and createBy.name != ''">
			AND u.name like
			<if test="dbName == 'oracle'">'%'||#{createBy.name}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{createBy.name}, '%')</if>
		</if>
		${sqlMap.dsf}
		ORDER BY a.update_date DESC
	</select>
	
	<select id="findAllList" resultType="OaNotify">
		SELECT 
			<include refid="oaNotifyColumns"/>
		FROM oa_notify a
		<include refid="oaNotifyJoins"/>
		WHERE a.del_flag = #{DEL_FLAG_NORMAL} 
		ORDER BY a.update_date DESC
	</select>
	
	<select id="findCount" resultType="Long">
		SELECT 
			count(1)
		FROM oa_notify a
		<if test="isSelf">
			JOIN oa_notify_record r ON r.oa_notify_id = a.id AND r.user_id = #{currentUser.id}
				<if test="readFlag != null and readFlag != ''">
					AND r.read_flag = #{readFlag}
				</if>
		</if>
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
		<if test="isSelf">
			AND a.STATUS = '1'
		</if>
	</select>
	
	<insert id="insert">
		INSERT INTO oa_notify(
			ID,
			TYPE,
			COMPANY_ID,
			OFFICE_ID,
			TITLE,
			CONTENT,
			CONTENTS,
			FILES,
			STATUS,
			CREATE_BY,
			CREATE_DATE,
			UPDATE_BY,
			UPDATE_DATE,
			REMARKS,
			start_date,
			end_date,
			number,
			process_instance_id,
			DEL_FLAG
		) VALUES (
			#{id},
			#{type},
			#{company.id},
			#{office.id},
			#{title},
			#{content},
			#{contents},
			#{files},
			#{status},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{startDate},
			#{endDate},
			#{number},
			#{processInstanceId},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE oa_notify SET 	
			TYPE = #{type},
			TITLE = #{title},
			COMPANY_ID = #{company.id},
			OFFICE_ID = #{office.id},
			CONTENT = #{content},
			CONTENTS = #{contents},
			FILES = #{files},
			STATUS = #{status},
			UPDATE_BY = #{updateBy.id},
			UPDATE_DATE = #{updateDate},
			start_date = #{startDate},
			end_date = #{endDate},
			end_date = #{endDate},
			number = #{number},
			process_instance_id = #{processInstanceId},
			REMARKS = #{remarks}
		WHERE id = #{id}
	</update>
	<update id="saveRemarks">
		UPDATE oa_notify SET
			REMARKS = #{remarks}
		WHERE id = #{id}
	</update>

	<update id="delete">
		DELETE FROM oa_notify
		WHERE id = #{id}
	</update>
	
	<update id="deleteByLogic">
		UPDATE oa_notify SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>

	<select id="getCreateId" resultType="string">
		SELECT create_by FROM oa_notify WHERE id = #{id}
	</select>

    <update id="updateStatusById">
        UPDATE oa_notify SET
        status = #{status}
        WHERE id = #{id}
    </update>

    <update id="updateProcessIdAndStatus" >
        UPDATE  oa_notify SET
        process_instance_id = #{processInstanceId},
        status = #{status}
        WHERE id = #{id}
    </update>
</mapper>