package com.huigou.topsun.ep.epApplication.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huigou.topsun.ep.epApplication.EpApplication;
import com.huigou.topsun.sap.common.HttpClient;
import com.huigou.topsun.util.SAPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * @Auther: xin.lu
 * @Date: 2024/06/22/15:23
 * @Description:
 */
@Service("epApplication")
public class EpApplicationImpl implements EpApplication {
    @Autowired
    private HttpClient httpClient;
    @Override
    public List<Map<String, String>> queryEpBrand(String brandName) {
        Map<String,Object> map = new HashMap<>();
        map.put("brandName",brandName);
        List<Map<String, String>> mapList = new ArrayList<>();
        String message = "";
        try {
            String executeForEp = httpClient.executeForEp(map, "md/brand/queryAllEnableBrand");
            Map<String, Object> resultMap = JSONObject.parseObject(executeForEp, Map.class);
            if ("200".equals(resultMap.get("code").toString())) {
                ObjectMapper objectMapper = SAPUtils.objectMapper();
                mapList = objectMapper.readValue(com.huigou.util.JSONUtil.toString(resultMap.get("data")), new TypeReference<List<Map<String, String>>>() {});
            }else {
                message = (String) resultMap.get("msg");
            }
        } catch (IOException e) {
            throw new RuntimeException("查询品牌出错：" + message);
        }
        return mapList;
    }

    @Override
    public List<Map<String, String>> queryEpPd(String brandCode) {
        Map<String,Object> map = new HashMap<>();
        //map.put("brandCode",brandCode);
        List<Map<String, String>> mapList = new ArrayList<>();
        String message = "";
        try {
            String executeForEp = httpClient.executeForEp(map, "md/mdItem/queryProductByBrandCode?brandCode="+brandCode);
            Map<String, Object> resultMap = JSONObject.parseObject(executeForEp, Map.class);
            if ("200".equals(resultMap.get("code").toString())) {
                ObjectMapper objectMapper = SAPUtils.objectMapper();
                mapList = objectMapper.readValue(com.huigou.util.JSONUtil.toString(resultMap.get("data")), new TypeReference<List<Map<String, String>>>() {});
            }else {
                message =(String) resultMap.get("msg");
            }
        } catch (IOException e) {
            throw new RuntimeException("查询产品出错：" + message);
        }
        return mapList;
    }

    @Override
    public List<Map<String, String>> queryProductAndBom(String itemCode) {
        Map<String,Object> map = new HashMap<>();
        //map.put("brandCode",brandCode);
        List<Map<String, String>> mapList = new ArrayList<>();
        String message = "";
        try {
            String executeForEp = httpClient.executeForEp(map, "md/mdItem/queryProductAndBomByItemCode?itemCode="+itemCode);
            Map<String, Object> resultMap = JSONObject.parseObject(executeForEp, Map.class);
            if ("200".equals(resultMap.get("code").toString())) {
                ObjectMapper objectMapper = SAPUtils.objectMapper();
                mapList = objectMapper.readValue(com.huigou.util.JSONUtil.toString(resultMap.get("data")), new TypeReference<List<Map<String, String>>>() {});
            }else {
                message =(String) resultMap.get("msg");
            }
        } catch (IOException e) {
            throw new RuntimeException("查询制程工艺、原材料出错：" + message);
        }
        return mapList;
    }
}
