package com.ebts.generator.controller;

import java.io.IOException;
import java.util.Map;


import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.enums.BusinessType;
import com.ebts.generator.dto.InterTableDto;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
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.ebts.generator.entity.InterTable;
import com.ebts.generator.service.InterTableService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 接口信息Controller
 *
 * @author binlin
 * @date 2021-01-25
 */

@RestController
@RequestMapping("/generator/intertable")
public class InterTableController extends BaseController {

    @Resource
    private InterTableService interTableService;

    /**
     * 查询接口信息列表
     */
    @PreAuthorize("@ebts.hasPermi('generator:intertable:list')")
    @GetMapping("/list")
    public Result<Map<String, Object>> list(InterTable interTable) {
        return interTableService.selectInterTableList(interTable);
    }

    /**
     * 预览代码(工作流)
     */

    @PreAuthorize("@ebts.hasPermi('generator:intertable:classpreview')")
    @Log(title = "预览代码(工作流)", businessType = BusinessType.GENCODE)
    @GetMapping("/classpreview/{id}")
    public Result<Map<String, String>> classPreview(@PathVariable("id") Long id) {
        return interTableService.previewCodeClass(id);
    }

    /**
     * 预览代码(工作台)
     */
    @PreAuthorize("@ebts.hasPermi('generator:intertable:modulepreview')")
    @Log(title = "预览代码(工作台)", businessType = BusinessType.GENCODE)
    @GetMapping("/modulepreview/{id}")
    public Result<Map<String, Object>> modulePreview(@PathVariable("id") Long id) {
        return interTableService.previewCodeModule(id);
    }


    /**
     * 生成代码(工作流)
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('generator:intertable:classgen')")
    @Log(title = "生成代码(工作流)", businessType = BusinessType.GENCODE)
    @GetMapping("/classgen/{id}")
    public Result<byte[]> classGenerator(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        Result<byte[]> result = interTableService.generatorCodeClass(id);
        if (result.isSuccess()) {
            genCode(response, result.getData());
            return Result.ok();
        } else {
            return result;
        }
    }

    /**
     * 生成代码(工作台)
     *
     * @param id
     * @param response
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('generator:intertable:moudlegen')")
    @Log(title = "生成代码(工作台)", businessType = BusinessType.GENCODE)
    @GetMapping("/modulegen/{id}")
    public Result<byte[]> moduleGenerator(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        Result<byte[]> result = interTableService.generatorCodeModule(id);
        if (result.isSuccess()) {
            genCode(response, result.getData());
            return Result.ok();
        } else {
            return result;
        }
    }


    /**
     * 修改接口信息
     */
    @PreAuthorize("@ebts.hasPermi('generator:intertable:edit')")
    @Log(title = "接口信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Integer> edit(@Validated @RequestBody InterTableDto interTableDto) {
        return interTableService.updateInterTable(interTableDto);
    }

    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"ebts.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}