package jnpf.model.authority.dto.menu;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jnpf.model.authority.po.FtbPermissionRole;
import jnpf.model.authority.po.FtbPermissionRoleMenu;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

@Data
public class FtbPermissionRoleAddDTO {

    @JsonIgnore
    public String id;
    /**
     * 角色名称
     */
    @NotBlank(message = "角色名称不能为空")
    @Size(max = 10, message = "角色名称长度不能超过10")
    private String roleName;

    /**
     * 角色描述
     */
    @Size(max = 200, message = "角色描述长度不能超过200")
    private String roleDescription;

    /**
     * 菜单和功能权限参数体
     */
    @Valid
    private List<PermissionRoleInnerDTO> permissionRoleInners = new ArrayList<>();

    @Data
    public static class PermissionRoleInnerDTO {
        /**
         * 是否开启权限0不开启1开启
         */
        @NotNull(message = "是否开启权限不能为空")
        private Integer roleEnabled;

        /**
         * 侧边栏菜单webId
         */
        private String functionWebId;

        /**
         * 侧边栏菜单appId
         */
        private String functionAppId;

        /**
        * 侧边栏菜单PCID
        */
        private String functionPcId;

        /**
         * 权限适用范围,0全部1所在组织和下级组织员工,2所在组织员工,3仅下属,4指定组织
         */
        private Integer scopePermission;

        /**
         * 指定组织id集合，英文逗号分割
         */
        private String specifyOrgIds;

        /**
         * 侧边栏菜单页面webId，英文逗号分割
         */
        private String functionPageWebId;

        /**
         * 侧边栏菜单页面appId，英文逗号分割
         */
        private String functionPageAppId;

        /**
         * 侧边栏菜单页面PCID，英文逗号分割
         */
        private String functionPagePcId;

        /**
         * 功能菜单及子页面主键id集合
         */
        private List<String> memberIds;

        public FtbPermissionRoleMenu convert(PermissionRoleInnerDTO data) {
            if (data == null) {
                return null;
            }
            FtbPermissionRoleMenu ftbPermissionRoleMenu = new FtbPermissionRoleMenu();
            ftbPermissionRoleMenu.setRoleEnabled(data.getRoleEnabled());
            ftbPermissionRoleMenu.setFunctionWebId(data.getFunctionWebId());
            ftbPermissionRoleMenu.setFunctionAppId(data.getFunctionAppId());
            ftbPermissionRoleMenu.setScopePermission(data.getScopePermission());
            ftbPermissionRoleMenu.setSpecifyOrgIds(data.getSpecifyOrgIds());
            ftbPermissionRoleMenu.setFunctionPageWebId(data.getFunctionPageWebId());
            ftbPermissionRoleMenu.setFunctionPageAppId(data.getFunctionPageAppId());
            ftbPermissionRoleMenu.setFunctionPcId(data.getFunctionPcId());
            ftbPermissionRoleMenu.setFunctionPagePcId(data.getFunctionPagePcId());
            return ftbPermissionRoleMenu;
        }

    }

    public FtbPermissionRole convertFtbPermissionRole(FtbPermissionRoleAddDTO ftbPermissionRoleAddDTO) {
        if (ftbPermissionRoleAddDTO == null) {
            return null;
        }
        FtbPermissionRole ftbPermissionRole = new FtbPermissionRole();
        if(StringUtils.isNotEmpty(ftbPermissionRoleAddDTO.getId())) ftbPermissionRole.setId(ftbPermissionRoleAddDTO.getId());
        ftbPermissionRole.setRoleName(ftbPermissionRoleAddDTO.getRoleName());
        ftbPermissionRole.setRoleDescription(ftbPermissionRoleAddDTO.getRoleDescription());
        return ftbPermissionRole;
    }

}
