package com.xdap.self_development.domain.view;

import com.definesys.mpaas.query.annotation.SQL;
import com.definesys.mpaas.query.annotation.SQLQuery;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.xdap.self_development.domain.pojo.ApprovalXdapUsersPojo;
import lombok.Data;

import java.sql.Timestamp;
import java.util.List;

// CASE af.flow_status WHEN 'COMPLETE' THEN 1 WHEN 'APPROVING' THEN 2 WHEN 'DRAFT' THEN 3 WHEN 'REJECTED' THEN 4 ELSE 5 END ASC,
@SQLQuery(value = {
        // 查询每个报告最新审批流程
        @SQL(view = "getAllReportApprovalList",
                sql = "WITH RankedFlows AS ( SELECT af.id AS flowId, r.id AS reportId, af.process_title AS title, af.flow_status AS status, af.create_by AS createBy , af.created_time AS createdTime, ac.assignees AS current_users, row_number() OVER (PARTITION BY af.template_id ORDER BY af.created_time DESC) AS rn FROM yfsjglpt_model_approval_flow af LEFT JOIN approval_config ac ON af.id = ac.approval_id AND af.current_node = ac.node_order LEFT JOIN yfsjglpt_model_benchmarking_report r ON af.template_id = r.id WHERE af.flow_type = 2 AND af.flow_status <> 'DRAFT' ) SELECT flowId, reportId, title, status, createBy , createdTime, current_users FROM RankedFlows WHERE rn = 1 AND title LIKE CONCAT('%', #title, '%') AND status LIKE CONCAT('%', #status, '%') AND createdTime LIKE CONCAT('%', #createdTime, '%') AND createBy in (#ids) ORDER BY createdTime DESC")
})
@Data
public class TodoAndReportListView {
    private String reportId;
    private String flowId;
    private String title;
    private String status;
    private String createBy;
    private String currentUsers;
    private String username;
    private List<ApprovalXdapUsersPojo> currentApprovers;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Timestamp createdTime;
}
