package com.boman.gen.controller;


import com.boman.common.core.constant.UserConstants;
import com.boman.common.core.utils.DateUtils;
import com.boman.common.core.utils.SecurityUtils;
import com.boman.common.core.web.controller.BaseController;
import com.boman.common.core.web.domain.AjaxResult;
import com.boman.common.log.annotation.Log;
import com.boman.common.log.enums.BusinessType;
import com.boman.common.security.annotation.PreAuthorize;
import com.boman.gen.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.*;

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

    @Autowired
    private IGenTableColumnService genTableColumnService;



    /**
    * @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.deleteGenTableColumnByIds(columnIds));
    }

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

}
