package com.lightinit.hsdataplatformresdir.impl.admin;

import com.lightinit.hsdataplatformresdir.entity.*;
import com.lightinit.hsdataplatformresdir.mapper.*;

import com.lightinit.hsdataplatformresdir.model.admin.APIOpenInterfaceViewModel;
import com.lightinit.hsdataplatformresdir.model.admin.APIOpenViewModel;
import com.lightinit.hsdataplatformresdir.model.admin.DataCatelogResourceModel;

import com.lightinit.hsdataplatformresdir.model.admin.LoginUser;
import com.lightinit.hsdataplatformresdir.service.admin.IDataCatelogResourceService;
import com.lightinit.hsdataplatformresdir.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class DataCatelogResourceServiceImpl implements IDataCatelogResourceService {
    @Autowired
    private DataCatelogMapper dataCatelogMapper;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private ResourceDataMapper resourceDataMapper;
    @Autowired
    private ResourceApiInterfaceInputMapper interfaceInputMapper;
    @Autowired
    private ResourceApiInterfaceOutputMapper interfaceOutputMapper;
    @Autowired
    private ResourceApiInterfaceMapper resourceApiInterfaceMapper;
    @Override
    public List<DataCatelogResourceModel> queryList(Long id,BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);

        return dataCatelogMapper.selectList(id,pager);
    }

    @Override
    public long queryCount(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return dataCatelogMapper.selectCount(id);
    }

    @Override
    public APIOpenViewModel toDetail(Long id) {
        APIOpenViewModel apiOpenViewModel = new APIOpenViewModel();
        Resource resource = resourceMapper.selectByPrimaryKey(id);
//        查询resource_api_interface
        ResourceApiInterfaceExample resourceApiInterfaceExample = new ResourceApiInterfaceExample();
        ResourceApiInterfaceExample.Criteria criteria = resourceApiInterfaceExample.createCriteria();
        criteria.andApiIdEqualTo(id);
        List<ResourceApiInterfaceWithBLOBs> list = resourceApiInterfaceMapper.selectByExampleWithBLOBs(resourceApiInterfaceExample);
//       List<ResourceApiInterfaceWithBLOBs>  list = dataCatelogMapper.selectView(id);
        if (list.size() > 0) {
            List<APIOpenInterfaceViewModel> apiOpenInterfaceViewModelList = new ArrayList<>();
            for (ResourceApiInterfaceWithBLOBs resourceApiInterfaceWithBLOBs : list) {
                APIOpenInterfaceViewModel apiOpenInterfaceViewModel = new APIOpenInterfaceViewModel();

                BeanUtils.copyProperties(resourceApiInterfaceWithBLOBs, apiOpenInterfaceViewModel);

                //查询查询resource_api_interfance_input表
                ResourceApiInterfaceInputExample inputExample = new ResourceApiInterfaceInputExample();
                ResourceApiInterfaceInputExample.Criteria criteria1 = inputExample.createCriteria();
                criteria1.andInterfaceIdEqualTo(resourceApiInterfaceWithBLOBs.getId());
                List<ResourceApiInterfaceInput> inputs = interfaceInputMapper.selectByExample(inputExample);
                apiOpenInterfaceViewModel.setInterfaceInputs(inputs);
                //查询查询resource_api_interfance_output表
                ResourceApiInterfaceOutputExample outputExample = new ResourceApiInterfaceOutputExample();
                ResourceApiInterfaceOutputExample.Criteria criteria2 = outputExample.createCriteria();
                criteria2.andInterfaceIdEqualTo(resourceApiInterfaceWithBLOBs.getId());
                List<ResourceApiInterfaceOutput> outputs = interfaceOutputMapper.selectByExample(outputExample);

                apiOpenInterfaceViewModel.setInterfaceOutputs(outputs);

                apiOpenInterfaceViewModelList.add(apiOpenInterfaceViewModel);
            }
            apiOpenViewModel.setInterfaceViewModels(apiOpenInterfaceViewModelList);
        }
        apiOpenViewModel.setResourceName(resource.getResourceName());
        return apiOpenViewModel;
    }

    @Override
    public ResourceDataWithBLOBs downloadById(Long id, LoginUser loginUser) {
        return  resourceDataMapper.selectByPrimaryKey(id);
    }
}
