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

import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sys.thirdparty.api.certification.dao.ISysThirdpartyapiCertificationMapper;
import com.hzya.frame.sys.thirdparty.api.parameter.dao.ISysThirdpartyapiParameterMapper;
import com.hzya.frame.sys.thirdparty.api.parameter.entity.SysThirdpartyapiParameter;
import com.hzya.frame.sys.thirdparty.api.test.dao.ISysThirdpartyapiTestMapper;
import com.hzya.frame.sys.thirdparty.api.test.entity.SysThirdpartyapiTest;
import com.hzya.frame.sys.thirdparty.api.testparameter.dao.ISysThirdpartyapiTestparameterMapper;
import com.hzya.frame.sys.thirdparty.api.testparameter.entity.SysThirdpartyapiTestparameter;
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.entity.SysThirdpartyapi;
import com.hzya.frame.sys.thirdparty.api.service.ISysThirdpartyapiService;
import com.hzya.frame.sys.thirdparty.api.dao.ISysThirdpartyapiMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hzya.frame.web.entity.JsonResultEntity;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @description: 第三方应用接口表  service
 * @tableName: sys_thirdparty_api
 * @entityName: SysThirdpartyapi
 * @author: gjh
 * @history: 1.0
 */
@Service(value = "systhirdpartyapiService")
public class SysThirdpartyapiServiceImpl extends ServiceUtil implements ISysThirdpartyapiService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    protected ISysThirdpartyapiMapper sysThirdpartyapiMapper;
    @Resource
    protected ISysThirdpartyapiParameterMapper sysThirdpartyapiParameterMapper;
    @Resource
    protected ISysThirdpartyapiCertificationMapper sysThirdpartyapiCertificationMapper;
    @Resource
    protected ISysThirdpartyapiTestMapper sysThirdpartyapiTestMapper;
    @Resource
    protected ISysThirdpartyapiTestparameterMapper sysThirdpartyapiTestparameterMapper;

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 根据ID，获取接口所有信息
     * @Date 4:27 下午 2023/3/15
     **/
    @Override
    public JsonResultEntity getThirdpartyApiById(JSONObject jsonObject) {

        //根据当前节点id，删除下级分类，以及下级接口
        SysThirdpartyapi sysThirdpartyapi = getData("jsonStr", jsonObject, SysThirdpartyapi.class);
        if (sysThirdpartyapi == null || sysThirdpartyapi.getId() == null || "".equals(sysThirdpartyapi.getId())) {
            return BaseResult.getFailureMessageEntity("请求参数不存在");
        }
        // 根据接口id获取接口
        SysThirdpartyapi entity = sysThirdpartyapiMapper.entity_get(sysThirdpartyapi.getId());
        if (entity != null && entity.getId() != null && !"".equals(entity.getId())) {
            //根据接口id获取第三方应用接口参数表
            SysThirdpartyapiParameter sysThirdpartyapiParameter = new SysThirdpartyapiParameter();
            sysThirdpartyapiParameter.setSts("Y");
            sysThirdpartyapiParameter.setThirdpartyApiId(entity.getId());
            List<SysThirdpartyapiParameter> sysThirdpartyapiParameterList = sysThirdpartyapiParameterMapper.entity_list_base(sysThirdpartyapiParameter);
            List<SysThirdpartyapiParameter> returnParameters = assemblyDataParameter(sysThirdpartyapiParameterList, 1, null);


            //根据接口id获取第三方应用接口认证信息表 TODO 暂时不需要这个表数据
            //SysThirdpartyapiCertification sysThirdpartyapiCertification = new SysThirdpartyapiCertification();
            //sysThirdpartyapiCertification.setSts("Y");
            //sysThirdpartyapiCertification.setThirdpartyApiId(entity.getId());
            //List<SysThirdpartyapiCertification> sysThirdpartyapiCertificationList = sysThirdpartyapiCertificationMapper.entity_list_base(sysThirdpartyapiCertification);
            //根据接口id获取第三方应用接口测试表
            //SysThirdpartyapiTest sysThirdpartyapiTest = new SysThirdpartyapiTest();
            //sysThirdpartyapiTest.setSts("Y");
            //sysThirdpartyapiTest.setThirdpartyApiId(entity.getId());
            //List<SysThirdpartyapiTest> sysThirdpartyapiTestList = sysThirdpartyapiTestMapper.entity_list_base(sysThirdpartyapiTest);
            //根据接口id获取第三方应用接口测试参数表
            //SysThirdpartyapiTestparameter sysThirdpartyapiTestparameter = new SysThirdpartyapiTestparameter();
            //sysThirdpartyapiTestparameter.setSts("Y");
            //sysThirdpartyapiTestparameter.setThirdpartyApiId(entity.getId());
            //List<SysThirdpartyapiTestparameter> sysThirdpartyapiTestparameterList = sysThirdpartyapiTestparameterMapper.entity_list_base(sysThirdpartyapiTestparameter);
            entity.setSysThirdpartyapiParameterList(returnParameters);
            //entity.setSysThirdpartyapiCertificationList(sysThirdpartyapiCertificationList);
            //entity.setSysThirdpartyapiTestList(sysThirdpartyapiTestList);
            //entity.setSysThirdpartyapiTestparameterList(sysThirdpartyapiTestparameterList);
            return BaseResult.getSuccessMessageEntity("获取接口成功", entity);
        } else {
            return BaseResult.getFailureMessageEntity("查询的接口不存在");
        }
    }

    /**
     * @param sysThirdpartyapiParameterList
     * @return java.util.List<com.hzya.frame.sys.thirdparty.api.parameter.entity.SysThirdpartyapiParameter>
     * @Author lvleigang
     * @Description 组装参数数据
     * @Date 3:09 下午 2023/3/27
     **/
    private List<SysThirdpartyapiParameter> assemblyDataParameter(List<SysThirdpartyapiParameter> sysThirdpartyapiParameterList, Integer lvl, String upid) {
        List<SysThirdpartyapiParameter> returnParameters = new ArrayList<>();

        if (sysThirdpartyapiParameterList != null && sysThirdpartyapiParameterList.size() > 0) {
            for (int i = 0; i < sysThirdpartyapiParameterList.size(); i++) {
                if (lvl.equals(sysThirdpartyapiParameterList.get(i).getLvl())) {
                    if (lvl.equals(1)) {
                        sysThirdpartyapiParameterList.get(i).setSysThirdpartyapiParameterList(assemblyDataParameter(sysThirdpartyapiParameterList, lvl + 1, sysThirdpartyapiParameterList.get(i).getId()));
                        returnParameters.add(sysThirdpartyapiParameterList.get(i));
                    } else {
                        if (upid.equals(sysThirdpartyapiParameterList.get(i).getUpId())) {
                            sysThirdpartyapiParameterList.get(i).setSysThirdpartyapiParameterList(assemblyDataParameter(sysThirdpartyapiParameterList, lvl + 1, sysThirdpartyapiParameterList.get(i).getId()));
                            returnParameters.add(sysThirdpartyapiParameterList.get(i));
                        }
                    }
                }
            }
        }
        return returnParameters;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 根据ID，获取删除接口所有信息
     * @Date 4:55 下午 2023/3/15
     **/
    @Override
    public JsonResultEntity delectThirdpartyApiById(JSONObject jsonObject) {
        //根据当前节点id，删除下级分类，以及下级接口
        SysThirdpartyapi sysThirdpartyapi = getData("jsonStr", jsonObject, SysThirdpartyapi.class);
        if (sysThirdpartyapi == null || sysThirdpartyapi.getId() == null || "".equals(sysThirdpartyapi.getId())) {
            return BaseResult.getFailureMessageEntity("请求参数不存在");
        }
        // 根据接口id获取接口
        SysThirdpartyapi entity = sysThirdpartyapiMapper.entity_get(sysThirdpartyapi.getId());
        if (entity != null && entity.getId() != null && !"".equals(entity.getId())) {
            //根据接口id获取第三方应用接口参数表
            SysThirdpartyapiParameter sysThirdpartyapiParameter = new SysThirdpartyapiParameter();
            sysThirdpartyapiParameter.setSts("Y");
            sysThirdpartyapiParameter.setThirdpartyApiId(entity.getId());
            sysThirdpartyapiParameter.setUpdate();
            sysThirdpartyapiParameterMapper.entity_logicDelete_Multi_Condition(sysThirdpartyapiParameter);
            //根据接口id获取第三方应用接口认证信息表 TODO 暂时不需要
            //SysThirdpartyapiCertification sysThirdpartyapiCertification = new SysThirdpartyapiCertification();
            //sysThirdpartyapiCertification.setSts("Y");
            //sysThirdpartyapiCertification.setUpdate();
            //sysThirdpartyapiCertification.setThirdpartyApiId(entity.getId());
            //sysThirdpartyapiCertificationMapper.entity_logicDelete_Multi_Condition(sysThirdpartyapiCertification);
            //根据接口id获取第三方应用接口测试表
            SysThirdpartyapiTest sysThirdpartyapiTest = new SysThirdpartyapiTest();
            sysThirdpartyapiTest.setSts("Y");
            sysThirdpartyapiTest.setUpdate();
            sysThirdpartyapiTest.setThirdpartyApiId(entity.getId());
            sysThirdpartyapiTestMapper.entity_logicDelete_Multi_Condition(sysThirdpartyapiTest);
            //根据接口id获取第三方应用接口测试参数表
            SysThirdpartyapiTestparameter sysThirdpartyapiTestparameter = new SysThirdpartyapiTestparameter();
            sysThirdpartyapiTestparameter.setSts("Y");
            sysThirdpartyapiTestparameter.setUpdate();
            sysThirdpartyapiTestparameter.setThirdpartyApiId(entity.getId());
            sysThirdpartyapiTestparameterMapper.entity_logicDelete_Multi_Condition(sysThirdpartyapiTestparameter);
            sysThirdpartyapi.setSts("Y");
            sysThirdpartyapi.setUpdate();
            sysThirdpartyapiMapper.entity_logicDelete(sysThirdpartyapi);
            return BaseResult.getSuccessMessageEntity("删除接口成功");
        } else {
            return BaseResult.getFailureMessageEntity("删除接口不存在");
        }
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 新增接口
     * @Date 11:05 上午 2023/3/16
     **/
    @Override
    public JsonResultEntity addThirdpartyApi(JSONObject jsonObject) {
        SysThirdpartyapi sysThirdpartyapi = getData("jsonStr", jsonObject, SysThirdpartyapi.class);
        if (sysThirdpartyapi == null) {
            return BaseResult.getFailureMessageEntity("请求参数不存在");
        }
        //保存 第三方应用接口表
        sysThirdpartyapi.setCreate();
        sysThirdpartyapiMapper.entity_insert(sysThirdpartyapi);

        //保存 第三方应用接口参数表
        List<SysThirdpartyapiParameter> sysThirdpartyapiParameterList = sysThirdpartyapi.getSysThirdpartyapiParameterList();
        if (sysThirdpartyapiParameterList != null && sysThirdpartyapiParameterList.size() > 0) {
            for (int i = 0; i < sysThirdpartyapiParameterList.size(); i++) {
                sysThirdpartyapiParameterList.get(i).setCreate();
                sysThirdpartyapiParameterList.get(i).setThirdpartyApiId(sysThirdpartyapi.getId());
                //判断是否有下级
                saveUpThirdpartyapiParameter(sysThirdpartyapiParameterList.get(i), sysThirdpartyapi.getId());
                sysThirdpartyapiParameterMapper.entity_insert(sysThirdpartyapiParameterList.get(i));
            }
        }
        //保存 第三方应用接口认证信息表 TODO 暂时不需要保存这个信息
        //List<SysThirdpartyapiCertification> sysThirdpartyapiCertificationList = sysThirdpartyapi.getSysThirdpartyapiCertificationList();
        //if(sysThirdpartyapiCertificationList != null && sysThirdpartyapiCertificationList.size() > 0){
        //	for (int i = 0; i < sysThirdpartyapiCertificationList.size(); i++) {
        //		sysThirdpartyapiCertificationList.get(i).setCreate().setThirdpartyApiId(sysThirdpartyapi.getId());
        //		sysThirdpartyapiCertificationMapper.entity_insert(sysThirdpartyapiCertificationList.get(i));
        //	}
        //}
        return BaseResult.getSuccessMessageEntity("保存接口信息成功");
    }

    /**
     * @param sysThirdpartyapiParameter
     * @param thirdpartyApiId
     * @return void
     * @Author lvleigang
     * @Description 判断保存参数的下级
     * @Date 2:25 下午 2023/3/27
     **/
    private void saveUpThirdpartyapiParameter(SysThirdpartyapiParameter sysThirdpartyapiParameter, String thirdpartyApiId) {
        if (sysThirdpartyapiParameter != null
                && sysThirdpartyapiParameter.getSysThirdpartyapiParameterList() != null
                && sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().size() > 0) {

            for (int i = 0; i < sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().size(); i++) {
                sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setCreate();
                sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setThirdpartyApiId(thirdpartyApiId);
                sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setUpId(sysThirdpartyapiParameter.getId());
                //判断是否有下级
                saveUpThirdpartyapiParameter(sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i), thirdpartyApiId);
                sysThirdpartyapiParameterMapper.entity_insert(sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i));
            }
        }
    }

    /**
     * @param sysThirdpartyapiParameter
     * @param thirdpartyApiId
     * @return void
     * @Author lvleigang
     * @Description 判断保存参数的下级
     * @Date 2:25 下午 2023/3/27
     **/
    private void saveOrupdateUpThirdpartyapiParameter(SysThirdpartyapiParameter sysThirdpartyapiParameter, String thirdpartyApiId) {
        if (sysThirdpartyapiParameter != null
                && sysThirdpartyapiParameter.getSysThirdpartyapiParameterList() != null
                && sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().size() > 0) {

            for (int i = 0; i < sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().size(); i++) {
                if (sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).getId() != null && !"".equals(sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).getId())) {
                    sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setUpdate();
                    sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setThirdpartyApiId(thirdpartyApiId);
                    sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setSts("Y");
                    sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setUpId(sysThirdpartyapiParameter.getId());
                    sysThirdpartyapiParameterMapper.entity_update(sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i));
                } else {
                    sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setCreate();
                    sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setThirdpartyApiId(thirdpartyApiId);
                    sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i).setUpId(sysThirdpartyapiParameter.getId());
                    sysThirdpartyapiParameterMapper.entity_insert(sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i));
                }
                saveOrupdateUpThirdpartyapiParameter(sysThirdpartyapiParameter.getSysThirdpartyapiParameterList().get(i), thirdpartyApiId);
            }
        }
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改接口
     * @Date 11:23 上午 2023/3/16
     **/
    @Override
    public JsonResultEntity updateThirdpartyApi(JSONObject jsonObject) {
        SysThirdpartyapi sysThirdpartyapi = getData("jsonStr", jsonObject, SysThirdpartyapi.class);
        if (sysThirdpartyapi == null || sysThirdpartyapi.getId() == null || "".equals(sysThirdpartyapi.getId())) {
            return BaseResult.getFailureMessageEntity("请求参数不存在");
        }
        //保存 第三方应用接口表
        sysThirdpartyapi.setUpdate();
        sysThirdpartyapiMapper.entity_update(sysThirdpartyapi);

        //先删除再保存 第三方应用接口参数表
        SysThirdpartyapiParameter sysThirdpartyapiParameter = new SysThirdpartyapiParameter();
        sysThirdpartyapiParameter.setThirdpartyApiId(sysThirdpartyapi.getId());
        sysThirdpartyapiParameter.setSts("Y");
        sysThirdpartyapiParameter.setUpdate();
        sysThirdpartyapiParameterMapper.entity_logicDelete_Multi_Condition(sysThirdpartyapiParameter);

        List<SysThirdpartyapiParameter> sysThirdpartyapiParameterList = sysThirdpartyapi.getSysThirdpartyapiParameterList();
        if (sysThirdpartyapiParameterList != null && sysThirdpartyapiParameterList.size() > 0) {

            for (int i = 0; i < sysThirdpartyapiParameterList.size(); i++) {
                if (sysThirdpartyapiParameterList.get(i).getId() != null && !"".equals(sysThirdpartyapiParameterList.get(i).getId())) {
                    sysThirdpartyapiParameterList.get(i).setUpdate();
                    sysThirdpartyapiParameterList.get(i).setThirdpartyApiId(sysThirdpartyapi.getId());
                    sysThirdpartyapiParameterList.get(i).setSts("Y");
                    sysThirdpartyapiParameterMapper.entity_update(sysThirdpartyapiParameterList.get(i));
                } else {
                    sysThirdpartyapiParameterList.get(i).setCreate();
                    sysThirdpartyapiParameterList.get(i).setThirdpartyApiId(sysThirdpartyapi.getId());
                    sysThirdpartyapiParameterMapper.entity_insert(sysThirdpartyapiParameterList.get(i));
                }
                saveOrupdateUpThirdpartyapiParameter(sysThirdpartyapiParameterList.get(i), sysThirdpartyapi.getId());
            }
        }

        //先删除再保存 第三方应用接口认证信息表 TODO 暂时不需要
        //SysThirdpartyapiCertification sysThirdpartyapiCertification = new SysThirdpartyapiCertification();
        //sysThirdpartyapiCertification.setThirdpartyApiId(sysThirdpartyapi.getId());
        //sysThirdpartyapiCertification.setSts("Y");
        //sysThirdpartyapiCertification.setUpdate();
        //sysThirdpartyapiCertificationMapper.entity_logicDelete_Multi_Condition(sysThirdpartyapiCertification);
        //List<SysThirdpartyapiCertification> sysThirdpartyapiCertificationList = sysThirdpartyapi.getSysThirdpartyapiCertificationList();
        //if(sysThirdpartyapiCertificationList != null && sysThirdpartyapiCertificationList.size() > 0){
        //	for (int i = 0; i < sysThirdpartyapiCertificationList.size(); i++) {
        //
        //		if(sysThirdpartyapiCertificationList.get(i).getId() != null && !"".equals(sysThirdpartyapiCertificationList.get(i).getId())){
        //			sysThirdpartyapiCertificationList.get(i).setUpdate().setThirdpartyApiId(sysThirdpartyapi.getId());
        //			sysThirdpartyapiCertificationList.get(i).setSts("Y");
        //			sysThirdpartyapiCertificationMapper.entity_update(sysThirdpartyapiCertificationList.get(i));
        //		}else {
        //			sysThirdpartyapiCertificationList.get(i).setCreate().setThirdpartyApiId(sysThirdpartyapi.getId());
        //			sysThirdpartyapiCertificationMapper.entity_insert(sysThirdpartyapiCertificationList.get(i));
        //		}
        //	}
        //}
        return BaseResult.getSuccessMessageEntity("修改接口信息成功");
    }


}
