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.SrmCooperateDTO;
import com.kingtom.shengtai.api.model.SrmSupplierDTO;
import com.kingtom.shengtai.app.srm.model.SrmCooperate;
import com.kingtom.shengtai.app.srm.model.SrmSupplier;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

/**
 * 应用模块名称</p>
 * 代码描述</p>
 * Copyright: Copyright (C) 2023 , Inc. All rights reserved. <p>
 * Company: 成都诚唐科技有限责任公司</p>
 *
 * @author wany
 * @since 2023/12/21
 */
@Mapper
public interface SrmMapper{

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

    @Mapping(target = "gmtCreate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "gmtModified", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "ext3", qualifiedByName = "mapToString")
    @Mapping(target = "image", qualifiedByName = "listToString")
    @Mapping(target = "product", qualifiedByName = "listToString")
    @Mapping(target = "certificates", qualifiedByName = "listToString")
    SrmSupplier convert(SrmSupplierDTO dto);

    @Mapping(target = "gmtCreate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "gmtModified", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "ext3", qualifiedByName = "stringToMap")
    @Mapping(target = "image", qualifiedByName = "stringToList")
    @Mapping(target = "product", qualifiedByName = "stringToList")
    @Mapping(target = "certificates", qualifiedByName = "stringToList")
    SrmSupplierDTO convert(SrmSupplier dto);

    @Mapping(target = "gmtCreate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "gmtModified", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "ext3", qualifiedByName = "mapToString")
    @Mapping(target = "time",  dateFormat = "yyyy-MM-dd")
    @Mapping(target = "product", qualifiedByName = "listToString")
    SrmCooperate convert(SrmCooperateDTO dto);

    @Mapping(target = "gmtCreate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "gmtModified", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "ext3", qualifiedByName = "stringToMap")
    @Mapping(target = "time",  dateFormat = "yyyy-MM-dd")
    @Mapping(target = "product", qualifiedByName = "stringToList")
    @Mapping(target = "supplierName", ignore = true)
    SrmCooperateDTO convert(SrmCooperate dto);


    @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(str == null){
            return new HashMap<>();
        }
        return JsonUtils.toObject(str, Map.class);
    }

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

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

}
