package com.ozs.mdm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ozs.mdm.dto.MdmInfoDataSourceInterfaceParamInDTO;
import com.ozs.mdm.dto.interfacedto.CallInterFaceParamDTO;
import com.ozs.mdm.service.InterfaceDataSourcesService;
import com.ozs.mdm.service.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Api(tags = { "数据中心-提供服务接口" })
@RestController
public class SupportController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private InterfaceDataSourcesService interfaceDataSourcesService;
    @ApiOperation( value = "数据源信息测试", notes = "测试")
    @PostMapping(value = "/mdm/interfaceDataSources/test", produces = "application/json;charset=UTF-8", consumes = "application/json;charset=UTF-8")
    public Object test(@RequestBody @ApiParam CallInterFaceParamDTO callInterFaceParamDTO) {

        RestTemplate restTemplate = new RestTemplate();
        String interfaceId  = callInterFaceParamDTO.getInterfaceId();
        try {
            String checkCode = interfaceDataSourcesService.getCheckCodeById(interfaceId);
            String url = interfaceDataSourcesService.getInterfaceUrlById(interfaceId);
            url+="?checkcode="+checkCode;
            String methodName = interfaceDataSourcesService.getinterfaceMethodById(interfaceId);
            List<MdmInfoDataSourceInterfaceParamInDTO> mdmInParam = interfaceDataSourcesService.getInterfaceInParamsByInterfaceId(interfaceId);
            for(MdmInfoDataSourceInterfaceParamInDTO InInfo:mdmInParam){
                if(callInterFaceParamDTO.getParamIn().get(InInfo.getParamKey())!=null){
                    url+="&"+InInfo.getParamKey()+"="+callInterFaceParamDTO.getParamIn().get(InInfo.getParamKey());
                }
            }
            String result = restTemplate.getForObject(url,String.class);
            JSONObject jsonObject = (JSONObject) JSONObject.parse(restTemplate.getForObject(url,String.class));
            Object dataValue = interfaceDataSourcesService.getParamOutDateValue(jsonObject, "data");
            return  dataValue;

//        for(Map.Entry<String, Object> entry:callInterFaceParamDTO.getParamIn().entrySet()){
//
//            params.put(entry.getKey(),entry.getValue());
//            System.out.println("key:"+entry.getKey()+","+"value:"+entry.getValue());
//        }


        } catch (Exception e) {
            e.printStackTrace();
            log.error("提供服务接口信息异常==>" + e.getMessage());
            return ReturnUtil.createExceptionReturn(e);
        }
    }

}
