<?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.qmth.distributed.print.business.mapper.TFFlowLogMapper">

    <select id="findByFlowId" resultType="com.qmth.distributed.print.business.entity.TFFlowLog">
        select * from t_f_flow_log tffl
         join t_f_flow_join tffj on
            tffj.flow_id = tffl.flow_id
         where tffl.school_id = #{schoolId} and tffl.flow_id = #{flowId}
    </select>

    <select id="findByObjectId" resultType="com.qmth.distributed.print.business.bean.result.TFFlowLogResult">
        select
            DISTINCT tffl.approve_id as approveId,
            tffl.approve_setup as approveSetup,
            tffl.approve_remark as approveRemark,
            tffl.approve_operation as approveOperation,
            tffl.pend_approve_id as pendApproveId,
            tffl.create_time as createTime,
            tffl.flow_id as flowId,
            tffl.object_id as objectId,
            tffl.object_table as objectTable,
            tffl.id,
            tffl.paper_attachment_id as paperAttachmentId,
            su.school_id as schoolId,
            su.login_name as loginName,
            su.real_name as realName,
            su.code,
            su.mobile_number as mobileNumber,
            su.org_id as orgId,
            so.name as orgName,
            so.`type` as orgType,
             (select group_concat(su.real_name, '(', so.name, ')' order by FIND_IN_SET(su.id, replace(tffl.pend_approve_id, ', ', ',')))
                 from sys_user su join sys_org so on so.id = su.org_id where FIND_IN_SET(su.id, replace(tffl.pend_approve_id, ', ', ','))) as pendApproveUserName,
            (
                 select
                     CONCAT(su.real_name, '(', so.name, ')')
                 from
                     sys_user su
                         join sys_org so on
                         so.id = su.org_id
                 where
                     su.id = tffl.approve_id) as approveUserName
        from
            t_f_flow_log tffl
        join sys_user su on
            su.id = tffl.approve_id
        join sys_org so on
            so.id = su.org_id
        where
            tffl.object_id = #{objectId}
            and tffl.enable = 1
        order by
            tffl.create_time
    </select>

    <select id="findApprovalInfoByFlowId"
            resultType="com.qmth.distributed.print.business.bean.dto.approvalForm.ApprovalInfo">
        SELECT
            tffl.approve_id AS approveId,
            su.real_name AS approveName,
            tffl.update_time AS time,
            tffl.approve_remark AS remark,
            so.name AS approveOrgName,
            tffl.approve_operation AS approveType
        FROM
            t_f_flow_log tffl
                LEFT JOIN
            sys_user su ON tffl.approve_id = su.id
                LEFT JOIN
            sys_org so ON su.org_id = so.id
        <where>
            <if test="flowId != null and flowId != ''">
                AND tffl.flow_id = #{flowId}
            </if>
        </where>
        ORDER BY tffl.update_time
    </select>

    <select id="findByFlowIdAndTaskId" resultType="com.qmth.distributed.print.business.bean.result.TFFlowLogResult">
        select
            DISTINCT tffl.approve_id as approveId,
                     tffl.approve_setup as approveSetup,
                     tffl.approve_remark as approveRemark,
                     tffl.approve_operation as approveOperation,
                     tffl.pend_approve_id as pendApproveId,
                     tffl.create_time as createTime,
                     tffl.flow_id as flowId,
                     tffl.object_id as objectId,
                     tffl.object_table as objectTable,
                     tffl.id,
                     tffl.paper_attachment_id as paperAttachmentId,
                     tffl.task_id as taskId,
                     su.school_id as schoolId,
                     su.login_name as loginName,
                     su.real_name as realName,
                     su.code,
                     su.mobile_number as mobileNumber,
                     su.org_id as orgId,
                     so.name as orgName,
                     so.`type` as orgType,
                     (select group_concat(su.login_name) from sys_user su where FIND_IN_SET(su.id, replace(tffl.pend_approve_id, ', ', ','))) as pendApproveUserName,
                     su.login_name as approveUserName
        from
            t_f_flow_log tffl
                join sys_user su on
                su.id = tffl.approve_id
                join sys_org so on
                so.id = su.org_id
        <where> 1 = 1
            <if test="flowId != null and flowId != ''">
                AND tffl.flow_id = #{flowId}
            </if>
            <if test="taskId != null and taskId != ''">
                AND tffl.task_id = #{taskId}
            </if>
          and tffl.enable = 1
        </where>
        order by
            tffl.create_time
    </select>

</mapper>
