package com.qiaoba.module.generator.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.module.generator.entity.GeneratorTableColumn;
import com.qiaoba.module.generator.entity.vo.TableColumnVo;
import com.qiaoba.module.generator.mapper.GeneratorTableColumnMapper;
import com.qiaoba.module.generator.service.GeneratorTableColumnService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 表字段 服务层实现
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/6/26 10:12
 */
@Service
@RequiredArgsConstructor
public class GeneratorTableColumnServiceImpl implements GeneratorTableColumnService {

    private final GeneratorTableColumnMapper generatorTableColumnMapper;

    @Override
    public List<TableColumnVo> selectDbColumnList(String tableName, boolean allowNull) {
        List<TableColumnVo> list = generatorTableColumnMapper.selectDbColumnList(tableName);
        if (CollUtil.isEmpty(list) && !allowNull) {
            throw new ServiceException(StrUtil.format("未查询到表字段信息, 表名: {}", tableName));
        }
        return list;
    }

    @Override
    public List<GeneratorTableColumn> selectListByTableId(String tableId, boolean allowNull) {
        QueryWrapper<GeneratorTableColumn> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(GeneratorTableColumn::getTableId, tableId);
        List<GeneratorTableColumn> list = generatorTableColumnMapper.selectList(wrapper);
        if (CollUtil.isEmpty(list) && !allowNull) {
            throw new ServiceException(StrUtil.format("未查询到表字段信息, tableId: {}", tableId));
        }
        return list;
    }


    @Override
    public Integer insert(GeneratorTableColumn generatorTableColumn) {
        return generatorTableColumnMapper.insert(generatorTableColumn);
    }

    @Override
    public void updateById(GeneratorTableColumn generatorTableColumn) {
        generatorTableColumnMapper.updateById(generatorTableColumn);
    }

    @Override
    public void updateBatchById(List<GeneratorTableColumn> columns) {
        Db.updateBatchById(columns);
    }

    @Override
    public void deleteByTableIds(List<String> tableIds) {
        generatorTableColumnMapper.deleteByTableIds(tableIds);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        generatorTableColumnMapper.deleteBatchIds(ids);
    }
}
