package com.njcn.rdms.module.system.controller.admin.permission.vo.role;

import cn.hutool.core.util.StrUtil;
import com.njcn.rdms.framework.common.pojo.PageParam;
import com.njcn.rdms.module.system.enums.permission.PermissionScopeTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.AssertTrue;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

import static com.njcn.rdms.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;

@Schema(description = "管理后台 - 角色分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
public class RolePageReqVO extends PageParam {

    @Schema(description = "角色名称，模糊匹配", example = "灿能")
    private String name;

    @Schema(description = "角色标识，模糊匹配", example = "rdms")
    private String code;

    @Schema(description = "展示状态，参见 CommonStatusEnum 枚举类", example = "1")
    private Integer status;

    @Schema(description = "创建时间", example = "[2022-07-01 00:00:00,2022-07-01 23:59:59]")
    @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
    private LocalDateTime[] createTime;

    @Schema(description = "作用域类型，global 或 object", example = "global")
    private String scopeType;

    @Schema(description = "对象类型，当 scopeType=object 时必填", example = "product")
    private String objectType;

    @AssertTrue(message = "scopeType 只能是 global 或 object")
    public boolean isScopeTypeValid() {
        return StrUtil.isBlank(scopeType)
                || PermissionScopeTypeEnum.GLOBAL.getScopeType().equals(scopeType)
                || PermissionScopeTypeEnum.OBJECT.getScopeType().equals(scopeType);
    }

    @AssertTrue(message = "scopeType=object 时 objectType 不能为空")
    public boolean isObjectTypeValid() {
        return !PermissionScopeTypeEnum.OBJECT.getScopeType().equals(scopeType)
                || StrUtil.isNotBlank(objectType);
    }

}
