package com.hchyun.generator.service.impl;

import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.utils.ServerResult;
import com.hchyun.generator.dao.AssociatedDao;
import com.hchyun.generator.entity.AssColumn;
import com.hchyun.generator.service.AssociatedService;
import com.hchyun.generator.util.AssociatedUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author 18209
 * @Date 2021/2/5 17:56
 * @Version 1.0
 */
@Service
public class AssociatedServiceImpl implements AssociatedService {
    private Logger logger = LoggerFactory.getLogger(AssociatedServiceImpl.class);

    @Autowired
    private AssociatedDao associatedDao;

    @Override
    public ServerResult<List<AssColumn>> assColumnsByTable(Long tableId) {
        try {
            List<AssColumn> assColumns = associatedDao.selectTableColumnByTableId(tableId);
            return new ServerResult<>(true,assColumns);
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<>(false,ReturnConstants.DB_EX);
        }
    }

    @Override
    public ServerResult<List<Map<String, Object>>> tableInfos() {
        try {
            List<Map<String, Object>> dataMap = associatedDao.tableInfos();
            return new ServerResult<>(true, dataMap);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<>(false, ReturnConstants.DB_EX);
        }
    }

    @Override
    public ServerResult<List<AssColumn>> assColumns(String tableName, Long tableId) {
        try {
            List<AssColumn> assColumns = associatedDao.selectTableColumn(tableName);
            for (AssColumn assColumn : assColumns) {
                AssociatedUtils.initAssColumn(assColumn, tableId);
            }
            return new ServerResult<>(true, assColumns);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<>(false, ReturnConstants.DB_EX);
        }
    }
}
