package com.storlead.system.pojo.vo;

import com.storlead.system.pojo.entity.NoteTagLibraryEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: sp-sales-gotr
 * @description:
 * @author: chenkq
 * @create: 2023-05-11 15:59
 */
@Data
public class NoteTagTreeVO {

    @ApiModelProperty(value = "标签")
    private Long tagId;

    @ApiModelProperty(value = "父id")
    private Long tagParentId;

    @ApiModelProperty(value = "标签名222")
    private String tagName;

    @ApiModelProperty(value = "样式颜色")
    private String tagColor;

    @ApiModelProperty(value = "样式颜色")
    private String bgColor;

    @ApiModelProperty("标签属性标记： 0: 中性 1:正向 2:负面 ")
    private Integer propertyType;

    @ApiModelProperty(value = "出现次数")
    private Integer total = 0;

    @ApiModelProperty(value = "标签百分比(保留四位小数)")
    private String tagPercent;

    @ApiModelProperty(value = "子标签")
    private List<NoteTagTreeVO> children;


    /**
     * List<NoteTagDetailEntity> 转为 List<NoteTagTreeVO>
     * @param tagls
     * @return
     */
    public static List<NoteTagTreeVO> noteTagToNoteTagTree(List<NoteTagLibraryEntity> tagls) {
        if (CollectionUtils.isEmpty(tagls)) {
            return null;
        }
        List<NoteTagTreeVO> simpleTreeVoList = tagls.stream().map(NoteTagTreeVO::noteTagToNoteTagTreeVO).filter(obj -> !Objects.isNull(obj)).collect(Collectors.toList());
        return simpleTreeVoList;
    }

    public static NoteTagTreeVO noteTagToNoteTagTreeVO(NoteTagLibraryEntity tag) {
        if (null == tag) {
            return null;
        }
        NoteTagTreeVO simpleTreeVo = new NoteTagTreeVO();
        simpleTreeVo.setTagId(tag.getTagId());
        simpleTreeVo.setTagParentId(tag.getTagParentId());
        simpleTreeVo.setBgColor(tag.getTagColor());
        simpleTreeVo.setTagColor(tag.getTagColor());
        simpleTreeVo.setTagName(tag.getTagName());
        simpleTreeVo.setPropertyType(tag.getPropertyType());
        return simpleTreeVo;
    }

    public static List<NoteTagTreeVO> createTreeVo(List<NoteTagTreeVO> treels, Long pid) {
        List<NoteTagTreeVO> trees = new ArrayList<>();
        for (NoteTagTreeVO tree : treels) {
            if (pid.equals(tree.getTagParentId())) {
                trees.add(tree);
                List<NoteTagTreeVO> childs =  createTreeVo(treels,tree.getTagId());
                   if (!CollectionUtils.isEmpty(childs)) {
                    if (!CollectionUtils.isEmpty(tree.getChildren())) {
                        childs.addAll(tree.getChildren());
                    }
                    tree.setChildren(childs);
                }
            }
        }
        return trees;
    }
}
