Pārlūkot izejas kodu

Merge remote-tracking branch 'origin/dev' into dev

8 gadi atpakaļ
vecāks
revīzija
4b94e389da

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 1 - 1
patient-co-wlyy/src/main/java/com/yihu/wlyy/entity/message/Message.java


+ 21 - 0
patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/message/MessageService.java

@ -3,6 +3,7 @@ package com.yihu.wlyy.service.app.message;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.transaction.Transactional;
@ -256,7 +257,27 @@ public class MessageService extends BaseService {
     */
    public Message findMessage(String sender, String receiver, String signStatus) {
        return messageDao.findByParams(sender, receiver, signStatus);
    }
    public Page<Message> find(String doctor, String type, long id, int pagesize) {
        if (pagesize <= 0) {
            pagesize = 10;
        }
        // 排序
        Sort sort = new Sort(Direction.DESC, "id","read");
        // 分页信息
        PageRequest pageRequest = new PageRequest(0, pagesize, sort);
        // 设置查询条件
        Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
        filters.put("receiver", new SearchFilter("receiver", Operator.EQ, doctor));
        filters.put("type", new SearchFilter("type", Operator.EQ, type));
        if (id > 0) {
            filters.put("id", new SearchFilter("id", Operator.LT, id));
        }
        Specification<Message> spec = DynamicSpecifications.bySearchFilter(filters.values(), Message.class);
        return messageDao.findAll(spec, pageRequest);
    }
}

+ 19 - 0
patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/record/DrugService.java

@ -73,20 +73,39 @@ public class DrugService extends BaseService {
                            String key = patientId + eventNo + orgId;
                            JSONObject catalogObject = new JSONObject();
                            String catalogCode = jsonObject.get("CATALOG_CODE").toString();
                            String endTimeNew = jsonObject.get("END_TIME").toString();
                            String catalogValue = systemDictMap.get(catalogCode);
                            jsonObject.remove("CATALOG_CODE");
                            if (jsonObjectMap.containsKey(key)) {
                                jsonObject = jsonObjectMap.get(key);
                                String endTimeOld = jsonObject.get("END_TIME").toString();
                                if (endTimeNew.compareTo(endTimeOld) < 0) {
                                    endTimeNew = endTimeOld;
                                }
                                catalogObject = jsonObject.getJSONObject("CATALOG");
                            }
                            catalogObject.put(catalogCode, catalogValue);
                            jsonObject.put("CATALOG", catalogObject);
                            jsonObject.put("END_TIME", endTimeNew);
                            jsonObjectMap.put(key, jsonObject);
                        }
                        for (String key : jsonObjectMap.keySet()) {
                            resultArray.put(jsonObjectMap.get(key));
                        }
                        JSONObject temp;
                        for (int i = 0; i < resultArray.length(); i++) {
                            for (int j = i+1; j < resultArray.length(); j++) {
                                String endTimeNew = resultArray.getJSONObject(j).get("END_TIME").toString();
                                String endTimeOld = resultArray.getJSONObject(i).get("END_TIME").toString();
                                if (endTimeNew.compareTo(endTimeOld) > 0) {
                                    temp = resultArray.getJSONObject(i);
                                    resultArray.put(i, resultArray.getJSONObject(j));
                                    resultArray.put(j, temp);  // 两个数交换位置
                                }
                            }
                        }
                    }
                }
            }

+ 22 - 0
patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/record/HospitalizationService.java

