package com.mortals.xhx.module.site.model;

import com.mortals.xhx.module.area.model.AreaEntity;
import lombok.Data;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 区域前端映射树结构实体类
 *
 * @author zxfei
 * @date 2022-01-12
 */
@Data
public class SiteTreeSelect implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 节点ID
     */
    private String id;

    /**
     * 站点编码
     */
    private String siteCode;

    /**
     * 节点名称
     */
    private String label;
    /**
     * 区域编码
     */
    private String areaCode;
    /**
     * 是否叶子节点
     */
    private Boolean isLeaf;

    /**
     * 经度
     */
    private String longitude;
    /**
     * 纬度
     */
    private String latitude;

    /**
     * 节点类型
     */
    private String type;

    /**
     * 图标
     */
    private String icon;

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

    public SiteTreeSelect(AreaEntity entity) {
        //如果是站点，则替换名称和id

        this.id = entity.getIid();
        this.label = entity.getName();
        if ("False".equalsIgnoreCase(entity.getHaveSonArea())) {
            this.isLeaf = true;
            this.children = new ArrayList();
        } else {
            this.isLeaf = false;
        }
        this.areaCode = entity.getAreaCode();
        this.type = "area";
        this.icon = "el-icon-folder";
    }

    public SiteTreeSelect(AreaEntity entity, Map<String, SiteEntity> siteMap) {
        List<SiteEntity> collect = siteMap.entrySet().stream().filter(f -> f.getKey().startsWith(entity.getAreaCode()))
                .map(m -> m.getValue())
                .collect(Collectors.toList());
        if (!ObjectUtils.isEmpty(collect)) {
            this.id = collect.stream().map(item -> item.getId().toString()).collect(Collectors.joining(","));
            this.label = collect.stream().map(item -> item.getSiteName()).collect(Collectors.joining(","));
            this.siteCode = collect.stream().map(item -> item.getSiteCode()).collect(Collectors.joining(","));
            this.type = "site";
            this.icon = "el-icon-wind-power";
            this.longitude = collect.stream().findFirst().map(item -> item.getLongitude()).orElseGet(() -> "");
            this.latitude = collect.stream().findFirst().map(item -> item.getLatitude()).orElseGet(() -> "");
        } else {
            this.id = entity.getIid();
            this.label = entity.getName();
            this.type = "area";
            this.icon = "el-icon-place";
        }
        if ("False".equalsIgnoreCase(entity.getHaveSonArea())) {
            this.isLeaf = true;
            //this.children = new ArrayList();
        } else {
            this.isLeaf = false;
            this.children = entity.getChildren().stream().map(item -> new SiteTreeSelect(item, siteMap)).collect(Collectors.toList());
        }
        this.areaCode = entity.getAreaCode();


    }

    public SiteTreeSelect(SiteEntity entity) {
        this.id = entity.getId().toString();
        this.label = entity.getSiteName();
        this.isLeaf = true;
        this.type = "site";
        this.icon = "el-icon-document";

        this.longitude = entity.getLongitude();
        this.latitude = entity.getLatitude();

    }

}