package org.xyzh.api.system.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.xyzh.common.vo.BaseVO;

import org.xyzh.common.dto.sys.TbSysUserDTO;
import org.xyzh.common.dto.sys.TbSysUserInfoDTO;
import org.xyzh.common.utils.crypto.AesEncryptUtil;

import java.util.List;

/**
 * @description    系统用户视图对象
 * @filename    SysUserVO.java
 * @author    yslg
 * @copyright yslg
 * @since 2025-11-05
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "系统用户视图对象")
public class SysUserVO extends BaseVO {
    
    // TbSysUserDTO对应字段
    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "用户编码")
    private String usercode;

    @Schema(description = "密码（敏感信息，仅用于创建/修改）")
    private String password;

    @Schema(description = "邮箱")
    private String email;
    
    @Schema(description = "手机")
    private String phone;

    @Schema(description = "手机号哈希码")
    private String phoneHash;

    @Schema(description = "微信ID")
    private String wechatId;

    @Schema(description = "用户状态")
    private String status;

    @Schema(description = "创建人")
    private String creator;

    // TbSysUserInfoDTO对应字段
    @Schema(description = "用户名")
    private String username;

    @Schema(description = "头像")
    private String avatar;

    @Schema(description = "性别")
    private Integer gender;

    @Schema(description = "等级")
    private Integer level;

    @Schema(description = "身份证号")
    private String idCard;

    @Schema(description = "地址")
    private String address;

    // 关联信息
    @Schema(description = "用户部门角色列表")
    private List<UserDeptRoleVO> deptRoles;

    @Schema(description = "用户角色权限列表")
    private List<PermissionVO> rolePermissions;

    @Schema(description = "用户视图权限列表")
    private List<PermissionVO> viewPermissions;

    public void setPhone(String phone){
        this.phone = phone;
        this.phoneHash = AesEncryptUtil.maskPhone(phone);
    }

    public static TbSysUserDTO toDTO(SysUserVO vo) {
        if (vo == null) {
            return null;
        }
        TbSysUserDTO dto = new TbSysUserDTO();
        dto.setUserId(vo.getUserId());
        dto.setUsercode(vo.getUsercode());
        dto.setPassword(vo.getPassword());
        dto.setEmail(vo.getEmail());
        dto.setPhone(vo.getPhone());
        dto.setWechatId(vo.getWechatId());
        dto.setStatus(vo.getStatus());
        dto.setOptsn(vo.getOptsn());
        dto.setCreator(vo.getCreator());
        dto.setDeptPath(vo.getDeptPath());
        dto.setRemark(vo.getRemark());
        dto.setCreateTime(vo.getCreateTime());
        dto.setUpdateTime(vo.getUpdateTime());
        dto.setDeleteTime(vo.getDeleteTime());
        dto.setDeleted(vo.getDeleted());
        return dto;
    }

        /**
     * 将 SysUserVO 转换为 TbSysUserInfoDTO
     */
    public static TbSysUserInfoDTO toUserInfoDTO(SysUserVO userVO) {
        TbSysUserInfoDTO userInfoDTO = new TbSysUserInfoDTO();
        userInfoDTO.setUserId(userVO.getUserId());
        userInfoDTO.setUsername(userVO.getUsername());
        userInfoDTO.setAvatar(userVO.getAvatar());
        userInfoDTO.setGender(userVO.getGender());
        userInfoDTO.setLevel(userVO.getLevel());
        userInfoDTO.setIdCard(userVO.getIdCard());
        userInfoDTO.setAddress(userVO.getAddress());
        
        // 继承自 BaseDTO 的字段
        userInfoDTO.setOptsn(userVO.getOptsn());
        userInfoDTO.setCreator(userVO.getCreator());
        userInfoDTO.setDeptPath(userVO.getDeptPath());
        userInfoDTO.setRemark(userVO.getRemark());
        userInfoDTO.setCreateTime(userVO.getCreateTime());
        userInfoDTO.setUpdateTime(userVO.getUpdateTime());
        userInfoDTO.setDeleteTime(userVO.getDeleteTime());
        userInfoDTO.setDeleted(userVO.getDeleted());
        
        return userInfoDTO;
    }
}

