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

import com.yihu.wlyy.statistics.dao.DoctorAdminTeamDao;
import com.yihu.wlyy.statistics.dao.HospitalDao;
import com.yihu.wlyy.statistics.dao.TownDao;
import com.yihu.wlyy.statistics.dao.WlyyQuotaResultDao;
import com.yihu.wlyy.statistics.etl.model.ETLModel;
import com.yihu.wlyy.statistics.job.business.Constant;
import com.yihu.wlyy.statistics.model.hosptial.Hospital;
import com.yihu.wlyy.statistics.model.job.WlyyQuotaResult;
import com.yihu.wlyy.statistics.model.system.City;
import com.yihu.wlyy.statistics.model.system.Town;
import com.yihu.wlyy.statistics.model.team.AdminTeam;
import com.yihu.wlyy.statistics.vo.WlyyQuotaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Created by Administrator on 2016.10.09.
 * 统计数据数据库存储器
 */

@Component
@Transactional
public class DBStorage   {
    private WlyyQuotaVO wlyyQuota;//指标对象
    private Map<String, AdminTeam> adminTeamMap = new HashMap<String, AdminTeam>();
    private Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
    private Map<String, Town> townsMap = new HashMap<String, Town>();
    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private TownDao townDao;
    @Autowired
    private DoctorAdminTeamDao doctorAdminTeamDao;
    @Autowired
    private WlyyQuotaResultDao wlyyQuotaResultDao;//指标结果Dao

    /**
     * 保存一级维度的数据
     * @param data
     * @param date
     * @param wlyyQuota
     * @throws Exception
     */
    public void saveByLevel1(List<Map<String, List<ETLModel>>> data,String date,WlyyQuotaVO wlyyQuota) throws Exception {
        this.wlyyQuota=wlyyQuota;
        List<AdminTeam> adminTeams=doctorAdminTeamDao.findAllTeam();
        for (AdminTeam adminTeam : adminTeams) {
            adminTeamMap.put(adminTeam.getId()+"", adminTeam);
        }
        //查找出系统全部的机构
        List<Hospital> hospitals = hospitalDao.findHospitalzxFWZ();
        for (Hospital hospital : hospitals) {
            hospitalsMap.put(hospital.getCode(), hospital);
        }
        //查找出厦门市全部的区
        List<Town> towns = townDao.findByCityCode(Constant.city);
        for (Town town : towns) {
            townsMap.put(town.getCode(), town);
        }
        //全部的市
        Map<String, City> cityMap = new HashMap<String, City>();
        City city=new City();
        city.setCode(Constant.city);
        city.setName(Constant.cityName);
        cityMap.put(Constant.city,city);
        //保存团队
        saveAdminTeamDataLevel1(data.get(0),date,adminTeamMap);
        //保存机构
        saveHospitalTeamDataLevel1(data.get(1),date,hospitalsMap);
        //保存区
        saveTownTeamDataLevel1(data.get(2),date,townsMap);
        //保存市
        saveCityTeamDataLevel1(data.get(3),date,cityMap);
    }

