package com.yihu.wlyy.job.elt.storage;

import com.yihu.wlyy.entity.address.City;
import com.yihu.wlyy.entity.address.Town;
import com.yihu.wlyy.entity.doctor.team.admin.AdminTeam;
import com.yihu.wlyy.entity.organization.Hospital;
import com.yihu.wlyy.job.Constant;
import com.yihu.wlyy.job.elt.base.ETLModel;
import com.yihu.wlyy.repository.address.TownDao;
import com.yihu.wlyy.repository.doctor.DoctorAdminTeamDao;
import com.yihu.wlyy.repository.organization.HospitalDao;
import com.yihu.wlyy.repository.statistics.WlyyQuotaResultDao;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.quota.WlyyQuotaVO;
import org.apache.axis.utils.Admin;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

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 RedisStorage {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private TownDao townDao;
    @Autowired
    private DoctorAdminTeamDao doctorAdminTeamDao;
    /**
     * 保存一级维度的数据
     * @param data
     * @param wlyyQuotaId
     * @throws Exception
     */
    public void saveByLevel1(List<Map<String, List<ETLModel>>> data,String wlyyQuotaId) throws Exception {
        List<AdminTeam> adminTeams=doctorAdminTeamDao.findAllTeam();
        Map<String, AdminTeam> adminTeamMap = new HashMap<String, AdminTeam>();
        for (AdminTeam adminTeam : adminTeams) {
            adminTeamMap.put(adminTeam.getId()+"", adminTeam);
        }
        //查找出系统全部的机构
        List<Hospital> hospitals = hospitalDao.findHospitalzxFWZ();
         Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
        for (Hospital hospital : hospitals) {
            hospitalsMap.put(hospital.getCode(), hospital);
        }
        //查找出厦门市全部的区
        List<Town> towns = townDao.findByCityCode(Constant.city);
        Map<String, Town> townsMap = new HashMap<String, Town>();
        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);
        
        Map<String, List<ETLModel>> adminTeam=data.get(0);//团队的数据
        Map<String, List<ETLModel>> orgTeam=data.get(1);//机构的数据
        Map<String, List<ETLModel>> townTeam=data.get(2);//区的数据
        Map<String, List<ETLModel>> cityTeam=data.get(3);//市的数据
        //保存市
        saveLevel1City(wlyyQuotaId, cityMap, cityTeam);
        //保存区
        saveLevel1Town(wlyyQuotaId, townsMap, townTeam);
        //保存机构
        saveLevel1Org(wlyyQuotaId, hospitalsMap, orgTeam);
        //保存全科团队
        saveLevel1Team(wlyyQuotaId, adminTeamMap, adminTeam);
    }

    /**
     * 保存二级维度的数据
     * @param data
     * @param wlyyQuotaId
     * @param foreachNum 标识二级维度循环的次数  例如 年龄就传6  性别就传3
     * @param type 1 性别 2 年龄 3 用户分组
     */
    public void saveByLevel2(List<Map<String, Map<String, List<ETLModel>>>> data, String wlyyQuotaId,Integer foreachNum,Integer type) throws Exception {
        List<AdminTeam> adminTeams=doctorAdminTeamDao.findAllTeam();
        Map<String, AdminTeam> adminTeamMap = new HashMap<String, AdminTeam>();
        for (AdminTeam adminTeam : adminTeams) {
            adminTeamMap.put(adminTeam.getId()+"", adminTeam);
        }
        //查找出系统全部的机构
        List<Hospital> hospitals = hospitalDao.findHospitalzxFWZ();
        Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
        for (Hospital hospital : hospitals) {
            hospitalsMap.put(hospital.getCode(), hospital);
        }
        //查找出厦门市全部的区
        List<Town> towns = townDao.findByCityCode(Constant.city);
        Map<String, Town> townsMap = new HashMap<String, Town>();
        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);

        Map<String, Map<String, List<ETLModel>>> adminTeam=data.get(0);//团队的数据
        Map<String, Map<String, List<ETLModel>>> orgTeam=data.get(1);//机构的数据
        Map<String, Map<String, List<ETLModel>>> townTeam=data.get(2);//区的数据
        Map<String, Map<String, List<ETLModel>>> cityTeam=data.get(3);//市的数据
        //保存市
        saveLevel2City(wlyyQuotaId, cityMap, cityTeam,foreachNum,type);
        //保存区
        saveLevel2Town(wlyyQuotaId, townsMap, townTeam,foreachNum,type);
        //保存机构
        saveLevel2Org(wlyyQuotaId, hospitalsMap, orgTeam,foreachNum,type);
        //保存全科团队
        saveLevel2Team(wlyyQuotaId, adminTeamMap, adminTeam,foreachNum,type);
    }

    private void saveLevel2Team(String wlyyQuotaId, Map<String, AdminTeam> adminTeamMap, Map<String, Map<String, List<ETLModel>>> eltModel, Integer foreachNum, Integer type) {
        for(Map.Entry<String,AdminTeam> adminTeam:adminTeamMap.entrySet()){
            Map<String, List<ETLModel>> oneELTMap= eltModel.get(adminTeam.getKey());
            for(int i=1;i<=foreachNum;i++){
                AdminTeam adminTeamObj=adminTeam.getValue();
                if(adminTeamObj==null)continue;
                JSONObject jo=new JSONObject();
                int num=0;
                if(oneELTMap!=null){
                    List<ETLModel> etlModel= oneELTMap.get(i+"");
                    if(etlModel!=null&&etlModel.size()>0){
                        num=etlModel.size();
                    }
                }
                jo.put("date", DateUtil.dateToStrLong(new Date()));
                jo.put("num", num);
                jo.put("teamName", adminTeamObj.getName());
                jo.put("teamId", adminTeamObj.getId());
                String key=i+"";
                jo.put("name", getLevel2Name(key,type));
                redisTemplate.opsForValue().set(new StringBuffer("quota:"+wlyyQuotaId+":1:"+key+":"+adminTeam.getKey()).toString(), jo.toString());
            }
        }
    }

    private void saveLevel2Org(String wlyyQuotaId, Map<String, Hospital> hospitalsMap, Map<String, Map<String, List<ETLModel>>> eltModel, Integer foreachNum, Integer type) {
        for(Map.Entry<String,Hospital> hospital:hospitalsMap.entrySet()){
            Map<String, List<ETLModel>> oneELTMap= eltModel.get(hospital.getKey());
            for(int i=1;i<=foreachNum;i++){
                Hospital hospitalObj=hospital.getValue();
                if(hospitalObj==null)continue;
                JSONObject jo=new JSONObject();
                int num=0;
                if(oneELTMap!=null){
                    List<ETLModel> etlModel= oneELTMap.get(i+"");
                    if(etlModel!=null&&etlModel.size()>0){
                        num=etlModel.size();
                    }
                }
                jo.put("date", DateUtil.dateToStrLong(new Date()));
                jo.put("num", num);
                jo.put("orgName", hospitalObj.getName());
                jo.put("orgCode", hospitalObj.getCode());
                String key=i+"";
                jo.put("name", getLevel2Name(key,type));
                redisTemplate.opsForValue().set(new StringBuffer("quota:"+wlyyQuotaId+":2:"+key+":"+hospital.getKey()).toString(), jo.toString());
            }
        }
    }

    private void saveLevel2Town(String wlyyQuotaId, Map<String, Town> townsMap, Map<String, Map<String, List<ETLModel>>> eltModel, Integer foreachNum, Integer type) {
        for(Map.Entry<String,Town> town:townsMap.entrySet()){
            Map<String, List<ETLModel>> oneELTMap= eltModel.get(town.getKey());
            for(int i=1;i<=foreachNum;i++){
                JSONObject jo=new JSONObject();
                int num=0;
                if(oneELTMap!=null){
                    List<ETLModel> etlModel= oneELTMap.get(i+"");
                    if(etlModel!=null&&etlModel.size()>0){
                        num=etlModel.size();
                    }
                }
                jo.put("date", DateUtil.dateToStrLong(new Date()));
                jo.put("num", num);
                jo.put("townName", town.getValue().getName());
                jo.put("townCode", town.getValue().getCode());
                String key=i+"";
                jo.put("name", getLevel2Name(key,type));
                redisTemplate.opsForValue().set(new StringBuffer("quota:"+wlyyQuotaId+":3:"+key+":"+town.getKey()).toString(), jo.toString());
            }
        }
    }

    private void saveLevel2City(String wlyyQuotaId, Map<String, City> cityMap, Map<String, Map<String, List<ETLModel>>> eltModel, Integer foreachNum, Integer type) {
        for(Map.Entry<String,City> city:cityMap.entrySet()){
            Map<String, List<ETLModel>> oneELTMap= eltModel.get(city.getKey());
            for(int i=1;i<=foreachNum;i++){
                JSONObject jo=new JSONObject();
                int num=0;
                if(oneELTMap!=null){
                    List<ETLModel> etlModel= oneELTMap.get(i+"");
                    if(etlModel!=null&&etlModel.size()>0){
                        num=etlModel.size();
                    }
                }
                jo.put("date", DateUtil.dateToStrLong(new Date()));
                jo.put("num", num);
                jo.put("cityName", city.getValue().getName());
                jo.put("cityCode", city.getValue().getCode());
                String key=i+"";
                jo.put("name", getLevel2Name(key,type));
                redisTemplate.opsForValue().set(new StringBuffer("quota:"+wlyyQuotaId+":4:"+key+":"+city.getKey()).toString(), jo.toString());
            }
        }
    }

    private void saveLevel1Team(String wlyyQuotaId, Map<String, AdminTeam> adminTeamMap, Map<String, List<ETLModel>> adminTeam) {
        for(Map.Entry<String,AdminTeam> entry:adminTeamMap.entrySet()){
            JSONObject jo=new JSONObject();
            Integer num=0;
            AdminTeam adminTeamObj=entry.getValue();
            if(adminTeamObj==null)continue;
            List<ETLModel> etlList=adminTeam.get(entry.getKey());
            if(etlList!=null&&etlList.size()>0){
                num=etlList.size();
            }
            jo.put("date", DateUtil.dateToStrLong(new Date()));
            jo.put("num", num);
            jo.put("teamName", adminTeamObj.getName());
            jo.put("teamId", adminTeamObj.getId());
            redisTemplate.opsForValue().set(new StringBuffer("quota:"+wlyyQuotaId+":1:"+entry.getKey()).toString(), jo.toString());
        }
    }

    private void saveLevel1Org(String wlyyQuotaId, Map<String, Hospital> hospitalsMap, Map<String, List<ETLModel>> orgTeam) {
        for(Map.Entry<String,Hospital> entry:hospitalsMap.entrySet()){
            Hospital hospital=entry.getValue();
            if(hospital==null)continue;
            JSONObject jo=new JSONObject();
            Integer num=0;
            List<ETLModel> etlList=orgTeam.get(entry.getKey());
            if(etlList!=null&&etlList.size()>0){
                num=etlList.size();
            }
            jo.put("date", DateUtil.dateToStrLong(new Date()));
            jo.put("num", num);
            jo.put("orgName", hospital.getName());
            jo.put("orgCode", hospital.getCode());
            redisTemplate.opsForValue().set(new StringBuffer("quota:"+wlyyQuotaId+":2:"+entry.getKey()).toString(), jo.toString());
        }
    }

    private void saveLevel1Town(String wlyyQuotaId, Map<String, Town> townsMap, Map<String, List<ETLModel>> townTeam) {
        for(Map.Entry<String,Town> entry:townsMap.entrySet()){
            JSONObject jo=new JSONObject();
            Integer num=0;
            List<ETLModel> etlList=townTeam.get(entry.getKey());
            if(etlList!=null&&etlList.size()>0){
                num=etlList.size();
            }
            jo.put("date", DateUtil.dateToStrLong(new Date()));
            jo.put("num", num);
            jo.put("townName", entry.getValue().getName());
            jo.put("townCode", entry.getValue().getCode());
            redisTemplate.opsForValue().set(new StringBuffer("quota:"+wlyyQuotaId+":3:"+entry.getKey()).toString(), jo.toString());
        }
    }

    private void saveLevel1City(String wlyyQuotaId, Map<String, City> cityMap, Map<String, List<ETLModel>> cityTeam) {
        for(Map.Entry<String,City> entry:cityMap.entrySet()){
            JSONObject jo=new JSONObject();
            Integer num=0;
            List<ETLModel> etlList=cityTeam.get(entry.getKey());
            if(etlList!=null&&etlList.size()>0){
                num=etlList.size();
            }
            jo.put("date", DateUtil.dateToStrLong(new Date()));
            jo.put("num", num);
            jo.put("cityName", entry.getValue().getName());
            jo.put("cityCode", entry.getValue().getCode());
            redisTemplate.opsForValue().set(new StringBuffer("quota:"+wlyyQuotaId+":4:"+entry.getKey()).toString(), jo.toString());
        }
    }

    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);
            }
            default:{
                return "";
            }
        }
    }
}
