package com.ebts.generator.service.impl;

import com.ebts.common.core.entity.Result;
import com.ebts.generator.dao.RelDao;
import com.ebts.generator.entity.RelColumn;
import com.ebts.generator.entity.RelTable;
import com.ebts.generator.service.RelService;
import com.ebts.generator.utils.RelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author 18209
 * @Date 2021/2/24 16:21
 * @Version 1.0
 */
@Service
public class RelServiceImpl implements RelService {
    private Logger logger = LoggerFactory.getLogger(RelServiceImpl.class);

    @Resource
    private RelDao relDao;

    /**
     * 获取到数据库表信息
     *
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Result<List<Map<String, Object>>> tableInfos() {
        List<Map<String, Object>> dataMap = relDao.tableInfos();
        return Result.ok(dataMap);
    }

    /**
     * 获取到关联边字段信息
     *
     * @param tableName 关联表表名
     * @param relId     关联关系id
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Result<List<RelColumn>> relColumns(String tableName, Long relId) {
        List<RelColumn> relColumns = relDao.selectTableColumn(tableName);
        for (RelColumn relColumn : relColumns) {
            RelUtil.initRelColumn(relColumn, relId);
        }
        return Result.ok(relColumns);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Result<List<Map<String, Object>>> tableColumns(String tableName) {
        List<Map<String, Object>> columns = relDao.tableColumns(tableName);
        return Result.ok(columns);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Result<List<RelTable>> relTableByTableId(Long tableId) {
        List<RelTable> relTables = relDao.relTableByTableId(tableId);
        return Result.ok(relTables);
    }
}