package com.yihu.wlyy.statistics.etl.role;

import com.yihu.wlyy.statistics.etl.model.ETLModel;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Created by Administrator on 2016.10.11.
 * 二级维度的公共的统计算法
 */
@Component
@Transactional
public class Level3Role {
    public List<Map<String, Map<String, Map<String, List<ETLModel>>>>> elt(List<Map<String, Map<String, List<ETLModel>>>> data) throws  Exception{
        List<Map<String, Map<String, Map<String, List<ETLModel>>>>> returnMapsList=new ArrayList<Map<String, Map<String, Map<String, List<ETLModel>>>>>();

        Map<String, Map<String, Map<String, List<ETLModel>>>> tjCityMap = new HashMap<String, Map<String, Map<String, List<ETLModel>>>>();//市级的统计map key 是区行政区划350200
        Map<String, Map<String, Map<String, List<ETLModel>>>> tjTownMap = new HashMap<String, Map<String, Map<String, List<ETLModel>>>>();//区级的统计map key 是区行政区划350206
        Map<String, Map<String, Map<String, List<ETLModel>>>> tjOrgMap = new HashMap<String, Map<String, Map<String, List<ETLModel>>>>();//机构的统计map key 是机构的code
        Map<String, Map<String, Map<String, List<ETLModel>>>> tjAdminTeamMap = new HashMap<String, Map<String, Map<String, List<ETLModel>>>>();//团队级的统计map 一  key 团队id

        //统计市
        compute(tjCityMap,data.get(3));
        //统计区
        compute(tjTownMap,data.get(2));
        //统计机构
        compute(tjOrgMap,data.get(1));
        //统计团队
        compute(tjAdminTeamMap,data.get(0));


        returnMapsList.add(0,tjAdminTeamMap);
        returnMapsList.add(1,tjOrgMap);
        returnMapsList.add(2,tjTownMap);
        returnMapsList.add(3,tjCityMap);

        return returnMapsList;
    }
    /**
     * 统计算法
     * @param tjMap
     * @param eltDateMap
     */
    private void compute(Map<String, Map<String, Map<String, List<ETLModel>>>> tjMap, Map<String, Map<String, List<ETLModel>>> eltDateMap) throws Exception{
        //遍历数据Map
        for(Map.Entry<String, Map<String, List<ETLModel>>> oneData:eltDateMap.entrySet()){
            Map<String, List<ETLModel>> datasOne=oneData.getValue();//得到数据列表 全部区或者市或者机构或者区的数据
            Map<String, Map<String, List<ETLModel>>> childrens=new HashMap<String, Map<String, List<ETLModel>>>();//返回的map

            for(Map.Entry<String, List<ETLModel>> datasTwo:datasOne.entrySet()){
                 //如果
                 List<ETLModel> datasTwoList=datasTwo.getValue();
                 //做三级维度的数据计算
                 Map<String, List<ETLModel>> newThreeDataMap=new HashMap<String, List<ETLModel>>();
                 for(ETLModel etlModel:datasTwoList){
                      if(newThreeDataMap.containsKey(etlModel.getLevel3Key())){
                          newThreeDataMap.get(etlModel.getLevel3Key()).add(etlModel);
                      }else{
                          List<ETLModel> newThreeDataList=new ArrayList<ETLModel>();
                          newThreeDataList.add(etlModel);
                          newThreeDataMap.put(etlModel.getLevel3Key(),newThreeDataList);
                      }
                 }
                //把三级维度统计好的数据加入到二级维度的Map去
                childrens.put(datasTwo.getKey(),newThreeDataMap);
            }
            //把二级维度的数据加入到一级维度里面去
            tjMap.put(oneData.getKey(),childrens);
        }
    }
}
