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

import com.mysql.jdbc.Blob;
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.QuartzJobLog;
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.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

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

@Component
@Scope("prototype")
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
    @Autowired
    private JdbcTemplate jdbcTemplate;




    /**
     * 保存一级维度的数据
     * @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);
        //保存团队
        saveAll(saveAdminTeamDataLevel1(data.get(0),date,adminTeamMap));
        //保存机构
        saveAll(saveHospitalTeamDataLevel1(data.get(1),date,hospitalsMap));
        //保存区
        saveAll(saveTownTeamDataLevel1(data.get(2),date,townsMap));
        //保存市
        saveAll(saveCityTeamDataLevel1(data.get(3),date,cityMap));
    }

    /**
            * 保存一级维度的数据
    * @param data
    * @param date
    * @param wlyyQuota
    * @throws Exception
    */
    public List<WlyyQuotaResult> getLevel1(List<Map<String, List<ETLModel>>> data,String date,WlyyQuotaVO wlyyQuota) throws Exception {
        List<WlyyQuotaResult> returnList=new ArrayList<WlyyQuotaResult>();
        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);
        //保存团队
        returnList.addAll(saveAdminTeamDataLevel1(data.get(0),date,adminTeamMap));
        //保存机构
        returnList.addAll(saveHospitalTeamDataLevel1(data.get(1),date,hospitalsMap));
        //保存区
        returnList.addAll(saveTownTeamDataLevel1(data.get(2),date,townsMap));
        //保存市
        returnList.addAll(saveCityTeamDataLevel1(data.get(3),date,cityMap));
        return returnList;
    }
    /**
     * 保存二级维度
     * @param data
     * @param day
     * @param wlyyQuota
     * @param foreachNum 标识二级维度循环的次数  例如 年龄就传6  性别就传3
     * @param type 1 性别 2 年龄 3 用户分组 4.签约费用 5 健康分布  6年龄2  7 年龄疾病
     */
    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.签约费用 5 健康分布  6年龄2  7 年龄疾病
     * @param start 起始位置 默认是1
     */
    public List<WlyyQuotaResult> getLevel2(List<Map<String, Map<String, List<ETLModel>>>> data, String day, WlyyQuotaVO wlyyQuota,Integer foreachNum,Integer type,Integer start)throws Exception {
        List<WlyyQuotaResult> returnList=new ArrayList<WlyyQuotaResult>();
        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);
        //保存团队
        returnList.addAll(saveAdminTeamDataLevel2(data.get(0),day,adminTeamMap,foreachNum,type,start));
        //保存机构
        returnList.addAll(saveHospitalTeamDataLevel2(data.get(1),day,hospitalsMap,foreachNum,type,start));
        //保存区
        returnList.addAll(saveTownTeamDataLevel2(data.get(2),day,townsMap,foreachNum,type,start));
        //保存市
        returnList.addAll(saveCityTeamDataLevel2(data.get(3),day,cityMap,foreachNum,type,start));
        return returnList;
    }
    /**
     * 保存二级维度
     * @param data
     * @param day
     * @param wlyyQuota
     * @param foreachNum 如果start是默认的 标识二级维度循环的次数  例如 年龄就传6  性别就传3
     * @param type 1 性别 2 年龄 3 用户分组 4.签约费用 5 健康分布
     * @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 type2 1性别 2年龄 3用户分组 4.签约费用 5健康服务 6年龄2
     * @param type3 1 疾病分组
     */
    public void saveByLevel3(List<Map<String, Map<String, Map<String, List<ETLModel>>>>> data, String day, WlyyQuotaVO wlyyQuota,Integer foreachNum2,Integer foreachNum3,Integer type2,Integer type3) 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,type2,type3,start2,start3);
        //保存机构
        saveHospitalTeamDataLevel3(data.get(1),day,hospitalsMap,foreachNum2,foreachNum3,type2,type3,start2,start3);
        //保存区
        saveTownTeamDataLevel3(data.get(2),day,townsMap,foreachNum2,foreachNum3,type2,type3,start2,start3);
        //保存市
        saveCityTeamDataLevel3(data.get(3),day,cityMap,foreachNum2,foreachNum3,type2,type3,start2,start3);
    }

    /**
     * 保存三级维度
     * @param data
     * @param day
     * @param wlyyQuota
     * @param foreachNum2 标识二级维度循环的次数  例如 年龄就传6  性别就传3
     * @param foreachNum3 标识三级维度循环的次数  例如 年龄就传6  性别就传3
     * @param type2 1 性别 2 年龄 3 用户分组 4.签约费用 5健康服务 6年龄2
     * @param type3 1 疾病类型
     * @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 type2,Integer type3,Integer start2,Integer start3) throws Exception{
        if(start2==null){
            start2=1;
        }
        if(start3==null){
            start2=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,type2,type3,start2,start3);
        //保存机构
        saveHospitalTeamDataLevel3(data.get(1),day,hospitalsMap,foreachNum2,foreachNum3,type2,type3,start2,start3);
        //保存区
        saveTownTeamDataLevel3(data.get(2),day,townsMap,foreachNum2,foreachNum3,type2,type3,start2,start3);
        //保存市
        saveCityTeamDataLevel3(data.get(3),day,cityMap,foreachNum2,foreachNum3,type2,type3,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 type2,Integer type3, Integer start2, Integer start3)throws  Exception {
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        //遍历团队的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=null;
                 if(twoETLMap!=null){
                     threeETLMap=twoETLMap.get(i+"");
                 }
                 for(int j=start3;j<=foreachNum3;j++){
                     int num=0;
                     if(threeETLMap!=null){
                         List<ETLModel> etlModelList= threeETLMap.get(j+"");
                         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 level2Name=getLevel2Name(level2,type2);
                     String level3Name=getLevel3Name(level3,type3);
                     wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,level3,level3Name,day));
                }
            }
        }
        saveAll(wlyyQuotaResults);
    }
    private void saveHospitalTeamDataLevel3(Map<String, Map<String, Map<String, List<ETLModel>>>> etlModelMap, String day, Map<String, Hospital> hospitalsMap, Integer foreachNum2, Integer foreachNum3, Integer type2,Integer type3, Integer start2, Integer start3) throws  Exception{
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        //遍历团队的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;
                    if(threeETLMap!=null&&threeETLMap.size()>0){
                        List<ETLModel> etlModelList= threeETLMap.get(j+"");
                        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="2";
                    String level2=i+"";
                    String level3=j+"";
                    String level2Name=getLevel2Name(level2,type2);
                    String level3Name=getLevel3Name(level3,type3);
                   wlyyQuotaResults.add( save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,level3,level3Name,day));
                }
            }
        }
        saveAll(wlyyQuotaResults);
    }
    private void saveTownTeamDataLevel3(Map<String, Map<String, Map<String, List<ETLModel>>>> etlModelMap, String day, Map<String, Town> townsMap, Integer foreachNum2, Integer foreachNum3, Integer type2,Integer type3,  Integer start2, Integer start3) throws  Exception{
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        //遍历团队的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;
                    if(threeETLMap!=null&&threeETLMap.size()>0){
                        List<ETLModel> etlModelList= threeETLMap.get(j+"");
                        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="3";
                    String level2=i+"";
                    String level3=j+"";
                    String level2Name=getLevel2Name(level2,type2);
                    String level3Name=getLevel3Name(level3,type3);
                   wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,level3,level3Name,day));
                }
            }
        }
        saveAll((wlyyQuotaResults));
    }
    private void saveCityTeamDataLevel3(Map<String, Map<String, Map<String, List<ETLModel>>>> etlModelMap, String day, Map<String, City> cityMap, Integer foreachNum2, Integer foreachNum3, Integer type2,Integer type3,  Integer start2, Integer start3) throws  Exception{
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        //遍历团队的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;
                    if(threeETLMap!=null&&threeETLMap.size()>0){
                        List<ETLModel> etlModelList= threeETLMap.get(j+"");
                        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="4";
                    String level2=i+"";
                    String level3=j+"";
                    String level2Name=getLevel2Name(level2,type2);
                    String level3Name=getLevel3Name(level3,type3);
                    wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,level3,level3Name,day));
                }
            }
        }
        saveAll(wlyyQuotaResults);
    }

    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);
        //保存团队
        saveAll( saveAdminTeamDataLevel2(data.get(0),day,adminTeamMap,foreachNum,type,start));
        //保存机构
        saveAll( saveHospitalTeamDataLevel2(data.get(1),day,hospitalsMap,foreachNum,type,start));
        //保存区
        saveAll( saveTownTeamDataLevel2(data.get(2),day,townsMap,foreachNum,type,start));
        //保存市
        saveAll( saveCityTeamDataLevel2(data.get(3),day,cityMap,foreachNum,type,start));
    }

    private List<WlyyQuotaResult> saveCityTeamDataLevel2(Map<String, Map<String, List<ETLModel>>> etlModelMap, String day, Map<String, City> citysMap ,Integer foreachNum,Integer type,Integer start) throws  Exception{
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
         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);
                 wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,null,null,day));
             }
         }
        return wlyyQuotaResults;
    }

    private List<WlyyQuotaResult> saveTownTeamDataLevel2(Map<String, Map<String, List<ETLModel>>> etlModelMap, String day, Map<String, Town> townsMap ,Integer foreachNum,Integer type,Integer start)throws  Exception {
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        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);
                wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,null,null,day));
            }
        }
        return wlyyQuotaResults;
    }

    private List<WlyyQuotaResult> saveHospitalTeamDataLevel2(Map<String, Map<String, List<ETLModel>>> etlModelMap, String day, Map<String, Hospital> hospitalsMap,Integer foreachNum,Integer type,Integer start)throws  Exception {
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        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);
                wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,null,null,day));
            }
        }
       return wlyyQuotaResults;
    }

    private List<WlyyQuotaResult> saveAdminTeamDataLevel2(Map<String, Map<String, List<ETLModel>>> etlModelMap, String day, Map<String, AdminTeam> adminTeamMap,Integer foreachNum,Integer type,Integer start)throws  Exception {
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        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);
                wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,level2,level2Name,null,null,day));
            }
        }
       return wlyyQuotaResults;
    }

    private  List<WlyyQuotaResult>  saveAdminTeamDataLevel1(Map<String, List<ETLModel>> mapData,String date,Map<String, AdminTeam> adminTeamMap)throws  Exception {
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        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";
            wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,null,null,null,null,date));
        }
       return wlyyQuotaResults;
    }

    private  List<WlyyQuotaResult>  saveHospitalTeamDataLevel1(Map<String, List<ETLModel>> mapData, String date,  Map<String, Hospital> hospitalsMap )throws  Exception {
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        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";
            wlyyQuotaResults.add( save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,null,null,null,null,date));
        }
        return wlyyQuotaResults;
    }

    private  List<WlyyQuotaResult>  saveTownTeamDataLevel1(Map<String, List<ETLModel>> mapData, String date,  Map<String, Town> townsMap)throws  Exception {
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        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";
            wlyyQuotaResults.add(save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,null,null,null,null,date));
        }
        return wlyyQuotaResults;
    }

    private  List<WlyyQuotaResult>  saveCityTeamDataLevel1(Map<String, List<ETLModel>> mapData, String date, Map<String, City> cityMap) throws  Exception{
        List<WlyyQuotaResult> wlyyQuotaResults=new ArrayList<WlyyQuotaResult>();
        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";
           wlyyQuotaResults.add( save(num, city, cityName, town, townName, org, orgName, doctorCode, doctorName, doctorJob, level,null,null,null,null,date));
        }
        return wlyyQuotaResults;
    }

    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 WlyyQuotaResult 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.setId(UUID.randomUUID().toString().replace("-",""));
        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+"");
        return wlyyQuotaResult;
    }

    @Async("dbStorageExecutor")
    @Transactional
    private void saveAll(List<WlyyQuotaResult> wlyyQuotaResults) throws  Exception{
        wlyyQuotaResultDao.save(wlyyQuotaResults);
    }

    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);
            }
            case  5:{
                //健康分布
                return Constant.getlevelHealthFbName(i);
            }

            case  6:{
                //年龄2
                return Constant.getLevelAge2(i);
            }
            case  7:{
                //年龄疾病
                return Constant.getLevelAgeDiseaseName(i);
            }
            default:{
                return "";
            }
        }
    }

    private String getLevel3Name(String j, Integer type) {
        switch (type){
            case  1:{
                //疾病类型
                return Constant.getLevelDiseaseName(j);
            }
            default:{
                return "";
            }
        }
    }
    @Transactional
    public void saveLog(final QuartzJobLog quartzJobLog){
       quartzJobLog.setId(UUID.randomUUID().toString().replace("-",""));
       String sql="INSERT INTO wlyy_job_log (id, job_start_time, Job_end_time, job_id, job_content, job_type, job_name) VALUES (?,?,?,?,?,?,?);";
        jdbcTemplate.update(connection -> {
            PreparedStatement statementCreator= connection.prepareCall(sql);
            statementCreator.setString(1,quartzJobLog.getId());
            statementCreator.setTimestamp(2,new Timestamp(quartzJobLog.getJobStartTime().getTime()));
            statementCreator.setTimestamp(3,new Timestamp(quartzJobLog.getJobEndTime().getTime()));
            statementCreator.setString(4,quartzJobLog.getJobId());
            statementCreator.setString(5,quartzJobLog.getJobContent());

            statementCreator.setString(6,quartzJobLog.getJobType());
            statementCreator.setString(7,quartzJobLog.getJobName());
            return statementCreator;
        });
    }

    public void saveByLevel1NoTeam(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);
        //保存机构
        saveAll(saveHospitalTeamDataLevel1(data.get(1),date,hospitalsMap));
        //保存区
        saveAll(saveTownTeamDataLevel1(data.get(2),date,townsMap));
        //保存市
        saveAll(saveCityTeamDataLevel1(data.get(3),date,cityMap));
    }
}
