package com.ruoyi.generator.controller;

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.poi.ExcelUtil;
import com.ruoyi.generator.domain.Template;
import com.ruoyi.generator.service.ITemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 代码模版Controller
 * 
 * @author ruoyi
 * @date 2023-11-24
 */
@RestController
@RequestMapping("/generator/template")
public class TemplateController extends BaseController
{
    @Autowired
    private ITemplateService templateService;

    /**
     * 查询代码模版列表
     */
    @PreAuthorize("@ss.hasPermi('generator:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(Template template)
    {
        startPage();
        List<Template> list = templateService.selectTemplateList(template);
        return getDataTable(list);
    }

    /**
     * 导出代码模版列表
     */
    @PreAuthorize("@ss.hasPermi('generator:template:export')")
    @Log(title = "代码模版", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Template template)
    {
        List<Template> list = templateService.selectTemplateList(template);
        ExcelUtil<Template> util = new ExcelUtil<Template>(Template.class);
        util.exportExcel(response, list, "代码模版数据");
    }

    /**
     * 查询所有代码模版列表
     */
    @PreAuthorize("@ss.hasPermi('generator:template:list')")
    @GetMapping("/all")
    public AjaxResult getAllTemplates()
    {
        List<Template> list = templateService.selectAllTemplates();
        return success(list);
    }

    /**
     * 获取代码模版详细信息
     */
    @PreAuthorize("@ss.hasPermi('generator:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return success(templateService.selectTemplateByTemplateId(templateId));
    }

    /**
     * 新增代码模版
     */
    @PreAuthorize("@ss.hasPermi('generator:template:add')")
    @Log(title = "代码模版", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Template template)
    {
        return toAjax(templateService.insertTemplate(template));
    }

    /**
     * 修改代码模版
     */
    @PreAuthorize("@ss.hasPermi('generator:template:edit')")
    @Log(title = "代码模版", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Template template)
    {
        return toAjax(templateService.updateTemplate(template));
    }

    /**
     * 删除代码模版
     */
    @PreAuthorize("@ss.hasPermi('generator:template:remove')")
    @Log(title = "代码模版", businessType = BusinessType.DELETE)
	@DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds)
    {
        return toAjax(templateService.deleteTemplateByTemplateIds(templateIds));
    }

    /**
     * 修改模版状态
     */
    @PreAuthorize("@ss.hasPermi('generator:template:edit')")
    @Log(title = "代码模版", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Template template)
    {
        return toAjax(templateService.updateTemplateStatus(template));
    }

}
