package com.usky.data.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.data.domain.DataInterface;
import com.usky.data.service.DataInterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据中心_数据接口表 前端控制器
 * </p>
 *
 * @author fyc
 * @since 2026-01-24
 */
@RestController
@RequestMapping("/dataInterface")
public class DataInterfaceController {

    @Autowired
    private DataInterfaceService dataInterfaceService;

    /**
     * 获取数据接口列表
     *
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @param keyword     关键字
     * @param type        接口类型 1:SQL操作, 2:静态数据, 3:API操作
     * @param enabledMark 启用状态 0:未启用，1:已启用
     * @param category    接口分类
     * @return
     */
    @RequestMapping
    @GetMapping
    public ApiResult<CommonPage<DataInterface>> getDataInterfaceList(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                                                     @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                                                     @RequestParam(value = "keyword", required = false) String keyword,
                                                                     @RequestParam(value = "type", required = false) String type,
                                                                     @RequestParam(value = "enabledMark", required = false) Integer enabledMark,
                                                                     @RequestParam(value = "category", required = false) String category
    ) {
        return ApiResult.success(dataInterfaceService.getDataInterfaceList(currentPage, pageSize, keyword, type, enabledMark, category));
    }

    /**
     * 获取接口列表(工作流选择时调用)
     *
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @param keyword     关键字
     * @param type        接口类型 1:静态数据, 2:SQL操作, 3:API操作
     * @param enabledMark 启用状态 0:未启用，1:已启用
     * @return
     */
    @GetMapping("/getList")
    public ApiResult<CommonPage<DataInterface>> getWorkflowList(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                                                @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                                                @RequestParam(value = "keyword", required = false) String keyword,
                                                                @RequestParam(value = "type", required = false) String type,
                                                                @RequestParam(value = "enabledMark", required = false) Integer enabledMark
    ) {
        return ApiResult.success(dataInterfaceService.getWorkflowList(currentPage, pageSize, keyword, type, enabledMark));
    }

    /**
     * 获取接口列表下拉框
     *
     * @return
     */
    @GetMapping("/Selector")
    public ApiResult<List<DataInterface>> getSelector() {
        return ApiResult.success(dataInterfaceService.getSelectorList());
    }

    /**
     * 获取接口参数列表下拉框
     *
     * @param id 主键
     * @return
     */
    @GetMapping("/getParam/{id}")
    public ApiResult<String> getInterfaceParams(@PathVariable("id") String id) {
        return ApiResult.success(dataInterfaceService.getInterfaceParameters(id));
    }

    /**
     * 获取接口数据
     *
     * @param id 主键
     * @return
     */
    @GetMapping("/{id}")
    public ApiResult<DataInterface> getInterfaceInfo(@PathVariable("id") String id) {
        return ApiResult.success(dataInterfaceService.getById(id));
    }

    /**
     * 添加接口
     *
     * @param dataInterface 接口实体
     * @return
     */
    @PostMapping
    public ApiResult<Boolean> addInterface(@RequestBody DataInterface dataInterface) {
        return ApiResult.success(dataInterfaceService.addInterface(dataInterface));
    }

    /**
     * 修改接口
     *
     * @param dataInterface 接口实体
     * @return
     */
    @PutMapping
    public ApiResult<Boolean> updateInterface(@RequestBody DataInterface dataInterface) {
        return ApiResult.success(dataInterfaceService.updateInterface(dataInterface));
    }

    /**
     * 删除接口
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("/{id}")
    public ApiResult<Boolean> deleteInterface(@PathVariable("id") Long id) {
        return ApiResult.success(dataInterfaceService.deleteInterface(id));
    }

    /**
     * 更新接口状态
     *
     * @param id 主键
     * @return
     */
    @PutMapping("/{id}/Actions/State")
    public ApiResult<Boolean> updateInterfaceState(@PathVariable("id") String id) {
        return ApiResult.success(dataInterfaceService.updateInterfaceStatus(id));
    }

    /**
     * 测试接口
     *
     * @param id     主键
     * @param params 参数、参数值对象
     * @return
     */
    @PostMapping("/{id}/actions/preview")
    public ApiResult<?> testInterface(@PathVariable("id") String id, @RequestBody(required = false) Map<String, Object> params) {
        return dataInterfaceService.testInterface(id, params);
    }

    /**
     * 访问接口GET
     *
     * @param id     主键
     * @param params 参数、参数值对象
     * @return
     */
    @GetMapping("/{id}/actions/response")
    public ApiResult<?> getInterfaceResponse(@PathVariable("id") String id, @RequestParam(required = false) Map<String, String> params) {
        return dataInterfaceService.getInterfaceResponse(id, params);
    }

    /**
     * 访问接口POST
     *
     * @param id     主键
     * @param params 参数、参数值对象
     * @return
     */
    @PostMapping("/{id}/actions/response")
    public ApiResult<?> postInterfaceResponse(@PathVariable("id") String id, @RequestBody(required = false) Map<String, String> params) {
        return dataInterfaceService.postInterfaceResponse(id, params);
    }

    /**
     * 获取接口分页数据
     *
     * @param id         主键
     * @param pageParams 分页参数
     * @return
     */
    @PostMapping("/{id}/actions/list")
    public ApiResult<?> getInterfacePageData(@PathVariable("id") String id, @RequestBody Map<String, Object> pageParams) {
        return dataInterfaceService.getInterfacePageData(id, pageParams);
    }

    /**
     * 获取接口详情数据
     *
     * @param id         主键
     * @param pageParams 分页参数
     * @return
     */
    @PostMapping("/{id}/actions/infoByIds")
    public ApiResult<List<Map<String, Object>>> getInterfaceDetailData(@PathVariable("id") String id, @RequestBody Map<String, Object> pageParams) {
        return dataInterfaceService.getInterfaceDetailData(id, pageParams);
    }

    /**
     * 获取接口字段
     *
     * @param id         主键
     * @param pageParams 参数、参数值
     * @return
     */
    @PostMapping("/{id}/actions/getFields")
    public ApiResult<List<String>> getInterfaceFields(@PathVariable("id") String id, @RequestBody(required = false) Map<String, Object> pageParams) {
        return dataInterfaceService.getInterfaceFields(id, pageParams);
    }
}

