package com.test.test.controller;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.test.common.annotation.Log;
import com.test.common.core.controller.BaseController;
import com.test.common.core.domain.AjaxResult;
import com.test.common.enums.BusinessType;
import com.test.test.domain.TestApi;
import com.test.test.service.ITestApiService;
import com.test.common.utils.poi.ExcelUtil;
import com.test.common.core.page.TableDataInfo;

/**
 * 接口Controller
 *
 * @author xiaoe
 * @date 2025-02-08
 */
@RestController
@RequestMapping("/test/api")
public class TestApiController extends BaseController {
    @Autowired
    private ITestApiService testApiService;

    /**
     * 查询接口列表
     */
    @PreAuthorize("@ss.hasPermi('test:api:list')")
    @GetMapping("/list")
    public TableDataInfo list(TestApi testApi) {
        startPage();
        List<TestApi> list = testApiService.selectTestApiList(testApi);
        return getDataTable(list);
    }

    /**
     * 导出接口列表
     */
    @PreAuthorize("@ss.hasPermi('test:api:export')")
    @Log(title = "接口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TestApi testApi) {
        List<TestApi> list = testApiService.selectTestApiList(testApi);
        ExcelUtil<TestApi> util = new ExcelUtil<TestApi>(TestApi.class);
        util.exportExcel(response, list, "接口数据");
    }

    /**
     * 获取接口详细信息
     */
    @PreAuthorize("@ss.hasPermi('test:api:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(testApiService.selectTestApiById(id));
    }

    /**
     * 新增接口
     */
    @PreAuthorize("@ss.hasPermi('test:api:add')")
    @Log(title = "接口", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TestApi testApi) {
        return toAjax(testApiService.insertTestApi(testApi));
    }

    /**
     * 修改接口
     */
    @PreAuthorize("@ss.hasPermi('test:api:edit')")
    @Log(title = "接口", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TestApi testApi) {
        return toAjax(testApiService.updateTestApi(testApi));
    }

    /**
     * 删除接口
     */
    @PreAuthorize("@ss.hasPermi('test:api:remove')")
    @Log(title = "接口", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(testApiService.deleteTestApiByIds(ids));
    }
}
