Explorar o código

Merge branch 'dev' of lyr/patient-co-management into dev

sand %!s(int64=8) %!d(string=hai) anos
pai
achega
a023b9ed38

src/main/java/com/yihu/wlyy/entity/doctor/DoctorFamousConsultTimesRemain.java → src/main/java/com/yihu/wlyy/entity/doctor/schedule/DoctorFamousConsultTimesRemain.java


+ 73 - 5
src/main/java/com/yihu/wlyy/entity/statistics/PopulationBase.java

@ -13,11 +13,30 @@ import javax.persistence.Table;
@Entity
@Table(name = "wlyy_people_num")
public class PopulationBase extends IdEntity implements java.io.Serializable{
    private String code;// 行政区划代码
    private String name;// 行政区划名称
    private Integer num;// 人口数
    private String type;// 类别 1市 2区
    private Integer year;// 年份
    // 行政区划代码
    private String code;
    // 行政区划名称
    private String name;
    // 人口数
    private Integer num;
    // 类别 1市 2区
    private String type;
    // 年份
    private Integer year;
    // 高血压发病数
    private Integer gxyNum;
    // 糖尿病发布数
    private Integer tnbNum;
    // 65岁以上老年人口数
    private Integer sixFiveNum;
    // 高血压任务数
    private Integer gxyTaskNum;
    // 糖尿病任务数
    private Integer tnbTaskNum;
    // 65岁以上老年人口任务数
    private Integer sixFiveTaskNum;
    // 户籍人口任务数
    private Integer taskNum;
    public String getCode() {
        return code;
@ -54,4 +73,53 @@ public class PopulationBase extends IdEntity implements java.io.Serializable{
    public void setYear(Integer year) {
        this.year = year;
    }
    public Integer getGxyNum() {
        return gxyNum;
    }
    public void setGxyNum(Integer gxyNum) {
        this.gxyNum = gxyNum;
    }
    public Integer getTnbNum() {
        return tnbNum;
    }
    public void setTnbNum(Integer tnbNum) {
        this.tnbNum = tnbNum;
    }
    public Integer getSixFiveNum() {
        return sixFiveNum;
    }
    public void setSixFiveNum(Integer sixFiveNum) {
        this.sixFiveNum = sixFiveNum;
    }
    public Integer getGxyTaskNum() {
        return gxyTaskNum;
    }
    public void setGxyTaskNum(Integer gxyTaskNum) {
        this.gxyTaskNum = gxyTaskNum;
    }
    public Integer getTnbTaskNum() {
        return tnbTaskNum;
    }
    public void setTnbTaskNum(Integer tnbTaskNum) {
        this.tnbTaskNum = tnbTaskNum;
    }
    public Integer getSixFiveTaskNum() {
        return sixFiveTaskNum;
    }
    public void setSixFiveTaskNum(Integer sixFiveTaskNum) {
        this.sixFiveTaskNum = sixFiveTaskNum;
    }
    public Integer getTaskNum() {
        return taskNum;
    }
    public void setTaskNum(Integer taskNum) {
        this.taskNum = taskNum;
    }
}

+ 57 - 49
src/main/java/com/yihu/wlyy/interceptors/DoctorInterceptor.java

@ -3,8 +3,16 @@ package com.yihu.wlyy.interceptors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yihu.wlyy.entity.security.Token;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.util.SystemConf;
import com.yihu.wlyy.util.SystemData;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
/**
 * 医生权限校验
 * @author George
@ -16,55 +24,55 @@ public class DoctorInterceptor extends BaseInterceptor {
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		boolean flag = true;
		try {
//			response.setCharacterEncoding("UTF-8");
//			JSONObject json = getAgent(request);
//			if (json == null) {
//				// 未登录
//				response.getOutputStream().write(error(SystemConf.NOT_LOGIN, "请登录后再操作!").getBytes());
//				return false;
//			}
//			String tokenStr = json.has("token") ? json.getString("token") : "";
//			String uid = json.has("uid") ? json.getString("uid") : "";
//			String imei = json.has("imei") ? json.getString("imei") : "";
//
//			if (StringUtils.isEmpty(tokenStr) || StringUtils.isEmpty(imei) || StringUtils.isEmpty(uid)) {
//				response.getOutputStream().write(error(SystemConf.NOT_LOGIN, "请登录后再操作!").getBytes());
//				return false;
//			}
//
//			Token token = SystemData.doctorTokens.get(uid);
//			if (token == null) {
//				token = tokenDao.findByPatient(uid, 2);
//				if (token != null) {
//					// 加入缓存
//					SystemData.doctorTokens.put(uid, token);
//				}
//			}
//			if (token == null || token.getPlatform() != 2) {
//				// 未登录
//				response.getOutputStream().write(error(SystemConf.NOT_LOGIN, "请登录后再操作!").getBytes());
//				flag = false;
//			} else {
//				if (token.getTimeout().getTime() < new Date().getTime()) {
//					// 登录超时
//					response.getOutputStream().write(error(SystemConf.LOGIN_TIMEOUT, "登录超时,请重新登录").getBytes());
//					flag = false;
//				} else if (!StringUtils.equals(tokenStr, token.getToken()) || !StringUtils.equals(uid, token.getUser()) || !StringUtils.equals(imei, token.getImei())) {
//					// 别处登录
//					response.getOutputStream().write(error(SystemConf.LOGIN_OTHER, "帐号在别处登录,请重新登录").getBytes());
//					flag = false;
//				} else {
//					// 一天只更新一次
//					if (DateUtil.getDays(token.getCzrq(), DateUtil.getNowDateShort()) != 0) {
//						// 今天未更新,则更新缓存
//						token.setCzrq(new Date());
//						// 更新内存
//						SystemData.doctorTokens.put(uid, token);
//						// 更新数据库
//						tokenDao.save(token);
//					}
//				}
//			}
			response.setCharacterEncoding("UTF-8");
			JSONObject json = getAgent(request);
			if (json == null) {
				// 未登录
				response.getOutputStream().write(error(SystemConf.NOT_LOGIN, "请登录后再操作!").getBytes());
				return false;
			}
			String tokenStr = json.has("token") ? json.getString("token") : "";
			String uid = json.has("uid") ? json.getString("uid") : "";
			String imei = json.has("imei") ? json.getString("imei") : "";
			if (StringUtils.isEmpty(tokenStr) || StringUtils.isEmpty(imei) || StringUtils.isEmpty(uid)) {
				response.getOutputStream().write(error(SystemConf.NOT_LOGIN, "请登录后再操作!").getBytes());
				return false;
			}
			Token token = SystemData.doctorTokens.get(uid);
			if (token == null) {
				token = tokenDao.findByPatient(uid, 2);
				if (token != null) {
					// 加入缓存
					SystemData.doctorTokens.put(uid, token);
				}
			}
			if (token == null || token.getPlatform() != 2) {
				// 未登录
				response.getOutputStream().write(error(SystemConf.NOT_LOGIN, "请登录后再操作!").getBytes());
				flag = false;
			} else {
				if (token.getTimeout().getTime() < new Date().getTime()) {
					// 登录超时
					response.getOutputStream().write(error(SystemConf.LOGIN_TIMEOUT, "登录超时,请重新登录").getBytes());
					flag = false;
				} else if (!StringUtils.equals(tokenStr, token.getToken()) || !StringUtils.equals(uid, token.getUser()) || !StringUtils.equals(imei, token.getImei())) {
					// 别处登录
					response.getOutputStream().write(error(SystemConf.LOGIN_OTHER, "帐号在别处登录,请重新登录").getBytes());
					flag = false;
				} else {
					// 一天只更新一次
					if (DateUtil.getDays(token.getCzrq(), DateUtil.getNowDateShort()) != 0) {
						// 今天未更新,则更新缓存
						token.setCzrq(new Date());
						// 更新内存
						SystemData.doctorTokens.put(uid, token);
						// 更新数据库
						tokenDao.save(token);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

+ 1 - 1
src/main/java/com/yihu/wlyy/repository/doctor/FamousDoctorTimesRemainDao.java

@ -1,6 +1,6 @@
package com.yihu.wlyy.repository.doctor;
import com.yihu.wlyy.entity.doctor.DoctorFamousConsultTimesRemain;
import com.yihu.wlyy.entity.doctor.schedule.DoctorFamousConsultTimesRemain;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;

+ 2 - 0
src/main/java/com/yihu/wlyy/repository/statistics/PopulationBaseDao.java

@ -12,4 +12,6 @@ public interface PopulationBaseDao extends PagingAndSortingRepository<Population
    @Query("select a.num from PopulationBase a where a.code = ?1 and a.year = (select max(b.year) from PopulationBase b where b.code = ?1)")
    int findPeopleNumByCode(String code);
    PopulationBase findByCodeAndYear(String code,Integer year);
}

+ 23 - 21
src/main/java/com/yihu/wlyy/service/app/scheduling/DoctorWorkTimeService.java

@ -1,6 +1,6 @@
package com.yihu.wlyy.service.app.scheduling;
import com.yihu.wlyy.entity.doctor.DoctorFamousConsultTimesRemain;
import com.yihu.wlyy.entity.doctor.schedule.DoctorFamousConsultTimesRemain;
import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.doctor.schedule.WlyyDoctorWorkTime;
import com.yihu.wlyy.entity.doctor.schedule.WlyyDoctorWorkWeek;
@ -28,6 +28,7 @@ import java.util.*;
@Transactional
public class DoctorWorkTimeService extends BaseService {
    static Object obj  = new Object();
    @Autowired
    private DoctorWorkTimeDao doctorWorkTimeDao;
    @Autowired
@ -382,32 +383,33 @@ public class DoctorWorkTimeService extends BaseService {
     */
    public boolean setDoctorCurrentConsultTimesRemain(String doctor) {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            DoctorFamousConsultTimesRemain timesRemain = timesRemainDao.findByDoctorAndConsultDate(doctor, df.format(new Date()));
            synchronized (obj) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                DoctorFamousConsultTimesRemain timesRemain = timesRemainDao.findByDoctorAndConsultDate(doctor, df.format(new Date()));
            if (timesRemain != null) {
                int times = timesRemain.getTimesRemain();
                if (timesRemain != null) {
                    int times = timesRemain.getTimesRemain();
                if (times < 1) {
                    throw new Exception("consult-times-error:doctor does not have not consult times remain");
                }
                    if (times < 1) {
                        throw new Exception("consult-times-error:doctor does not have not consult times remain");
                    }
                timesRemain.setTimesRemain(times - 1);
                timesRemainDao.save(timesRemain);
            } else {
                DoctorFamousConsultTimesRemain timesRemainNew = new DoctorFamousConsultTimesRemain();
                WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);
                    timesRemain.setTimesRemain(times - 1);
                    timesRemainDao.save(timesRemain);
                } else {
                    DoctorFamousConsultTimesRemain timesRemainNew = new DoctorFamousConsultTimesRemain();
                    WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);
                if (workTime == null || workTime.getFamousConsultTimes() < 1) {
                    throw new Exception("consult-times-error:doctor does not have consult times setting");
                }
                    if (workTime == null || workTime.getFamousConsultTimes() < 1) {
                        throw new Exception("consult-times-error:doctor does not have consult times setting");
                    }
                timesRemainNew.setDoctor(doctor);
                timesRemainNew.setConsultDate(df.format(new Date()));
                timesRemainNew.setTimesRemain(workTime.getFamousConsultTimes() - 1);
                timesRemainDao.save(timesRemainNew);
                    timesRemainNew.setDoctor(doctor);
                    timesRemainNew.setConsultDate(df.format(new Date()));
                    timesRemainNew.setTimesRemain(workTime.getFamousConsultTimes() - 1);
                    timesRemainDao.save(timesRemainNew);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();

+ 671 - 413
src/main/java/com/yihu/wlyy/service/app/statistics/StatisticsService.java

@ -3,6 +3,8 @@ package com.yihu.wlyy.service.app.statistics;
import com.yihu.wlyy.entity.address.Hospital;
import com.yihu.wlyy.entity.address.Town;
import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.statistics.PopulationBase;
import com.yihu.wlyy.repository.*;
import com.yihu.wlyy.repository.address.CityDao;
import com.yihu.wlyy.repository.address.TownDao;
import com.yihu.wlyy.repository.doctor.DoctorDao;
@ -58,7 +60,7 @@ public class StatisticsService extends BaseService {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    PopulationBaseDao populationBaseDao;
    PopulationBaseDao peopleNumDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
@ -66,42 +68,42 @@ public class StatisticsService extends BaseService {
     * 查询截止某个日期累计量
     *
     * @param endDate 截止日期
     * @param area 区域或机构代码
     * @param level 级别
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public long getTotalAmount(String endDate,String area,int level,String index){
    public long getTotalAmount(String endDate, String area, int level, String index) {
        int todayAmount = 0;
        // 查询语句
        String sql = " select " +
                     "     sum(result) amount" +
                     " from  " +
                     "     wlyy_quota_result " +
                     " where " +
                     "     quato_code = '" + index + "' " +
                     "   and level1_type = ? and del = '1'" +
                     "   and quota_date <= ? ";
        if(level == 4){
                "     sum(result) amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '" + index + "' " +
                "   and level1_type = ? and del = '1'" +
                "   and quota_date <= ? ";
        if (level == 4) {
            // 市级别
            sql += " and city = ? ";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            sql += " and town = ? ";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            sql += " and org_code = ? ";
        }else if(level == 1){
        } else if (level == 1) {
            sql += " and qkdoctor_code = ?";
        }
        Map<String,Object> result = jdbcTemplate.queryForMap(sql,new Object[]{level,endDate,area});
        Map<String, Object> result = jdbcTemplate.queryForMap(sql, new Object[]{level, endDate, area});
        // 截止日期包含当天,则从redis查询当天统计数据
        if(endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0){
            String val =  redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
            if(!StringUtils.isEmpty(val)) {
        if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").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") && valJson.getInt("num") > 0) {
                    todayAmount = valJson.getInt("num");
@ -109,9 +111,9 @@ public class StatisticsService extends BaseService {
            }
        }
        if(result != null){
            return (result.get("amount") != null ? Math.round((double)result.get("amount")) : 0) + todayAmount;
        }else{
        if (result != null) {
            return (result.get("amount") != null ? Math.round((double) result.get("amount")) : 0) + todayAmount;
        } else {
            return 0 + todayAmount;
        }
    }
@ -120,19 +122,39 @@ public class StatisticsService extends BaseService {
     * 查询截止某个日期累计签约率
     *
     * @param endDate 截止日期
     * @param area 区域或机构代码
     * @param level 级别
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public String getSignRate(String endDate,String area,int level) throws Exception {
        long signAmount = getTotalAmount(endDate,area,level,SIGN);
        long serviceNum = populationBaseDao.findPeopleNumByCode(area);
        DecimalFormat df = new DecimalFormat("0.00");
        if(serviceNum > 0){
            return df.format((signAmount*1.00)/serviceNum*100);
        }else{
            throw new Exception("服务人口为0");
    public String getSignRate(String endDate, String area, int level) throws Exception {
        long signAmount = getTotalAmount(endDate, area, level, SIGN);
        PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(area, Calendar.getInstance().get(Calendar.YEAR));
        DecimalFormat df = new DecimalFormat("0.0000");
        if (peopleNum != null && peopleNum.getNum() > 0) {
            return df.format((signAmount * 1.0000) / peopleNum.getNum() * 100);
        } else {
            throw new Exception("户籍人口为0");
        }
    }
    /**
     * 查询截止某个日期签约完成率
     *
     * @param endDate 截止日期
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public String getSignTaskRate(String endDate, String area, int level) throws Exception {
        long signAmount = getTotalAmount(endDate, area, level, SIGN);
        PopulationBase peopleNum = peopleNumDao.findByCodeAndYear(area, Calendar.getInstance().get(Calendar.YEAR));
        DecimalFormat df = new DecimalFormat("0.0000");
        if (peopleNum != null && peopleNum.getTaskNum() > 0) {
            return df.format((signAmount * 1.0000) / peopleNum.getTaskNum() * 100);
        } else {
            throw new Exception("户籍人口任务数为0");
        }
    }
@ -140,11 +162,11 @@ public class StatisticsService extends BaseService {
     * 查询截止某个日期累计建档率
     *
     * @param endDate 截止日期
     * @param area 区域或机构代码
     * @param level 级别
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public String getFilingRate(String endDate,String area,int level){
    public String getFilingRate(String endDate, String area, int level) {
        return "";
    }
@ -153,12 +175,12 @@ public class StatisticsService extends BaseService {
     * 获取某个指标在某个期间的增长量
     *
     * @param startDate 起始时间
     * @param endDate 结束时间
     * @param area 区域或机构代码
     * @param level 级别
     * @param endDate   结束时间
     * @param area      区域或机构代码
     * @param level     级别
     * @return
     */
    public long getIntervalIncrement(String startDate,String endDate,String area,int level,String index){
    public long getIntervalIncrement(String startDate, String endDate, String area, int level, String index) {
        int todayAmount = 0;
        // 查询语句
@ -172,25 +194,25 @@ public class StatisticsService extends BaseService {
                "   and quota_date >= ? " +
                "   and quota_date <= ? ";
        if(level == 4){
        if (level == 4) {
            // 市级别
            sql += " and city = ? ";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            sql += " and town = ? ";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            sql += " and org_code = ? ";
        }else if(level == 1){
        } else if (level == 1) {
            sql += " and qkdoctor_code = ?";
        }
        Map<String,Object> result = jdbcTemplate.queryForMap(sql,new Object[]{level,startDate,endDate,area});
        Map<String, Object> result = jdbcTemplate.queryForMap(sql, new Object[]{level, startDate, endDate, area});
        // 截止日期包含当天,则从redis查询当天统计数据
        if(endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0){
            String val =  redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
            if(!StringUtils.isEmpty(val)) {
        if (endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").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") && valJson.getInt("num") > 0) {
                    todayAmount = valJson.getInt("num");
@ -198,9 +220,9 @@ public class StatisticsService extends BaseService {
            }
        }
        if(result != null){
            return (result.get("amount") != null ? Math.round((double)result.get("amount")) : 0) + todayAmount;
        }else{
        if (result != null) {
            return (result.get("amount") != null ? Math.round((double) result.get("amount")) : 0) + todayAmount;
        } else {
            return 0 + todayAmount;
        }
    }
@ -209,31 +231,31 @@ public class StatisticsService extends BaseService {
     * 查询截止某个日期某个区域后机构各下级累计签约情况
     *
     * @param endDate 截止日期
     * @param area 区域或机构代码
     * @param level 级别
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public JSONArray getLowLevelSignDetail(String endDate, String area, int level){
    public JSONArray getLowLevelSignDetail(String endDate, String area, int level) {
        String areaField = "";
        String lowLevelField = "";
        String lowLevelName = "";
        if(level == 4){
        if (level == 4) {
            // 市级别
            areaField = "city";
            lowLevelField = "town";
            lowLevelName = "town_name";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
            lowLevelField = "org_code";
            lowLevelName = "org_name";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
            lowLevelField = "qkdoctor_code";
            lowLevelName = "qkdoctor_name";
        }else if(level == 1){
        } else if (level == 1) {
        }
        // 查询语句
@ -258,19 +280,19 @@ public class StatisticsService extends BaseService {
                "   and a." + areaField + " = ? " +
                " group by a." + lowLevelField + ",a." + lowLevelName;
        List<Map<String,Object>> resultList = jdbcTemplate.queryForList(sql,new Object[]{level - 1,endDate,area});
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{level - 1, endDate, area});
        if(resultList != null){
        if (resultList != null) {
            DecimalFormat df = new DecimalFormat("0.00");
            for(Map<String,Object> map : resultList){
                map.put("amount",map.get("amount") != null ? Math.round((double)map.get("amount")) : 0);
            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") != null ? Math.round((double) map.get("amount")) : 0);
//                if(level > 3){
//                    map.put("rate",df.format(map.get("rate") != null ? map.get("rate") : 0));
//                }
            }
            return new JSONArray(resultList);
        }else {
        } else {
            return new JSONArray();
        }
    }
@ -279,31 +301,31 @@ public class StatisticsService extends BaseService {
     * 查询截止某个日期某个区域后机构各下级累计建档情况
     *
     * @param endDate 截止日期
     * @param area 区域或机构代码
     * @param level 级别
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public JSONArray getLowLevelFilingDetail(String endDate, String area, int level){
    public JSONArray getLowLevelFilingDetail(String endDate, String area, int level) {
        String areaField = "";
        String lowLevelField = "";
        String lowLevelName = "";
        if(level == 4){
        if (level == 4) {
            // 市级别
            areaField = "city";
            lowLevelField = "town";
            lowLevelName = "town_name";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
            lowLevelField = "org_code";
            lowLevelName = "org_name";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
            lowLevelField = "qkdoctor_code";
            lowLevelName = "qkdoctor_name";
        }else if(level == 1){
        } else if (level == 1) {
        }
        // 查询语句
@ -320,15 +342,15 @@ public class StatisticsService extends BaseService {
                "   and " + areaField + " = ? " +
                " group by " + lowLevelField + "," + lowLevelName;
        List<Map<String,Object>> resultList = jdbcTemplate.queryForList(sql,new Object[]{endDate,area});
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{endDate, area});
        if(resultList != null){
            for(Map<String,Object> map : resultList){
                map.put("amount",map.get("amount") != null ? Math.round((double)map.get("amount")) : 0);
        if (resultList != null) {
            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") != null ? Math.round((double) map.get("amount")) : 0);
            }
            return new JSONArray(resultList);
        }else {
        } else {
            return new JSONArray();
        }
    }
@ -337,33 +359,49 @@ public class StatisticsService extends BaseService {
     * 查询截止某个日期某个区域后机构各下级指标累计情况
     *
     * @param endDate 截止日期
     * @param area 区域或机构代码
     * @param level 级别
     * @param area    区域或机构代码
     * @param level   级别
     * @return
     */
    public JSONArray getLowLevelTotalDetail(String endDate, String area, int level,String index,int sort){
    public JSONArray getLowLevelTotalDetail(String endDate, String area, int level, String index, int sort, String lowLevel) throws Exception {
        String areaField = "";
        String lowLevelField = "";
        String lowLevelName = "";
        if(level == 4){
        if (level == 4) {
            // 市级别
            areaField = "city";
            lowLevelField = "town";
            lowLevelName = "town_name";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
            lowLevelField = "org_code";
            lowLevelName = "org_name";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
            lowLevelField = "qkdoctor_code";
            lowLevelName = "qkdoctor_name";
        }else if(level == 1){
        } 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 sql = " select " +
                "     ifnull(" + lowLevelField + ",'') code " +
@ -378,60 +416,60 @@ public class StatisticsService extends BaseService {
                "   and " + areaField + " = ? " +
                " group by " + lowLevelField + "," + lowLevelName;
        if(sort == 1){
        if (sort == 1) {
            sql += " order by amount desc";
        }else{
        } else {
            sql += " order by amount asc ";
        }
        List<Map<String,Object>> resultList = jdbcTemplate.queryForList(sql,new Object[]{level - 1,endDate,area});
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{(StringUtils.isEmpty(lowLevel)?(level - 1):lowLevel), endDate, area});
        // 结果为空时,自建结果集
        if(resultList == null || resultList.size() < 1){
        if (resultList == null || resultList.size() < 1) {
            resultList = new ArrayList<>();
            if(level  == 4){
            if (level == 4) {
                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",Double.valueOf("0.0"));
                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", Double.valueOf("0.0"));
                    }
                }
            }else if(level == 3){
            } else if (level == 3) {
                List<Hospital> hospitals = hospitalDao.findByTownCode(area);
                if(hospitals != null){
                    for(Hospital hos : hospitals){
                        Map<String,Object> obj =  new HashMap<>();
                        obj.put("code",hos.getCode());
                        obj.put("name",hos.getName());
                        obj.put("amount",Double.valueOf("0.0"));
                if (hospitals != null) {
                    for (Hospital hos : hospitals) {
                        Map<String, Object> obj = new HashMap<>();
                        obj.put("code", hos.getCode());
                        obj.put("name", hos.getName());
                        obj.put("amount", Double.valueOf("0.0"));
                    }
                }
            }else if(level == 2){
                List<Doctor> doctors = doctorDao.findDoctorByLevelAndHospital(area,2);
                if(doctors != null){
                    for(Doctor doc : doctors){
                        Map<String,Object> obj =  new HashMap<>();
                        obj.put("code",doc.getCode());
                        obj.put("name",doc.getName());
                        obj.put("amount",Double.valueOf("0.0"));
            } else if (level == 2) {
                List<Doctor> doctors = doctorDao.findDoctorByLevelAndHospital(area, 2);
                if (doctors != null) {
                    for (Doctor doc : doctors) {
                        Map<String, Object> obj = new HashMap<>();
                        obj.put("code", doc.getCode());
                        obj.put("name", doc.getName());
                        obj.put("amount", Double.valueOf("0.0"));
                    }
                }
            }
        }
        if(resultList != null){
            for(Map<String,Object> map : resultList){
                map.put("amount",map.get("amount") != null ? Math.round((double)map.get("amount")) : 0);
        if (resultList != null) {
            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") != null ? Math.round((double) map.get("amount")) : 0);
                // 截止日期包含当天,则从redis查询当天统计数据
                if(endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0){
                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 - 1) + ":" + code);
                    if(!StringUtils.isEmpty(val)) {
                    String val = redisTemplate.opsForValue().get("quota:" + index + ":" + (StringUtils.isEmpty(lowLevel)?(level - 1):lowLevel) + ":" + code);
                    if (!StringUtils.isEmpty(val)) {
                        JSONObject valJson = new JSONObject(val);
                        if (valJson.has("num") && valJson.getInt("num") > 0) {
                            map.put("amount",(long)map.get("amount") + valJson.getInt("num"));
                            map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
                        }
                    }
                }
@ -442,18 +480,18 @@ public class StatisticsService extends BaseService {
                    long map1value = (long) o1.get("amount");
                    long map2value = (long) o2.get("amount");
                    if(map1value - map2value > 0){
                    if (map1value - map2value > 0) {
                        return sort == 1 ? -1 : 1;
                    }else if(map1value - map2value < 0){
                    } else if (map1value - map2value < 0) {
                        return sort == 1 ? 1 : -1;
                    }else{
                    } else {
                        return 0;
                    }
                }
            });
            return new JSONArray(resultList);
        }else {
        } else {
            return new JSONArray();
        }
    }
@ -462,35 +500,51 @@ public class StatisticsService extends BaseService {
     * 查询某个日期范围内某个区域或机构各下级指标增量情况
     *
     * @param startDate 起始日期
     * @param endDate 结束日期
     * @param area 区域或机构代码
     * @param level 级别
     * @param index 指标
     * @param endDate   结束日期
     * @param area      区域或机构代码
     * @param level     级别
     * @param index     指标
     * @return
     */
    public JSONArray getLowLevelIncrementDetail(String startDate,String endDate,String area,int level,String index,int sort){
    public JSONArray getLowLevelIncrementDetail(String startDate, String endDate, String area, int level, String index, int sort,String lowLevel) throws Exception {
        String areaField = "";
        String lowLevelField = "";
        String lowLevelName = "";
        if(level == 4){
        if (level == 4) {
            // 市级别
            areaField = "city";
            lowLevelField = "town";
            lowLevelName = "town_name";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
            lowLevelField = "org_code";
            lowLevelName = "org_name";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
            lowLevelField = "qkdoctor_code";
            lowLevelName = "qkdoctor_name";
        }else if(level == 1){
        } 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 sql = " select " +
                "     ifnull(" + lowLevelField + ",'') code " +
@ -506,64 +560,64 @@ public class StatisticsService extends BaseService {
                "   and " + areaField + " = ? " +
                " group by " + lowLevelField + "," + lowLevelName;
        if(sort == 1){
        if (sort == 1) {
            sql += " order by amount desc";
        }else{
        } else {
            sql += " order by amount asc ";
        }
        List<Map<String,Object>> resultList = jdbcTemplate.queryForList(sql,new Object[]{level - 1,startDate,endDate,area});
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, new Object[]{(StringUtils.isEmpty(lowLevel)?(level - 1):lowLevel), startDate, endDate, area});
        // 结果为空时,自建结果集
        if(resultList == null || resultList.size() < 1){
        if (resultList == null || resultList.size() < 1) {
            resultList = new ArrayList<>();
            if(level  == 4){
            if (level == 4) {
                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",Double.valueOf("0.0"));
                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", Double.valueOf("0.0"));
                        resultList.add(obj);
                    }
                }
            }else if(level == 3){
            } else if (level == 3) {
                List<Hospital> hospitals = hospitalDao.findByTownCode(area);
                if(hospitals != null){
                    for(Hospital hos : hospitals){
                        Map<String,Object> obj =  new HashMap<>();
                        obj.put("code",hos.getCode());
                        obj.put("name",hos.getName());
                        obj.put("amount",Double.valueOf("0.0"));
                if (hospitals != null) {
                    for (Hospital hos : hospitals) {
                        Map<String, Object> obj = new HashMap<>();
                        obj.put("code", hos.getCode());
                        obj.put("name", hos.getName());
                        obj.put("amount", Double.valueOf("0.0"));
                        resultList.add(obj);
                    }
                }
            }else if(level == 2){
                List<Doctor> doctors = doctorDao.findDoctorByLevelAndHospital(area,2);
                if(doctors != null){
                    for(Doctor doc : doctors){
                        Map<String,Object> obj =  new HashMap<>();
                        obj.put("code",doc.getCode());
                        obj.put("name",doc.getName());
                        obj.put("amount",Double.valueOf("0.0"));
            } else if (level == 2) {
                List<Doctor> doctors = doctorDao.findDoctorByLevelAndHospital(area, 2);
                if (doctors != null) {
                    for (Doctor doc : doctors) {
                        Map<String, Object> obj = new HashMap<>();
                        obj.put("code", doc.getCode());
                        obj.put("name", doc.getName());
                        obj.put("amount", Double.valueOf("0.0"));
                        resultList.add(obj);
                    }
                }
            }
        }
        if(resultList != null){
            for(Map<String,Object> map : resultList){
                map.put("amount",map.get("amount") != null ? Math.round((double)map.get("amount")) : 0);
        if (resultList != null) {
            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") != null ? Math.round((double) map.get("amount")) : 0);
                // 截止日期包含当天,则从redis查询当天统计数据
                if(endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0){
                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 - 1) + ":" + code);
                    if(!StringUtils.isEmpty(val)) {
                    String val = redisTemplate.opsForValue().get("quota:" + index + ":" + (StringUtils.isEmpty(lowLevel)?(level - 1):lowLevel) + ":" + code);
                    if (!StringUtils.isEmpty(val)) {
                        JSONObject valJson = new JSONObject(val);
                        if (valJson.has("num") && valJson.getInt("num") > 0) {
                            map.put("amount",(long)map.get("amount") + valJson.getInt("num"));
                            map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
                        }
                    }
                }
@ -574,18 +628,18 @@ public class StatisticsService extends BaseService {
                    long map1value = (long) o1.get("amount");
                    long map2value = (long) o2.get("amount");
                    if(map1value - map2value > 0){
                    if (map1value - map2value > 0) {
                        return sort == 1 ? -1 : 1;
                    }else if(map1value - map2value < 0){
                    } else if (map1value - map2value < 0) {
                        return sort == 1 ? 1 : -1;
                    }else{
                    } else {
                        return 0;
                    }
                }
            });
            return new JSONArray(resultList);
        }else {
        } else {
            return new JSONArray();
        }
    }
@ -594,22 +648,20 @@ public class StatisticsService extends BaseService {
     * 获取时间范围内按间隔统计指标增量
     *
     * @param startDate 起始时间
     * @param endDate 结束时间
     * @param interval 时间间隔
     * @param area 区域或机构代码
     * @param level 级别
     * @param index 指标
     * @param endDate   结束时间
     * @param interval  时间间隔
     * @param area      区域或机构代码
     * @param level     级别
     * @param index     指标
     * @return
     */
    public JSONArray getDateIncrementDetail(String startDate,String endDate,int interval,String area,int level,String index) throws Exception {
        if(interval == 1) {
            return dateStatistics(startDate,endDate,area,level,index);
        }
        else if(interval == 2){
            return weekStatistics(startDate,endDate,area,level,index);
        }
        else if(interval == 3) {
            return monthStatistics(startDate,endDate,area,level,index);
    public JSONArray getDateIncrementDetail(String startDate, String endDate, int interval, String area, int level, String index) throws Exception {
        if (interval == 1) {
            return dateStatistics(startDate, endDate, area, level, index);
        } else if (interval == 2) {
            return weekStatistics(startDate, endDate, area, level, index);
        } else if (interval == 3) {
            return monthStatistics(startDate, endDate, area, level, index);
        }
        return new JSONArray();
@ -625,21 +677,21 @@ public class StatisticsService extends BaseService {
     * @param index
     * @return
     */
    private JSONArray dateStatistics(String startDate,String endDate,String area,int level,String index){
    private JSONArray dateStatistics(String startDate, String endDate, String area, int level, String index) {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String areaField = "";
        String sql ="";
        String sql = "";
        if(level == 4){
        if (level == 4) {
            // 市级别
            areaField = "city";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        }else if(level == 1){
        } else if (level == 1) {
            // 机构级别
            areaField = "qkdoctor_code";
        }
@ -647,59 +699,59 @@ public class StatisticsService extends BaseService {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // 起始日期
        Calendar start = Calendar.getInstance();
        start.setTime(DateUtil.strToDate(startDate,DateUtil.YYYY_MM_DD));
        start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
        //结束日期
        Calendar end = Calendar.getInstance();
        end.setTime(DateUtil.strToDate(endDate,DateUtil.YYYY_MM_DD));
        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){
        if (startDate.compareTo(endDate) == 0) {
            flag = false;
        }
        // 计算统计日期
        while(flag){
        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){
            next.add(Calendar.DATE, 1);
            if (df.format(next.getTime()).compareTo(endDate) < 0) {
                days.add(next);
            }else{
            } else {
                days.add(end);
                flag = false;
            }
        }
        Map<String,JSONObject> countResult = new HashMap<>();
        Map<String, JSONObject> countResult = new HashMap<>();
        // 统计预计构建
        for(int i = 0;i < days.size() ; i++) {
        for (int i = 0; i < days.size(); i++) {
            String startStr = "";
            long amount = 0;
            startStr = df.format(days.get(i).getTime());
            // 当前范围包含当天,则需添加当天的统计数据
            if(startStr.compareTo(today) == 0) {
                String val =  redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                if(!StringUtils.isEmpty(val)) {
            if (startStr.compareTo(today) == 0) {
                String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                if (!StringUtils.isEmpty(val)) {
                    JSONObject valJson = new JSONObject(val);
                    if (valJson.has("num") && valJson.getInt("num") > 0) {
                        amount = (long)valJson.getInt("num");
                        amount = (long) valJson.getInt("num");
                    }
                }
            }
            JSONObject range = new JSONObject();
            range.put("range",startStr);
            range.put("amount",amount);
            countResult.put(startStr,range);
            range.put("range", startStr);
            range.put("amount", amount);
            countResult.put(startStr, range);
        }
        sql =  " select " +
        sql = " select " +
                "     ifnull(quota_date,'') as 'range' " +
                "     ,ifnull(sum(result),0) amount " +
                " from  " +
@ -712,15 +764,15 @@ public class StatisticsService extends BaseService {
                "   and " + areaField + " = '" + area + "' " +
                "   group by quota_date ";
        List<Map<String,Object>> resultList = jdbcTemplate.queryForList(sql);
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
        if(resultList != null){
            for(Map<String,Object> map : resultList){
                if(countResult.containsKey(map.get("range").toString())){
                    JSONObject range = (JSONObject)countResult.get(map.get("range").toString());
        if (resultList != null) {
            for (Map<String, Object> map : resultList) {
                if (countResult.containsKey(map.get("range").toString())) {
                    JSONObject range = (JSONObject) countResult.get(map.get("range").toString());
                    long amount = range.getLong("amount");
                    long resultAmount = map.get("amount") != null ? Math.round((double)map.get("amount")) : 0;
                    range.put("amount",amount + resultAmount);
                    long resultAmount = map.get("amount") != null ? Math.round((double) map.get("amount")) : 0;
                    range.put("amount", amount + resultAmount);
                }
            }
@ -729,18 +781,18 @@ public class StatisticsService extends BaseService {
            result.sort(new Comparator<JSONObject>() {
                @Override
                public int compare(JSONObject o1, JSONObject o2) {
                    if(o1.getString("range").compareTo(o2.getString("range")) > 0){
                    if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
                        return 1;
                    }else if(o1.getString("range").compareTo(o2.getString("range")) < 0){
                    } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
                        return -1;
                    }else{
                    } else {
                        return 0;
                    }
                }
            });
            return new JSONArray(result);
        }else {
        } else {
            return new JSONArray();
        }
    }
@ -755,21 +807,21 @@ public class StatisticsService extends BaseService {
     * @param index
     * @return
     */
    private JSONArray weekStatistics(String startDate,String endDate,String area,int level,String index) throws Exception {
    private JSONArray weekStatistics(String startDate, String endDate, String area, int level, String index) throws Exception {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String areaField = "";
        String sql ="";
        String sql = "";
        if(level == 4){
        if (level == 4) {
            // 市级别
            areaField = "city";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        }else if(level == 1){
        } else if (level == 1) {
            // 机构级别
            areaField = "qkdoctor_code";
        }
@ -777,12 +829,12 @@ public class StatisticsService extends BaseService {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // 起始日期
        Calendar start = Calendar.getInstance();
        start.setTime(DateUtil.strToDate(startDate,DateUtil.YYYY_MM_DD));
        start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
        // 第一个统计周期结束日期
        String firstEnd = "";
        // 结束日期
        Calendar end = Calendar.getInstance();
        end.setTime(DateUtil.strToDate(endDate,DateUtil.YYYY_MM_DD));
        end.setTime(DateUtil.strToDate(endDate, DateUtil.YYYY_MM_DD));
        // 起始日期为周几
        int week = start.get(Calendar.DAY_OF_WEEK);
        int incre = 7 - week + 1;
@ -793,33 +845,33 @@ public class StatisticsService extends BaseService {
        boolean flag = true;
        int i = 0;
        if(startDate.compareTo(endDate) == 0){
        if (startDate.compareTo(endDate) == 0) {
            flag = false;
            days.add(end);
            firstEnd = df.format(end.getTime());
        }
        // 计算统计日期
        while(flag){
        while (flag) {
            Calendar next = Calendar.getInstance();
            next.setTime(days.get(days.size() - 1).getTime());
            if(i == 0) {
                if(incre != 7) {
            if (i == 0) {
                if (incre != 7) {
                    next.add(Calendar.DATE, incre);
                }
            }else{
            } else {
                next.add(Calendar.DATE, 7);
            }
            if(df.format(next.getTime()).compareTo(df.format(end.getTime())) < 0){
            if (df.format(next.getTime()).compareTo(df.format(end.getTime())) < 0) {
                days.add(next);
                if(i == 0){
                if (i == 0) {
                    firstEnd = df.format(next.getTime());
                }
            }else{
            } else {
                days.add(end);
                flag = false;
                if(i == 0){
                if (i == 0) {
                    firstEnd = df.format(end.getTime());
                }
            }
@ -828,28 +880,28 @@ public class StatisticsService extends BaseService {
        }
        // 结果集
        Map<String,JSONObject> countResult = new HashMap<>();
        Map<String, JSONObject> countResult = new HashMap<>();
        // 算出每个查询周期
        for(int j = 0; j < days.size() - 1;j++){
        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){
            if (j == 0) {
                startStr = df.format(days.get(j).getTime());
            }else{
            } else {
                Calendar cal = Calendar.getInstance();
                cal.setTime(days.get(j).getTime());
                cal.add(Calendar.DATE,1);
                cal.add(Calendar.DATE, 1);
                startStr = df.format(cal.getTime());
            }
            // 当前范围包含当天,则需添加当天的统计数据
            if(startStr.compareTo(today) <= 0 && endStr.compareTo(today) >= 0) {
                String val =  redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                if(!StringUtils.isEmpty(val)) {
            if (startStr.compareTo(today) <= 0 && endStr.compareTo(today) >= 0) {
                String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                if (!StringUtils.isEmpty(val)) {
                    JSONObject valJson = new JSONObject(val);
                    if (valJson.has("num") && valJson.getInt("num") > 0) {
                        amount = valJson.getInt("num");
@ -857,16 +909,16 @@ public class StatisticsService extends BaseService {
                }
            }
            JSONObject range =  new JSONObject();
            JSONObject range = new JSONObject();
            range.put("range",startStr);
            range.put("amount",amount);
            range.put("range", startStr);
            range.put("amount", amount);
            countResult.put(startStr + ":" + endStr,range);
            countResult.put(startStr + ":" + endStr, range);
        }
        // 查询时间范围内所有记录
        sql =  " select " +
        sql = " select " +
                "     ifnull(quota_date,'') as 'range' " +
                "     ,ifnull(sum(result),0) amount " +
                " from  " +
@ -879,45 +931,45 @@ public class StatisticsService extends BaseService {
                "   and " + areaField + " = '" + area + "' " +
                "   group by quota_date ";
        List<Map<String,Object>> resultList = jdbcTemplate.queryForList(sql);
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
        if(resultList != null){
        if (resultList != null) {
            // 计算结果
            for(Map<String,Object> map : resultList){
                long resultAmount = map.get("amount") != null ? Math.round((double)map.get("amount")) : 0;
            for (Map<String, Object> map : resultList) {
                long resultAmount = map.get("amount") != null ? Math.round((double) map.get("amount")) : 0;
                String range = map.get("range").toString();
                if(org.apache.commons.lang3.StringUtils.isNotEmpty(range)){
                if (org.apache.commons.lang3.StringUtils.isNotEmpty(range)) {
                    // 起始日期
                    String startStr = "";
                    // 结束日期
                    String endStr = "";
                    if(range.compareTo(startDate) >= 0 && range.compareTo(firstEnd) <= 0){
                        startStr =  startDate;
                    if (range.compareTo(startDate) >= 0 && range.compareTo(firstEnd) <= 0) {
                        startStr = startDate;
                        endStr = firstEnd;
                    }else{
                    } else {
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(df.parse(range));
                        int calWeek =  cal.get(Calendar.DAY_OF_WEEK) - 2;
                        int calWeek = cal.get(Calendar.DAY_OF_WEEK) - 2;
                        if(calWeek == -1){
                        if (calWeek == -1) {
                            cal.add(Calendar.DATE, -6);
                        }else{
                        } else {
                            cal.add(Calendar.DATE, -calWeek);
                        }
                        startStr = df.format(cal.getTime());
                        cal.add(Calendar.DATE,6);
                        cal.add(Calendar.DATE, 6);
                        endStr = df.format(cal.getTime());
                        if(endStr.compareTo(endDate) > 0){
                        if (endStr.compareTo(endDate) > 0) {
                            endStr = endDate;
                        }
                    }
                    JSONObject json = countResult.get(startStr + ":" + endStr);
                    json.put("amount",json.getLong("amount") + resultAmount);
                    json.put("amount", json.getLong("amount") + resultAmount);
                }
            }
@ -927,18 +979,18 @@ public class StatisticsService extends BaseService {
            result.sort(new Comparator<JSONObject>() {
                @Override
                public int compare(JSONObject o1, JSONObject o2) {
                    if(o1.getString("range").compareTo(o2.getString("range")) > 0){
                    if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
                        return 1;
                    }else if(o1.getString("range").compareTo(o2.getString("range")) < 0){
                    } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
                        return -1;
                    }else{
                    } else {
                        return 0;
                    }
                }
            });
            return new JSONArray(result);
        }else{
        } else {
            return new JSONArray();
        }
    }
@ -954,21 +1006,21 @@ public class StatisticsService extends BaseService {
     * @return
     * @throws Exception
     */
    private JSONArray monthStatistics(String startDate,String endDate,String area,int level,String index) throws Exception{
    private JSONArray monthStatistics(String startDate, String endDate, String area, int level, String index) throws Exception {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String areaField = "";
        String sql ="";
        String sql = "";
        if(level == 4){
        if (level == 4) {
            // 市级别
            areaField = "city";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        }else if(level == 1){
        } else if (level == 1) {
            // 机构级别
            areaField = "qkdoctor_code";
        }
@ -976,10 +1028,10 @@ public class StatisticsService extends BaseService {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // 起始日期
        Calendar start = Calendar.getInstance();
        start.setTime(DateUtil.strToDate(startDate,DateUtil.YYYY_MM_DD));
        start.setTime(DateUtil.strToDate(startDate, DateUtil.YYYY_MM_DD));
        // 结束日期
        Calendar end = Calendar.getInstance();
        end.setTime(DateUtil.strToDate(endDate,DateUtil.YYYY_MM_DD));
        end.setTime(DateUtil.strToDate(endDate, DateUtil.YYYY_MM_DD));
        // 第一个结束日期
        String firstEnd = "";
@ -990,34 +1042,34 @@ public class StatisticsService extends BaseService {
        boolean flag = true;
        int k = 0;
        if(startDate.compareTo(endDate) == 0){
        if (startDate.compareTo(endDate) == 0) {
            flag = false;
            days.add(end);
            firstEnd = df.format(end.getTime());
        }
        // 统计日期计算
        while(flag){
        while (flag) {
            Calendar next = Calendar.getInstance();
            next.setTime(days.get(days.size() - 1).getTime());
            if(k == 0){
            if (k == 0) {
                next.add(Calendar.MONTH, 1);
            }else{
            } 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){
            if (df.format(next.getTime()).compareTo(df.format(end.getTime())) < 0) {
                days.add(next);
                if(k == 0){
                if (k == 0) {
                    firstEnd = df.format(next.getTime());
                }
            }else{
            } else {
                days.add(end);
                flag = false;
                if(k == 0){
                if (k == 0) {
                    firstEnd = df.format(end.getTime());
                }
            }
@ -1025,27 +1077,27 @@ public class StatisticsService extends BaseService {
        }
        // 统计结果
        Map<String,JSONObject> countResult = new HashMap<>();
        Map<String, JSONObject> countResult = new HashMap<>();
        for(int i = 0; i < days.size() - 1; i++) {
        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){
            if (i == 0) {
                startStr = df.format(days.get(i).getTime());
            }else{
            } else {
                Calendar cal = Calendar.getInstance();
                cal.setTime(days.get(i).getTime());
                cal.add(Calendar.DATE,1);
                cal.add(Calendar.DATE, 1);
                startStr = df.format(cal.getTime());
            }
            // 当天数据计算
            if(startStr.compareTo(today) <= 0 && endStr.compareTo(today) >= 0) {
                String val =  redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                if(!StringUtils.isEmpty(val)) {
            if (startStr.compareTo(today) <= 0 && endStr.compareTo(today) >= 0) {
                String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + area);
                if (!StringUtils.isEmpty(val)) {
                    JSONObject valJson = new JSONObject(val);
                    if (valJson.has("num") && valJson.getInt("num") > 0) {
                        amount = valJson.getInt("num");
@ -1053,16 +1105,16 @@ public class StatisticsService extends BaseService {
                }
            }
            JSONObject range =  new JSONObject();
            JSONObject range = new JSONObject();
            range.put("range",startStr);
            range.put("amount",amount);
            range.put("range", startStr);
            range.put("amount", amount);
            countResult.put(startStr + ":" + endStr,range);
            countResult.put(startStr + ":" + endStr, range);
        }
        // 查询时间范围内所有记录
        sql =  " select " +
        sql = " select " +
                "     ifnull(quota_date,'') as 'range' " +
                "     ,ifnull(sum(result),0) amount " +
                " from  " +
@ -1075,41 +1127,41 @@ public class StatisticsService extends BaseService {
                "   and " + areaField + " = '" + area + "' " +
                "   group by quota_date ";
        List<Map<String,Object>> resultList = jdbcTemplate.queryForList(sql);
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
        if(resultList != null){
        if (resultList != null) {
            // 计算结果
            for(Map<String,Object> map : resultList){
                long resultAmount = map.get("amount") != null ? Math.round((double)map.get("amount")) : 0;
            for (Map<String, Object> map : resultList) {
                long resultAmount = map.get("amount") != null ? Math.round((double) map.get("amount")) : 0;
                String range = map.get("range").toString();
                if(org.apache.commons.lang3.StringUtils.isNotEmpty(range)){
                if (org.apache.commons.lang3.StringUtils.isNotEmpty(range)) {
                    // 起始日期
                    String startStr = "";
                    // 结束日期
                    String endStr = "";
                    if(range.compareTo(startDate) >= 0 && range.compareTo(firstEnd) <= 0){
                        startStr =  startDate;
                    if (range.compareTo(startDate) >= 0 && range.compareTo(firstEnd) <= 0) {
                        startStr = startDate;
                        endStr = firstEnd;
                    }else{
                    } else {
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(df.parse(range));
                        cal.set(Calendar.DATE, 1);
                        startStr = df.format(cal.getTime());
                        cal.add(Calendar.MONTH,1);
                        cal.set(Calendar.DATE,1);
                        cal.add(Calendar.DATE,-1);
                        cal.add(Calendar.MONTH, 1);
                        cal.set(Calendar.DATE, 1);
                        cal.add(Calendar.DATE, -1);
                        endStr = df.format(cal.getTime());
                        if(endStr.compareTo(endDate) > 0){
                        if (endStr.compareTo(endDate) > 0) {
                            endStr = endDate;
                        }
                    }
                    JSONObject json = countResult.get(startStr + ":" + endStr);
                    json.put("amount",json.getLong("amount") + resultAmount);
                    json.put("amount", json.getLong("amount") + resultAmount);
                }
            }
@ -1119,18 +1171,18 @@ public class StatisticsService extends BaseService {
            result.sort(new Comparator<JSONObject>() {
                @Override
                public int compare(JSONObject o1, JSONObject o2) {
                    if(o1.getString("range").compareTo(o2.getString("range")) > 0){
                    if (o1.getString("range").compareTo(o2.getString("range")) > 0) {
                        return 1;
                    }else if(o1.getString("range").compareTo(o2.getString("range")) < 0){
                    } else if (o1.getString("range").compareTo(o2.getString("range")) < 0) {
                        return -1;
                    }else{
                    } else {
                        return 0;
                    }
                }
            });
            return new JSONArray(result);
        }else{
        } else {
            return new JSONArray();
        }
    }
@ -1140,25 +1192,25 @@ public class StatisticsService extends BaseService {
     * 获取二级维度下指标时间范围内增量
     *
     * @param startDate 起始时间
     * @param endDate 结束时间
     * @param area 区域或机构代码
     * @param level 级别
     * @param index 指标
     * @param endDate   结束时间
     * @param area      区域或机构代码
     * @param level     级别
     * @param index     指标
     * @return
     */
    public JSONArray getLevelTwoIndexIncrement(String startDate,String endDate,String area,int level,String index){
    public JSONArray getLevelTwoIndexIncrement(String startDate, String endDate, String area, int level, String index) {
        String areaField = "";
        if(level == 4){
        if (level == 4) {
            // 市级别
            areaField = "city";
        }else if(level == 3){
        } else if (level == 3) {
            // 区、城镇级别
            areaField = "town";
        }else if(level == 2){
        } else if (level == 2) {
            // 机构级别
            areaField = "org_code";
        }else if(level == 1){
        } else if (level == 1) {
            // 团队
            areaField = "qkdoctor_code";
        }
@ -1174,175 +1226,381 @@ public class StatisticsService extends BaseService {
                "   and level1_type = ? and del = '1'";
        if(!org.apache.commons.lang3.StringUtils.isEmpty(startDate)){
        if (!org.apache.commons.lang3.StringUtils.isEmpty(startDate)) {
            sql += "   and quota_date >= ? ";
        }
        sql +=  "   and quota_date <= ? " +
        sql += "   and quota_date <= ? " +
                "   and " + areaField + " = ? " +
                " group by level2_type,level2_type_name";
        List<Map<String,Object>> resultList = null;
        List<Map<String, Object>> resultList = null;
        if(org.apache.commons.lang3.StringUtils.isEmpty(startDate)){
            resultList = jdbcTemplate.queryForList(sql,new Object[]{level,endDate,area});
        }else {
        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) {
        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"));
            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"));
                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"));
                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"));
            } 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"));
                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"));
                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"));
                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"));
                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", "100");
                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"));
            } 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"));
                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"));
                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"));
                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"));
                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"));
                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);
            }
        }
        if(resultList != null){
        if (resultList != null) {
            long total = 0;
            for(Map<String,Object> map : resultList){
                map.put("amount",map.get("amount") == null ? 0 : Math.round((double)map.get("amount")));
            for (Map<String, Object> map : resultList) {
                map.put("amount", map.get("amount") == null ? 0 : Math.round((double) map.get("amount")));
                // 当天数据统计
                if(endDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) >= 0){
                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)) {
                    String val = redisTemplate.opsForValue().get("quota:" + index + ":" + level + ":" + code + ":" + area);
                    if (!StringUtils.isEmpty(val)) {
                        JSONObject valJson = new JSONObject(val);
                        if (valJson.has("num") && valJson.getInt("num") > 0) {
                            map.put("amount",(long)map.get("amount") + valJson.getInt("num"));
                            map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
                        }
                    }
                }
                if(index.equals(GROUP)){
                if (index.equals(GROUP)) {
                    // 分组指标总数算法
                    String code = map.get("code") != null ? String.valueOf(map.get("code")) : "";
                    if(!code.equals("2")){
                        total += (long)map.get("amount");
                    if (code.equals("1") || code.equals("2") || code.equals("3")) {
                        total += (long) map.get("amount");
                    }
                }else{
                    total += (long)map.get("amount");
                } else {
                    total += (long) map.get("amount");
                }
            }
            if(!index.equals(AGE)) {
                DecimalFormat df = new DecimalFormat("0.00");
                double rateTotal = 0.00;
            if (!index.equals(AGE)) {
                DecimalFormat df = new DecimalFormat("0.0000");
                double rateTotal = 0.0000;
                for (Map<String, Object> map : resultList) {
                    double rateG = (total > 0 ? ((long) map.get("amount")) * 1.00 / total * 100 : 0);
                    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));
                    rateTotal += rateG;
//                    if (index.equals(GROUP)) {
//                        if (code.equals("1") || code.equals("2") || code.equals("3")) {
//                            rateTotal += rateG;
//                        }
//                    } else {
//                        rateTotal += rateG;
//                    }
                }
                if(1 - rateTotal > 0) {
                    for (Map<String, Object> map : resultList) {
                        if ((long) map.get("amount") > 0) {
                            map.put("rate",df.format(Double.valueOf((String)map.get("rate")) +(1 - rateTotal)));
                            break;
                        }
                    }
                }
//                if (1 - rateTotal > 0) {
//                    for (Map<String, Object> map : resultList) {
//                        if (index.equals(GROUP)) {
//                            String code = String.valueOf(map.get("code"));
//                            if ((long) map.get("amount") > 0 && (code.equals("1") || code.equals("2") || code.equals("3"))) {
//                                map.put("rate", df.format(Double.valueOf((String) map.get("rate")) + (1 - rateTotal)));
//                                break;
//                            }
//                        }else{
//                            if ((long) map.get("amount") > 0) {
//                                map.put("rate", df.format(Double.valueOf((String) map.get("rate")) + (1 - rateTotal)));
//                                break;
//                            }
//                        }
//                    }
//                }
            }
            if(index.equals(SEX) && resultList.size() > 0){
            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")
                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){
                if (flag) {
                    resultList.remove(i);
                }
            }
            return new JSONArray(resultList);
        }else {
        } else {
            return new JSONArray();
        }
    }
    /**
     * 统计65以上人群数据
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @return
     */
    public JSONArray getSixFiveStatistics(String startDate, String endDate, String area, int level) {
        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(level3_type,'') code " +
                "     ,ifnull(level3_type_name,'') 'name' " +
                "     ,ifnull(sum(result),0) amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '12' " +
                "   and level2_type = '6' " +
                "   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 level3_type,level3_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<>();
            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) map.get("amount")));
                // 当天数据统计
                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") && valJson.getInt("num") > 0) {
                            map.put("amount", (long) map.get("amount") + valJson.getInt("num"));
                        }
                    }
                }
            }
            // 65岁以上人群总数统计
            long sixFiveTotal = getSixFiveTotal(startDate, 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 startDate
     * @param endDate
     * @param area
     * @param level
     * @return
     */
    public long getSixFiveTotal(String startDate, String endDate, String area, int level) {
        String areaField = "";
        long total = 0;
        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(sum(result),0) amount" +
                " from  " +
                "     wlyy_quota_result " +
                " where " +
                "     quato_code = '8' " +
                "   and level2_type = '6' " +
                "   and level1_type = ? and del = '1'";
        if (!org.apache.commons.lang3.StringUtils.isEmpty(startDate)) {
            sql += "   and quota_date >= ? ";
        }
        sql += "   and quota_date <= ? " +
                "   and " + areaField + " = ? ";
        Map<String, Object> result = null;
        if (org.apache.commons.lang3.StringUtils.isEmpty(startDate)) {
            result = jdbcTemplate.queryForMap(sql, new Object[]{level, endDate, area});
        } else {
            result = jdbcTemplate.queryForMap(sql, new Object[]{level, startDate, endDate, area});
        }
        if (result != null && result.containsKey("amount")) {
            total += (result.get("amount") == null ? 0 : Math.round((double) result.get("amount")));
        }
        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") && valJson.getInt("num") > 0) {
                total += valJson.getInt("num");
            }
        }
        return total;
    }
}

+ 96 - 46
src/main/java/com/yihu/wlyy/web/statistic/StatisticsController.java

@ -23,31 +23,31 @@ public class StatisticsController extends BaseController {
     * 指标按间隔统计
     *
     * @param startDate 起始日期
     * @param endDate 结束时间
     * @param interval 时间间隔
     * @param area 区域或机构
     * @param level 级别
     * @param index 指标
     * @param endDate   结束时间
     * @param interval  时间间隔
     * @param area      区域或机构
     * @param level     级别
     * @param index     指标
     * @return
     */
    @RequestMapping(value = "/interval")
    @ResponseBody
    public String indexInterval(String startDate,String endDate,int interval,String area,int level,String index){
    public String indexInterval(String startDate, String endDate, int interval, String area, int level, String index) {
        String tag = "";
        try{
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();
            if(index != null){
                for(String idx : indexes) {
                    result.put("index_" + idx,statisticsService.getDateIncrementDetail(startDate,endDate,interval,area,level,idx));
            if (index != null) {
                for (String idx : indexes) {
                    result.put("index_" + idx, statisticsService.getDateIncrementDetail(startDate, endDate, interval, area, level, idx));
                }
            }
            return write(200,"查询成功!","data",result);
        }catch (Exception e){
            return write(200, "查询成功!", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1,tag +"查询失败!");
            return error(-1, tag + "查询失败!");
        }
    }
@ -63,19 +63,19 @@ public class StatisticsController extends BaseController {
     */
    @RequestMapping("/increment")
    @ResponseBody
    public String getIndexIncrement(String startDate,String endDate,String area,int level,String index){
        try{
    public String getIndexIncrement(String startDate, String endDate, String area, int level, String index) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();
            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getIntervalIncrement(startDate,endDate,area,level,idx));
            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getIntervalIncrement(startDate, endDate, area, level, idx));
            }
            return write(200,"查询成功","data",result);
        }catch (Exception e){
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1,"查询失败");
            return error(-1, "查询失败");
        }
    }
@ -90,19 +90,19 @@ public class StatisticsController extends BaseController {
     */
    @RequestMapping("/total")
    @ResponseBody
    public String getIndexTotal(String endDate,String area,int level,String index){
        try{
    public String getIndexTotal(String endDate, String area, int level, String index) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();
            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getTotalAmount(endDate,area,level,idx));
            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getTotalAmount(endDate, area, level, idx));
            }
            return write(200,"查询成功","data",result);
        }catch (Exception e){
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1,"查询失败");
            return error(-1, "查询失败");
        }
    }
@ -118,19 +118,19 @@ public class StatisticsController extends BaseController {
     */
    @RequestMapping("/lowlevel_increment")
    @ResponseBody
    public String getIndexLowLevelIncrement(String startDate,String endDate,String area,int level,String index,int sort){
        try{
    public String getIndexLowLevelIncrement(String startDate, String endDate, String area, int level, String index, int sort,String lowLevel) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();
            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getLowLevelIncrementDetail(startDate,endDate,area,level,idx,sort));
            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getLowLevelIncrementDetail(startDate, endDate, area, level, idx, sort,lowLevel));
            }
            return write(200,"查询成功","data",result);
        }catch (Exception e){
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1,"查询失败");
            return error(-1, "查询失败");
        }
    }
@ -145,19 +145,19 @@ public class StatisticsController extends BaseController {
     */
    @RequestMapping("/lowlevel_total")
    @ResponseBody
    public String getIndexLowLevelTotal(String endDate,String area,int level,String index,int sort){
        try{
    public String getIndexLowLevelTotal(String endDate, String area, int level, String index, int sort, String lowLevel) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();
            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getLowLevelTotalDetail(endDate,area,level,idx,sort));
            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getLowLevelTotalDetail(endDate, area, level, idx, sort, lowLevel));
            }
            return write(200,"查询成功","data",result);
        }catch (Exception e){
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1,"查询失败");
            return error(-1, "查询失败");
        }
    }
@ -173,20 +173,70 @@ public class StatisticsController extends BaseController {
     */
    @RequestMapping("/leveltwo_increment")
    @ResponseBody
    public String getIndexLevelTwoIncrement(String startDate,String endDate,String area,int level,String index){
        try{
    public String getIndexLevelTwoIncrement(String startDate, String endDate, String area, int level, String index) {
        try {
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();
            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getLevelTwoIndexIncrement(startDate,endDate,area,level,idx));
            for (String idx : indexes) {
                result.put("index_" + idx, statisticsService.getLevelTwoIndexIncrement(startDate, endDate, area, level, idx));
            }
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }
    /**
     * 获取签约率、签约完成率
     *
     * @param endDate 截止日期
     * @param area    区域
     * @param level   区域级别 4:城市 3:区 2:社区 1:团队
     * @param type    1:签约率  2:签约完成率
     * @return
     */
    @RequestMapping(value = "/rate")
    @ResponseBody
    public String getAreaRate(String endDate, String area, int level, String type) {
        try {
            String result = "0.00";
            if (type.equals("1")) {
                result = statisticsService.getSignRate(endDate,area,level);
            } else {
                result = statisticsService.getSignTaskRate(endDate,area,level);
            }
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }
    /**
     * 获取三级指标增量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @return
     */
    @RequestMapping(value = "/sixfive_statistics")
    @ResponseBody
    public String getSixFiveStatistics(String startDate, String endDate, String area, int level){
        try{
            JSONArray result = statisticsService.getSixFiveStatistics(startDate,endDate,area,level);
            return write(200,"查询成功","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }
}