package com.ozs.mdm.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ozs.mdm.dto.*;
import com.ozs.mdm.mapper.InterfaceDataSourcesMapper;
import com.ozs.mdm.service.InterfaceDataSourcesService;
import com.ozs.mdm.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class InterfaceDataSourcesServiceImpl  implements InterfaceDataSourcesService {
    @Autowired
    private InterfaceDataSourcesMapper interfaceDataSourcesMapper;


    @Override
    public R<List<MdmInfoDataSourceInterfaceDTO>>listInterfaceDataSources(String methodName, String status ,String projectName, String sort, String order,
                                                                        Integer page, Integer pageSize) throws Exception {

        R<MdmInfoDataSourceInterfaceDTO> r = new R<>();
        if (!sort.isEmpty()){
            sort =" mds."+sort;
        }
        r.pageStart(sort, order, page, pageSize);
        List<MdmInfoDataSourceInterfaceDTO> resultList =  this.interfaceDataSourcesMapper.listInterfaceDataSources(methodName,status,projectName);
        return r.page(resultList);
    }

    @Override
    public MdmInfoDataSourceInterfaceVO getInterfaceDataSourcesById(String interfaceId) throws Exception {
        MdmInfoDataSourceInterfaceVO resultList =  this.interfaceDataSourcesMapper.getInterfaceDataSourcesById(interfaceId);

        List<MdmInfoDataSourceInterfaceParamInDTO> paramInResult = this.interfaceDataSourcesMapper.getInterfaceParamIn(interfaceId);
        List<MdmInfoDataSourceInterfaceParamOutDTO> paramOutResult = this.interfaceDataSourcesMapper.getInterfaceParamOut(interfaceId);
        resultList.setParamInput(paramInResult);
        resultList.setParamOut(paramOutResult);
        return resultList;

    }

    @Override
    public R<String>  addInterfaceDataSources(MdmInfoDataSourceInterfaceVO mdmInterfaceVO) throws Exception {

        R<String> r = new R<>();
        mdmInterfaceVO.setInterfaceId(UUID.randomUUID().toString());
        MdmInfoDataSourceInterfaceDTO mdmInterfaceDTO = new MdmInfoDataSourceInterfaceDTO();
        mdmInterfaceDTO.setInterfaceId(mdmInterfaceVO.getInterfaceId());
        mdmInterfaceDTO.setInterfaceName(mdmInterfaceVO.getInterfaceName());
        mdmInterfaceDTO.setInterfaceDesc(mdmInterfaceVO.getInterfaceDesc());
        mdmInterfaceDTO.setProjectId(mdmInterfaceVO.getProjectId());
        mdmInterfaceDTO.setInterfaceUrl(mdmInterfaceVO.getInterfaceUrl());
        mdmInterfaceDTO.setProtocolId(mdmInterfaceVO.getProtocolId());
        mdmInterfaceDTO.setMethodId(mdmInterfaceVO.getMethodId());
        mdmInterfaceDTO.setUpdateTime(mdmInterfaceVO.getUpdateTime());
        mdmInterfaceDTO.setOperatorId(mdmInterfaceVO.getOperatorId());

        mdmInterfaceDTO.setStatusId(mdmInterfaceVO.getStatusId());
        for ( MdmInfoDataSourceInterfaceParamInDTO mdparamIn: mdmInterfaceVO.getParamInput()){

            mdparamIn.setInterfaceId(mdmInterfaceVO.getInterfaceId());
        }

        for (MdmInfoDataSourceInterfaceParamOutDTO mdparamOut:mdmInterfaceVO.getParamOut()){
            mdparamOut.setInterfaceId(mdmInterfaceVO.getInterfaceId());
        }
        this.interfaceDataSourcesMapper.addInterfaceDataSources(mdmInterfaceVO);
        this.interfaceDataSourcesMapper.addInputNodes(mdmInterfaceVO.getParamInput());
        this.interfaceDataSourcesMapper.addOutNodes(mdmInterfaceVO.getParamOut());
        r.setMsg("接口数据新增成功！");
        return r;
    }

    @Override
    @Transactional
    public R<String> updateInterfaceDataSourcesById(MdmInfoDataSourceInterfaceVO mdmInterfaceVO) {
        R<String> r = new R<>();
        MdmInfoDataSourceInterfaceDTO mdmInterfaceDTO = new MdmInfoDataSourceInterfaceDTO();

        mdmInterfaceDTO.setInterfaceId(mdmInterfaceVO.getInterfaceId());
        mdmInterfaceDTO.setInterfaceName(mdmInterfaceVO.getInterfaceName());
        mdmInterfaceDTO.setInterfaceDesc(mdmInterfaceVO.getInterfaceDesc());
        mdmInterfaceDTO.setProjectId(mdmInterfaceVO.getProjectId());
        mdmInterfaceDTO.setInterfaceUrl(mdmInterfaceVO.getInterfaceUrl());
        mdmInterfaceDTO.setProtocolId(mdmInterfaceVO.getProtocolId());
        mdmInterfaceDTO.setMethodId(mdmInterfaceVO.getMethodId());
        mdmInterfaceDTO.setUpdateTime(mdmInterfaceVO.getUpdateTime());
        mdmInterfaceDTO.setOperatorId(mdmInterfaceVO.getOperatorId());
        mdmInterfaceDTO.setStatusId(mdmInterfaceVO.getStatusId());

        this.interfaceDataSourcesMapper.deleteInputParam(mdmInterfaceVO.getInterfaceId());
        this.interfaceDataSourcesMapper.deleteOutParam(mdmInterfaceVO.getInterfaceId());

        for ( MdmInfoDataSourceInterfaceParamInDTO mdparamIn: mdmInterfaceVO.getParamInput()){
            mdparamIn.setInterfaceId(mdmInterfaceVO.getInterfaceId());
        }

        for (MdmInfoDataSourceInterfaceParamOutDTO mdparamOut:mdmInterfaceVO.getParamOut()){
            mdparamOut.setInterfaceId(mdmInterfaceVO.getInterfaceId());
        }

        this.interfaceDataSourcesMapper.updateInterfaceDataSourcesById(mdmInterfaceVO);

        this.interfaceDataSourcesMapper.updateInputNodes(mdmInterfaceVO.getParamInput());

        this.interfaceDataSourcesMapper.updateOutNodes(mdmInterfaceVO.getParamOut());
        r.setMsg("接口数据源信息修改成功");
        return r;

    }

    @Override
    public MdmInfoDataSourceInterfaceDTO testInterfaceDataSources(String interfaceId) throws Exception {
        return null;
    }

    @Override
    public void deleteInterfaceDataSources(String interfaceId) {
        this.interfaceDataSourcesMapper.deleteInputParam(interfaceId);
        this.interfaceDataSourcesMapper.deleteOutParam(interfaceId);
        this.interfaceDataSourcesMapper.deleteInterfaceDataSources(interfaceId);
    }

    @Override
    public void updateInterfaceDataSourcesStatus(MdmInfoDataSourceInterfaceDTO mdsid) throws Exception {

        this.interfaceDataSourcesMapper.updateInterfaceDataSourcesStatus(mdsid);
    }

    @Override
    public R<List<MdmDimDataSourceInterfaceMethodDTO>> listRequestMode() {
        return  new R(interfaceDataSourcesMapper.listRequestMode());
    }

    @Override
    public R<List<MdmDimDataSourceInterfaceStatusDTO>> listInterfaceDataSourcesStatus() throws Exception {
        return new R(interfaceDataSourcesMapper.listInterfaceDataSourcesStatus());
    }

    @Override
    public String getInterfaceUrlById(String interfaceId) {
        return interfaceDataSourcesMapper.getInterfaceUrlById(interfaceId);
    }

    @Override
    public String getinterfaceMethodById(String interfaceId) throws Exception {
        return interfaceDataSourcesMapper.getinterfaceMethodById(interfaceId);
    }

    @Override
    public R<List<MdmInfoDataSourceInterfaceVO>> listInterfaceName() throws Exception{
        return new R(interfaceDataSourcesMapper.listInterfaceName());
    }

    @Override
    public R<List<MdmInfoDataSourceInterfaceVO>> getInterfaceByProjectId(String projectId) throws Exception{
        return new R(interfaceDataSourcesMapper.getInterfaceByProjectId(projectId));
    }

    @Override
    public R<List<MdmInfoDataSourceInterfaceParamOutDTO>> getInterfaceOutParamsByInterfaceId(String interfaceId) {
        return new R(interfaceDataSourcesMapper.getInterfaceOutParamsByInterfaceId(interfaceId));
    }

    @Override
    public String getCheckCodeById(String interfaceId) throws Exception {
        return interfaceDataSourcesMapper.getCheckCodeById(interfaceId);
    }

    @Override
    public  List<MdmInfoDataSourceInterfaceParamInDTO> getInterfaceInParamsByInterfaceId(String interfaceId) {
        return  interfaceDataSourcesMapper.getInterfaceInParamsByInterfaceId(interfaceId);
    }

    /**
     * 获取节点数据
     *
     * @param body
     * @param nodeSite
     * @return
     */
    public Object getParamOutDateValue(JSONObject body, String nodeSite) {
        String[] keyArr = nodeSite.split("\\.");
        Object dataValue = null;
        int i = 1;
        for (String key : keyArr) {
            // 是否最后一级
            if (i == keyArr.length) {
                dataValue = body.get(key);
            } else {
                body = (JSONObject) JSONObject.parse(body.get(key).toString());
            }
            i++;
        }
        return dataValue;

    }

    @Override
    public R<List<MdmTreeDTO>> listProjectAndInterfaceTree() throws Exception {
        List list = new ArrayList();
        List<MdmTreeDTO> mdmInfoData = interfaceDataSourcesMapper.listInterfaceInfo();

        List<MdmTreeDTO> mdmInfoProject=interfaceDataSourcesMapper.listInterfaceProjectInfo();
        list.addAll(mdmInfoData);
        list.addAll(mdmInfoProject);
        return  new R(list);
    }
}
