package com.gyee.frame.service.StandardPointTable;

import com.gyee.frame.mapper.StandardPointTableMapper.StandardPointTableMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class StandardPointTableService {
    @Resource
    private StandardPointTableMapper standardPointTableMapper;

    //获得数据表名
    public List getTableName(){ return standardPointTableMapper.getTableName(); }

    //获取表的所有列名
    public List<String> getAllColumns(String tableName)
    {
        return standardPointTableMapper.getAllColumns(tableName);
    }

    //联合表
    public List<Map<String, Object>> getDataByColumns(@Param("tableName") String tableName, @Param("columnsName") String columnsName, @Param("excelData")List<Map<String, Object>> excelData) {

        List<Map<String, Object>> dataByColumnsName = standardPointTableMapper.getDataByColumns(tableName,columnsName);//数据库数据

        ArrayList<Map<String,Object>> uniformTable=new ArrayList<>() ;
        Iterator<Map<String,Object>> tableRowIterator=dataByColumnsName.iterator();//数据库表的行
        while (tableRowIterator.hasNext())//根据数据库表的风机导航
        {
            Map<String,Object> tabRow=tableRowIterator.next();
            Iterator<Map<String,Object>> excelRowIterator=excelData.iterator();//excel的行
            while (excelRowIterator.hasNext()) // Excel表的行
            {
                Map<String,Object> newRow=new HashMap<>() ;
                newRow.putAll(excelRowIterator.next()); //
                newRow.putAll(tabRow); // 加上数据库表的行,
                uniformTable.add(newRow);//联合表加上合成后的行
            }

        }
        return uniformTable;
    }

    //将数据存到数据库
    public void saveData(@Param("unionData")List<Map<String, Object>> unionData,@Param("tableName") String tableName){
        Iterator<Map<String,Object>> Data=unionData.iterator();
        while (Data.hasNext()) {
            standardPointTableMapper.saveData(Data.next(), tableName);

        }

    }

}
