package com.boman.gen.controller;


import com.boman.domain.constant.UserConstants;
import com.boman.common.core.utils.SecurityUtils;
import com.boman.common.core.web.controller.BaseController;
import com.boman.domain.dto.AjaxResult;
import com.boman.common.core.web.page.TableDataInfo;
import com.boman.common.log.annotation.Log;
import com.boman.common.log.enums.BusinessType;
import com.boman.domain.GenTableColumn;
import com.boman.gen.service.IGenTableColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tjf
 * @Description gen_table_column
 * @Date 2021/3/24
 */
@RequestMapping("/genTableColumn")
@RestController
public class GenTableColumnController extends BaseController {

    @Autowired
    private IGenTableColumnService genTableColumnService;




    /**
     * 查询代码生成列表
     */
    @GetMapping("/list")
    public TableDataInfo genTableColumnList(GenTableColumn genTableColumn) {
        startPage();
        List<GenTableColumn> list = genTableColumnService.selectGenTableColumnList(genTableColumn);
        return getDataTable(list);
    }

    /**
    * @Description 新增业务表字段
    * @author tjf
    * @Date 2021/3/24
    */
    @PostMapping
    @Log(title = "新增业务表字段", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody GenTableColumn genTableColumn) {
        if (UserConstants.NOT_UNIQUE.equals(genTableColumnService.checkColumnNameUnique(genTableColumn)))
        {
            return AjaxResult.error("修改参数'" + genTableColumn.getColumnName() + "'失败，参数键名已存在");
        }
        genTableColumn.setCreateBy(SecurityUtils.getUsername());
        return toAjax(genTableColumnService.insertGenTableColumn(genTableColumn));
    }



    /**
    * @Description 修改业务表字段 列名称不可以修改
    * @author tjf
    * @Date 2021/3/24
    */
    @Log(title = "修改业务表字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody GenTableColumn genTableColumn)
    {
        genTableColumn.setUpdateBy(SecurityUtils.getUsername());
        return toAjax( genTableColumnService.updateGenTableColumn(genTableColumn));
    }

    /**
     * 删除表字段
     */
    @Log(title = "删除表字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{columnIds}")
    public AjaxResult remove(@PathVariable String columnIds)
    {
        return toAjax(genTableColumnService.deleteGenTableColumnByColumnIds(columnIds));
    }

    /**
     * 根据表编号获取字段详细信息
     */
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable Long tableId)
    {
        return AjaxResult.success(genTableColumnService.selectGenTableColumnListByTableId(tableId));
    }


    /**
     * 功能描述: 根据表名查询改表所有的字段信息
     *
     * @param tableName tableName
     * @return java.util.List<com.boman.domain.GenTableColumn>
     */
    @GetMapping(value = "/listColumnsByTableName/{tableName}")
    public List<GenTableColumn> listColumnsByTableName(@PathVariable("tableName") String tableName) {
        return genTableColumnService.listColumnsByTableName(tableName);
    }

    /**
     * 根据字段编号获取字段详细信息
     */
    @GetMapping(value = "/getColumnInfo/{columnId}")
    public AjaxResult getColumnInfo(@PathVariable Long columnId)
    {
        return AjaxResult.success(genTableColumnService.selectGenTableColumnListByColumnId(columnId));
    }


    /**
     * 根据字段编号获取字段详细信息, 把此外键对应的名称返回给前台
     */
    @GetMapping(value = "/getByIdWithForeignKey/{columnId}")
    public AjaxResult getByIdWithForeignKey(@PathVariable Long columnId)
    {
        return AjaxResult.success(genTableColumnService.getByIdWithForeignKey(columnId));
    }

    /**
     * 根据主键查找
     */
    @GetMapping(value = "/pk/{columnId}")
    public GenTableColumn getById(@PathVariable Long columnId) {
        return genTableColumnService.selectGenTableColumnListByColumnId(columnId);
    }

}
