package com.microservices.system.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.microservices.common.core.annotation.Excel;
import com.microservices.common.core.web.domain.BaseEntity;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.system.api.domain.SysUser;
import lombok.Data;

import java.util.Calendar;
import java.util.Date;

/**
 * GitLink平台用户对象 users
 *
 * @author otto
 * @date 2023-03-26
 */
@Data
public class ForgeUser extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 用户id
     */
    private Long id;

    /**
     * 登录用户名
     */
    @Excel(name = "登录用户名")
    private String login;

    /**
     * $column.columnComment
     */
    @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
    private String hashedPassword;

    /**
     * 名
     */
    @Excel(name = "名")
    private String firstname;

    /**
     * 姓
     */
    @Excel(name = "姓")
    private String lastname;

    /**
     * 邮箱
     */
    @Excel(name = "邮箱")
    private String mail;

    /**
     * 是否管理员
     */
    @Excel(name = "是否管理员")
    private Integer admin;

    /**
     * 状态
     */
    @Excel(name = "状态")
    private Long status;

    /**
     * 最后一次登录时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "最后一次登录时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date lastLoginOn;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date createdOn;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date updatedOn;

    /**
     * 用户类型
     */
    @Excel(name = "用户类型")
    private String type;

    /**
     * 邮箱通知类型
     */
    @Excel(name = "邮箱通知类型")
    private String mailNotification;

    /**
     * 手机号
     */
    @Excel(name = "手机号")
    private String phone;

    /**
     * 用户昵称
     */
    @Excel(name = "用户昵称")
    private String nickname;

    public void setLastLoginOn(Date lastLoginOn) {
        this.lastLoginOn = addEightHours(lastLoginOn);
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = addEightHours(createdOn);
    }

    public void setUpdatedOn(Date updatedOn) {
        this.updatedOn = addEightHours(updatedOn);
    }

    private Date addEightHours(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, 8);
        return cal.getTime();
    }

    public SysUser copyToSysUser() {
        SysUser sysUser = new SysUser();
        sysUser.setGitlinkUserId(this.id);
        sysUser.setUserName(this.login);
        sysUser.setEmail(this.mail);
        sysUser.setPhonenumber(this.phone);
        sysUser.setPassword(SecurityUtils.encryptPassword("hnxjy2023"));
        return sysUser;
    }
}
