<?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.bpm.dal.mysql.business.BpmBusinessFormMapper">

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

    <select id="getElderlyInfoByInstanceId" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.elderly.ElderlyLogRecordVO">
        SELECT
            ei.id AS elderlyId,
            ei.elder_name,
            bbf.id,
            bbf.type
        FROM
            bpm_business_form bbf
                INNER JOIN elderly_info ei ON bbf.business_id2 = ei.id
        WHERE
            bbf.process_instance_id = #{processInstanceId}
    </select>

    <select id="selectMyBpmPage" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.elderly.BpmElderlyInfoVO">
        SELECT
            ecr.id,
            ecr.change_date,
            snm.create_time,
            ecr.current_flag,
            bbf.process_instance_id,
            bbf.type,
            bbf.`status`,
            snm.id AS messageId,
            snm.notify_type,
            snm.template_content,
            snm.completed_status,
            snm.read_status
        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="pageVO.status != null and pageVO.queryType != 0">
                and bbf.`status` = #{pageVO.status}
            </if>
            <if test="pageVO.queryType == 0">
                and (bbf.`status` = #{pageVO.status} OR (bbf.`status` IN (3,4) AND snm.read_status = 0))
            </if>
            <if test="pageVO.completedStatus != null">
                and snm.completed_status = #{pageVO.completedStatus}
            </if>
            <if test="pageVO.notifyType == null">
                and snm.notify_type in (0, 1)
            </if>
            <if test="pageVO.notifyType != null">
                and snm.notify_type = #{pageVO.notifyType}
            </if>
            <if test="pageVO.businessType != null">
                and snm.business_type = #{pageVO.businessType}
            </if>
            <if test="pageVO.templateContent != null">
                and snm.template_content LIKE CONCAT('%', #{pageVO.templateContent}, '%')
            </if>
            <if test="pageVO.startUserId != null">
                AND ecr.creator = #{pageVO.loginNickname}
                AND bbf.creator = #{loginUserId}
            </if>
        UNION
        SELECT
            err.id,
            err.retreat_date,
            snm.create_time,
            err.current_flag,
            bbf.process_instance_id,
            bbf.type,
            bbf.`status`,
            snm.id AS messageId,
            snm.notify_type,
            snm.template_content,
            snm.completed_status,
            snm.read_status
        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="pageVO.status != null and pageVO.queryType != 0">
                and bbf.`status` = #{pageVO.status}
            </if>
            <if test="pageVO.queryType == 0">
                and (bbf.`status` = #{pageVO.status} OR (bbf.`status` IN (3,4) AND snm.read_status = 0))
            </if>
            <if test="pageVO.completedStatus != null">
                and snm.completed_status = #{pageVO.completedStatus}
            </if>
            <if test="pageVO.notifyType == null">
                and snm.notify_type in (0, 1)
            </if>
            <if test="pageVO.notifyType != null">
                and snm.notify_type = #{pageVO.notifyType}
            </if>
            <if test="pageVO.businessType != null">
                and snm.business_type = #{pageVO.businessType}
            </if>
            <if test="pageVO.templateContent != null">
                and snm.template_content LIKE CONCAT('%', #{pageVO.templateContent}, '%')
            </if>
            <if test="pageVO.startUserId != null">
                AND err.creator = #{loginUserId}
                AND bbf.creator = #{loginUserId}
            </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectMyBpmPagTest" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.elderly.BpmElderlyInfoVO">
        SELECT
        ecr.id,
        ecr.change_date,
        ecr.create_time,
        ecr.current_flag,
        bbf.process_instance_id,
        bbf.type,
        bbf.`status`,
        snm.id AS messageId,
        snm.notify_type,
        snm.template_content,
        snm.completed_status
        FROM
        elderly_change_record ecr
        INNER JOIN bpm_business_form bbf ON ecr.id = bbf.business_id AND bbf.type IN (4,5,6,7,8)
        INNER JOIN system_notify_message snm ON bbf.process_instance_id = snm.pk_message
        WHERE
        snm.user_id = #{loginUserId}
        <if test="pageVO.status != null">
            and bbf.`status` = #{pageVO.status}
        </if>
        <if test="pageVO.completedStatus != null">
            and snm.completed_status = #{pageVO.completedStatus}
        </if>
        <if test="pageVO.notifyType != null">
            and snm.notify_type = #{pageVO.notifyType}
        </if>
        <if test="pageVO.businessType != null">
            and snm.business_type = #{pageVO.businessType}
        </if>
        <if test="pageVO.templateContent != null">
            and snm.template_content LIKE CONCAT('%', #{pageVO.templateContent}, '%')
        </if>
        <if test="pageVO.startUserId != null">
            AND ecr.creator = #{loginUserId}
            AND bbf.creator = #{loginUserId}
        </if>
        UNION
        SELECT
        err.id,
        err.retreat_date,
        err.create_time,
        err.current_flag,
        bbf.process_instance_id,
        bbf.type,
        bbf.`status`,
        snm.id AS messageId,
        snm.notify_type,
        snm.template_content,
        snm.completed_status
        FROM
        elderly_retreat_record err
        INNER JOIN bpm_business_form bbf ON err.id = bbf.business_id 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}
        <if test="pageVO.status != null">
            and bbf.`status` = #{pageVO.status}
        </if>
        <if test="pageVO.completedStatus != null">
            and snm.completed_status = #{pageVO.completedStatus}
        </if>
        <if test="pageVO.notifyType != null">
            and snm.notify_type = #{pageVO.notifyType}
        </if>
        <if test="pageVO.businessType != null">
            and snm.business_type = #{pageVO.businessType}
        </if>
        <if test="pageVO.templateContent != null">
            and snm.template_content LIKE CONCAT('%', #{pageVO.templateContent}, '%')
        </if>
        <if test="pageVO.startUserId != null">
            AND err.creator = #{loginUserId}
            AND bbf.creator = #{loginUserId}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="getElderMapByBusinessKey" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.elderly.BpmElderlyInfoVO">
        SELECT
            CONCAT(bbf.id, '') AS id,
            ei.elder_name,
            st.name AS tenantName
        FROM
            bpm_business_form bbf
                INNER JOIN elderly_info ei ON bbf.business_id2 = ei.id
                INNER JOIN system_tenant st ON bbf.start_tenant_id = st.id
        WHERE
            bbf.id IN
        <foreach collection="businessKeys" item="businessKey" open="(" separator="," close=")">
            #{businessKey}
        </foreach>
    </select>

    <select id="getElderMapByProcessInstanceId" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.elderly.BpmElderlyInfoVO">
        SELECT
        process_instance_id AS id,
        ei.elder_name,
        bbf.type,
        bbf.status
        FROM
        bpm_business_form bbf
        INNER JOIN elderly_info ei ON bbf.business_id2 = ei.id
        WHERE
        bbf.process_instance_id IN
        <foreach collection="processInstanceIds" item="processInstanceId" open="(" separator="," close=")">
            #{processInstanceId}
        </foreach>
    </select>

    <select id="selectCopyPage" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.elderly.BpmElderlyInfoVO">
        SELECT
            bpic.id,
            bpic.create_time,
            bbf.process_instance_id,
            bbf.type,
            bbf.`status`,
            snm.id AS messageId,
            snm.notify_type,
            snm.template_content,
            snm.completed_status,
            snm.read_status,
            snm.business_sub_type,
            snm.buginess_name
        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="pageVO.status != null">
            and bbf.`status` = #{pageVO.status}
        </if>
        <if test="pageVO.completedStatus != null">
            and snm.completed_status = #{pageVO.completedStatus}
        </if>
        <if test="pageVO.notifyType != null">
            and snm.notify_type = #{pageVO.notifyType}
        </if>
        <if test="pageVO.businessType != null">
            and snm.business_type = #{pageVO.businessType}
        </if>
        <if test="pageVO.templateContent != null">
            and snm.template_content LIKE CONCAT('%', #{pageVO.templateContent}, '%')
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectMyMessagePage" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.elderly.BpmElderlyInfoVO">
        SELECT  * FROM system_notify_message
        WHERE user_id = #{loginUserId}
          AND completed_status = 0
          AND deleted = 0
        <if test="pageVO.businessType != null">
            AND business_type = #{pageVO.businessType}
        </if>
        <if test="pageVO.notifyType != null">
            AND notify_type = #{pageVO.notifyType}
        </if>
        order by create_time DESC
    </select>

    <select id="getTenantNameList" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskRespVO">
        SELECT
        CONCAT(st.id, '') AS tenantId,
        st.name AS tenantName,
        ei.elder_name,
        bbf.process_instance_id
        FROM
        bpm_business_form bbf
        INNER JOIN elderly_info ei ON bbf.business_id2 = ei.id
        INNER JOIN system_tenant st ON ei.tenant_id = st.id
        WHERE
        bbf.process_instance_id IN
        <foreach collection="processInstanceIds" item="processInstanceId" open="(" separator="," close=")">
            #{processInstanceId}
        </foreach>
    </select>

    <select id="getReadList" resultType="cn.iocoder.yudao.module.bpm.controller.admin.task.vo.elderly.BpmElderlyInfoVO">
        SELECT
            bbf.process_instance_id AS id,
            bbf.read_status
        FROM
            bpm_business_form bbf
        WHERE
            bbf.read_status = 0
            AND bbf.process_instance_id IN
            <foreach collection="ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        GROUP BY bbf.process_instance_id, bbf.read_status
    </select>


</mapper>