@ -58,6 +58,9 @@ public class HospitalizationService extends BaseService {
            if (status == 200){
                String data = responseObject.getString("data");
                if (!StringUtils.isEmpty(data)){
                    if (data.startsWith("error")) {
                        return data;
                    }
                    JSONObject jsonData = new JSONObject(data);
                    JSONArray jsonArray = jsonData.getJSONArray("EhrList");
                    if (!jsonArray.isNull(0) && jsonArray.getJSONObject(0).length() != 0) {
@ -70,20 +73,39 @@ public class HospitalizationService extends BaseService {
                            String key = patientId + eventNo + orgId;
                            JSONObject catalogObject = new JSONObject();
                            String catalogCode = jsonObject.get("CATALOG_CODE").toString();
                            String endTimeNew = jsonObject.get("END_TIME").toString();
                            String catalogValue = systemDictMap.get(catalogCode);
                            jsonObject.remove("CATALOG_CODE");
                            if (jsonObjectMap.containsKey(key)) {
                                jsonObject = jsonObjectMap.get(key);
                                String endTimeOld = jsonObject.get("END_TIME").toString();
                                if (endTimeNew.compareTo(endTimeOld) < 0) {
                                    endTimeNew = endTimeOld;
                                }
                                catalogObject = jsonObject.getJSONObject("CATALOG");
                            }
                            catalogObject.put(catalogCode, catalogValue);
                            jsonObject.put("CATALOG", catalogObject);
                            jsonObject.put("END_TIME", endTimeNew);
                            jsonObjectMap.put(key, jsonObject);
                        }
                        for (String key : jsonObjectMap.keySet()) {
                            resultArray.put(jsonObjectMap.get(key));
                        }
                        JSONObject temp;
                        for (int i = 0; i < resultArray.length(); i++) {
                            for (int j = i+1; j < resultArray.length(); j++) {
                                String endTimeNew = resultArray.getJSONObject(j).get("END_TIME").toString();
                                String endTimeOld = resultArray.getJSONObject(i).get("END_TIME").toString();
                                if (endTimeNew.compareTo(endTimeOld) > 0) {
                                    temp = resultArray.getJSONObject(i);
                                    resultArray.put(i, resultArray.getJSONObject(j));
                                    resultArray.put(j, temp);  // 两个数交换位置
                                }
                            }
                        }
                    }
                }
            }

+ 44 - 4
patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/record/InspectionService.java

@ -58,10 +58,38 @@ public class InspectionService extends BaseService {
    public String getInspectionAndChecking(String strSSID, String startNum, String endNum) throws Exception {
        JSONArray total = new JSONArray();
        JSONArray check = getChecking(strSSID, startNum, endNum);
        if (!check.isNull(0)) {
            JSONObject jsonObject = check.getJSONObject(0);
            if (!StringUtils.isEmpty(jsonObject.get("error"))) {
                return jsonObject.get("error").toString();
            }
        }
        JSONArray inspect = getInspection(strSSID, startNum, endNum);
        if (!inspect.isNull(0)) {
            JSONObject jsonObject = inspect.getJSONObject(0);
            if (!StringUtils.isEmpty(jsonObject.get("error"))) {
                return jsonObject.get("error").toString();
            }
        }
        //TODO 检查检验数据合并
        total.put(check);
        total.put(inspect);
        for (int i = 0; i < check.length(); i++) {
            total.put(check.getJSONObject(i));
        }
        for (int i = 0; i < inspect.length(); i++) {
            total.put(inspect.getJSONObject(i));
        }
        JSONObject temp;
        for (int i = 0; i < total.length(); i++) {
            for (int j = i+1; j < total.length(); j++) {
                String endTimeNew = total.getJSONObject(j).get("END_TIME").toString();
                String endTimeOld = total.getJSONObject(i).get("END_TIME").toString();
                if (endTimeNew.compareTo(endTimeOld) > 0) {
                    temp = total.getJSONObject(i);
                    total.put(i, total.getJSONObject(j));
                    total.put(j, temp);  // 两个数交换位置
                }
            }
        }
        return total.toString();
    }
@ -87,12 +115,18 @@ public class InspectionService extends BaseService {
            systemDictMap.put(systemDict.getCode(), systemDict.getValue());
        }
        if (!org.apache.commons.lang3.StringUtils.isEmpty(response)){
        if (!StringUtils.isEmpty(response)){
            JSONObject responseObject = new JSONObject(response);
            int status = responseObject.getInt("status");
            if (status == 200){
                String data = responseObject.getString("data");
                if (!org.apache.commons.lang3.StringUtils.isEmpty(data)){
                if (!StringUtils.isEmpty(data)){
                    if (data.startsWith("error")) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("error", data);
                        resultArray.put(jsonObject);
                        return resultArray ;
                    }
                    JSONObject jsonData = new JSONObject(data);
                    JSONArray jsonArray = jsonData.getJSONArray("EhrList");
                    if (!jsonArray.isNull(0) && jsonArray.getJSONObject(0).length() != 0) {
@ -105,14 +139,20 @@ public class InspectionService extends BaseService {
                            String key = patientId + eventNo + orgId;
                            JSONObject catalogObject = new JSONObject();
                            String catalogCode = jsonObject.get("CATALOG_CODE").toString();
                            String endTimeNew = jsonObject.get("END_TIME").toString();
                            String catalogValue = systemDictMap.get(catalogCode);
                            jsonObject.remove("CATALOG_CODE");
                            if (jsonObjectMap.containsKey(key)) {
                                jsonObject = jsonObjectMap.get(key);
                                String endTimeOld = jsonObject.get("END_TIME").toString();
                                if (endTimeNew.compareTo(endTimeOld) < 0) {
                                    endTimeNew = endTimeOld;
                                }
                                catalogObject = jsonObject.getJSONObject("CATALOG");
                            }
                            catalogObject.put(catalogCode, catalogValue);
                            jsonObject.put("CATALOG", catalogObject);
                            jsonObject.put("END_TIME", endTimeNew);
                            jsonObjectMap.put(key, jsonObject);
                        }

+ 4 - 4
patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/statistics/StatisticsAllService.java

@ -392,8 +392,8 @@ public class StatisticsAllService extends BaseService {
                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("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);
@ -643,8 +643,8 @@ public class StatisticsAllService extends BaseService {
                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("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);

+ 0 - 1746
patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/statistics/StatisticsService.java

@ -42,26 +42,6 @@ public class StatisticsService extends BaseService {
    HospitalDao hospitalDao;
    @Autowired
    DoctorAdminTeamDao adminTeamDao;
    /******************************************数据库统计指标***************************************/
    // 签约人数
    public static String SIGN = "1";
    // 解约人数
    public static String SURRENDER = "2";
    // 咨询数
    public static String CONSULT = "3";
    // 随访数
    public static String FOLLOWUP = "4";
    // 指导数
    public static String GUIDANCE = "5";
    // 咨询数
    public static String SEX = "6";
    // 随访数
    public static String GROUP = "7";
    // 指导数
    public static String AGE = "8";
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
@ -160,188 +140,6 @@ public class StatisticsService extends BaseService {
        }
    }
    /**
     * 获取一级指标达到量
     *
     * @param endDate 截止日期
     * @param area    区域
     * @param index   指标
     * @param level   级别
     * @return
     */
    public long getIndexTotal(String endDate,String area, int level , String index) {
        int todayAmount = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateCon = endDate;
        if (endDate.compareTo(dateFormat.format(new Date())) >= 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = dateFormat.format(calendar.getTime());
        }
        // 查询语句
        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 = ?";
        }
        // 截止日期包含当天,则从redis查询当天统计数据
        if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0) {
            String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area + ":" + getQuotaTimeStamp());
            if (!StringUtils.isEmpty(val)) {
                JSONObject valJson = new JSONObject(val);
                if (valJson.has("num")) {
                    todayAmount = valJson.getInt("num");
                }
            }
        }
        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, new Object[]{index,level, dateCon, area});
        if (result != null && result.size() > 0) {
            return (result.get(0).get("amount") != null ? (Long.valueOf(result.get(0).get("amount").toString()) + todayAmount) : todayAmount);
        } else {
            return todayAmount;
        }
    }
    /**
     * 获取缴费
     *
     * @param endDate 截止日期
     * @param area    区域
     * @param level   级别
     * @return
     */
    public long getWeiJiaoFei(String endDate, String area, int level) {
        int todayAmount = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateCon = endDate;
        if (endDate.compareTo(dateFormat.format(new Date())) == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = dateFormat.format(calendar.getTime());
        }
        // 查询语句
        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 = ?";
        }
        // 截止日期包含当天,则从redis查询当天统计数据
        if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0) {
            String val = redisTemplate.opsForValue().get("quota:16:" + level + ":" + area + ":0:" + getQuotaTimeStamp());
            if (!StringUtils.isEmpty(val)) {
                JSONObject valJson = new JSONObject(val);
                if (valJson.has("num")) {
                    todayAmount = valJson.getInt("num");
                }
            }
        }
        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, new Object[]{level, dateCon, area});
        if (result != null && result.size() > 0) {
            return (result.get(0).get("amount") != null ? (Long.valueOf(result.get(0).get("amount").toString()) + todayAmount) : todayAmount);
        } else {
            return todayAmount;
        }
    }
    /**
     * 查询截止某个日期累计签约率
     *
     * @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;
    }
    /**
     * 获取某个指标在某个期间的增长量
     *
@ -1309,1548 +1107,4 @@ public class StatisticsService extends BaseService {
        }
    }
    /**
     * 获取二级维度下指标时间范围内增量
     *
     * @param startDate 起始时间
     * @param endDate   结束时间
     * @param area      区域或机构代码
     * @param level     级别
     * @param index     指标
     * @return
     */
    public JSONArray getLevelTwoIndexIncrement(String startDate, String endDate, String area, int level, String index) {
        String areaField = "";
        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(sum(result),0.0) amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = ? and del = '1'";
        if (!org.apache.commons.lang3.StringUtils.isEmpty(startDate)) {
            sql += "   and quota_date >= ? ";
        }
        sql += "   and quota_date <= ? " +
                "   and " + areaField + " = ? " +
                " group by level2_type,level2_type_name";
        List<Map<String, Object>> resultList = null;
        if (org.apache.commons.lang3.StringUtils.isEmpty(startDate)) {
            resultList = jdbcTemplate.queryForList(sql, new Object[]{level, endDate, area});
        } else {
            resultList = jdbcTemplate.queryForList(sql, new Object[]{level, startDate, endDate, area});
        }
        if (resultList == null || resultList.size() < 1) {
            resultList = new ArrayList<>();
            if (index.equals(SEX)) {
                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(GROUP)) {
                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", "慢病人群");
                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);
            } else if (index.equals(AGE)) {
                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);
            }
        }
        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 (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0) {
                    String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
                    String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + code + ":" + area);
                    if (!StringUtils.isEmpty(val)) {
                        JSONObject valJson = new JSONObject(val);
                        if (valJson.has("num")) {
                            map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
                        }
                    }
                }
                if (index.equals(GROUP)) {
                    // 分组指标总数算法
                    String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
                    if (code.equals("1") || code.equals("2") || code.equals("3")) {
                        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(AGE)) {
                DecimalFormat df = new DecimalFormat("0.0000");
                double rateTotal = 0.0000;
                for (Map<String, Object> map : resultList) {
                    String code = String.valueOf(map.get("code"));
                    double rateG = (total > 0 ? ((long) map.get("amount")) * 1.0000 / total * 100 : 0);
                    map.put("rate", df.format(rateG));
                }
            }
            if (index.equals(SEX) && 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();
        }
    }
    /**
     * 统计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");
        String dateCon = endDate;
        if (endDate.compareTo(datef.format(new Date())) == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = datef.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";
        }
        // 查询语句
        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";
        List<Map<String, Object>> resultList = null;
        resultList = jdbcTemplate.queryForList(sql, new Object[]{level, dateCon, 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())));
                // 当天数据统计
                if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) == 0) {
                    String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
                    String val = redisTemplate.opsForValue().get("quota:12:" + level + ":6:" + code + ":" + area);
                    if (!StringUtils.isEmpty(val)) {
                        JSONObject valJson = new JSONObject(val);
                        if (valJson.has("num")) {
                            map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
                        }
                    }
                }
            }
            // 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");
        String dateCon = endDate;
        if (endDate.compareTo(datef.format(new Date())) == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = datef.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";
        }
        // 查询语句
        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, dateCon, 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())));
        }
        if (endDate.compareTo(datef.format(new Date())) == 0) {
            String code = "6";
            String val = redisTemplate.opsForValue().get("quota:8:" + level + ":" + code + ":" + area);
            if (!StringUtils.isEmpty(val)) {
                JSONObject valJson = new JSONObject(val);
                if (valJson.has("num")) {
                    total += valJson.getInt("num");
                }
            }
        }
        return total;
    }
    /**
     * 查询某个级别的某个指标到达量
     *
     * @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 {
        String areaField = "";
        String lowLevelField = "";
        String lowLevelName = "";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateCon = date;
        if (date.compareTo(dateFormat.format(new Date())) == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = dateFormat.format(calendar.getTime());
        }
        if (level == 4) {
            // 市级别
            areaField = "city";
            lowLevelField = "town";
            lowLevelName = "town_name";
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
            lowLevelField = "org_code";
            lowLevelName = "org_name";
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
            lowLevelField = "qkdoctor_code";
            lowLevelName = "qkdoctor_name";
        } else if (level == 1) {
            throw new Exception("param level error");
        }
        if (!StringUtils.isEmpty(lowLevel)) {
            if (lowLevel.equals("3")) {
                lowLevelField = "town";
                lowLevelName = "town_name";
            } else if (lowLevel.equals("2")) {
                lowLevelField = "org_code";
                lowLevelName = "org_name";
            } else if (lowLevel.equals("1")) {
                lowLevelField = "qkdoctor_code";
                lowLevelName = "qkdoctor_name";
            } else {
                throw new Exception("param lowLevel error");
            }
        }
        String low_level = String.valueOf(StringUtils.isEmpty(lowLevel) ? (level - 1) : lowLevel);
        // 查询语句
        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 ";
        }
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{low_level, dateCon, area});
        // 结果为空时,自建结果集
        if (resultList == null || resultList.size() < 1) {
            resultList = new ArrayList<>();
            if (low_level.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");
                        obj.put("rate", Double.valueOf("0.0000"));
                        resultList.add(obj);
                    }
                }
            } else if (low_level.equals("2")) {
                List<Town> towns = new ArrayList<>();
                if (level == 4) {
                    towns = townDao.findByCityCode(area);
                } else if (level == 3) {
                    Town town = townDao.findByCode(area);
                    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 (low_level.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);
                        }
                    }
                }
            }
        }
        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);
                // 截止日期包含当天,则从redis查询当天统计数据
                if (date.compareTo(dateFormat.format(new Date())) == 0) {
                    String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
                    String val = redisTemplate.opsForValue().get("quota:" + index + ":" + low_level + ":" + code);
                    if (!StringUtils.isEmpty(val)) {
                        JSONObject valJson = new JSONObject(val);
                        if (valJson.has("num")) {
                            map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
                        }
                    }
                }
                if (!low_level.equals("1")) {
                    String redisNum = redisTemplate.opsForValue().get("people:num:" + map.get("code").toString());
                    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 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) throws Exception {
        int taskNum = 0;
        JSONObject json = new JSONObject();
        if (level > 1) {
            String redisNum = redisTemplate.opsForValue().get("people:num:" + area);
            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");
                }
            }
        }
        json.put("taskNum", taskNum);
        if (interval == 1) {
            JSONArray jsonArray = dateTotalStatistics(startDate, endDate, area, level, index);
            json.put("data", jsonArray);
            return json;
        } else if (interval == 2) {
            JSONArray jsonArray = weekTotalStatistics(startDate, endDate, area, level, index);
            json.put("data", jsonArray);
            return json;
        } else if (interval == 3) {
            JSONArray jsonArray = monthTotalStatistics(startDate, endDate, area, level, index);
            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) 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 = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                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 + "' ";
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
        if (resultList != null) {
            String preStr = "";
            if (endDate.equals(df.format(new Date()))) {
                Calendar preDate = Calendar.getInstance();
                preDate.setTime(df.parse(endDate));
                preDate.add(Calendar.DATE, -1);
                preStr = df.format(preDate.getTime());
            }
            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);
                }
                if (org.apache.commons.lang3.StringUtils.isNotEmpty(preStr) &&
                        preStr.equals(map.get("range").toString())) {
                    JSONObject range = (JSONObject) countResult.get(endDate);
                    if (range != null) {
                        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) 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 = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                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 + "' ";
        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);
                }
                if (endDate.equals(df.format(new Date())) && org.apache.commons.lang3.StringUtils.isNotEmpty(pre) && range.equals(pre)) {
                    JSONObject jsonEnd = countResult.get(endDate);
                    if (jsonEnd != null) {
                        long amount = map.get("amount") == null ? 0L : Long.valueOf(map.get("amount").toString());
                        jsonEnd.put("amount", jsonEnd.getLong("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) 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);
            firstEnd = df.format(end.getTime());
        }
        // 统计日期计算
        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);
                if (k == 0) {
                    firstEnd = df.format(next.getTime());
                }
            } else {
                days.add(end);
                flag = false;
                if (k == 0) {
                    firstEnd = df.format(end.getTime());
                }
            }
            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 = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                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 + "' ";
        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);
                }
                if (endDate.equals(df.format(new Date())) &&
                        org.apache.commons.lang3.StringUtils.isNotEmpty(pre)
                        && range.equals(pre)) {
                    JSONObject jsonEnd = countResult.get(endDate);
                    if (jsonEnd != null) {
                        long amount = map.get("amount") == null ? 0L : Long.valueOf(map.get("amount").toString());
                        jsonEnd.put("amount", jsonEnd.getLong("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 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");
        String dateCon = endDate;
        if (endDate.compareTo(datef.format(new Date())) == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            dateCon = datef.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";
        }
        // 查询语句
        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 = null;
        resultList = jdbcTemplate.queryForList(sql, new Object[]{level, dateCon, area});
        if (resultList == null || resultList.size() < 1) {
            resultList = new ArrayList<>();
            if (index.equals(SEX)) {
                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(GROUP)) {
                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(AGE)) {
                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);
            }
        }
        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 (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) == 0) {
                    String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
                    String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + code + ":" + area);
                    if (!StringUtils.isEmpty(val)) {
                        JSONObject valJson = new JSONObject(val);
                        if (valJson.has("num")) {
                            map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
                        }
                    }
                }
                if (index.equals(GROUP)) {
                    // 分组指标总数算法
                    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(AGE)) {
                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(SEX) && 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();
        }
    }
}

+ 45 - 1
patient-co-wlyy/src/main/java/com/yihu/wlyy/web/doctor/message/DoctorMessageController.java

@ -18,6 +18,8 @@ import com.yihu.wlyy.service.app.message.MessageService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;
import java.util.List;
@Controller
@RequestMapping(value = "/doctor/message", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "医生端-消息")
@ -201,5 +203,47 @@ public class DoctorMessageController extends BaseController {
			return error(-1, "获取消息失败!");
		}
	}
	/**
	 * @param type 消息类型  1.是家庭签约信息  2.体征消息  3分配健管师
	 * @param id  消息id
	 * @param pagesize  每頁顯示條數
	 * @return
	 */
	@RequestMapping(value = "findMessage")
	@ResponseBody
	public String findMessage(String type,
							   long id,
							   int pagesize) {
		try {
			JSONArray array = new JSONArray();
			Page<Message> list = messageService.find(getUID(), type,id,pagesize);
			for (Message msg : list) {
				if (msg == null) {
					continue;
				}
				JSONObject json = new JSONObject();
				// 消息ID
				json.put("id", msg.getId());
				// 发送人标识
				json.put("sender", msg.getSender());
				// 消息內容
				json.put("content", msg.getContent());
				// 发送人姓名
				json.put("senderName", msg.getSenderName());
				// 发送人头像
				json.put("senderPhoto", msg.getSenderPhoto());
				// 是否已读:1未读,0已读
				json.put("read", msg.getRead());
				// 是否已读:
				json.put("sex", msg.getSex());
				// 发送时间
				json.put("czrq", DateUtil.dateToStrLong(msg.getCzrq()));
				array.put(json);
			}
			return write(200, "获取消息成功!", "list", array);
		} catch (Exception e) {
			error(e);
			return error(-1, "获取消息失败!");
		}
	}
}