package com.hzya.frame.sys.thirdparty.api.test.service.impl;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sys.dictionaryshopNew.dao.ISysDictionaryshopNewMapper;
import com.hzya.frame.sys.dictionaryshopNew.entity.SysDictionaryshopNew;
import com.hzya.frame.sys.thirdparty.api.dao.ISysThirdpartyapiMapper;
import com.hzya.frame.sys.thirdparty.api.entity.SysThirdpartyapi;
import com.hzya.frame.sys.thirdparty.api.testparameter.dao.ISysThirdpartyapiTestparameterMapper;
import com.hzya.frame.sys.thirdparty.api.testparameter.entity.SysThirdpartyapiTestparameter;
import com.hzya.frame.util.HttpUtil;
import com.hzya.frame.web.entity.BaseResult;
import org.springframework.stereotype.Service;
import com.hzya.frame.serviceUtil.ServiceUtil;
import com.hzya.frame.sys.thirdparty.api.test.entity.SysThirdpartyapiTest;
import com.hzya.frame.sys.thirdparty.api.test.service.ISysThirdpartyapiTestService;
import com.hzya.frame.sys.thirdparty.api.test.dao.ISysThirdpartyapiTestMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hzya.frame.web.entity.JsonResultEntity;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @description: 第三方应用接口测试表  service
 * @tableName:   sys_thirdparty_api_test
 * @entityName:  SysThirdpartyapiTest
 * @author: 	 gjh
 * @history:	 1.0
 */
