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.GenProject;
import com.ruoyi.generator.service.IGenProjectService;
import org.apache.commons.io.IOUtils;
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.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 项目表Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/generator/project")
public class GenProjectController extends BaseController
{
    @Autowired
    private IGenProjectService genProjectService;

    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('generator:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(GenProject genProject)
    {
        startPage();
        List<GenProject> list = genProjectService.selectGenProjectList(genProject);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @PreAuthorize("@ss.hasPermi('generator:project:export')")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GenProject genProject)
    {
        List<GenProject> list = genProjectService.selectGenProjectList(genProject);
        ExcelUtil<GenProject> util = new ExcelUtil<GenProject>(GenProject.class);
        util.exportExcel(response, list, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('generator:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        return success(genProjectService.selectGenProjectById(projectId));
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('generator:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GenProject genProject)
    {
        return toAjax(genProjectService.insertGenProject(genProject));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('generator:project:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GenProject genProject)
    {
        return toAjax(genProjectService.updateGenProject(genProject));
    }

    /**
     * 删除项目
     */
    @PreAuthorize("@ss.hasPermi('generator:project:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(genProjectService.deleteGenProjectByIds(projectIds));
    }

    /**
     * 获取项目结构
     */
    @PreAuthorize("@ss.hasPermi('generator:project:preview')")
    @GetMapping("/structure/{projectId}/{type}")
    public AjaxResult getStructure(@PathVariable("projectId") Long projectId, @PathVariable("type") String type) {
        return success(genProjectService.getProjectStructure(projectId, type));
    }

    /**
     * 预览代码
     */
    @PreAuthorize("@ss.hasPermi('generator:project:preview')")
    @GetMapping("/preview/{projectId}/{tableId}/{keyword}/{type}")
    public AjaxResult preview(@PathVariable("projectId") Long projectId,
                              @PathVariable("tableId") Long tableId,
                              @PathVariable("keyword") String keyword,
                              @PathVariable("type") String type) throws IOException
    {
        Map<String, String> dataMap = genProjectService.previewCode(projectId, tableId, keyword, type);
        return success(dataMap);
    }

    /**
     * 下载项目结构
     */
    @PreAuthorize("@ss.hasPermi('generator:project:download')")
    @GetMapping("/download/{projectId}/{type}")
    public void downloadStructure(@PathVariable("projectId") Long projectId,
                                  @PathVariable("type") String type,
                                  HttpServletResponse response) throws IOException {
        byte[] data = genProjectService.downloadStructure(projectId, type);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"project.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
} 