/**
 * 模板实体类，用于存储模型模板的基本信息
 *
 * @version 1.0
 * @since 2025
 */
package com.xdap.self_development.domain.pojo;

import com.definesys.mpaas.query.annotation.*;
import com.definesys.mpaas.query.model.MpaasBasePojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;

@SQLQuery(value = {
        @SQL(view = "getLatestTemplateOrder",
                sql = "WITH ranked_templates AS ( SELECT t.*, ROW_NUMBER() OVER (PARTITION BY template_name ORDER BY CASE status WHEN 'COMPLETE' THEN 1 WHEN 'APPROVING' THEN 2 ELSE 3 END, version DESC) AS rn FROM yfsjglpt_model_template t WHERE status IN ('COMPLETE', 'DRAFT', 'APPROVING') ) SELECT rt.order_num, rt.id, rt.template_name, rt.version, rt.status , rt.created_time, rt.published_time, rt.create_by AS \"createBy\" FROM ranked_templates rt WHERE rt.rn = 1 ORDER BY CASE rt.status WHEN 'COMPLETE' THEN 1 WHEN 'APPROVING' THEN 2 WHEN 'DRAFT' THEN 3 WHEN 'RETURNED' THEN 4 WHEN 'REJECTED' THEN 5 ELSE 6 END ASC, rt.created_time DESC"),
        // 查找每个模板的最新版本并且优先级按照已发布、审核中、草稿状态转变（只有这三种状态）
        // CASE ranked.status WHEN 'COMPLETE' THEN 1 WHEN 'APPROVING' THEN 2 WHEN 'DRAFT' THEN 3 WHEN 'RETURNED' THEN 4 WHEN 'REJECTED' THEN 5 ELSE 6 END ASC,
        @SQL(view = "getLatestTemplate",
                sql = "SELECT ROW_NUMBER() OVER (ORDER BY ranked.created_time DESC) AS display_order_num, ranked.id, ranked.template_name, ranked.version, ranked.return_num , ranked.status, ranked.created_time, ranked.published_time, ROW_NUMBER() OVER (ORDER BY ranked.order_num DESC) AS order_num , ranked.create_by AS \"createBy\" FROM ( SELECT t.*, ROW_NUMBER() OVER (PARTITION BY template_name ORDER BY CASE WHEN status = 'COMPLETE' THEN 1 WHEN status = 'APPROVING' THEN 2 ELSE 3 END, version DESC) AS rn FROM yfsjglpt_model_template t WHERE status IN ('COMPLETE', 'DRAFT', 'APPROVING') ) ranked WHERE rn = 1 ORDER BY ranked.created_time DESC"
        ),
        // 获取所有模板的最新版本号（包括所有状态）
        @SQL(view = "getNewTemplate",
                sql = "SELECT t.* FROM yfsjglpt_model_template t JOIN ( SELECT template_name, MAX(version) AS latest_version FROM yfsjglpt_model_template GROUP BY template_name ) latest_template ON t.template_name = latest_template.template_name AND t.version = latest_template.latest_version ")
})
@EqualsAndHashCode(callSuper = true)
@Data
@Table("yfsjglpt_model_template")
@NoArgsConstructor
public class TemplatePojo extends MpaasBasePojo {
    /**
     * 模板ID
     */
    @RowID(sequence = "template_s", type = RowIDType.UUID)
    private String id;
    /**
     * 模板名称
     */
    private String templateName;
    /**
     * 版本号
     */
    private Integer version;
    /**
     * 状态（DRAFT草稿、APPROVING审批中、COMPLETE已完成）
     */
    private String status;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Timestamp createdTime;
    /**
     * 发布时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Timestamp publishedTime;
    /**
     * 排序号
     */
    private Long orderNum;
    /**
     * 创建者
     */
    private String createBy;
    /**
     * 退回次数
     */
    private Integer returnNum;

    /**
     * 最大排序号
     */
    @Column(type = ColumnType.CALCULATE)
    private Integer maxOrder;

    public TemplatePojo(String templateName, Integer version, String status,
                        Timestamp createdTime, Timestamp publishedTime,
                        String createBy, Long orderNum, Integer returnNum) {
        this.templateName = templateName;
        this.version = version;
        this.status = status;
        this.createdTime = createdTime;
        this.publishedTime = publishedTime;
        this.createBy = createBy;
        this.orderNum = orderNum;
        this.returnNum = returnNum;
    }
}
