package com.ningdatech.pmapi.meta.assembler;

import com.ningdatech.pmapi.meta.model.entity.MetaTag;
import com.ningdatech.pmapi.meta.model.dto.TagDTO;
import com.ningdatech.pmapi.meta.model.dto.TagTreeDTO;
import com.ningdatech.pmapi.meta.model.vo.ResTagTreeVO;
import org.apache.commons.collections4.CollectionUtils;

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

/**
 * @author liuxinxin
 * @date 2022/7/22 上午9:57
 */

public class MetaTagAssembler {

    public static TagDTO toTagDTO(MetaTag metaTagManage) {
        TagDTO tagDTO = new TagDTO();
        tagDTO.setParentCode(metaTagManage.getParentCode());
        tagDTO.setTagCode(metaTagManage.getTagCode());
        tagDTO.setTagLevel(metaTagManage.getTagLevel());
        tagDTO.setTagName(metaTagManage.getTagName());
        return tagDTO;
    }

    public static List<TagTreeDTO> toTagTreeDTOList(List<TagDTO> tagDTOList) {
        if (CollectionUtils.isEmpty(tagDTOList)) {
            return new ArrayList<>();
        }
        return tagDTOList.stream().map(MetaTagAssembler::toTagTreeDTO).collect(Collectors.toList());

    }

    public static TagTreeDTO toTagTreeDTO(TagDTO tagDTO) {
        TagTreeDTO tagTreeDTO = new TagTreeDTO();
        tagTreeDTO.setParentCode(tagDTO.getParentCode());
        tagTreeDTO.setTagCode(tagDTO.getTagCode());
        tagTreeDTO.setTagLevel(tagDTO.getTagLevel());
        tagTreeDTO.setTagName(tagDTO.getTagName());
        return tagTreeDTO;
    }

    public static List<ResTagTreeVO> toTagTreeVOList(List<TagTreeDTO> tagTreeDTOList) {
        List<ResTagTreeVO> resTagTreeVOList = new ArrayList<>();
        for (TagTreeDTO tagTreeDTO : tagTreeDTOList) {
            ResTagTreeVO resTagTreeVO = new ResTagTreeVO();
            resTagTreeVO.setTagLevel(tagTreeDTO.getTagLevel());
            resTagTreeVO.setTagName(tagTreeDTO.getTagName());
            resTagTreeVO.setParentCode(tagTreeDTO.getParentCode());
            resTagTreeVO.setTagCode(tagTreeDTO.getTagCode());
            resTagTreeVO.setUnionCode(tagTreeDTO.getTagName() + "##" + tagTreeDTO.getTagCode());
            if (CollectionUtils.isNotEmpty(tagTreeDTO.getChildren())) {
                resTagTreeVO.setChildren(toTagTreeVOList(tagTreeDTO.getChildren()));
            }
            resTagTreeVOList.add(resTagTreeVO);
        }
        return resTagTreeVOList;
    }
}
