package cn.reghao.autodop.dmaster.rbac.model.dto;

import cn.reghao.autodop.dmaster.rbac.model.po.Role;
import cn.reghao.jdkutil.converter.DateTimeConverter;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Pattern;

/**
 * @author reghao
 * @date 2021-07-14 14:14:07
 */
@NoArgsConstructor
@Data
public class RoleDTO {
    private Integer roleId;
    @Pattern(regexp = "^\\w+$", message = "角色只能是数字、英文字符和下划线")
    private String title;
    @Pattern(regexp = "^[\\u4e00-\\u9fa5]{0,}$", message = "角色名只能是中文字符")
    private String name;
    @Length(max = 100, message = "描述的长度不超过 100 个中文字符")
    private String description;
    private String createTime;

    public RoleDTO(Role role) {
        this.roleId = role.getId();
        this.title = role.getTitle().split("ROLE_")[1];
        this.name = role.getName();
        this.description = role.getDescription();
        this.createTime = DateTimeConverter.format(role.getCreateTime());
    }

    public Role to() {
        Role role = new Role();
        if (this.getRoleId() != null) {
            role.setId(this.getRoleId());
        }

        role.setTitle("ROLE_" + this.getTitle());
        role.setName(this.getName());
        role.setDescription(this.getDescription());
        return role;
    }
}
