package top.imwork.assist.generator.controller;

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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.imwork.assist.generator.service.GeneratorService;
import top.imwork.commons.core.pojo.ResponseMsg;

import java.util.Map;

/**
 * 〈功能简述〉<br> 
 * 〈〉
 *
 * @author imwork
 * @create 2020/5/10 0010
 * @since 1.0.0
 */
@RequestMapping("/generator")
@Controller
public class GeneratorController {
    @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());
    }
}