package com.yhy.module.core.controller.admin.quota.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Data;

@Schema(description = "管理后台 - 定额基价树保存 Request VO")
@Data
public class QuotaCatalogTreeSaveReqVO {

    @Schema(description = "主键", example = "1")
    private Long id;

    @Schema(description = "关联定额专业节点ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @NotNull(message = "关联定额专业节点ID不能为空")
    private Long catalogItemId;

    @Schema(description = "父节点ID", example = "1")
    private Long parentId;

    @Schema(description = "编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "01")
    @NotBlank(message = "编码不能为空")
    private String code;

    @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "土石方工程")
    @NotBlank(message = "名称不能为空")
    private String name;

    @Schema(description = "单位", example = "m²")
    private String unit;

    @Schema(description = "内容类型：directory-目录，content-内容", requiredMode = Schema.RequiredMode.REQUIRED, example = "directory")
    @NotBlank(message = "内容类型不能为空")
    private String contentType;

    @Schema(description = "排序", example = "1")
    private Integer sortOrder;
    
    /**
     * 自定义 setter 处理前端传递的无效值
     */
    @com.fasterxml.jackson.annotation.JsonSetter("sortOrder")
    public void setSortOrderSafe(Object value) {
        if (value == null) {
            this.sortOrder = null;
        } else if (value instanceof Number) {
            // 如果是数字类型，尝试转换为 Integer
            long longValue = ((Number) value).longValue();
            // 如果值超出 Integer 范围，设置为 null（让后端自动计算）
            if (longValue > Integer.MAX_VALUE || longValue < Integer.MIN_VALUE) {
                this.sortOrder = null;
            } else {
                this.sortOrder = (int) longValue;
            }
        } else {
            this.sortOrder = null;
        }
    }

    @Schema(description = "扩展属性")
    private Map<String, Object> attributes;

    @Schema(description = "注解/备注", example = "这是一个注解")
    private String remark;
}
