package io.renren.modules.levelManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.levelManage.dao.UniversalTableDao;
import io.renren.modules.levelManage.entity.UniversalTableEntity;
import io.renren.modules.levelManage.service.UniversalTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service("universalTableService")
@DataSource("heBing")
public class UniversalTableServiceImpl extends ServiceImpl<UniversalTableDao, UniversalTableEntity> implements UniversalTableService {

    @Autowired
    UniversalTableDao universalTableDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UniversalTableEntity> page = this.page(
                new Query<UniversalTableEntity>().getPage(params),
                new QueryWrapper<UniversalTableEntity>()
        );

        return new PageUtils(page);
    }

    public Map<String,Object> selectById(Long id,String tableName,List<String> tableCols)
    {

        return universalTableDao.selectById(id,tableName,tableCols);
    }

    public List<Map<String,Object>> list(String tableName,List<String> tableCols,Integer page,Integer size)
    {
        if (page != null && size != null){
            page = (page - 1) * size;
        }
        return universalTableDao.list(tableName,tableCols,page,size);
    }

    public List<Map<String,Object>> selectByName(String tableName,List<String> tableCols,Integer page,Integer size,String name)
    {
        if (page != null && size != null){
            page = (page - 1) * size;
        }
        return universalTableDao.selectByName(tableName,tableCols,page,size,name);
    }

    public List<Map<String,Object>> selectByNameWithParent(String tableName,List<String> tableCols,Integer page,Integer size,
                                                           String name,Long parentRowId,Long tableId)
    {
        if (page != null && size != null){
            page = (page - 1) * size;
        }
        return universalTableDao.selectByNameWithParent(tableName,tableCols,page,size,name,parentRowId,tableId);
    }

    public Integer getCountByName(String tableName,String name)
    {
        return universalTableDao.getCountByName(tableName,name);
    }

    public Long saveWithCols(String tableName,List<Map<String,Object>> cols,UniversalTableEntity universalTableEntity)
    {
        return universalTableDao.saveWithCols(tableName,cols,universalTableEntity);
    }

    public Integer updateWithCols(String tableName,List<Map<String,Object>> cols,Long id)
    {
        return universalTableDao.updateWithCols(tableName,cols,id);
    }

    public  Integer removeByIdsFromTableName(List<Long> ids,String tableName)
    {
        return universalTableDao.removeByIdsFromTableName(ids,tableName);
    }

    public List<Map<String,Object>> listAllItem(String tableName)
    {
        return universalTableDao.listAllItem(tableName);
    }

    public List<Map<String,Object>> listNotLeafItem(String tableName)
    {
        return universalTableDao.listNotLeafItem(tableName);
    }

    public List<Map<String,Object>> listLeafItem(String tableName,List<String> tableCols)
    {
        return universalTableDao.listLeafItem(tableName,tableCols);
    }

    public List<Map<String, Object>> listBySonIds(String tableName, List<String> tableCols, List<Long> ids, Integer page, Integer size){
        if (page != null && size != null){
            page = (page - 1) * size;
        }
        return universalTableDao.listBySonIds(tableName,tableCols,ids,page,size);
    }

    public boolean updateHbaseTableName(String tableName, Long id, String hbaseTableName){
        return universalTableDao.updateHbaseTableName(tableName,id, hbaseTableName);
    }

    public String selectNameById(String tableName, Long id)
    {
        return universalTableDao.selectNameById(tableName,id);
    }

    public Integer getCount(String tableName)
    {
        return universalTableDao.getCount(tableName);
    }

    public Integer getCountByNameWithParent(String tableName, String name, Long parentRowId, Long tableId)
    {
        return universalTableDao.getCountByNameWithParent(tableName,name,parentRowId,tableId);
    }

    public List<Map<String,Object>> listItemWithLeafInfo(String tableName){
        return universalTableDao.listItemWithLeafInfo(tableName);
    }

    public List<Map<String,Object>> listNotLeafItemInfo(String tableName, List<String> tableCols){
        return universalTableDao.listNotLeafItemInfo(tableName,tableCols);
    }

    public List<Map<String,Object>>  listAllItemInfo(String tableName, List<String> tableCols){
        return universalTableDao.listAllItemInfo(tableName,tableCols);
    }

    public List<Map<String, Object>> listBySonIdsNotLimit(String tableName, List<String> tableCols, List<Long> sonIds){
        return universalTableDao.listBySonIdsNotLimit(tableName,tableCols,sonIds);
    }

}