package com.ruoyi.web.controller.dxp.system.integration;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dxp.constant.DxpConstants;
import com.ruoyi.dxp.domain.ServiceIntegration;
import com.ruoyi.dxp.domain.ServiceItem;
import com.ruoyi.dxp.dto.ServiceIntegrationSave;
import com.ruoyi.dxp.dto.database.ServiceUtilsApiCallParam;
import com.ruoyi.dxp.service.IServiceIntegrationService;
import com.ruoyi.dxp.service.IServiceItemService;
import com.ruoyi.dxp.utils.ServiceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 整合服务Controller
 * 
 * @author jy
 * @date 2022-11-29
 */
@RestController
@RequestMapping("/system/integration/service")
public class ServiceIntegrationController extends BaseController
{
    @Autowired
    private IServiceIntegrationService serviceIntegrationService;

    @Autowired
    private IServiceItemService serviceItemService;

    /**
     * 查询整合服务列表
     */
    @PreAuthorize("@ss.hasPermi('system/integration:service:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServiceIntegration serviceIntegration)
    {
        startPage();
        List<ServiceIntegration> list = serviceIntegrationService.selectServiceIntegrationList(serviceIntegration);
        return getDataTable(list);
    }

    /**
     * 获取整合服务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system/integration:service:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(serviceIntegrationService.selectServiceIntegrationById(id));
    }

    /**
     * 查询服务参数
     * @param id 服务id
     * @return 参数列表
     */
    @PreAuthorize("@ss.hasPermi('system/integration:service:query')")
    @GetMapping(value = "/serviceitem")
    public AjaxResult getServiceItem(Long id) {
        ServiceItem req = new ServiceItem();
        req.setServiceType(DxpConstants.DATA_TYPE_INTEGRATION);
        req.setServiceId(id);
        return success(serviceItemService.selectServiceItemList(req));
    }

    /**
     * 新增整合服务
     */
    @PreAuthorize("@ss.hasPermi('system/integration:service:add')")
    @Log(title = "整合服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceIntegrationSave serviceIntegration)
    {
        return serviceIntegrationService.save(serviceIntegration);
    }

    /**
     * 修改整合服务
     */
    @PreAuthorize("@ss.hasPermi('system/integration:service:edit')")
    @Log(title = "整合服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceIntegrationSave serviceIntegration)
    {
        return serviceIntegrationService.save(serviceIntegration);
    }

    /**
     * 删除整合服务
     */
    @PreAuthorize("@ss.hasPermi('system/integration:service:remove')")
    @Log(title = "整合服务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        return serviceIntegrationService.deleteServiceIntegrationById(ids);
    }

    /**
     * 接口调试
     */
    @PostMapping("/apicall")
    public AjaxResult apiCall(@RequestBody ServiceUtilsApiCallParam req) {
        if (StringUtils.isEmpty(req.getParamForm())) {
            req.setParamForm("body");
        }
        if (StringUtils.isEmpty((req.getRequestMethod()))) {
            req.setRequestMethod("post");
        }

        if (StringUtils.isEmpty(req.getServiceUrl())) {
            return error("请求地址不能为空");
        }

//        if (StringUtils.isEmpty(req.getRequestParam())) {
//            return error("请求参数不能为空");
//        }
        return success(ServiceUtils.apiCall(req));
    }

    @Anonymous
    @PostMapping("/alter-table")
    public AjaxResult alterTable() {
        serviceIntegrationService.alterTable();
        return success();
    }
}
