package com.tourism.webadmin.app.website.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.common.additional.config.ApplicationConfig;
import com.tourism.common.additional.utils.UrlConvertUtils;
import com.tourism.webadmin.app.website.service.TourGroupTypeService;
import com.tourism.webadmin.app.website.vo.TourGroupTreeTypeVo;
import com.tourism.webadmin.back.dao.TourImGroupTypeMapper;
import com.tourism.webadmin.back.model.TourImGroupType;
import com.tourism.webadmin.back.model.constant.IsTrue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class TourGroupTypeServiceImpl extends ServiceImpl<TourImGroupTypeMapper, TourImGroupType> implements TourGroupTypeService {
    @Autowired
    private ApplicationConfig applicationConfig;


    @Override
    public List<TourGroupTreeTypeVo> treeType() {
        List<TourImGroupType> list = lambdaQuery().eq(TourImGroupType::getEnable, IsTrue.TRUE).list();
        //父级菜单
        List<TourImGroupType> collect = list.stream().filter(item -> item.getParentId()==0).collect(Collectors.toList());
        List<TourGroupTreeTypeVo> parentList = BeanUtil.copyToList(collect, TourGroupTreeTypeVo.class);
        //子级菜单
        List<TourGroupTreeTypeVo> vos = getChildren(parentList, list);
        //图片路径转换
        vos.forEach(item->{
            if(item.getTypeIcon()!=null){
                item.setTypeIcon(UrlConvertUtils.urlConvertSingle(applicationConfig.getHostIpPort(),item.getTypeIcon()));
            }
        });
        return vos;
    }
    public List<TourGroupTreeTypeVo> getChildren(List<TourGroupTreeTypeVo> parentList, List<TourImGroupType> list){
        for (TourImGroupType tourImGroupType : list) {
            for (TourGroupTreeTypeVo tourGroupTreeTypeVo : parentList) {
                if(tourImGroupType.getParentId().toString().equals(tourGroupTreeTypeVo.getId())){
                    TourGroupTreeTypeVo bean = BeanUtil.toBean(tourImGroupType, TourGroupTreeTypeVo.class);
                    tourGroupTreeTypeVo.getChildren().add(bean);
                }
            }
        }
        return parentList;
    }
}
