package com.gyee.wisdom.dao.sql.system.entity;

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.ToData;
import com.gyee.wisdom.common.data.system.model.User;
import com.gyee.wisdom.dao.sql.ModelConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Data
@NoArgsConstructor
@ToString
@EqualsAndHashCode()
@Entity
@Table(name = ModelConstants.USER_)
public class UserEntity implements ToData<User> {
    /**
     * 主键ID
     */
    @Id
    //@GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = ModelConstants.ID_)
    private String id;
    /**
     * 登录名称
     */
    @Column(name = ModelConstants.USER_NAME)
    private String userName;
    /**
     * 密码
     */
    @Column(name = ModelConstants.PASSWORD_)
    private String password;
    /**
     * 密码加密的盐
     */
    @Column(name = ModelConstants.SALT_)
    private String salt;
    /**
     * 邮箱
     */
    @Column(name = ModelConstants.EMAIL_)
    private String email;
    /**
     * 是否禁用
     */
    @Column(name = ModelConstants.DISABLED_)
    // @org.hibernate.annotations.Type(type="yes_no")
    private boolean disabled;
    /**
     * 创建时间
     */
    @Column(name = ModelConstants.CREATE_TIME)
    private Date createTime;
    /**
     * 最后登录时间
     */
    @Column(name = ModelConstants.LAST_TIME)
    private Date lastTime;
    /**
     * 中文名称
     */
    @Column(name = ModelConstants.CHINESE_NAME)
    private String chineseName;
    /**
     * 联系电话
     */
    @Column(name = ModelConstants.CONTRACT_TEL)
    private String contractTel;
    /**
     * app注册ID
     */
    @Column(name = ModelConstants.REGISTION_ID)
    private String registionId;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = ModelConstants.USER_ROLE,
            joinColumns = @JoinColumn(name = ModelConstants.USER_ID, referencedColumnName = ModelConstants.ID_),
            inverseJoinColumns = @JoinColumn(name = ModelConstants.ROLE_ID, referencedColumnName = ModelConstants.ID_))
    private List<RoleEntity> roles;


    public UserEntity(User user) {
        this.id = user.getId();
        this.chineseName = user.getChineseName();
        this.contractTel = user.getContractTel();
        this.createTime = user.getCreateTime();
        this.disabled = user.isDisabled();
        this.email = user.getEmail();
        this.lastTime = user.getLastTime();
        this.password = user.getPassword();
        this.registionId = user.getRegistionId();
        this.salt = user.getSalt();
        this.userName = user.getUserName();

        List<RoleEntity> entityList = new ArrayList<>();
        if (user.getRoles() == null) {
            user.setRoles(new ArrayList<>());
        } else {
            for (int i = 0; i < user.getRoles().size(); i++) {
                RoleEntity roleEntity = new RoleEntity(user.getRoles().get(i));
                entityList.add(roleEntity);
            }
        }
        this.roles = entityList;

    }

    @Override
    public User toData() {
        User user = new User();
        user.setPassword(password);
        user.setId(id);
        user.setContractTel(contractTel);
        user.setEmail(email);
        user.setUserName(userName);
        user.setChineseName(chineseName);
        user.setCreateTime(createTime);
        user.setDisabled(disabled);
        user.setRegistionId(registionId);
        user.setSalt(salt);
        user.setLastTime(lastTime);
        //user.setRoles(DataUtil.convertDataList(roles));

        return user;
    }


}
