Forráskód Böngészése

Merge branch 'dev' of http://192.168.1.220:10080/Amoy/patient-co-management into dev

zhenglingfeng 8 éve
szülő
commit
73177b3e07

+ 1 - 1
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/dao/DoctorDao.java

@ -110,6 +110,6 @@ public interface DoctorDao extends PagingAndSortingRepository<Doctor, Long>, Jpa
    Page<Doctor> doctorFamousDoctorList(Pageable pageRequest);
    @Query("select d from Doctor d where d.del = 1 and d.password is null")
    @Query("select d from Doctor d where d.del = 1 and (d.password is null or password = '' )")
    List<Doctor> findAllNoPasswordDoctors();
}

+ 1 - 2
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/dao/PatientDao.java

@ -51,7 +51,6 @@ public interface PatientDao extends PagingAndSortingRepository<Patient, Long> {
	@Query(" select p from Patient p,SignFamily s where p.code=s.patient and s.status > 0 ")
	List<Patient> findAllSignPatient();
	@Query(" select p from Patient p where password is null ")
	@Query(" select p from Patient p where password is null or password = ''  ")
	List<Patient> findAllIdCardPatientAndNoPassword();
}

+ 6 - 4
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/CurrentDayAllQuotaJob.java

@ -132,11 +132,8 @@ public class CurrentDayAllQuotaJob implements Job {
    //统计
    private void computequota() throws Exception {
        //更新统计时间
        redisTemplate.opsForValue().set("quota:date",DateUtil.dateToStrLong(new Date()));
        //更新时间key
        RedisStorage.timeKey=new Date().getTime()+"";
        redisTemplate.opsForValue().set("quota:timeKey",RedisStorage.timeKey);
        QuartzJobLog quartzJobLog =new QuartzJobLog();
        quartzJobLog.setJobStartTime(new Date());
@ -173,6 +170,11 @@ public class CurrentDayAllQuotaJob implements Job {
            redisTemplate.expire(key, 1, TimeUnit.HOURS);//1小时过期
        }
        RedisStorage.keys.clear();//清空Key
        //更新统计时间
        redisTemplate.opsForValue().set("quota:date",DateUtil.dateToStrLong(new Date()));
        //更新时间key
        redisTemplate.opsForValue().set("quota:timeKey",RedisStorage.timeKey);
    }
    private void computequota_16_1(String sql,List<SignFamily> signFamilies_1,List<SignFamily> deleteSignFamilies) {

+ 7 - 0
patient-co-wlyy/pom.xml

@ -271,6 +271,13 @@
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.7</version>
        </dependency>
        <!-- LOGGING begin -->
        <dependency>
            <groupId>org.slf4j</groupId>

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

@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.HttpClientUtil;
import com.yihu.wlyy.util.SystemConf;
import com.yihu.wlyy.util.XMLUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
@ -13,9 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by zhenglingfeng on 2016/10/17.
@ -46,7 +43,7 @@ public class HealthDataService extends BaseService {
            int status = jsonObject.getInt("status");
            if (status == 200){
                result = jsonObject.getString("data");
                result = result.replace("<\\?xml version=\"1.0\" encoding=\"utf-8\"\\?>","");
                result = result.replaceAll("<\\?xml version=\"1.0\" encoding=\"utf-8\"\\?>","");
                if (result.startsWith("error")) {
                    return result;
                }

+ 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 - 5
patient-co-wlyy/src/main/java/com/yihu/wlyy/util/XMLUtil.java

@ -13,6 +13,7 @@ import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultAttribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
@ -544,13 +545,13 @@ public class XMLUtil {
        ele2map(map, rootElement);
        System.out.println(map);
        // 到此xml2map完成,下面的代码是将map转成了json用来观察我们的xml2map转换的是否ok
        String string = JSONObject.fromObject(map).toString();
        String string = net.sf.json.JSONObject.fromObject(map).toString();
        System.out.println(string);
        return map;
    }
    private static void ele2map(Map map, Element ele) {
        System.out.println(ele);
    private static void ele2map(Map map, Element ele)  {
        // 获得当前节点的子节点
        List<Element> elements = ele.elements();
        if (elements.size() == 0) {
@ -560,7 +561,13 @@ public class XMLUtil {
            // 只有一个子节点说明不用考虑list的情况,直接继续递归即可
            Map<String, Object> tempMap = new HashMap<String, Object>();
            ele2map(tempMap, elements.get(0));
            map.put(ele.getName(), tempMap);
            //设置标签属性
            setAttributes(tempMap,elements.get(0));
            if (tempMap.size()==1){
                map.put(ele.getName(), ele.getText());
            }else {
                map.put(ele.getName(), tempMap);
            }
        } else {
            // 多个子节点的话就得考虑list的情况了,比如多个子节点有节点名称相同的
            // 构造一个map用来去重
@ -578,19 +585,52 @@ public class XMLUtil {
                    for (Element element : elements2) {
                        Map<String, Object> tempMap1 = new HashMap<String, Object>();
                        ele2map(tempMap1, element);
                        setAttributes(tempMap1,element);
                        list.add(tempMap1);
                    }
                    map.put(string, list);
                } else {
                    // 同名的数量不大于1则直接递归去
                    Map<String, Object> tempMap1 = new HashMap<String, Object>();
                    ele2map(tempMap1, elements2.get(0));
                    map.put(string, tempMap1);
                    setAttributes(tempMap1,elements2.get(0));
                    if (tempMap1.containsKey(string)) {
                        map.put(string, tempMap1.get(string));
                    }else {
                        map.put(string, tempMap1);
                    }
                }
            }
        }
    }
    /**
     *
     * 设置属性值(xml转map)
     * @param map
     * @param element
     */
    public static void setAttributes(Map map,Element element){
        List list = element.attributes();
        Map<String,Object> attrMap = null;
        DefaultAttribute e = null;
        if (!list.isEmpty()) {
            attrMap = new HashMap<>();
            for (int i = 0; i < list.size(); i++) {
                e = (DefaultAttribute) list.get(i);
                attrMap.put(e.getName(),e.getText());
            }
            attrMap.put("text",element.getText());
        }
        if (attrMap!=null && !attrMap.isEmpty()){
            map.put(element.getName(),attrMap);
        }
    }
    public static Map xmltoMap(String xml) {