<?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.sop.business.mapper.TFFlowLogMapper">

    <select id="findByObjectId" resultType="com.qmth.sop.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.id,
                     tffl.attachment_ids as attachmentIds,
                     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 t.type from t_f_custom_flow t where t.id = tffe.t_f_custom_flow_id) as flowType,
--                      IFNULL((
--                                 select
--                                     GROUP_CONCAT(su.real_name, '(', so.name, ')')
--                                 from
--                                     sys_user su
--                                         left join sys_org so on so.id = su.org_id
--                                 where
--                                     FIND_IN_SET(su.id, tffl.pend_approve_id)),(
--                                 select
--                                     GROUP_CONCAT(su.real_name)
--                                 from
--                                     sys_user su
--                                 where
--                                     FIND_IN_SET(su.id, tffl.pend_approve_id))) as pendApproveUserName,
--                      IFNULL((
--                                 select
--                                     CONCAT(su.real_name, '(', so.name, ')')
--                                 from
--                                     sys_user su
--                                         left join sys_org so on
--                                         so.id = su.org_id
--                                 where
--                                     su.id = tffl.approve_id),su.real_name) as approveUserName,
                     aht.NAME_ as taskName
        from
            t_f_flow_log tffl
            left join sys_user su on su.id = tffl.approve_id
            left join sys_org so on so.id = su.org_id
            join t_f_custom_flow_entity tffe on tffe.id = tffl.object_id
            left join act_hi_taskinst aht on aht.PROC_INST_ID_ = cast(tffl.flow_id as char) and aht.ID_ = cast(tffl.task_id as char)
        where
            tffl.object_id = #{objectId}
        order by
            tffl.create_time desc
    </select>

    <select id="findByLastFlowLog" resultType="com.qmth.sop.business.entity.TFFlowLog">
        select * from t_f_flow_log tffl where tffl.flow_id = #{flowId} order by tffl.create_time desc limit 1
    </select>

    <select id="findByLastFlowLogApproveUser" resultType="java.lang.String">
        select group_concat(tffl.approve_id) from t_f_flow_log tffl where tffl.flow_id = #{flowId} and tffl.approve_setup > 1
    </select>
</mapper>
