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.Menu;
import com.gyee.wisdom.common.data.system.model.Role;
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.List;

@Data
@NoArgsConstructor
@ToString
@EqualsAndHashCode()
@Entity
@Table(name = ModelConstants.MENU_)
public class MenuEntity implements ToData<Menu> {
    /**
     * 主键ID
     */
    @Id
    //  @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = ModelConstants.ID_)
    private String id;


    /**
     * 菜单名称
     */
    @Column(name = ModelConstants.LABLEL_)
    private String label;

    /**
     * 路径
     */
    @Column(name = ModelConstants.PAHT_)
    private String path;

    /**
     * 排序
     */
    @Column(name = ModelConstants.ORDER_NO)
    private int orderNo;

    /**
     * 层级
     */
    @Column(name = ModelConstants.LAYER_)
    private int layer;

    /**
     * 链接地址
     */
    @Column(name = ModelConstants.URL_)
    private String url;

    /**
     * 类型
     */
    @Column(name = ModelConstants.TYPE_)
    private int type;

    /**
     * 样式
     */
    @Column(name = ModelConstants.STYLE_)
    private String style;

    /**
     * 是否禁用
     */
    @Column(name = ModelConstants.DISABLED_)
    private boolean disabled;

    @ManyToMany()
    @JoinTable(
            name = ModelConstants.ROLE_MENU,
            joinColumns = @JoinColumn(name = ModelConstants.MENU_ID, referencedColumnName = ModelConstants.ID_),
            inverseJoinColumns = @JoinColumn(name = ModelConstants.ROLE_ID, referencedColumnName = ModelConstants.ID_))
    private List<RoleEntity> roles;


    public MenuEntity(Menu menu) {
        this.id = menu.getId();
        this.label = menu.getLabel();
        this.path = menu.getPath();
        this.orderNo = menu.getOrderNo();
        this.layer = menu.getLayer();
        this.url = menu.getUrl();
        this.type = menu.getType();
        this.style = menu.getStyle();
        this.disabled = menu.isDisabled();
        List<RoleEntity> entityList = new ArrayList<>();
        if (menu.getRoles() == null) {
            menu.setRoles(new ArrayList<>());
        } else {
            for (int i = 0; i < menu.getRoles().size(); i++) {
                RoleEntity roleEntity = new RoleEntity(menu.getRoles().get(i));
                entityList.add(roleEntity);
            }
        }


        this.roles = entityList;
    }

    @Override
    public Menu toData() {
        Menu data = new Menu();
        data.setId(this.id);
        data.setLabel(this.label);
        data.setPath(this.path);
        data.setOrderNo(this.orderNo);
        data.setLayer(this.layer);
        data.setUrl(this.url);
        data.setType(this.type);
        data.setStyle(this.style);
        data.setDisabled(this.disabled);
        data.setRoles(DataUtil.convertDataList(this.roles));
        return data;
    }
}
