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.common.utils.SubString;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.dataSet.DataSetUtils.ToEnglish;
import io.renren.modules.levelManage.dao.UniversalTableDao;
import io.renren.modules.levelManage.entity.LevelEntity;
import io.renren.modules.levelManage.entity.LevelRelationEntity;
import io.renren.modules.levelManage.entity.UniversalTableEntity;
import io.renren.modules.levelManage.service.LevelRelationService;
import io.renren.modules.levelManage.service.LevelService;
import io.renren.modules.levelManage.service.LevelTableService;
import io.renren.modules.levelManage.service.UniversalTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


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

    @Autowired
    UniversalTableDao universalTableDao;

    @Autowired
    LevelRelationService levelRelationService;
    @Autowired
    LevelService levelService;
    @Autowired
    LevelTableService levelTableService;

    @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);
    }

    @Override
    public Map<String, Object> getOtherLeafItem(Long tableId, Long id) {
        //1.获取选中的item的相关子item
        List<LevelRelationEntity> levelRelationEntities = levelRelationService.selectByParent(tableId, id);
        //2.获取子表名
        LevelEntity sonLevel = levelService.getEnabledSon(tableId);
        if(sonLevel==null)
            return null;
        String sonTableName = sonLevel.getTableName();
        //3.在表中获取对应的叶子节点信息
        List<String> tableCols = levelTableService.listColsByLevelId(sonLevel.getId());
        List<Map<String, Object>> candidateItem = listLeafItem(sonTableName, tableCols);
        //4.通过检查levelRelationEntities和candidateItem中的重合部分，获取目标item,为了加速，用了HashMap
        List<Map<String, Object>> data = new ArrayList<>();
        //用一个map装所有的item，之后遍历关系，将其中的item信息取出，发给前端
        HashMap<Object, Object> cItemId_lRelationSonId = new HashMap<>();
        for (int i = 0; i < candidateItem.size(); i++) {
            cItemId_lRelationSonId.put(candidateItem.get(i).get("id"), i);
        }
        for (LevelRelationEntity levelRelation : levelRelationEntities) {
            if (cItemId_lRelationSonId.get(levelRelation.getSonRowId()) != null) {
                int itemId = (int) cItemId_lRelationSonId.get(levelRelation.getSonRowId());
                data.add(candidateItem.get(itemId));
            }
        }
        //data是<列名，具体值>的list
        //对bool值需要修改为是/否，否则前端不显示
        List<String> boolCols = levelTableService.listBoolColsById(sonLevel.getId());
        modifyBool(data, boolCols);
        //去除名字前面的随机数
        SubString.subStringList(data);
        //将测点名称转拼音返回前端
        List<String> pinyin = new ArrayList<>();
        for (Map<String, Object> datum : data) {
            pinyin.add(ToEnglish.getPinYin((String) datum.get("name")));
        }
        HashMap<String,Object> result=new HashMap<>();
        result.put("data",data);
        result.put("levelId", sonLevel.getId());
        result.put("pinyin", pinyin);
        return result;

    }
    private void modifyBool(List<Map<String,Object>> data,List<String> boolCols){
        for(Map<String,Object> map :data)
        {
            for(String col: boolCols)
            {
                Byte a =1;
                if(map.get(col)==a)
                {
                    map.put(col,"是");
                }
                else
                    map.put(col,"否");
            }
        }
    }

}