package com.qiaoba.module.generator.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qiaoba.api.job.annotation.Log;
import com.qiaoba.api.job.enums.BusinessType;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.module.generator.constant.GenConstants;
import com.qiaoba.module.generator.entity.GeneratorTable;
import com.qiaoba.module.generator.entity.GeneratorTableColumn;
import com.qiaoba.module.generator.entity.dto.GeneratorTableDto;
import com.qiaoba.module.generator.entity.dto.TableDto;
import com.qiaoba.module.generator.service.GeneratorTableColumnService;
import com.qiaoba.module.generator.service.GeneratorTableService;
import com.qiaoba.module.generator.util.GeneratorUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/6/26 9:09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tool/gen")
public class GeneratorController {

    private final GeneratorTableService generatorTableService;
    private final GeneratorTableColumnService generatorTableColumnService;

    /**
     * 代码生成列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TableDto dto, PageQuery pageQuery) {
        return generatorTableService.selectPageList(dto, pageQuery);
    }

    /**
     * 代码生成详细
     */
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable String tableId) {
        GeneratorTable table = generatorTableService.selectById(tableId);
        List<GeneratorTableColumn> list = generatorTableColumnService.selectListByTableId(tableId, false);
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("info", table);
        map.put("rows", list);
        return AjaxResult.success(map);
    }

    /**
     * 修改代码生成
     */
    @PutMapping
    public AjaxResult edit(@RequestBody GeneratorTableDto dto) {
        GeneratorTable generatorTable = BeanUtil.copyProperties(dto, GeneratorTable.class);
        int result = generatorTableService.updateById(generatorTable);
        generatorTableColumnService.updateBatchById(dto.getColumns());
        return AjaxResult.toAjax(result);
    }

    /**
     * 批量删除代码生成
     */
    @DeleteMapping("/{tableIds}")
    @Log(title = "代码生成", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable List<String> tableIds) {
        return AjaxResult.toAjax(generatorTableService.deleteByIds(tableIds));
    }

    /**
     * 数据库表列表(排除已导入表)
     */
    @GetMapping("/db/list")
    public TableDataInfo dbTableList(TableDto dto, PageQuery pageQuery) {
        return generatorTableService.selectPageDbTableList(dto, pageQuery);
    }

    /**
     * 导入表结构
     */
    @PostMapping("/importTable")
    public AjaxResult importTable(@RequestBody TableDto dto) {
        return AjaxResult.toAjax(generatorTableService.importTable(dto));
    }

    /**
     * 预览代码
     */
    @GetMapping("/preview/{tableId}")
    public AjaxResult preview(@PathVariable("tableId") String tableId) {
        Map<String, String> dataMap = generatorTableService.previewCode(tableId, GenConstants.TEMPLATE_TYPE_ALL);
        return AjaxResult.success(dataMap);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download/{tableId}/{templateType}")
    @Log(title = "代码生成", businessType = BusinessType.GENCODE)
    public void download(HttpServletResponse response, @PathVariable String tableId, @PathVariable String templateType) throws IOException {
        byte[] data = generatorTableService.downloadCode(tableId, templateType);
        GeneratorUtil.download(response, data);
    }

    /**
     * 同步表结构
     */
    @GetMapping("/syncTable/{tableId}")
    public AjaxResult syncTable(@PathVariable String tableId) {
        generatorTableService.syncTable(tableId);
        return AjaxResult.success();
    }

}
