<?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.notify.mapper.NotifyMapper">
    <resultMap id="BaseResultMap" type="com.jeeplus.notify.service.dto.NotifyDTO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
        <result property="type" column="type" jdbcType="VARCHAR"/>
        <result property="title" column="title" jdbcType="VARCHAR"/>
        <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
        <result property="notifyType" column="notify_type" jdbcType="VARCHAR"/>
        <result property="no" column="no" jdbcType="VARCHAR"/>
        <result property="beginDate" column="begin_date" jdbcType="TIMESTAMP"/>
        <result property="endDate" column="end_date" jdbcType="TIMESTAMP"/>
        <result property="officeId" column="office_id" jdbcType="VARCHAR"/>
        <result property="procInsId" column="proc_ins_id" jdbcType="VARCHAR"/>
        <result property="processDefinitionId" column="process_definition_id" jdbcType="VARCHAR"/>
        <result property="readNum" column="read_num" jdbcType="VARCHAR"/>
        <result property="unReadNum" column="un_read_num" jdbcType="VARCHAR"/>
        <result property="createBy.name" column="name" jdbcType="VARCHAR"/>
        <result property="createBy.photo" column="photo" jdbcType="VARCHAR"/>
        <result property="readFlag" column="read_flag" jdbcType="VARCHAR"/>
        <result property="isClose" column="is_close" jdbcType="VARCHAR"/>
        <collection property="workAttachmentDtoList" ofType="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto" column="id" select="getFileList"></collection>
        <collection property="pluginNotifyOfficesDTOList" ofType="com.jeeplus.notify.service.dto.PluginNotifyOfficesDTO" column="id" select="getOfficeList"></collection>
        <collection property="pluginNotifyUserDTOList" ofType="com.jeeplus.notify.service.dto.PluginNotifyUserDTO" column="id" select="getUserList"></collection>
    <collection property="comments" ofType="com.jeeplus.notify.service.dto.PluginNotifyCommentsDTO" column="id" select="getComments"></collection>
    </resultMap>
    <resultMap id="AttachmentMap" type="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
        <result property="createBy.name" column="create_name" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="VARCHAR"/>
        <result property="url" column="url" jdbcType="VARCHAR"/>
        <result property="name" column="attachment_name" jdbcType="VARCHAR"/>
        <result property="size" column="file_size" jdbcType="VARCHAR"/>
    </resultMap>
    <sql id="notifyColumns">
        a.id,
        a.type,
        a.title,
        a.content,
        a.files,
        a.status,
        a.create_by,
        a.create_date,
        a.update_by,
        a.update_date,
        a.remarks,
        a.del_flag,
        a.notify_type,
        a.no,
        a.begin_date,
        a.end_date,
        a.office_id,
        a.proc_ins_id,
        a.process_definition_id,
        u.name,
        u.photo,
        b.read_num,
        b.un_read_num,
        a.is_close
    </sql>
    <sql id="File_Column_List">
        wa.id,
        wa.create_by,
        wa.create_date,
        wa.update_by,
        wa.update_date,
        wa.remarks,
        wa.del_flag,
        wa.url,
        wa.type,
        wa.attachment_id,
        wa.attachment_name,
        wa.attachment_flag,
        wa.module_type,
        wa.attachment_type,
        wa.file_size,
        wa.sort,
        wa.description
    </sql>
    <sql id="Users_Column_List">
        pnu.id,
        pnu.create_by,
        pnu.create_date,
        pnu.update_by,
        pnu.update_date,
        pnu.del_flag,
        pnu.remarks,
        pnu.user_id,
        pnu.notify_id
    </sql>
    <sql id="Offices_Column_List">
        pno.id,
        pno.create_by,
        pno.create_date,
        pno.update_by,
        pno.update_date,
        pno.del_flag,
        pno.remarks,
        pno.office_id,
        pno.notify_id
    </sql>
    <sql id="Comments_Column_List">
        pnc.id,
        pnc.create_by as create_id,
        pnc.create_date,
        pnc.update_by,
        pnc.update_date,
        pnc.notify_id,
        pnc.user_id,
        pnc.comments,
        pnc.del_flag
    </sql>
    <select id="getComments" resultType="com.jeeplus.notify.service.dto.PluginNotifyCommentsDTO">
        select
        <include refid="Comments_Column_List"></include>,
        su.name as user_name
        from plugin_notify_comments pnc
        left join sys_user su on su.id = pnc.user_id and su.del_flag = '0'
        where pnc.del_flag = '0' and pnc.notify_id = #{id}
        order by pnc.create_date desc
    </select>
    <select id="getOfficeList" resultType="com.jeeplus.notify.service.dto.PluginNotifyOfficesDTO">
        select
        <include refid="Offices_Column_List"></include>,
        so.name as office_name
        from plugin_notify_offices pno
        left join sys_office so on so.id = pno.office_id and so.del_flag = '0'
        where pno.del_flag = '0' and pno.notify_id = #{id}
    </select>
    <select id="getUserList" resultType="com.jeeplus.notify.service.dto.PluginNotifyUserDTO">
        select
        <include refid="Users_Column_List"></include>,
        su.name as user_name,
        so.name as office_name
        from plugin_notify_user pnu
        left join sys_user su on su.id = pnu.user_id and su.del_flag = '0'
        left join sys_office so on so.id = su.office_id and so.del_flag = '0'
        where pnu.del_flag = '0' and pnu.notify_id = #{id}
    </select>
    <select id="getFileList" resultMap="AttachmentMap">
        select
        <include refid="File_Column_List"></include>,
        su.name as create_name
        from work_attachment wa
        left join sys_user su on su.id = wa.create_by and su.del_flag = '0'
        where wa.del_flag = '0' and wa.attachment_id = #{id}
    </select>

    <sql id="notifyJoins">
        <!-- 查询已读和未读条数 -->
        LEFT JOIN (
        SELECT r.notify_id,
        sum(case when r.del_flag = 0 and r.read_flag = '1' then 1 else 0 end) read_num,
        sum(case when r.del_flag = 0 and r.read_flag != '1' then 1 else 0 end) un_read_num
        FROM plugin_notify_record r GROUP BY r.notify_id
        ) b ON b.notify_id = a.id
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="notifyColumns"/>
        FROM plugin_notify a
        LEFT JOIN sys_user u ON u.id = a.CREATE_BY AND u.del_flag = '0'
        <include refid="notifyJoins"/>
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="findList" resultMap="BaseResultMap">
        SELECT
        <include refid="notifyColumns"/>
        <if test="isSelf">,
            r.read_flag
        </if>
        FROM plugin_notify a
        LEFT JOIN sys_user u ON u.id = a.CREATE_BY AND u.del_flag = '0'
        <include refid="notifyJoins"/>
        <!-- 我的通知 -->
        <if test="isSelf">
            JOIN plugin_notify_record r ON r.notify_id = a.id AND r.user_id = #{currentUserId} AND r.del_flag = 0
            <if test="readFlag != null and readFlag != ''">
                AND r.read_flag = #{readFlag}
            </if>
        </if>
        ${ew.customSqlSegment}
        <if test="isSelf">
            and ((a.notify_type = 0 and NOW() >= a.begin_date and NOW() &lt;= a.end_date) or a.notify_type = 1)
        </if>
        order by a.create_date desc
    </select>



    <select id="findCount" resultType="Long">
        SELECT
        count(1)
        FROM plugin_notify a
        <if test="isSelf">
            JOIN plugin_notify_record r ON r.notify_id = a.id AND r.user_id = #{currentUserId} AND r.del_flag = 0
            <if test="readFlag != null and readFlag != ''">
                AND r.read_flag = #{readFlag}
            </if>
        </if>
        WHERE a.del_flag = 0
    </select>

    <update id="updateStatusById">
        update plugin_notify set status = #{status} where id = #{id}
    </update>

    <update id="updateContentById">
        update plugin_notify set content = #{content} where id = #{id}
    </update>
    <update id="closeById">
        update plugin_notify set is_close = "1" where id = #{id}
    </update>

    <update id="updateNotifyClose">
        update plugin_notify set is_close = "1"
        where
        notify_type = 0 and NOW() > end_date and del_flag = 0
    </update>

</mapper>
