SELECT l.user_id AS uid, l.user_name AS uname, 1 AS leave_app, 0 AS leave_approve, 0 AS borrow_app, 0 AS inbound_cnt, COALESCE(l.duration,0) AS leave_duration
SELECT l.user_id AS uid, l.user_name AS uname, 1 AS leave_app, 0 AS leave_approve, 0 AS borrow_app, 0 AS inbound_cnt, COALESCE(l.duration,0) AS leave_duration
FROM jl_key_dm_leave_application l
FROM jl_key_dm_leave_application l
<where>
<where>
<iftest="start != null and start != ''">
AND l.is_used = 1 AND l.STATUS != 0
AND l.create_time > #{start}
<iftest="start != null and start != ''">
</if>
AND l.start_time > #{start}
<iftest="end != null and end != ''">
</if>
AND l.create_time < #{end}
<iftest="end != null and end != ''">
</if>
AND l.start_time < #{end}
</where>
</if>
UNION ALL
</where>
SELECT r.approver_id AS uid, r.approver_name AS uname, 0, CASE WHEN r.approver_result = 1 THEN 1 ELSE 0 END, 0, 0, 0
UNION ALL
FROM jl_key_dm_leave_approval_record r
SELECT r.approver_id AS uid, r.approver_name AS uname, 0, CASE WHEN r.approver_result = 1 THEN 1 ELSE 0 END, 0, 0, 0
<where>
FROM jl_key_dm_leave_approval_record r
<iftest="start != null and start != ''">
<where>
AND r.approver_time > #{start}
AND r.is_used = 1 AND r.STATUS = 2
</if>
<iftest="start != null and start != ''">
<iftest="end != null and end != ''">
AND r.approver_time > #{start}
AND r.approver_time < #{end}
</if>
</if>
<iftest="end != null and end != ''">
</where>
AND r.approver_time < #{end}
UNION ALL
</if>
SELECT b.applicant_id AS uid, b.applicant_name AS uname, 0, 0, 1, 0, 0
</where>
FROM jl_key_dm_borrow_application b
UNION ALL
<where>
SELECT b.applicant_id AS uid, b.applicant_name AS uname, 0, 0, 1, 0, 0
<iftest="start != null and start != ''">
FROM jl_key_dm_borrow_application b
AND b.create_time > #{start}
<where>
</if>
AND b.is_used = 1 AND b.approval_status != 0
<iftest="end != null and end != ''">
<iftest="start != null and start != ''">
AND b.create_time < #{end}
AND b.submit_time > #{start}
</if>
</if>
</where>
<iftest="end != null and end != ''">
UNION ALL
AND b.submit_time < #{end}
SELECT o.receiver_id AS uid, o.receiver_name AS uname, 0, 0, 0, 1, 0
</if>
FROM jl_key_dm_outbound_record o
</where>
<where>
UNION ALL
<iftest="start != null and start != ''">
SELECT ir.create_by AS uid, du.`name` AS uname, 0, 0, 0, 1, 0
AND o.create_time > #{start}
FROM jl_key_dm_inbound_record ir LEFT JOIN jl_key_dm_user du on ir.create_by = du.id
</if>
<where>
<iftest="end != null and end != ''">
AND ir.is_used = 1
AND o.create_time < #{end}
<iftest="start != null and start != ''">
</if>
AND ir.create_time > #{start}
</where>
</if>
<iftest="end != null and end != ''">
AND ir.create_time < #{end}
</if>
</where>
) t
) t
GROUP BY uid, uname
GROUP BY uid, uname
ORDER BY leave_applications DESC, borrow_applications DESC
ORDER BY leave_applications DESC, borrow_applications DESC