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;

@SQLQuery(value = {
        // CASE af.flow_status WHEN 'COMPLETE' THEN 1 WHEN 'APPROVING' THEN 2 WHEN 'DRAFT' THEN 3 WHEN 'REJECTED' THEN 4 ELSE 5 END ASC
        @SQL(view = "getAllApproval",
                sql = "WITH RankedFlows AS ( SELECT af.id AS flowId, t.id AS templateId, t.template_name AS templateName, t.version AS version, 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_template t ON af.template_id = t.id WHERE af.flow_type = 1 ) SELECT flowId, templateId, templateName, version, status , createBy, createdTime, current_users FROM RankedFlows WHERE rn = 1 AND templateName LIKE CONCAT('%', #templateName, '%')AND status LIKE CONCAT('%', #status, '%')AND createdTime LIKE CONCAT('%', #createdTime, '%') AND createBy in (#ids) ORDER BY createdTime DESC"),
        @SQL(view = "getApprovalByUserIdAndIds",
                sql = "SELECT af.id AS \"flowId\", t.id AS \"templateId\", t.template_name AS \"templateName\", t.version AS \"version\", af.flow_status AS \"status\" , af.create_by AS \"createBy\", af.created_time AS \"createdTime\" , CASE  WHEN current_node = 'PROOFREAD' THEN  proofread_users WHEN current_node = 'REVIEW' THEN review_users WHEN current_node = 'COUNTERSIGN' THEN countersign_users WHEN current_node = 'APPROVE' THEN approve_users END AS current_users FROM yfsjglpt_model_approval_flow af JOIN yfsjglpt_model_template t ON af.template_id = t.id WHERE t.template_name like CONCAT('%',#templateName, '%') and af.flow_status like CONCAT('%',#status, '%') and af.flow_type = 1 and af.id NOT IN (#ids) AND af.create_by = #createBy"),
})
@Data
public class TodoAndTemplateView {
    private String templateId;
    private String flowId;
    private String templateName;
    private String version;
    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;
}
