package com.starry.generator.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.generator.entity.GenTableEntity;
import com.starry.generator.entity.vo.GenTableVo;
import com.starry.generator.service.GenTableService;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 * @since 2022/7/14
 */
@RestController
@RequestMapping("/tool/gen")
public class GenController {

    @Resource
    private GenTableService genTableService;

    @ApiOperation(value = "查询数据库列表")
    @GetMapping("/db/list")
    public R dataList(GenTableEntity genTableEntity) {
        IPage<GenTableEntity> list = genTableService.selectDbTableList(genTableEntity);
        return R.ok(list);
    }

    @ApiOperation(value = "保存表结构")
    @PostMapping("/importTable")
    public R importTableSave(String tables) {
        String[] tableNames = Convert.toStrArray(tables);
        // 查询表信息
        List<GenTableEntity> tableList = genTableService.selectDbTableListByNames(tableNames);
        genTableService.importGenTable(tableList);
        return R.ok();
    }

    @ApiOperation(value = "查询生成表列表")
    @GetMapping("/list")
    public R genList(GenTableVo vo) {
        return R.ok(genTableService.selectGenTableList(vo));
    }

    @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=\"admin.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
