/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.sys.service.dto;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.jeeplus.common.excel.annotation.ExcelDictProperty;
import com.jeeplus.config.swagger.IgnoreSwaggerParameter;
import com.jeeplus.core.excel.converter.ExcelDictDTOConverter;
import com.jeeplus.core.excel.converter.ExcelOfficeDTOConverter;
import com.jeeplus.core.excel.converter.ExcelPostListDTOConverter;
import com.jeeplus.core.excel.converter.ExcelRoleListDTOConverter;
import com.jeeplus.core.query.Query;
import com.jeeplus.core.query.QueryType;
import com.jeeplus.core.service.dto.BaseDTO;
import com.jeeplus.sys.feign.ITenantApi;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户Entity
 *
 * @author jeeplus
 * @version 2021-12-05
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class UserSignatureScribeDTO extends BaseDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * openId
     */
    @ApiModelProperty("用户关注公众号的唯一id")
    private String openId;

    /**
     * 财务公司签字注师被选择次数
     */
    private Integer accountantUserFlag;

    /**
     * 执业资格证类型
     */
    private String certType;

    /**
     * 前端组件传‘true’则查询全部
     */
    private String selectAll;

    /**
     * 角色信息
     */
    private List<RoleDTO> roleList;

    private String manageOfficeIds;   //管理的部门id

    /**
     * 管理的部门名称
     */
    private List<String> manageOfficeNameList;

    /**
     * 执业资格证列表
     */
    private List<CertDTO> certDTOList;

    private OfficeDTO corporationDTO;   //公司信息
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    private Date updatePasswordDate;	//修改密码时间

    private String UpPassword;//用户的密码是否修改


    /**
     * 登录名
     */
    @Length(min = 1, max = 100)
    @Query
    private String loginName;

    /**
     * 密码
     */
    @Length(min = 1, max = 100)
    @ExcelIgnore
    private String password;


    /**
     * 姓名
     */
    @Length(min = 1, max = 100)
    @Query(tableColumn = "a.name")
    @ExcelProperty("姓名")
    private String name;


    /**
     * 工号
     */
    //@Length(min = 1, max = 100)
    private String no;

    /**
     * 归属公司
     */
    @NotNull(message = "公司不能为空")
    @Query(type = QueryType.EQ, tableColumn = "c.id", javaField = "companyDTO.id")
    @IgnoreSwaggerParameter
    private OfficeDTO companyDTO;

    /**
     * 归属部门
     */
    @NotNull(message = "部门不能为空")
    @Query(type = QueryType.EQ, tableColumn = "o.id", javaField = "officeDTO.id")
    @IgnoreSwaggerParameter
    @ExcelProperty(value = "部门", converter = ExcelOfficeDTOConverter.class)
    private OfficeDTO officeDTO;


    /**
     * 财务公司签字注师被选择次数
     */
    @ExcelProperty("签章份数")
    private Integer accountantUserCount;

    /**
     * 邮箱
     */
    @Email(message = "邮箱格式不正确")
    @Length(min = 0, max = 100)
    private String email;

    /**
     * 电话
     */
    @Length(min = 0, max = 100)
    private String phone;

    /**
     * 手机
     */
    @Length(min = 0, max = 100)
    private String mobile;

    /**
     * 最后登录IP
     */
    @IgnoreSwaggerParameter
    @ExcelIgnore
    private String loginIp;

    /**
     * 最后登录日期
     */
    @IgnoreSwaggerParameter
    @ExcelIgnore
    private Date loginDate;

    /**
     * 是否允许登录
     */
    private String loginFlag;

    /**
     * 头像
     */
    @ExcelIgnore
    private String photo;

    /**
     * 二维码
     */
    @ExcelIgnore
    private String qrCode;

    /**
     * 原登录名
     */
    @ExcelIgnore
    private String oldLoginName;

    /**
     * 旧密码
     */
    @ExcelIgnore
    private String oldPassword;

    /**
     * 新密码
     */
    @ExcelIgnore
    private String newPassword;

    /**
     * 签名
     */
    @ExcelIgnore
    private String sign;

    /**
     * 登录token
     */
    @ExcelIgnore
    private String token;

    /**
     * 备注
     */
    private String remarks;

    /**
     * 超级管理员标志
     */
    @ExcelIgnore
    private boolean isAdmin;

    /**
     * 是否获取其他服务待办
     */
    private String otherServiceFlag;

    /**
     * 根据角色查询用户条件
     */
    @Query(type = QueryType.EQ, javaField = "roleDTO.id", tableColumn = "r.id")
    @IgnoreSwaggerParameter
    @ExcelIgnore
    private RoleDTO roleDTO;

    /**
     * 根据岗位查询用户
     */
    @ExcelIgnore
    @Query(type = QueryType.EQ, javaField = "postDTO.id", tableColumn = "p.id")
    private PostDTO postDTO;

    /**
     * 拥有角色列表
     */
    @IgnoreSwaggerParameter
    @JsonIgnore
    private List <RoleDTO> roleDTOList = Lists.newArrayList ( );

    /**
     * 拥有岗位列表
     */
    @IgnoreSwaggerParameter
    @JsonIgnore
    private List <PostDTO> postDTOList = Lists.newArrayList ( );

    public UserSignatureScribeDTO() {
        super ( );
    }

    public UserSignatureScribeDTO(String id) {
        super ( id );
    }


    /**
     * 获取包含的角色id列表
     *
     * @return
     */
    public List <String> getRoleIdList() {
        if ( roleDTOList == null ) {
            return Lists.newArrayList ( );
        }
        List <String> roleIdList = roleDTOList.stream ( ).map ( roleDTO -> roleDTO.getId ( ) ).collect ( Collectors.toList ( ) );
        return roleIdList;
    }

    /**
     * 设置角色
     *
     * @param roleIdList
     */
    public void setRoleIdList(List <String> roleIdList) {
        for (String roleId : roleIdList) {
            RoleDTO roleDTO = new RoleDTO ( roleId );
            roleDTOList.add ( roleDTO );
        }
    }

    /**
     * 获取包含的岗位id列表
     *
     * @return
     */
    public List <String> getPostIdList() {
        if ( postDTOList == null ) {
            return Lists.newArrayList ( );
        }
        List <String> postIdList = postDTOList.stream ( ).map ( postDTO -> postDTO.getId ( ) ).collect ( Collectors.toList ( ) );
        return postIdList;
    }

    /**
     * 设置岗位
     *
     * @param postIdList
     */
    public void setPostIdList(List <String> postIdList) {
        for (String postId : postIdList) {
            PostDTO postDTO = new PostDTO ( postId );
            postDTOList.add ( postDTO );
        }
    }


    /**
     * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
     */
    public String getRoleNames() {
        List <String> roleNames = roleDTOList.stream ( ).map ( roleDTO -> roleDTO.getName ( ) ).collect ( Collectors.toList ( ) );
        return StrUtil.join ( ",", roleNames );
    }

    public String getRoleIds() {
        List <String> roleIds = roleDTOList.stream ( ).map ( roleDTO -> roleDTO.getId ( ) ).collect ( Collectors.toList ( ) );
        return StrUtil.join ( ",", roleIds );
    }

    /**
     * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
     */
    public String getPostNames() {
        List <String> postNames = postDTOList.stream ( ).map ( postDTO -> postDTO.getName ( ) ).collect ( Collectors.toList ( ) );
        return StrUtil.join ( ",", postNames );
    }

    public String getPostIds() {
        List <String> postIds = roleDTOList.stream ( ).map ( postDTO -> postDTO.getId ( ) ).collect ( Collectors.toList ( ) );
        return StrUtil.join ( ",", postIds );
    }


    /**
     * 获取租户的id缓存key
     */
    public String getIdCacheKey() {

        return getId ( );
    }

    /**
     * 获取租户的loginName缓存key
     */
    public String getLoginNameCacheKey() {
        String tenantId;
        if ( this.getTenantDTO ( ) != null ) {
            tenantId = this.getTenantDTO ( ).getId ( );
        } else {
            tenantId = SpringUtil.getBean ( ITenantApi.class ).getCurrentTenantId ( );
        }
        return tenantId + ":" + getLoginName ( );
    }

    public String getOldLoginNameCacheKey() {
        String tenantId;
        if ( this.getTenantDTO ( ) != null ) {
            tenantId = this.getTenantDTO ( ).getId ( );
        } else {
            tenantId = SpringUtil.getBean ( ITenantApi.class ).getCurrentTenantId ( );
        }
        return tenantId + ":" + getOldLoginName ( );
    }


}
