package com.engine.organization.entity.company.dto;

import com.engine.organization.annotation.OrganizationTable;
import com.engine.organization.annotation.TableTitle;
import com.engine.organization.entity.department.dto.DepartmentListDTO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:dxfeng
 * @createTime: 2022/11/24
 * @version: 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@OrganizationTable(pageId = "dcfd9d27-6ba2-11ed-996a-00ffcbed7508")
public class CompListDTO {
    /**
     * 主键id
     */
    private Integer id;
    /**
     * 是否被引用
     */
    private Integer isUsed;
    /**
     * 名称
     */
    @TableTitle(title = "名称", dataIndex = "subCompanyDesc", key = "subCompanyDesc")
    private String subCompanyDesc;

    /**
     * 编号
     */
    @TableTitle(title = "编号", dataIndex = "subCompanyCode", key = "subCompanyCode")
    private String subCompanyCode;

    /**
     * 简称
     */
    @TableTitle(title = "简称", dataIndex = "subCompanyName", key = "subCompanyName")
    private String subCompanyName;

    /**
     * 上级公司
     */
    @TableTitle(title = "上级分部", dataIndex = "supSubComName", key = "supSubComName")
    private String supSubComName;

    private Integer supSubComId;


    @TableTitle(title = "显示顺序", dataIndex = "showOrder", key = "showOrder", sorter = true)
    private Integer showOrder;

    /**
     * 禁用标记
     */
    @TableTitle(title = "启用", dataIndex = "canceled", key = "canceled")
    private Integer canceled;

    /**
     * 操作列
     */
    @TableTitle(title = "", dataIndex = "operate", key = "operate")
    private String operate;

    /**
     * 子节点
     */
    private List<CompListDTO> children;

    public Integer getShowOrder() {
        return null == showOrder ? 0 : showOrder;
    }

    private String orderType;

    public List<CompListDTO> getChildren() {
        if (CollectionUtils.isEmpty(children)) {
            return children;
        }
        if ("asc".equalsIgnoreCase(orderType)) {
            return children.stream().sorted(Comparator.comparing(CompListDTO::getShowOrder)).collect(Collectors.toList());
        } else {
            return children.stream().sorted(Comparator.comparing(CompListDTO::getShowOrder).reversed()).collect(Collectors.toList());
        }
    }
}
