-- 根据参数 {"currentUserId":844,"key":"HZJ202512160010","pageNum":1,"pageSize":50} 生成的实际SQL语句

SELECT
    t1.*,
    group_concat(distinct t2.username) as cost_current_reviewer
FROM cost t1
LEFT JOIN cost_current_reviewer t2 ON t1.cost_no = t2.cost_no
-- 因为currentUserId不为null，所以添加用户权限关联查询
LEFT JOIN (
    SELECT * FROM (
        SELECT t3.oa_user_id, cc.company_no 
        FROM bailun_other.oa_user t3
        LEFT JOIN user_role_binding urb ON t3.oa_user_id = urb.oa_user_id
        LEFT JOIN role_cost_company_binding rccb ON urb.role_id = rccb.role_id
        LEFT JOIN cost_company cc ON rccb.cost_company_id = cc.`id`
        WHERE t3.oa_user_id = 844
    ) tt 
    GROUP BY company_no
) t ON t1.company_no = t.company_no
WHERE
    TRUE
-- 添加currentUserId条件：只能查看自己创建的或有权限查看的公司的费用单
AND (t1.create_userid=844 OR t.company_no IS NOT NULL)
-- 因为key不为null，会被分割成keys列表，添加关键字搜索条件
AND (FALSE
    OR t1.cost_plan_no = 'HZJ202512160010'
    OR t1.cost_no LIKE CONCAT('%','HZJ202512160010','%')
    OR t1.type_name LIKE CONCAT('%','HZJ202512160010','%')
    OR t1.company_name LIKE CONCAT('%','HZJ202512160010','%')
    OR t1.bank_card_user LIKE CONCAT('%','HZJ202512160010','%')
)
GROUP BY t1.id
ORDER BY t1.id DESC
-- 分页参数：pageNum=1, pageSize=50，MyBatis会自动添加LIMIT
LIMIT 0, 50;