package com.kingtom.shengtai.api.mapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kingtom.kirin.core.common.utils.JsonUtils;
import com.kingtom.shengtai.api.model.ProductDirDTO;
import com.kingtom.shengtai.api.model.ProductDirTreeDTO;
import com.kingtom.shengtai.app.srm.model.ProductDir;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

/**
 * 应用模块名称</p>
 * 产品目录表 dto 和 entity之间的转换类。</p>
 * Copyright: Copyright (C) 2024 , Inc. All rights reserved. <p>
 * Company: 成都诚唐科技有限责任公司</p>
 *
 * @author wany
 * @since 2024/01/17
 */
@Mapper
public interface ProductDirMapper{

    public static final ProductDirMapper INSTANCE = Mappers.getMapper(ProductDirMapper.class);

    @Mapping(target = "ext3", qualifiedByName = "mapToString")
    @Mapping(target = "gmtCreate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "gmtModified", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "path", qualifiedByName = "listToString")
    ProductDir convert(ProductDirDTO dto);

    @Mapping(target = "ext3", qualifiedByName = "stringToMap")
    @Mapping(target = "gmtCreate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "gmtModified", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "path", qualifiedByName = "stringToList")
    ProductDirDTO convert(ProductDir productDir);


    @Mapping(target = "children", ignore = true)
    ProductDirTreeDTO convertDTOToTree(ProductDirDTO menu);


    @Named("stringToList")
    default List<String> stringToList(String str){
        if(StringUtils.isEmpty(str)){
            return new ArrayList<>();
        }
        return JsonUtils.toObjectList(str, String.class);
    }

    @Named("listToString")
    default String listToString(List<String> list){
        if(list == null){
            list = new ArrayList<>();
        }
        return JsonUtils.toJson(list);
    }

    @Named("mapToString")
    default String mapToString(Map<String, Object> map){
        if(map == null){
            map = new HashMap<>();
        }
        return JsonUtils.toJson(map);
    }

    @Named("stringToMap")
    default Map<String, Object> stringToMap(String str){
        if(StringUtils.isEmpty(str)){
            return new HashMap<>();
        }
        return JsonUtils.toObject(str, Map.class);
    }

}