package com.agri.generator.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.agri.generator.dto.ApiConfigDTO;
import com.agri.generator.util.CodeGenerator;
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.agri.common.annotation.Log;
import com.agri.common.core.controller.BaseController;
import com.agri.common.core.domain.AjaxResult;
import com.agri.common.enums.BusinessType;
import com.agri.generator.domain.SysApiInfo;
import com.agri.generator.service.ISysApiInfoService;
import com.agri.common.utils.poi.ExcelUtil;
import com.agri.common.core.page.TableDataInfo;

/**
 * 接口基础信息Controller
 * 
 * @author agri
 * @date 2025-12-30
 */
@RestController
@RequestMapping("/tool/api")
public class SysApiInfoController extends BaseController
{
    @Autowired
    private ISysApiInfoService sysApiInfoService;


    @Autowired
    private CodeGenerator codeGenerator;
    /**
     * 查询接口基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('tool:api:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysApiInfo sysApiInfo)
    {
        startPage();
        List<SysApiInfo> list = sysApiInfoService.selectSysApiInfoList(sysApiInfo);
        return getDataTable(list);
    }

    /**
     * 导出接口基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('tool:api:export')")
    @Log(title = "接口基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysApiInfo sysApiInfo)
    {
        List<SysApiInfo> list = sysApiInfoService.selectSysApiInfoList(sysApiInfo);
        ExcelUtil<SysApiInfo> util = new ExcelUtil<SysApiInfo>(SysApiInfo.class);
        util.exportExcel(response, list, "接口基础信息数据");
    }

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

    /**
     * 新增接口基础信息
     */
    @PreAuthorize("@ss.hasPermi('tool:api:add')")
    @Log(title = "接口基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysApiInfo sysApiInfo)
    {
        return toAjax(sysApiInfoService.insertSysApiInfo(sysApiInfo));
    }

    /**
     * 修改接口基础信息
     */
    @PreAuthorize("@ss.hasPermi('tool:api:edit')")
    @Log(title = "接口基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysApiInfo sysApiInfo)
    {
        return toAjax(sysApiInfoService.updateSysApiInfo(sysApiInfo));
    }

    /**
     * 接收前端配置，生成接口相关代码
     */
    @PostMapping("/code")
    public String generateCode(@Valid @RequestBody ApiConfigDTO apiConfig) {
        try {
            codeGenerator.generateApiCode(apiConfig);
            return "代码生成成功，生成路径：" + System.getProperty("user.dir") + "/generated-code/";
        } catch (Exception e) {
            return "代码生成失败：" + e.getMessage();
        }
    }

    /**
     * 删除接口基础信息
     */
    @PreAuthorize("@ss.hasPermi('tool:api:remove')")
    @Log(title = "接口基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysApiInfoService.deleteSysApiInfoByIds(ids));
    }
}
