package com.yihu.wlyy.statistics.job.business;

import com.yihu.wlyy.statistics.dao.*;
import com.yihu.wlyy.statistics.etl.dataFilter.*;
import com.yihu.wlyy.statistics.etl.extract.DBExtract;
import com.yihu.wlyy.statistics.etl.model.ETLModel;
import com.yihu.wlyy.statistics.etl.model.FilterModel;
import com.yihu.wlyy.statistics.etl.mycache.CachePool;
import com.yihu.wlyy.statistics.etl.role.*;
import com.yihu.wlyy.statistics.etl.storage.RedisStorage;
import com.yihu.wlyy.statistics.model.consult.ConsultTeam;
import com.yihu.wlyy.statistics.model.consult.ConsultTeamDto;
import com.yihu.wlyy.statistics.model.hosptial.Hospital;
import com.yihu.wlyy.statistics.model.job.QuartzJobLog;
import com.yihu.wlyy.statistics.model.patient.PatientHealthGuidance;
import com.yihu.wlyy.statistics.model.patient.PatientReservation;
import com.yihu.wlyy.statistics.model.signfamily.SignFamily;
import com.yihu.wlyy.statistics.model.signfamily.SignFamilyRenew;
import com.yihu.wlyy.statistics.model.system.Town;
import com.yihu.wlyy.statistics.model.team.AdminTeam;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.util.JsonUtil;
import com.yihu.wlyy.statistics.util.SpringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.json.JSONObject;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;


import javax.annotation.Resource;
import javax.sql.DataSource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 实时统计今天的数据
 */
@Component
@Scope("prototype")
@DisallowConcurrentExecution//防止到了执行时间点前一任务还在执行中，但是这时有空闲的线程，那么马上又会执行，这样一来就会存在同一job被并行执行
public class CurrentDayAllQuotaJob implements Job {
    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private TownDao townDao;
    @Autowired
    private QuartzJobLogDao quartzJobLogDao;//执行日志Dao
    @Autowired
    private DoctorAdminTeamDao doctorAdminTeamDao;
    @Autowired
    private ConsultDataFilter consultDataFilter;
    @Autowired
    private HealthGuideDataFilter healthGuideDataFilter;
    @Autowired
    private AgentAppointmentFilter agentAppointmentFilter;
    @Autowired
    private FollowUpDataFilter followUpDataFilter;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    List<Town> towns = null;//厦门市全部的区
    List<Hospital> hospitals = null;//系统全部的机构
    List<AdminTeam> adminTeams = null;//系统全部的团队
    Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
    Map<String, Town> townsMap = new HashMap<String, Town>();
    Map<String, AdminTeam> adminTeamMap = new HashMap<String, AdminTeam>();

    private String now = getDayString(-1) + Constant.quota_date_last;

    private String tomorrow = getDayString(1);

    private StringBuffer allContent = new StringBuffer();//日志内容

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Value("${redis.quota.current.expire}")
    String currentExpire = "2";


