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 NoteTagSelectVO {

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

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

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

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

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

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


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

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

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