package com.yihu.wlyy.service.app.statistics;

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.entity.statistics.PopulationBase;
import com.yihu.wlyy.repository.address.CityDao;
import com.yihu.wlyy.repository.address.TownDao;
import com.yihu.wlyy.repository.doctor.DoctorAdminTeamDao;
import com.yihu.wlyy.repository.doctor.DoctorDao;
import com.yihu.wlyy.repository.organization.HospitalDao;
import com.yihu.wlyy.repository.statistics.PopulationBaseDao;
import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.DateUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lyr-pc on 2016/10/21.
 */
@Service
public class StatisticsAllService extends BaseService {
    @Autowired
    CityDao cityDao;
    @Autowired
    TownDao townDao;
    @Autowired
    DoctorDao doctorDao;
    @Autowired
    HospitalDao hospitalDao;
    @Autowired
    DoctorAdminTeamDao adminTeamDao;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    PopulationBaseDao peopleNumDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取时间戳
     *
     * @return
     */
    public String getQuotaTimeStamp() {
        return redisTemplate.opsForValue().get("quota:timeKey");
    }

    /**
     * 获取一级指标达到量
     *
     * @param endDate 截止日期
     * @param area    区域
     * @param index   指标
     * @param level   级别
     * @return
     */
    public long getIndexTotal(String endDate, String area, int level, String index) {
        long total = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        boolean preFlag = false;
        String preDate = endDate;

        if (endDate.compareTo(dateFormat.format(new Date())) >= 0) {
            String val = "";
            try {
                val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area + ":" + getQuotaTimeStamp());
            } catch (Exception e) {
                val = "";
            }
            if (!StringUtils.isEmpty(val)) {
                JSONObject valJson = new JSONObject(val);
                if (valJson.has("num")) {
                    total = valJson.getInt("num");
                }
            } else {
                preFlag = true;
                Calendar today = Calendar.getInstance();
                today.add(Calendar.DATE, -1);
                preDate = dateFormat.format(today.getTime());
            }
        } else {
            preFlag = true;
        }
        if (preFlag) {
            // 查询语句
            String sql = " select " +
                    "     ifnull(result,'0') amount" +
                    " from  " +
                    "     wlyy_quota_result " +
                    " where " +
                    "     quato_code = ? " +
                    "   and level1_type = ? and del = '1'" +
                    "   and quota_date = ? ";

            if (level == 4) {
                // 市级别
                sql += " and city = ? ";
            } else if (level == 3) {
                // 区、城镇级别
                sql += " and town = ? ";
            } else if (level == 2) {
                // 机构级别
                sql += " and org_code = ? ";
            } else if (level == 1) {
                sql += " and qkdoctor_code = ?";
            }

            List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, new Object[]{index, level, preDate, area});