    //im库的jdbcTemplate
    private JdbcTemplate imjdbcTemplate;
    @Resource(name = "imData")
    private DataSource dataSource;

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);


            //初始化im数据源
            this.imjdbcTemplate = new JdbcTemplate();
            this.imjdbcTemplate.setDataSource(dataSource);


            towns = townDao.findByCityCode(Constant.city); //查找出厦门市全部的区
            hospitals = hospitalDao.findHospitalzxFWZ(); //查找出系统全部的机构
            adminTeams = doctorAdminTeamDao.findAllTeam();//查找出系统全部的全科医生
            for (Hospital hospital : hospitals) {
                hospitalsMap.put(hospital.getCode(), hospital);
            }
            for (Town town : towns) {
                townsMap.put(town.getCode(), town);
            }
            for (AdminTeam adminTeam : adminTeams) {
                adminTeamMap.put(adminTeam.getId() + "", adminTeam);
            }
            //清空缓存
            CachePool.cleanAllCache();
            //计算指标
            computequota();


        } catch (Exception e) {
            //延迟上一次key的生命周期
            if (CachePool.getKeys() != null && CachePool.getKeys().size() > 0) {
                for (String key : CachePool.getKeys()) {
                    if (!StringUtils.isEmpty(key)) {
                        redisTemplate.expire(key, Integer.valueOf(currentExpire), TimeUnit.HOURS);//2小时过期}
                    }
                }
            }
            //如果出錯立即重新執行
            JobExecutionException e2 = new JobExecutionException(e);
            e2.setRefireImmediately(true);
            e.printStackTrace();
        }
    }

    //统计
    private void computequota() throws Exception {

        RedisStorage.timeKey = new Date().getTime() + "";

        QuartzJobLog quartzJobLog = new QuartzJobLog();
        quartzJobLog.setJobStartTime(new Date());
        allContent = new StringBuffer();
        //找出今天的签约信息 yesterday,now
        String sql = " select id,code,idcard,hospital,admin_team_code,expenses_status,patient,server_type from wlyy_sign_family a where " +
                "  a.type =2 and " +
                " status in (1,2) and  " +
                " a.expenses_time< '" + tomorrow + "' and " +
                " a.sign_year ='"+Constant.getNowYearByDateYeaterday(tomorrow)+"' and " +
                " a.expenses_status=1 ";
        String sqlCount = " select count(id) from wlyy_sign_family a where  " +
                " a.type =2 and " +
                " status in (1,2) and  " +
                " a.expenses_time< '" + tomorrow + "' and " +
                " a.sign_year ='"+Constant.getNowYearByDateYeaterday(tomorrow)+"' and " +
                " a.expenses_status=1 ";

        //抽取數據
        List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extractByPage(SignFamily.class, sql, sqlCount, true);
        computequota_1(sql, signFamilies, null); //统计今天的签约
        computequota_2(); //统计今天的解约
        computequota_3(); //统计今天的健康咨询量
        computequota_4(); //统计今天的随访量
        computequota_5(); //统计今天的健康指导
        computequota_6(sql, signFamilies, null); //统计今天的签约患者性别
        computequota_7(sql, signFamilies, null);//统计今天的签约患者分组
        computequota_8(sql, signFamilies, null);//统计今天的签约患者年龄
        computequota_9();//统计今天的待签约
        computequota_10();//统计今天的改签
        computequota_12(sql, signFamilies, null);//统计今天的签约下按年龄分组后再
        computequota_13(sql, signFamilies, null);//统计今天的签约量
        computequota_16_1(sql, signFamilies, null);//统计今天的签约数的扣费钱
        computequota_15(sql, signFamilies, null);//统计今天的健康分布统计
        computequota_17(sql, signFamilies, null);//统计今天的年龄疾病
        computequota_20();//统计今天的已经签约患者绑定设备
        computequota_21();//统计今天的已经签约微信关注人数
        computequota_49();//统计今天的已经签约未扣费微信关注人数
        computequota_50();//统计今天的已经签约已扣费微信关注人数
        computequota_22();//未回复的咨询量
        computequota_23();//及时回复的咨询量
        computequota_24();//及时回复的咨询量时间分布
        computequota_25();//统计质询量总数
        computequota_26();//统计未缴费签约总数
        computequota_27();//统计代预约量
        computequota_28();//统计平均值
        computequota_31(); //统计续签夸团队
        computequota_32(); //统计续签夸社区
        computequota_33(); //统计续签夸区
        computequota_34(); //续签团队签入
        computequota_35(); //统计团队签出
        computequota_36(sql, signFamilies, null); //签约服务类型
        //computequota_37(); //统计团队转签 没用指标
        computequota_38(); //续签机构签入
        computequota_39(); //统计机构签出
        computequota_40(); //续签区签入
        computequota_41(); //统计区签出
        computequota_42(); //统计团队续签
        computequota_43(); //统计社区续签
        computequota_44(); //统计区续签
        computequota_29(); //统计市级续签
        //computequota_45(); //统计社区转签 没用指标
        //computequota_46(); //统计区转签  没用指标
        computequota_30(); //统计团队续签原因
//        computequota_47(); //统计社区续签原因 没用指标
//        computequota_48(); //统计区续签原因 没用指标


        quartzJobLog.setJobContent(allContent.toString());
        quartzJobLog.setJobName("实时统计");
        quartzJobLog.setJobId("11");
        quartzJobLog.setJobType("1");
        quartzJobLog.setJobEndTime(new Date());
        quartzJobLogDao.save(quartzJobLog);

        initCurrentExpire();//初始化生命周期

        for (String key : RedisStorage.keys) {
            if (!StringUtils.isEmpty(key)) {
                redisTemplate.expire(key, Integer.valueOf(currentExpire), TimeUnit.HOURS);//2小时过期}
            }
        }
        CachePool.addKeys(RedisStorage.keys);//缓存key

        //更新统计时间
        redisTemplate.opsForValue().set("quota:date", DateUtil.dateToStrLong(new Date()));
        //更新时间key
        redisTemplate.opsForValue().set("quota:timeKey", RedisStorage.timeKey);

        RedisStorage.keys.clear();//清空Key
    }

    private void computequota_37() {
        String quotaId = "37";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where  fr.old_admin_team_id!=fr.admin_team_id  and fr.hospital=fr.old_hospital and LEFT(fr.hospital,6)=LEFT(fr.old_hospital,6)and fr.apply_date<'"+tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "'";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------37----------");
    }

    private void computequota_36(String sql, List<SignFamily> signFamilies, Object o) {
        String quotaId = "36";
        //找出今天的签约信息
        //String sql=" select id,code,idcard,hospital,admin_team_code from wlyy_sign_family a where  a.type =2 and  a.expenses_time>= '"+now+"' and  a.expenses_time< '"+tomorrow+"' and a.expenses_status=1 ";
        try {
            //抽取數據
            // List<SignFamily> signFamilies= SpringUtil.getBean(DBExtract.class).extract(SignFamily.class,sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, SignDataFilter.level2ServerType, sql, null);
            //统计数据 一级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> patientSexRoleData = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(patientSexRoleData, null, quotaId, 13, 10, 1);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------36----------");
    }


    private void computequota_22() {
        String quotaId = "22";
        try {
            //查找Im库中的所有的医生ID
            List<String> consultIdMaps = getConsultIdsByIm();

            List<ConsultTeam> consultTeams = new ArrayList<>();
            StringBuffer sb = new StringBuffer("");
            for (int i = 0; i < consultIdMaps.size(); i++) {
                sb.append(consultIdMaps.get(i));
                if (i < (consultIdMaps.size() - 1)) {
                    sb.append(",");
                }
                if ((1 + 1) % 100 == 0) {
                    //每隔100统计一次
                    String sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "' and a.consult in (" + sb.toString() + ")";
                    //抽取數據
                    List<ConsultTeam> consultTeamsTemp = SpringUtil.getBean(DBExtract.class).extract(ConsultTeam.class, sql);
                    consultTeams.addAll(consultTeamsTemp);
                    sb = new StringBuffer();//重置 in条件
                } else {
                    //判断是不是最后的一段
                    if (consultIdMaps.size() == (i + 1)) {
                        String sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "'and a.consult in (" + sb.toString() + ")";
                        //抽取數據
                        List<ConsultTeam> consultTeamsTemp = SpringUtil.getBean(DBExtract.class).extract(ConsultTeam.class, sql);
                        consultTeams.addAll(consultTeamsTemp);
                    }
                }
            }
            String sql = "";
            if (consultIdMaps.size() > 0) {
                sql = " select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "'and a.consult in (" + sb.toString() + ")";
            } else {
                sql = " select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "'and a.consult ='noRey' ";
            }

            System.out.println("consultTeams num:" + consultTeams.size());
            //過濾數據
            FilterModel etlModels = consultDataFilter.filter(consultTeams, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------22-----------");

    }

    private void computequota_23() {
        String quotaId = "23";
        try {
            //查找Im库中的所有的医生ID
            List<String> consultIdMaps = getConsultIdsByIm23();
            StringBuffer sb = new StringBuffer("");
            List<ConsultTeam> consultTeams = new ArrayList<>();
            for (int i = 0; i < consultIdMaps.size(); i++) {
                sb.append(consultIdMaps.get(i));
                if (i < (consultIdMaps.size() - 1)) {
                    sb.append(",");
                }
                if ((1 + 1) % 100 == 0) {
                    //每隔100统计一次
                    String sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "' and a.consult in (" + sb.toString() + ")";
                    //抽取數據
                    List<ConsultTeam> consultTeamsTemp = SpringUtil.getBean(DBExtract.class).extract(ConsultTeam.class, sql);
                    consultTeams.addAll(consultTeamsTemp);
                    sb = new StringBuffer();//重置 in条件
                } else {
                    //判断是不是最后的一段
                    if (consultIdMaps.size() == (i + 1)) {
                        String sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow) +Constant.quota_date_last+ "' and a.consult in (" + sb.toString() + ")";
                        //抽取數據
                        List<ConsultTeam> consultTeamsTemp = SpringUtil.getBean(DBExtract.class).extract(ConsultTeam.class, sql);
                        consultTeams.addAll(consultTeamsTemp);
                    }
                }
            }
            String sql = "";
            //找出今天的咨询信息
            if (consultIdMaps.size() > 0) {
                sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and a.consult in (" + sb.toString() + ")";
            } else {
                sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and a.consult ='Rey' ";
            }

            System.out.println("consultTeams num:" + consultTeams.size());
            //過濾數據
            FilterModel etlModels = consultDataFilter.filter(consultTeams, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------23-----------");

    }

    private void computequota_24() {
        String quotaId = "24";
        try {
            //查找Im库中的所有的医生ID
            List<Map<String, Object>> ims = getConsultIdsByIm24();

            List<ConsultTeam> consultTeams = new ArrayList<>();
            //得到医生的id
            List<String> consultIdMaps = new ArrayList<>();
            ims.stream().forEach(map -> {
                if (map.containsKey("consultId")) {
                    consultIdMaps.add(String.valueOf("'" + map.get("consultId") + "'"));
                }
            });

            StringBuffer sb = new StringBuffer("");
            for (int i = 0; i < consultIdMaps.size(); i++) {
                sb.append(consultIdMaps.get(i));
                if (i < (consultIdMaps.size() - 1)) {
                    sb.append(",");
                }
                if ((1 + 1) % 100 == 0) {
                    //每隔100统计一次
                    String sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "' and a.consult in (" + sb.toString() + ")";
                    //抽取數據
                    List<ConsultTeam> consultTeamsTemp = SpringUtil.getBean(DBExtract.class).extract(ConsultTeam.class, sql);
                    consultTeams.addAll(consultTeamsTemp);
                    sb = new StringBuffer();//重置 in条件
                } else {
                    //判断是不是最后的一段
                    if (consultIdMaps.size() == (i + 1)) {
                        String sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "'and a.consult in (" + sb.toString() + ")";
                        //抽取數據
                        List<ConsultTeam> consultTeamsTemp = SpringUtil.getBean(DBExtract.class).extract(ConsultTeam.class, sql);
                        consultTeams.addAll(consultTeamsTemp);
                    }
                }
            }
            String sql = "";
            //找出今天的咨询信息
            if (consultIdMaps.size() > 0) {
                sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "'and a.consult in (" + sb.toString() + ")";
            } else {
                sql = "select admin_team_code,id,consult from wlyy_consult_team a where  a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "' and a.consult ='Rey' ";
            } //抽取數據

            List<ConsultTeamDto> consultTeamDtos = getConsultTeams(ims, consultTeams);
            System.out.println("consultTeamDtos num:" + consultTeamDtos.size());
            //過濾數據
            FilterModel etlModels = consultDataFilter.filter2(consultTeamDtos, sql, null, ConsultDataFilter.level2ReplyTime);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 5, 8);

            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------24----------");

    }

    /**
     * 找出 im中的所有未回复的topic
     *
     * @return
     */
    public List<String> getConsultIdsByIm() {
        String sql = "SELECT  DISTINCT t.id consultId " +
                " FROM " +
                " topics t, " +
                " participants p, " +
                " doctors d ," +
                " sessions s " +
                " WHERE " +
                " p.participant_id = d.id " +
                " AND t.session_id = p.session_id " +
                " AND t.session_id = s.id " +
                " AND s.type = 1 " +
                " AND t.status <>10 " +
                " AND t.`reply`=0 " +
                " AND t.create_time< '" + tomorrow + "'";
        List<Map<String, Object>> ims = imjdbcTemplate.queryForList(sql);
        //得到医生的id
        List<String> ids = new ArrayList<>();
        ims.stream().forEach(map -> {
            if (map.containsKey("consultId")) {
                ids.add(String.valueOf("'" + map.get("consultId") + "'"));
            }
        });
        System.out.println("22 ims 实时:" + ims.size());
        System.out.println("22 实时sql:" + sql);
        System.out.println("22 ids 实时:" + ids.size());
        return ids;
    }

    /**
     * 找出 im中的及时回复的topic
     *
     * @return
     */
    public List<String> getConsultIdsByIm23() {
        String sql = "SELECT  DISTINCT t.id consultId " +
                " FROM " +
                " topics t, " +
                " participants p, " +
                " doctors d ," +
                " sessions s " +
                " WHERE " +
                " p.participant_id = d.id " +
                " AND t.session_id = p.session_id " +
                " AND t.session_id = s.id " +
                " AND s.business_type=2 " +
                " AND t.`reply`=1 " +
                " AND TIMESTAMPDIFF(SECOND ,t.create_time ,t.reply_time) <86400 AND TIMESTAMPDIFF(SECOND ,t.create_time,t.reply_time) >0 " +
                " AND t.create_time< '" + tomorrow + "'";
        List<Map<String, Object>> ims = imjdbcTemplate.queryForList(sql);
        //得到医生的id
        List<String> ids = new ArrayList<>();
        ims.stream().forEach(map -> {
            if (map.containsKey("consultId")) {
                ids.add(String.valueOf("'" + map.get("consultId") + "'"));
            }
        });
        System.out.println("23 ims 实时:" + ims.size());
        System.out.println("23 实时sql:" + sql);
        System.out.println("23 ids 实时:" + ids.size());
        return ids;
    }

    /**
     * 找出 im中的及时回复的topic
     *
     * @return
     */
    public List<Map<String, Object>> getConsultIdsByIm24() {
        String sql = "SELECT  DISTINCT t.id consultId ,t.reply_time replyTime" +
                " FROM " +
                " topics t, " +
                " participants p, " +
                " doctors d ," +
                " sessions s " +
                " WHERE " +
                " p.participant_id = d.id " +
                " AND t.session_id = p.session_id " +
                " AND t.session_id = s.id " +
                " AND s.business_type=2 " +
                " AND t.`reply`=1 " +
                " AND TIMESTAMPDIFF(SECOND ,t.create_time,t.reply_time) <86400 AND TIMESTAMPDIFF(SECOND ,t.create_time,t.reply_time) >0 " +
                " AND t.create_time< '" + tomorrow + "'";
        List<Map<String, Object>> ims = imjdbcTemplate.queryForList(sql);

        return ims;
    }

    public List<ConsultTeamDto> getConsultTeams(List<Map<String, Object>> ims, List<ConsultTeam> consultTeams) {
        List<ConsultTeamDto> rs = new ArrayList<>();
        if (consultTeams != null && consultTeams.size() > 0 && ims != null && ims.size() > 0) {

            //將List转换为Map,减小循环层级
            Map<String, Object> mapList = new HashedMap();
            for (Map<String, Object> m : ims) {
                mapList.put(m.get("consultId").toString(), m);
            }
            //合并结果集
            for (ConsultTeam consultTeam : consultTeams) {
                ConsultTeamDto con = new ConsultTeamDto();
                String key = consultTeam.getConsult();
                Map<String, Object> ms = (Map<String, Object>) mapList.get(key);
                con.setConsult(key);
                con.setId(consultTeam.getId());
                con.setAdminTeamCode(consultTeam.getAdminTeamCode());
                con.setReplyTime((Date) ms.get("replyTime"));
                rs.add(con);
            }
            return rs;
        }
        return rs;
    }


    private void computequota_21() {
        try {
            //找出今天的签约信息 yesterday,now
            String sql = " SELECT " +
                    "  sf. CODE, " +
                    "  sf.idcard, " +
                    "  sf.hospital, " +
                    "  sf.admin_team_code " +
                    " FROM " +
                    "  wlyy_patient p, " +
                    "  wlyy_sign_family sf " +
                    " WHERE " +
                    "   p.idcard = sf.idcard " +
                    " AND sf.type = 2 " +
                    " AND sf. STATUS IN (1, 2) " +
                    " AND p.openid is not null  " +
                    " AND p.openid_time <'" + tomorrow + "' " +
                    " and sf.sign_year ='"+Constant.getNowYearByDate(tomorrow)+"'  ";
            //抽取數據
            List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamily.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, "21");
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------21----------");
    }

    private void computequota_49() {
        try {
            //找出今天的签约信息 yesterday,now
            String sql = " SELECT " +
                    "  sf. CODE, " +
                    "  sf.idcard, " +
                    "  sf.hospital, " +
                    "  sf.admin_team_code " +
                    " FROM " +
                    "  wlyy_patient p, " +
                    "  wlyy_sign_family sf " +
                    " WHERE " +
                    "   p.idcard = sf.idcard " +
                    " AND sf.type = 2 " +
                    " AND sf. STATUS IN (1, 2) " +
                    " AND sf.expenses_status =0" +
                    " AND p.openid is not null  " +
                    " AND p.openid_time <'" + tomorrow + "' " +
                    " and sf.sign_year ='"+Constant.getNowYearByDate(tomorrow)+"'  ";
            //抽取數據
            List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamily.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, "49");
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------49----------");
    }
    private void computequota_50() {
        try {
            //找出今天的签约信息 yesterday,now
            String sql = " SELECT " +
                    "  sf. CODE, " +
                    "  sf.idcard, " +
                    "  sf.hospital, " +
                    "  sf.admin_team_code " +
                    " FROM " +
                    "  wlyy_patient p, " +
                    "  wlyy_sign_family sf " +
                    " WHERE " +
                    "   p.idcard = sf.idcard " +
                    " AND sf.type = 2 " +
                    " AND sf. STATUS IN (1, 2) " +
                    " AND sf.expenses_status =1" +
                    " AND p.openid is not null  " +
                    " AND p.openid_time <'" + tomorrow + "' " +
                    " and sf.sign_year ='"+Constant.getNowYearByDate(tomorrow)+"'  ";
            //抽取數據
            List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamily.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, "50");
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------50----------");
    }

    private void computequota_20() {
        try {
            String sql = " SELECT " +
                    "  sf. CODE, " +
                    "  sf.idcard, " +
                    "  sf.hospital, " +
                    "  sf.admin_team_code " +
                    " FROM " +
                    "  wlyy_patient_device pd, " +
                    "  wlyy_sign_family sf " +
                    " WHERE " +
                    "  pd.user_idcard = sf.idcard " +
                    " AND sf.type = 2 " +
                    " AND pd.del=0 " +
                    " AND sf. STATUS IN (1, 2) " +
                    " AND pd.czrq <' " + tomorrow + "'"+
                    " and sf.sign_year ='"+Constant.getNowYearByDate(tomorrow)+"'  " ;
            //抽取數據
            List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamily.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, "20");
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------20----------");
    }

    private void initCurrentExpire() {
        try {
            if (StringUtils.isEmpty(currentExpire)) {
                currentExpire = "2";//默认是2小时
            }
            if (!org.apache.commons.lang.StringUtils.isNumeric(currentExpire)) {
                currentExpire = "2";//默认是2小时
            } else {
                Integer cout = Integer.valueOf(currentExpire);
                if (cout <= 0) {
                    currentExpire = "2";//默认是2小时
                }
            }
        } catch (Exception e) {
            currentExpire = "2";//默认是2小时
        }
    }

    private void computequota_17(String sql, List<SignFamily> signFamilies, Object o) {
        String quotaId = "17";
        try {
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, SignDataFilter.level2AgeDisease, sql, null);
            //统计数据 1级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存数据 1 表示：从1开始循环 6表示：循环6次 3表示 2级维度是 3
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 3, 7, 1);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------17----------");
    }

    private void computequota_16_1(String sql, List<SignFamily> signFamilies_1, List<SignFamily> deleteSignFamilies) {
        String quotaId = "16";
        try {
            sql = " select id,code,idcard,hospital,admin_team_code,expenses_status from wlyy_sign_family a where  " +
                    "a.type =2 and " +
                    " (expenses_status !=1 or expenses_status is null) and " +
                    " status in (1,2) and    " +
                    " a.sign_year ='"+Constant.getNowYearByDateYeaterday(tomorrow)+"' and  " +
                    " a.apply_date< '" + tomorrow + "' ";
            String sqlCount = " select count(id) from wlyy_sign_family a where " +
                    "  a.type =2 and " +
                    "  (expenses_status !=1 or expenses_status is null) and " +
                    " status in (1,2) and  " +
                    " a.sign_year ='"+Constant.getNowYearByDateYeaterday(tomorrow)+"' and  " +
                    " a.apply_date< '" + tomorrow + "' ";
            List<SignFamily> signFamilies_2 = SpringUtil.getBean(DBExtract.class).extractByPage(SignFamily.class, sql, sqlCount, true);
            signFamilies_2.addAll(signFamilies_1);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies_2, SignDataFilter.level2Expenses, sql, null);
            //统计数据 一级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存redis
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 2, 6, 0);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------16-1----------");
    }

    private void computequota_13(String sql, List<SignFamily> signFamilies, List<SignFamily> deleteSignFamilies) {
        //找出今天的签约信息 yesterday,now
        try {
            //抽取數據 分页抽取
            //List<SignFamily> signFamilies= SpringUtil.getBean(DBExtract.class).extractByPage(SignFamily.class,sql,sqlCount,true);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnData = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnData, null, "13");
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------13----------");
    }

    private void computequota_16_0(String sql, List<SignFamily> signFamilies_1, List<SignFamily> deleteSignFamilies) {
        String quotaId = "16";
        //找出今天的签约信息
        try {
            //抽取第二种条件的语句 找出未缴费的
            sql = " select id,code,idcard,hospital,admin_team_code,expenses_status from wlyy_sign_family a where  a.type =2 and (expenses_status =0 or expenses_status is null) and    a.apply_date< '" + tomorrow + "' ";
            String sqlCount = " select count(id) from wlyy_sign_family a where  a.type =2 and (expenses_status =0 or expenses_status is null)  and  a.apply_date< '" + tomorrow + "' ";
            List<SignFamily> signFamilies_2 = SpringUtil.getBean(DBExtract.class).extractByPage(SignFamily.class, sql, sqlCount, true);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies_2, SignDataFilter.level2Expenses, sql, null);
            //统计数据
            //统计数据 一级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 2, 6, 0);

            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------16-0----------");
    }

    private void computequota_15(String sql, List<SignFamily> signFamilies, List<SignFamily> deleteSignFamilies) {
        String quotaId = "15";
        //找出今天的签约信息
        //String sql=" select id,code,idcard,hospital,admin_team_code from wlyy_sign_family a where  a.type =2 and  a.expenses_time>= '"+now+"' and  a.expenses_time< '"+tomorrow+"' and a.expenses_status=1 ";
        try {
            //抽取數據
            // List<SignFamily> signFamilies= SpringUtil.getBean(DBExtract.class).extract(SignFamily.class,sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, SignDataFilter.level2Health, sql, null);
            //统计数据 一级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> patientSexRoleData = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(patientSexRoleData, null, quotaId, 4, 5, 0);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------15----------");
    }

    private void computequota_12(String sql, List<SignFamily> signFamilies, List<SignFamily> deleteSignFamilies) throws Exception {
        String quotaId = "12";
        //找出今天的签约信息
        //String sql=" select id,code,idcard,hospital,admin_team_code from wlyy_sign_family a where  a.type =2 and  a.expenses_time>= '"+now+"' and  a.expenses_time< '"+tomorrow+"' and a.expenses_status=1 ";
        //String deleteSql=" select id,code,idcard,hospital,admin_team_code from wlyy_sign_family a where  a.type =2 and  a.czrq>= '"+now+"' and  a.czrq< '"+tomorrow+"' and a.expenses_status=3  ";

        try {
            //抽取數據
            // List<SignFamily> signFamilies= SpringUtil.getBean(DBExtract.class).extract(SignFamily.class,sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, SignDataFilter.level2Age, SignDataFilter.level3Disease, sql, null);
            //统计数据 一级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> patientSexRoleData = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //统计数据 三级维度
            List<Map<String, Map<String, Map<String, List<ETLModel>>>>> returnD = SpringUtil.getBean(Level3Role.class).elt(patientSexRoleData);
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel3(returnD, null, quotaId, 6, 4, 2, 1, 1, 1);
        } catch (Exception e) {

        }
        allContent.append("-----------12----------");
    }

    private void compute(Map<String, Map<String, Map>> rootMap, String rootKey, String ageCode, String diseaseType) {
        if (rootMap.containsKey(rootKey)) {
            //得到市下面的所有的年龄map
            Map<String, Map> groupMapTemp = rootMap.get(rootKey);
            if (groupMapTemp.containsKey(ageCode)) {
                //得到这个年龄下的患者map
                Map<String, Long> mape = groupMapTemp.get(ageCode);
                if (mape.containsKey(diseaseType)) {
                    mape.put(diseaseType, mape.get(diseaseType) + 1L);
                } else {
                    mape.put(diseaseType, 1L);
                }
            } else {
                //新增疾病的统计map
                Map<String, Long> groupMapTemp1 = new HashMap<String, Long>();
                groupMapTemp1.put(diseaseType, 1L);
                groupMapTemp.put(ageCode, groupMapTemp1);
            }
        } else {
            //没有就新建统计数据  新增疾病的统计map
            Map<String, Long> groupMapTemp = new HashMap<String, Long>();
            groupMapTemp.put(diseaseType, 1L);
            //把统计疾病的map放入对应的年龄组map中
            Map<String, Map> groupMapTemp2 = new HashMap<String, Map>();
            groupMapTemp2.put(ageCode, groupMapTemp);
            //把年龄组map放入市的map里面
            rootMap.put(rootKey, groupMapTemp2);
        }
    }

    private void computequota_1(String sql, List<SignFamily> signFamilies, List<SignFamily> deleteSignFamilies) {
        try {
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, "1");
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------1----------");
    }


    private void computequota_2() {
        String quotaId = "2";
        try {
            //找出今天的解约信息
            String sql = " select id,code,idcard,hospital,admin_team_code,expenses_status from wlyy_sign_family a where  a.type =2 and a.status in(-3) and  a.apply_unsign_date< '" + tomorrow + "' and a.apply_unsign_date >'"+Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last+"' ";
            //抽取數據
            List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamily.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------2-----------");
    }

    private void computequota_3() {
        String quotaId = "3";
        try {
            //找出今天的咨询信息
            String sql = " select * from wlyy_consult_team a where  a.czrq>= '" + now + "'and a.czrq< '" + tomorrow + "'";
            //抽取數據
            List<ConsultTeam> consultTeams = SpringUtil.getBean(DBExtract.class).extract(ConsultTeam.class, sql);
            //過濾數據
            FilterModel etlModels = consultDataFilter.filter(consultTeams, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------3-----------");
    }

    private void computequota_4() {
        String quotaId = "4";
        try {
            //找出今天的随访信息
            String sql = " select * from wlyy_followup a where  a.status=1 and  a.followup_plan_date< '" + tomorrow + "' and a.followup_plan_date>= '" + now + "' ";

            //找出今天的随访信息
            List<Map<String, Object>> plans = jdbcTemplate.queryForList(sql);

            FilterModel etlModels = followUpDataFilter.filter(plans, sql, null);
            // 统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------4----------");
    }

    private void computequota_5() {
        String quotaId = "5";

        String sql = "SELECT * FROM wlyy_patient_health_guidance a WHERE a.create_time< '" + tomorrow + "' and a.create_time>= '" + now + "'";
        try {
            //抽取數據
            List<PatientHealthGuidance> consultTeams = SpringUtil.getBean(DBExtract.class).extract(PatientHealthGuidance.class, sql);
            //過濾數據
            FilterModel etlModels = healthGuideDataFilter.filter(consultTeams, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------5----------");
    }

    private void computequota_6(String sql, List<SignFamily> signFamilies, List<SignFamily> deleteSignFamilies) throws Exception {
        String quotaId = "6";
        //找出今天的签约信息
        //  String sql=" select id,code,idcard,hospital,admin_team_code,expenses_status from wlyy_sign_family a where  a.type =2 and  a.expenses_time>= '"+now+"' and  a.expenses_time< '"+tomorrow+"' and a.expenses_status=1 ";
        try {
            //抽取數據
            //List<SignFamily> signFamilies= SpringUtil.getBean(DBExtract.class).extract(SignFamily.class,sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, SignDataFilter.level2Sex, sql, null);
            //统计数据 1级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 3, 1);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------6----------");
    }

    private void computequota_7(String sql, List<SignFamily> signFamilies, List<SignFamily> deleteSignFamilies) {
        String quotaId = "7";
        try {
            //  String sql=" select id,code,idcard,hospital,admin_team_code,expenses_status from wlyy_sign_family a where  a.type =2 and  a.expenses_time>= '"+now+"' and  a.expenses_time< '"+tomorrow+"' and a.expenses_status=1 ";
            //抽取數據
            //List<SignFamily> signFamilies= SpringUtil.getBean(DBExtract.class).extract(SignFamily.class,sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, SignDataFilter.level2Group, sql, null);
            //统计数据 1级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存数据 1 表示：从1开始循环 6表示：循环6次 3表示 2级维度是 3
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 7, 3, -1);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------7-----------");
    }

    private void computequota_8(String sql, List<SignFamily> signFamilies, List<SignFamily> deleteSignFamilies) throws Exception {
        String quotaId = "8";
        try {
            //找出今天的签约信息
            // String sql=" select id,code,idcard,hospital,admin_team_code,expenses_status from wlyy_sign_family a where  a.type =2  and  a.expenses_time>= '"+now+"' and  a.expenses_time< '"+tomorrow+"' and a.expenses_status=1 ";
            //抽取數據
            // List<SignFamily> signFamilies= SpringUtil.getBean(DBExtract.class).extract(SignFamily.class,sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, SignDataFilter.level2Age, sql, null);
            //统计数据 1级维度
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 6, 2);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------8-----------");
    }

    private void computequota_9() throws ParseException {
        String quotaId = "9";
        //找出今天的待签约信息
        String sql = " select id,code,idcard,hospital,admin_team_code,doctor,doctor_health from wlyy_sign_family a where   a.type =2  and a.status=0 and  a.patient_apply_date< '" + tomorrow + "' and a.patient_apply_date >'"+Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last+"'  ";
        try {
            //抽取數據
            List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamily.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filterNoTeam(signFamilies, null, null, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------9-----------");
    }

    private void computequota_10() {
        String quotaId = "10";
        try {
            //找出今天的已改簽信息
            String sql = " select id,code,idcard,hospital,admin_team_code from wlyy_sign_family a where  a.type =2 and a.status =100 and   a.patient_apply_date< '" + tomorrow + "' and a.patient_apply_date >'"+Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last+"' and a.expenses_status=1 ";
            //抽取數據
            List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamily.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------10----------");
    }

    private void computequota_25() {
        String quotaId = "25";
        try {
            //找出总的咨询信息
            String sql = " select * from wlyy_consult_team a where a.czrq< '" + tomorrow + "' and  a.czrq> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "'";
            //抽取數據
            List<ConsultTeam> consultTeams = SpringUtil.getBean(DBExtract.class).extract(ConsultTeam.class, sql);
            //過濾數據
            FilterModel etlModels = consultDataFilter.filter(consultTeams, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("----------25-----------");
    }

    private void computequota_26() {
        String quotaId = "26";
        try {
            //找出今天统计未缴费签约总数
            String sql = "select code,idcard,hospital,admin_team_code from wlyy_sign_family a where  a.type =2 and  a.status>0  and  a.apply_date< '" + tomorrow + "' and  a.apply_date> '" + Constant.getStartTimeByDate(tomorrow)+Constant.quota_date_last + "'  and a.expenses_status=0 ";
            //抽取數據
            List<SignFamily> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamily.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(SignDataFilter.class).filter(signFamilies, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------26----------");
    }

    private void computequota_27() {
        String quotaId = "27";

        String sql = "select w.id,w.patient from  wlyy_patient_reservation w,wlyy_doctor d where w.doctor = d.code and  w.doctor is not null and w.dname is not null and w.czrq< '" + tomorrow + "' and w.czrq>= '" + now + "'";
        try {
            //抽取數據
            List<PatientReservation> patientReservations = SpringUtil.getBean(DBExtract.class).extract(PatientReservation.class, sql);
            //過濾數據
            FilterModel etlModels = agentAppointmentFilter.filter(patientReservations, sql, null);
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------27----------");
    }

    /**
     * SQL统计平均值存储redis
     * 统计市,区,机构,团队医生满意度
     */
    private void computequota_28() {
        //获取市,区统计结果
        List<Map<String, Object>> m1 = getCityTowms();
        JSONObject json1 = new JSONObject();
        json1.put("data", m1);
        String key = "quota:28:4:" + Constant.city + ":3:" + RedisStorage.timeKey;
        redisTemplate.opsForValue().set(key, json1.toString());
        RedisStorage.keys.add(key);
        //获取市,机构统计结果
        List<Map<String, Object>> m2 = getCityHoss();
        JSONObject json2 = new JSONObject();
        json2.put("data", m2);
        String key1 = "quota:28:4:" + Constant.city + ":2:" + RedisStorage.timeKey;
        redisTemplate.opsForValue().set(key1, json2.toString());
        RedisStorage.keys.add(key1);
        //统计区级低下所有机构
        if (m1 != null && m1.size() > 0) {
            for (Map<String, Object> m : m1) {
                String town = (String) m.get("code");
                List<Map<String, Object>> m3 = getTownHoss(town);
                JSONObject json3 = new JSONObject();
                json3.put("data", m3);
                String key2 = "quota:28:3:" + town + ":2:" + RedisStorage.timeKey;
                redisTemplate.opsForValue().set(key2, json3.toString());
                RedisStorage.keys.add(key2);
            }
        }
        //得到所有机构
        List<Map<String, Object>> allHs = getAllHoss();
        //得到所有团队统计结果
        List<Map<String, Object>> allTs = getAllHosTeams();

        //将团队按机构分组,分别存储
        if (allHs != null && allHs.size() > 0) {
            for (Map<String, Object> h : allHs) {
                String code = (String) h.get("code");
                List<Map<String, Object>> hosTeams = new ArrayList<>();
                Iterator<Map<String, Object>> it = allTs.iterator();
                while (it.hasNext()) {
                    Map<String, Object> x = it.next();
                    String hospital = (String) x.get("hospital");
                    if (code.equals(hospital)) {
                        hosTeams.add(x);
                        it.remove();
                    }
                }
                JSONObject json = new JSONObject();
                json.put("data", hosTeams);
                String k = "quota:28:2:" + code + ":1:" + RedisStorage.timeKey;
                redisTemplate.opsForValue().set(k, json.toString());
                RedisStorage.keys.add(k);
            }
        }
    }

    private void computequota_29() {
        String quotaId = "29";
        try {
            String sql=" select fr.sign_code code,fr.old_hospital hospital,fr.old_admin_team_id admin_team_id from wlyy_sign_family_renew_log fr where  " +
                    " fr.apply_date<'"+ tomorrow+"' and " +
                    " fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and " +
                    " LENGTH(fr.hospital)=10 and " +
                    " LENGTH(fr.old_hospital)=10 "; //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------29----------");
    }

    private void computequota_30() {
        String quotaId = "30";
        try {
            String sql="select fr.sign_code code,fr.old_hospital hospital,fr.old_admin_team_id admin_team_id,fr.renew_change_reason from wlyy_sign_family_renew_log fr where " +
                    " fr.old_admin_team_id!=fr.admin_team_id  " +
                    " and LENGTH(fr.hospital)=10 " +
                    " and LENGTH(fr.old_hospital)=10 "+
                    " and fr.apply_date<'"+tomorrow+"' " +
                    " and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "'";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies,"1", sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存redis
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 6, 9, 1);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------30----------");
    }

    private void computequota_47() {
        String quotaId = "47";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id,fr.renew_change_reason from wlyy_sign_family_renew_log fr where  fr.hospital=fr.old_hospital and LEFT(fr.hospital,6)=LEFT(fr.old_hospital,6) and fr.apply_date<'"+tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "'";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies,"1", sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存redis
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 6, 9, 1);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------47----------");
    }

    private void computequota_48() {
        String quotaId = "48";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id,fr.renew_change_reason from wlyy_sign_family_renew_log fr where LEFT(fr.hospital,6)=LEFT(fr.old_hospital,6)  and fr.apply_date<'"+tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "'";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies,"1", sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //统计数据 二级维度
            List<Map<String, Map<String, List<ETLModel>>>> level2Data = SpringUtil.getBean(Level2Role.class).elt(returnDatas);
            //保存redis
            SpringUtil.getBean(RedisStorage.class).saveByLevel2(level2Data, null, quotaId, 6, 9, 1);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------48----------");
    }

    private void computequota_31() {
        String quotaId = "31";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where    fr.apply_date<'"+tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and fr.admin_team_id!=fr.old_admin_team_id and fr.hospital=fr.old_hospital and LEFT(fr.hospital,6)=LEFT(fr.old_hospital,6)" ;
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------31----------");
    }

    private void computequota_32() {
         String quotaId = "32";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where  " +
                    " fr.apply_date<'"+tomorrow+"' and " +
                    " fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and " +
                    " fr.admin_team_id!=fr.old_admin_team_id and " +
                    " fr.hospital!=fr.old_hospital and " +
                    " LEFT(fr.hospital,6)=LEFT(fr.old_hospital,6) and"+
                    "   LENGTH(fr.hospital)=10 and" +
                    "   LENGTH(fr.old_hospital)=10";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------32----------");
    }

    private void computequota_33() {
        String quotaId = "33";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where " +
                    " fr.apply_date<'"+tomorrow+"' and " +
                    " fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and " +
                    " fr.admin_team_id!=fr.old_admin_team_id and " +
                    " fr.hospital!=fr.old_hospital and " +
                    " LEFT(fr.hospital,6)!=LEFT(fr.old_hospital,6) and"+
                     "   LENGTH(fr.hospital)=10 and" +
                    "   LENGTH(fr.old_hospital)=10";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------33----------");
    }

    private void computequota_34() {
        String quotaId = "34";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where  " +
                    " fr.apply_date<'"+tomorrow+"' and " +
                    " fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and " +
                    " fr.admin_team_id!=fr.old_admin_team_id and" +
                    " LENGTH(fr.hospital)=10 and " +
                    " LENGTH(fr.old_hospital)=10";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------34----------");
    }

    private void computequota_35() {
        String quotaId = "35";
        try {
            String sql=" select fr.sign_code code,fr.old_hospital hospital,fr.old_admin_team_id admin_team_id from wlyy_sign_family_renew_log fr where    fr.apply_date<'"+ tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and fr.admin_team_id!=fr.old_admin_team_id  and LENGTH(fr.hospital)=10 and LENGTH(fr.old_hospital)=10 ";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------35----------");
    }
    private void computequota_38() {
        String quotaId = "38";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where   fr.apply_date<'"+tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and fr.admin_team_id!=fr.old_admin_team_id  and fr.hospital!=fr.old_hospital and LENGTH(fr.hospital)=10 and LENGTH(fr.old_hospital)=10";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------38----------");
    }

    private void computequota_39() {
        String quotaId = "39";
        try {
            String sql=" select fr.sign_code code,fr.old_hospital hospital,fr.old_admin_team_id admin_team_id from wlyy_sign_family_renew_log fr where   fr.apply_date<'"+ tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and fr.admin_team_id!=fr.old_admin_team_id and fr.hospital!=fr.old_hospital and LENGTH(fr.hospital)=10 and LENGTH(fr.old_hospital)=10 ";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------39----------");
    }
    public List<Map<String, Object>> getCityTowms() {
        List<Map<String, Object>> map = jdbcTemplate.queryForList(getAvgSqlByCode("city_town_SQL"));
        return map;
    }

    private void computequota_40() {
        String quotaId = "40";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where   fr.apply_date<'"+tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and fr.admin_team_id!=fr.old_admin_team_id and fr.hospital!=fr.old_hospital and LEFT(fr.hospital,6)!=LEFT(fr.old_hospital,6) and LENGTH(fr.hospital)=10 and LENGTH(fr.old_hospital)=10";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------40----------");
    }

    private void computequota_41() {
        String quotaId = "41";
        try {
            String sql=" select fr.sign_code code,fr.old_hospital hospital,fr.old_admin_team_id admin_team_id from wlyy_sign_family_renew_log fr where   fr.apply_date<'"+ tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and fr.admin_team_id!=fr.old_admin_team_id and fr.hospital!=fr.old_hospital and LEFT(fr.hospital,6)!=LEFT(fr.old_hospital,6)  and LENGTH(fr.hospital)=10 and LENGTH(fr.old_hospital)=10 ";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------41----------");
    }

    private void computequota_42() {
        String quotaId = "42";
        try {
            String sql=" select fr.sign_code code,fr.old_hospital hospital,fr.old_admin_team_id admin_team_id from wlyy_sign_family_renew_log fr where   fr.apply_date<'"+ tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and fr.admin_team_id=fr.old_admin_team_id  and LENGTH(fr.hospital)=10 and LENGTH(fr.old_hospital)=10 ";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------42----------");
    }
    private void computequota_43() {
        String quotaId = "43";
        try {
            String sql=" select fr.sign_code code,fr.old_hospital hospital,fr.old_admin_team_id admin_team_id from wlyy_sign_family_renew_log fr where   fr.apply_date<'"+ tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "'and  fr.hospital=fr.old_hospital  and LENGTH(fr.hospital)=10 and LENGTH(fr.old_hospital)=10 ";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------43----------");
    }
    private void computequota_44() {
        String quotaId = "44";
        try {
            String sql=" select fr.sign_code code,fr.old_hospital hospital,fr.old_admin_team_id admin_team_id from wlyy_sign_family_renew_log fr where  " +
                    " fr.apply_date<'"+ tomorrow+"' and " +
                    " fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "' and " +
                    " LEFT(fr.hospital,6)=LEFT(fr.old_hospital,6)  and " +
                    " LENGTH(fr.hospital)=10 and " +
                    " LENGTH(fr.old_hospital)=10 ";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------44----------");
    }
    private void computequota_45() {
        String quotaId = "45";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where  fr.hospital!=fr.old_hospital and LEFT(fr.hospital,6)=LEFT(fr.old_hospital,6)and fr.apply_date<'"+tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "'";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------45----------");
    }
    private void computequota_46() {
        String quotaId = "46";
        try {
            String sql="select fr.sign_code code,fr.hospital,fr.admin_team_id from wlyy_sign_family_renew_log fr where  LEFT(fr.hospital,6)!=LEFT(fr.old_hospital,6) and fr.apply_date<'"+tomorrow+"' and  fr.sign_year= '" + Constant.getNowYearByDate(tomorrow) + "'";
            //抽取數據
            List<SignFamilyRenew> signFamilies = SpringUtil.getBean(DBExtract.class).extract(SignFamilyRenew.class, sql);
            //清洗數據
            FilterModel etlModels = SpringUtil.getBean(RenewSignDataFilter.class).filter(signFamilies, sql, "now");
            //统计数据
            List<Map<String, List<ETLModel>>> returnDatas = SpringUtil.getBean(Level1Role.class).elt(etlModels.getEtlModelList());
            //保存数据
            SpringUtil.getBean(RedisStorage.class).saveByLevel1(returnDatas, null, quotaId);
            allContent.append(JsonUtil.objToStr(etlModels.getLogModel()));
        } catch (Exception e) {
            e.printStackTrace();
            allContent.append("统计失败:" + e.getMessage());
        }
        allContent.append("-----------46----------");
    }
    public List<Map<String, Object>> getCityHoss() {
        List<Map<String, Object>> map = jdbcTemplate.queryForList(getAvgSqlByCode("city_hos_SQL"));
        return map;
    }

    public List<Map<String, Object>> getTownHoss(String townCode) {
        List<Map<String, Object>> map = jdbcTemplate.queryForList(getAvgSqlByCode("town_hos_SQL"), townCode);
        return map;
    }

    public List<Map<String, Object>> getAllHosTeams() {
        List<Map<String, Object>> map = jdbcTemplate.queryForList(getAvgSqlByCode("team_SQL"));
        return map;
    }

    public List<Map<String, Object>> getAllHoss() {
        List<Map<String, Object>> map = jdbcTemplate.queryForList(getAvgSqlByCode("All_hos_SQL"));
        return map;
    }


    public String getAvgSqlByCode(String code) {

        if ("city_town_SQL".equals(code)) {
            //统计市级各区
            String city_town_SQL = "SELECT t.code,t.`name` ,ifnull(ROUND(AVG(s.score),1),0) as avgCount " +
                    " FROM wlyy_doctor d " +
                    " RIGHT JOIN dm_town t on t.code=d.town " +
                    " LEFT JOIN wlyy_evaluate_score s ON d.code = s.doctor " +
                    " WHERE t.city='" + Constant.city + "' GROUP BY d.town order by avgCount DESC ";
            return city_town_SQL;
        } else if ("city_hos_SQL".equals(code)) {
            //统计市级各社区
            String city_hos_SQL = "SELECT  t.code,t.`name`,ifnull(ROUND(AVG(s.score),1),0) as avgCount " +
                    " FROM wlyy_doctor d " +
                    " RIGHT JOIN dm_hospital t on t.code = d.hospital " +
                    " LEFT JOIN wlyy_evaluate_score s ON d.code = s.doctor " +
                    " WHERE t.level = 2 AND t.del='1' AND t.city='" + Constant.city + "' AND LENGTH(t.code)<=10 GROUP BY left(t.code,8) order by avgCount DESC ";
            return city_hos_SQL;
        } else if ("town_hos_SQL".equals(code)) {
            //统计区级各社区
            String town_hos_SQL = "SELECT  t.code,t.`name`,ifnull(ROUND(AVG(s.score),1),0) as avgCount " +
                    " FROM wlyy_doctor d " +
                    " RIGHT JOIN dm_hospital t on t.code = d.hospital " +
                    " LEFT JOIN wlyy_evaluate_score s ON d.code = s.doctor " +
                    " WHERE t.level = 2 AND t.del='1' AND t.town= ? AND LENGTH(t.code)<=10 GROUP BY left(t.code,8) order by avgCount DESC ";
            return town_hos_SQL;
        } else if ("team_SQL".equals(code)) {
            //查找所有团队
            String team_SQL = "SELECT  t.id,t.`name` ,d.hospital,ifnull(ROUND(AVG(s.score),1),0) as avgCount " +
                    " FROM wlyy_doctor d " +
                    " RIGHT JOIN wlyy_admin_team t on t.leader_code = d.code " +
                    " LEFT JOIN wlyy_evaluate_score s ON d.code = s.doctor " +
                    " WHERE LENGTH(d.hospital)<=10 " +
                    " GROUP BY t.id,left(d.hospital,8) order by avgCount DESC ";
            return team_SQL;
        } else if ("All_hos_SQL".equals(code)) {
            //查找所有机构
            String All_hos_SQL = "SELECT t.code FROM dm_hospital t where t.level = 2 AND t.del='1' AND LENGTH(t.code)<=10 AND t.city ='" + Constant.city + "' GROUP BY left(t.code,8) ";
            return All_hos_SQL;
        }
        return "";
    }

    public static String getDayString(Integer size) {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, size);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

}
