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

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.definesys.mpaas.query.annotation.*;
import com.definesys.mpaas.query.model.MpaasBasePojo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@SQLQuery(value = {
        @SQL(view = "selectAllParameters",
                sql = "SELECT p.* FROM yfsjglpt_model_template t LEFT JOIN yfsjglpt_model_parameter_and_template pat ON t.id = pat.template_id LEFT JOIN yfsjglpt_model_parameter p ON pat.parameter_id = p.id WHERE t.status = 'COMPLETE'"),
        @SQL(view = "selectTcParameter",
                sql = "SELECT p.* FROM yfsjglpt_model_template t LEFT JOIN yfsjglpt_model_parameter_and_template pat ON t.id = pat.template_id LEFT JOIN yfsjglpt_model_parameter p ON pat.parameter_id = p.id WHERE t.status = 'COMPLETE' AND p.parameter_source = '数据平台手工录入'"),
        @SQL(view = "selectParametersByTIdAndCreateBy",
                sql = "select p.* from yfsjglpt_model_parameter p left join yfsjglpt_model_parameter_and_template tap on p.id = tap.parameter_id where tap.template_id = #templateId and p.created_by = #createdBy order by p.parameter_number"),
        @SQL(view = "selectParametersByTId",
                sql = "select p.* from yfsjglpt_model_parameter p left join yfsjglpt_model_parameter_and_template tap on p.id = tap.parameter_id where tap.template_id = #templateId order by p.parameter_number"),
        @SQL(view = "getAllSubsystemAndPara",
                sql = "SELECT DISTINCT mp.subsystem_name, mp.parameter_name, mp.id, mp.parameter_type, mp.parts_name, mt.order_num FROM yfsjglpt_model_parameter mp INNER JOIN yfsjglpt_model_parameter_and_template mpat ON mp.id = mpat.parameter_id INNER JOIN yfsjglpt_model_template mt ON mpat.template_id = mt.id INNER JOIN ( SELECT template_name, MAX(version) AS max_version FROM yfsjglpt_model_template WHERE status = 'COMPLETE' GROUP BY template_name ) latest ON mt.template_name = latest.template_name AND mt.version = latest.max_version WHERE mt.status = 'COMPLETE' ORDER BY mt.order_num"),
        @SQL(view = "getAllSubsystem",
                sql = "SELECT DISTINCT mp.subsystem_name FROM yfsjglpt_model_parameter mp INNER JOIN yfsjglpt_model_parameter_and_template mpat ON mp.id = mpat.parameter_id INNER JOIN yfsjglpt_model_template mt ON mpat.template_id = mt.id INNER JOIN ( SELECT template_name, MAX(version) AS max_version FROM yfsjglpt_model_template WHERE status = 'COMPLETE' GROUP BY template_name ) latest ON mt.template_name = latest.template_name AND mt.version = latest.max_version WHERE mt.status = 'COMPLETE' ORDER BY mp.subsystem_name"),
        @SQL(view = "selectParametersByTemplateId",
                sql = "select p.* from yfsjglpt_model_parameter p left join yfsjglpt_model_parameter_and_template tap on p.id = tap.parameter_id where p.id in (#parameterIds) and tap.template_id = #templateId"),
        @SQL(view = "selectMaxNumber",
                sql = "SELECT max(parameter_number) AS max_number FROM yfsjglpt_model_parameter p LEFT JOIN yfsjglpt_model_parameter_and_template tap ON p.id = tap.parameter_id WHERE tap.template_id = #templateId")
})
@EqualsAndHashCode(callSuper = true)
@Data
@Table("yfsjglpt_model_parameter")
@NoArgsConstructor
public class ParameterPojo extends MpaasBasePojo {
    /**
     * 参数ID
     */
    @RowID(sequence = "parameter_s", type = RowIDType.UUID)
    private String id;
    /**
     * 参数序号
     */
    @ExcelProperty("序号")
    @ColumnWidth(15)
    private String parameterNumber;
    /**
     * 子系统名称
     */
    @ExcelProperty("子系统")
    @ColumnWidth(20)
    private String subsystemName;
    /**
     * 参数类型
     */
    @ExcelProperty("类型")
    @ColumnWidth(20)
    private String parameterType;
    /**
     * 零部件名称
     */
    @ExcelProperty("零部件名称")
    @ColumnWidth(20)
    private String partsName;
    /**
     * 参数名称
     */
    @ExcelProperty("参数名称")
    @ColumnWidth(20)
    private String parameterName;
    /**
     * 参数单位
     */
    @ExcelProperty("单位")
    @ColumnWidth(15)
    private String unit;

    /**
     * 标明参数值是否为数值
     */
    @ExcelProperty("参数值类型")
    @ColumnWidth(15)
    private String isNumber;

    /**
     * 参数来源
     */
    @ExcelProperty("参数计划来源")
    @ColumnWidth(20)
    private String parameterSource;
    /**
     * 分类属性表编号或公式
     */
    @ExcelProperty("当前可适用的分类属性表编号（需要修订）或公式")
    @ColumnWidth(30)
    private String numberOrFormula;
    /**
     * 填写部门
     */
    @ExcelProperty("填写部门")
    @ColumnWidth(20)
    private String department;
    /**
     * 备注信息
     */
    @ExcelProperty("备注")
    @ColumnWidth(30)
    private String remarks;
    /**
     * 创建者
     */
    @ExcelIgnore
    private String createdBy;
    /**
     * 排序号
     */
    @ExcelIgnore
    @Column(type=ColumnType.CALCULATE)
    private Integer orderNum;
    /**
     * 最大序号
     */
    @ExcelIgnore
    @Column(value = "max_number",type=ColumnType.CALCULATE)
    private Integer maxNumber;

    public ParameterPojo(String parameterNumber, String subsystemName, String parameterType,
                         String partsName, String parameterName, String unit, String parameterSource,
                         String numberOrFormula, String department, String remarks) {
        this.parameterNumber = parameterNumber;
        this.subsystemName = subsystemName;
        this.parameterType = parameterType;
        this.partsName = partsName;
        this.parameterName = parameterName;
        this.unit = unit;
        this.parameterSource = parameterSource;
        this.numberOrFormula = numberOrFormula;
        this.department = department;
        this.remarks = remarks;
    }

    public static String[] getFieldDescriptions() {
        return new String[]{
                "非必填",
                "必填",
                "必填",
                "非必填",
                "必填",
                "非必填",
                "非必填，用于标明参数值是否为数值，如需填写，填入数值或非数值，不填入默认非数值类型",
                "必填，并且为下列之一：数据平台手工录入、TC分类表、公式计算",
                "非必填，如果参数来源为'公式计算'则必填。填写规范：参数名可以是中文、数字、大小写字母、中文括号 `（）`必须以中文开头，不能包含运算符和英文括号；参数名也可以是数字；支持圆周率，必须是小写的`π`，不支持`Π`；负号 `-` 可作为操作数前缀。公式示例：π*(直径（内径）/2)*(直径（内径）/2)",
                "非必填，如果参数来源为'数据平台手工录入'则必填；部门必须为研发总院的部门，如‘研发部’",
                "非必填"};
    }
}