    /**
     * 保存二级维度
     * @param data
     * @param day
     * @param wlyyQuota
     * @param foreachNum 标识二级维度循环的次数  例如 年龄就传6  性别就传3
     * @param type 1 性别 2 年龄 3 用户分组 4.签约费用
     */
    public void saveByLevel2(List<Map<String, Map<String, List<ETLModel>>>> data, String day, WlyyQuotaVO wlyyQuota,Integer foreachNum,Integer type) throws Exception{
        Integer start=1;
        this.wlyyQuota=wlyyQuota;
        level2Public(data, day, foreachNum, type, start);
    }
    /**
     * 保存二级维度
     * @param data
     * @param day
     * @param wlyyQuota
     * @param foreachNum 如果start是默认的 标识二级维度循环的次数  例如 年龄就传6  性别就传3
     * @param type 1 性别 2 年龄 3 用户分组 4.签约费用
     * @param start 起始位置 默认是1
     */
    public void saveByLevel2(List<Map<String, Map<String, List<ETLModel>>>> data, String day, WlyyQuotaVO wlyyQuota,Integer foreachNum,Integer type,Integer start)throws Exception {
        this.wlyyQuota=wlyyQuota;
        level2Public(data, day, foreachNum, type, start);
    }
    /**
     * 保存三级维度
     * @param data
     * @param day
     * @param wlyyQuota
     * @param foreachNum2 标识二级维度循环的次数  例如 年龄就传6  性别就传3
     * @param foreachNum3 标识三级维度循环的次数  例如 年龄就传6  性别就传3
     * @param type 1 性别 2 年龄 3 用户分组 4.签约费用
     */
    public void saveByLevel3(List<Map<String, Map<String, Map<String, List<ETLModel>>>>> data, String day, WlyyQuotaVO wlyyQuota,Integer foreachNum2,Integer foreachNum3,Integer type) throws Exception{
        Integer start2=1;
        Integer start3=1;
        this.wlyyQuota=wlyyQuota;
        List<AdminTeam> adminTeams=doctorAdminTeamDao.findAllTeam();
        for (AdminTeam adminTeam : adminTeams) {
            adminTeamMap.put(adminTeam.getId()+"", adminTeam);
        }
        //查找出系统全部的机构
        List<Hospital> hospitals = hospitalDao.findHospitalzxFWZ();
        for (Hospital hospital : hospitals) {
            hospitalsMap.put(hospital.getCode(), hospital);
        }
        //查找出厦门市全部的区
        List<Town> towns = townDao.findByCityCode(Constant.city);
        for (Town town : towns) {
            townsMap.put(town.getCode(), town);
        }
        //全部的市
        City city=new City();
        city.setCode(Constant.city);
        city.setName(Constant.cityName);
        Map<String, City> cityMap = new HashMap<String, City>();
        cityMap.put(Constant.city,city);
        //保存团队
        saveAdminTeamDataLevel3(data.get(0),day,adminTeamMap,foreachNum2,foreachNum3,type,start2,start3);
        //保存机构
        saveHospitalTeamDataLevel3(data.get(1),day,hospitalsMap,foreachNum2,foreachNum3,type,start2,start3);
        //保存区
        saveTownTeamDataLevel3(data.get(2),day,townsMap,foreachNum2,foreachNum3,type,start2,start3);
        //保存市
        saveCityTeamDataLevel3(data.get(3),day,cityMap,foreachNum2,foreachNum3,type,start2,start3);
    }

    /**
     * 保存三级维度
     * @param data
     * @param day
     * @param wlyyQuota
     * @param foreachNum2 标识二级维度循环的次数  例如 年龄就传6  性别就传3
     * @param foreachNum3 标识三级维度循环的次数  例如 年龄就传6  性别就传3
     * @param type 1 性别 2 年龄 3 用户分组 4.签约费用
     * @param start2 耳機維度起始位置 默认是1
     * @param start3 耳機維度起始位置 默认是1
     */
    public void saveByLevel3(List<Map<String, Map<String, Map<String, List<ETLModel>>>>> data, String day, WlyyQuotaVO wlyyQuota,Integer foreachNum2,Integer foreachNum3,Integer type,Integer start2,Integer start3) throws Exception{
        if(start2==null){
            start2=1;
        }
        if(start3==null){
            start2=3;
        }
        this.wlyyQuota=wlyyQuota;
        List<AdminTeam> adminTeams=doctorAdminTeamDao.findAllTeam();
        for (AdminTeam adminTeam : adminTeams) {
            adminTeamMap.put(adminTeam.getId()+"", adminTeam);
        }
        //查找出系统全部的机构
        List<Hospital> hospitals = hospitalDao.findHospitalzxFWZ();
        for (Hospital hospital : hospitals) {
            hospitalsMap.put(hospital.getCode(), hospital);
        }
        //查找出厦门市全部的区
        List<Town> towns = townDao.findByCityCode(Constant.city);
        for (Town town : towns) {
            townsMap.put(town.getCode(), town);
        }
        //全部的市
        City city=new City();
        city.setCode(Constant.city);
        city.setName(Constant.cityName);
        Map<String, City> cityMap = new HashMap<String, City>();
        cityMap.put(Constant.city,city);
        //保存团队
        saveAdminTeamDataLevel3(data.get(0),day,adminTeamMap,foreachNum2,foreachNum3,type,start2,start3);
        //保存机构
        saveHospitalTeamDataLevel3(data.get(1),day,hospitalsMap,foreachNum2,foreachNum3,type,start2,start3);
        //保存区
        saveTownTeamDataLevel3(data.get(2),day,townsMap,foreachNum2,foreachNum3,type,start2,start3);
        //保存市
        saveCityTeamDataLevel3(data.get(3),day,cityMap,foreachNum2,foreachNum3,type,start2,start3);
    }



