package com.boman.gen.service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Condition;

import com.boman.common.core.constant.UserConstants;
import com.boman.common.core.utils.StringUtils;
import com.boman.gen.domain.GenTable;
import com.boman.gen.mapper.GenTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boman.common.core.text.Convert;
import com.boman.gen.domain.GenTableColumn;
import com.boman.gen.mapper.GenTableColumnMapper;

/**
 * 业务字段 服务层实现
 *
 * @author ruoyi
 */
@Service
public class GenTableColumnServiceImpl implements IGenTableColumnService {
    @Autowired
    private GenTableColumnMapper genTableColumnMapper;
    @Autowired
    private GenTableMapper genTableMapper;

    /**
     * 查询业务字段列表
     *
     * @param tableId 业务字段编号
     * @return 业务字段集合
     */
    @Override
    public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId) {
        return genTableColumnMapper.selectGenTableColumnListByTableId(tableId);
    }

    /**
     * 根据gen_table_column的id查询字段对象
     * @param columnId
     * @return
     */
    @Override
    public GenTableColumn selectGenTableColumnListByColumnId(Long columnId) {
        return   genTableColumnMapper.selectGenTableColumnListByColumnId(columnId);
    }

    /**
     * 查询业务字段列表
     *
     * @param tableIdList 业务字段编号List
     * @return 业务字段集合
     */
    @Override
    public List<GenTableColumn> listByTableIdList(List<Long> tableIdList) {
        if (null == tableIdList || tableIdList.isEmpty()) {
            return Collections.emptyList();
        }

        return genTableColumnMapper.listByTableIdList(tableIdList);
    }

    /**
     * 新增业务字段
     *
     * @param genTableColumn 业务字段信息
     * @return 结果
     */
    @Override
    public int insertGenTableColumn(GenTableColumn genTableColumn) {
        return genTableColumnMapper.insertGenTableColumn(genTableColumn);
    }

    /**
     * 修改业务字段
     *
     * @param genTableColumn 业务字段信息
     * @return 结果
     */
    @Override
    public int updateGenTableColumn(GenTableColumn genTableColumn) {
        isAk(genTableColumn);
        return genTableColumnMapper.updateGenTableColumn(genTableColumn);
    }

    /**
     * 判断对象是否设置显示和输入字段
     * @param genTableColumn
     */
    public void isAk(GenTableColumn genTableColumn) {
        //是否是输入
        String isIn = genTableColumn.getIsIn();
        //是否是显示
        String isOut = genTableColumn.getIsOut();
        GenTable genTable = new GenTable();
        if (UserConstants.YES.equals(isIn)){
            genTable.setTableId(genTableColumn.getTableId());
            genTable.setAkColumn(genTableColumn.getColumnId());
        }
        if (UserConstants.YES.equals(isOut)){
            genTable.setTableId(genTableColumn.getTableId());
            genTable.setDkColumn(genTableColumn.getColumnId());
        }
        if (genTable.getTableId() != null){
            genTableMapper.updateGenTable(genTable);
        }
    }


    /**
     * 删除业务字段对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGenTableColumnByIds(String ids) {
        return genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids));
    }

    /**
     * 删除业务字段对象
     * @param ids 字段id
     * @return
     */
    @Override
    public int deleteGenTableColumnByColumnIds(String ids) {
        return genTableColumnMapper.deleteGenTableColumnByColumnIds(Convert.toLongArray(ids));
    }

    /**
     * 校验column_name是否重复
     *
     * @param genTableColumn
     * @return
     */
    @Override
    public String checkColumnNameUnique(GenTableColumn genTableColumn) {
        Long columnId = StringUtils.isNull(genTableColumn.getColumnId()) ? -1L : genTableColumn.getColumnId();
        GenTableColumn column = genTableColumnMapper.checkColumnNameUnique(genTableColumn.getColumnName());
        if (StringUtils.isNotNull(column) && column.getColumnId().longValue() != columnId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}