package com.ruoyi.generator.controller;

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

import com.ruoyi.generator.domain.Template;
import com.ruoyi.generator.service.ITemplateService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generator.domain.SysProjectTemplate;
import com.ruoyi.generator.service.ISysProjectTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目模板配置Controller
 * 
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/generator/project/template")
public class SysProjectTemplateController extends BaseController
{
    @Autowired
    private ISysProjectTemplateService sysProjectTemplateService;
    @Autowired
    private ITemplateService templateService;

    /**
     * 查询项目模板配置列表
     */
    @PreAuthorize("@ss.hasPermi('generator:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProjectTemplate sysProjectTemplate)
    {
        startPage();
        List<SysProjectTemplate> list = sysProjectTemplateService.selectSysProjectTemplateList(sysProjectTemplate);
        return getDataTable(list);
    }

    /**
     * 根据项目ID查询项目模板配置列表
     */
    @PreAuthorize("@ss.hasPermi('generator:template:list')")
    @GetMapping("/list/{projectId}")
    public List<SysProjectTemplate> listByProjectId(@PathVariable("projectId") Long projectId)
    {
        List<SysProjectTemplate> list = sysProjectTemplateService.selectSysProjectTemplateListByProjectId(projectId);
        return list;
    }

    /**
     * 导出项目模板配置列表
     */
    @PreAuthorize("@ss.hasPermi('generator:template:export')")
    @Log(title = "项目模板配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProjectTemplate sysProjectTemplate)
    {
        List<SysProjectTemplate> list = sysProjectTemplateService.selectSysProjectTemplateList(sysProjectTemplate);
        ExcelUtil<SysProjectTemplate> util = new ExcelUtil<SysProjectTemplate>(SysProjectTemplate.class);
        util.exportExcel(response, list, "项目模板配置数据");
    }

    /**
     * 获取项目模板配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('generator:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysProjectTemplateService.selectSysProjectTemplateById(id));
    }

    /**
     * 新增项目模板配置
     */
    @PreAuthorize("@ss.hasPermi('generator:template:add')")
    @Log(title = "项目模板配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProjectTemplate sysProjectTemplate)
    {
        Template template = templateService.selectTemplateByTemplateId(sysProjectTemplate.getTemplateId());
        sysProjectTemplate.setTemplateType(template.getTemplateType());
        return toAjax(sysProjectTemplateService.insertSysProjectTemplate(sysProjectTemplate));
    }

    /**
     * 修改项目模板配置
     */
    @PreAuthorize("@ss.hasPermi('generator:template:edit')")
    @Log(title = "项目模板配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProjectTemplate sysProjectTemplate)
    {
        return toAjax(sysProjectTemplateService.updateSysProjectTemplate(sysProjectTemplate));
    }

    /**
     * 删除项目模板配置
     */
    @PreAuthorize("@ss.hasPermi('generator:template:remove')")
    @Log(title = "项目模板配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysProjectTemplateService.deleteSysProjectTemplateByIds(ids));
    }

    /**
     * 删除项目模板配置
     */
    @PreAuthorize("@ss.hasPermi('generator:template:remove')")
    @Log(title = "项目模板配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{projectId}")
    public AjaxResult removeByProjectId(@PathVariable Long projectId)
    {
        return toAjax(sysProjectTemplateService.deleteSysProjectTemplateByProjectId(projectId));
    }
}
