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

import com.yihu.wlyy.statistics.etl.model.ETLModel;
import org.springframework.context.annotation.Scope;
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.09.
 * 1级维度 机构等级分组
 */
@Component
@Scope("prototype")
public class Level1Role {

    /**
     * 返回的Listmap中 list第一個是 团队 第二个是机构 第三个是区  第四个市
     * @param models
     * @return
     * @throws Exception
     */
    public List<Map<String, List<ETLModel>>> elt(List<ETLModel> models) throws  Exception{
        List<Map<String, List<ETLModel>>> returnMapsList=new ArrayList<Map<String, List<ETLModel>>>();

        Map<String, List<ETLModel>> tjCityMap = new HashMap<String, List<ETLModel>>();//市级的统计map key 是区行政区划350200
        Map<String, List<ETLModel>> tjTownMap = new HashMap<String, List<ETLModel>>();//区级的统计map key 是区行政区划350206
        Map<String, List<ETLModel>> tjOrgMap = new HashMap<String, List<ETLModel>>();//机构的统计map key 是机构的code
        Map<String, List<ETLModel>> tjAdminTeamMap = new HashMap<String, List<ETLModel>>();//团队级的统计map 一  key 团队id
        for(ETLModel etlModel:models){
            //统计市市
            compute(tjCityMap, etlModel.getCity(),etlModel);
            //统计市区
            compute(tjTownMap, etlModel.getTown(),etlModel);
            //统计市机构
            compute(tjOrgMap, etlModel.getHospital(),etlModel);
            //统计团队
            compute(tjAdminTeamMap, etlModel.getAdminTeam(),etlModel);

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

        return returnMapsList;
    }

    /**
     * 统计算法
     * @param eltMap
     * @param key
     * @param etlModel
     */
    private void compute(Map<String, List<ETLModel>> eltMap,String key,ETLModel etlModel) {
        if (eltMap.containsKey(key)) {
            List<ETLModel> etlModels=eltMap.get(key);
            etlModels.add(etlModel);
        } else {
            List<ETLModel> etlModels=new ArrayList<ETLModel>();
            etlModels.add(etlModel);
            eltMap.put(key,etlModels);
        }
    }

    public List<Map<String,List<ETLModel>>> eltNoTeam(List<ETLModel> models) {
        List<Map<String, List<ETLModel>>> returnMapsList=new ArrayList<Map<String, List<ETLModel>>>();

        Map<String, List<ETLModel>> tjCityMap = new HashMap<String, List<ETLModel>>();//市级的统计map key 是区行政区划350200
        Map<String, List<ETLModel>> tjTownMap = new HashMap<String, List<ETLModel>>();//区级的统计map key 是区行政区划350206
        Map<String, List<ETLModel>> tjOrgMap = new HashMap<String, List<ETLModel>>();//机构的统计map key 是机构的code
        if(models.size()>0){
            for(ETLModel etlModel:models){
                //统计市市
                compute(tjCityMap, etlModel.getCity(),etlModel);
                //统计市区
                compute(tjTownMap, etlModel.getTown(),etlModel);
                //统计市机构
                compute(tjOrgMap, etlModel.getHospital(),etlModel);

            }
            returnMapsList.add(0,new HashMap<>());
            returnMapsList.add(1,tjOrgMap);
            returnMapsList.add(2,tjTownMap);
            returnMapsList.add(3,tjCityMap);
        }else{
            returnMapsList.add(0,new HashMap<>());
            returnMapsList.add(1,new HashMap<>());
            returnMapsList.add(2,new HashMap<>());
            returnMapsList.add(3,new HashMap<>());
        }

        return returnMapsList;
    }
}
