package com.cico.nest.lcdp.boot.core.system.dal.dataobject.user;

import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cico.nest.lcdp.boot.tenant.core.db.TenantBaseDO;
import com.cico.nest.lcdp.boot.api.system.enums.common.SexEnum;
import com.cico.nest.lcdp.framework.api.user.dto.LcdpUserDTO;
import com.cico.nest.lcdp.framework.common.enums.CommonStatusEnum;
import com.cico.nest.lcdp.framework.mybatis.core.type.JsonLongSetTypeHandler;
import lombok.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * 管理后台的用户 DO
 *
 * @author zws
 */
@TableName(value = "system_users", autoResultMap = true) // 由于 SQL Server 的 system_user 是关键字，所以使用 system_users
@KeySequence("system_user_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库，可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AdminUserDO extends TenantBaseDO {

    /**
     * 用户ID
     */
    @TableId
    private Long id;
    /**
     * 用户账号
     */
    private String username;
    /**
     * 加密后的密码
     *
     * 因为目前使用 {@link BCryptPasswordEncoder} 加密器，所以无需自己处理 salt 盐
     */
    private String password;
    /**
     * 用户昵称
     */
    private String nickname;
    /**
     * 备注
     */
    private String remark;
    /**
     * 部门 ID
     */
    private String deptId;
    /**
     * 岗位编号数组
     */
    @TableField(typeHandler = JsonLongSetTypeHandler.class)
    private Set<Long> postIds;
    /**
     * 用户邮箱
     */
    private String email;
    /**
     * 手机号码
     */
    private String mobile;
    /**
     * 用户性别
     *
     * 枚举类 {@link SexEnum}
     */
    private Integer sex;
    /**
     * 用户头像
     */
    private String avatar;
    /**
     * 帐号状态
     *
     * 枚举 {@link CommonStatusEnum}
     */
    private Integer status;
    /**
     * 最后登录IP
     */
    private String loginIp;
    /**
     * 最后登录时间
     */
    private LocalDateTime loginDate;

    /**
     * 门户系统权限编码
     */
    private String sysCodeList;

    /**
     * 数据来源
     */
    private String source;

    /**
     * 用户类型，见字典：101101正式用户，101102虚拟用户
     */
    private String userType;

    public AdminUserDO setId(Long id) {
        this.id = id;
        return this;
    }

    public AdminUserDO setLoginIp(String loginIp) {
        this.loginIp = loginIp;
        return this;
    }

    public AdminUserDO setLoginDate(LocalDateTime loginDate) {
        this.loginDate = loginDate;
        return this;
    }

    public AdminUserDO setPassword(String password) {
        this.password = password;
        return this;
    }

    public AdminUserDO setPostIds(Set<Long> postIds) {
        this.postIds = postIds;
        return this;
    }

    public static List<LcdpUserDTO> toLoginUserDTO(List<AdminUserDO> adminUserDOs){
        return adminUserDOs.stream().map(AdminUserDO::toLoginUserDTO).toList();
    }
    public static LcdpUserDTO toLoginUserDTO(AdminUserDO adminUserDO){
        LcdpUserDTO loginUserDTO = new LcdpUserDTO();
        loginUserDTO.setId(String.valueOf(adminUserDO.getId()));
        loginUserDTO.setEmail(adminUserDO.getEmail());
        loginUserDTO.setUsername(adminUserDO.getUsername());
        loginUserDTO.setNickname(adminUserDO.getNickname());
        loginUserDTO.setAvatar(adminUserDO.getAvatar());
        Optional.ofNullable(adminUserDO.getMobile()).ifPresent(phone-> loginUserDTO.setPhones(List.of(phone)));
        Optional.ofNullable(adminUserDO.getTenantId()).ifPresent(tenant-> loginUserDTO.setTenantIds(List.of(tenant)));
        return loginUserDTO;
    }
}
