package top.imwork.window.silos.controller.generator;

import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.imwork.commons.core.pojo.ResponseMsg;
import top.imwork.window.silos.service.GeneratorService;

import java.util.Map;

/**
 * Copyright (C), 2015-2021
 * FileName: GeneratorController
 * Author<作者姓名>:   jiangxiaowei
 * CreateTime<创建时间>:   2021/11/14 17:38
 * UpdateTime<修改时间>:   2021/11/14 17:38
 * Description〈功能简述〉: 代码生成服务控制器
 * History<历史描述>:
 * Since<版本号>: 1.0.0.0
 */
@RequestMapping("/generator")
@Controller
public class GeneratorController {
    @GetMapping("/generator.html")
    public String index() {
        return "/generator/generator";
    }

    @Autowired
    private GeneratorService generatorService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public ResponseMsg list(@RequestParam Map<String, Object> params){
        return generatorService.queryList(params);
    }

    /**
     * 生成代码
     */
    @RequestMapping("/generator")
    public void generator(@RequestParam Map<String, String> params, HttpServletResponse response) throws Exception {
        String tables = params.get("tableName");
        byte[] data = generatorService.generatorCode(tables.split(","));
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"imwork.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}