package com.cico.nest.lcdp.boot.core.system.service.user.dto;

import com.cico.nest.lcdp.boot.core.system.dal.dataobject.dept.DeptDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.permission.RoleDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.user.AdminUserDO;
import com.cico.nest.lcdp.framework.api.dept.dto.LcdpDeptDTO;
import com.cico.nest.lcdp.framework.api.role.dto.LcdpRoleDTO;
import com.cico.nest.lcdp.framework.api.user.dto.LcdpUserDTO;
import lombok.Data;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author zws
 * @date 2024/7/23
 */

@Data
public class AdminUserRoleDeptDTO extends AdminUserDO {

    private List<RoleDO> roleList;

    private DeptDO dept;


    public static List<LcdpUserDTO> toUserDTO(List<AdminUserRoleDeptDTO> adminUserRoleDeptDTOList){
        return adminUserRoleDeptDTOList.stream().map(AdminUserRoleDeptDTO::toUserDTO).toList();
    }


    public static LcdpUserDTO toUserDTO(AdminUserRoleDeptDTO adminUserRoleDeptDTO) {
        LcdpUserDTO userDTO = new LcdpUserDTO();
        userDTO.setId(String.valueOf(adminUserRoleDeptDTO.getId()));
        userDTO.setUsername(adminUserRoleDeptDTO.getUsername());
        userDTO.setNickname(adminUserRoleDeptDTO.getNickname());
        userDTO.setEmail(adminUserRoleDeptDTO.getEmail());
        userDTO.setPhones(Collections.singletonList(adminUserRoleDeptDTO.getMobile()));
        userDTO.setAvatar(adminUserRoleDeptDTO.getAvatar());
        //部门不为空
        Optional.ofNullable(adminUserRoleDeptDTO.getDept())
                .ifPresent(dept -> {
                    userDTO.setSuperior(String.valueOf(dept.getLeaderUserId()));
                    LcdpDeptDTO deptDTO = DeptDO.toDeptDTO(dept);
                    userDTO.setLoginDepts(List.of(deptDTO));
                });
        //角色不为空
        Optional.ofNullable(adminUserRoleDeptDTO.getRoleList())
                .ifPresent(roleList -> {
                    List<LcdpRoleDTO> roleDTOList = RoleDO.toRoleDTO(roleList);
                    userDTO.setLoginRoles(roleDTOList);
                });
        return userDTO;
    }

}
