<?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="cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyMessageMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectMyBpmCount" resultType="java.lang.Long">
        SELECT COUNT(*) FROM (
            SELECT
                ecr.id,
                snm.template_content
            FROM
                elderly_change_record ecr
            INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id AND ecr.current_flag = 0 AND bbf.type IN (4,5,6,7,8,9)
            INNER JOIN system_notify_message snm ON bbf.process_instance_id = snm.pk_message
            WHERE
                snm.user_id = #{loginUserId} AND snm.deleted = 0 AND bbf.deleted = 0 AND ecr.deleted = 0
                <if test="status != null and queryType != 0">
                    and bbf.`status` = #{status}
                </if>
                <if test="queryType == 0">
                    and (bbf.status = #{status} OR ( bbf.status IN ( 3, 4 ) AND snm.read_status = 0 ))
                </if>
                <if test="readStatus != null">
                    and snm.read_status = #{readStatus}
                </if>
                <if test="completedStatus != null">
                    and snm.completed_status = #{completedStatus}
                </if>
                <if test="notifyType == null">
                    and snm.notify_type in (0,1)
                </if>
                <if test="notifyType != null">
                    and snm.notify_type = #{notifyType}
                </if>
                <if test="businessType != null">
                    and snm.business_type = #{businessType}
                </if>
                <if test="startUserId != null">
                    AND ecr.creator = #{loginUserId}
                    AND bbf.creator = #{loginUserId}
                </if>
            UNION
            SELECT
                err.id,
                snm.template_content
            FROM
                elderly_retreat_record err
            INNER JOIN bpm_business_form bbf ON err.id = bbf.business_id AND err.current_flag = 0 AND bbf.type IN (1,2)
            INNER JOIN system_notify_message snm ON bbf.process_instance_id = snm.pk_message
            WHERE
                snm.user_id = #{loginUserId}
                AND snm.deleted = 0
                AND bbf.deleted = 0
                AND err.deleted = 0
        <if test="status != null and queryType != 0">
                    and bbf.`status` = #{status}
                </if>
                <if test="queryType == 0">
                    and (bbf.status = #{status} OR ( bbf.status IN ( 3, 4 ) AND snm.read_status = 0 ))
                </if>
                <if test="completedStatus != null">
                    and snm.completed_status = #{completedStatus}
                </if>
                <if test="readStatus != null">
                    and snm.read_status = #{readStatus}
                </if>
                <if test="notifyType == null">
                    and snm.notify_type in (0,1)
                </if>
                <if test="notifyType != null">
                    and snm.notify_type = #{notifyType}
                </if>
                <if test="businessType != null">
                    and snm.business_type = #{businessType}
                </if>
                <if test="startUserId != null">
                    AND err.creator = #{loginUserId}
                    AND bbf.creator = #{loginUserId}
                </if>
        ) TOTAL
    </select>

    <select id="selectMyCopyCount" resultType="java.lang.Long">
        SELECT
            COUNT(*)
        FROM
            bpm_process_instance_copy bpic
        INNER JOIN bpm_business_form bbf ON bpic.process_instance_id = bbf.process_instance_id
        INNER JOIN system_notify_message snm ON bpic.user_id = snm.user_id AND bbf.process_instance_id = snm.pk_message
        WHERE
            bpic.user_id = #{loginUserId} AND snm.deleted = 0 AND bbf.deleted = 0 AND bpic.deleted = 0
            <if test="completedStatus != null">
                and snm.completed_status = #{completedStatus}
            </if>
            <if test="readStatus != null">
                and snm.read_status = #{readStatus}
            </if>
            <if test="notifyType != null">
                and snm.notify_type = #{notifyType}
            </if>
            <if test="businessType != null">
                and snm.business_type = #{businessType}
            </if>
    </select>

    <select id="selectBpmBusinessForm" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.BpmBusinessFormRespVO">
        SELECT  * from bpm_business_form
        WHERE process_instance_id = #{processInstanceId} LIMIT 1
    </select>

    <update id="updateMyCreateReadById">
        UPDATE bpm_business_form
        SET
            read_status = 1
        WHERE
            process_instance_id = #{processInstanceId}
    </update>

</mapper>