content: package com.xiliu.generator.controller;
  
  import cn.hutool.core.convert.Convert;
  import com.baomidou.mybatisplus.core.metadata.IPage;
  import com.xiliu.common.result.R;
  import com.xiliu.common.annotation.Log;
  import com.xiliu.common.controller.BaseController;
  import com.xiliu.common.enums.BusinessType;
  import com.xiliu.common.page.TableDataInfo;
  import com.xiliu.generator.entity.GenTable;
  import com.xiliu.generator.service.GenTableService;
  import io.swagger.annotations.ApiOperation;
  import org.apache.commons.io.IOUtils;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.*;
  
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  import java.util.List;
  import java.util.Map;
  
  
  /**
   * @author xiliu
   * @description
   * @date 2022/7/14
   */
  @RestController
  @RequestMapping("/tool/gen")
  public class GenController extends BaseController {
  
      @Autowired
      private GenTableService genTableService;
  
      @ApiOperation(value = "查询数据库列表")
      @GetMapping("/db/list")
      public R dataList(GenTable genTable) {
          IPage<GenTable> list = genTableService.selectDbTableList(genTable);
          return R.ok(list);
      }
  
      @ApiOperation(value = "保存表结构")
      @PostMapping("/importTable")
      public R importTableSave(String tables) {
          String[] tableNames = Convert.toStrArray(tables);
          // 查询表信息
          List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
          genTableService.importGenTable(tableList);
          return R.ok();
      }
  
      @ApiOperation(value = "查询生成表列表")
      @GetMapping("/list")
      public TableDataInfo genList(GenTable genTable) {
          startPage();
          List<GenTable> list = genTableService.selectGenTableList(genTable);
          return getDataTable(list);
      }
  
      @ApiOperation(value = "预览代码")
      @GetMapping("/preview/{tableId}")
      public R preview(@PathVariable("tableId") Long tableId) {
          Map<String, String> dataMap = genTableService.previewCode(tableId);
          return R.ok(dataMap);
      }
  
      @ApiOperation(value = "生成代码（下载方式）")
      @GetMapping("/download/{tableName}")
      public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
          byte[] data = genTableService.downloadCode(tableName);
          genCode(response, data);
      }
  
      @Log(title = "代码生成", businessType = BusinessType.GENCODE)
      @ApiOperation(value = "批量生成代码")
      @GetMapping("/download/batch")
      public void batchGenCode(HttpServletResponse response, String tables) throws IOException {
          String[] tableNames = Convert.toStrArray(tables);
          byte[] data = genTableService.downloadCode(tableNames);
          genCode(response, data);
      }
  
      @ApiOperation(value = "删除代码生成")
      @DeleteMapping("/{tableIds}")
      public R remove(@PathVariable Long[] tableIds) {
          genTableService.deleteGenTableByIds(tableIds);
          return R.ok();
      }
  
      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=\"xiliu.zip\"");
          response.addHeader("Content-Length", "" + data.length);
          response.setContentType("application/octet-stream; charset=UTF-8");
          IOUtils.write(data, response.getOutputStream());
      }
  }
  