package net.northking.cctp.testbug.feign.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.testbug.dto.dict.DictItem;
import net.northking.cctp.testbug.dto.platform.CpNotice;
import net.northking.cctp.testbug.dto.platform.CpTemplatePropertiesDetailDto;
import net.northking.cctp.testbug.dto.platform.UserQueryDto;
import net.northking.cctp.testbug.feign.client.PlatFormFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

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

/**
 * @author: puchao.ma
 * @date: 2021/11/10 10:45
 */
@Configuration
public class PlatFormFeignServer {

    @Autowired
    private PlatFormFeignClient platFormFeignClient;
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(PlatFormFeignServer.class);

    public Map<String, Object> queryDict(List<String> catalogCodeList) {
        ResultWrapper<Map<String, Object>> query = platFormFeignClient.query(catalogCodeList);
        if (query.isSuccess()) {
            return query.getData();
        }
        return null;
    }

    public Map<String, Map<String, String>> queryDictData(List<String> catalogCodeList) {

        Map<String, Map<String, String>> map = new HashMap<>();
        long l = System.currentTimeMillis();
        logger.info("行49开始:0");
        ResultWrapper<Map<String, Object>> query = platFormFeignClient.query(catalogCodeList);
        logger.info("行49结束:"+(System.currentTimeMillis()-l));
        if (query.isSuccess()) {
            query.getData().forEach((k, v) -> {
                List<DictItem> dictItems = JSONArray.parseArray(JSON.toJSONString(v), DictItem.class);
                Map<String, String> map1 = dictItems.stream().collect(Collectors.toMap(DictItem::getItemCode, DictItem::getItemName));
                map.put(k, map1);
            });
        }
        return map;
    }

    public List<String> findUserIdByName(UserQueryDto query) {
        return platFormFeignClient.findUserIdByName(query);
    }

    public List<CpTemplatePropertiesDetailDto> getSysProperties (Integer templateType) {
        long l = System.currentTimeMillis();
        logger.info("行68开始:0");
        ResultWrapper<List<CpTemplatePropertiesDetailDto>> wrapper = platFormFeignClient.getSysProperties(templateType);
        logger.info("行68结束:"+(System.currentTimeMillis()-l));
        if (wrapper.isSuccess()) {
            return wrapper.getData();
        }
        return null;
    }

    public String sendNoticeMQ(List<CpNotice> cpNotice) {
        ResultWrapper<String> wrapper = platFormFeignClient.sendNoticeMQ(cpNotice);

        if (wrapper.isSuccess()) {
            return wrapper.getData();
        }
        return null;
    }
}