@Service(value="systhirdpartyapitestService")
public class  SysThirdpartyapiTestServiceImpl extends ServiceUtil implements ISysThirdpartyapiTestService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	protected ISysThirdpartyapiTestMapper  systhirdpartyapitestMapper;

	@Resource
	protected ISysThirdpartyapiMapper sysThirdpartyapiMapper;
	@Resource
	protected ISysDictionaryshopNewMapper sysDictionaryshopNewMapper;
	@Resource
	protected ISysThirdpartyapiTestparameterMapper  sysThirdpartyapiTestparameterMapper;


	/**
	 * @Author lvleigang
	 * @Description  查询所有用例
	 * @Date 2:25 下午 2023/3/16
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
    @Override
    public JsonResultEntity queryThirdpartyapitest(JSONObject jsonObject) {
		SysThirdpartyapiTest sysThirdpartyapiTest = getData("jsonStr",jsonObject,SysThirdpartyapiTest.class);
		if(sysThirdpartyapiTest == null || sysThirdpartyapiTest.getThirdpartyApiId() == null || "".equals(sysThirdpartyapiTest.getThirdpartyApiId())){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		sysThirdpartyapiTest.setSts("Y");
		List<SysThirdpartyapiTest> sysThirdpartyapiTestList = systhirdpartyapitestMapper.entity_list_like(sysThirdpartyapiTest);
        return BaseResult.getSuccessMessageEntity("查询所有用例成功",sysThirdpartyapiTestList);
    }

    /**
     * @Author lvleigang
     * @Description  查询用例信息
     * @Date 2:32 下午 2023/3/16
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
	@Override
	public JsonResultEntity getThirdpartyapitest(JSONObject jsonObject) {
		SysThirdpartyapiTest sysThirdpartyapiTest = getData("jsonStr",jsonObject,SysThirdpartyapiTest.class);
		if(sysThirdpartyapiTest == null || sysThirdpartyapiTest.getId() == null || "".equals(sysThirdpartyapiTest.getId())){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		sysThirdpartyapiTest = systhirdpartyapitestMapper.entity_get(sysThirdpartyapiTest.getId());
		if(sysThirdpartyapiTest == null || sysThirdpartyapiTest.getId() == null || "".equals(sysThirdpartyapiTest.getId())){
			return BaseResult.getFailureMessageEntity("查询用例失败");
		}
		//查询第三方应用接口测试参数表
		SysThirdpartyapiTestparameter sysThirdpartyapiTestparameter = new SysThirdpartyapiTestparameter();
		sysThirdpartyapiTestparameter.setThirdpartyApiTestId(sysThirdpartyapiTest.getId());
		sysThirdpartyapiTestparameter.setSts("Y");
		List<SysThirdpartyapiTestparameter> sysThirdpartyapiTestparameterList = sysThirdpartyapiTestparameterMapper.entity_list_base(sysThirdpartyapiTestparameter);
		sysThirdpartyapiTest.setSysThirdpartyapiTestparameterList(sysThirdpartyapiTestparameterList);
		return BaseResult.getSuccessMessageEntity("查询所有用例成功",sysThirdpartyapiTest);


	}

	/**
	 * @Author lvleigang
	 * @Description  新增用例信息
	 * @Date 2:40 下午 2023/3/16
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity addThirdpartyapitest(JSONObject jsonObject) {
		SysThirdpartyapiTest sysThirdpartyapiTest = getData("jsonStr",jsonObject,SysThirdpartyapiTest.class);
		if(sysThirdpartyapiTest == null ){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		//保存 第三方应用接口表
		sysThirdpartyapiTest.setCreate();
		systhirdpartyapitestMapper.entity_insert(sysThirdpartyapiTest);

		//保存 第三方应用接口参数表
		List<SysThirdpartyapiTestparameter> sysThirdpartyapiTestparameterList = sysThirdpartyapiTest.getSysThirdpartyapiTestparameterList();
		if(sysThirdpartyapiTestparameterList != null && sysThirdpartyapiTestparameterList.size() > 0){
			for (int i = 0; i < sysThirdpartyapiTestparameterList.size(); i++) {
				sysThirdpartyapiTestparameterList.get(i).setCreate();
				sysThirdpartyapiTestparameterList.get(i).setThirdpartyApiTestId(sysThirdpartyapiTest.getId());
				sysThirdpartyapiTestparameterMapper.entity_insert(sysThirdpartyapiTestparameterList.get(i));
			}
		}
		return BaseResult.getSuccessMessageEntity("保存用例成功");
	}

	/**
	 * @Author lvleigang
	 * @Description   测试接口
	 * @Date 4:49 下午 2023/3/16
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
    @Override
    public JsonResultEntity testApi(JSONObject jsonObject) {
		SysThirdpartyapiTest sysThirdpartyapiTest = getData("jsonStr",jsonObject,SysThirdpartyapiTest.class);
		if(sysThirdpartyapiTest == null || sysThirdpartyapiTest.getThirdpartyApiId() == null || "".equals(sysThirdpartyapiTest.getThirdpartyApiId()) ){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		//根据接口id查询接口获取请求路径
		SysThirdpartyapi sysThirdpartyapi = sysThirdpartyapiMapper.entity_get(sysThirdpartyapiTest.getThirdpartyApiId());
		if(sysThirdpartyapi == null || sysThirdpartyapi.getId() == null || "".equals(sysThirdpartyapi.getId()) ){
			return BaseResult.getFailureMessageEntity("接口不存在不存在");
		}
		//路径
    	String url = sysThirdpartyapi.getApiServiceAddress();
		//设置编码
		String charset = null;
		//新增请求头 不覆盖原有请求头
		Map<String, String> headerMap = new HashMap<>();
		SysDictionaryshopNew contentType = getDictionaryshopByValue("sys_thirdparty_api","content_type",sysThirdpartyapi.getContentType());
		if(contentType != null ) {
            headerMap.put("Content-Type",contentType.getColumnContent());
        }
		SysDictionaryshopNew charSetDictionary = getDictionaryshopByValue("sys_thirdparty_api","api_code",sysThirdpartyapi.getApiCode());
		if(charSetDictionary != null )  {
			headerMap.put("charset",charSetDictionary.getColumnContent());
			charset = charSetDictionary.getColumnContent();
		}
		SysDictionaryshopNew accept = getDictionaryshopByValue("sys_thirdparty_api","accept",sysThirdpartyapi.getAccept());
		if(accept != null ) {
            headerMap.put("Accept",accept.getColumnContent());
        }

		if(sysThirdpartyapiTest.getHeaders() != null && sysThirdpartyapiTest.getHeaders().size() > 0){
			for (int i = 0; i < sysThirdpartyapiTest.getHeaders().size(); i++) {
				headerMap.put(sysThirdpartyapiTest.getHeaders().get(i).getName(),sysThirdpartyapiTest.getHeaders().get(i).getValue());
			}
		}
		//headerMap.put("tl","loginService");
		//headerMap.put("as","sysTestjdbc");
		//headerMap.put("dj","doLogin");
		//表单内容
		JSONObject body = new JSONObject();

		if(sysThirdpartyapiTest.getBodys() != null && sysThirdpartyapiTest.getBodys().size() > 0){
			for (int i = 0; i < sysThirdpartyapiTest.getBodys().size(); i++) {
				body.put(sysThirdpartyapiTest.getBodys().get(i).getName(),sysThirdpartyapiTest.getBodys().get(i).getValue());
			}
		}
		//body.put("login_name","admin");
		//body.put("password","123456");

		//超时，毫秒
		Integer timeout = 20000;

		//执行
		HttpResponse httpResponse = null;
		if("1".equals(sysThirdpartyapi.getRequestMethod())){ //post
			httpResponse = new HttpUtil().postExecute(url,charset,headerMap,body,timeout);
		}else if ("2".equals(sysThirdpartyapi.getRequestMethod())){ //get
			httpResponse = new HttpUtil().getExecute(url,charset,headerMap,body,timeout);
		}
		//返回
		if(httpResponse.getStatus()== 200){
			return BaseResult.getSuccessMessageEntity("调用接口成功",httpResponse.body());
		}else {
			return BaseResult.getFailureMessageEntity("调用接口失败",httpResponse.body());
		}
    }

    /**
     * @Author lvleigang
     * @Description  获取唯一枚举
     * @Date 10:11 上午 2023/3/20
     * @param tabName
     * @param columnName
     * @param columnValue
     * @return com.hzya.frame.sys.dictionaryshopNew.entity.SysDictionaryshopNew
     **/
	private SysDictionaryshopNew getDictionaryshopByValue(String tabName, String columnName, String columnValue) {
		SysDictionaryshopNew sysDictionaryshopNew = new SysDictionaryshopNew();
		sysDictionaryshopNew.setTabName(tabName);
		sysDictionaryshopNew.setColumnName(columnName);
		sysDictionaryshopNew.setColumnValue(columnValue);
		return sysDictionaryshopNewMapper.entity_get_by_value(sysDictionaryshopNew);
	}
}
