package com.yc.ship.module.miniapplet.convert.catalogue;

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.miniapplet.controller.admin.catalogue.vo.CatalogueRespVO;
import com.yc.ship.module.miniapplet.controller.admin.content.vo.ContentRespVO;
import com.yc.ship.module.miniapplet.dal.dataobject.catalogue.CatalogueDO;
import com.yc.ship.module.miniapplet.dal.dataobject.content.ContentDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper
public interface CatalogueConvert {

    CatalogueConvert INSTANCE = Mappers.getMapper(CatalogueConvert.class);

    default List<CatalogueRespVO> convertCatalogueList(List<CatalogueDO> list, Map<Long, CatalogueDO> catalogueMap) {
        return CollectionUtils.convertList(list, catalogue -> {
            CatalogueRespVO catalogueVO = BeanUtils.toBean(catalogue, CatalogueRespVO.class);
            MapUtils.findAndThen(catalogueMap, catalogue.getParentId(), c -> catalogueVO.setParentName(c.getName()));
            return catalogueVO;
        });
    }

    default List<ContentRespVO> convertContentList(List<ContentDO> list, Map<Long, CatalogueDO> catalogueMap) {
        return CollectionUtils.convertList(list, content -> {
            ContentRespVO contentVO = BeanUtils.toBean(content, ContentRespVO.class);
            MapUtils.findAndThen(catalogueMap, contentVO.getCatalogueId(), c -> contentVO.setCatalogueName(c.getName()));
            return contentVO;
        });
    }


}