            if (result != null && result.size() > 0) {
                total = (result.get(0).get("amount") != null ? Long.valueOf(result.get(0).get("amount").toString()) : 0L);
            }
        }

        return total;
    }

    /**
     * 获取三师转签或高危人群签约
     *
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    public JSONObject getSszqAndGwrq(String endDate, String area, int level, String index) {
        JSONObject json = new JSONObject();

        // 查询语句
        String sql = " select " +
                "     ifnull(result,'0') amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = ? " +
                "   and level1_type = ? and del = '1'" +
                "   and quota_date = ? ";

        if (level == 4) {
            // 市级别
            sql += " and city = ? ";
        } else if (level == 3) {
            // 区、城镇级别
            sql += " and town = ? ";
        } else if (level == 2) {
            // 机构级别
            sql += " and org_code = ? ";
        } else if (level == 1) {
            sql += " and qkdoctor_code = ?";
        }

        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, new Object[]{index, level, endDate, area});

        if (result != null && result.size() > 0) {
            String total = (result.get(0).get("amount") != null ? result.get(0).get("amount").toString() : "0");

            if (total.equals("0")) {
                json.put("amount", 0L);
                json.put("num", 0L);
                json.put("rate", 0.0000D);
            } else {
                DecimalFormat df = new DecimalFormat("0.0000");
                String[] args = total.split("/");
                json.put("amount", Long.valueOf(args[0]));
                json.put("num", Long.valueOf(args[1]));
                json.put("rate", df.format(Long.valueOf(args[0]) * 1.0000 / Long.valueOf(args[1]) * 100));
            }
        } else {
            json.put("amount", 0L);
            json.put("num", 0L);
            json.put("rate", 0.0000D);
        }

        return json;
    }

    /**
     * 获取缴费
     *
     * @param endDate 截止日期
     * @param area    区域
     * @param level   级别
     * @return
     */
    public long getWeiJiaoFei(String endDate, String area, int level) {
        long total = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        boolean preFlag = false;
        String preDate = endDate;

        if (endDate.compareTo(dateFormat.format(new Date())) >= 0) {
            String val = "";
            try {
                val = redisTemplate.opsForValue().get("quota:16:" + level + ":" + area + ":0:" + getQuotaTimeStamp());
            } catch (Exception e) {
                val = "";
            }
            if (!StringUtils.isEmpty(val)) {
                JSONObject valJson = new JSONObject(val);
                if (valJson.has("num")) {
                    total = valJson.getInt("num");
                }
            } else {
                preFlag = true;
                Calendar today = Calendar.getInstance();
                today.add(Calendar.DATE, -1);
                preDate = dateFormat.format(today.getTime());
            }
        } else {
            preFlag = true;
        }
        if (preFlag) {
            // 查询语句
            String sql = " select " +
                    "     ifnull(result,'0') amount" +
                    " from  " +
                    "     wlyy_quota_result " +
                    " where " +
                    "     quato_code = '16' " +
                    "   and level1_type = ? and del = '1'" +
                    "   and level2_type = '0' " +
                    "   and quota_date = ? ";

            if (level == 4) {
                // 市级别
                sql += " and city = ? ";
            } else if (level == 3) {
                // 区、城镇级别
                sql += " and town = ? ";
            } else if (level == 2) {
                // 机构级别
                sql += " and org_code = ? ";
            } else if (level == 1) {
                sql += " and qkdoctor_code = ?";
            }

            List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, new Object[]{level, preDate, area});

            if (result != null && result.size() > 0) {
                total = (result.get(0).get("amount") != null ? Long.valueOf(result.get(0).get("amount").toString()) : 0L);
            }
        }

        return total;
    }

    /**
     * 查询截止某个日期累计签约率
     *
     * @param endDate 截止日期
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public JSONObject getSignRate(String endDate, String area, int level) throws Exception {
        JSONObject json = new JSONObject();
        long signAmount = getIndexTotal(endDate, area, level, "13");
        PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(area, Calendar.getInstance().get(Calendar.YEAR));
        DecimalFormat df = new DecimalFormat("0.0000");

        if (peopleNum != null && peopleNum.getNum() > 0) {
            json.put("rate", df.format((signAmount * 1.0000) / peopleNum.getNum() * 100));
            json.put("sign", signAmount);
            json.put("people", peopleNum.getNum());
        } else {
            json.put("rate", "0.0000");
            json.put("sign", signAmount);
            json.put("people", 0);
        }

        return json;
    }

    /**
     * 查询截止某个日期签约完成率
     *
     * @param endDate 截止日期
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public JSONObject getSignTaskRate(String endDate, String area, int level) throws Exception {
        JSONObject json = new JSONObject();
        long signAmount = getIndexTotal(endDate, area, level, "13");
        PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(area, Calendar.getInstance().get(Calendar.YEAR));
        DecimalFormat df = new DecimalFormat("0.0000");

        if (peopleNum != null && peopleNum.getTaskNum() > 0) {
            json.put("rate", df.format((signAmount * 1.0000) / peopleNum.getTaskNum() * 100));
            json.put("sign", signAmount);
            json.put("people", peopleNum.getTaskNum());
        } else {
            json.put("rate", "0.0000");
            json.put("sign", signAmount);
            json.put("people", 0);
        }

        return json;
    }


    /**
     * 获取签约分组信息
     *
     * @param endDate
     * @param lowCode
     * @param area
     * @param level
     * @return
     */
    public JSONObject getGroupInfo(String endDate, String lowCode, String area, int level) {
        JSONObject json = new JSONObject();
        long total = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        boolean preFlag = false;
        String preDate = endDate;

        if (endDate.compareTo(dateFormat.format(new Date())) >= 0) {
            String val = "";
            try {
                val = redisTemplate.opsForValue().get("quota:17:" + level + ":" + area + ":" + lowCode + ":" + getQuotaTimeStamp());
            } catch (Exception e) {
                val = "";
            }
            if (!StringUtils.isEmpty(val)) {
                JSONObject valJson = new JSONObject(val);
                if (valJson.has("num")) {
                    total = valJson.getInt("num");
                }
            } else {
                preFlag = true;
                Calendar today = Calendar.getInstance();
                today.add(Calendar.DATE, -1);
                preDate = dateFormat.format(today.getTime());
            }
        } else {
            preFlag = true;
        }
        if (preFlag) {
            // 查询语句
            String sql = " select " +
                    "     ifnull(result,'0') amount" +
                    " from  " +
                    "     wlyy_quota_result " +
                    " where " +
                    "     quato_code = '17' " +
                    "   and level1_type = ? and del = '1'" +
                    "   and quota_date = ? " +
                    "   and level2_type = ? ";

            if (level == 4) {
                // 市级别
                sql += " and city = ? ";
            } else if (level == 3) {
                // 区、城镇级别
                sql += " and town = ? ";
            } else if (level == 2) {
                // 机构级别
                sql += " and org_code = ? ";
            } else if (level == 1) {
                sql += " and qkdoctor_code = ? ";
            }

            List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, new Object[]{level, preDate, lowCode, area});

            if (result != null && result.size() > 0) {
                total = (result.get(0).get("amount") != null ? Long.valueOf(result.get(0).get("amount").toString()) : 0L);
            }
        }

        int num = 0;
        int taskNum = 0;
        String redisNum = "";
        try {
            redisNum = redisTemplate.opsForValue().get("people:num:" + area);
        } catch (Exception e) {
            redisNum = "";
        }
        if (StringUtils.isEmpty(redisNum)) {
            PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(area, Calendar.getInstance().get(Calendar.YEAR));
            if (peopleNum != null) {
                if (lowCode.equals("3")) {
                    num = peopleNum.getSixFiveNum();
                    taskNum = peopleNum.getSixFiveTaskNum();
                } else if (lowCode.equals("1")) {
                    num = peopleNum.getGxyNum();
                    taskNum = peopleNum.getGxyTaskNum();
                } else if (lowCode.equals("2")) {
                    num = peopleNum.getTnbNum();
                    taskNum = peopleNum.getTnbTaskNum();
                }
            }
        } else {
            JSONObject peopleNum = new JSONObject(redisNum);
            if (peopleNum != null) {

                if (lowCode.equals("3")) {
                    num = peopleNum.getInt("sixFiveNum");
                    taskNum = peopleNum.getInt("sixFiveTaskNum");
                } else if (lowCode.equals("1")) {
                    num = peopleNum.getInt("gxyNum");
                    taskNum = peopleNum.getInt("gxyTaskNum");
                } else if (lowCode.equals("2")) {
                    num = peopleNum.getInt("tnbNum");
                    taskNum = peopleNum.getInt("tnbTaskNum");
                }
            }
        }

        DecimalFormat df = new DecimalFormat("0.0000");
        json.put("sign", total);
        JSONObject signRate = new JSONObject();
        signRate.put("sign", total);
        signRate.put("people", num);
        signRate.put("rate", df.format((total * 1.0000) / num * 100));
        json.put("signRate", signRate);
        JSONObject signTaskRate = new JSONObject();
        signTaskRate.put("sign", total);
        signTaskRate.put("people", taskNum);
        signTaskRate.put("rate", df.format((total * 1.0000) / taskNum * 100));
        json.put("signTaskRate", signTaskRate);

        return json;
    }

    /**
     * 查询某个级别的某个指标到达量
     *
     * @param date
     * @param area
     * @param level
     * @param index
     * @param sort
     * @param lowLevel
     * @return
     * @throws Exception
     */
    public JSONArray getLevelTwoLowLevelTotal(String date, String area, int level, String index, int sort, String lowLevel, String lowCode) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String low_level = String.valueOf(StringUtils.isEmpty(lowLevel) ? (level - 1) : lowLevel);
        List<Map<String, Object>> resultList = new ArrayList<>();

        if (date.compareTo(dateFormat.format(new Date())) >= 0) {
            resultList = getLevelTwoTodayLowLevelTotal(area, level, index, sort, lowLevel, lowCode);
        } else {
            resultList = getLevelTwoTodayBeforeLowLevelTotal(date, area, level, index, sort, lowLevel, lowCode);
        }

        // 结果为空时,自建结果集
        if (resultList == null || resultList.size() < 1) {
            resultList = getLowLevelMapKey(level, low_level, area);
        }

        if (resultList != null) {
            DecimalFormat df = new DecimalFormat("0.0000");
            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") != null ? Long.valueOf(map.get("amount").toString()) : 0L);

                if (!low_level.equals("1")) {
                    String redisNum = "";
                    try {
                        redisNum = redisTemplate.opsForValue().get("people:num:" + map.get("code").toString());
                    } catch (Exception e) {
                        redisNum = "";
                    }
                    if (StringUtils.isEmpty(redisNum)) {
                        PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(map.get("code").toString(), Calendar.getInstance().get(Calendar.YEAR));
                        if (peopleNum != null) {
                            int num = 0;
                            int taskNum = 0;

                            if (lowCode.equals("3")) {
                                num = peopleNum.getSixFiveNum();
                                taskNum = peopleNum.getSixFiveTaskNum();
                            } else if (lowCode.equals("1")) {
                                num = peopleNum.getGxyNum();
                                taskNum = peopleNum.getGxyTaskNum();
                            } else if (lowCode.equals("2")) {
                                num = peopleNum.getTnbNum();
                                taskNum = peopleNum.getTnbTaskNum();
                            }
                            map.put("rate", df.format(((long) map.get("amount") * 1.0000) / num * 100));
                            map.put("taskRate", df.format(((long) map.get("amount") * 1.0000) / taskNum * 100));
                            map.put("targetRate", df.format(taskNum * 1.0000 / num * 100));
                            map.put("num", num);
                            map.put("task", taskNum);
                        }
                    } else {
                        JSONObject peopleNum = new JSONObject(redisNum);
                        if (peopleNum != null) {
                            int num = 0;
                            int taskNum = 0;

                            if (lowCode.equals("3")) {
                                num = peopleNum.getInt("sixFiveNum");
                                taskNum = peopleNum.getInt("sixFiveTaskNum");
                            } else if (lowCode.equals("1")) {
                                num = peopleNum.getInt("gxyNum");
                                taskNum = peopleNum.getInt("gxyTaskNum");
                            } else if (lowCode.equals("2")) {
                                num = peopleNum.getInt("tnbNum");
                                taskNum = peopleNum.getInt("tnbTaskNum");
                            }
                            map.put("rate", df.format(((long) map.get("amount") * 1.0000) / num * 100));
                            map.put("taskRate", df.format(((long) map.get("amount") * 1.0000) / taskNum * 100));
                            map.put("targetRate", df.format(taskNum * 1.0000 / num * 100));
                            map.put("num", num);
                            map.put("task", taskNum);
                        }
                    }
                }
            }

            Collections.sort(resultList, new Comparator<Map<String, Object>>() {
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    long map1value = (long) o1.get("amount");
                    long map2value = (long) o2.get("amount");

                    if (map1value - map2value > 0) {
                        return sort == 1 ? -1 : 1;
                    } else if (map1value - map2value < 0) {
                        return sort == 1 ? 1 : -1;
                    } else {
                        return 0;
                    }
                }
            });

            return new JSONArray(resultList);
        } else {
            return new JSONArray();
        }
    }

    /**
     * 查询某个级别的某个指标到达量
     *
     * @param date
     * @param area
     * @param level
     * @param index
     * @param sort
     * @param lowLevel
     * @return
     * @throws Exception
     */
    public JSONArray getLowLevelTotal(String date, String area, int level, String index, int sort, String lowLevel) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String low_level = String.valueOf(StringUtils.isEmpty(lowLevel) ? (level - 1) : lowLevel);
        List<Map<String, Object>> resultList = new ArrayList<>();

        if (date.compareTo(dateFormat.format(new Date())) >= 0) {
            resultList = getTodayLowLevelTotal(area, level, index, sort, lowLevel);
        } else {
            resultList = getTodayBeforeLowLevelTotal(date, area, level, index, sort, lowLevel);
        }

        // 结果为空时,自建结果集
        if (resultList == null || resultList.size() < 1) {
            resultList = getLowLevelMapKey(level, low_level, area);
        }

        if (resultList != null) {
            DecimalFormat df = new DecimalFormat("0.0000");
            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") != null ? Long.valueOf(map.get("amount").toString()) : 0L);

                if (!low_level.equals("1")) {
                    String redisNum = "";
                    try {
                        redisNum = redisTemplate.opsForValue().get("people:num:" + map.get("code").toString());
                    } catch (Exception e) {
                        redisNum = "";
                    }
                    if (StringUtils.isEmpty(redisNum)) {
                        PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(map.get("code").toString(), Calendar.getInstance().get(Calendar.YEAR));
                        if (peopleNum != null) {
                            map.put("rate", df.format(((long) map.get("amount") * 1.0000) / peopleNum.getNum() * 100));
                            map.put("taskRate", df.format(((long) map.get("amount") * 1.0000) / peopleNum.getTaskNum() * 100));
                            map.put("targetRate", df.format(peopleNum.getTaskNum() * 1.0000 / peopleNum.getNum() * 100));
                            map.put("num", peopleNum.getNum());
                            map.put("task", peopleNum.getTaskNum());
                        }
                    } else {
                        JSONObject peopleNum = new JSONObject(redisNum);
                        if (peopleNum != null) {
                            map.put("rate", df.format(((long) map.get("amount") * 1.0000) / peopleNum.getInt("num") * 100));
                            map.put("taskRate", df.format(((long) map.get("amount") * 1.0000) / peopleNum.getInt("taskNum") * 100));
                            map.put("targetRate", df.format(peopleNum.getInt("taskNum") * 1.0000 / peopleNum.getInt("num") * 100));
                            map.put("num", peopleNum.getInt("num"));
                            map.put("task", peopleNum.getInt("taskNum"));
                        }
                    }
                }
            }

            Collections.sort(resultList, new Comparator<Map<String, Object>>() {
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    long map1value = (long) o1.get("amount");
                    long map2value = (long) o2.get("amount");

                    if (map1value - map2value > 0) {
                        return sort == 1 ? -1 : 1;
                    } else if (map1value - map2value < 0) {
                        return sort == 1 ? 1 : -1;
                    } else {
                        return 0;
                    }
                }
            });

            return new JSONArray(resultList);
        } else {
            return new JSONArray();
        }
    }


    /**
     * 查询三师转签率、高危人群签约率
     *
     * @param date
     * @param area
     * @param level
     * @param index
     * @param sort
     * @param lowLevel
     * @return
     * @throws Exception
     */
    public JSONArray getLowLevelTotalSpecial(String date, String area, int level, String index, int sort, String lowLevel) throws Exception {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String low_level = String.valueOf(StringUtils.isEmpty(lowLevel) ? (level - 1) : lowLevel);
        String areaField = "";
        String lowLevelField = "";
        String lowLevelName = "";

        if (level == 4) {
            // 市级别
            areaField = "city";
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        } else if (level == 1) {
            throw new Exception("param level error");
        }

        if (low_level.equals("3")) {
            lowLevelField = "town";
            lowLevelName = "town_name";
        } else if (low_level.equals("2")) {
            lowLevelField = "org_code";
            lowLevelName = "org_name";
        } else if (low_level.equals("1")) {
            lowLevelField = "qkdoctor_code";
            lowLevelName = "qkdoctor_name";
        } else {
            throw new Exception("param lowLevel error");
        }

        // 查询语句
        String sql = " select " +
                "     ifnull(" + lowLevelField + ",'') code " +
                "     ,ifnull(" + lowLevelName + ",'') 'name' " +
                "     ,ifnull(result,'0') amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = ? and del = '1'" +
                "   and quota_date = ? " +
                "   and " + areaField + " = ? ";

        resultList = jdbcTemplate.queryForList(sql, new Object[]{low_level, date, area});

        if (resultList == null || resultList.size() < 1) {
            resultList = getLowLevelMapKey(level, low_level, area);
        }

        for (Map<String, Object> map : resultList) {
            if (map.get("amount").toString().equals("0")) {
                map.put("amount", 0L);
                map.put("num", 0);
                map.put("rate", 0.0000D);
            } else {
                DecimalFormat df = new DecimalFormat("0.0000");
                String[] args = map.get("amount").toString().split("/");

                map.put("amount", Long.valueOf(args[0]));
                map.put("num", Long.valueOf(args[1]));
                map.put("rate", df.format((Long.valueOf(args[0]) * 1.0000) / Long.valueOf(args[1]) * 100));
            }
        }

        Collections.sort(resultList, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String map1value = o1.get("rate").toString();
                String map2value = o2.get("rate").toString();

                if (map1value.compareTo(map2value) > 0) {
                    return sort == 1 ? -1 : 1;
                } else if (map1value.compareTo(map2value) < 0) {
                    return sort == 1 ? 1 : -1;
                } else {
                    return 0;
                }
            }
        });

        return new JSONArray(resultList);
    }


    /**
     * 获取过去某个日期某个区域一级指标的下级统计
     *
     * @param date
     * @param area
     * @param level
     * @param index
     * @param lowLevel
     * @return
     */
    public List<Map<String, Object>> getLevelTwoTodayBeforeLowLevelTotal(String date, String area, int level, String index, int sort, String lowLevel, String lowCode) throws Exception {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String low_level = String.valueOf(StringUtils.isEmpty(lowLevel) ? (level - 1) : lowLevel);
        String areaField = "";
        String lowLevelField = "";
        String lowLevelName = "";

        if (level == 4) {
            // 市级别
            areaField = "city";
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        } else if (level == 1) {
            throw new Exception("param level error");
        }

        if (low_level.equals("3")) {
            lowLevelField = "town";
            lowLevelName = "town_name";
        } else if (low_level.equals("2")) {
            lowLevelField = "org_code";
            lowLevelName = "org_name";
        } else if (low_level.equals("1")) {
            lowLevelField = "qkdoctor_code";
            lowLevelName = "qkdoctor_name";
        } else {
            throw new Exception("param lowLevel error");
        }

        // 查询语句
        String sql = " select " +
                "     ifnull(" + lowLevelField + ",'') code " +
                "     ,ifnull(" + lowLevelName + ",'') 'name' " +
                "     ,ifnull(result,'0') amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = ? and del = '1'" +
                "   and quota_date = ? " +
                "   and " + areaField + " = ? " +
                "   and level2_type = ? ";

        if (sort == 1) {
            sql += " order by amount desc ";
        } else {
            sql += " order by amount asc ";
        }

        resultList = jdbcTemplate.queryForList(sql, new Object[]{low_level, date, area, lowCode});

        return resultList;
    }

    /**
     * 获取过去某个日期某个区域一级指标的下级统计
     *
     * @param date
     * @param area
     * @param level
     * @param index
     * @param lowLevel
     * @return
     */
    public List<Map<String, Object>> getTodayBeforeLowLevelTotal(String date, String area, int level, String index, int sort, String lowLevel) throws Exception {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String low_level = String.valueOf(StringUtils.isEmpty(lowLevel) ? (level - 1) : lowLevel);
        String areaField = "";
        String lowLevelField = "";
        String lowLevelName = "";

        if (level == 4) {
            // 市级别
            areaField = "city";
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        } else if (level == 1) {
            throw new Exception("param level error");
        }

        if (low_level.equals("3")) {
            lowLevelField = "town";
            lowLevelName = "town_name";
        } else if (low_level.equals("2")) {
            lowLevelField = "org_code";
            lowLevelName = "org_name";
        } else if (low_level.equals("1")) {
            lowLevelField = "qkdoctor_code";
            lowLevelName = "qkdoctor_name";
        } else {
            throw new Exception("param lowLevel error");
        }

        // 查询语句
        String sql = " select " +
                "     ifnull(" + lowLevelField + ",'') code " +
                "     ,ifnull(" + lowLevelName + ",'') 'name' " +
                "     ,ifnull(result,'0') amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = ? and del = '1'" +
                "   and quota_date = ? " +
                "   and " + areaField + " = ? ";

        if (sort == 1) {
            sql += " order by amount desc";
        } else {
            sql += " order by amount asc ";
        }

        resultList = jdbcTemplate.queryForList(sql, new Object[]{low_level, date, area});

        return resultList;
    }


    /**
     * 获取今天某个区域一级指标的下级统计
     *
     * @param area
     * @param level
     * @param index
     * @param lowLevel
     * @return
     */
    public List<Map<String, Object>> getLevelTwoTodayLowLevelTotal(String area, int level, String index, int sort, String lowLevel, String lowCode) throws Exception {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String low_level = String.valueOf(StringUtils.isEmpty(lowLevel) ? (level - 1) : lowLevel);
        resultList = getLowLevelMapKey(level, low_level, area);
        boolean flag = false;

        for (Map<String, Object> map : resultList) {
            //String redisData = redisTemplate.opsForValue().get("quota:" + index + ":" + low_level + ":" + String.valueOf(map.get("code")) + ":" + lowCode + ":" + getQuotaTimeStamp());
            String redisData = "";
            try {
                redisData = redisTemplate.opsForValue().get("quota:" + index + ":" + low_level + ":" + String.valueOf(map.get("code")) + ":" + lowCode + ":" + getQuotaTimeStamp());
            } catch (Exception e) {
                redisData = "";
            }
            if (!StringUtils.isEmpty(redisData)) {
                JSONObject json = new JSONObject(redisData);
                map.put("amount", Long.valueOf(String.valueOf(json.get("num"))));
            } else {
                flag = true;
                resultList = new ArrayList<>();
                break;
            }
        }

        if (flag) {
            Calendar today = Calendar.getInstance();
            today.add(Calendar.DATE, -1);
            String preDate = new SimpleDateFormat("yyyy-MM-dd").format(today.getTime());
            resultList = getLevelTwoTodayBeforeLowLevelTotal(preDate, area, level, index, sort, lowLevel, lowCode);
        }

        return resultList;
    }

    /**
     * 获取今天某个区域一级指标的下级统计
     *
     * @param area
     * @param level
     * @param index
     * @param lowLevel
     * @return
     */
    public List<Map<String, Object>> getTodayLowLevelTotal(String area, int level, String index, int sort, String lowLevel) throws Exception {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String low_level = String.valueOf(StringUtils.isEmpty(lowLevel) ? (level - 1) : lowLevel);
        String redisData = "";
        try {
            redisData = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area + ":" + low_level + ":" + getQuotaTimeStamp());
        } catch (Exception e) {
            redisData = "";
        }
        if (!StringUtils.isEmpty(redisData)) {
            JSONArray jsonArray = new JSONArray(redisData);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = jsonArray.getJSONObject(i);
                Map<String, Object> data = new HashMap<>();

                data.put("code", String.valueOf(json.get("code")));
                data.put("name", String.valueOf(json.get("name")));
                data.put("amount", Long.valueOf(String.valueOf(json.get("num"))));

                resultList.add(data);
            }
        } else {
            Calendar today = Calendar.getInstance();
            today.add(Calendar.DATE, -1);
            String preDate = new SimpleDateFormat("yyyy-MM-dd").format(today.getTime());
            resultList = getTodayBeforeLowLevelTotal(preDate, area, level, index, sort, lowLevel);
        }

        if (resultList.size() < 1) {
            resultList = getLowLevelMapKey(level, low_level, area);
        }

        return resultList;
    }


    /**
     * 获取下级统计key
     *
     * @param level
     * @param lowLevel
     * @param area
     * @return
     */
    public List<Map<String, Object>> getLowLevelMapKey(int level, String lowLevel, String area) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (lowLevel.equals("3")) {
            List<Town> towns = townDao.findByCityCode(area);
            if (towns != null) {
                for (Town town : towns) {
                    Map<String, Object> obj = new HashMap<>();
                    obj.put("code", town.getCode());
                    obj.put("name", town.getName());
                    obj.put("amount", "0");
                    resultList.add(obj);
                }
            }
        } else if (lowLevel.equals("2")) {
            List<Town> towns = new ArrayList<>();
            if (level == 4) {
                towns = townDao.findByCityCode(area);
            } else if (level == 3) {
                Town town = townDao.findByCode(area);
                if(town != null) {
                    towns.add(town);
                }
            }

            if (towns != null && towns.size() > 0) {
                for (Town town : towns) {
                    List<Hospital> hospitals = hospitalDao.findByTownCode(town.getCode());

                    for (Hospital hos : hospitals) {
                        if (hos.getCode().length() > 10) {
                            continue;
                        }
                        Map<String, Object> obj = new HashMap<>();
                        obj.put("code", hos.getCode());
                        obj.put("name", hos.getName());
                        obj.put("amount", "0");
                        resultList.add(obj);
                    }
                }
            }
        } else if (lowLevel.equals("1")) {
            List<Hospital> hospitals = new ArrayList<>();
            if (level == 4) {
                hospitals = hospitalDao.findByCity(area);
            } else if (level == 3) {
                hospitals = hospitalDao.findByTownCode(area);
            } else {
                Hospital hos = hospitalDao.findByCode(area);
                if (hos != null) {
                    hospitals.add(hos);
                }
            }

            if (hospitals != null && hospitals.size() > 0) {
                for (Hospital hos : hospitals) {
                    if (hos.getCode().length() > 10) {
                        continue;
                    }
                    List<AdminTeam> teams = adminTeamDao.findByOrgCode(hos.getCode());

                    for (AdminTeam team : teams) {
                        Map<String, Object> obj = new HashMap<>();
                        obj.put("code", String.valueOf(team.getId()));
                        obj.put("name", team.getName());
                        obj.put("amount", "0");
                        resultList.add(obj);
                    }
                }
            }
        }

        return resultList;
    }


    /**
     * 获取二级指标的到达量
     *
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    public JSONArray getIndexLevelTwototal(String endDate, String area, int level, String index) {
        String areaField = "";
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, Object>> resultList = new ArrayList<>();

        if (endDate.compareTo(datef.format(new Date())) >= 0) {
            resultList = getTodayLevelTwoTotal(area, level, index);
        } else {
            resultList = getTodyBeforeLevelTwototal(endDate, area, level, index);
        }

        if (resultList != null) {
            long total = 0;

            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") == null ? 0 : Math.round(Double.valueOf(map.get("amount").toString())));

                if (index.equals("7")) {
                    // 分组指标总数算法
                    String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
                    if (code.equals("0") || code.equals("1") || code.equals("2") || code.equals("3") || code.equals("7")) {
                        total += (long) map.get("amount");
                    }
                } else if (index.equals("16")) {
                    String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";

                    if (!code.equals("2")) {
                        total += (long) map.get("amount");
                    }
                } else {
                    total += (long) map.get("amount");
                }
            }

            if (!index.equals("8")) {
                DecimalFormat df = new DecimalFormat("0.0000");
                for (Map<String, Object> map : resultList) {
                    double rateG = (total > 0 ? ((long) map.get("amount")) * 1.0000 / total * 100 : 0);
                    map.put("rate", df.format(rateG));
                }
            }
            if (index.equals("6") && resultList.size() > 0) {
                int i = 0;
                boolean flag = false;
                for (; i < resultList.size(); i++) {
                    if (resultList.get(i).get("code") != null && String.valueOf(resultList.get(i).get("code")).equals("3")
                            && String.valueOf(resultList.get(i).get("amount")).equals("0")) {
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    resultList.remove(i);
                }
            }
            return new JSONArray(resultList);
        } else {
            return new JSONArray();
        }
    }

    /**
     * 获取二级指标的到达量
     *
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    public List<Map<String, Object>> getTodyBeforeLevelTwototal(String endDate, String area, int level, String index) {
        String areaField = "";
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");

        if (level == 4) {
            // 市级别
            areaField = "city";
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        } else if (level == 1) {
            // 团队
            areaField = "qkdoctor_code";
        }
        // 查询语句
        String sql = " select " +
                "     ifnull(level2_type,'') code " +
                "     ,ifnull(level2_type_name,'') 'name' " +
                "     ,ifnull(result,0.0) amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = ? and del = '1'";


        sql += "   and quota_date = ? " +
                "   and " + areaField + " = ? " +
                " group by level2_type,level2_type_name";

        List<Map<String, Object>> resultList = new ArrayList<>();

        resultList = jdbcTemplate.queryForList(sql, new Object[]{level, endDate, area});

        if (resultList == null || resultList.size() < 1) {
            getLevelTwoMapKey(index);
        }

        return resultList;
    }


    /**
     * 查询某个指标的二级维度
     *
     * @param area
     * @param level
     * @param index
     * @return
     */
    public List<Map<String, Object>> getTodayLevelTwoTotal(String area, int level, String index) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String redisData = "";
        try {
            redisData = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area + ":" + getQuotaTimeStamp());
        } catch (Exception e) {
            redisData = "";
        }
        if (!StringUtils.isEmpty(redisData)) {
            JSONArray jsonArray = new JSONArray(redisData);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = jsonArray.getJSONObject(i);
                Map<String, Object> data = new HashMap<>();

                data.put("code", String.valueOf(json.get("code")));
                data.put("name", String.valueOf(json.get("name")));
                data.put("amount", Long.valueOf(String.valueOf(json.get("num"))));

                resultList.add(data);
            }
        } else {
            Calendar today = Calendar.getInstance();
            today.add(Calendar.DATE, -1);
            String preDate = new SimpleDateFormat("yyyy-MM-dd").format(today.getTime());
            resultList = getTodyBeforeLevelTwototal(preDate, area, level, index);
        }

        if (resultList.size() < 1) {
            resultList = getLevelTwoMapKey(index);
        }

        return resultList;
    }

    /**
     * 获取某个二级指标的统计key
     *
     * @param index
     * @return
     */
    public List<Map<String, Object>> getLevelTwoMapKey(String index) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (index.equals("6")) {
            Map<String, Object> women = new HashMap<>();
            women.put("code", "1");
            women.put("name", "女");
            women.put("amount", Double.valueOf("0.0"));
            resultList.add(women);

            Map<String, Object> man = new HashMap<>();
            man.put("code", "2");
            man.put("name", "男");
            man.put("amount", Double.valueOf("0.0"));
            resultList.add(man);

            Map<String, Object> unknown = new HashMap<>();
            unknown.put("code", "3");
            unknown.put("name", "未知");
            unknown.put("amount", Double.valueOf("0.0"));
            resultList.add(unknown);
        } else if (index.equals("7")) {
            Map<String, Object> nogroup = new HashMap<>();
            nogroup.put("code", "0");
            nogroup.put("name", "未分组");
            nogroup.put("amount", Double.valueOf("0.0"));
            resultList.add(nogroup);

            Map<String, Object> normal = new HashMap<>();
            normal.put("code", "1");
            normal.put("name", "普通人群");
            normal.put("amount", Double.valueOf("0.0"));
            resultList.add(normal);

            Map<String, Object> manbing = new HashMap<>();
            manbing.put("code", "2");
            manbing.put("name", "慢病人群65岁以下");
            manbing.put("amount", Double.valueOf("0.0"));
            resultList.add(manbing);

            Map<String, Object> upsixfive = new HashMap<>();
            upsixfive.put("code", "3");
            upsixfive.put("name", "65岁以上人群");
            upsixfive.put("amount", Double.valueOf("0.0"));
            resultList.add(upsixfive);

            Map<String, Object> gxy = new HashMap<>();
            gxy.put("code", "4");
            gxy.put("name", "高血压");
            gxy.put("amount", Double.valueOf("0.0"));
            resultList.add(gxy);

            Map<String, Object> tnb = new HashMap<>();
            tnb.put("code", "5");
            tnb.put("name", "糖尿病");
            tnb.put("amount", Double.valueOf("0.0"));
            resultList.add(tnb);

            Map<String, Object> tnbGxy = new HashMap<>();
            tnbGxy.put("code", "6");
            tnbGxy.put("name", "高血压和糖尿病");
            tnbGxy.put("amount", Double.valueOf("0.0"));
            resultList.add(tnb);

            Map<String, Object> mb65UpGxy = new HashMap<>();
            mb65UpGxy.put("code", "7");
            mb65UpGxy.put("name", "慢病人群65岁以上");
            mb65UpGxy.put("amount", Double.valueOf("0.0"));
            resultList.add(mb65UpGxy);
        } else if (index.equals("8")) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("code", "1");
            map1.put("name", "0~6");
            map1.put("amount", Double.valueOf("0.0"));
            resultList.add(map1);

            Map<String, Object> map2 = new HashMap<>();
            map2.put("code", "2");
            map2.put("name", "7~18");
            map2.put("amount", Double.valueOf("0.0"));
            resultList.add(map2);

            Map<String, Object> map3 = new HashMap<>();
            map3.put("code", "3");
            map3.put("name", "18~30");
            map3.put("amount", Double.valueOf("0.0"));
            resultList.add(map3);

            Map<String, Object> map4 = new HashMap<>();
            map4.put("code", "4");
            map4.put("name", "30~50");
            map4.put("amount", Double.valueOf("0.0"));
            resultList.add(map4);

            Map<String, Object> map5 = new HashMap<>();
            map5.put("code", "5");
            map5.put("name", "50~65");
            map5.put("amount", Double.valueOf("0.0"));
            resultList.add(map5);

            Map<String, Object> map6 = new HashMap<>();
            map6.put("code", "6");
            map6.put("name", "50~65");
            map6.put("amount", Double.valueOf("0.0"));
            resultList.add(map6);
        } else if (index.equals("16")) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("code", "0");
            map1.put("name", "未缴费人数");
            map1.put("amount", Double.valueOf("0.0"));
            resultList.add(map1);

            Map<String, Object> map2 = new HashMap<>();
            map2.put("code", "1");
            map2.put("name", "已缴费人数");
            map2.put("amount", Double.valueOf("0.0"));
            resultList.add(map2);

            Map<String, Object> map3 = new HashMap<>();
            map3.put("code", "2");
            map3.put("name", "已退费人数");
            map3.put("amount", Double.valueOf("0.0"));
            resultList.add(map3);
        } else if (index.equals("15")) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("code", "0");
            map1.put("name", "未标注");
            map1.put("amount", Double.valueOf("0.0"));
            resultList.add(map1);

            Map<String, Object> map2 = new HashMap<>();
            map2.put("code", "1");
            map2.put("name", "健康人群");
            map2.put("amount", Double.valueOf("0.0"));
            resultList.add(map2);

            Map<String, Object> map3 = new HashMap<>();
            map3.put("code", "2");
            map3.put("name", "患病人群");
            map3.put("amount", Double.valueOf("0.0"));
            resultList.add(map3);

            Map<String, Object> map4 = new HashMap<>();
            map4.put("code", "3");
            map4.put("name", "高危人群");
            map4.put("amount", Double.valueOf("0.0"));
            resultList.add(map4);

            Map<String, Object> map5 = new HashMap<>();
            map5.put("code", "4");
            map5.put("name", "恢复期人群");
            map5.put("amount", Double.valueOf("0.0"));
            resultList.add(map5);
        }

        return resultList;
    }


    /**
     * 统计65以上人群数据
     *
     * @param endDate
     * @param area
     * @param level
     * @return
     */
    public JSONArray getSixFiveStatistics(String endDate, String area, int level) {
        String areaField = "";
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, Object>> resultList = new ArrayList<>();
        boolean preFlag = false;
        String preDate = endDate;

        if (endDate.compareTo(datef.format(new Date())) >= 0) {
            String redisData = "";
            try {
                redisData = redisTemplate.opsForValue().get("quota:12:" + level + ":" + area + ":6:" + getQuotaTimeStamp());
            } catch (Exception e) {
                redisData = "";
            }
            if (!StringUtils.isEmpty(redisData)) {
                JSONArray jsonArray = new JSONArray(redisData);

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject json = jsonArray.getJSONObject(i);
                    Map<String, Object> data = new HashMap<>();

                    data.put("code", json.getString("code"));
                    data.put("name", json.getString("name"));
                    data.put("amount", Long.valueOf(String.valueOf(json.get("num"))));

                    resultList.add(data);
                }
            } else {
                preFlag = true;
                Calendar today = Calendar.getInstance();
                today.add(Calendar.DATE, -1);
                preDate = datef.format(today.getTime());
            }
        } else {
            preFlag = true;
        }

        if (preFlag) {
            if (level == 4) {
                // 市级别
                areaField = "city";
            } else if (level == 3) {
                // 区、城镇级别
                areaField = "town";
            } else if (level == 2) {
                // 机构级别
                areaField = "org_code";
            } else if (level == 1) {
                // 团队
                areaField = "qkdoctor_code";
            }

            // 查询语句
            String sql = " select " +
                    "     ifnull(level3_type,'') code " +
                    "     ,ifnull(level3_type_name,'') 'name' " +
                    "     ,ifnull(result,0.0) amount" +
                    " from  " +
                    "     wlyy_quota_result " +
                    " where " +
                    "     quato_code = '12' " +
                    "   and level2_type = '6' " +
                    "   and level1_type = ? and del = '1'";


            sql += "   and quota_date = ? " +
                    "   and " + areaField + " = ? " +
                    " group by level3_type,level3_type_name";

            resultList = jdbcTemplate.queryForList(sql, new Object[]{level, preDate, area});
        }

        if (resultList == null || resultList.size() < 1) {
            resultList = new ArrayList<>();

            Map<String, Object> gxy = new HashMap<>();
            gxy.put("code", "1");
            gxy.put("name", "高血压");
            gxy.put("amount", Double.valueOf("0.0"));
            resultList.add(gxy);

            Map<String, Object> tnb = new HashMap<>();
            tnb.put("code", "2");
            tnb.put("name", "糖尿病");
            tnb.put("amount", Double.valueOf("0.0"));
            resultList.add(tnb);

            Map<String, Object> gxyTnb = new HashMap<>();
            gxyTnb.put("code", "3");
            gxyTnb.put("name", "高血压+糖尿病");
            gxyTnb.put("amount", Double.valueOf("0.0"));
            resultList.add(gxyTnb);

            Map<String, Object> jk = new HashMap<>();
            jk.put("code", "4");
            jk.put("name", "健康人群");
            jk.put("amount", Double.valueOf("0.0"));
            resultList.add(jk);
        }

        if (resultList != null) {
            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") == null ? 0 : Math.round(Double.valueOf(map.get("amount").toString())));
            }

            // 65岁以上人群总数统计
            long sixFiveTotal = getSixFiveTotal(endDate, area, level);
            Map<String, Object> sixFive = new HashMap<>();

            sixFive.put("code", "0");
            sixFive.put("name", "总数");
            sixFive.put("amount", sixFiveTotal);
            resultList.add(sixFive);

            return new JSONArray(resultList);
        } else {
            return new JSONArray();
        }
    }

    /**
     * 获取65岁以上人群总数
     *
     * @param endDate
     * @param area
     * @param level
     * @return
     */
    public long getSixFiveTotal(String endDate, String area, int level) {
        String areaField = "";
        long total = 0;
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        boolean preFlag = false;
        String preDate = endDate;

        if (endDate.compareTo(datef.format(new Date())) >= 0) {
            String code = "6";
            String val = "";
            try {
                val = redisTemplate.opsForValue().get("quota:8:" + level + ":" + area + ":" + code + ":" + getQuotaTimeStamp());
            } catch (Exception e) {
                val = "";
            }
            if (!StringUtils.isEmpty(val)) {
                JSONObject valJson = new JSONObject(val);
                if (valJson.has("num")) {
                    total = valJson.getInt("num");
                }
            } else {
                preFlag = true;
                Calendar today = Calendar.getInstance();
                today.add(Calendar.DATE, -1);
                preDate = datef.format(today.getTime());
            }
        } else {
            preFlag = true;
        }

        if (preFlag) {
            if (level == 4) {
                // 市级别
                areaField = "city";
            } else if (level == 3) {
                // 区、城镇级别
                areaField = "town";
            } else if (level == 2) {
                // 机构级别
                areaField = "org_code";
            } else if (level == 1) {
                // 团队
                areaField = "qkdoctor_code";
            }

            // 查询语句
            String sql = " select " +
                    "     ifnull(result,0.0) amount" +
                    " from  " +
                    "     wlyy_quota_result " +
                    " where " +
                    "     quato_code = '8' " +
                    "   and level2_type = '6' " +
                    "   and level1_type = ? and del = '1'";


            sql += "   and quota_date = ? " +
                    "   and " + areaField + " = ? ";

            List<Map<String, Object>> result = null;

            result = jdbcTemplate.queryForList(sql, new Object[]{level, preDate, area});

            if (result != null && result.size() > 0) {
                total = (result.get(0).get("amount") == null ? 0 : Math.round(Double.valueOf(result.get(0).get("amount").toString())));
            }
        }

        return total;
    }

    /**
     * 查询某个日期范围内的某个指标每天到达量
     *
     * @param startDate
     * @param endDate
     * @param interval
     * @param area
     * @param level
     * @param index
     * @return
     */
    public JSONObject getDateTotal(String startDate, String endDate, int interval, String area, int level, String index, String lowCode) throws Exception {
        int taskNum = 0;
        JSONObject json = new JSONObject();

        if (level > 1 && StringUtils.isEmpty(lowCode)) {
            String redisNum = "";
            try {
                redisNum = redisTemplate.opsForValue().get("people:num:" + area);
            } catch (Exception e) {
                redisNum = "";
            }

            if (StringUtils.isEmpty(redisNum)) {
                PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(area, Calendar.getInstance().get(Calendar.YEAR));
                if (peopleNum != null) {
                    taskNum = peopleNum.getTaskNum();
                }
            } else {
                JSONObject peopleNum = new JSONObject(redisNum);
                if (peopleNum != null) {
                    taskNum = peopleNum.getInt("taskNum");
                }
            }
        }
        if (!StringUtils.isEmpty(lowCode)) {
            String redisNum = "";
            try {
                redisNum = redisTemplate.opsForValue().get("people:num:" + area);
            } catch (Exception e) {
                redisNum = "";
            }
            if (StringUtils.isEmpty(redisNum)) {
                PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(area, Calendar.getInstance().get(Calendar.YEAR));
                if (peopleNum != null) {
                    if (lowCode.equals("3")) {
                        taskNum = peopleNum.getSixFiveTaskNum();
                    } else if (lowCode.equals("1")) {
                        taskNum = peopleNum.getGxyTaskNum();
                    } else if (lowCode.equals("2")) {
                        taskNum = peopleNum.getTnbTaskNum();
                    }
                }
            } else {
                JSONObject peopleNum = new JSONObject(redisNum);
                if (peopleNum != null) {
                    if (lowCode.equals("3")) {
                        taskNum = peopleNum.getInt("sixFiveTaskNum");
                    } else if (lowCode.equals("1")) {
                        taskNum = peopleNum.getInt("gxyTaskNum");
                    } else if (lowCode.equals("2")) {
                        taskNum = peopleNum.getInt("tnbTaskNum");
                    }
                }
            }
        }

        json.put("taskNum", taskNum);

        if (interval == 1) {
            JSONArray jsonArray = dateTotalStatistics(startDate, endDate, area, level, index, lowCode);
            json.put("data", jsonArray);
            return json;
        } else if (interval == 2) {
            JSONArray jsonArray = weekTotalStatistics(startDate, endDate, area, level, index, lowCode);
            json.put("data", jsonArray);
            return json;
        } else if (interval == 3) {
            JSONArray jsonArray = monthTotalStatistics(startDate, endDate, area, level, index, lowCode);
            json.put("data", jsonArray);
            return json;
        }

        return json;
    }

    /**
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    public JSONArray dateTotalStatistics(String startDate, String endDate, String area, int level, String index, String lowCode) throws ParseException {
        String areaField = "";
        String sql = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateCon = endDate;

        if (endDate.compareTo(df.format(new Date())) == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = df.format(calendar.getTime());
        }

        if (level == 4) {
            // 市级别
            areaField = "city";
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        } else if (level == 1) {
            // 机构级别
            areaField = "qkdoctor_code";
        }

        // 起始日期
        Calendar start = Calendar.getInstance();
        start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
        //结束日期
        Calendar end = Calendar.getInstance();
        end.setTime(DateUtil.strToDate(endDate, DateUtil.YYYY_MM_DD));
        // 日期集合
        List<Calendar> days = new ArrayList<>();
        days.add(start);

        boolean flag = true;

        if (startDate.compareTo(endDate) == 0) {
            flag = false;
        }
        // 计算统计日期
        while (flag) {
            Calendar next = Calendar.getInstance();
            next.setTime(days.get(days.size() - 1).getTime());
            next.add(Calendar.DATE, 1);
            if (df.format(next.getTime()).compareTo(endDate) < 0) {
                days.add(next);
            } else {
                days.add(end);
                flag = false;
            }
        }

        Map<String, JSONObject> countResult = new HashMap<>();

        // 统计预计构建
        for (int i = 0; i < days.size(); i++) {
            String startStr = "";
            long amount = 0;

            startStr = df.format(days.get(i).getTime());

            // 当前范围包含当天，则需添加当天的统计数据
            if (startStr.compareTo(df.format(new Date())) == 0) {
                String val = "";
                try {
                    val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area + ":" + (!StringUtils.isEmpty(lowCode) ? (lowCode + ":") : "") + getQuotaTimeStamp());
                } catch (Exception e) {
                    val = "";
                }
                if (!StringUtils.isEmpty(val)) {
                    JSONObject valJson = new JSONObject(val);
                    if (valJson.has("num")) {
                        amount = (long) valJson.getInt("num");
                    }
                }
            }

            JSONObject range = new JSONObject();
            range.put("range", startStr);
            range.put("amount", amount);
            countResult.put(startStr, range);
        }

        if (startDate.equals(df.format(new Date()))) {
            Calendar preDate = Calendar.getInstance();
            preDate.setTime(df.parse(endDate));
            preDate.add(Calendar.DATE, -1);

            startDate = df.format(preDate.getTime());
        }

        sql = " select " +
                "     ifnull(quota_date,'') as 'range' " +
                "     ,ifnull(result,'0') amount " +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = '" + level + "' and del = '1' " +
                "   and quota_date >= '" + startDate + "' " +
                "   and quota_date <= '" + dateCon + "' " +
                "   and " + areaField + " = '" + area + "' ";

        if (!StringUtils.isEmpty(lowCode)) {
            sql += "  and level2_type = '" + lowCode + "' ";
        }

        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);

        if (resultList != null) {
            for (Map<String, Object> map : resultList) {
                if (countResult.containsKey(map.get("range").toString())) {
                    JSONObject range = (JSONObject) countResult.get(map.get("range").toString());
                    long amount = range.getLong("amount");
                    long resultAmount = map.get("amount") != null ? Long.valueOf(map.get("amount").toString()) : 0L;
                    range.put("amount", amount + resultAmount);
                }
            }

            List<JSONObject> result = new ArrayList<>(countResult.values());

            result.sort(new Comparator<JSONObject>() {
                @Override
                public int compare(JSONObject o1, JSONObject o2) {
                    if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
                        return 1;
                    } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });

            return new JSONArray(result);
        } else {
            return new JSONArray();
        }
    }

    /**
     * 按周统计
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    private JSONArray weekTotalStatistics(String startDate, String endDate, String area, int level, String index, String lowCode) throws Exception {
        String areaField = "";
        String sql = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateCon = endDate;

        if (endDate.compareTo(df.format(new Date())) == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = df.format(calendar.getTime());
        }

        if (level == 4) {
            // 市级别
            areaField = "city";
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        } else if (level == 1) {
            // 机构级别
            areaField = "qkdoctor_code";
        }

        // 起始日期
        Calendar start = Calendar.getInstance();
        start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
        // 第一个统计周期结束日期
        String firstEnd = "";
        // 结束日期
        Calendar end = Calendar.getInstance();
        end.setTime(DateUtil.strToDate(endDate, DateUtil.YYYY_MM_DD));
        // 起始日期为周几
        int week = start.get(Calendar.DAY_OF_WEEK);
        int incre = 7 - week + 1;

        // 日期集合
        List<Calendar> days = new ArrayList<>();
        days.add(start);
        boolean flag = true;
        int i = 0;

        if (startDate.compareTo(endDate) == 0) {
            flag = false;
            days.add(end);
            firstEnd = df.format(end.getTime());
        }

        // 计算统计日期
        while (flag) {
            Calendar next = Calendar.getInstance();
            next.setTime(days.get(days.size() - 1).getTime());

            if (i == 0) {
                if (incre != 7) {
                    next.add(Calendar.DATE, incre);
                }
            } else {
                next.add(Calendar.DATE, 7);
            }
            if (df.format(next.getTime()).compareTo(df.format(end.getTime())) < 0) {
                days.add(next);
                if (i == 0) {
                    firstEnd = df.format(next.getTime());
                }
            } else {
                days.add(end);
                flag = false;
                if (i == 0) {
                    firstEnd = df.format(end.getTime());
                }
            }

            i++;
        }

        // 结果集
        Map<String, JSONObject> countResult = new HashMap<>();

        // 算出每个查询周期
        for (int j = 0; j < days.size() - 1; j++) {
            String startStr = "";
            String endStr = df.format(days.get(j + 1).getTime());
            long amount = 0;

            // 起始、截止日期
            if (j == 0) {
                startStr = df.format(days.get(j).getTime());
            } else {
                Calendar cal = Calendar.getInstance();
                cal.setTime(days.get(j).getTime());
                cal.add(Calendar.DATE, 1);
                startStr = df.format(cal.getTime());
            }

            // 当前范围包含当天，则需添加当天的统计数据
            if (startStr.compareTo(df.format(new Date())) <= 0 && endStr.compareTo(df.format(new Date())) >= 0) {
                String val = "";
                try {
                    val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area + ":" + (!StringUtils.isEmpty(lowCode) ? (lowCode + ":") : "") + getQuotaTimeStamp());
                } catch (Exception e) {
                    val = "";
                }
                if (!StringUtils.isEmpty(val)) {
                    JSONObject valJson = new JSONObject(val);
                    if (valJson.has("num")) {
                        amount = valJson.getInt("num");
                    }
                }
            }

            JSONObject range = new JSONObject();

            range.put("range", endStr);
            range.put("amount", amount);

            countResult.put(endStr, range);
        }

        if (startDate.equals(df.format(new Date()))) {
            Calendar preDate = Calendar.getInstance();
            preDate.setTime(df.parse(endDate));
            preDate.add(Calendar.DATE, -1);

            startDate = df.format(preDate.getTime());
        }

        // 查询时间范围内所有记录
        sql = " select " +
                "     ifnull(quota_date,'') as 'range' " +
                "     ,ifnull(result,'0') amount " +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = '" + level + "' and del = '1' " +
                "   and quota_date >= '" + startDate + "' " +
                "   and quota_date <= '" + dateCon + "' " +
                "   and " + areaField + " = '" + area + "' ";

        if (!StringUtils.isEmpty(lowCode)) {
            sql += "  and level2_type = '" + lowCode + "' ";
        }

        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);

        if (resultList != null) {
            // 计算结果
            for (Map<String, Object> map : resultList) {
                String range = map.get("range").toString();
                JSONObject json = countResult.get(range);

                if (json != null) {
                    long amount = map.get("amount") == null ? 0L : Long.valueOf(map.get("amount").toString());
                    json.put("amount", amount);
                }
            }

            List<JSONObject> result = new ArrayList<>(countResult.values());

            // 排序
            result.sort(new Comparator<JSONObject>() {
                @Override
                public int compare(JSONObject o1, JSONObject o2) {
                    if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
                        return 1;
                    } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });

            return new JSONArray(result);
        } else {
            return new JSONArray();
        }
    }

    /**
     * 按月统计
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     * @throws Exception
     */
    private JSONArray monthTotalStatistics(String startDate, String endDate, String area, int level, String index, String lowCode) throws Exception {
        String areaField = "";
        String sql = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateCon = endDate;

        if (endDate.compareTo(df.format(new Date())) == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = df.format(calendar.getTime());
        }

        if (level == 4) {
            // 市级别
            areaField = "city";
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        } else if (level == 1) {
            // 机构级别
            areaField = "qkdoctor_code";
        }

        // 起始日期
        Calendar start = Calendar.getInstance();
        start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
        // 结束日期
        Calendar end = Calendar.getInstance();
        end.setTime(DateUtil.strToDate(endDate, DateUtil.YYYY_MM_DD));
        // 第一个结束日期
        String firstEnd = "";

        // 日期集合
        List<Calendar> days = new ArrayList<>();
        days.add(start);

        boolean flag = true;
        int k = 0;

        if (startDate.compareTo(endDate) == 0) {
            flag = false;
            days.add(end);
        }

        // 统计日期计算
        while (flag) {
            Calendar next = Calendar.getInstance();

            next.setTime(days.get(days.size() - 1).getTime());
            if (k == 0) {
                next.add(Calendar.MONTH, 1);
            } else {
                next.add(Calendar.MONTH, 2);
            }
            next.set(Calendar.DAY_OF_MONTH, 1);
            next.add(Calendar.DAY_OF_MONTH, -1);

            if (df.format(next.getTime()).compareTo(df.format(end.getTime())) < 0) {
                days.add(next);
            } else {
                days.add(end);
                flag = false;
            }
            k++;
        }

        // 统计结果
        Map<String, JSONObject> countResult = new HashMap<>();

        for (int i = 0; i < days.size() - 1; i++) {
            String startStr = "";
            String endStr = df.format(days.get(i + 1).getTime());
            int amount = 0;

            // 起始时间计算
            if (i == 0) {
                startStr = df.format(days.get(i).getTime());
            } else {
                Calendar cal = Calendar.getInstance();
                cal.setTime(days.get(i).getTime());
                cal.add(Calendar.DATE, 1);
                startStr = df.format(cal.getTime());
            }

            // 当天数据计算
            if (startStr.compareTo(df.format(new Date())) <= 0 && endStr.compareTo(df.format(new Date())) >= 0) {
                String val = "";
                try {
                    val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area + ":" + (!StringUtils.isEmpty(lowCode) ? (lowCode + ":") : "") + getQuotaTimeStamp());
                } catch (Exception e) {
                    val = "";
                }
                if (!StringUtils.isEmpty(val)) {
                    JSONObject valJson = new JSONObject(val);
                    if (valJson.has("num")) {
                        amount = valJson.getInt("num");
                    }
                }
            }

            JSONObject range = new JSONObject();

            range.put("range", endStr);
            range.put("amount", amount);

            countResult.put(endStr, range);
        }

        if (startDate.equals(df.format(new Date()))) {
            Calendar preDate = Calendar.getInstance();
            preDate.setTime(df.parse(endDate));
            preDate.add(Calendar.DATE, -1);

            startDate = df.format(preDate.getTime());
        }

        // 查询时间范围内所有记录
        sql = " select " +
                "     ifnull(quota_date,'') as 'range' " +
                "     ,ifnull(result,'0') amount " +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = '" + level + "' and del = '1' " +
                "   and quota_date >= '" + startDate + "' " +
                "   and quota_date <= '" + dateCon + "' " +
                "   and " + areaField + " = '" + area + "' ";

        if (!StringUtils.isEmpty(lowCode)) {
            sql += "  and level2_type = '" + lowCode + "' ";
        }

        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);

        if (resultList != null) {
            String pre = "";
            if (endDate.equals(df.format(new Date()))) {
                Calendar preDate = Calendar.getInstance();
                preDate.setTime(df.parse(endDate));
                preDate.add(Calendar.DATE, -1);
                pre = df.format(preDate.getTime());
            }
            // 计算结果
            for (Map<String, Object> map : resultList) {
                String range = map.get("range").toString();
                JSONObject json = countResult.get(range);

                if (json != null) {
                    long amount = map.get("amount") == null ? 0L : Long.valueOf(map.get("amount").toString());
                    json.put("amount", amount);
                }
            }

            List<JSONObject> result = new ArrayList<>(countResult.values());

            // 排序
            result.sort(new Comparator<JSONObject>() {
                @Override
                public int compare(JSONObject o1, JSONObject o2) {
                    if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
                        return 1;
                    } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });

            return new JSONArray(result);
        } else {
            return new JSONArray();
        }
    }

}