    private void saveAdminTeamDataLevel3(Map<String, Map<String, Map<String, List<ETLModel>>>> etlModelMap, String day, Map<String, AdminTeam> adminTeamMap, Integer foreachNum2, Integer foreachNum3, Integer type, Integer start2, Integer start3) {
        //遍历团队的Map
        for(Map.Entry<String,AdminTeam> oneAdminTeam:adminTeamMap.entrySet()){
            //得到每个团队的2级维度
            Map<String, Map<String, List<ETLModel>>> twoETLMap= etlModelMap.get(oneAdminTeam.getKey());
            for(int i=start2;i<=foreachNum2;i++){
                 //得到每个团队的三级维度
                 Map<String, List<ETLModel>> threeETLMap= new HashMap<String, List<ETLModel>>();
                 if(twoETLMap!=null){
                     threeETLMap=twoETLMap.get(i);
                 }
                 for(int j=start3;j<=foreachNum3;j++){
                     int num=0;
                     List<ETLModel> etlModelList= threeETLMap.get(i);
                     if(etlModelList!=null&&etlModelList.size()>0){
                         num=etlModelList.size();
                     }
                     AdminTeam adminTeamObj = adminTeamMap.get(oneAdminTeam.getKey());//团队
                     if(adminTeamObj==null)continue;
                     String orgCodeTemp=getOrg(adminTeamObj.getOrgCode());
                     Hospital hospital = hospitalsMap.get(orgCodeTemp);
                     if(hospital==null)continue;
                     String city=Constant.city;
                     String cityName=Constant.cityName;
                     String town=hospital.getTown();
                     String townName=hospital.getTownName();
                     String org=hospital.getCode();
                     String orgName=hospital.getName();
                     String doctorCode=adminTeamObj.getId()+"";
                     String doctorName=adminTeamObj.getName();
                     String doctorJob="";
                     String level="1";
                     String level2=i+"";
                     String level3=j+"";
                     String level3Name=getLevel3Name(level3,type);
                     save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,null,level3,level3Name,day);
                }
            }
        }
    }
    private void saveHospitalTeamDataLevel3(Map<String, Map<String, Map<String, List<ETLModel>>>> etlModelMap, String day, Map<String, Hospital> hospitalsMap, Integer foreachNum2, Integer foreachNum3, Integer type, Integer start2, Integer start3) {
        //遍历团队的Map
        for(Map.Entry<String,Hospital> oneHospital:hospitalsMap.entrySet()){
            //得到每个团队的2级维度
            Map<String, Map<String, List<ETLModel>>> twoETLMap= etlModelMap.get(oneHospital.getKey());
            for(int i=start2;i<=foreachNum2;i++){
                //得到每个团队的三级维度
                Map<String, List<ETLModel>> threeETLMap= new HashMap<String, List<ETLModel>>();
                if(twoETLMap!=null){
                    threeETLMap=twoETLMap.get(i);
                }
                for(int j=start3;j<=foreachNum3;j++){
                    int num=0;
                    List<ETLModel> etlModelList= threeETLMap.get(i);
                    if(etlModelList!=null&&etlModelList.size()>0){
                        num=etlModelList.size();
                    }
                    Hospital hospital = hospitalsMap.get(oneHospital.getKey());
                    if(hospital==null)continue;
                    String city=Constant.city;
                    String cityName=Constant.cityName;
                    String town=hospital.getTown();
                    String townName=hospital.getTownName();
                    String org=hospital.getCode();
                    String orgName=hospital.getName();
                    String doctorCode="";
                    String doctorName="";
                    String doctorJob="";
                    String level="1";
                    String level2=i+"";
                    String level3=j+"";
                    String level3Name=getLevel3Name(level3,type);
                    save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,null,level3,level3Name,day);
                }
            }
        }
    }
    private void saveTownTeamDataLevel3(Map<String, Map<String, Map<String, List<ETLModel>>>> etlModelMap, String day, Map<String, Town> townsMap, Integer foreachNum2, Integer foreachNum3, Integer type, Integer start2, Integer start3) {
        //遍历团队的Map
        for(Map.Entry<String,Town> oneTownTeam:townsMap.entrySet()){
            //得到每个团队的2级维度
            Map<String, Map<String, List<ETLModel>>> twoETLMap= etlModelMap.get(oneTownTeam.getKey());
            for(int i=start2;i<=foreachNum2;i++){
                //得到每个团队的三级维度
                Map<String, List<ETLModel>> threeETLMap= new HashMap<String, List<ETLModel>>();
                if(twoETLMap!=null){
                    threeETLMap=twoETLMap.get(i);
                }
                for(int j=start3;j<=foreachNum3;j++){
                    int num=0;
                    List<ETLModel> etlModelList= threeETLMap.get(i);
                    if(etlModelList!=null&&etlModelList.size()>0){
                        num=etlModelList.size();
                    }
                    Town townObj=townsMap.get(oneTownTeam.getKey());
                    if(townObj==null)continue;
                    String city=Constant.city;
                    String cityName=Constant.cityName;
                    String town=townObj.getCode();
                    String townName=townObj.getName();
                    String org="";
                    String orgName="";
                    String doctorCode="";
                    String doctorName="";
                    String doctorJob="";
                    String level="1";
                    String level2=i+"";
                    String level3=j+"";
                    String level3Name=getLevel3Name(level3,type);
                    save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,null,level3,level3Name,day);
                }
            }
        }
    }
    private void saveCityTeamDataLevel3(Map<String, Map<String, Map<String, List<ETLModel>>>> etlModelMap, String day, Map<String, City> cityMap, Integer foreachNum2, Integer foreachNum3, Integer type, Integer start2, Integer start3) {
        //遍历团队的Map
        for(Map.Entry<String,City> oneCityTeam:cityMap.entrySet()){
            //得到每个团队的2级维度
            Map<String, Map<String, List<ETLModel>>> twoETLMap= etlModelMap.get(oneCityTeam.getKey());
            for(int i=start2;i<=foreachNum2;i++){
                //得到每个团队的三级维度
                Map<String, List<ETLModel>> threeETLMap= new HashMap<String, List<ETLModel>>();
                if(twoETLMap!=null){
                    threeETLMap=twoETLMap.get(i);
                }
                for(int j=start3;j<=foreachNum3;j++){
                    int num=0;
                    List<ETLModel> etlModelList= threeETLMap.get(i);
                    if(etlModelList!=null&&etlModelList.size()>0){
                        num=etlModelList.size();
                    }
                    String city=Constant.city;
                    String cityName=Constant.cityName;
                    String town="";
                    String townName="";
                    String org="";
                    String orgName="";
                    String doctorCode="";
                    String doctorName="";
                    String doctorJob="";
                    String level="1";
                    String level2=i+"";
                    String level3=j+"";
                    String level3Name=getLevel3Name(level3,type);
                    save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,null,level3,level3Name,day);
                }
            }
        }
    }

    private void level2Public(List<Map<String, Map<String, List<ETLModel>>>> data, String day, Integer foreachNum, Integer type, Integer start)throws Exception {
        List<AdminTeam> adminTeams=doctorAdminTeamDao.findAllTeam();
        for (AdminTeam adminTeam : adminTeams) {
            adminTeamMap.put(adminTeam.getId()+"", adminTeam);
        }
        //查找出系统全部的机构
        List<Hospital> hospitals = hospitalDao.findHospitalzxFWZ();
        for (Hospital hospital : hospitals) {
            hospitalsMap.put(hospital.getCode(), hospital);
        }
        //查找出厦门市全部的区
        List<Town> towns = townDao.findByCityCode(Constant.city);
        for (Town town : towns) {
            townsMap.put(town.getCode(), town);
        }
        //全部的市
        City city=new City();
        city.setCode(Constant.city);
        city.setName(Constant.cityName);
        Map<String, City> cityMap = new HashMap<String, City>();
        cityMap.put(Constant.city,city);
        //保存团队
        saveAdminTeamDataLevel2(data.get(0),day,adminTeamMap,foreachNum,type,start);
        //保存机构
        saveHospitalTeamDataLevel2(data.get(1),day,hospitalsMap,foreachNum,type,start);
        //保存区
        saveTownTeamDataLevel2(data.get(2),day,townsMap,foreachNum,type,start);
        //保存市
        saveCityTeamDataLevel2(data.get(3),day,cityMap,foreachNum,type,start);
    }

    private void saveCityTeamDataLevel2(Map<String, Map<String, List<ETLModel>>> etlModelMap, String day, Map<String, City> citysMap ,Integer foreachNum,Integer type,Integer start) {
         for(Map.Entry<String,City> oneCity:citysMap.entrySet()){
             Map<String, List<ETLModel>> oneELTMap= etlModelMap.get(oneCity.getKey());
             for(int i=start;i<=foreachNum;i++){
                 int num=0;
                 if(oneELTMap!=null){
                     List<ETLModel> etlModel= oneELTMap.get(i+"");
                     if(etlModel!=null&&etlModel.size()>0){
                         num=etlModel.size();
                     }
                 }
                 City cityObj=oneCity.getValue();
                 String city=cityObj.getCode();
                 String cityName=cityObj.getName();
                 String town="";
                 String townName="";
                 String org="";
                 String orgName="";
                 String doctorCode="";
                 String doctorName="";
                 String doctorJob="";
                 String level="4";
                 String level2=i+"";
                 String level2Name=getLevel2Name(level2,type);
                 save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,null,null,day);
             }
         }
    }

    private void saveTownTeamDataLevel2(Map<String, Map<String, List<ETLModel>>> etlModelMap, String day, Map<String, Town> townsMap ,Integer foreachNum,Integer type,Integer start) {
        for(Map.Entry<String,Town> oneTown:townsMap.entrySet()){
            Map<String, List<ETLModel>> oneELTMap= etlModelMap.get(oneTown.getKey());
            for(int i=start;i<=foreachNum;i++){
                int num=0;
                if(oneELTMap!=null){
                    List<ETLModel> etlModel= oneELTMap.get(i+"");
                    if(etlModel!=null&&etlModel.size()>0){
                        num=etlModel.size();
                    }
                }
                Town townObj=oneTown.getValue();
                String city=Constant.city;
                String cityName=Constant.cityName;
                String town=townObj.getCode();
                String townName=townObj.getName();
                String org="";
                String orgName="";
                String doctorCode="";
                String doctorName="";
                String doctorJob="";
                String level="3";
                String level2=i+"";
                String level2Name=getLevel2Name(level2,type);
                save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,null,null,day);
            }
        }
    }

    private void saveHospitalTeamDataLevel2(Map<String, Map<String, List<ETLModel>>> etlModelMap, String day, Map<String, Hospital> hospitalsMap,Integer foreachNum,Integer type,Integer start) {
        for(Map.Entry<String,Hospital> oneHospital:hospitalsMap.entrySet()){
            Map<String, List<ETLModel>> oneELTMap= etlModelMap.get(oneHospital.getKey());
            for(int i=start;i<=foreachNum;i++){
                int num=0;
                if(oneELTMap!=null){
                    List<ETLModel> etlModel= oneELTMap.get(i+"");
                    if(etlModel!=null&&etlModel.size()>0){
                        num=etlModel.size();
                    }
                }
                Hospital hospital = oneHospital.getValue();//得到全科医生的机构
                if(hospital==null)continue;
                String city=Constant.city;
                String cityName=Constant.cityName;
                String town=hospital.getTown();
                String townName=hospital.getTownName();
                String org=getOrg(hospital.getCode());
                String orgName=hospital.getName();
                String doctorCode="";
                String doctorName="";
                String doctorJob="";
                String level="2";
                String level2=i+"";
                String level2Name=getLevel2Name(level2,type);
                save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,null,null,day);
            }
        }
    }

    private void saveAdminTeamDataLevel2(Map<String, Map<String, List<ETLModel>>> etlModelMap, String day, Map<String, AdminTeam> adminTeamMap,Integer foreachNum,Integer type,Integer start) {
        for(Map.Entry<String,AdminTeam> adminTeam:adminTeamMap.entrySet()){
            Map<String, List<ETLModel>> oneELTMap= etlModelMap.get(adminTeam.getKey());
            for(int i=start;i<=foreachNum;i++){
                int num=0;
                if(oneELTMap!=null){
                    List<ETLModel> etlModel= oneELTMap.get(i+"");
                    if(etlModel!=null&&etlModel.size()>0){
                        num=etlModel.size();
                    }
                }
                AdminTeam adminTeamObj = adminTeamMap.get(adminTeam.getKey());//团队
                if(adminTeamObj==null)continue;
                String orgCodeTemp=getOrg(adminTeamObj.getOrgCode());
                Hospital hospital = hospitalsMap.get(orgCodeTemp);
                if(hospital==null)continue;
                String city=Constant.city;
                String cityName=Constant.cityName;
                String town=hospital.getTown();
                String townName=hospital.getTownName();
                String org=hospital.getCode();
                String orgName=hospital.getName();
                String doctorCode=adminTeamObj.getId()+"";
                String doctorName=adminTeamObj.getName();
                String doctorJob="";
                String level="1";
                String level2=i+"";
                String level2Name=getLevel2Name(level2,type);
                save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,null,null,day);
            }
        }
    }

    private void saveAdminTeamDataLevel1(Map<String, List<ETLModel>> mapData,String date,Map<String, AdminTeam> adminTeamMap) {
        for(Map.Entry<String, AdminTeam> entry:adminTeamMap.entrySet()){
            List<ETLModel> etlModel= mapData.get(entry.getKey());
            int num=0;
            if(etlModel!=null&&etlModel.size()>0){
                num=etlModel.size();
            }
            AdminTeam adminTeam = adminTeamMap.get(entry.getKey());//得到全科医生
            if(adminTeam==null)continue;
            String orgCodeTemp=getOrg(adminTeam.getOrgCode());
            Hospital hospital = hospitalsMap.get(orgCodeTemp);//得到全科医生的机构
            if(hospital==null)continue;
            String city=Constant.city;
            String cityName=Constant.cityName;
            String town=hospital.getTown();
            String townName=hospital.getTownName();
            String org=getOrg(hospital.getCode());
            String orgName=hospital.getName();
            String doctorCode=adminTeam.getId()+"";
            String doctorName=adminTeam.getName();
            String doctorJob="";
            String level="1";
            save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,null,null,null,null,date);
        }
    }

    private void saveHospitalTeamDataLevel1(Map<String, List<ETLModel>> mapData, String date,  Map<String, Hospital> hospitalsMap ) {
        for(Map.Entry<String, Hospital> entry:hospitalsMap.entrySet()){
            List<ETLModel> etlModel= mapData.get(entry.getKey());
            int num=0;
            if(etlModel!=null&&etlModel.size()>0){
                num=etlModel.size();
            }
            Hospital hospital = entry.getValue();//得到全科医生的机构
            String city=Constant.city;
            String cityName=Constant.cityName;
            if(hospital==null)continue;
            String town=hospital.getTown();
            String townName=hospital.getTownName();
            String org=getOrg(hospital.getCode());
            String orgName=hospital.getName();
            String doctorCode="";
            String doctorName="";
            String doctorJob="";
            String level="2";
            save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,null,null,null,null,date);
        }
    }

    private void saveTownTeamDataLevel1(Map<String, List<ETLModel>> mapData, String date,  Map<String, Town> townsMap) {
        for(Map.Entry<String, Town> entry:townsMap.entrySet()){
            List<ETLModel> etlModel= mapData.get(entry.getKey());
            int num=0;
            if(etlModel!=null&&etlModel.size()>0){
                num=etlModel.size();
            }
            Town townObj=entry.getValue();
            String city=Constant.city;
            String cityName=Constant.cityName;
            String town=townObj.getCode();
            String townName=townObj.getName();
            String org="";
            String orgName="";
            String doctorCode="";
            String doctorName="";
            String doctorJob="";
            String level="3";
            save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,null,null,null,null,date);
        }
    }

    private void saveCityTeamDataLevel1(Map<String, List<ETLModel>> mapData, String date, Map<String, City> cityMap) {
        for(Map.Entry<String, City> entry:cityMap.entrySet()){
            List<ETLModel> etlModel= mapData.get(entry.getKey());
            int num=0;
            if(etlModel!=null&&etlModel.size()>0){
                num=etlModel.size();
            }
            City cityObj=entry.getValue();
            String city=cityObj.getCode();
            String cityName=cityObj.getName();
            String town="";
            String townName="";
            String org="";
            String orgName="";
            String doctorCode="";
            String doctorName="";
            String doctorJob="";
            String level="4";
            save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,null,null,null,null,date);
        }
    }

    private String getOrg(String org){
        if(!"00".equals(org.substring(org.length()-2,org.length()))){
            return org.substring(0,org.length()-2)+"00";
        }else{
            return org;
        }
    }

    /**
     * 保存方案
     * @param city
     * @param cityName
     * @param town
     * @param townName
     * @param org
     * @param orgName
     * @param doctorCode
     * @param doctorName
     * @param doctorJob
     * @param level1
     */
    private void save(int num,  String city, String cityName, String town, String townName, String org, String orgName, String doctorCode, String doctorName, String doctorJob, String level1, String level2, String level2Name,String level3, String level3Name,String date) {
        WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
        wlyyQuotaResult.setDel("1");
        wlyyQuotaResult.setOrgCode(org);
        wlyyQuotaResult.setOrgName(orgName);
        wlyyQuotaResult.setCity(city);
        wlyyQuotaResult.setCityName(cityName);
        wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
        wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
        wlyyQuotaResult.setQuotaDate(date);
        wlyyQuotaResult.setCreateTime(new Date());
        wlyyQuotaResult.setQkdoctorJobName(doctorJob);
        wlyyQuotaResult.setQkdoctorName(doctorName);
        wlyyQuotaResult.setQkdoctorCode(doctorCode);
        wlyyQuotaResult.setTown(town);
        wlyyQuotaResult.setTownName(townName);
        wlyyQuotaResult.setLevel1Type(level1);
        wlyyQuotaResult.setLevel2Type(level2);
        wlyyQuotaResult.setLevel2TypeName(level2Name);
        wlyyQuotaResult.setLevel3Type(level3);
        wlyyQuotaResult.setLevel3TypeName(level3Name);
        //判断全科医生是否有改签量
        wlyyQuotaResult.setResult(num+"");
        wlyyQuotaResultDao.save(wlyyQuotaResult);
    }

    private String getLevel2Name(String i, Integer type) {
        switch (type){
            case  1:{
                //性别
                return Constant.getLevelSexName(i);
            }
            case  2:{
                //年龄
                return Constant.getLevelAgeName(i);
            }
            case  3:{
                //用户分组
                return Constant.getLevelGroupName(i);
            }
            case  4:{
                //费用类别
                return Constant.getLevelExpenseName(i);
            }
            default:{
                return "";
            }
        }
    }

    private String getLevel3Name(String j, Integer type) {
        switch (type){
            case  1:{
                //签约费用
                return Constant.getLevelExpenseName(j);
            }
            default:{
                return "";
            }
        }
    }
}
