package com.hchyun.generator.controller;

import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.core.entity.AjaxResult;
import com.hchyun.common.utils.ServerResult;
import com.hchyun.generator.entity.AssColumn;
import com.hchyun.generator.service.AssociatedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author 18209
 * @Date 2021/2/5 17:38
 * @Version 1.0
 */
@RestController
@Api("关联查询")
@RequestMapping("/ass")
public class AssociatedController {
    private Logger logger = LoggerFactory.getLogger(AssociatedController.class);

    @Autowired
    private AssociatedService associatedService;


    @ApiOperation("tableid")
    @GetMapping("/{tableId}")
    private AjaxResult assColumnsByTable(@PathVariable("tableId")Long tableId){
        try {
            ServerResult<List<AssColumn>> serverResult = associatedService.assColumnsByTable(tableId);
            if (serverResult.isStart()){
                return AjaxResult.success(serverResult.getData());
            }else {
                return AjaxResult.error(serverResult.getMsg());
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error();
        }
    }

    @ApiOperation("获取表信息")
    @GetMapping("/tableinfos")
    public AjaxResult tableInfos(){
        try {
            ServerResult<List<Map<String,Object>>> serverResult = associatedService.tableInfos();
            if (serverResult.isStart()){
                return AjaxResult.success(serverResult.getData());
            }else {
                return AjaxResult.error(serverResult.getMsg());
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }
    @ApiOperation("关联表字段")
    @GetMapping("/{tableName}/{tableId}")
    public AjaxResult assColumns(@PathVariable("tableName")String tableName,@PathVariable("tableId") Long tableId){
        try {
            ServerResult<List<AssColumn>> serverResult = associatedService.assColumns(tableName, tableId);
            if (serverResult.isStart()){
                return AjaxResult.success(serverResult.getData());
            }else {
                return AjaxResult.error(serverResult.getMsg());
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }
}
