package com.zfire.mall.manager.convert;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.config.TradeConfigAddParam;
import com.zfire.mall.manager.bean.config.TradeConfigEditParam;
import com.zfire.mall.manager.bean.config.TradeConfigListLevelVO;
import com.zfire.mall.manager.bean.config.TradeConfigListVO;
import com.zfire.mall.manager.logic.config.TradeConfigLogic;
import com.zfire.mall.manager.plus.entity.TradeCategoryConfig;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

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

/**
 * @author qinrongjun
 * @description
 * @date 2023/7/26 14:24 星期三
 */
@Mapper(config = CommonMapperConfig.class, imports = {TradeConfigLogic.class})
public interface TradeConfigConvert {
    TradeConfigConvert INSTANCE = Mappers.getMapper(TradeConfigConvert.class);

    @Mapping(target = "parentId", expression = "java(StringUtils.defaultIfBlank(var0.getParentId(), TradeConfigLogic.DEFAULT_TOP_LEVEL_ID))")
    @Mapping(target = "createName", source = "username")
    TradeCategoryConfig toDO(TradeConfigAddParam var0, String username);

    @Mapping(target = "parentId", expression = "java(StringUtils.defaultIfBlank(var0.getParentId(), TradeConfigLogic.DEFAULT_TOP_LEVEL_ID))")
    @Mapping(target = "updateName", source = "username")
    TradeCategoryConfig toDO(TradeConfigEditParam var0, String username);

    @Mapping(target = "parentName", expression = "java(parentNameByIdMap.get(var0.getParentId()))")
    TradeConfigListVO toVO(TradeCategoryConfig var0, @Context Map<String, String> parentNameByIdMap);

    Page<TradeConfigListVO> toPageVO(IPage<TradeCategoryConfig> var0, @Context Map<String, String> parentNameByIdMap);

    List<TradeConfigListLevelVO> toLevelVOList(List<TradeCategoryConfig> var0List, @Context Map<String, List<TradeCategoryConfig>> childByParentIdMap);

    @Mapping(target = "childList", expression = "java(toVOList(childByParentIdMap.get(var0.getId()),var0.getName()))")
    TradeConfigListLevelVO toLevelVO(TradeCategoryConfig var0, @Context Map<String, List<TradeCategoryConfig>> childByParentIdMap);


    @Mapping(target = "parentName", expression = "java(parentName)")
    TradeConfigListLevelVO toVO(TradeCategoryConfig var0, @Context String parentName);

    List<TradeConfigListLevelVO> toVOList(List<TradeCategoryConfig> childList, @Context String parentName);
    List<TradeConfigListLevelVO> toVOList(List<TradeCategoryConfig> var0List);



}