Browse Source

Merge branch 'dev' of http://192.168.1.220:10080/jiwei/wlyy2.0 into dev

Conflicts:
	svr/svr-base/src/main/java/com/yihu/jw/base/endpoint/servicePackage/ServicePackageEndpoint.java
yeshijie 6 years ago
parent
commit
d56547c24d
89 changed files with 9952 additions and 0 deletions
  1. 32 0
      common/common-entity/src/main/java/com/yihu/jw/entity/base/statistics/JobConfigDO.java
  2. 90 0
      common/common-entity/src/main/java/com/yihu/jw/entity/base/statistics/JobLogDO.java
  3. 170 0
      svr/svr-statistics/doc/es/QuartzJobConfig.java
  4. 127 0
      svr/svr-statistics/doc/es/QuartzJobConfig1.java
  5. 108 0
      svr/svr-statistics/doc/es/统计新增索引.txt
  6. 192 0
      svr/svr-statistics/doc/sql/统计相关表数据备份20171121.txt
  7. BIN
      svr/svr-statistics/doc/业务文档/统计指标流程.docx
  8. BIN
      svr/svr-statistics/doc/部署文档/正式库/后台/tomcat部署.docx
  9. 455 0
      svr/svr-statistics/pom.xml
  10. 120 0
      svr/svr-statistics/readme.MD
  11. 56 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/Application.java
  12. 50 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/AsyncConfig.java
  13. 240 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/DataSourceConfig.java
  14. 30 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/MVCConfig.java
  15. 64 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/SwaggerConfig.java
  16. 54 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/WebSecurityConfig.java
  17. 27 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/jpa/HibernateProperties.java
  18. 60 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/jpa/WlyyJpa.java
  19. 25 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/quartz/JobFactory.java
  20. 57 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/quartz/SchedulerConfig.java
  21. 15 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/war/ServletInitializer.java
  22. 291 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/controller/JobController.java
  23. 278 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/controller/QueryController.java
  24. 35 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/dao/QuartzJobConfigDao.java
  25. 11 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/dao/QuartzJobLogDao.java
  26. 11 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/dao/QuartzQuotaDao.java
  27. 16 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/dao/WlyyDimensionQuotaDao.java
  28. 28 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/cache/Cache.java
  29. 237 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/compute/ComputeHelper.java
  30. 23 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/Convert.java
  31. 51 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/ConvertHelper.java
  32. 59 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/AgeConvert.java
  33. 70 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/ConsultTimeConvert.java
  34. 58 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/DeviceTypeConvert.java
  35. 96 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/DiseaseAndAgeConvert.java
  36. 75 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/DiseaseLableConvert.java
  37. 32 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/DispensaryTypeConvert.java
  38. 30 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/HealthGuidanceConvert.java
  39. 59 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/HealthLableConvert.java
  40. 66 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/PrescriptionDiseaseConvert.java
  41. 69 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/PrescriptionPatientDiseaseConvert.java
  42. 52 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/PrescriptionStatusConvert.java
  43. 55 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/ServerTypeConvert.java
  44. 36 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/SexConvert.java
  45. 38 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/SimpleConvert.java
  46. 66 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/UseDeviceTypeConvert.java
  47. 33 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/model/HealthLable.java
  48. 26 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/model/PrescriptionDisease.java
  49. 25 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/model/ServerType.java
  50. 90 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/ExtractHelper.java
  51. 66 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/db/DBExtract.java
  52. 156 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/db/DBPageExtract.java
  53. 162 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/db/Data2Save.java
  54. 69 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/db/ESExtract.java
  55. 47 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/filter/FilterHelper.java
  56. 34 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/filter/filter/NotNullFilter.java
  57. 29 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/save/SaveHelper.java
  58. 102 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/save/es/ElasticFactory.java
  59. 112 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/save/es/ElastricSearchSave.java
  60. 29 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/CacheCleanJob.java
  61. 434 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/CurrentMysqlToEsQuotaJob.java
  62. 305 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/EsToEsQuotaJob.java
  63. 343 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/MysqlToEsQuotaJob.java
  64. 43 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/service/ElasticsearchService.java
  65. 595 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/service/JobService.java
  66. 177 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/Contant.java
  67. 818 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/DateUtil.java
  68. 578 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/ElasticsearchUtil.java
  69. 128 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/HttpClientUtil.java
  70. 162 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/IdCardUtil.java
  71. 45 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/JsonDateValueProcessor.java
  72. 64 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/MD5.java
  73. 118 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/QuartzHelper.java
  74. 63 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/SpringUtil.java
  75. 206 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/DataModel.java
  76. 25 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/DictModel.java
  77. 30 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/ErrModel.java
  78. 34 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/FilterModel.java
  79. 19 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/JobLogModel.java
  80. 318 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/SaveModel.java
  81. 174 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/WlyyJobConfigVO.java
  82. 145 0
      svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/WlyyQuotaVO.java
  83. 55 0
      svr/svr-statistics/src/main/resources/application-dev.yml
  84. 60 0
      svr/svr-statistics/src/main/resources/application-devtest.yml
  85. 42 0
      svr/svr-statistics/src/main/resources/application-prod.yml
  86. 60 0
      svr/svr-statistics/src/main/resources/application-test.yml
  87. 72 0
      svr/svr-statistics/src/main/resources/application.yml
  88. 4 0
      svr/svr-statistics/src/main/resources/logback.xml
  89. 41 0
      svr/svr-statistics/src/main/resources/quartz.properties

+ 32 - 0
common/common-entity/src/main/java/com/yihu/jw/entity/base/statistics/JobConfigDO.java

@ -5,6 +5,7 @@ import com.yihu.jw.entity.UuidIdentityEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
/**
@ -16,6 +17,7 @@ import java.io.Serializable;
public class JobConfigDO extends UuidIdentityEntity implements Serializable {
    private String saasId;
    private String quotaId;//指标id
    private String jobName;//任务名称
    private String jobInfo;//任务描述
    private String jobType;//任务类型(0--单次执行  1--周期执行 2--监听任务)
@ -33,6 +35,9 @@ public class JobConfigDO extends UuidIdentityEntity implements Serializable {
    private String timeLevel;//1增量 2到达量 3生成到达量也生成增量
    private Integer incrementInterval;//增量时间间隔1天,2周,3月
    private String startTime;
    private String endTime;
    @Column(name = "saas_id")
    public String getSaasId() {
        return saasId;
@ -42,6 +47,15 @@ public class JobConfigDO extends UuidIdentityEntity implements Serializable {
        this.saasId = saasId;
    }
    @Column(name = "quota_id")
    public String getQuotaId() {
        return quotaId;
    }
    public void setQuotaId(String quotaId) {
        this.quotaId = quotaId;
    }
    @Column(name = "job_name")
    public String getJobName() {
        return jobName;
@ -182,4 +196,22 @@ public class JobConfigDO extends UuidIdentityEntity implements Serializable {
    public void setIncrementInterval(Integer incrementInterval) {
        this.incrementInterval = incrementInterval;
    }
    @Transient
    public String getStartTime() {
        return startTime;
    }
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }
    @Transient
    public String getEndTime() {
        return endTime;
    }
    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
}

+ 90 - 0
common/common-entity/src/main/java/com/yihu/jw/entity/base/statistics/JobLogDO.java

@ -0,0 +1,90 @@
package com.yihu.jw.entity.base.statistics;
import com.yihu.jw.entity.UuidIdentityEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
/**
 * job执行日志表
 * @author yeshijie on 2018/9/3.
 */
@Entity
@Table(name = "base_quota")
public class JobLogDO extends UuidIdentityEntity implements Serializable {
    private String saasId;//
    private Date jobStartTime;//任务开始执行时间
    private Date jobEndTime;//任务结束时间
    private String jobId;//任务id
    private String jobContent;//任务执行情况
    private String jobType;// 1成功 0失败
    private String jobName;//任务名称
    @Column(name = "saas_id")
    public String getSaasId() {
        return saasId;
    }
    public void setSaasId(String saasId) {
        this.saasId = saasId;
    }
    @Column(name = "job_start_time")
    public Date getJobStartTime() {
        return jobStartTime;
    }
    public void setJobStartTime(Date jobStartTime) {
        this.jobStartTime = jobStartTime;
    }
    @Column(name = "job_end_time")
    public Date getJobEndTime() {
        return jobEndTime;
    }
    public void setJobEndTime(Date jobEndTime) {
        this.jobEndTime = jobEndTime;
    }
    @Column(name = "job_id")
    public String getJobId() {
        return jobId;
    }
    public void setJobId(String jobId) {
        this.jobId = jobId;
    }
    @Column(name = "job_content")
    public String getJobContent() {
        return jobContent;
    }
    public void setJobContent(String jobContent) {
        this.jobContent = jobContent;
    }
    @Column(name = "job_type")
    public String getJobType() {
        return jobType;
    }
    public void setJobType(String jobType) {
        this.jobType = jobType;
    }
    @Column(name = "job_name")
    public String getJobName() {
        return jobName;
    }
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }
}

+ 170 - 0
svr/svr-statistics/doc/es/QuartzJobConfig.java

@ -0,0 +1,170 @@
package com.yihu.wlyy.entity.job;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
/**
 * 计划任务
 */
@Entity
@Table(name = "wlyy_job_config")
public class QuartzJobConfig implements java.io.Serializable {
	private String id;
	private String quotaId;//指标id
	private String jobName;//任务名称
	private String jobInfo;//任务描述
	private String jobType;//任务类型(0--单次执行  1--周期执行 2--监听任务)
	private String jobClass;//任务执行的class
	private String quartzCron;//quartz表达式
	private String status;//1 启动 0停止
	private String del;//是否删除 1正常 0删除
	private String sql;
	private String sqlCount;
	private String sqlDay;
	private String sqlYear;
	private String cacheKey;//缓存的key
	/** minimal constructor */
	public QuartzJobConfig() {
	}
	/** full constructor */
	public QuartzJobConfig(String jobName, String jobInfo, String jobType,
                           String jobClass, String quartzCron, String status) {
		this.jobName = jobName;
		this.jobInfo = jobInfo;
		this.jobType = jobType;
		this.jobClass = jobClass;
		this.quartzCron = quartzCron;
		this.status = status;
	}
	// Property accessors
	@GenericGenerator(name = "generator", strategy = "uuid")
	@Id
	@GeneratedValue(generator = "generator")
	@Column(name = "id", unique = true, nullable = false, length = 50)
	public String getId() {
		return this.id;
	}
	public void setId(String id) {
		this.id = id;
	}
	@Column(name = "job_name", length = 50)
	public String getJobName() {
		return this.jobName;
	}
	public void setJobName(String jobName) {
		this.jobName = jobName;
	}
	@Column(name = "job_info", length = 200)
	public String getJobInfo() {
		return this.jobInfo;
	}
	public void setJobInfo(String jobInfo) {
		this.jobInfo = jobInfo;
	}
	@Column(name = "job_type", length = 10)
	public String getJobType() {
		return this.jobType;
	}
	public void setJobType(String jobType) {
		this.jobType = jobType;
	}
	@Column(name = "job_class", length = 200)
	public String getJobClass() {
		return jobClass;
	}
	public void setJobClass(String jobClass) {
		this.jobClass = jobClass;
	}
	@Column(name = "quartz_cron", length = 200)
	public String getQuartzCron() {
		return this.quartzCron;
	}
	public void setQuartzCron(String quartzCron) {
		this.quartzCron = quartzCron;
	}
	@Column(name = "status", length = 1)
	public String getStatus() {
		return this.status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	@Column(name = "quota_id", length = 50)
	public String getQuotaId() {
		return quotaId;
	}
	public void setQuotaId(String quotaId) {
		this.quotaId = quotaId;
	}
	@Column(name = "del", length = 1)
	public String getDel() {
		return del;
	}
	public void setDel(String del) {
		this.del = del;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public String getSqlCount() {
		return sqlCount;
	}
	public void setSqlCount(String sqlCount) {
		this.sqlCount = sqlCount;
	}
	public String getSqlDay() {
		return sqlDay;
	}
	public void setSqlDay(String sqlDay) {
		this.sqlDay = sqlDay;
	}
	public String getSqlYear() {
		return sqlYear;
	}
	public void setSqlYear(String sqlYear) {
		this.sqlYear = sqlYear;
	}
	public String getCacheKey() {
		return cacheKey;
	}
	public void setCacheKey(String cacheKey) {
		this.cacheKey = cacheKey;
	}
}

+ 127 - 0
svr/svr-statistics/doc/es/QuartzJobConfig1.java

@ -0,0 +1,127 @@
//package com.yihu.wlyy.entity.job;
//
//import javax.persistence.Column;
//import javax.persistence.Entity;
//import javax.persistence.GeneratedValue;
//import javax.persistence.Id;
//import javax.persistence.Table;
//import org.hibernate.annotations.GenericGenerator;
//
///**
// * 计划任务
// */
//@Entity
//@Table(name = "wlyy_job_config")
//public class QuartzJobConfig implements java.io.Serializable {
//
//	private String id;
//	private String quotaId;//指标id
//	private String jobName;//任务名称
//	private String jobInfo;//任务描述
//	private String jobType;//任务类型(0--单次执行  1--周期执行 2--监听任务)
//	private String jobClass;//任务执行的class
//	private String quartzCron;//quartz表达式
//	private String status;//1 启动 0停止
//	private String del;//是否删除 1正常 0删除
//
//	/** minimal constructor */
//	public QuartzJobConfig() {
//
//	}
//
//	/** full constructor */
//	public QuartzJobConfig(String jobName, String jobInfo, String jobType,
//                           String jobClass, String quartzCron, String status) {
//		this.jobName = jobName;
//		this.jobInfo = jobInfo;
//		this.jobType = jobType;
//		this.jobClass = jobClass;
//		this.quartzCron = quartzCron;
//		this.status = status;
//	}
//
//	// Property accessors
//	@GenericGenerator(name = "generator", strategy = "uuid")
//	@Id
//	@GeneratedValue(generator = "generator")
//	@Column(name = "id", unique = true, nullable = false, length = 50)
//	public String getId() {
//		return this.id;
//	}
//
//	public void setId(String id) {
//		this.id = id;
//	}
//
//	@Column(name = "job_name", length = 50)
//	public String getJobName() {
//		return this.jobName;
//	}
//
//	public void setJobName(String jobName) {
//		this.jobName = jobName;
//	}
//
//	@Column(name = "job_info", length = 200)
//	public String getJobInfo() {
//		return this.jobInfo;
//	}
//
//	public void setJobInfo(String jobInfo) {
//		this.jobInfo = jobInfo;
//	}
//
//	@Column(name = "job_type", length = 10)
//	public String getJobType() {
//		return this.jobType;
//	}
//
//	public void setJobType(String jobType) {
//		this.jobType = jobType;
//	}
//
//	@Column(name = "job_class", length = 200)
//	public String getJobClass() {
//		return jobClass;
//	}
//
//	public void setJobClass(String jobClass) {
//		this.jobClass = jobClass;
//	}
//
//	@Column(name = "quartz_cron", length = 200)
//	public String getQuartzCron() {
//		return this.quartzCron;
//	}
//
//	public void setQuartzCron(String quartzCron) {
//		this.quartzCron = quartzCron;
//	}
//
//	@Column(name = "status", length = 1)
//	public String getStatus() {
//		return this.status;
//	}
//
//	public void setStatus(String status) {
//		this.status = status;
//	}
//
//	@Column(name = "quota_id", length = 50)
//	public String getQuotaId() {
//		return quotaId;
//	}
//
//	public void setQuotaId(String quotaId) {
//		this.quotaId = quotaId;
//	}
//
//	@Column(name = "del", length = 1)
//	public String getDel() {
//		return del;
//	}
//
//	public void setDel(String del) {
//		this.del = del;
//	}
//}

+ 108 - 0
svr/svr-statistics/doc/es/统计新增索引.txt

@ -0,0 +1,108 @@
创建索引
POST  http://172.19.103.68:9200/wlyy_quota_test
查询索引
GET http://172.19.103.68:9200/wlyy_quota_test/
给索引加mapping
POST http://172.19.103.68:9200/wlyy_quota_test/wlyy_quota_test/_mapping
{
    "wlyy_quota_test": {
            "properties": {
                "quotaCode": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "city": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "cityName": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "town": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "townName": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "hospital": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "hospitalName": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "team": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "teamName": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "slaveKey1": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "slaveKey1Name": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "slaveKey2": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "slaveKey2Name": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "slaveKey3": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "slaveKey3Name": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "slaveKey4": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "slaveKey4Name": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "areaLevel": {
                    "type": "string",
                    "index": "not_analyzed"
                }, 
               "timeLevel": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "result1": {
                    "type": "double"
                },
                "result2": {
                    "type": "double"
                },
                "quotaDate": {
                    "type": "date"
                },
                "createTime": {
                    "type": "date"
                }
            }
        }
  }
增加索引搜索大小
PUT  http://172.19.103.68:9200/wlyy_quota_test/_settings
{
  "index": {
    "max_result_window": 500000
  }
}

File diff suppressed because it is too large
+ 192 - 0
svr/svr-statistics/doc/sql/统计相关表数据备份20171121.txt


BIN
svr/svr-statistics/doc/业务文档/统计指标流程.docx


BIN
svr/svr-statistics/doc/部署文档/正式库/后台/tomcat部署.docx


+ 455 - 0
svr/svr-statistics/pom.xml

@ -0,0 +1,455 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.yihu.jw</groupId>
        <artifactId>wlyy-parent-pom</artifactId>
        <version>2.0.0</version>
        <relativePath>../../wlyy-parent-pom/pom.xml</relativePath>
    </parent>
    <groupId>com.yihu.jw</groupId>
    <artifactId>svr-statistics</artifactId>
    <packaging>war</packaging>
    <version>${parent.version}</version>
    <modelVersion>4.0.0</modelVersion>
    <!--<packaging>jar</packaging>-->
    <name>svr-statistics</name>
    <properties>
        <version.commons-bean-utils>1.9.2</version.commons-bean-utils>
        <version.commons-codec>1.9</version.commons-codec>
        <version.commons-collections>3.2.1</version.commons-collections>
        <version.commons-compress>1.9</version.commons-compress>
        <version.commons-dbcp2>2.1.1</version.commons-dbcp2>
        <version.commons-dbutils>1.6</version.commons-dbutils>
        <version.commons-io>2.4</version.commons-io>
        <version.commons-lang3>3.2.1</version.commons-lang3>
        <version.commons-pool2>2.4.2</version.commons-pool2>
        <version.hibernate>4.3.11.Final</version.hibernate>
        <version.hibernate-annotations>4.0.5.Final</version.hibernate-annotations>
        <version.hibernate-jpa-api>1.0.0.Final</version.hibernate-jpa-api>
        <version.http-client>4.5.1</version.http-client>
        <version.http-core>4.4.3</version.http-core>
        <version.java>1.8</version.java>
        <version.jackson>2.6.6</version.jackson>
        <version.jedis>2.9.0</version.jedis>
        <version.jcl-over-slf4j>1.7.19</version.jcl-over-slf4j>
        <version.jul-over-slf4j>1.7.21</version.jul-over-slf4j>
        <version.joda-time>2.8.2</version.joda-time>
        <version.junit>4.12</version.junit>
        <version.log4j2>2.4.1</version.log4j2>
        <version.logback>1.1.7</version.logback>
        <version.mysql>5.1.38</version.mysql>
        <version.quartz>2.2.3</version.quartz>
        <version.servlet-api>3.1.0</version.servlet-api>
        <version.slf4j>1.7.21</version.slf4j>
        <version.swagger>2.4.0</version.swagger>
        <version.swagger-ui>2.4.0</version.swagger-ui>
        <version.tomcat-embed>8.0.28</version.tomcat-embed>
        <version.elasticsearch>2.4.4</version.elasticsearch>
        <version.jest>2.4.0</version.jest>
        <version.httpmime>4.4.1</version.httpmime>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.yihu.jw</groupId>
            <artifactId>common-entity</artifactId>
        </dependency>
        <dependency>
            <groupId>com.yihu.jw</groupId>
            <artifactId>common-rest-model</artifactId>
        </dependency>
        <!--Jackson library-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${version.jackson}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${version.jackson}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${version.jackson}</version>
        </dependency>
        <!--elasticsearch start-->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${version.elasticsearch}</version>
        </dependency>
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>${version.jest}</version>
        </dependency>
        <!--elasticsearch end-->
        <!--Apache commons library-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>${version.httpmime}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${version.commons-lang3}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>${version.http-core}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${version.http-client}</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>${version.commons-bean-utils}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>${version.commons-pool2}</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>${version.commons-dbutils}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>${version.commons-dbcp2}</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${version.commons-io}</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>${version.commons-collections}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>${version.commons-compress}</version>
        </dependency>
        <!--Hibernate framework-->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>${version.hibernate-jpa-api}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${version.hibernate}</version>
        </dependency>
        <!--Miscellaneous-->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${version.quartz}</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>${version.quartz}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${version.servlet-api}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${version.slf4j}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${version.junit}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${version.swagger}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${version.swagger-ui}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${version.mysql}</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${version.joda-time}</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>${version.commons-codec}</version>
        </dependency>
        <!--Log framework-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${version.log4j2}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${version.log4j2}</version>
        </dependency>
        <!--Log framework to SLF4J bridge: x-over-slf4j-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${version.jcl-over-slf4j}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-jdk14</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
            <version>${version.jul-over-slf4j}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--SLF4J api-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${version.slf4j}</version>
        </dependency>
        <!--The single log BACKEND-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${version.logback}</version>
        </dependency>
        <!--Spring framework family-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <!--Spring integration family-->
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-jmx</artifactId>
        </dependency>
        <!--Spring batch family-->
        <!--Spring boot family-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator-docs</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20151123</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.nlpcn</groupId>
            <artifactId>elasticsearch-sql</artifactId>
            <version>2.4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>delete-by-query</artifactId>
            <version>2.4.1</version>
        </dependency>
        <!-- Alibaba -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${verion.fastjson}</version>
        </dependency>
        <!-- Jzkl Starter -->
        <dependency>
            <groupId>com.yihu</groupId>
            <artifactId>swagger-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.yihu</groupId>
            <artifactId>mysql-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.yihu</groupId>
            <artifactId>elasticsearch-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>patient-co-statistics-es</finalName>
        <plugins>
            <!--打成war包需要的配置-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <version>3.1</version>
            </plugin>
        </plugins>
    </build>
</project>

+ 120 - 0
svr/svr-statistics/readme.MD

@ -0,0 +1,120 @@
[es-sql](http://172.19.103.68:9200/_plugin/sql/) 例子
[elasticsearch-sql文档](https://github.com/NLPchina/elasticsearch-sql/wiki/Functions-support)
**重命名配置支持**
{elasticsearch_home}/config/elasticsearch.yml
```
script.inline: on
script.indexed: on
script.engine.groovy.inline.aggs: on
```
**目前2.4.2 es-sql不支持update delete**
6.x 系列的es支持delete
**查询表**
```SELECT * FROM wlyy_quota_test```
**条件查询表**
```SELECT * FROM wlyy_quota_test where townName='湖里区'```
**模糊查询**
SELECT * FROM wlyy_quota_test where townName like '%湖里%'
**查询返回单个字段**
```SELECT city FROM wlyy_quota_test```
**查询返回单个字段并且重命名**
```SELECT city as slavename FROM wlyy_quota_test```
**查询根据某个字段聚合**
```SELECT city  FROM wlyy_quota_test group by city```
**查询根据某个字段聚合并且重命名**
```SELECT city as slavename  FROM wlyy_quota_test group by slavename```
**条件查询并且排序表**
```SELECT * FROM wlyy_quota_test where townName='湖里区' order by createTime desc ```
**分页查询(分页查询不能与group by 同时使用)**
```SELECT city as slavename  FROM wlyy_quota_test limit 0,10```
**分页查询带条件(分页查询不能与group by 同时使用)**
```SELECT townName as slavename  FROM wlyy_quota_test where townName='湖里区' limit 0,10```
**count使用**
```SELECT count(*) num FROM wlyy_quota_test where townName='湖里区'```
**count distinct使用**
```select count( distinct town) from wlyy_quota_test```
**max使用**
```SELECT max(result1) FROM wlyy_quota_test where townName='湖里区'```
**min使用**
```SELECT min(result1) FROM wlyy_quota_test where townName='湖里区'```
**sum使用**
```select sum(result1) from wlyy_quota_test```
**按照年龄分组使用**
```SELECT count(age) FROM account GROUP BY range(age, 20,25,30,35,40)```
**时间聚合使用**
```select town,townName,sum(result1) result1 from wlyy_quota_test group by town,townName,date_histogram(field='quotaDate','interval'='month')```
interval例子
```
1天    date_histogram(field='quotaDate','interval'='1d')
每周   date_histogram(field='quotaDate','interval'='week')
每月   date_histogram(field='quotaDate','interval'='month')
每季度 date_histogram(field='quotaDate','interval'='quarter')
每年   date_histogram(field='quotaDate','interval'='year')
```
**子字段(nested)查询**
```SELECT townName.name FROM wlyy_quota_test where townName.name like '%湖里%'```
**join(只支持2个表,简单的查询 不支持分组)**
```select  table1.town town1 ,table2.townName townName2 from wlyy_quota_test table1 join wlyy_quota_test table2 on table1.town=table2.town```

+ 56 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/Application.java

@ -0,0 +1,56 @@
package com.yihu.wlyy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.support.ErrorPageFilter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
/**
 * Created by Administrator on 2016.10.12.
 */
@SpringBootApplication
@EnableJpaRepositories(
        entityManagerFactoryRef = "wlyyEntityManagerFactory",
        transactionManagerRef = "wlyyTransactionManager")
@EnableAutoConfiguration(exclude = {
        ElasticsearchAutoConfiguration.class,
        ElasticsearchRepositoriesAutoConfiguration.class,
        ElasticsearchDataAutoConfiguration.class,
        RedisRepositoriesAutoConfiguration.class
})
public class Application {
    public static ApplicationContext ctx = null;
    public static void main(String[] args) {
        ctx = SpringApplication.run(Application.class, args);
    }
    /**
     * start 解决如下问题
     * Cannot forward to error page for
     * request [/strategy/list/] as the response has already been committed. As a
     * result, the response may have the wrong status code. If your application is
     * running on WebSphere Application Server you may be able to resolve this
     * problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false
     */
    @Bean
    public ErrorPageFilter errorPageFilter() {
        return new ErrorPageFilter();
    }
    @Bean
    public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }
    //end
}

+ 50 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/AsyncConfig.java

@ -0,0 +1,50 @@
package com.yihu.wlyy.statistics.config;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
 * Created by Administrator on 2016.10.18.
 * 启用多綫程
 */
@Configuration
@EnableAsync
public class AsyncConfig  {
    /** 如果池中的实际线程数小于corePoolSize,无论是否其中有空闲的线程,都会给新的任务产生新的线程 */
    private int corePoolSize = 5;
    /** 如果池中的线程数=maximumPoolSize,则有空闲线程使用空闲线程,否则新任务放入queueCapacity. */
    private int maxPoolSize = 20;
    /** 缓冲队列. */
    private int queueCapacity = 10;
    @Bean
    public Executor dbExtractExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
    @Bean
    public Executor dbStorageExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}

+ 240 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/DataSourceConfig.java

@ -0,0 +1,240 @@
package com.yihu.wlyy.statistics.config;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
 * Created by Administrator on 2016.10.20.
 * 多数据源配置
 */
@Configuration
public class DataSourceConfig {
    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;
    @Value("${spring.datasource.initialSize}")
    private Integer initialSize;
    @Value("${spring.datasource.minIdle}")
    private Integer minIdle;
    @Value("${spring.datasource.maxActive}")
    private Integer maxActive;
    @Value("${spring.datasource.maxWait}")
    private Integer maxWait;
    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private Integer timeBetweenEvictionRunsMillis;
    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private Integer minEvictableIdleTimeMillis;
    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;
    @Value("${spring.datasource.testWhileIdle}")
    private Boolean testWhileIdle;
    @Value("${spring.datasource.testOnBorrow}")
    private Boolean testOnBorrow;
    @Value("${spring.datasource.testOnReturn}")
    private Boolean testOnReturn;
    @Value("${spring.datasource.poolPreparedStatements}")
    private Boolean poolPreparedStatements;
    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private Integer maxPoolPreparedStatementPerConnectionSize;
    @Value("${spring.datasource.removeAbandoned}")
    private Boolean removeAbandoned;
    @Value("${spring.datasource.removeAbandonedTimeout}")
    private Integer removeAbandonedTimeout;
    @Value("${spring.datasource.logAbandoned}")
    private Boolean logAbandoned;
    @Value("${spring.datasource.filters")
    private String filters;
    @Value("${spring.datasource.primaryReadWrite.url}")
    private String primaryReadWriteUrl;
    @Value("${spring.datasource.primaryReadWrite.username}")
    private String primaryReadWriteUsername;
    @Value("${spring.datasource.primaryReadWrite.password}")
    private String primaryReadWritePassword;
    /**
     * 主数据源
     *
     * @return
     */
    @Bean(name = "primaryReadWrite")
    @Primary//主库 默认不写名字用这个
    public DataSource primaryReadWriteDataSource(StatFilter statFilter) throws SQLException {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(primaryReadWriteUrl);
        datasource.setUsername(primaryReadWriteUsername);
        datasource.setPassword(primaryReadWritePassword);
        datasource.setDriverClassName(driverClassName);
        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        datasource.setRemoveAbandoned(removeAbandoned);
        datasource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        datasource.setLogAbandoned(logAbandoned);
        datasource.setFilters(filters);
        datasource.setConnectProperties(properties());//;# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        datasource.setUseGlobalDataSourceStat(true);// 合并多个DruidDataSource的监控数据
        List proxyFilters = new ArrayList<>();
        proxyFilters.add(statFilter);
        datasource.setProxyFilters(proxyFilters);
        return datasource;
    }
    @Value("${spring.datasource.primaryRead.url}")
    private String primaryReadUrl;
    @Value("${spring.datasource.primaryRead.username}")
    private String pprimaryReadUsername;
    @Value("${spring.datasource.primaryRead.password}")
    private String primaryReadPassword;
    @Bean(name = "primaryRead")
    public DataSource primaryReadDataSource(StatFilter statFilter) throws SQLException {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(primaryReadUrl);
        datasource.setUsername(pprimaryReadUsername);
        datasource.setPassword(primaryReadPassword);
        datasource.setDriverClassName(driverClassName);
        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        datasource.setRemoveAbandoned(removeAbandoned);
        datasource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        datasource.setLogAbandoned(logAbandoned);
        datasource.setFilters(filters);
        datasource.setConnectProperties(properties());//;# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        datasource.setUseGlobalDataSourceStat(true);// 合并多个DruidDataSource的监控数据
        List proxyFilters = new ArrayList<>();
        proxyFilters.add(statFilter);
        datasource.setProxyFilters(proxyFilters);
        return datasource;
    }
    @Value("${spring.datasource.im.url}")
    private String imUrl;
    @Value("${spring.datasource.im.username}")
    private String imUsername;
    @Value("${spring.datasource.im.password}")
    private String imPassword;
    @Bean(name = "imData")
    public DataSource imDataSource(StatFilter statFilter) throws SQLException {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(imUrl);
        datasource.setUsername(imUsername);
        datasource.setPassword(imPassword);
        datasource.setDriverClassName(driverClassName);
        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        datasource.setRemoveAbandoned(removeAbandoned);
        datasource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        datasource.setLogAbandoned(logAbandoned);
        datasource.setFilters(filters);
        datasource.setConnectProperties(properties());//;# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        datasource.setUseGlobalDataSourceStat(true);// 合并多个DruidDataSource的监控数据
        List proxyFilters = new ArrayList<>();
        proxyFilters.add(statFilter);
        datasource.setProxyFilters(proxyFilters);
        return datasource;
    }
    private Properties properties() {
        Properties properties = new Properties();
        properties.put("druid.stat.mergeSql", "true");
        properties.put("slowSqlMillis", "1000");
        return properties;
    }
    //------------------------------------druid 监控----------------------------------------------
    @Bean
    public ServletRegistrationBean statViewServlet() {
        //创建servlet注册实体
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //设置ip白名单
        //servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow
        // servletRegistrationBean.addInitParameter("deny","192.168.0.19");
        //设置控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername", "jkzl");
        servletRegistrationBean.addInitParameter("loginPassword", "jkzlehr");
        //是否可以重置数据
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean(WebStatFilter webStatFilter) {
        //创建过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(webStatFilter);
        //设置过滤器过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的形式
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
    @Bean
    public StatFilter statFilter() {
        return new StatFilter();
    }
    @Bean
    public WebStatFilter webStatFilter() {
        return new WebStatFilter();
    }
    //------------------------------------druid 监控----------------------------------------------
}

+ 30 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/MVCConfig.java

@ -0,0 +1,30 @@
package com.yihu.wlyy.statistics.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.List;
/**
 * Created by chenweida on 2017/7/17.
 */
@Configuration
public class MVCConfig extends WebMvcConfigurerAdapter {
    /**
     * 为null的数据不返回
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(mappingJackson2HttpMessageConverter);
    }
}

+ 64 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/SwaggerConfig.java

@ -0,0 +1,64 @@
package com.yihu.wlyy.statistics.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
/**
 * Created by chenweida on 2016/2/3.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter {
    private static final String PUBLIC_API = "Default";
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    @Bean
    public Docket publicAPI() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(PUBLIC_API)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")
                .select()
                .paths(or(
                        regex("/job/.*"),
                        regex("/quotaQuery/.*"),
                        regex("/extractDataToEs/.*")
                ))
                .build()
                .apiInfo(publicApiInfo());
    }
    private ApiInfo publicApiInfo() {
        ApiInfo apiInfo = new ApiInfo("三师平台统计分析API",
                "统计分析接口。",
                "1.0",
                "No terms of service",
                "admin@jkzl.com",
                "The Apache License, Version 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0.html"
        );
        return apiInfo;
    }
}

+ 54 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/WebSecurityConfig.java

@ -0,0 +1,54 @@
package com.yihu.wlyy.statistics.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
 * Created by Administrator on 2016.10.17.
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Value("${security.basic.username}")
    String username;
    @Value("${security.basic.password}")
    String password;
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/metrics/**").permitAll()
                .antMatchers("/health/**").permitAll()
                .antMatchers("/env/**").permitAll()
                .antMatchers("/dump/**").permitAll()
                .antMatchers("/trace/**").permitAll()
                .antMatchers("/mappings/**").permitAll()
                .antMatchers("/actuator/**").permitAll()
                .antMatchers("/beans/**").permitAll()
                .antMatchers("/configprops/**").permitAll()
                .antMatchers("/docs/**").permitAll()
                .antMatchers("/info/**").permitAll()
                .antMatchers("/configuration/**").permitAll()
                .antMatchers("/jolokia/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf().disable()
                .formLogin().defaultSuccessUrl("/swagger-ui.html").failureUrl("/login") //登录成功之后的跳转
                .permitAll()
                .and()
                .logout().logoutSuccessUrl("/login")
                .permitAll();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                .withUser(username).password(password).roles("USER");
    }
}

+ 27 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/jpa/HibernateProperties.java

@ -0,0 +1,27 @@
package com.yihu.wlyy.statistics.config.jpa;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Properties;
/**
 * Created by chenweida on 2017/4/6.
 */
@Component
public class HibernateProperties {
    @Value("${hibernate.dialect}")
    private String dialect;
    @Value("${hibernate.show_sql}")
    private String show_sql;
    @Value("${hibernate.ejb.naming_strategy}")
    private String naming_strategy;
    public  Properties hibProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect",dialect);
        properties.put("hibernate.show_sql", show_sql);
        properties.put("hibernate.ejb.naming_strategy", naming_strategy);
        return properties;
    }
}

+ 60 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/jpa/WlyyJpa.java

@ -0,0 +1,60 @@
package com.yihu.wlyy.statistics.config.jpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
/**
 * Created by chenweida on 2017/4/6.
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "wlyyEntityManagerFactory",
        transactionManagerRef = "wlyyTransactionManager",
        basePackages = {"com.yihu.wlyy.statistics.dao"})   //设置Repository所在位置
public class WlyyJpa {
    @Autowired
    private HibernateProperties hibernateProperties;
    @Bean(name = "wlyyEntityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(
            DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
        emfb.setDataSource(dataSource);
        emfb.setPackagesToScan("com.yihu.wlyy");
        emfb.setPersistenceUnitName("wlyy");
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        emfb.setJpaVendorAdapter(vendorAdapter);
        emfb.setJpaProperties(hibernateProperties.hibProperties());
        return emfb;
    }
    @Bean(name = "wlyyTransactionManager")
    @Primary
    JpaTransactionManager transactionManagerSecondary(
            @Qualifier("wlyyEntityManagerFactory") EntityManagerFactory builder) {
        return new JpaTransactionManager(builder);
    }
}

+ 25 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/quartz/JobFactory.java

@ -0,0 +1,25 @@
package com.yihu.wlyy.statistics.config.quartz;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
/**
 * Created by Administrator on 2016.10.12.
 * 為了讓quartz種可以使用Spring的注入
 */
@Component("jobFactory")
public class JobFactory extends AdaptableJobFactory {
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;
    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        // 调用父类的方法
        Object jobInstance = super.createJobInstance(bundle);
       // 进行注入
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}

+ 57 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/quartz/SchedulerConfig.java

@ -0,0 +1,57 @@
package com.yihu.wlyy.statistics.config.quartz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
/**
 * Created by chenweida on 2016/2/3.
 */
@Configuration
public class SchedulerConfig {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private JobFactory jobFactory;
    @Autowired
    private DataSource dataSource;
    @Value("${quartz.name}")
    private String quartzName;
    @Bean
    SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setJobFactory(jobFactory);
        bean.setApplicationContext(this.applicationContext);
        bean.setOverwriteExistingJobs(true);
        bean.setBeanName(quartzName);
        bean.setStartupDelay(20);// 延时启动
        bean.setAutoStartup(true);
        bean.setDataSource(dataSource);
        bean.setQuartzProperties(quartzProperties());
        return bean;
    }
    /**
     * quartz配置文件
     * @return
     * @throws IOException
     */
    @Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }
}

+ 15 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/config/war/ServletInitializer.java

@ -0,0 +1,15 @@
package com.yihu.wlyy.statistics.config.war;
import com.yihu.wlyy.Application;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
/**
 * Created by Administrator on 2016.10.14.
 */
public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

+ 291 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/controller/JobController.java

@ -0,0 +1,291 @@
package com.yihu.wlyy.statistics.controller;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.wlyy.statistics.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * 任务启动
 *
 * @author chenweida
 */
@RestController
@RequestMapping(value = "/job", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "后台-任务控制")
public class JobController extends EnvelopRestEndpoint {
    @Autowired
    private  JobService jobService;
    /**
     * 启动任务
     *
     * @param id id
     * @return
     */
    @ApiOperation(value = "根据ID立即单个任务")
    @RequestMapping(value = "startNowById", method = RequestMethod.GET)
    public Envelop startNowById(
            @ApiParam(name = "id", value = "任务ID", required = true)
            @RequestParam(value = "id", required = true) String id) throws Exception {
        jobService.startNowById(id);
        return success("启动成功!");
    }
    /**
     * 生成过去几天的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去几天的数据")
    @RequestMapping(value = "productDataByDay", method = RequestMethod.GET)
    public Envelop productDataByDay(
            @ApiParam(name = "day", value = "距离今天的天数(如果是要生成昨天的数据,day=1)")
            @RequestParam(value = "day", required = true) int day) throws Exception {
        jobService.productDataByDay(day);
        return success("启动成功!");
    }
    /**
     * 生成过去某一天的全部的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去某一天的全部的数据")
    @RequestMapping(value = "productDataByOneDay", method = RequestMethod.GET)
    public Envelop productDataByOneDay(
            @ApiParam(name = "day", value = "yyyy-MM-dd")
            @RequestParam(value = "day", required = true)String day) throws Exception {
        jobService.productDataByOneDay(day);
        return success("启动成功!");
    }
    /**
     * 生成过去某一天到某一天的全部的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的全部的数据(包含头尾)")
    @RequestMapping(value = "productDataByDayToDay", method = RequestMethod.GET)
    public Envelop productDataByDayToDay(
            @ApiParam(name = "start", value = "yyyy-MM-dd", required = true)
            @RequestParam(value = "start", required = true)String start,
            @ApiParam(name = "end", value = "yyyy-MM-dd", required = true)
            @RequestParam(value = "end", required = true)String end) throws Exception {
        jobService.productDataByDayToDay(start,end);
        return success("启动成功!");
    }
    /**
     * 生成过去某一天到某一天的某个指标的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的某个指标的数据(包含头尾)")
    @RequestMapping(value = "productDataByDayToDayAndId", method = RequestMethod.GET)
    public Envelop productDataByDayToDayAndId(
            @ApiParam(name = "start", value = "yyyy-MM-dd")
            @RequestParam(value = "start", required = true)String start,
            @ApiParam(name = "end", value = "yyyy-MM-dd")
            @RequestParam(value = "end", required = true)String end,
            @ApiParam(name = "ids", value = "任务ids多个逗号分割")
            @RequestParam(value = "ids", required = true)String ids) throws Exception {
        jobService.productDataByDayToDayAndId(start,end,ids);
        return success("启动成功!");
    }
    /**
     * 生成过去某一天的某一个指标的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去某一天的全部的数据")
    @RequestMapping(value = "productDataByOneDayWithId", method = RequestMethod.GET)
    public Envelop productDataByOneDayWithId(
            @ApiParam(name = "day", value = "yyyy-MM-dd")
            @RequestParam(value = "day", required = true)String day,
            @ApiParam(name = "id", value = "任务id")
            @RequestParam(value = "id", required = true)String id) throws Exception {
        jobService.productDataByOneDayWithId(day, id);
        return success("启动成功!");
    }
    /**
     * 生成过去到现在的全部的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去到现在的全部的数据")
    @RequestMapping(value = "productDataByDayAndId", method = RequestMethod.GET)
    public Envelop productDataByDayAndId(
            @ApiParam(name = "day", value = "距离今天的天数(如果是要生成昨天的数据,day=1)")
            @RequestParam(value = "day", required = true) int day,
            @ApiParam(name="id",required=true)
            @RequestParam(value = "id", required = true) String id) throws Exception {
        jobService.productDataByDayAndId(day, id);
        return success("启动成功!");
    }
    /**
     * 启动任务
     *
     * @param id id
     * @return
    */
    @ApiOperation(value = "启动单个任务")
    @RequestMapping(value = "startById", method = RequestMethod.GET)
    public Envelop startById(
            @ApiParam(name="id",value="任务id",required=true)
            @RequestParam(value = "id", required = true) String id) throws Exception {
        jobService.startById(id);
        return success("启动成功!");
    }
    /**
     * 停止任务
     *
     * @param id id
     * @return
     */
    @ApiOperation(value = "停止单个任务")
    @RequestMapping(value = "stopById", method = RequestMethod.GET)
    public Envelop stopById(
            @ApiParam(name="id",value="任务id",required=true)
            @RequestParam(value = "id", required = true)String id) throws Exception {
        jobService.stopById(id);
        return success("停止成功!");
    }
    /**
     * 停止所有任务
     *
     * @return
     */
    @ApiOperation(value = "停止所有任务")
    @RequestMapping(value = "stopAll", method = RequestMethod.GET)
    public Envelop stopAll() throws Exception {
        jobService.stopAll();
        return success("停止成功!");
    }
    /**
     * 启动所有任务
     *
     * @return
     */
    @ApiOperation(value = "启动所有任务")
    @RequestMapping(value = "startAll", method = RequestMethod.GET)
    public Envelop startAll() throws Exception {
        jobService.startAll();
        return success("启动成功!");
    }
    /**
     * 启动判断的任务
     *
     * @return
     */
    @ApiOperation(value = "启动清除缓存的任务")
    @RequestMapping(value = "startCleanCacheJob", method = RequestMethod.GET)
    public Envelop startCleanCacheJob() throws Exception {
        jobService.startCleanCacheJob();
        return success("启动成功!");
    }
    /**
     * 停止判断的任务
     *
     * @return
     */
    @ApiOperation(value = "停止清除缓存的任务")
    @RequestMapping(value = "stopCleanCacheJob", method = RequestMethod.GET)
    public Envelop stopCleanCacheJob() throws Exception {
        jobService.stopCleanCacheJob();
        return success("停止成功!");
    }
    //================================================没有休眠时间=============================================================
    /**
     * 生成过去某一天到某一天的某个指标的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的某个指标的数据(包含头尾)没有休眠时间")
    @RequestMapping(value = "productDataByDayToDayAndIdNoSleep", method = RequestMethod.GET)
    public Envelop productDataByDayToDayAndIdNoSleep(
            @ApiParam(name = "start", value = "yyyy-MM-dd")
            @RequestParam(value = "start", required = true)String start,
            @ApiParam(name = "end", value = "yyyy-MM-dd")
            @RequestParam(value = "end", required = true)String end,
            @ApiParam(name = "id", value = "任务id")
            @RequestParam(value = "id", required = true)String id,
            @ApiParam(name = "sleepTime", value = "任务间隔时间(秒)")
            @RequestParam(value = "sleepTime", required = true)Long sleepTime) throws Exception {
        jobService.productDataByDayToDayAndIdNoSleep(start,end,id,sleepTime);
        return success("启动成功!");
    }
    /**
     * 生成过去某一天的全部的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去某一天的全部的数据")
    @RequestMapping(value = "productDataByOneDayNoSleep", method = RequestMethod.GET)
    public Envelop productDataByOneDayNoSleep(
            @ApiParam(name = "day", value = "yyyy-MM-dd")
            @RequestParam(value = "day", required = true)String day,
            @ApiParam(name = "sleepTime", value = "任务间隔时间(秒)")
            @RequestParam(value = "sleepTime", required = true)Long sleepTime) throws Exception {
        jobService.productDataByOneDayNoSleep(day,sleepTime);
        return success("启动成功!");
    }
    /************************************************************************************/
    /**
     * 生成过去某一天到某一天的全部的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的全部的数据(包含头尾)按周或月统计")
    @RequestMapping(value = "productDataByDayToDay2", method = RequestMethod.GET)
    public Envelop productDataByDayToDay2(
            @ApiParam(name = "start", value = "yyyy-MM-dd", required = true)
            @RequestParam(value = "start", required = true)String start,
            @ApiParam(name = "end", value = "yyyy-MM-dd", required = true)
            @RequestParam(value = "end", required = true)String end,
            @ApiParam(name = "incrementInterval", value = "2周,3月", required = true)
            @RequestParam(value = "incrementInterval", required = true)Integer incrementInterval,
            @ApiParam(name = "id", value = "指标id,逗号分隔(为空表示全部)", required = false)
            @RequestParam(value = "id", required = false)String id) throws Exception {
        if(incrementInterval==2){
            jobService.productWeekByDayToDay(start,end,id);
        }else if(incrementInterval==3){
            jobService.productMonthByDayToDay(start,end,id);
        }else{
            return success("启动失败!");
        }
        return success("启动成功!");
    }
}

+ 278 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/controller/QueryController.java

@ -0,0 +1,278 @@
package com.yihu.wlyy.statistics.controller;
import com.yihu.wlyy.statistics.etl.cache.Cache;
import com.yihu.wlyy.statistics.etl.save.es.ElasticFactory;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.util.ElasticsearchUtil;
import com.yihu.wlyy.statistics.vo.SaveModel;
import io.searchbox.client.JestClient;
import io.searchbox.core.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * Created by chenweida on 2017/7/1.
 */
@RestController
@RequestMapping("/quotaQuery")
@Api(description = "指标查询")
public class QueryController {
    @Autowired
    private ElasticsearchUtil elasticsearchUtil;
    @Autowired
    private ElasticFactory elasticFactory;
    @Value("${es.type}")
    private String esType;
    @Value("${es.index}")
    private String esIndex;
    @ApiOperation(value = "执行sql")
    @RequestMapping(value = "/excuteSQL", method = RequestMethod.GET)
    public List<SaveModel> excuteSQL(
            @ApiParam(name = "sql", value = "执行的", required = true) @RequestParam(value = "sql", required = true) String sql) {
        List<SaveModel> saveModels = elasticsearchUtil.excute(sql);
        return saveModels;
    }
    @ApiOperation(value = "查询某几个指标某个层级的列表")
    @RequestMapping(value = "/getQuotasList", method = RequestMethod.GET)
    public Map<String, List<SaveModel>> getQuotasList(
            @ApiParam(name = "ids", value = "指标id,多个逗号分割", required = true) @RequestParam(value = "ids", required = true) String ids,
            @ApiParam(name = "code", value = "指标的code", required = true) @RequestParam(value = "code", required = true) String code,
            @ApiParam(name = "arealevel", value = "指标的level(1 省 2 市 3 区县 4 机构 5团队)", required = true) @RequestParam(value = "arealevel", required = true) String arealevel,
            @ApiParam(name = "childrenArealevel", value = "指标的level(1 省 2 市 3 区县 4 机构 5团队)", required = true) @RequestParam(value = "childrenArealevel", required = true) String childrenArealevel,
            @ApiParam(name = "timeLevel", value = " 1增量的指标  2到达量的指标", required = true) @RequestParam(value = "timeLevel", required = true) String timeLevel,
            @ApiParam(name = "date", value = "时间(yyyy-MM-dd),不传默认查今天", required = false) @RequestParam(value = "date", required = false) String date) {
        //如果日期是空初始化是今天
        if (StringUtils.isEmpty(date)) {
            date = DateUtil.dateToStrShort(new Date());
        }
        Map<String, List<SaveModel>> jo = new HashMap<>();
        for (String id : ids.split(",")) {
            jo.put("index_" + id, elasticsearchUtil.findOneDateQuotaByChllevel(id, code, date, timeLevel, arealevel, childrenArealevel));
        }
        return jo;
    }
    @ApiOperation(value = "查询某个1级维度指标")
    @RequestMapping(value = "/getQuotaLevel1", method = RequestMethod.GET)
    public List<SaveModel> getQuotaLevel1(
            @ApiParam(name = "id", value = "指标id", required = true) @RequestParam(value = "id", required = true) String id,
            @ApiParam(name = "code", value = "指标的code", required = true) @RequestParam(value = "code", required = true) String code,
            @ApiParam(name = "arealevel", value = "指标的level(1 省 2 市 3 区县 4 机构 5团队)", required = true) @RequestParam(value = "arealevel", required = true) String arealevel,
            @ApiParam(name = "timeLevel", value = " 1增量的指标  2到达量的指标", required = true) @RequestParam(value = "timeLevel", required = true) String timeLevel,
            @ApiParam(name = "date", value = "时间(yyyy-MM-dd),不传默认查今天", required = false) @RequestParam(value = "date", required = false) String date) {
        //如果日期是空初始化是今天
        if (StringUtils.isEmpty(date)) {
            date = DateUtil.dateToStrShort(new Date());
        }
        List<SaveModel> saveModels = elasticsearchUtil.findOneDateQuotaLevel1(id, code, date, timeLevel, arealevel);
        return saveModels;
    }
    @ApiOperation(value = "查询某个2级维度指标")
    @RequestMapping(value = "/getQuotaLevel2", method = RequestMethod.GET)
    public List<SaveModel> getQuotaLevel2(
            @ApiParam(name = "id", value = "指标id", required = true) @RequestParam(value = "id", required = true) String id,
            @ApiParam(name = "code", value = "指标的code", required = true) @RequestParam(value = "code", required = true) String code,
            @ApiParam(name = "arealevel", value = "指标的level(1 省 2 市 3 区县 4 机构 5团队)", required = true) @RequestParam(value = "arealevel", required = true) String arealevel,
            @ApiParam(name = "timeLevel", value = " 1增量的指标  2到达量的指标", required = true) @RequestParam(value = "timeLevel", required = true) String timeLevel,
            @ApiParam(name = "date", value = "时间(yyyy-MM-dd),不传默认查今天", required = false) @RequestParam(value = "date", required = false) String date) {
        //如果日期是空初始化是今天
        if (StringUtils.isEmpty(date)) {
            date = DateUtil.dateToStrShort(new Date());
        }
        List<SaveModel> saveModels = elasticsearchUtil.findOneDateQuotaLevel2(id, code, date, timeLevel, arealevel);
        return saveModels;
    }
    @ApiOperation(value = "查询某个指标某个层级的折线图")
    @RequestMapping(value = "/getQuotasLine", method = RequestMethod.GET)
    public List<SaveModel> getQuotasLine(
            @ApiParam(name = "id", value = "指标id", required = true) @RequestParam(value = "id", required = true) String id,
            @ApiParam(name = "code", value = "指标的code", required = true) @RequestParam(value = "code", required = true) String code,
            @ApiParam(name = "arealevel", value = "指标的level(1 省 2 市 3 区县 4 机构 5团队)", required = true) @RequestParam(value = "arealevel", required = true) String arealevel,
            @ApiParam(name = "timeLevel", value = " 1增量的指标  2到达量的指标", required = true) @RequestParam(value = "timeLevel", required = true) String timeLevel,
            @ApiParam(name = "startDate", value = "时间(yyyy-MM-dd)", required = false) @RequestParam(value = "startDate", required = true) String startDate,
            @ApiParam(name = "endDate", value = "时间(yyyy-MM-dd),不传默认查今天", required = false) @RequestParam(value = "endDate", required = false) String endDate,
            @ApiParam(name = "interval", value = "1日 2周 3月", required = false) @RequestParam(value = "interval", required = false) String interval
    ) {
        //如果日期是空初始化是今天
        if (StringUtils.isEmpty(endDate)) {
            endDate = DateUtil.dateToStrShort(new Date());
        }
        List<SaveModel> saveModels = elasticsearchUtil.findQuotaLines(id, code, startDate, endDate, timeLevel, arealevel, interval);
        return saveModels;
    }
    @ApiOperation(value = "查询某几个指标的到达量")
    @RequestMapping(value = "/getQuotas", method = RequestMethod.GET)
    public Map<String, List<SaveModel>> getQuotas(
            @ApiParam(name = "ids", value = "指标id,多个逗号分割", required = true) @RequestParam(value = "ids", required = true) String ids,
            @ApiParam(name = "code", value = "指标的code", required = true) @RequestParam(value = "code", required = true) String code,
            @ApiParam(name = "arealevel", value = "指标的level(1 省 2 市 3 区县 4 机构 5团队)", required = true) @RequestParam(value = "arealevel", required = true) String arealevel,
            @ApiParam(name = "date", value = "时间(yyyy-MM-dd),不传默认查今天", required = false) @RequestParam(value = "date", required = false) String date) {
        Map<String, List<SaveModel>> returnMap = new HashMap<>();
        for (String id : ids.split(",")) {
            returnMap.put("index_" + id, elasticsearchUtil.findOneDateQuota(id, code, date, "2", arealevel));
        }
        return returnMap;
    }
    @ApiOperation(value = "删除某一天的某一个指标的数据")
    @RequestMapping(value = "/deleteQuotaWithId", method = RequestMethod.DELETE)
    public String deleteQuota(
            @ApiParam(name = "id", value = "指标id", required = true) @RequestParam(value = "id", required = true) String id,
            @ApiParam(name = "date", value = "时间(yyyy-MM-dd)", required = true) @RequestParam(value = "date", required = true) String date) {
        return deleteData(DateUtil.strToDate(date, "yyyy-MM-dd"), id).toString();
    }
    @ApiOperation(value = "删除某一天的数据")
    @RequestMapping(value = "/deleteQuota", method = RequestMethod.DELETE)
    public String deleteQuota(
            @ApiParam(name = "date", value = "时间(yyyy-MM-dd)", required = true) @RequestParam(value = "date", required = true) String date) {
        return deleteData(DateUtil.strToDate(date, "yyyy-MM-dd"), null).toString();
    }
    @ApiOperation(value = "删除某一天到某一天的数据")
    @RequestMapping(value = "/deleteQuotaDateToDate", method = RequestMethod.DELETE)
    public String deleteQuotaDateToDate(
            @ApiParam(name = "start", value = "时间(yyyy-MM-dd)", required = true) @RequestParam(value = "start", required = true) String start,
            @ApiParam(name = "end", value = "时间(yyyy-MM-dd)", required = true) @RequestParam(value = "end", required = true) String end) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(start);
            Date endDate = endDate = sdf.parse(end);
            int day = daysBetween(startDate, endDate);
            for (int i = 0; i < day; i++) {
                Cache.cleanCache();//清空缓存
                deleteData(DateUtil.strToDate(getYesterday(i, startDate), "yyyy-MM-dd"), null).toString();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "删除成功";
    }
    @ApiOperation(value = "删除某一天到某一天的数据")
    @RequestMapping(value = "/deleteQuotaDateToDateWithId", method = RequestMethod.DELETE)
    public String deleteQuotaDateToDateWithId(
            @ApiParam(name = "id", value = "指标id", required = true) @RequestParam(value = "id", required = true) String id,
            @ApiParam(name = "start", value = "时间(yyyy-MM-dd)", required = true) @RequestParam(value = "start", required = true) String start,
            @ApiParam(name = "end", value = "时间(yyyy-MM-dd)", required = true) @RequestParam(value = "end", required = true) String end) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(start);
            Date endDate = endDate = sdf.parse(end);
            int day = daysBetween(startDate, endDate);
            for (int i = 0; i < day; i++) {
                Cache.cleanCache();//清空缓存
                deleteData(DateUtil.strToDate(getYesterday(i, startDate), "yyyy-MM-dd"), id).toString();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "删除成功";
    }
    public static String getYesterday(Integer day, Date startDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        cal.add(Calendar.DAY_OF_MONTH, day);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return yesterday;
    }
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }
    /**
     * 删除 某个指标某一天的某个timelevel的数据
     *
     * @param quotaDate
     * @param quotaCode
     */
    private net.sf.json.JSONObject deleteData(Date quotaDate, String quotaCode) {
        net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject();
        JestClient jestClient = null;
        try {
            jestClient = elasticFactory.getJestClient();
            //先根据条件查找出来
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            if (quotaDate != null) {
                boolQueryBuilder.must(QueryBuilders.matchQuery("quotaDate", quotaDate));
            }
            if (!StringUtils.isEmpty(quotaCode)) {
                boolQueryBuilder.must(QueryBuilders.matchQuery("quotaCode", quotaCode));
            }
            searchSourceBuilder.query(boolQueryBuilder
            ).size(500000);//一次取10000条
            Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(esIndex).addType(esType)
                    .build();
            SearchResult result = jestClient.execute(search);
            List<SaveModel> saveModels = result.getSourceAsObjectList(SaveModel.class);
            //根据id批量删除
            Bulk.Builder bulk = new Bulk.Builder().defaultIndex(esIndex).defaultType(esType);
            for (SaveModel obj : saveModels) {
                Delete index = new Delete.Builder(obj.getId()).build();
                bulk.addAction(index);
            }
            BulkResult br = jestClient.execute(bulk.build());
            jsonObject.put("flag", br.isSucceeded());
            jsonObject.put("count", saveModels.size());
            return jsonObject;
        } catch (Exception e) {
            jsonObject.put("flag", false);
            jsonObject.put("message", e.getMessage());
            return jsonObject;
        } finally {
            if (jestClient != null) {
                jestClient.shutdownClient();
            }
        }
    }
}

+ 35 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/dao/QuartzJobConfigDao.java

@ -0,0 +1,35 @@
package com.yihu.wlyy.statistics.dao;
import com.yihu.jw.entity.base.statistics.JobConfigDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
 * Created by Administrator on 2016/8/17.
 */
public interface QuartzJobConfigDao extends PagingAndSortingRepository<JobConfigDO, String>, JpaSpecificationExecutor<JobConfigDO> {
    @Query(" FROM JobConfigDO a WHERE a.id=?1 and a.status=?2 and a.del='1'")
    JobConfigDO findById(String code, String status);
    @Query(" FROM JobConfigDO a WHERE a.status=?1 and a.del='1'")
    List<JobConfigDO> findByAll(String s);
    @Query(" FROM JobConfigDO a WHERE  a.del='1' and a.id !=11 order by a.id asc")
    List<JobConfigDO> findByIds();
    @Query(" FROM JobConfigDO a WHERE a.id=?1 and a.del='1'")
    JobConfigDO findById(String id);
    @Modifying
    @Query(" update JobConfigDO a set a.status=?2 where a.id=?1 ")
    int updateStatus(String id, String status);
    @Query(" from JobConfigDO p where p.status=?1 and p.del='1'")
    List<JobConfigDO> findByStatus(String s);
}

+ 11 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/dao/QuartzJobLogDao.java

@ -0,0 +1,11 @@
package com.yihu.wlyy.statistics.dao;
import com.yihu.jw.entity.base.statistics.JobLogDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
 * Created by Administrator on 2016/8/16.
 */
public interface QuartzJobLogDao extends PagingAndSortingRepository<JobLogDO, String>, JpaSpecificationExecutor<JobLogDO> {
}

+ 11 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/dao/QuartzQuotaDao.java

@ -0,0 +1,11 @@
package com.yihu.wlyy.statistics.dao;
import com.yihu.jw.entity.base.statistics.QuotaDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
 * Created by Administrator on 2016/8/16.
 */
public interface QuartzQuotaDao extends PagingAndSortingRepository<QuotaDO, String>, JpaSpecificationExecutor<QuotaDO> {
}

+ 16 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/dao/WlyyDimensionQuotaDao.java

@ -0,0 +1,16 @@
package com.yihu.wlyy.statistics.dao;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
 * Created by chenweida on 2017/7/10.
 */
public interface WlyyDimensionQuotaDao extends PagingAndSortingRepository<DimensionQuotaDO, Long>, JpaSpecificationExecutor< DimensionQuotaDO> {
    @Query("from DimensionQuotaDO w where w.quotaCode=?1 order by sort asc")
    List<DimensionQuotaDO> findDimensionQuotasByQuotaCode(String id);
}

+ 28 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/cache/Cache.java

@ -0,0 +1,28 @@
package com.yihu.wlyy.statistics.etl.cache;
import com.yihu.wlyy.statistics.vo.DataModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2017/7/11.
 */
public class Cache {
    private static Map<String, List<DataModel>> cache = new HashMap<>();
    public static void addCache(String key, List<DataModel> data) {
        cache.put(key, data);
    }
    public static List<DataModel> getCache(String key) {
        return cache.get(key);
    }
    public static void cleanCache(){
        cache.clear();
    }
}

+ 237 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/compute/ComputeHelper.java

@ -0,0 +1,237 @@
package com.yihu.wlyy.statistics.etl.compute;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.jw.entity.base.team.BaseTeamDO;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import com.yihu.wlyy.statistics.vo.SaveModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.*;
/**
 * Created by chenweida on 2017/6/1.
 */
@Component
@Scope("prototype")
public class ComputeHelper {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * @param dataModels      过滤之后的数据
     * @param dimensionQuotas 指标的维度
     * @param wlyyJobCongId
     * @param endTime
     * @return
     */
    public List<SaveModel> compute(List<DataModel> dataModels, List<DimensionQuotaDO> dimensionQuotas, String wlyyJobCongId, String endTime, String timeLevel) {
        try {
            //得到全部团队
            List<BaseTeamDO> teams = findAllTeam();
            //计算数据 返回map key 根据团队和维度拼凑在map中的key   1-2-1  如果维度是长度是0返回团队id 1
            Map<String, List<DataModel>> computeMap = computeDataModel(dimensionQuotas, dataModels);
            //初始化map并且设置统计出来的数目
            List<SaveModel> savemodels = initAndSetResult(teams, dimensionQuotas, computeMap, wlyyJobCongId, endTime, timeLevel);
            return savemodels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * //计算数据 返回map
     *
     * @param dataModels      过滤过后的元数据
     * @param dimensionQuotas 该指标的维度
     * @return
     */
    private Map<String, List<DataModel>> computeDataModel(List<DimensionQuotaDO> dimensionQuotas, List<DataModel> dataModels) {
        Map<String, List<DataModel>> returnMap = new HashMap<>();
        dataModels.stream().forEach(one -> {
            String key = getKey(one, dimensionQuotas.size());
            List<DataModel> sms = new ArrayList<DataModel>();
            if (returnMap.containsKey(key)) {
                sms = returnMap.get(key);
            }
            sms.add(one);
            returnMap.put(key, sms);
        });
        return returnMap;
    }
    /**
     * 获取key  根据团队和维度拼凑在map中的key   1-2-1  如果维度是长度是0返回团队id 1
     *
     * @param one
     * @param size
     * @return
     */
    private String getKey(DataModel one, int size) {
        try {
            if (size > 0) {
                StringBuffer str = new StringBuffer(one.getTeam());
                for (int i = 1; i <= size; i++) {
                    String invokeKey = (String) DataModel.class.getMethod("getSlaveKey" + i).invoke(one);
                    str.append("-" + invokeKey);
                }
                return str.toString();
            } else {
                return one.getTeam();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 得到全部团队
     *
     * @return
     */
    private List<BaseTeamDO> findAllTeam() {
        String sql = "SELECT " +
                "  wat.id id, " +
                "  wat.name name, " +
                "  h.`code` org_code, " +
                "  h.`name` org_name, " +
                "  h.town   town_code, " +
                "  h.town_name  town_name " +
                "FROM " +
                "  dm_hospital h, " +
                "  wlyy_admin_team wat " +
                "WHERE " +
                "  h.`code` = wat.org_code " +
                "AND wat.available = 1";
        List<BaseTeamDO> adminTeams = jdbcTemplate.query(sql, new BeanPropertyRowMapper(BaseTeamDO.class));
        return adminTeams;
    }
    public static void main(String[] args) {
        System.out.println("3502060200".substring(0, 8));
    }
    /**
     * 初始化map
     */
    private List<SaveModel> initAndSetResult(List<BaseTeamDO> teams, List<DimensionQuotaDO> dimensionQuotas, Map<String, List<DataModel>> countMap, String wlyyJobCongId, String endTime, String timeLevel) throws Exception {
        List<SaveModel> returnList = new ArrayList<>();
        Map<String, SaveModel> lastMaps = new HashMap<>();
        //初始化团队的数据
        for (int i = 0; i < teams.size(); i++) {
            BaseTeamDO one = teams.get(i);
            //排除测试机构
            if (one.getOrgId().length() == 10) {
                SaveModel saveModel = new SaveModel();
                saveModel.setCity("350200");
                saveModel.setCityName("厦门市");
//                saveModel.setTown(one.getTownCode());
//                saveModel.setTownName(one.getTownName());
//                //把末尾不是00的机构转换成00
//                String orgCode = one.getOrgCode();
//                if (!"00".equals(orgCode.substring(8))) {
//                    orgCode = orgCode.substring(0, 8) + "00";
//                }
//                saveModel.setHospital(orgCode);
//                saveModel.setHospitalName(one.getOrgName());
                saveModel.setTeam(one.getId().toString());
                saveModel.setTeamName(one.getName());
                saveModel.setQuotaCode(wlyyJobCongId);
                saveModel.setCreateTime(new Date());
                saveModel.setTimeLevel(timeLevel);
                saveModel.setAreaLevel("5");//团队
                saveModel.setQuotaDate(DateUtil.strToDate(endTime, "yyyy-MM-dd"));
                lastMaps.put(one.getId().toString(), saveModel);
            }
        }
        //根据维度初始化数据
            for (int i = 0; i < dimensionQuotas.size(); i++) {
                Map<String, SaveModel> mapTemp = new HashMap<>();
                DimensionQuotaDO dimensionQuota = dimensionQuotas.get(i);
//                List<DictModel> dictModels = jdbcTemplate.query(dimensionQuota.getDictSql(), new BeanPropertyRowMapper(DictModel.class));
                List<Map<String, Object>> listMap = jdbcTemplate.queryForList(dimensionQuota.getDictSql());
                for (Map.Entry<String, SaveModel> oneSaveModel : lastMaps.entrySet()) {
                    for (int j = 0; j < listMap.size(); j++) {
                        SaveModel saveModelTemp = new SaveModel();
                        Map<String, Object> dictModel = listMap.get(j);
                        BeanUtils.copyProperties(oneSaveModel.getValue(), saveModelTemp);
                        SaveModel.class.getMethod("setSlaveKey" + (i + 1) + "Name", String.class).invoke(saveModelTemp, dictModel.get("name"));
                        SaveModel.class.getMethod("setSlaveKey" + (i + 1), String.class).invoke(saveModelTemp, dictModel.get("code"));
                        //根据维度生成新的key
                        StringBuffer strKey = new StringBuffer(oneSaveModel.getKey() + "-" + dictModel.get("code"));
                        mapTemp.put(strKey.toString(), saveModelTemp);
                    }
                }
            //如果字典长度是0 抛出异常
            if (listMap.size() == 0) {
                throw new Exception("dict size:0,sql:" + dimensionQuota.getDictSql());
            }
            lastMaps = mapTemp;
        }
//        for (int i = 0; i < dimensionQuotas.size(); i++) {
//            Map<String, SaveModel> mapTemp = new HashMap<>();
//            WlyyDimensionQuota dimensionQuota = dimensionQuotas.get(i);
//            List<DictModel> dictModels = jdbcTemplate.query(dimensionQuota.getDictSql(), new BeanPropertyRowMapper(DictModel.class));
//            for (Map.Entry<String, SaveModel> oneSaveModel : lastMaps.entrySet()) {
//                for (int j = 0; j < dictModels.size(); j++) {
//                    SaveModel saveModelTemp = new SaveModel();
//                    DictModel dictModel = dictModels.get(j);
//
//                    BeanUtils.copyProperties(oneSaveModel.getValue(), saveModelTemp);
//                    SaveModel.class.getMethod("setSlaveKey" + (i + 1) + "Name", String.class).invoke(saveModelTemp, dictModel.getName());
//                    SaveModel.class.getMethod("setSlaveKey" + (i + 1), String.class).invoke(saveModelTemp, dictModel.getCode());
//                    //根据维度生成新的key
//                    StringBuffer strKey = new StringBuffer(oneSaveModel.getKey() + "-" + dictModel.getCode());
//                    mapTemp.put(strKey.toString(), saveModelTemp);
//                }
//            }
//            //如果字典长度是0 抛出异常
//            if (dictModels.size() == 0) {
//                throw new Exception("dict size:0,sql:" + dimensionQuota.getDictSql());
//            }
//            lastMaps = mapTemp;
//        }
        //设置值
        for (Map.Entry<String, SaveModel> one : lastMaps.entrySet()) {
            if (countMap.containsKey(one.getKey())) {
                List<DataModel> dataModels = countMap.get(one.getKey());
                Double result1 = 0.0;
                Double result2 = 0.0;
                for (int j = 0; j < dataModels.size(); j++) {
                    DataModel dataModel = dataModels.get(j);
                    result1 += dataModel.getNum();
                    result2++;
                }
                SaveModel saveModel = one.getValue();
                saveModel.setResult1(result1);
                saveModel.setResult2(result2);
                if(dataModels.size()>0&& StringUtils.isNotEmpty(dataModels.get(0).getSlaveKey1())&&!StringUtils.isNotEmpty(saveModel.getSlaveKey1())){
                    saveModel.setSlaveKey1(dataModels.get(0).getSlaveKey1());
                }
                if(dataModels.size()>0&& StringUtils.isNotEmpty(dataModels.get(0).getSlaveKey2())&&!StringUtils.isNotEmpty(saveModel.getSlaveKey2())){
                    saveModel.setSlaveKey2(dataModels.get(0).getSlaveKey2());
                }
                returnList.add(saveModel);
            } else {
                returnList.add(one.getValue());
            }
        }
        return returnList;
    }
}

+ 23 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/Convert.java

@ -0,0 +1,23 @@
package com.yihu.wlyy.statistics.etl.convert;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
 * Created by chenweida on 2017/6/5.
 */
public interface Convert {
    /**
     *
     * @param jdbcTemplate jdbc工具
     * @param oneList 需要赋值的数据
     * @param slaveLevel 维度 1 2 3
     * @param temp 指标类
     * @return
     */
    public List<DataModel> convert(JdbcTemplate jdbcTemplate,List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp );
}

+ 51 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/ConvertHelper.java

@ -0,0 +1,51 @@
package com.yihu.wlyy.statistics.etl.convert;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.wlyy.statistics.vo.DataModel;
import com.yihu.wlyy.statistics.vo.FilterModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.lang.reflect.Method;
import java.util.List;
/**
 * Created by chenweida on 2017/6/5.
 * 维度的key值转换器
 */
@Component
@Scope("prototype")
public class ConvertHelper {
    @Autowired
    private WlyyDimensionQuotaDao wlyyDimensionQuotaDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 把每个维度的key清洗到对应的slaveKey中
     *
     * @param filterModel     过滤之后的model
     * @param dimensionQuotas 维度的列表
     * @return
     * @throws Exception
     */
    public FilterModel convert(FilterModel filterModel, List<DimensionQuotaDO> dimensionQuotas) throws Exception {
        List<DataModel> data = filterModel.getData();
        for (int i = 0; i < dimensionQuotas.size(); i++) {
            DimensionQuotaDO temp = dimensionQuotas.get(i);
            String clazz = temp.getConvertClazz();
            if (!StringUtils.isEmpty(clazz)) {
                //反射出对象并且调用convert方法去转换对应的slavekey
                Object obj = Class.forName(clazz).newInstance();
                Method method = obj.getClass().getMethod("convert", JdbcTemplate.class, List.class, String.class, DimensionQuotaDO.class);
                data = (List<DataModel>) method.invoke(obj, jdbcTemplate, data, String.valueOf(i + 1), temp);
                filterModel.setData(data);
            }
        }
        return filterModel;
    }
}

+ 59 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/AgeConvert.java

@ -0,0 +1,59 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.util.Contant;
import com.yihu.wlyy.statistics.util.IdCardUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2017/6/5.
 */
public class AgeConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp ) {
        Map<String,Object> tempMap = new HashMap<>();
        List<DataModel> result = new ArrayList<>();
        for(DataModel one:oneList) {
            try {
                Object value = one.getIdcard();
                Integer age= IdCardUtil.getAgeForIdcard(String.valueOf(value));
                String key = getAgeCode(age);
                tempMap.put(String.valueOf(value),one);
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, key);
//                result.add(one);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        return oneList;
    }
    public String getAgeCode(Integer age) {
        if (age <= 6) {
            return Contant.convert.level_age_1;
        } else if (age >= 7 && age <= 18) {
            return Contant.convert.level_age_2;
        } else if (age >= 19 && age <= 30) {
            return Contant.convert.level_age_3;
        } else if (age >= 31 && age <= 50) {
            return Contant.convert.level_age_4;
        } else if (age >= 51 && age < 65) {
            return Contant.convert.level_age_5;
        } else {
            return Contant.convert.level_age_6;
        }
    }
}

+ 70 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/ConsultTimeConvert.java

@ -0,0 +1,70 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
/**
 * Created by chenweida on 2017/6/5.
 */
public class ConsultTimeConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        oneList.stream().forEach(one -> {
            try {
                String value =one.getSlaveKey1();
                String valueStr = "1";
                if (!StringUtils.isEmpty(value)) {
                    try {
                        Date time= DateUtil.strToDate(one.getSlaveKey1(),"yyyy-MM-dd HH:mm:ss");
                        valueStr= paserReplyTime(time);
                    } catch (Exception e) {
                    }
                }
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, valueStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return oneList;
    }
    public String paserReplyTime(Date date){
        if(date!=null){
            Calendar calendar  =  new GregorianCalendar();
            calendar.setTime(date);
            //获取小时
            int hour = calendar.get(calendar.HOUR_OF_DAY);
            if(hour>=0&&hour<8){
                return "1";
            }else if(hour>=8&&hour<12){
                return "2";
            }else if(hour>=12&&hour<13){
                return "3";
            }else if(hour>=13&&hour<18){
                return "4";
            }else if(hour>=18&&hour<24){
                return"5";
            }else{
                return "";
            }
        }
        return "";
    }
}

+ 58 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/DeviceTypeConvert.java

@ -0,0 +1,58 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2017/6/5.
 */
public class DeviceTypeConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate,List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        String sql = temp.getDictSql();
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        for(DataModel one:oneList) {
            try {
                String key = getKey(one,result);
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, key);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        return oneList;
    }
    //1、血糖,2、血压
    public String getKey(DataModel dataModel,List<Map<String,Object>> dict){
        if("1".equals(dataModel.getServerType())){
             for (Map<String,Object> one : dict){
                String result = one.get("name")+"";
                if(result.equals("血糖")){
                    return one.get("code")+"";
                }
            }
        }else if("2".equals(dataModel.getServerType())){
            for (Map<String,Object> one : dict){
                String result = one.get("name")+"";
                if(result.equals("血压")){
                    return one.get("code")+"";
                }
            }
        }
        return "";
    }
}

+ 96 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/DiseaseAndAgeConvert.java

@ -0,0 +1,96 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.etl.convert.wlyy.model.HealthLable;
import com.yihu.wlyy.statistics.util.IdCardUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 疾病年龄
 * Created by chenweida on 2017/10/16.
 */
public class DiseaseAndAgeConvert implements Convert {
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        List<DataModel> returnList = new ArrayList<>();
        Map<String, List<String>> healthLablesMap = new HashMap<>();//key是患者code value是患者标签 1高血压 2糖尿病 3 65岁以上
        //初始化标签Map
        initHealthLabesMap(jdbcTemplate, healthLablesMap, oneList);
        //把标签Map设置到对应的维度里面
        for (DataModel dataModel : oneList) {
            List<String> labels = healthLablesMap.get(dataModel.getPatient());
            if (labels != null && labels.size() > 0) {
                labels.stream().forEach(str -> {
                    try {
                        DataModel dataModelTemp = new DataModel();
                        BeanUtils.copyProperties(dataModel, dataModelTemp);
                        DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(dataModelTemp, str);
                        returnList.add(dataModelTemp);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
            //判断患者是否是老年人是
            int age = IdCardUtil.getAgeForIdcard(dataModel.getIdcard());
            if (age >= 65) {
                try {
                    DataModel dataModelTemp = new DataModel();
                    BeanUtils.copyProperties(dataModel, dataModelTemp);
                    DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(dataModelTemp, "3");
                    returnList.add(dataModelTemp);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return returnList;
    }
    private void initHealthLabesMap(JdbcTemplate jdbcTemplate, Map<String, List<String>> healthLablesMap, List<DataModel> oneList) {
        //得到患者的疾病标签(只要高血压 糖尿病)
        String sql = "SELECT " +
                "  spli.label AS health_lable, " +
                "  spli.patient " +
                "FROM " +
                "  wlyy_sign_patient_label_info spli " +
                "WHERE " +
                "  spli.label_type = '3' " +
                "AND spli. STATUS = '1' " +
                "AND ( spli.label =1 or spli.label =2 )";
        List<HealthLable> healthLables = jdbcTemplate.query(sql, new BeanPropertyRowMapper(HealthLable.class));
        healthLables.stream().forEach(one -> {
            List<String> labels = healthLablesMap.get(one.getPatient());
            if (labels == null) {
                labels = new ArrayList<String>();
            }
            labels.add(one.getHealthLable());
            healthLablesMap.put(one.getPatient(), labels);
        });
//        //-得到患者的老年人标签
//        oneList.stream().forEach(one->{
//            int age = IdCardUtil.getAgeForIdcard(one.getIdcard());
//            if(age>=65){
//                num++;
//                List<String> labels = healthLablesMap.get(one.getPatient());
//                if (labels == null) {
//                    labels = new ArrayList<String>();
//                }
//                labels.add("3");
//                healthLablesMap.put(one.getPatient(), labels);
//            }
//        });
//        System.out.println("num:" + num);
    }
}

+ 75 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/DiseaseLableConvert.java

@ -0,0 +1,75 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.etl.convert.wlyy.model.HealthLable;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2017/6/5.
 */
public class DiseaseLableConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        List<DataModel> returnList = new ArrayList<>();
        //得到全部的服务标签
        String sql = "SELECT " +
                "  spli.label AS health_lable, " +
                "  spli.patient " +
                "FROM " +
                "  wlyy_sign_patient_label_info spli " +
                "WHERE " +
                "  spli.label_type = '3' " +
                "AND spli. STATUS = '1'";
        List<HealthLable> healthLables = jdbcTemplate.query(sql, new BeanPropertyRowMapper(HealthLable.class));
        //疾病标签可能有多个 转mapkey是患者code value是服务标签的code
        Map<String, List<String>> healthLablesMap = new HashMap<>();
        healthLables.stream().forEach(one -> {
            List<String> value = new ArrayList<String>();
            if (healthLablesMap.containsKey(one.getPatient())) {
                value = healthLablesMap.get(one.getPatient());
            }
            value.add(one.getHealthLable());
            healthLablesMap.put(one.getPatient(),value);
        });
        //给DataModel复制维度数据
        oneList.stream().forEach(one -> {
            try {
                //如果没有标签信息默认在未分组
                List<String> value = healthLablesMap.get(one.getPatient());
                if(value==null||value.size()==0){
                    value=new ArrayList<String>();
                    value.add("0");
                }
                //如果
                for (int i = 0; i < value.size(); i++) {
                    DataModel dataModelTemp = new DataModel();
                    BeanUtils.copyProperties(one, dataModelTemp);
                    DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(dataModelTemp, value.get(i));
                    returnList.add(dataModelTemp);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return returnList;
    }
}

+ 32 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/DispensaryTypeConvert.java

@ -0,0 +1,32 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
 * 长处方疾病
 * Created by zhangdan on 2017/10/26.
 */
public class DispensaryTypeConvert implements Convert {
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp ) {
        for(DataModel one:oneList){
            try {
//                Object value = DataModel.class.getMethod("get" + temp.getKey()).invoke(one);
                //获取到prescriptionCode,求长处方配送方式
                String sql ="SELECT dispensary_type FROM wlyy_prescription WHERE code=?";
                Object[] args = {one.getPrescriptionCode()};
                String type=jdbcTemplate.queryForObject(sql,args,String.class);
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, type);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        return oneList;
    }
}

+ 30 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/HealthGuidanceConvert.java

@ -0,0 +1,30 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
 * 健康指导推送统计已读、未读
 * Created by zhangdan on 2017/10/26.
 */
public class HealthGuidanceConvert implements Convert {
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        for (DataModel one : oneList) {
            try {
                Object value = DataModel.class.getMethod("get" + temp.getKey()).invoke(one);
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, value.toString());
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        ;
        return oneList;
    }
}

+ 59 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/HealthLableConvert.java

@ -0,0 +1,59 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.etl.convert.wlyy.model.HealthLable;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2017/6/5.
 */
public class HealthLableConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        //得到全部的服务标签
        String sql = "SELECT " +
                "  spli.label AS health_lable, " +
                "  spli.patient " +
                "FROM " +
                "  wlyy_sign_patient_label_info spli " +
                "WHERE " +
                "  spli.label_type = '2' " +
                "AND spli. STATUS = '1'";
        List<HealthLable> healthLables = jdbcTemplate.query(sql, new BeanPropertyRowMapper(HealthLable.class));
        Map<String, String> healthLablesMap = new HashMap<>();
        healthLables.stream().forEach(one -> {
            healthLablesMap.put(one.getPatient(), one.getHealthLable());
        });
        oneList.stream().forEach(one -> {
            try {
                Object value = healthLablesMap.get(one.getPatient());
                if (StringUtils.isEmpty(value)) {
                    value="0";
                }
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return oneList;
    }
}

+ 66 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/PrescriptionDiseaseConvert.java

@ -0,0 +1,66 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 长处方疾病
 * Created by zhangdan on 2017/10/26.
 */
public class PrescriptionDiseaseConvert implements Convert {
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        List<DataModel> returnList = new ArrayList<>();
        //初始化标签Map
        //把标签Map设置到对应的维度里面
        List<String> keyList = null;
        Map<String, List<String>> returnMap = initHealthLabesMap(jdbcTemplate, temp);
        for (DataModel one : oneList) {
            try {
                keyList = returnMap.get(one.getPrescriptionCode());
                if (keyList != null) {
                    for (int i = 0; i < keyList.size(); i++) {
                        String key = keyList.get(i);
                        DataModel dataModelTemp = new DataModel();
                        BeanUtils.copyProperties(one, dataModelTemp);
                        DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(dataModelTemp, key);
                        returnList.add(dataModelTemp);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnList;
    }
    private Map<String, List<String>> initHealthLabesMap(JdbcTemplate jdbcTemplate, DimensionQuotaDO temp) {
//        //得到长处方的疾病标签(只要高血压 糖尿病)
        Map<String, List<String>> returnMap = new HashMap<>();
        String sql = "SELECT " +
                "  health_problem AS healthProblem ," +
                "  prescription_code AS prescriptionCode " +
                "  FROM " +
                "  wlyy_prescription_diagnosis ";
        List<Map<String, Object>> diagnosisList = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : diagnosisList) {
            String key = (String) map.get("prescriptionCode");
            List<String> healthProblemList = returnMap.get(key);
            if (healthProblemList == null) {
                healthProblemList = new ArrayList<>();
            }
            healthProblemList.add((String) map.get("healthProblem"));
            returnMap.put(key, healthProblemList);
        }
        return returnMap;
    }
}

+ 69 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/PrescriptionPatientDiseaseConvert.java

@ -0,0 +1,69 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 长处方疾病
 * Created by zhangdan on 2017/10/26.
 */
public class PrescriptionPatientDiseaseConvert implements Convert {
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        List<DataModel> returnList = new ArrayList<>();
        //初始化标签Map
        //把标签Map设置到对应的维度里面
        List<String> keyList = null;
        Map<String,List<String>>  returnMap=initHealthLabesMap(jdbcTemplate, temp);
        for (DataModel one : oneList) {
            try {
                keyList = returnMap.get(one.getPatient());
                if (keyList != null) {
                    for (int i=0;i<keyList.size();i++) {
                        String key =keyList.get(i);
                        DataModel dataModelTemp = new DataModel();
                        BeanUtils.copyProperties(one, dataModelTemp);
                        DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(dataModelTemp, key);
                        returnList.add(dataModelTemp);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnList;
    }
    private  Map<String,List<String>> initHealthLabesMap(JdbcTemplate jdbcTemplate, DimensionQuotaDO temp) {
//        //得到长处方的疾病标签(只要高血压 糖尿病)
        Map<String,List<String>>  returnMap = new HashMap<>();
        String sql = "SELECT distinct " +
                "  d.health_problem AS healthProblem, " +
                "  p.patient AS patient " +
                "FROM " +
                "  wlyy_prescription p, " +
                "  wlyy_prescription_diagnosis d " +
                "WHERE " +
                "p.`code`=d.prescription_code AND p.status=100 ";
        List<Map<String, Object>> diagnosisList = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : diagnosisList) {
            String key= (String) map.get("patient");
            List<String> healthProblemList=returnMap.get(key);
            if(healthProblemList==null){
                healthProblemList=new ArrayList<>();
            }
            healthProblemList.add((String) map.get("healthProblem"));
            returnMap.put(key,healthProblemList);
        }
        return returnMap;
    }
}

+ 52 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/PrescriptionStatusConvert.java

@ -0,0 +1,52 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.util.Contant;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
 * 长处方疾病
 * Created by zhangdan on 2017/10/26.
 */
public class PrescriptionStatusConvert implements Convert {
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        for (DataModel one : oneList) {
            try {
//                Object value = DataModel.class.getMethod("get" + temp.getKey()).invoke(one);
                //获取到prescriptionCode,求长处方状态
                String sql = "SELECT status FROM wlyy_prescription WHERE code=?";
                //int status = jdbcTemplate.queryForObject(sql, Integer.class);
                Object[] args = {one.getPrescriptionCode()};
                int status = jdbcTemplate.queryForObject(sql, args, Integer.class);
                String key = getStatusCode(status);
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, key);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        ;
        return oneList;
    }
    public String getStatusCode(Integer status) {
        if (status >= 0 && status < 100) {
            return Contant.prescriptionStatus.status_1;
        } else if (status==100) {
            return Contant.prescriptionStatus.status_2;
        } else if (status==-2) {
            return Contant.prescriptionStatus.status_3;
        } else if (status==-1) {
            return Contant.prescriptionStatus.status_4;
        } else if (status==-3||status==-4||status==-5) {
            return Contant.prescriptionStatus.status_5;
        } else{
            return "0";
        }
    }
}

+ 55 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/ServerTypeConvert.java

@ -0,0 +1,55 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.etl.convert.wlyy.model.ServerType;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2017/10/18.
 * 服务类型转换器
 */
public class ServerTypeConvert implements Convert {
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        List<DataModel> returnList = new ArrayList<>();
        //得到患者的服务类型
        String sql = "select w.sign_code signCode,w.server_type serverType from wlyy_sign_family_server w ";
        List<ServerType> serverTypes = jdbcTemplate.query(sql, new BeanPropertyRowMapper(ServerType.class));
        //List转Map LIST key签约表code value是服务类型
        Map<String, List<String>> serverTypesMap = new HashMap<>();
        serverTypes.forEach(one -> {
            List<String> serverTypeList = serverTypesMap.get(one.getSignCode());
            if (serverTypeList == null) {
                serverTypeList = new ArrayList<String>();
            }
            serverTypeList.add(one.getServerType());
            serverTypesMap.put(one.getSignCode(),serverTypeList);
        });
        oneList.stream().forEach(one -> {
            List<String> serverTypeList = serverTypesMap.get(one.getBusinessId());
            if (serverTypeList != null) {
                for (int i = 0; i < serverTypeList.size(); i++) {
                    try {
                        DataModel dataModelTemp = new DataModel();
                        BeanUtils.copyProperties(one, dataModelTemp);
                        DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(dataModelTemp, serverTypeList.get(i));
                        returnList.add(dataModelTemp);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return returnList;
    }
}

+ 36 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/SexConvert.java

@ -0,0 +1,36 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.util.IdCardUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
 * Created by chenweida on 2017/6/5.
 */
public class SexConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel>  oneList, String slaveLevel, DimensionQuotaDO temp ) {
        oneList.stream().forEach(one -> {
            try {
                Object value = DataModel.class.getMethod("get" + temp.getKey()).invoke(one);
                String sex= IdCardUtil.getSexForIdcard(String.valueOf(value));
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, sex);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return oneList;
    }
}

+ 38 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/SimpleConvert.java

@ -0,0 +1,38 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import java.util.List;
/**
 * Created by chenweida on 2017/6/5.
 */
public class SimpleConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel>  oneList, String slaveLevel, DimensionQuotaDO temp ) {
        oneList.stream().forEach(one -> {
            try {
                Object value = DataModel.class.getMethod("get" + temp.getKey()).invoke(one);
                if(StringUtils.isEmpty(value)){
                    value="0";
                }
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, value.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return oneList;
    }
}

+ 66 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/UseDeviceTypeConvert.java

@ -0,0 +1,66 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.wlyy.statistics.etl.convert.Convert;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2017/6/5.
 */
public class UseDeviceTypeConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate,List<DataModel> oneList, String slaveLevel, DimensionQuotaDO temp) {
        String sql = temp.getDictSql();
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        Map<String,Object> map = null;
        for(DataModel one:oneList) {
            try {
                map = getTeam(one,jdbcTemplate);
                if(map!=null){
                    one.setTeam(map.get("grant_admin_team")+"");
                    one.setServerType(map.get("device_name")+"");
                }else{
                    continue;
                }
                String key = getKey(one,result);
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, key);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        return oneList;
    }
    public String getKey(DataModel dataModel,List<Map<String,Object>> dict){
        for (Map<String,Object> one : dict){
            String result = one.get("name")+"";
            if(dataModel.getServerType().equals(result)||dataModel.getServerType().indexOf(result)>0){
                return one.get("code")+"";
            }
        }
        return "";
    }
    public Map<String,Object> getTeam(DataModel dataModel,JdbcTemplate jdbcTemplate){
        String deviceCode =  dataModel.getHealthProblem();
        String sql = " select * from device.wlyy_devices d where d.device_code ='"+deviceCode+"' and is_grant=1";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        if(result.size()>0){
            return result.get(0);
        }
        return null;
    }
}

+ 33 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/model/HealthLable.java

@ -0,0 +1,33 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy.model;
/**
 * Created by chenweida on 2017/7/1.
 * SELECT
 * spli.label AS health_lable,
 * spli.patient
 * FROM
 * wlyy_sign_patient_label_info spli
 * WHERE
 * spli.label_type = '2'
 * AND spli. STATUS = '1'
 */
public class HealthLable {
    private String healthLable;
    private String patient;
    public String getHealthLable() {
        return healthLable;
    }
    public void setHealthLable(String healthLable) {
        this.healthLable = healthLable;
    }
    public String getPatient() {
        return patient;
    }
    public void setPatient(String patient) {
        this.patient = patient;
    }
}

+ 26 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/model/PrescriptionDisease.java

@ -0,0 +1,26 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy.model;
/**
 * Created by zhangdan on 2017/10/26.
 */
public class PrescriptionDisease {
    private String healthProblem;
    private String prescriptionCode;
    public String getHealthProblem() {
        return healthProblem;
    }
    public void setHealthProblem(String healthProblem) {
        this.healthProblem = healthProblem;
    }
    public String getPrescriptionCode() {
        return prescriptionCode;
    }
    public void setPrescriptionCode(String prescriptionCode) {
        this.prescriptionCode = prescriptionCode;
    }
}

+ 25 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/convert/wlyy/model/ServerType.java

@ -0,0 +1,25 @@
package com.yihu.wlyy.statistics.etl.convert.wlyy.model;
/**
 * Created by chenweida on 2017/10/18.
 */
public class ServerType {
    private String signCode;
    private String serverType;
    public String getSignCode() {
        return signCode;
    }
    public void setSignCode(String signCode) {
        this.signCode = signCode;
    }
    public String getServerType() {
        return serverType;
    }
    public void setServerType(String serverType) {
        this.serverType = serverType;
    }
}

+ 90 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/ExtractHelper.java

@ -0,0 +1,90 @@
package com.yihu.wlyy.statistics.etl.extract;
import com.yihu.jw.entity.base.statistics.JobConfigDO;
import com.yihu.wlyy.statistics.etl.extract.db.DBExtract;
import com.yihu.wlyy.statistics.etl.extract.db.ESExtract;
import com.yihu.wlyy.statistics.util.SpringUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
/**
 * Created by chenweida on 2017/6/1.
 */
@Component
@Scope("prototype")
public class ExtractHelper {
    private String startTime;
    private String endTime;
    private Logger logger = LoggerFactory.getLogger(ExtractHelper.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value("${wlyy.im.databaseName}")
    private String imDatabaseName;
    /**
     * 公共的抽取数据
     *
     * @param wlyyJobConfigVO
     * @return
     * @throws Exception
     */
    public List<DataModel> extractData(JobConfigDO wlyyJobConfigVO, String startTime, String endTime, String year, String timeLevel) throws Exception {
        try {
            this.startTime = startTime;
            this.endTime = endTime;
            String timeSql = "";
            if ("1".equals(timeLevel)) {
                timeSql = wlyyJobConfigVO.getSqlDay();
            } else if ("2".equals(timeLevel)) {
                timeSql = wlyyJobConfigVO.getSqlYear();
            } else {
                timeSql = wlyyJobConfigVO.getSqlDay();
            }
            //如果为null 初始化
            if(StringUtils.isEmpty(timeSql)){
                timeSql="";
            }
            //设置时间
            String sql = initSql(wlyyJobConfigVO.getSql() + "  " + timeSql, startTime, endTime, year);
            String sqlCount = initSql(wlyyJobConfigVO.getSqlCount() + "  " + timeSql, startTime, endTime, year);
            logger.info(" sql: " + sql);
            logger.info(" sqlCount: " + sqlCount);
            //如果是数据库从数据库抽取
            if (StringUtils.isEmpty(wlyyJobConfigVO.getExtractType())||"1".equals(wlyyJobConfigVO.getExtractType())){
                //抽取数据库
                return SpringUtil.getBean(DBExtract.class).extractByPage(
                        DataModel.class,
                        sql,
                        sqlCount,
                        true,
                        jdbcTemplate);
            }else if("2".equals(wlyyJobConfigVO.getExtractType())){
                //抽取ES
               return SpringUtil.getBean(ESExtract.class).extract(wlyyJobConfigVO);
            }
        } catch (Exception e) {
            logger.error("extract error:" + e.getMessage());
            logger.error("quotaVO str:" + wlyyJobConfigVO.toString());
        }
        return null;
    }
    public String initSql(String sql, String startTime, String endTime, String year) {
        return sql.replace("[startTime]", startTime).replace("[endTime]", endTime).replace("[year]", year).replace("[im]",imDatabaseName);
    }
}

+ 66 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/db/DBExtract.java

@ -0,0 +1,66 @@
package com.yihu.wlyy.statistics.etl.extract.db;
import com.yihu.wlyy.statistics.util.SpringUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
/**
 * Created by Administrator on 2016.10.16.
 * 数据库抽取
 */
@Component
@Scope("prototype")
public class DBExtract<T> {
    Integer pageSize=100000;
    /**
     * 不分页抽取
     * @param clazz
     * @param sql
     * @return
     */
    public List<T> extract(Class<T> clazz,String sql,JdbcTemplate jdbcTemplate){
        List<T> returnList= jdbcTemplate.query(sql,new BeanPropertyRowMapper(clazz));
        return returnList;
    }
    /**
     * 分页抽取
     * @param clazz
     * @param sql
     * @param pageSize 每页显示多少 默认10000
     * @param isMultithreading 是否多线程抽取 默认否
     * @return
     */
    public List<T> extractByPage(Class<T> clazz,String sql,String countSql,int pageSize,Boolean isMultithreading,JdbcTemplate jdbcTemplate)throws  Exception{
        return SpringUtil.getBean(DBPageExtract.class).extractByPage(clazz,sql,countSql,pageSize,isMultithreading,jdbcTemplate);
    }
    /**
     * 分页抽取
     * @param clazz
     * @param sql
     * @param isMultithreading 是否多线程抽取 默认否
     * @return
     */
    public List<T> extractByPage(Class<T> clazz,String sql,String countSql,Boolean isMultithreading,JdbcTemplate jdbcTemplate)throws  Exception{
        if(StringUtils.isEmpty(countSql)){
            return extract(clazz,sql,jdbcTemplate);
        }
        return SpringUtil.getBean(DBPageExtract.class).extractByPage(clazz,sql,countSql,pageSize,isMultithreading,jdbcTemplate);
    }
    /**
     * 分页抽取
     * @param clazz
     * @param sql
     * @return
     */
    public List<Object> extractByPage(Class<T> clazz,String sql,JdbcTemplate jdbcTemplate)throws  Exception{
        Boolean isMultithreading=false;
        String countSql="";
        return SpringUtil.getBean(DBPageExtract.class).extractByPage(clazz,sql,countSql,pageSize,isMultithreading,jdbcTemplate);
    }
}

+ 156 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/db/DBPageExtract.java

@ -0,0 +1,156 @@
package com.yihu.wlyy.statistics.etl.extract.db;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
 * Created by Administrator on 2016.10.16.
 * 分页抽取器
 */
@Component
@Scope("prototype")
public class DBPageExtract<T> {
    private  List<T> returnList=new ArrayList<T>();
    public synchronized void addData( List<T> dataList) {
         returnList.addAll(dataList);
    }
    public List<T> extractByPage(Class<T> clazz, String sql, String countSql, int pageSize, Boolean isMultithreading,JdbcTemplate jdbcTemplate) throws Exception{
        if(!StringUtils.isEmpty(countSql)){
            isMultithreading=true;
        }
        if(isMultithreading){
            if(StringUtils.isEmpty(countSql)){
                throw new Exception("countSql is null");
            }
            return MultiThreadExtract(clazz, sql,countSql, pageSize,jdbcTemplate);
        }else{
            return noMultiThreadExtract(clazz, sql, pageSize,jdbcTemplate);
        }
    }
    /**
     * 不用多线程抽取
     * @param clazz
     * @param sql
     * @param pageSize
     * @return
     */
    private List<T> noMultiThreadExtract(Class<T> clazz, String sql, int pageSize,JdbcTemplate jdbcTemplate) {
        List<T> returnList=new ArrayList<T>();
        int start=0;
        int page=1;
        while (true){
            String finalSql=sql+" limit "+start+","+pageSize; //拼凑分页的语句
            List<T> listTemp= jdbcTemplate.query(finalSql,new BeanPropertyRowMapper(clazz));
            returnList.addAll(listTemp);//添加到list里面
            //判断是都是最后页面
            if(listTemp.size()!=pageSize){
                listTemp.clear();
                break;
            }else{
                start =page*pageSize;
                page++;
                listTemp.clear();
            }
        }
        return returnList;
    }
    /**
     * 多线程抽取数据
     * @param clazz
     * @param sql
     * @param pageSize
     * @return
     */
    private   List<T> MultiThreadExtract(Class<T> clazz, String sql,String countSql, int pageSize,JdbcTemplate jdbcTemplate)  {
        try{
            //得到数据的总数
            Integer dataCount=getCount(countSql,jdbcTemplate);
            //根据count 计算出 总共要执行几次
            Integer forCount=getForCount(dataCount,pageSize);
            forCount++;
            //綫程返回值的值
            List<AsyncResult<Boolean>> asyncResultPool=new ArrayList<AsyncResult<Boolean>>();
            for(int page=0;page<forCount;page++){
                //启动多线程采集数据
                AsyncResult<Boolean> future= multiExtractData(sql,page*pageSize,pageSize,clazz,jdbcTemplate);
                asyncResultPool.add(future);
            }
            ///循环判断,等待获取结果信息
            while (true){
                //得到迭代器
                Iterator<AsyncResult<Boolean>> asyncResultIterator=asyncResultPool.iterator();
                //判断有没有下一个
                while (asyncResultIterator.hasNext()){
                    AsyncResult<Boolean> asyncResult= asyncResultIterator.next();
                    if(asyncResult.isDone()){
                        //如果做完了就移除迭代器
                        asyncResultIterator.remove();
                    }else{
                        Thread.sleep(500L);
                    }
                }
                //如果一直移除到没有下一个就跳出循环 说明数据已经采集完成
                if(!asyncResultIterator.hasNext()){
                    break;
                }
            }
            return returnList;
        }catch (Exception e){
            e.printStackTrace();
            //如果多线程错误 就改用单线程
            return noMultiThreadExtract(clazz,sql,pageSize,jdbcTemplate);
        }
    }
    private Integer getForCount(Integer dataCount, int pageSize) {
        //根据所有的数据取余数
        int yushu=dataCount%pageSize;
        int page=dataCount/pageSize;
        if(yushu==0){
            //如果没有余数 返回总的页数
            return page;
        }else{
            //如果有余数 页数多加1
            page++;
            return page;
        }
    }
    private Integer getCount(String countSql,JdbcTemplate jdbcTemplate) {
        Integer countMap= jdbcTemplate.queryForObject(countSql,Integer.class);
        return countMap;
    }
    /**
     * 多线程采集数据
     * @param sql
     * @param start
     * @param pageSize
     * @param clazz
     * @return
     */
    @Async("dbExtractExecutor")
    private AsyncResult<Boolean> multiExtractData(String sql, int start, int pageSize, Class<T> clazz, JdbcTemplate jdbcTemplate) {
        String finalSql=sql+" limit "+start+","+pageSize; //拼凑分页的语句
        List<T> listTemp= jdbcTemplate.query(finalSql,new BeanPropertyRowMapper(clazz));
        addData(listTemp);
        return new AsyncResult<>(true);
    }
}

+ 162 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/db/Data2Save.java

@ -0,0 +1,162 @@
package com.yihu.wlyy.statistics.etl.extract.db;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.jw.entity.base.statistics.JobConfigDO;
import com.yihu.jw.entity.base.team.BaseTeamDO;
import com.yihu.wlyy.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.wlyy.statistics.vo.DataModel;
import com.yihu.wlyy.statistics.vo.DictModel;
import com.yihu.wlyy.statistics.vo.SaveModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.*;
/**
 * Created by chenweida on 2017/12/28.
 */
@Component
public class Data2Save {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private WlyyDimensionQuotaDao dimensionQuotaDao;
    public List<SaveModel> data2save(List<DataModel> dataModels, JobConfigDO quartzJobConfig, Date quotaDate, String timeLevel) throws Exception{
        //得到全部的指标
        List<DimensionQuotaDO> dimensionQuotas = dimensionQuotaDao.findDimensionQuotasByQuotaCode(quartzJobConfig.getQuotaId());
        //初始化维度的数据
        Map<String, DataModel> dataModelMap = new HashMap<>();
        if (dataModels != null) {
            for (DataModel dataModel : dataModels) {
                StringBuilder sb = new StringBuilder();
                sb.append(dataModel.getTeam());
                for(int i = 0; i < dimensionQuotas.size(); i++){
                    int slaveKeyNum = i+1;
                    String invokeKey = (String) DataModel.class.getMethod("getSlaveKey"+slaveKeyNum).invoke(dataModel);
                    sb.append("-"+invokeKey);
                }
                if(dataModelMap.get(dataModel.getTeam())==null) {
                    dataModelMap.put(sb.toString(), dataModel);
                }else{
                    double result1 = dataModelMap.get(dataModel.getTeam()).getResult1();
                    dataModel.setResult1(dataModel.getResult1()+result1);
                    dataModelMap.put(sb.toString(), dataModel);
                }
            }
        }
        //得到全部团队
        List<BaseTeamDO> teams = findAllTeam();
        List<SaveModel> savelist = new ArrayList<>();
        //没维度
        for (BaseTeamDO adminTeam : teams) {
            if(adminTeam.getOrgId().length() != 10){
                continue;
            }
            SaveModel saveModel = new SaveModel();
            saveModel.setCity("350200");
            saveModel.setCityName("厦门市");
//            saveModel.setTown(adminTeam.getTownCode());
//            saveModel.setTownName(adminTeam.getTownName());
//            saveModel.setHospital(adminTeam.getOrgCode());
//            saveModel.setHospitalName(adminTeam.getOrgName());
            saveModel.setTeam(adminTeam.getId().toString());
            saveModel.setTeamName(adminTeam.getName());
            saveModel.setQuotaCode(quartzJobConfig.getQuotaId());
            saveModel.setAreaLevel(SaveModel.teamLevel);
            saveModel.setTimeLevel(timeLevel);
            saveModel.setCreateTime(new Date());
            saveModel.setQuotaDate(quotaDate);
            saveModel.setResult1(0.0);
            saveModel.setResult2(0.0);
            //设置没有维度的数据
            if (dimensionQuotas==null || dimensionQuotas.size()==0){
                DataModel dataModel = dataModelMap.get(adminTeam.getId().toString());
                if (dataModel!=null){
                    saveModel.setResult1(dataModel.getResult1());
                    saveModel.setResult2(dataModel.getResult1());
                }
            }
            savelist.add(saveModel);
        }
        //如果有维度的数据就设置维度的数据
        if (dimensionQuotas!=null && dimensionQuotas.size()>0){
            //如果有维度就设置维度的数据
            for (int i = 0; i < dimensionQuotas.size(); i++) {
                String dictSql = dimensionQuotas.get(i).getDictSql();
                List<DictModel> dictModels = jdbcTemplate.query(dictSql, new BeanPropertyRowMapper(DictModel.class));
                int savleKeyNum = i + 1;
                savelist = setSlaveKey(savleKeyNum, savelist, dictModels,dataModelMap,dimensionQuotas.size());
            }
        }
        return savelist;
    }
    private List<SaveModel> setSlaveKey(int savleKeyNum, List<SaveModel> savelist, List<DictModel> dictModels,Map<String, DataModel> dataModelMap,int dimensize) {
        List<SaveModel> newSavelist = new ArrayList<>();
        for(SaveModel one:savelist) {
            for(DictModel dict:dictModels){
            SaveModel newSaveModel = new SaveModel();
            BeanUtils.copyProperties(one, newSaveModel);
            try {
                SaveModel.class.getMethod("setSlaveKey" + savleKeyNum, String.class).invoke(newSaveModel, dict.getCode());
                SaveModel.class.getMethod("setSlaveKey" + savleKeyNum + "Name", String.class).invoke(newSaveModel, dict.getName());
                if (savleKeyNum == dimensize) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(newSaveModel.getTeam());
                    for (int i = 1; i <= dimensize; i++) {
                        String invokeKey = (String) SaveModel.class.getMethod("getSlaveKey" + i).invoke(newSaveModel);
                        sb.append("-" + invokeKey);
                    }
                    DataModel dataModel = dataModelMap.get(sb.toString());
                    if (dataModel != null) {
                        newSaveModel.setResult1(dataModel.getResult1());
                        newSaveModel.setResult2(dataModel.getResult1());
                    }
                }
                newSavelist.add(newSaveModel);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        }
        return newSavelist;
    }
    /**
     * 得到全部团队
     *
     * @return
     */
    private List<BaseTeamDO> findAllTeam() {
        //  Map<String, AdminTeam> adminTeamMap = new HashMap<>();
        String sql = "SELECT " +
                "  wat.id id, " +
                "  wat.name name, " +
                "  h.`code` org_code, " +
                "  h.`name` org_name, " +
                "  h.town   town_code, " +
                "  h.town_name  town_name " +
                "FROM " +
                "  dm_hospital h, " +
                "  wlyy_admin_team wat " +
                "WHERE " +
                "  h.`code` = wat.org_code " +
                " AND length (h.`code`) = 10 "+
                "AND wat.available = 1";
        List<BaseTeamDO> adminTeams = jdbcTemplate.query(sql, new BeanPropertyRowMapper(BaseTeamDO.class));
//        for (AdminTeam adminTeam : adminTeams) {
//            adminTeamMap.put(adminTeam.getId().toString(), adminTeam);
//        }
        return adminTeams;
    }
}

+ 69 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/extract/db/ESExtract.java

@ -0,0 +1,69 @@
package com.yihu.wlyy.statistics.etl.extract.db;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.jw.entity.base.statistics.JobConfigDO;
import com.yihu.wlyy.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.wlyy.statistics.util.ElasticsearchUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
/**
 * Created by Administrator on 2016.10.16.
 * ES抽取
 */
@Component
@Scope("prototype")
public class ESExtract<T> {
    @Autowired
    private ElasticsearchUtil elasticsearchUtil;
    @Value("${es.type}")
    private String esType;
    @Value("${es.index}")
    private String esIndex;
    @Autowired
    private WlyyDimensionQuotaDao dimensionQuotaDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * ES抽取
     *
     * @param wlyyJobConfigVO
     * @return
     */
    public List<DataModel> extract(JobConfigDO wlyyJobConfigVO) {
        //扩展维度
        String sql= initSql(wlyyJobConfigVO.getSql(),wlyyJobConfigVO.getStartTime(),wlyyJobConfigVO.getEndTime());
        String re = "";
        //得到该指标的维度
        List<DimensionQuotaDO> dimensionQuotas = dimensionQuotaDao.findDimensionQuotasByQuotaCode(wlyyJobConfigVO.getQuotaId());
        if (dimensionQuotas != null && dimensionQuotas.size() > 0) {
            for(int i=1;i<=dimensionQuotas.size();i++){
                sql+=",slaveKey"+i;
                re+=","+dimensionQuotas.get(i-1).getKey()+" AS slaveKey"+i;
//                dictSql = one.getDictSql();
//                List<Map<String, Object>> temp = jdbcTemplate.queryForList(dictSql);
            }
        }
        sql = sql.replace("[re]", re);
//        sql="SELECT adminTeamCode AS team ,COUNT(*) AS result1  ,isRead AS slaveKey1   FROM health_edu_article_patient_test3 where userType=2 AND createTime < '2018-01-04T17:00:00+0800'  AND createTime >= '2018-01-03T17:00:00+0800' group by team,slaveKey1";
        List<DataModel> dataModels = elasticsearchUtil.excute(sql,DataModel.class);
        return dataModels;
    }
    //初始化时间
    public String initSql(String sql, String startTime, String endTime) {
        return sql.replace("[startTime]", startTime).replace("[endTime]", endTime);
    }
}

+ 47 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/filter/FilterHelper.java

@ -0,0 +1,47 @@
package com.yihu.wlyy.statistics.etl.filter;
import com.yihu.wlyy.statistics.vo.DataModel;
import com.yihu.wlyy.statistics.vo.ErrModel;
import com.yihu.wlyy.statistics.vo.FilterModel;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by chenweida on 2017/6/1.
 */
@Component
@Scope("prototype")
public class FilterHelper {
    public FilterHelper() {
    }
    /**
     * 根据过滤规则过滤数据
     *
     * @param dataModels
     * @return
     */
    public FilterModel filter(List<DataModel> dataModels) {
        //成功的list
        List<DataModel> newList = new ArrayList<>();
        //失败的list
        List<ErrModel> errorList = new ArrayList<>();
        dataModels.stream().forEach(one -> {
            String returnStr=one.isNull();
            if (StringUtils.isEmpty(returnStr)) {
                newList.add(one);
            }else{
                //脏数据
                errorList.add(new ErrModel(one.getBusinessId(),returnStr));
            }
        });
        return new FilterModel(newList, errorList);
    }
}

+ 34 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/filter/filter/NotNullFilter.java

@ -0,0 +1,34 @@
package com.yihu.wlyy.statistics.etl.filter.filter;
import com.yihu.wlyy.statistics.vo.FilterModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
 * Created by chenweida on 2017/6/1.
 */
@Component
@Scope("prototype")
public class NotNullFilter {
    private Logger logger = LoggerFactory.getLogger(NotNullFilter.class);
    /**
     * 不判断主维度的数据是否为空,只判断从维度额的数据是否为空
     *
     * @param filterModel
     * @return
     */
    public FilterModel filter(FilterModel filterModel) {
        try {
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return null;
    }
}

+ 29 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/save/SaveHelper.java

@ -0,0 +1,29 @@
package com.yihu.wlyy.statistics.etl.save;
import com.yihu.wlyy.statistics.etl.save.es.ElastricSearchSave;
import com.yihu.wlyy.statistics.util.SpringUtil;
import com.yihu.wlyy.statistics.vo.SaveModel;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.List;
/**
 * Created by chenweida on 2017/6/2.
 */
@Component
@Scope("prototype")
public class SaveHelper {
    public Boolean save(List<SaveModel> sms) {
        return SpringUtil.getBean(ElastricSearchSave.class).save(sms);
    }
    public Boolean update(List<SaveModel> sms) {
        return SpringUtil.getBean(ElastricSearchSave.class).update(sms);
    }
}

+ 102 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/save/es/ElasticFactory.java

@ -0,0 +1,102 @@
package com.yihu.wlyy.statistics.etl.save.es;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
 * Created by chenweida on 2017/6/5.
 */
@Component
public class ElasticFactory {
    private static JestClientFactory factory = null;
    @Value("${es.host}")
    private String esHost;//http://59.61.92.90:9065,http://59.61.92.90:9067
    @Value("${es.tHost}")
    private String tHost;// 59.61.92.90:9066,59.61.92.90:9068
    @Value("${es.clusterName}")
    private String clusterName;
//-----------------------------------jestClient----------------------------------------
    /**
     * @param "http://localhost:9200"
     * @return
     */
    public JestClient getJestClient() {
        if (factory == null) {
            //初始化链接
            init();
        }
        return factory.getObject();
    }
    /**
     * 初始化链接
     * 9200
     */
    public synchronized void init() {
        String[] hostArray = esHost.split(",");
        // Construct a new Jest client according to configuration via factory
        factory = new JestClientFactory();
        HttpClientConfig httpClientConfig = new HttpClientConfig
                .Builder(Arrays.asList(hostArray))//http://59.61.92.90:9065,http://59.61.92.90:9067
                .multiThreaded(true)
                .maxTotalConnection(50)// 最大链接
                .maxConnectionIdleTime(120, TimeUnit.SECONDS)//链接等待时间
                .connTimeout(60 * 1000)
                // .discoveryEnabled(true)
                .readTimeout(60 * 1000)//60秒
                .build();
        factory.setHttpClientConfig(httpClientConfig);//得到链接
    }
    //-----------------------------------TransportClient----------------------------------------
    private TransportClient transportClient;
    public Client getTransportClient() {
        try {
            initTranClient();
            return transportClient;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 9300
     *
     * @throws UnknownHostException
     */
    private synchronized void initTranClient() throws UnknownHostException {
        if (transportClient == null) {
            String[] hosts = tHost.split(",");
            Settings settings = Settings.settingsBuilder()
                    // .put("client.transport.sniff", true)//开启嗅探功能
                    .put("cluster.name", StringUtils.isEmpty(clusterName) ? "jkzl" : clusterName)//默认集群名字是jkzl
                    .build();
            transportClient = TransportClient.builder().settings(settings).build();
            for (String oneHost : hosts) {
                String[] hostAndport = oneHost.split(":");
                transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndport[0]), Integer.valueOf(hostAndport[1])));
            }
        }
    }
}

+ 112 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/etl/save/es/ElastricSearchSave.java

@ -0,0 +1,112 @@
package com.yihu.wlyy.statistics.etl.save.es;
import com.alibaba.fastjson.JSONObject;
import com.yihu.wlyy.statistics.vo.SaveModel;
import io.searchbox.client.JestClient;
import io.searchbox.core.Bulk;
import io.searchbox.core.BulkResult;
import io.searchbox.core.Index;
import io.searchbox.core.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
;
/**
 * Created by chenweida on 2017/6/2.
 */
@Component
@Scope("prototype")
public class ElastricSearchSave {
    @Value("${es.type}")
    private String esType;
    @Value("${es.index}")
    private String esIndex;
    private Logger logger = LoggerFactory.getLogger(ElastricSearchSave.class);
    @Autowired
    private ElasticFactory elasticFactory;
    public Boolean save(List<SaveModel> sms) {
        JestClient jestClient = null;
        try {
            //得到链接
            jestClient = elasticFactory.getJestClient();
            int success = 0;
            int error = 0;
            Bulk.Builder bulk = new Bulk.Builder().defaultIndex(esIndex).defaultType(esType);
            for (SaveModel obj : sms) {
                try {
                    obj.setCreateTime(new Date());
                    Index index = new Index.Builder(obj).build();
                    success++;
                    bulk.addAction(index);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                    error++;
                }
            }
            BulkResult br = jestClient.execute(bulk.build());
            logger.info("save flag:" + br.isSucceeded());
            logger.info("save success:" + success);
            logger.info("save error:" + error);
            return br.isSucceeded();
        } catch (Exception e) {
            logger.error(" save error :" + e.getMessage());
        } finally {
            if (jestClient != null) {
                jestClient.shutdownClient();
            }
        }
        return null;
    }
    public Boolean update(List<SaveModel> sms) {
        JestClient jestClient = null;
        try {
            //得到链接
            jestClient = elasticFactory.getJestClient();
            int success = 0;
            int error = 0;
            boolean isSuccessed = true;
            Bulk.Builder bulk = new Bulk.Builder().defaultIndex(esIndex).defaultType(esType);
            for (SaveModel obj : sms) {
                try {
                    JSONObject jo = new JSONObject();
                    jo.put("doc", obj);
                    Update index = new Update.Builder(jo.toString()).index(esIndex).type(esType).id(obj.getId()).build();
                    bulk.addAction(index);
                    success++;
                } catch (Exception e) {
                    error++;
                    isSuccessed = false;
                }
            }
            BulkResult br = jestClient.execute(bulk.build());
            logger.info("update flag:" + br.isSucceeded());
            logger.info("update success:" + success);
            logger.info("update error:" + error);
            jestClient.shutdownClient();
            return isSuccessed;
        } catch (Exception e) {
            logger.error(" update error :" + e.getMessage());
        } finally {
            if (jestClient != null) {
                jestClient.shutdownClient();
            }
        }
        return null;
    }
}

+ 29 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/CacheCleanJob.java

@ -0,0 +1,29 @@
package com.yihu.wlyy.statistics.job.business;
import com.yihu.wlyy.statistics.etl.cache.Cache;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
 * Created by Administrator on 2016.10.19.
 */
@Component
@Scope("prototype")
public class CacheCleanJob implements Job {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    public static String jobKey="CLEAN_CACHE_JOB";
    public static String cron="0 55 23 * * ?";
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        logger.info("clean cache start");
        Cache.cleanCache();
        logger.info("clean cache end");
    }
}

+ 434 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/CurrentMysqlToEsQuotaJob.java

@ -0,0 +1,434 @@
package com.yihu.wlyy.statistics.job.business;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.jw.entity.base.statistics.JobConfigDO;
import com.yihu.jw.entity.base.statistics.JobLogDO;
import com.yihu.wlyy.statistics.dao.QuartzJobConfigDao;
import com.yihu.wlyy.statistics.dao.QuartzJobLogDao;
import com.yihu.wlyy.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.wlyy.statistics.etl.cache.Cache;
import com.yihu.wlyy.statistics.etl.compute.ComputeHelper;
import com.yihu.wlyy.statistics.etl.convert.ConvertHelper;
import com.yihu.wlyy.statistics.etl.extract.ExtractHelper;
import com.yihu.wlyy.statistics.etl.extract.db.Data2Save;
import com.yihu.wlyy.statistics.etl.filter.FilterHelper;
import com.yihu.wlyy.statistics.etl.save.SaveHelper;
import com.yihu.wlyy.statistics.etl.save.es.ElasticFactory;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.util.SpringUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import com.yihu.wlyy.statistics.vo.FilterModel;
import com.yihu.wlyy.statistics.vo.SaveModel;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import net.sf.json.JSONArray;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * Created by chenweida on 2017/7/10.  实时查询
 */
@Component
@Scope("prototype")
@DisallowConcurrentExecution//防止到了执行时间点前一任务还在执行中,但是这时有空闲的线程,那么马上又会执行,这样一来就会存在同一job被并行执行
public class CurrentMysqlToEsQuotaJob implements Job {
    private Logger logger = LoggerFactory.getLogger(MysqlToEsQuotaJob.class);
    private String endTime;//结束时间
    private String startTime;//开始时间
    private String year;//要统计的年份
    private Date quotaDate;//统计的时间
    private String timeLevel;//1 日 2年
    private String wlyyJobCongId;//指标对象
    private JobConfigDO quartzJobConfig;//指标对象
    @Autowired
    private QuartzJobLogDao quartzJobLogDao;//执行日志Dao
    @Autowired
    private WlyyDimensionQuotaDao dimensionQuotaDao;
    @Autowired
    private ElasticFactory elasticFactory;
    @Value("${es.type}")
    private String esType;
    @Value("${es.index}")
    private String esIndex;
    @Autowired
    private QuartzJobConfigDao quartzJobConfigDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private Data2Save data2Save;
    private String incrementInterval;//增量的时间间隔(天)
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            //springz注入
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            //初始化参数
            initParams(context);
            //统计指标
            computequota();
        } catch (Exception e) {
            //如果出錯立即重新執行
            JobExecutionException e2 = new JobExecutionException(e);
            e2.setRefireImmediately(true);
            e.printStackTrace();
        }
    }
    /**
     * 初始化参数
     *
     * @param context
     */
    private void initParams(JobExecutionContext context) throws Exception {
        JobDataMap map = context.getJobDetail().getJobDataMap();
        this.timeLevel = map.getString("timeLevel");
        //为空默认是统计昨天的数据  统计昨天的数据是从 前天的下午17:00:00 到昨天的下午17:00:00
        //初始化结束时间
        this.endTime = DateUtil.getStringDate("yyyy-MM-dd HH:mm:ss");
        this.startTime = map.getString("startTime");
        this.incrementInterval = map.get("incrementInterval")!=null?String.valueOf(map.get("incrementInterval")):"1";
        //初始化统计年份
        this.year = getNowYearByDate();
        //初始化开始时间
        if ("2".equals(timeLevel)) {
            //按年度到达量
            startTime = this.year + "-06-30 17:00:00";
        } else {
//            //增量
//            this.startTime = new LocalDate(new DateTime().minusDays(1)).toString("yyyy-MM-dd") + " 17:00:00"; //2017-06-01 17:00:00
            //增量
            if (StringUtils.isEmpty(startTime)) {
//                startTime = new LocalDate(new DateTime().minusDays(2)).toString("yyyy-MM-dd") + " 17:00:00"; //2017-06-01 17:00:00
                getStartTime();
            } else {
                startTime = startTime + " 17:00:00";
            }
        }
        this.quotaDate = DateUtil.strToDate(endTime, "yyyy-MM-dd");
        this.wlyyJobCongId = map.getString("jobConfig");
        this.quartzJobConfig = quartzJobConfigDao.findById(wlyyJobCongId);
    }
    /**
     * 计算指标
     */
    private void computequota() {
        //清空缓存
        Cache.cleanCache();
        List<JobConfigDO> list = quartzJobConfigDao.findByIds();
        list.stream().forEach(one -> {
            try {
                if (StringUtils.isEmpty(one.getExtractType()) || "1".equals(one.getExtractType())){
                    mysql(one);
                }else if("2".equals(one.getExtractType())){
                    es(one);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        //更新统计时间
        redisTemplate.opsForValue().set("quota:date", DateUtil.dateToStrLong(new Date()));
    }
    private void es(JobConfigDO one) throws Exception{
        this.endTime = DateUtil.getStringDate("yyyy-MM-dd'T'HH:mm:ssZZZ");
        //初始化统计年份
        this.year = getNowYearByDate();
        //初始化开始时间
        if ("2".equals(timeLevel)) {
            //按年度到达量
            startTime = this.year + "-06-30T17:00:00+0800";
        } else {
            //增量
            this.startTime = new LocalDate(new DateTime().minusDays(1)).toString("yyyy-MM-dd") + "T17:00:00+0800"; //2017-06-01 17:00:00
        }
        this.quotaDate = DateUtil.strToDate(endTime, "yyyy-MM-dd");
        one.setStartTime(startTime);
        one.setEndTime(endTime);
        logger.info("========================quotaCode:" + one.getQuotaId() + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + ",timeLevel:" + timeLevel + " start========================");
        JobLogDO tjQuotaLog = new JobLogDO();
        tjQuotaLog.setJobId( one.getQuotaId());
        tjQuotaLog.setJobStartTime(new Date());
        // 1..抽取数据 如果是累加就是 List<DataModel>  如果是相除 Map<String,List<DataModel>>
        List<DataModel> dataModels = extract2Es(one);
        // 2 DataModel 转SaveModel即可
        List<SaveModel> saveModels = data2Save.data2save(dataModels,one,quotaDate,timeLevel);
        // 3.更新数据
        Boolean success = updateData(saveModels, quotaDate, one.getId(), timeLevel);
        tjQuotaLog.setJobType(success ? "1" : "0");
        tjQuotaLog.setJobEndTime(new Date());
        //tjQuotaLog.setJobContent(JSONArray.fromObject(filterModel.getErrorModels()).toString());
        saveLog(tjQuotaLog);
        logger.info("========================quotaCode:" +  one.getQuotaId() + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + " end========================");
    }
    private void mysql(JobConfigDO one) {
        logger.info("========================quotaCode:" + one.getId() + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + ",timeLevel:" + timeLevel + " start========================");
        JobLogDO tjQuotaLog = new JobLogDO();
        tjQuotaLog.setJobId(one.getId());
        tjQuotaLog.setJobStartTime(new Date());
        //1..抽取数据 如果是累加就是 List<DataModel>  如果是相除 Map<String,List<DataModel>>
        List<DataModel> dataModels = extract2Es(one);
        //2..根据规则过滤数据
        FilterModel filterModel = filter(dataModels);
        //得到改指标的维度
        List<DimensionQuotaDO> dimensionQuotas = dimensionQuotaDao.findDimensionQuotasByQuotaCode(one.getId());
        //2.1.从维度的key转换
        if (dimensionQuotas != null && dimensionQuotas.size() > 0) {
            filterModel = convert(filterModel, dimensionQuotas);
        }
        //3.统计数据
        List<SaveModel> sms = compute(filterModel.getData(), dimensionQuotas, timeLevel, one);
        //4.更新数据
        Boolean success = updateData(sms, quotaDate, one.getId(), timeLevel);
        tjQuotaLog.setJobType(success ? "1" : "0");
        tjQuotaLog.setJobEndTime(new Date());
        tjQuotaLog.setJobContent(JSONArray.fromObject(filterModel.getErrorModels()).toString());
        saveLog(tjQuotaLog);
        logger.info("========================quotaCode:" + one.getId() + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + " end========================");
    }
    private List<DataModel> extract2Es(JobConfigDO one) {
        try {
            List<DataModel> dataModels = SpringUtil.getBean(ExtractHelper.class).extractData(one, startTime, endTime, year, timeLevel);
            logger.info("quotaCode:" + one.getQuotaId() + ",size:" + dataModels.size());
            return dataModels;
        } catch (Exception e) {
            logger.error("extract error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 删除 某个指标某一天的某个timelevel的数据
     *
     * @param quotaDate
     * @param quotaCode
     * @param timeLevel
     */
    private boolean updateData(List<SaveModel> sms, Date quotaDate, String quotaCode, String timeLevel) {
        JestClient jestClient = null;
        try {
            jestClient = elasticFactory.getJestClient();
            //先根据条件查找出来
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(
                    new BoolQueryBuilder()
                            .must(QueryBuilders.matchQuery("quotaCode", quotaCode))
                            .must(QueryBuilders.matchQuery("timeLevel", timeLevel))
                            .must(QueryBuilders.matchQuery("quotaDate", quotaDate)))
                    .size(500000);
            Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(esIndex).addType(esType)
                    .build();
            SearchResult result = jestClient.execute(search);
            List<SaveModel> quarySaveModels = result.getSourceAsObjectList(SaveModel.class);
            //如果之前有值就用查询出来的然后修改数目即可
            if (quarySaveModels != null && quarySaveModels.size() > 0) {
                List<SaveModel> saveModels = new ArrayList<>();
                //把新的值赋值给ES查询出来的值
                Map<String, SaveModel> newSaveModelMaps = new HashMap<>();
                //list转map
                for (SaveModel newSaveModel : sms) {
                    StringBuffer key = new StringBuffer(newSaveModel.getTeam() + "-" + newSaveModel.getSlaveKey1() + "-" + newSaveModel.getSlaveKey2() + "-" + newSaveModel.getSlaveKey3() + "-" + newSaveModel.getSlaveKey4());
                    newSaveModelMaps.put(key.toString(), newSaveModel);
                }
                for (SaveModel esSavemodel : quarySaveModels) {
                    StringBuffer key = new StringBuffer(esSavemodel.getTeam() + "-" + esSavemodel.getSlaveKey1() + "-" + esSavemodel.getSlaveKey2() + "-" + esSavemodel.getSlaveKey3() + "-" + esSavemodel.getSlaveKey4());
                    SaveModel newSaveMode = newSaveModelMaps.get(key.toString());
                    if (newSaveMode != null) {
                        esSavemodel.setResult1(newSaveMode.getResult1());
                        esSavemodel.setResult2(newSaveMode.getResult2());
                        esSavemodel.setCreateTime(new Date());
                    }
                    saveModels.add(esSavemodel);
                }
                return updateDate(saveModels);
            } else {
                return saveDate(sms);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jestClient != null) {
                jestClient.shutdownClient();
            }
        }
        return false;
    }
    private boolean updateDate(List<SaveModel> sms) {
        try {
            return SpringUtil.getBean(SaveHelper.class).update(sms);
        } catch (Exception e) {
            logger.error("save error:" + e.getMessage());
        }
        return false;
    }
    @Transactional
    private void saveLog(JobLogDO tjQuotaLog) {
        quartzJobLogDao.save(tjQuotaLog);
    }
    private FilterModel convert(FilterModel dataModels, List<DimensionQuotaDO> dimensionQuotas) {
        try {
            return SpringUtil.getBean(ConvertHelper.class).convert(dataModels, dimensionQuotas);
        } catch (Exception e) {
            logger.error("convert error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 保存数据
     *
     * @param sms
     */
    private Boolean saveDate(List<SaveModel> sms) {
        try {
            return SpringUtil.getBean(SaveHelper.class).save(sms);
        } catch (Exception e) {
            logger.error("save error:" + e.getMessage());
        }
        return false;
    }
    /**
     * 根据计算规则统计数据
     *
     * @param dataModels
     */
    private List<SaveModel> compute(List<DataModel> dataModels, List<DimensionQuotaDO> dimensionQuotas, String timeLevel, JobConfigDO quartzJobConfig) {
        try {
            return SpringUtil.getBean(ComputeHelper.class).compute(dataModels, dimensionQuotas, quartzJobConfig.getId(), endTime, timeLevel);
        } catch (Exception e) {
            logger.error("compute error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 过滤数据
     *
     * @param filterModel
     * @return
     */
    private FilterModel filter(List<DataModel> filterModel) {
        try {
            return SpringUtil.getBean(FilterHelper.class).filter(filterModel);
        } catch (Exception e) {
            logger.error("filter error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 抽取数据
     *
     * @return
     */
    private List<DataModel> extract(JobConfigDO quartzJobConfig) {
        try {
            List<DataModel> dataModels = null;
            //先判断指标是否支持缓存
            if (StringUtils.isEmpty(quartzJobConfig.getCacheKey())) {
                //不支持直接去数据库拿
                dataModels = SpringUtil.getBean(ExtractHelper.class).extractData(quartzJobConfig, startTime, endTime, year, timeLevel);
            } else {
                //缓存的key 是 时间+timelevel+key
                StringBuffer bu = new StringBuffer(DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + "-" + timeLevel + quartzJobConfig.getCacheKey());
                //支持的话判断缓存有没有值
                dataModels = Cache.getCache(bu.toString());
                if (dataModels == null) {
                    //如果缓存是空的那么直接数据库拿 在放入缓存
                    dataModels = SpringUtil.getBean(ExtractHelper.class).extractData(quartzJobConfig, startTime, endTime, year, timeLevel);
                    Cache.addCache(bu.toString(), dataModels);
                }
            }
            return dataModels;
        } catch (Exception e) {
            logger.error("extract error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 获取现在时间属于那个年度
     *
     * @return
     */
    public static String getNowYearByDate() throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        String todayString = simpleDateFormat.format(today);
        String startDateString = (1900 + today.getYear()) + "-06-30";
        Date startDate = simpleDateFormat.parse(startDateString);
        if (simpleDateFormat.parse(todayString).after(startDate)) {
            return (1900 + today.getYear()) + "";
        } else {
            return (1900 + today.getYear() - 1) + "";
        }
    }
    public static void main(String[] args) {
        SimpleDateFormat s=new SimpleDateFormat("");
        System.out.println(s.format(new Date()));
    }
    public void getStartTime() throws Exception{
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        if("1".equals(this.incrementInterval)){//日
            startTime = new LocalDate(new DateTime().minusDays(2)).toString("yyyy-MM-dd") + " 17:00:00";
        }else if("2".equals(this.incrementInterval)){//周
            Date monday = DateUtil.getMondayOfThisDayToDate(sf.parse(endTime));
//            startTime =sf.format(monday)+ " 17:00:00";
            startTime =DateUtil.getNextDay(monday,-1)+ " 17:00:00";
        }else if("3".equals(this.incrementInterval)){//月
            Date fristDay = DateUtil.getFristDayOfMonthToDate(sf.parse(endTime));
//            startTime = sf.format(fristDay)+ " 17:00:00";
            startTime =DateUtil.getNextDay(fristDay,-1)+ " 17:00:00";
        }
    }
}

+ 305 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/EsToEsQuotaJob.java

@ -0,0 +1,305 @@
package com.yihu.wlyy.statistics.job.business;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.jw.entity.base.statistics.JobConfigDO;
import com.yihu.jw.entity.base.statistics.JobLogDO;
import com.yihu.wlyy.statistics.dao.QuartzJobConfigDao;
import com.yihu.wlyy.statistics.dao.QuartzJobLogDao;
import com.yihu.wlyy.statistics.etl.compute.ComputeHelper;
import com.yihu.wlyy.statistics.etl.convert.ConvertHelper;
import com.yihu.wlyy.statistics.etl.extract.ExtractHelper;
import com.yihu.wlyy.statistics.etl.extract.db.Data2Save;
import com.yihu.wlyy.statistics.etl.filter.FilterHelper;
import com.yihu.wlyy.statistics.etl.save.SaveHelper;
import com.yihu.wlyy.statistics.etl.save.es.ElasticFactory;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.util.SpringUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import com.yihu.wlyy.statistics.vo.FilterModel;
import com.yihu.wlyy.statistics.vo.SaveModel;
import io.searchbox.client.JestClient;
import io.searchbox.core.*;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * Created by chenweida on 2017/7/10.
 */
@Component
@Scope("prototype")
@DisallowConcurrentExecution//防止到了执行时间点前一任务还在执行中,但是这时有空闲的线程,那么马上又会执行,这样一来就会存在同一job被并行执行
public class EsToEsQuotaJob implements Job {
    private Logger logger = LoggerFactory.getLogger(EsToEsQuotaJob.class);
    private String wlyyJobCongId;//指标对象
    private JobConfigDO quartzJobConfig;//指标对象
    private String endTime;//结束时间
    private String startTime;//开始时间
    private String year;//要统计的年份
    private Date quotaDate;//统计的时间
    private String timeLevel;//1 日 2年
    @Autowired
    private QuartzJobLogDao quartzJobLogDao;//执行日志Dao
    @Autowired
    private QuartzJobConfigDao quartzJobConfigDao;
    @Autowired
    private ElasticFactory elasticFactory;
    @Value("${es.type}")
    private String esType;
    @Value("${es.index}")
    private String esIndex;
    @Autowired
    private Data2Save data2Save;
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            //springz注入
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            //初始化参数
            initParams(context);
            //统计指标
            computequota();
        } catch (Exception e) {
            //如果出錯立即重新執行
            JobExecutionException e2 = new JobExecutionException(e);
            e2.setRefireImmediately(true);
            e.printStackTrace();
        }
    }
    /**
     * 初始化参数
     *
     * @param context
     */
    private void initParams(JobExecutionContext context) throws Exception {
        JobDataMap map = context.getJobDetail().getJobDataMap();
        this.timeLevel = map.getString("timeLevel");
        this.endTime = map.getString("endTime");
        this.startTime = map.getString("startTime");
        //为空默认是统计昨天的数据  统计昨天的数据是从 前天的下午17:00:00 到昨天的下午17:00:00
        //初始化结束时间
        if (StringUtils.isEmpty(endTime)) {
            endTime = new LocalDate(new DateTime().minusDays(1)).toString("yyyy-MM-dd") + "T17:00:00+0800"; //2017-06-01 17:00:00
        } else {
            endTime = endTime + "T17:00:00+0800";
        }
        //初始化统计年份
        this.year = getNowYearByDate(endTime);
        //初始化开始时间
        if ("2".equals(timeLevel)) {
            //按年度到达量  2017-09-15T14:15:14+0800
            startTime = this.year + "-06-30T17:00:00+0800";
        } else {
            //增量
            if (StringUtils.isEmpty(startTime)) {
                startTime = new LocalDate(new DateTime().minusDays(2)).toString("yyyy-MM-dd") + "T17:00:00+0800"; //2017-06-01 17:00:00
            } else {
                startTime = startTime + "T17:00:00+0800";
            }
        }
        this.quotaDate = DateUtil.strToDate(map.getString("endTime"), "yyyy-MM-dd");
        this.wlyyJobCongId = map.getString("jobConfig");
        this.quartzJobConfig = quartzJobConfigDao.findById(wlyyJobCongId);
        quartzJobConfig.setStartTime(startTime);
        quartzJobConfig.setEndTime(endTime);
    }
    /**
     * 计算指标
     */
    private void computequota() {
        try {
            logger.info("========================quotaCode:" + wlyyJobCongId + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + ",timeLevel:" + timeLevel + " start========================");
            JobLogDO tjQuotaLog = new JobLogDO();
            tjQuotaLog.setJobId(wlyyJobCongId);
            tjQuotaLog.setJobStartTime(new Date());
            // 0 删除这天的数据
            deleteData(quotaDate, wlyyJobCongId, timeLevel);
            // 1..抽取数据 如果是累加就是 List<DataModel>  如果是相除 Map<String,List<DataModel>>
            List<DataModel> dataModels = extract();
            // 2 DataModel 转SaveModel即可
            List<SaveModel> saveModels = data2Save.data2save(dataModels,quartzJobConfig,quotaDate,timeLevel);
            // 3.保存数据
            Boolean success = saveDate(saveModels);
            tjQuotaLog.setJobType(success ? "1" : "0");
            tjQuotaLog.setJobEndTime(new Date());
            //tjQuotaLog.setJobContent(JSONArray.fromObject(filterModel.getErrorModels()).toString());
            saveLog(tjQuotaLog);
            logger.info("========================quotaCode:" + wlyyJobCongId + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + " end========================");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 删除 某个指标某一天的某个timelevel的数据
     *
     * @param quotaDate
     * @param quotaCode
     * @param timeLevel
     */
    private void deleteData(Date quotaDate, String quotaCode, String timeLevel) {
        JestClient jestClient = null;
        try {
            jestClient = elasticFactory.getJestClient();
            //先根据条件查找出来
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(
                    new BoolQueryBuilder()
                            .must(QueryBuilders.matchQuery("quotaCode", quotaCode))
                            .must(QueryBuilders.matchQuery("timeLevel", timeLevel))
                            .must(QueryBuilders.matchQuery("quotaDate", quotaDate))
            ).size(500000);//一次取10000条
            Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(esIndex).addType(esType)
                    .build();
            SearchResult result = jestClient.execute(search);
            List<SaveModel> saveModels = result.getSourceAsObjectList(SaveModel.class);
            //根据id批量删除
            Bulk.Builder bulk = new Bulk.Builder().defaultIndex(esIndex).defaultType(esType);
            for (SaveModel obj : saveModels) {
                Delete index = new Delete.Builder(obj.getId()).build();
                bulk.addAction(index);
            }
            BulkResult br = jestClient.execute(bulk.build());
            logger.info("delete data count:" + saveModels.size());
            logger.info("delete flag:" + br.isSucceeded());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jestClient != null) {
                jestClient.shutdownClient();
            }
        }
    }
    @Transactional
    private void saveLog(JobLogDO tjQuotaLog) {
        quartzJobLogDao.save(tjQuotaLog);
    }
    private FilterModel convert(FilterModel dataModels, List<DimensionQuotaDO> dimensionQuotas) {
        try {
            return SpringUtil.getBean(ConvertHelper.class).convert(dataModels, dimensionQuotas);
        } catch (Exception e) {
            logger.error("convert error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 保存数据
     *
     * @param sms
     */
    private Boolean saveDate(List<SaveModel> sms) {
        try {
            return SpringUtil.getBean(SaveHelper.class).save(sms);
        } catch (Exception e) {
            logger.error("save error:" + e.getMessage());
        }
        return false;
    }
    /**
     * 根据计算规则统计数据
     *
     * @param dataModels
     */
    private List<SaveModel> compute(List<DataModel> dataModels, List<DimensionQuotaDO> dimensionQuotas, String timeLevel) {
        try {
            return SpringUtil.getBean(ComputeHelper.class).compute(dataModels, dimensionQuotas, wlyyJobCongId, endTime, timeLevel);
        } catch (Exception e) {
            logger.error("compute error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 过滤数据
     *
     * @param dataModel
     * @return
     */
    private FilterModel filter(List<DataModel> dataModel) {
        try {
            FilterModel filterModel = SpringUtil.getBean(FilterHelper.class).filter(dataModel);
            logger.info("FilterModel:  success sizs:" + filterModel.getData().size() + ",error size:" + filterModel.getErrorModels().size());
            return filterModel;
        } catch (Exception e) {
            logger.error("filter error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 抽取数据
     *
     * @return
     */
    private List<DataModel> extract() {
        try {
            List<DataModel> dataModels = SpringUtil.getBean(ExtractHelper.class).extractData(quartzJobConfig, startTime, endTime, year, timeLevel);
            logger.info("quotaCode:" + wlyyJobCongId + ",size:" + dataModels.size());
            return dataModels;
        } catch (Exception e) {
            logger.error("extract error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 获取现在时间属于那个年度
     *
     * @param date
     * @return
     */
    public static String getNowYearByDate(String date) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date today = simpleDateFormat.parse(date);
        String todayString = simpleDateFormat.format(today);
        String startDateString = (1900 + today.getYear()) + "-06-30";
        Date startDate = simpleDateFormat.parse(startDateString);
        if (simpleDateFormat.parse(todayString).after(startDate)) {
            return (1900 + today.getYear()) + "";
        } else {
            return (1900 + today.getYear() - 1) + "";
        }
    }
}

+ 343 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/job/business/MysqlToEsQuotaJob.java

@ -0,0 +1,343 @@
package com.yihu.wlyy.statistics.job.business;
import com.yihu.jw.entity.base.statistics.DimensionQuotaDO;
import com.yihu.jw.entity.base.statistics.JobConfigDO;
import com.yihu.jw.entity.base.statistics.JobLogDO;
import com.yihu.wlyy.statistics.dao.QuartzJobConfigDao;
import com.yihu.wlyy.statistics.dao.QuartzJobLogDao;
import com.yihu.wlyy.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.wlyy.statistics.etl.cache.Cache;
import com.yihu.wlyy.statistics.etl.compute.ComputeHelper;
import com.yihu.wlyy.statistics.etl.convert.ConvertHelper;
import com.yihu.wlyy.statistics.etl.extract.ExtractHelper;
import com.yihu.wlyy.statistics.etl.filter.FilterHelper;
import com.yihu.wlyy.statistics.etl.save.SaveHelper;
import com.yihu.wlyy.statistics.etl.save.es.ElasticFactory;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.util.SpringUtil;
import com.yihu.wlyy.statistics.vo.DataModel;
import com.yihu.wlyy.statistics.vo.FilterModel;
import com.yihu.wlyy.statistics.vo.SaveModel;
import io.searchbox.client.JestClient;
import io.searchbox.core.*;
import net.sf.json.JSONArray;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * Created by chenweida on 2017/7/10.
 */
@Component
@Scope("prototype")
@DisallowConcurrentExecution//防止到了执行时间点前一任务还在执行中,但是这时有空闲的线程,那么马上又会执行,这样一来就会存在同一job被并行执行
public class MysqlToEsQuotaJob implements Job {
    private Logger logger = LoggerFactory.getLogger(MysqlToEsQuotaJob.class);
    private String wlyyJobCongId;//指标对象
    private JobConfigDO quartzJobConfig;//指标对象
    private String endTime;//结束时间
    private String startTime;//开始时间
    private String year;//要统计的年份
    private Date quotaDate;//统计的时间
    private String timeLevel;//1 日 2年
    private String incrementInterval;//增量的时间间隔(天)
    @Autowired
    private QuartzJobLogDao quartzJobLogDao;//执行日志Dao
    @Autowired
    private WlyyDimensionQuotaDao dimensionQuotaDao;
    @Autowired
    private QuartzJobConfigDao quartzJobConfigDao;
    @Autowired
    private ElasticFactory elasticFactory;
    @Value("${es.type}")
    private String esType;
    @Value("${es.index}")
    private String esIndex;
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            //springz注入
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            //初始化参数
            initParams(context);
            //统计指标
            computequota();
        } catch (Exception e) {
            //如果出錯立即重新執行
            JobExecutionException e2 = new JobExecutionException(e);
            e2.setRefireImmediately(true);
            e.printStackTrace();
        }
    }
    /**
     * 初始化参数
     *
     * @param context
     */
    private void initParams(JobExecutionContext context) throws Exception {
        JobDataMap map = context.getJobDetail().getJobDataMap();
        this.timeLevel = map.getString("timeLevel");
        this.endTime = map.getString("endTime");
        this.startTime = map.getString("startTime");
        this.incrementInterval = map.get("incrementInterval")!=null?String.valueOf(map.get("incrementInterval")):"1";
        //为空默认是统计昨天的数据  统计昨天的数据是从 前天的下午17:00:00 到昨天的下午17:00:00
        //初始化结束时间
        if (StringUtils.isEmpty(endTime)) {
            endTime = new LocalDate(new DateTime().minusDays(1)).toString("yyyy-MM-dd") + " 17:00:00"; //2017-06-01 17:00:00
        } else {
            endTime = endTime + " 17:00:00";
        }
        //初始化统计年份
        this.year = getNowYearByDate(endTime);
        //初始化开始时间
        if ("2".equals(timeLevel)) {
            //按年度到达量
            startTime = this.year + "-06-30 17:00:00";
        } else {
            //增量
            if (StringUtils.isEmpty(startTime)) {
//                startTime = new LocalDate(new DateTime().minusDays(2)).toString("yyyy-MM-dd") + " 17:00:00"; //2017-06-01 17:00:00
                getStartTime();
            } else {
                startTime = startTime + " 17:00:00";
            }
        }
        this.quotaDate = DateUtil.strToDate(endTime, "yyyy-MM-dd");
        this.wlyyJobCongId = map.getString("jobConfig");
        this.quartzJobConfig = quartzJobConfigDao.findById(wlyyJobCongId);
    }
    /**
     * 计算指标
     */
    private void computequota() {
        try {
            logger.info("========================quotaCode:" + wlyyJobCongId + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + ",timeLevel:" + timeLevel + " start========================");
            JobLogDO tjQuotaLog = new JobLogDO();
            tjQuotaLog.setJobId(wlyyJobCongId);
            tjQuotaLog.setJobStartTime(new Date());
            // 0 删除这天的数据
            deleteData(quotaDate, wlyyJobCongId, timeLevel);
            //1..抽取数据 如果是累加就是 List<DataModel>  如果是相除 Map<String,List<DataModel>>
            List<DataModel> dataModels = extract();
            //2..根据规则过滤数据
            FilterModel filterModel = filter(dataModels);
            //得到该指标的维度
            List<DimensionQuotaDO> dimensionQuotas = dimensionQuotaDao.findDimensionQuotasByQuotaCode(wlyyJobCongId);
            //2.1.从维度的key转换
            if (dimensionQuotas != null && dimensionQuotas.size() > 0) {
                filterModel = convert(filterModel, dimensionQuotas);
            }
            //3.统计数据
            List<SaveModel> sms = compute(filterModel.getData(), dimensionQuotas, timeLevel);
            //4.保存数据
            Boolean success = saveDate(sms);
            tjQuotaLog.setJobType(success ? "1" : "0");
            tjQuotaLog.setJobEndTime(new Date());
            tjQuotaLog.setJobContent(JSONArray.fromObject(filterModel.getErrorModels()).toString());
            saveLog(tjQuotaLog);
            logger.info("========================quotaCode:" + wlyyJobCongId + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + " end========================");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 删除 某个指标某一天的某个timelevel的数据
     *
     * @param quotaDate
     * @param quotaCode
     * @param timeLevel
     */
    private void deleteData(Date quotaDate, String quotaCode, String timeLevel) {
        JestClient jestClient = null;
        try {
            jestClient = elasticFactory.getJestClient();
            //先根据条件查找出来
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(
                    new BoolQueryBuilder()
                            .must(QueryBuilders.matchQuery("quotaCode", quotaCode))
                            .must(QueryBuilders.matchQuery("timeLevel", timeLevel))
                            .must(QueryBuilders.matchQuery("quotaDate", quotaDate))
            ).size(500000);//一次取10000条
            Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(esIndex).addType(esType)
                    .build();
            SearchResult result = jestClient.execute(search);
            List<SaveModel> saveModels = result.getSourceAsObjectList(SaveModel.class);
            //根据id批量删除
            Bulk.Builder bulk = new Bulk.Builder().defaultIndex(esIndex).defaultType(esType);
            for (SaveModel obj : saveModels) {
                Delete index = new Delete.Builder(obj.getId()).build();
                bulk.addAction(index);
            }
            BulkResult br = jestClient.execute(bulk.build());
            logger.info("delete data count:" + saveModels.size());
            logger.info("delete flag:" + br.isSucceeded());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jestClient != null) {
                jestClient.shutdownClient();
            }
        }
    }
    @Transactional
    private void saveLog(JobLogDO tjQuotaLog) {
        quartzJobLogDao.save(tjQuotaLog);
    }
    private FilterModel convert(FilterModel dataModels, List<DimensionQuotaDO> dimensionQuotas) {
        try {
            return SpringUtil.getBean(ConvertHelper.class).convert(dataModels, dimensionQuotas);
        } catch (Exception e) {
            logger.error("convert error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 保存数据
     *
     * @param sms
     */
    private Boolean saveDate(List<SaveModel> sms) {
        try {
            return SpringUtil.getBean(SaveHelper.class).save(sms);
        } catch (Exception e) {
            logger.error("save error:" + e.getMessage());
        }
        return false;
    }
    /**
     * 根据计算规则统计数据
     *
     * @param dataModels
     */
    private List<SaveModel> compute(List<DataModel> dataModels, List<DimensionQuotaDO> dimensionQuotas, String timeLevel) {
        try {
            return SpringUtil.getBean(ComputeHelper.class).compute(dataModels, dimensionQuotas, wlyyJobCongId, endTime, timeLevel);
        } catch (Exception e) {
            logger.error("compute error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 过滤数据
     *
     * @param dataModel
     * @return
     */
    private FilterModel filter(List<DataModel> dataModel) {
        try {
            FilterModel filterModel = SpringUtil.getBean(FilterHelper.class).filter(dataModel);
            logger.info("FilterModel:  success sizs:" + filterModel.getData().size() + ",error size:" + filterModel.getErrorModels().size());
            return filterModel;
        } catch (Exception e) {
            logger.error("filter error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 抽取数据
     *
     * @return
     */
    private List<DataModel> extract() {
        try {
            List<DataModel> dataModels = null;
            //先判断指标是否支持缓存
            if (StringUtils.isEmpty(quartzJobConfig.getCacheKey())) {
                //不支持直接去数据库拿
                dataModels = SpringUtil.getBean(ExtractHelper.class).extractData(quartzJobConfig, startTime, endTime, year, timeLevel);
            } else {
                //缓存的key 是 时间+timelevel+key
                StringBuffer bu = new StringBuffer(DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + "-" + timeLevel + "-" + quartzJobConfig.getCacheKey());
                //支持的话判断缓存有没有值
                dataModels = Cache.getCache(bu.toString());
                if (dataModels == null) {
                    //如果缓存是空的那么直接数据库拿 在放入缓存
                    dataModels = SpringUtil.getBean(ExtractHelper.class).extractData(quartzJobConfig, startTime, endTime, year, timeLevel);
                    Cache.addCache(bu.toString(), dataModels);
                }
            }
            logger.info("quotaCode:" + wlyyJobCongId + ",size:" + dataModels.size());
            return dataModels;
        } catch (Exception e) {
            logger.error("extract error:" + e.getMessage());
        }
        return null;
    }
    /**
     * 获取现在时间属于那个年度
     *
     * @param date
     * @return
     */
    public static String getNowYearByDate(String date) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date today = simpleDateFormat.parse(date);
        String todayString = simpleDateFormat.format(today);
        String startDateString = (1900 + today.getYear()) + "-06-30";
        Date startDate = simpleDateFormat.parse(startDateString);
        if (simpleDateFormat.parse(todayString).after(startDate)) {
            return (1900 + today.getYear()) + "";
        } else {
            return (1900 + today.getYear() - 1) + "";
        }
    }
    public void getStartTime() throws Exception{
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        if("1".equals(this.incrementInterval)){//日
            startTime = new LocalDate(new DateTime().minusDays(2)).toString("yyyy-MM-dd") + " 17:00:00";
        }else if("2".equals(this.incrementInterval)){//周
            Date monday = DateUtil.getMondayOfThisDayToDate(sf.parse(endTime));
//            startTime =sf.format(monday)+ " 17:00:00";
            startTime =DateUtil.getNextDay(monday,-1)+ " 17:00:00";
        }else if("3".equals(this.incrementInterval)){//月
            Date fristDay = DateUtil.getFristDayOfMonthToDate(sf.parse(endTime));
//            startTime = sf.format(fristDay)+ " 17:00:00";
            startTime =DateUtil.getNextDay(fristDay,-1)+ " 17:00:00";
        }
    }
}

+ 43 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/service/ElasticsearchService.java

@ -0,0 +1,43 @@
package com.yihu.wlyy.statistics.service;
import com.yihu.wlyy.statistics.util.ElasticsearchUtil;
import com.yihu.wlyy.statistics.vo.SaveModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * Created by chenweida on 2017/7/14.
 */
@Service
public class ElasticsearchService {
    @Autowired
    private ElasticsearchUtil elasticsearchUtil;
    /**
     * 查询一级维度的指标
     *
     * @param quotaCode 指标id
     * @param timeLevel 1增量 2到达量
     * @param areaLevel 1 省 2 市 3 区县 4 机构 5团队
     * @param code      如果areaLevel是3就是区的code 如果是4就是机构的code
     * @param quotaDate 时间 yyyy-MM-dd
     * @return
     */
    public SaveModel findDimension1Quota(String quotaCode, String timeLevel, String areaLevel, String code, String quotaDate) {
        return null;
    }
    public SaveModel findDimension1QuotaList(String quotaCode, String timelevel, String parentAreaLevel, String code, String quotaDate, String chlAreaLevel) {
        return null;
    }
    public List<SaveModel> excute(String sql) {
        return elasticsearchUtil.excute(sql);
    }
}

+ 595 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/service/JobService.java

@ -0,0 +1,595 @@
package com.yihu.wlyy.statistics.service;
import com.yihu.jw.entity.base.statistics.JobConfigDO;
import com.yihu.wlyy.statistics.dao.QuartzJobConfigDao;
import com.yihu.wlyy.statistics.etl.cache.Cache;
import com.yihu.wlyy.statistics.job.business.CacheCleanJob;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.util.QuartzHelper;
import com.yihu.wlyy.statistics.vo.WlyyJobConfigVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author chenweida
 */
@Service
public class JobService {
    @Value("${JobService.sleepTime}")
    private Integer sleepTime;
    @Autowired
    private QuartzHelper quartzHelper;
    @Autowired
    private QuartzJobConfigDao wlyyJobConfigDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Transactional
    public void stopById(String id) throws Exception {
        JobConfigDO quartzJobConfig = wlyyJobConfigDao.findById(id, "1");
        if (quartzJobConfig != null) {
            for (int j = 1; j <= 2; j++) {
                if (breakPoint(quartzJobConfig, j)) continue;
                quartzHelper.removeJob(quartzJobConfig.getId() + "-" + j);
                wlyyJobConfigDao.updateStatus(quartzJobConfig.getId(), "0");
            }
        } else {
            throw new Exception("任务已经停止");
        }
    }
    @Transactional
    public void startById(String id) throws Exception {
        JobConfigDO quartzJobConfig = wlyyJobConfigDao.findById(id, "0");
        if (quartzJobConfig != null) {
            startOneJob(quartzJobConfig);
        } else {
            throw new Exception("任务已经启动");
        }
    }
    @Transactional
    public void stopAll() throws Exception {
        List<JobConfigDO> quartzJobConfigs = wlyyJobConfigDao.findByAll("1");
        if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
            for (JobConfigDO quartzJobConfig : quartzJobConfigs) {
                for (int j = 1; j <= 2; j++) {
                    if (breakPoint(quartzJobConfig, j)) continue;
                    quartzHelper.removeJob(quartzJobConfig.getId() + "-" + j);
                    wlyyJobConfigDao.updateStatus(quartzJobConfig.getId(), "0");
                    ;
                }
            }
        } else {
            throw new Exception("任务已经全部停止");
        }
    }
    @Transactional
    public void startAll() throws Exception {
        List<JobConfigDO> quartzJobConfigs = wlyyJobConfigDao.findByAll("0");
        if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
            for (JobConfigDO quartzJobConfig : quartzJobConfigs) {
                startOneJob(quartzJobConfig);
            }
        } else {
            throw new Exception("任务已经全部启动");
        }
    }
    /**
     * 启动单个任务
     *
     * @param quartzJobConfig
     * @throws Exception
     */
    @Transactional
    private void startOneJob(JobConfigDO quartzJobConfig) throws Exception {
        WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
        BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("jobConfig", wlyyJobConfigVO.getId());
        params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
        if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
            for (int j = 1; j <= 2; j++) {
                if (breakPoint(wlyyJobConfigVO, j)) continue;
                params.put("timeLevel", j + "");
                //往quartz框架添加任务
                quartzHelper.addJob(getRightClass(quartzJobConfig), quartzJobConfig.getQuartzCron(), quartzJobConfig.getId() + "-" + j, params);
                wlyyJobConfigDao.updateStatus(quartzJobConfig.getId(), "1");//设置任务状态是启动 }
            }
        }
    }
    public void startNowById(String id) throws Exception {
        JobConfigDO quartzJobConfig = wlyyJobConfigDao.findOne(id);
        WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
        BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
        Map<String, String> params = new HashMap<String, String>();
        params.put("jobConfig", wlyyJobConfigVO.getId());
        params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
        for (int i = 1; i <= 2; i++) {
            if (breakPoint(wlyyJobConfigVO, i)) continue;
            params.put("timeLevel", i + "");
            //往quartz框架添加任务
            if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
                Thread.sleep(sleepTime);
            }
        }
    }
    private boolean breakPoint(WlyyJobConfigVO wlyyJobConfigVO, int i) {
        //该方法使用于每日统计
        if(wlyyJobConfigVO.getIncrementInterval()!=1){
            return true;
        }
        //如果为空或者等3说明纪要生成到达量也要生成增量
        if (StringUtils.isEmpty(wlyyJobConfigVO.getTimeLevel()) || Integer.valueOf(wlyyJobConfigVO.getTimeLevel()) == 3) {
            return false;
        }
        //如果不为空 并且是1或者2 说明只要增量或者只要到达量
        if (!(StringUtils.isEmpty(wlyyJobConfigVO.getTimeLevel())) && Integer.valueOf(wlyyJobConfigVO.getTimeLevel()) == i) {
            return false;
        }
        return true;
    }
    private boolean breakPoint(JobConfigDO wlyyJobConfigVO, int i) {
        //该方法使用于每日统计
        if(wlyyJobConfigVO.getIncrementInterval()!=1){
            return true;
        }
        //如果为空或者等3说明纪要生成到达量也要生成增量
        if (StringUtils.isEmpty(wlyyJobConfigVO.getTimeLevel()) || Integer.valueOf(wlyyJobConfigVO.getTimeLevel()) == 3) {
            return false;
        }
        //如果不为空 并且是1或者2 说明只要增量或者只要到达量
        if (!(StringUtils.isEmpty(wlyyJobConfigVO.getTimeLevel())) && Integer.valueOf(wlyyJobConfigVO.getTimeLevel()) == i) {
            return false;
        }
        return true;
    }
    public void productDataByDay(Integer day) throws Exception {
        List<JobConfigDO> quartzJobConfigs = wlyyJobConfigDao.findByIds();
        for (JobConfigDO quartzJobConfig : quartzJobConfigs) {
            WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
            BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
            Map<String, String> params = new HashMap<String, String>();
            params.put("jobConfig", wlyyJobConfigVO.getId());
            params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
            for (int i = 1; i <= day; i++) {
                for (int j = 1; j <= 2; j++) {
                    if (breakPoint(wlyyJobConfigVO, j)) continue;
                    params.put("timeLevel", j + "");
                    //往quartz框架添加任务
                    params.put("startTime", getYesterday(0 - i - 1));
                    params.put("endTime", getYesterday(0 - i));
                    if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                        quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
                        Thread.sleep(sleepTime);
                    }
                }
            }
        }
    }
    public static String getYesterday(Integer day) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, day);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return yesterday;
    }
    public void productDataByOneDay(String yesterday) throws Exception {
        String sql = "select * from wlyy_job_config_new a where  a.del='1' and a.id !=11 order by a.id asc";
        SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dataSimple.parse(yesterday);
        if (date == null) {
            throw new Exception("时间格式错误");
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
        Date nowDate = calendar.getTime();   //这个时间就是日期往后推一天的结果
        String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
        List<JobConfigDO> quartzJobConfigs = jdbcTemplate.query(sql, new BeanPropertyRowMapper(JobConfigDO.class));
        for (JobConfigDO quartzJobConfig : quartzJobConfigs) {
            WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
            BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
            Map<String, String> params = new HashMap<String, String>();
            params.put("jobConfig", wlyyJobConfigVO.getId());
            params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
            //往quartz框架添加任务
            params.put("startTime", daybefore);
            params.put("endTime", yesterday);
            for (int j = 1; j <= 2; j++) {
                if (breakPoint(wlyyJobConfigVO, j)) continue;
                params.put("timeLevel", j + "");
                if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                    quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
                    Thread.sleep(sleepTime);
                }
            }
        }
    }
    /**
     * @param quartzJobConfig
     * @return
     * @throws ClassNotFoundException
     */
    private Class getRightClass(JobConfigDO quartzJobConfig) throws ClassNotFoundException {
        return Class.forName(quartzJobConfig.getJobClass());
    }
    public void productDataByOneDayWithId(String yesterday, String id) throws Exception {
        SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dataSimple.parse(yesterday);
        if (date == null) {
            throw new Exception("时间格式错误");
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
        Date nowDate = calendar.getTime();   //这个时间就是日期往后推一天的结果
        String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
        JobConfigDO quartzJobConfig = wlyyJobConfigDao.findById(id);
        if (quartzJobConfig == null) {
            throw new Exception("id不存在");
        }
        WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
        BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
        Map<String, String> params = new HashMap<String, String>();
        params.put("jobConfig", wlyyJobConfigVO.getId());
        params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
        //往quartz框架添加任务
        params.put("startTime", daybefore);
        params.put("endTime", yesterday);
        for (int j = 1; j <= 2; j++) {
            if (breakPoint(wlyyJobConfigVO, j)) continue;
            params.put("timeLevel", j + "");
            if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
                Thread.sleep(sleepTime);
            }
        }
    }
    public void productDataByDayAndId(Integer day, String id) throws Exception {
        JobConfigDO quartzJobConfig = wlyyJobConfigDao.findById(id);
        if (quartzJobConfig == null) {
            throw new Exception("id不存在");
        }
        WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
        BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
        Map<String, String> params = new HashMap<String, String>();
        params.put("jobConfig", wlyyJobConfigVO.getId());
        params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
        for (int i = 1; i <= day; i++) {
            //往quartz框架添加任务
            params.put("startTime", getYesterday(0 - i - 1));
            params.put("endTime", getYesterday(0 - i));
            for (int j = 1; j <= 2; j++) {
                if (breakPoint(wlyyJobConfigVO, j)) continue;
                params.put("timeLevel", j + "");
                if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                    quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
                    Thread.sleep(sleepTime);
                }
            }
        }
    }
    public void productDataByDayToDay(String start, String end) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (startDate.after(endDate)) {
            throw new Exception("日期参数错误");
        }
        int day = daysBetween(startDate, endDate);
        for (int i = 0; i < day; i++) {
            Cache.cleanCache();//清空缓存
            // Thread.sleep(60000L);//休息60秒
            productDataByOneDay(getYesterday(i, startDate));
        }
    }
    public static String getYesterday(Integer day, Date startDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        cal.add(Calendar.DAY_OF_MONTH, day);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return yesterday;
    }
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }
    public void productDataByDayToDayAndId(String start, String end, String ids) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (startDate.after(endDate)) {
            throw new Exception("日期参数错误");
        }
        int day = daysBetween(startDate, endDate);
        String[] idStr = ids.split(",");
        for (int i = 0; i < day; i++) {
            Cache.cleanCache();//清空缓存
            for (String id : idStr) {
                productDataByOneDayWithId(getYesterday(i, startDate), id);
            }
        }
    }
    //================================================没有休眠时间=============================================================
    public void productDataByDayToDayAndIdNoSleep(String start, String end, String id, Long sleepTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (startDate.after(endDate)) {
            throw new Exception("日期参数错误");
        }
        int day = daysBetween(startDate, endDate);
        for (int i = 0; i < day; i++) {
            Cache.cleanCache();//清空缓存
            productDataByOneDayWithIdNoSleep(getYesterday(i, startDate), id, sleepTime);
        }
    }
    public void productDataByOneDayWithIdNoSleep(String yesterday, String id, Long sleepTime) throws Exception {
        SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dataSimple.parse(yesterday);
        if (date == null) {
            throw new Exception("时间格式错误");
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
        Date nowDate = calendar.getTime();   //这个时间就是日期往后推一天的结果
        String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
        JobConfigDO quartzJobConfig = wlyyJobConfigDao.findById(id);
        if (quartzJobConfig == null) {
            throw new Exception("id不存在");
        }
        WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
        BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
        Map<String, String> params = new HashMap<String, String>();
        params.put("jobConfig", wlyyJobConfigVO.getId());
        params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
        //往quartz框架添加任务
        params.put("startTime", daybefore);
        params.put("endTime", yesterday);
        for (int j = 1; j <= 2; j++) {
            params.put("timeLevel", j + "");
            if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
            }
            Thread.sleep(sleepTime * 1000L);
        }
    }
    public void productDataByOneDayNoSleep(String yesterday, Long sleepTime) throws Exception {
        String sql = "select * from wlyy_job_config_new a where  a.del='1' and a.id !=11 order by a.id asc";
        SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dataSimple.parse(yesterday);
        if (date == null) {
            throw new Exception("时间格式错误");
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
        Date nowDate = calendar.getTime();   //这个时间就是日期往后推一天的结果
        String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
        List<JobConfigDO> quartzJobConfigs = jdbcTemplate.query(sql, new BeanPropertyRowMapper(JobConfigDO.class));
        for (JobConfigDO quartzJobConfig : quartzJobConfigs) {
            WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
            BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
            Map<String, String> params = new HashMap<String, String>();
            params.put("jobConfig", wlyyJobConfigVO.getId());
            params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
            //往quartz框架添加任务
            params.put("startTime", daybefore);
            params.put("endTime", yesterday);
            for (int j = 1; j <= 2; j++) {
                params.put("timeLevel", j + "");
                if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                    quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
                }
                Thread.sleep(sleepTime * 1000L);
            }
        }
    }
    public void startCleanCacheJob() throws Exception {
        if (!quartzHelper.isExistJob(CacheCleanJob.jobKey)) {
            quartzHelper.addJob(CacheCleanJob.class, CacheCleanJob.cron, CacheCleanJob.jobKey, new HashMap<>());
        } else {
            throw new Exception("已经启动");
        }
    }
    public void stopCleanCacheJob() throws Exception {
        if (quartzHelper.isExistJob(CacheCleanJob.jobKey)) {
            quartzHelper.removeJob(CacheCleanJob.jobKey);
        } else {
            throw new Exception("已经停止");
        }
    }
    /**************************************按周或按月****************************************/
    public void productWeekByDayToDay(String start, String end,String id) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (startDate.after(endDate)) {
            throw new Exception("日期参数错误");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int a = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.setTime(endDate);
        int b = calendar.get(Calendar.YEAR);
        int startWeek = DateUtil.week(startDate);
        if(a!=b){
            Date lastDate = DateUtil.getYearLast(a);
            int lastWeek = DateUtil.week(lastDate);
            for(int i=startWeek;i<=lastWeek;i++){
                start = getDate(startDate ,i,2);
                end = getDate(startDate ,i,7);
                productDataByOneDay2(DateUtil.getNextDay(sdf.parse(start),-1),DateUtil.getNextDay(sdf.parse(end),1),2,id);
            }
            startDate =lastDate;
        }
        startWeek = DateUtil.week(startDate);
        int endWeek = DateUtil.week(endDate);
        for(int i=startWeek;i<=endWeek;i++){
            start = getDate(startDate ,i,2);
            end = getDate(startDate ,i,7);
            productDataByOneDay2(DateUtil.getNextDay(sdf.parse(start),-1),DateUtil.getNextDay(sdf.parse(end),1),2,id);
        }
    }
    public void productMonthByDayToDay(String start, String end,String id) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (startDate.after(endDate)) {
            throw new Exception("日期参数错误");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int a = calendar.get(Calendar.YEAR);
        int a1 = calendar.get(Calendar.MONTH);
        calendar.clear();
        calendar.setTime(endDate);
        int b = calendar.get(Calendar.YEAR);
        int b1 = calendar.get(Calendar.MONTH);
        if(a!=b){
            for(int i=a1;i<=12;i++){
//            start = getDate(startDate ,i,1)+" 00:00:00";
//            end = getDate(startDate ,i,7)+" 59:59:59";
                start = DateUtil.getFristDayOfMonth(startDate);
                end = DateUtil.getLastDayOfMonth(startDate);
                productDataByOneDay2(start,end,3,id);
            }
            a1=1;
            calendar.clear();
            calendar.set(Calendar.YEAR, b);
            startDate = calendar.getTime();
        }
        for(int i=a1;i<=b1;i++){
            start = DateUtil.getFristDayOfMonth(startDate);
            end = DateUtil.getLastDayOfMonth(startDate);
            productDataByOneDay2(start,end,3,id);
        }
    }
    public void productDataByOneDay2(String start, String end,Integer incrementInterval,String id) throws Exception {
        String condition = "";
        if(!StringUtils.isEmpty(id)){
            condition+=" and a.id in ("+id+") ";
        }
        if(incrementInterval!=null){
            condition +=" and a.increment_interval ="+incrementInterval.intValue();
        }
        String sql = "select * from wlyy_job_config_new a where  a.del='1' and a.id !=11 "+condition+" order by a.id asc";
        List<JobConfigDO> quartzJobConfigs = jdbcTemplate.query(sql, new BeanPropertyRowMapper(JobConfigDO.class));
        for (JobConfigDO quartzJobConfig : quartzJobConfigs) {
            WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
            BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
            Map<String, String> params = new HashMap<String, String>();
            params.put("jobConfig", wlyyJobConfigVO.getId());
            params.put("incrementInterval", wlyyJobConfigVO.getIncrementInterval()!=null?String.valueOf(wlyyJobConfigVO.getIncrementInterval()):"1");
            //往quartz框架添加任务
            params.put("startTime", start);
            params.put("endTime", end);
            for (int j = 1; j <= 2; j++) {
//                if (Integer.valueOf(wlyyJobConfigVO.getId()) > 86 && j == 2) continue;
                params.put("timeLevel", j + "");
                if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                    quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
                    Thread.sleep(sleepTime);
                }
            }
        }
    }
    public String getDate(Date date ,int weekNum,int day){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum); // 设置为2016年的第10周
        cal.set(Calendar.DAY_OF_WEEK, day); // 1表示周日,2表示周一,7表示周六
        return sf.format(cal.getTime());
    }
}

+ 177 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/Contant.java

@ -0,0 +1,177 @@
package com.yihu.wlyy.statistics.util;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by chenweida on 2017/6/1.
 */
public class Contant {
    //抽取数据的开始时间
    public static String startTime = "${start_time}";
    //抽取数据的结束时间
    public static String endTime = "${end_time}";
    //数据库类型
    public static class db_type {
        public static String oracle = "oracle";
        public static String mysql = "mysql";
    }
    /**
     * DateModel的map的key常量
     */
    public static class extract {
        public static final String computeKey1 = "oneKey";
        public static final String computeKey2 = "senondKey";
    }
    /**
     * 运算常量
     */
    public static class compute {
        public static final String add = "1"; //累加
        public static final String division = "2"; //相除
    }
    /**
     * 主维度常量
     */
    public static class main_dimension {
        public static final String time_day = "1";//时间维度 日
        public static final String time_week = "2";//时间维度  月
        public static final String time_month = "3";//时间维度 周
        public static final String time_year = "4";//时间维度 年
        public static final String area_province = "5";//行政区划 省
        public static final String area_city = "6";//行政区划 市
        public static final String area_town = "7";//行政区划 区县
        public static final String area_org = "8";//行政区划 机构
        public static final String area_team = "9";//行政区划  团队
    }
    /**
     * areaLevel 具体的值
     */
    public static class main_dimension_areaLevel {
        public static final String area_province = "1";//行政区划 省
        public static final String area_city = "2";//行政区划 市
        public static final String area_town = "3";//行政区划 区县
        public static final String area_org = "4";//行政区划 机构
        public static final String area_team = "5";//行政区划  团队
        public static String getAreaLevelByMainDimension(String key) {
            switch (key) {
                case main_dimension.area_province: {
                    return area_province;
                }
                case main_dimension.area_city: {
                    return area_city;
                }
                case main_dimension.area_town: {
                    return area_town;
                }
                case main_dimension.area_org: {
                    return area_org;
                }
                case main_dimension.area_team: {
                    return area_team;
                }
            }
            return "";
        }
    }
    /**
     * 主维度 时间维度
     */
    public static class main_dimension_timeLevel {
        public static final String year = "1";
        public static final String month = "2";
        public static final String week = "3";
        public static final String day = "4";
    }
    /**
     * 从维度常量
     */
    public static class slave_dimension {
        public static final String sex = "1";//性别
        public static final String age = "2";//年龄段
    }
    public static class slave_dimension_key {
        public static final String one = "one";
        public static final String two = "two";
    }
    public static class save_status {
        public static final String success = "1";
        public static final String fail = "0";
    }
    /**
     * 数据过滤用到的参量
     */
    public static class role {
        public static final String not_null = "1";//非空
    }
    public static class save {
        public static final String mysql = "1";
        public static final String redis = "2";
        public static final String es = "3";
    }
    public static class convert{
        public static String level_age_1="1";
        public static String level_age_2="2";
        public static String level_age_3="3";
        public static String level_age_4="4";
        public static String level_age_5="5";
        public static String level_age_6="6";
        public static String level_age_1_name="0~6";
        public static String level_age_2_name="7~18";
        public static String level_age_3_name="19~30";
        public static String level_age_4_name="31~50";
        public static String level_age_5_name="51~65";
        public static String level_age_6_name=">65";
    }
    public static class prescriptionStatus{
        public static String status_1="1";//已完成
        public static String status_2="2";//已取消
        public static String status_3="3";//审核不通过
        public static String status_4="4";//进行中
        public static String status_5="5";//其他原因取消
//        public static String status_6="6";
//        public static String status_7="7";
//        public static String status_8="8";
//        public static String status_9="9";
//        public static String status_10="10";
//        public static String status_11="11";
//        public static String status_12="12";
//        public static String status_13="13";
//        public static String status_1_name="续方取消";
//        public static String status_2_name="审核不通过";
//        public static String status_3_name="审核中";
//        public static String status_4_name="药师审核中";
//        public static String status_5_name="药师审核失败";
//        public static String status_6_name="开方中/药师审核成功";
//        public static String status_7_name="开方失败/预结算失败";
//        public static String status_8_name="待支付";
//        public static String status_9_name="支付成功/待配药";
//        public static String status_10_name="等待领药";
//        public static String status_11_name="配送中";
//        public static String status_12_name="已完成";
//        public static String status_13_name="进行中";
    }
}

+ 818 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/DateUtil.java

@ -0,0 +1,818 @@
package com.yihu.wlyy.statistics.util;
import org.apache.commons.lang3.StringUtils;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
public class DateUtil {
	public static final String HH_MM = "HH:mm";
	public static final String HH_MM_SS = "HH:mm:ss";
	public static final String YY = "yy";
	public static final String YYYYMM = "yyyyMM";
	public static final String YYYYMMDD = "yyyyMMdd";
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	/**
	 * 字符串转时间格式
	 */
	public static Date strToDate(String strDate) {
		if (StringUtils.isEmpty(strDate)) {
			return null;
		}
		else{
			int length = strDate.length();
			if(strDate.contains("-"))
			{
				if(length == 10)
				{
					 return strToDate(strDate,YYYY_MM_DD);
				}
				else if(length == 19)
				{
					return strToDate(strDate,YYYY_MM_DD_HH_MM_SS);
				}
			}
			else{
				if(length == 8)
				{
					return strToDate(strDate,YYYYMMDD);
				}
				else if(length == 14)
				{
					return strToDate(strDate,YYYYMMDDHHMMSS);
				}
			}
		}
		return null;
	}
	/**
	  * 获取现在时间
	  * 
	  * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	  */
	public static Date getNowDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		String dateString = formatter.format(currentTime);
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(dateString, pos);
	}
	/**
	 * 获取现在时间
	 * 
	 * @return返回短时间格式 yyyy-MM-dd
	 */
	public static Date getNowDateShort() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
		String dateString = formatter.format(currentTime);
		return strToDate(dateString, YYYY_MM_DD);
	}
	/**
	 * 获取现在时间
	 * 
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		return formatter.format(currentTime);
	}
	/**
	 * 获取现在时间
	 * 
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate(String format) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(currentTime);
	}
	/**
	 * 获取现在时间
	 * 
	 * @return 返回短时间字符串格式yyyy-MM-dd
	 */
	public static String getStringDateShort() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
		return formatter.format(currentTime);
	}
	/**
	 * 获取时间 小时:分;秒 HH:mm:ss
	 * 
	 * @return
	 */
	public static String getTimeShort() {
		SimpleDateFormat formatter = new SimpleDateFormat(HH_MM_SS);
		Date currentTime = new Date();
		return formatter.format(currentTime);
	}
	/**
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDateLong(String strDate) {
		if (StringUtils.isEmpty(strDate)) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(strDate, pos);
	}
	public static Date strToDateShort(String strDate) {
		if (StringUtils.isEmpty(strDate)) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(strDate, pos);
	}
	/**
	 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToStrLong(Date dateDate) {
		if (dateDate == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		return formatter.format(dateDate);
	}
	/**
	 * 将长时间格式时间转换为字符串 yyyy-MM-dd
	 *
	 * @param dateDate
	 * @return
	 */
	public static String dateToStrShort(Date dateDate) {
		if (dateDate == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
		return formatter.format(dateDate);
	}
	/**
	 * 将短时间格式时间转换为字符串 yyyy-MM-dd
	 *
	 * @param dateDate
	 * @param
	 * @return
	 */
	public static String dateToStr(Date dateDate, String format) {
		if (dateDate == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(dateDate);
	}
	/**
	 * 将短时间格式字符串转换为时间
	 *
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate, String format) {
		if (StringUtils.isEmpty(strDate)) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(strDate, pos);
	}
	public static Date strToDateAppendNowTime(String strDate, String format) {
		if (StringUtils.isEmpty(strDate)) {
			return null;
		}
		strDate += " " + getTimeShort();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(strDate, pos);
	}
	/**
	 * 得到现在时间
	 *
	 * @return
	 */
	public static Date getNow() {
		Date currentTime = new Date();
		return currentTime;
	}
	/**
	 * 提取一个月中的最后一天
	 *
	 * @param day
	 * @return
	 */
	public static Date getLastDate(long day) {
		Date date = new Date();
		long date_3_hm = date.getTime() - 3600000 * 34 * day;
		Date date_3_hm_date = new Date(date_3_hm);
		return date_3_hm_date;
	}
	/**
	 * 得到现在时间
	 *
	 * @return 字符串 yyyyMMdd HHmmss
	 */
	public static String getStringToday() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}
	/**
	 * 得到现在小时
	 */
	public static String getHour() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		String dateString = formatter.format(currentTime);
		String hour;
		hour = dateString.substring(11, 13);
		return hour;
	}
	/**
	 * 得到现在分钟
	 *
	 * @return
	 */
	public static String getTime() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		String dateString = formatter.format(currentTime);
		String min;
		min = dateString.substring(14, 16);
		return min;
	}
	/**
	 * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
	 *
	 * @param sformat
	 *            yyyyMMddhhmmss
	 * @return
	 */
	public static String getUserDate(String sformat) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(sformat);
		String dateString = formatter.format(currentTime);
		return dateString;
	}
	/**
	 * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
	 */
	public static String getTwoHour(String st1, String st2) {
		String[] kk = null;
		String[] jj = null;
		kk = st1.split(":");
		jj = st2.split(":");
		if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
			return "0";
		else {
			double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
			double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
			if ((y - u) > 0)
				return y - u + "";
			else
				return "0";
		}
	}
	/**
	 * 得到二个日期间的间隔天数
	 */
	public static String getTwoDay(String sj1, String sj2) {
		SimpleDateFormat myFormatter = new SimpleDateFormat(YYYY_MM_DD);
		long day = 0;
		try {
			Date date = myFormatter.parse(sj1);
			Date mydate = myFormatter.parse(sj2);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		} catch (Exception e) {
			return "";
		}
		return day + "";
	}
	/**
	 * 时间前推或后推分钟,其中JJ表示分钟.
	 */
	public static String getPreTime(String sj1, String jj) {
		SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		String mydate1 = "";
		try {
			Date date1 = format.parse(sj1);
			long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
			date1.setTime(Time * 1000);
			mydate1 = format.format(date1);
		} catch (Exception e) {
		}
		return mydate1;
	}
	/**
	 * 得到一个时间延后或前移几分钟的时间,nowdate为时间,delay为前移或后延的分钟数
	 */
	public static Date getNextMin(Date date, int delay) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MINUTE, delay);
			return cal.getTime();
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
	 */
	public static String getNextDay(String nowdate, int delay) {
		try {
			SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
			String mdate = "";
			Date d = strToDate(nowdate, YYYY_MM_DD);
			long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
			d.setTime(myTime * 1000);
			mdate = format.format(d);
			return mdate;
		} catch (Exception e) {
			return "";
		}
	}
	// public static String getNextDay(Date d, int delay) {
	// try {
	// SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
	// String mdate = "";
	// long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
	// d.setTime(myTime * 1000);
	// mdate = format.format(d);
	// return mdate;
	// } catch (Exception e) {
	// return "";
	// }
	// }
	public static String getNextDay(Date d, int days) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.DATE, days);
		return dateToStrShort(c.getTime());
	}
	public static String getNextMonth(Date d, int months) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.MONTH, months);
		return dateToStrShort(c.getTime());
	}
	public static String getNextYear(Date d, int year) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.YEAR, year);
		return dateToStrShort(c.getTime());
	}
	/**
	 * 判断是否润年
	 *
	 * @param ddate
	 * @return
	 */
	public static boolean isLeapYear(String ddate) {
		/**
		 * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
		 * 3.能被4整除同时能被100整除则不是闰年
		 */
		Date d = strToDate(ddate, YYYY_MM_DD);
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(d);
		int year = gc.get(Calendar.YEAR);
		if ((year % 400) == 0)
			return true;
		else if ((year % 4) == 0) {
			if ((year % 100) == 0)
				return false;
			else
				return true;
		} else
			return false;
	}
	/**
	 * 返回美国时间格式 26 Apr 2006
	 *
	 * @param str
	 * @return
	 */
	public static String getEDate(String str) {
		SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(str, pos);
		String j = strtodate.toString();
		String[] k = j.split(" ");
		return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
	}
	/**
	 * 获取一个月的最后一天
	 *
	 * @param dat
	 * @return
	 */
	public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
		String str = dat.substring(0, 8);
		String month = dat.substring(5, 7);
		int mon = Integer.parseInt(month);
		if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
			str += "31";
		} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
			str += "30";
		} else {
			if (isLeapYear(dat)) {
				str += "29";
			} else {
				str += "28";
			}
		}
		return str;
	}
	/**
	 * 判断二个时间是否在同一个周
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameWeekDates(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (0 == subYear) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
			// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}
	/**
	 * 产生周序列,即得到当前时间所在的年度是第几周
	 *
	 * @return
	 */
	public static String getSeqWeek() {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
		if (week.length() == 1)
			week = "0" + week;
		String year = Integer.toString(c.get(Calendar.YEAR));
		return year + week;
	}
	/**
	 * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
	 *
	 * @param sdate
	 * @param num
	 * @return
	 */
	public static String getWeek(String sdate, String num) {
		// 再转换为时间
		Date dd = DateUtil.strToDate(sdate, YYYY_MM_DD);
		Calendar c = Calendar.getInstance();
		c.setTime(dd);
		if (num.equals("1")) // 返回星期一所在的日期
			c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		else if (num.equals("2")) // 返回星期二所在的日期
			c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
		else if (num.equals("3")) // 返回星期三所在的日期
			c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		else if (num.equals("4")) // 返回星期四所在的日期
			c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
		else if (num.equals("5")) // 返回星期五所在的日期
			c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		else if (num.equals("6")) // 返回星期六所在的日期
			c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
		else if (num.equals("0")) // 返回星期日所在的日期
			c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}
	/**
	 * 根据一个日期,返回是星期几的字符串
	 *
	 * @param sdate
	 * @return
	 */
	public static String getWeek(String sdate) {
		// 再转换为时间
		Date date = DateUtil.strToDate(sdate, YYYY_MM_DD);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		// int hour=c.get(Calendar.DAY_OF_WEEK);
		// hour中存的就是星期几了,其范围 1~7
		// 1=星期日 7=星期六,其他类推
		return new SimpleDateFormat("EEEE").format(c.getTime());
	}
	public static String getWeekStr(String sdate) {
		String str = "";
		str = DateUtil.getWeek(sdate);
		if ("1".equals(str)) {
			str = "星期日";
		} else if ("2".equals(str)) {
			str = "星期一";
		} else if ("3".equals(str)) {
			str = "星期二";
		} else if ("4".equals(str)) {
			str = "星期三";
		} else if ("5".equals(str)) {
			str = "星期四";
		} else if ("6".equals(str)) {
			str = "星期五";
		} else if ("7".equals(str)) {
			str = "星期六";
		}
		return str;
	}
	/**
	 * 两个时间之间的天数
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getDays(String date1, String date2) {
		if (date1 == null || date1.equals(""))
			return 0;
		if (date2 == null || date2.equals(""))
			return 0;
		// 转换为标准时间
		SimpleDateFormat myFormatter = new SimpleDateFormat(YYYY_MM_DD);
		Date date = null;
		Date mydate = null;
		try {
			date = myFormatter.parse(date1);
			mydate = myFormatter.parse(date2);
		} catch (Exception e) {
		}
		long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		return day;
	}
	/**
	 * 返回两个日期相差的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getDays(Date date1, Date date2) {
		if (date1 == null || date2 == null)
			return 0;
		long day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);
		return day;
	}
	/**
	 * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
	 * 此函数返回该日历第一行星期日所在的日期
	 * 
	 * @param sdate
	 * @return
	 */
	public static String getNowMonth(String sdate) {
		// 取该时间所在月的一号
		sdate = sdate.substring(0, 8) + "01";
		// 得到这个月的1号是星期几
		Date date = DateUtil.strToDate(sdate, YYYY_MM_DD);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int u = c.get(Calendar.DAY_OF_WEEK);
		String newday = DateUtil.getNextDay(sdate, 1 - u);
		return newday;
	}
	/**
	 * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
	 * 
	 * @param k 表示是取几位随机数,可以自己定
	 */
	public static String getNo(int k) {
		return getUserDate("yyyyMMddhhmmss") + getRandom(k);
	}
	/**
	 * 返回一个随机数
	 * 
	 * @param i
	 * @return
	 */
	public static String getRandom(int i) {
		Random jjj = new Random();
		if (i == 0)
			return "";
		String jj = "";
		for (int k = 0; k < i; k++) {
			jj = jj + jjj.nextInt(9);
		}
		return jj;
	}
	/**
	 * 根据用户生日计算年龄
	 */
	public static int getAgeByBirthday(Date birthday) {
		try {
			Calendar cal = Calendar.getInstance();
			if (cal.before(birthday)) {
				return 0;
			}
			int yearNow = cal.get(Calendar.YEAR);
			int monthNow = cal.get(Calendar.MONTH) + 1;
			int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
			cal.setTime(birthday);
			int yearBirth = cal.get(Calendar.YEAR);
			int monthBirth = cal.get(Calendar.MONTH) + 1;
			int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
			int age = yearNow - yearBirth;
			if (monthNow <= monthBirth) {
				if (monthNow == monthBirth) {
					if (dayOfMonthNow < dayOfMonthBirth) {
						age--;
					}
				} else {
					age--;
				}
			}
			return age;
		} catch (Exception e) {
			return 0;
		}
	}
	public static void main(String[] args) {
//		String hour = "12:22:12";
//		System.out.println(Time.valueOf(hour));
//		System.out.println(getNowDate());
//
		System.out.println(getNextYear(new Date(), -65));
	}
	public static int getNowMonth(){
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.MONTH)+1;
	}
	public static int getNowYear(){
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
	/**
	 * 获取周一
	 * @return
	 */
	public static String getMondayOfThisDate(Date date) {
		SimpleDateFormat df2 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		return df2.format(getMondayOfThisDayToDate(date));
	}
	/**
	 * 获取周一
	 * @return
	 */
	public static Date getMondayOfThisDayToDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 1);
		return c.getTime();
	}
    /**
     * 得到本周周日
     *
     * @return yyyy-MM-dd
     */
    public static String getSundayOfThisDate(Date date) {
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return df2.format(c.getTime());
    }
	/**
	 * 获取当月第一天
	 * @return
	 */
	public static String getFristDayOfMonth(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(getFristDayOfMonthToDate(date));
	}
	public static Date getFristDayOfMonthToDate(Date date) {
		// 获取前月的第一天
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
		return c.getTime();
	}
	/**
	 * 获取当月的最后一天
	 * @param date
	 * @return
	 */
	public static String getLastDayOfMonth(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(getLastDayOfMonthToDate(date));
	}
	public static Date getLastDayOfMonthToDate(Date date){
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		return ca.getTime();
	}
	//获取输入时间是当前年度的第几周
	public static Integer week(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}
	//获取输入时间是当前年度的第几周
	public static Integer month(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH);
	}
	/**
	 * 获取某年最后一天日期
	 * @param year 年份
	 * @return Date
	 */
	public static Date getYearLast(int year){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();
		return currYearLast;
	}
}

+ 578 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/ElasticsearchUtil.java

@ -0,0 +1,578 @@
package com.yihu.wlyy.statistics.util;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.yihu.wlyy.statistics.etl.save.es.ElasticFactory;
import com.yihu.wlyy.statistics.vo.DataModel;
import com.yihu.wlyy.statistics.vo.SaveModel;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.nlpcn.es4sql.domain.Select;
import org.nlpcn.es4sql.jdbc.ObjectResult;
import org.nlpcn.es4sql.jdbc.ObjectResultsExtractor;
import org.nlpcn.es4sql.parse.ElasticSqlExprParser;
import org.nlpcn.es4sql.parse.SqlParser;
import org.nlpcn.es4sql.query.AggregationQueryAction;
import org.nlpcn.es4sql.query.DefaultQueryAction;
import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * Created by chenweida on 2017/7/17.
 * SELECT town,townName,sum(result1) result1 FROM wlyy_quota_test
 * where quotaCode='1'
 * group by town,townName , date_histogram(field='quotaDate','interval'='week')
 */
@Component
public class ElasticsearchUtil {
    private Logger logger= LoggerFactory.getLogger(ElasticsearchUtil.class);
    @Autowired
    private ElasticFactory elasticFactory;
    @Value("${es.type}")
    private String esType;
    @Value("${es.index}")
    private String esIndex;
    /**
     * 折线图
     *
     * @param quotaCode 指标quotacode
     * @param code      机构code,或者区code或者团队code或者城市code
     * @param startDate 开始日期 yyyy-MM-dd
     * @param endDate   结束日期 yyyy-MM-dd
     * @param timeLevel 1增量 2到达量
     * @param areaLevel 1 省 2 市 3 区县 4 机构 5团队
     * @param interval  1日 2周 3月
     * @return
     */
    public List<SaveModel> findQuotaLines(String quotaCode,
                                          String code,
                                          String startDate,
                                          String endDate,
                                          String timeLevel,
                                          String areaLevel,
                                          String interval) {
        //时间格式转换  yyyy-MM-dd转成 2017-07-17T00:00:00+0800
        startDate = changeDate(startDate);
        //时间格式转换  yyyy-MM-dd转成 2017-07-17T00:00:00+0800
        endDate = changeDate(endDate);
        StringBuffer sql = new StringBuffer();
        StringBuffer groupBy = new StringBuffer();
        if (SaveModel.teamLevel.equals(areaLevel)) {
            sql.append("select team,teamName,result1,result2 from "+esType+" where team='" + code + "'");
        } else if (SaveModel.OrgLevel.equals(areaLevel)) {
            sql.append("select hospital,hospitalName,sum(result1) result1,sum(result2) result2 from "+esType+" where hospital='" + code + "'");
            groupBy.append(" group by hospital,hospitalName");
        } else if (SaveModel.townLevel.equals(areaLevel)) {
            sql.append("select town,townName,sum(result1) result1,sum(result2) result2 from "+esType+" where town='" + code + "'");
            groupBy.append(" group by town,townName");
        } else if (SaveModel.cityLevel.equals(areaLevel)) {
            sql.append("select city,cityName,sum(result1) result1,sum(result2) result2 from "+esType+" where city='" + code + "'");
            groupBy.append(" group by city,cityName");
        }
        sql.append(" and quotaCode='" + quotaCode + "'  ");
        sql.append(" and timeLevel='" + timeLevel + "'  ");
        sql.append(" and areaLevel='5'");
        sql.append(" and quotaDate >='" + startDate + "'  ");
        sql.append(" and quotaDate <='" + endDate + "'  ");
        //根据时间维度分组
        if (SaveModel.interval_month.equals(interval)) {
            groupBy.append(" ,date_histogram(field='quotaDate','interval'='month') ");
        } else if (SaveModel.interval_week.equals(interval)) {
            groupBy.append(" ,date_histogram(field='quotaDate','interval'='week') ");
        } else if (SaveModel.interval_day.equals(interval)) {
            groupBy.append(" ,date_histogram(field='quotaDate','interval'='1d') ");
        }
        sql.append(groupBy);
        return excute(sql.toString());
    }
    /**
     * 查询某个指标某一天的量
     *
     * @param quotaCode 指标quotacode
     * @param quotaDate 时间 yyyy-MM-dd
     * @param timeLevel 1增量 2到达量
     * @param areaLevel 1 省 2 市 3 区县 4 机构 5团队
     * @return
     */
    public List<SaveModel> findOneDateQuota(String quotaCode,
                                            String code,
                                            String quotaDate,
                                            String timeLevel,
                                            String areaLevel) {
        //时间格式转换  yyyy-MM-dd转成 2017-07-17T00:00:00+0800
        quotaDate = changeDate(quotaDate);
        StringBuffer sql = new StringBuffer();
        StringBuffer groupBy = new StringBuffer();
        if (SaveModel.teamLevel.equals(areaLevel)) {
            sql.append("select team,teamName,result1,result2 from "+esType+" where team='" + code + "'");
        } else if (SaveModel.OrgLevel.equals(areaLevel)) {
            sql.append("select hospital,hospitalName,sum(result1) result1,sum(result2) result2 from "+esType+" where hospital='" + code + "'");
            groupBy.append(" group by hospital,hospitalName");
        } else if (SaveModel.townLevel.equals(areaLevel)) {
            sql.append("select town,townName,sum(result1) result1,sum(result2) result2 from "+esType+" where town='" + code + "'");
            groupBy.append(" group by town,townName");
        } else if (SaveModel.cityLevel.equals(areaLevel)) {
            sql.append("select city,cityName,sum(result1) result1,sum(result2) result2 from "+esType+" where city='" + code + "'");
            groupBy.append(" group by city,cityName");
        }
        sql.append(" and quotaCode='" + quotaCode + "'  ");
        sql.append(" and timeLevel='" + timeLevel + "'  ");
        sql.append(" and areaLevel='5'");
        sql.append(" and quotaDate='" + quotaDate + "'");
        sql.append(groupBy);
        return excute(sql.toString());
    }
    /**
     * 查询某个2级维度指标某一天的数据
     *
     * @param quotaCode 指标quotacode
     * @param quotaDate 时间 yyyy-MM-dd
     * @param code      机构code或者团队code或者town code或者city code
     * @param timeLevel 1增量 2到达量
     * @param areaLevel 1 省 2 市 3 区县 4 机构 5团队
     * @return
     */
    public List<SaveModel> findOneDateQuotaLevel2(String quotaCode,
                                                  String code,
                                                  String quotaDate,
                                                  String timeLevel,
                                                  String areaLevel) {
        //时间格式转换  yyyy-MM-dd转成 2017-07-17T00:00:00+0800
        quotaDate = changeDate(quotaDate);
        StringBuffer sql = new StringBuffer();
        StringBuffer groupBy = new StringBuffer();
        if (SaveModel.teamLevel.equals(areaLevel)) {
            sql.append("select team,teamName,slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name,result1,result2 from "+esType+" where team='" + code + "'");
            groupBy.append("  group by slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name");
        } else if (SaveModel.OrgLevel.equals(areaLevel)) {
            sql.append("select hospital,hospitalName,slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name,sum(result1) result1,sum(result2) result2 from "+esType+" where hospital='" + code + "'");
            groupBy.append("  group by slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name");
        } else if (SaveModel.townLevel.equals(areaLevel)) {
            sql.append("select town,townName,slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name,sum(result1) result1,sum(result2) result2 from "+esType+" where town='" + code + "'");
            groupBy.append("  group by slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name");
        } else if (SaveModel.cityLevel.equals(areaLevel)) {
            sql.append("select city,cityName,slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name,sum(result1) result1,sum(result2) result2 from "+esType+" where city='" + code + "'");
            groupBy.append("  group by slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name");
        }
        sql.append(" and quotaCode='" + quotaCode + "'  ");
        sql.append(" and timeLevel='" + timeLevel + "'  ");
        sql.append(" and areaLevel='5'");
        sql.append(" and quotaDate='" + quotaDate + "'");
        sql.append(groupBy);
        return excute(sql.toString());
    }
    /**
     * 查询某个一级维度指标某一天的数据
     *
     * @param quotaCode 指标quotacode
     * @param quotaDate 时间 yyyy-MM-dd
     * @param code      机构code或者团队code或者town code或者city code
     * @param timeLevel 1增量 2到达量
     * @param areaLevel 1 省 2 市 3 区县 4 机构 5团队
     * @return
     */
    public List<SaveModel> findOneDateQuotaLevel1(String quotaCode,
                                                  String code,
                                                  String quotaDate,
                                                  String timeLevel,
                                                  String areaLevel) {
        //时间格式转换  yyyy-MM-dd转成 2017-07-17T00:00:00+0800
        quotaDate = changeDate(quotaDate);
        StringBuffer sql = new StringBuffer();
        StringBuffer groupBy = new StringBuffer();
        if (SaveModel.teamLevel.equals(areaLevel)) {
            sql.append("select team,teamName,slaveKey1,slaveKey1Name,result1,result2 from "+esType+" where team='" + code + "'");
            groupBy.append("  group by team,teamName,slaveKey1,slaveKey1Name");
        } else if (SaveModel.OrgLevel.equals(areaLevel)) {
            sql.append("select hospital,hospitalName,slaveKey1,slaveKey1Name,sum(result1) result1,sum(result2) result2 from "+esType+" where hospital='" + code + "'");
            groupBy.append("  group by hospital,hospitalName,slaveKey1,slaveKey1Name");
        } else if (SaveModel.townLevel.equals(areaLevel)) {
            sql.append("select town,townName,slaveKey1,slaveKey1Name,sum(result1) result1,sum(result2) result2 from "+esType+" where town='" + code + "'");
            groupBy.append("  group by town,townName,slaveKey1,slaveKey1Name");
        } else if (SaveModel.cityLevel.equals(areaLevel)) {
            sql.append("select city,cityName,slaveKey1,slaveKey1Name,sum(result1) result1,sum(result2) result2 from "+esType+" where city='" + code + "'");
            groupBy.append("  group by city,cityName,slaveKey1,slaveKey1Name");
        }
        sql.append(" and quotaCode='" + quotaCode + "'  ");
        sql.append(" and timeLevel='" + timeLevel + "'  ");
        sql.append(" and areaLevel='5'");
        sql.append(" and quotaDate='" + quotaDate + "'");
        sql.append(groupBy);
        return excute(sql.toString());
    }
    /**
     * 查询某一天父level下的子level 例如 查询市下面的团队,或者区下面的团队
     *
     * @param quotaCode      指标code
     * @param code           机构code或者团队code或者town code或者city code
     * @param quotaDate      指标code
     * @param timeLevel      1增量 2到达量
     * @param areaLevel      父arealevel
     * @param childAreaLevel 子arealevel
     * @return
     */
    public List<SaveModel> findOneDateQuotaByChllevel(String quotaCode,
                                                      String code,
                                                      String quotaDate,
                                                      String timeLevel,
                                                      String areaLevel,
                                                      String childAreaLevel) {
        //时间格式转换  yyyy-MM-dd转成 2017-07-17T00:00:00+0800
        quotaDate = changeDate(quotaDate);
        StringBuffer sql = new StringBuffer();
        StringBuffer groupBy = new StringBuffer();
        //根据 childAreaLevel   group by
        if (SaveModel.teamLevel.equals(childAreaLevel)) {
            sql.append("select team,teamName,result1,result2 from "+esType+" where  ");
        } else if (SaveModel.OrgLevel.equals(childAreaLevel)) {
            sql.append("select hospital,hospitalName,sum(result1) result1,sum(result2) result2 from "+esType+" where  ");
            groupBy.append(" group by hospital,hospitalName");
        } else if (SaveModel.townLevel.equals(childAreaLevel)) {
            sql.append("select town,townName,sum(result1) result1,sum(result2) result2 from "+esType+" where ");
            groupBy.append(" group by town,townName");
        } else if (SaveModel.cityLevel.equals(childAreaLevel)) {
            sql.append("select city,cityName,sum(result1) result1,sum(result2) result2 from "+esType+" where  ");
            groupBy.append(" group by city,cityName");
        }
        sql.append("  quotaCode='" + quotaCode + "'  ");
        sql.append(" and timeLevel='" + timeLevel + "'  ");
        sql.append(" and areaLevel='5'");
        sql.append(" and quotaDate='" + quotaDate + "'");
        //查询code
        if (SaveModel.teamLevel.equals(areaLevel)) {
            sql.append(" and team='" + code + "'");
        } else if (SaveModel.OrgLevel.equals(areaLevel)) {
            sql.append(" and hospital='" + code + "'");
        } else if (SaveModel.townLevel.equals(areaLevel)) {
            sql.append(" and town='" + code + "'");
        } else if (SaveModel.cityLevel.equals(areaLevel)) {
            sql.append(" and city='" + code + "'");
        }
        sql.append(groupBy);
        return excute(sql.toString());
    }
    /**
     * 时间格式转换  yyyy-MM-dd转成 2017-07-17T00:00:00+0800
     *
     * @param quotaDate
     */
    private String changeDate(String quotaDate) {
        return quotaDate + "T00:00:00+0800";
    }
    /**
     * 执行sql查询es
     *
     * @param sql
     * @return
     */
    public List<SaveModel> excute(String sql) {
        List<SaveModel> saveModels = new ArrayList<>();
        try {
            SQLExprParser parser = new ElasticSqlExprParser(sql);
            SQLExpr expr = parser.expr();
            SQLQueryExpr queryExpr = (SQLQueryExpr) expr;
//            if (parser.getLexer().token() != Token.EOF) {
//                throw new ParserException("illegal sql expr : " + sql);
//            }
            Select select = null;
            select = new SqlParser().parseSelect(queryExpr);
            //通过抽象语法树,封装成自定义的Select,包含了select、from、where group、limit等
            AggregationQueryAction action = null;
            DefaultQueryAction queryAction = null;
            SqlElasticSearchRequestBuilder requestBuilder = null;
            if (select.isAgg) {
                //包含计算的的排序分组的
                action = new AggregationQueryAction(elasticFactory.getTransportClient(), select);
                requestBuilder = action.explain();
            } else {
                //封装成自己的Select对象
                Client client = elasticFactory.getTransportClient();
                queryAction = new DefaultQueryAction(client, select);
                requestBuilder = queryAction.explain();
            }
            SearchResponse response = (SearchResponse) requestBuilder.get();
            Object queryResult = null;
            if(sql.toUpperCase().indexOf("GROUP")!=-1||sql.toUpperCase().indexOf("SUM")!=-1){
                queryResult = response.getAggregations();
            }else{
                queryResult = response.getHits();
            }
            ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
            List<String> heads = temp.getHeaders();
            temp.getLines().stream().forEach(one -> {
                try {
                    SaveModel saveModel = new SaveModel();
                    for (int i = 0; i < one.size(); i++) {
                        String key = null;
                        Object value = one.get(i);
                        if(heads.get(i).startsWith("_")){
                            continue;
                        }
                        if (heads.get(i).contains("date_histogram")) {
                            key = "setQuotaDate";
                            value=DateUtil.strToDate(String.valueOf(value),"yyyy-MM-dd HH:mm:ss");
                        } else {
                            key = "set" + UpFirstStr(heads.get(i));
                        }
                        if (value instanceof String) {
                            SaveModel.class.getMethod(key, String.class).invoke(saveModel, value);
                        } else if (value instanceof Integer) {
                            SaveModel.class.getMethod(key, Integer.class).invoke(saveModel, value);
                        } else if (value instanceof Double) {
                            SaveModel.class.getMethod(key, Integer.class).invoke(saveModel, ((Double) value).intValue());
                        } else if (value instanceof java.util.Date) {
                            SaveModel.class.getMethod(key, java.util.Date.class).invoke(saveModel, value);
                        }
                    }
                    saveModels.add(saveModel);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return saveModels;
    }
    public<T> List<T> excute(String sql, Class<T> clazz) {
        List saveModels = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXX");
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //解决 group by之后默认是200的问题
            if (sql.toLowerCase().contains("group by")) {
                sql = sql + " limit 0,2000";
            }
            SQLExprParser parser = new ElasticSqlExprParser(sql);
            SQLExpr expr = parser.expr();
            SQLQueryExpr queryExpr = (SQLQueryExpr) expr;
            Select select = null;
            select = new SqlParser().parseSelect(queryExpr);
            //通过抽象语法树,封装成自定义的Select,包含了select、from、where group、limit等
            AggregationQueryAction action = null;
            DefaultQueryAction queryAction = null;
            SqlElasticSearchRequestBuilder requestBuilder = null;
            if (select.isAgg) {
                //包含计算的的排序分组的
                action = new AggregationQueryAction(elasticFactory.getTransportClient(), select);
                requestBuilder = action.explain();
            } else {
                //封装成自己的Select对象
                queryAction = new DefaultQueryAction(elasticFactory.getTransportClient(), select);
                requestBuilder = queryAction.explain();
            }
            SearchResponse response = (SearchResponse) requestBuilder.get();
            Object queryResult = null;
            if (sql.toUpperCase().indexOf("GROUP") != -1 || sql.toUpperCase().indexOf("SUM") != -1 || select.isAgg) {
                queryResult = response.getAggregations();
            } else {
                queryResult = response.getHits();
            }
            ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
            List<String> heads = temp.getHeaders();
            temp.getLines().forEach(one -> {
                Object saveModel = null;
                try {
                    saveModel = clazz.newInstance();
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }
                for (int i = 0; i < one.size(); i++) {
                    try {
                        String key = null;
                        Object value = one.get(i);
                        if (heads.get(i).startsWith("_")) {
                            continue;
                        }
                        key = "set" + UpFirstStr(heads.get(i));
                        if (heads.get(i).contains("quotaDate") || heads.get(i).contains("createTime") || heads.get(i).contains("date_histogram")) {
                            if (heads.get(i).contains("date_histogram")) {
                                key = "setQuotaDate";
                            }
                            try {
                                //yyyy-MM-dd'T'HH:mm:ssXX
                                value = dateFormat.parse(String.valueOf(one.get(i)));
                            } catch (Exception e) {
                                //yyyy-MM-dd HH:mm:ss
                                try {
                                    value = dateFormat1.parse(String.valueOf(one.get(i)));
                                }catch (Exception e1){
                                    Timestamp ts = new Timestamp(Long.parseLong(String.valueOf(one.get(i))));
                                    try {
                                        Date date = new Date();
                                        date = ts;
                                        value =date;
                                    } catch (Exception e2) {
                                        value = String.valueOf(one.get(i));
                                    }
                                }
                            }
//                            value = DateUtil.strToDate(String.valueOf(value).replace("T00:00:00+0800", " 00:00:00"), "yyyy-MM-dd HH:mm:ss");
                        }
                        if (value instanceof String) {
                            clazz.getMethod(key, String.class).invoke(saveModel, value);
                        } else if (value instanceof Integer) {
                            clazz.getMethod(key, Integer.class).invoke(saveModel, value);
                        } else if (value instanceof Double) {
                            clazz.getMethod(key, Double.class).invoke(saveModel, value);
                        } else if (value instanceof java.util.Date) {
                            clazz.getMethod(key, java.util.Date.class).invoke(saveModel, value);
                        } else if (value instanceof java.util.List) {
                            clazz.getMethod(key, java.util.List.class).invoke(saveModel, value);
                        }
                    } catch (Exception e) {
                        logger.warn(e.getMessage());
                    }
                }
                saveModels.add(saveModel);
            });
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return saveModels;
    }
    /**
     * 执行sql查询es
     *
     * @param sql
     * @return
     */
    public List<DataModel> excuteDataModel(String sql) {
        List<DataModel> saveModels = new ArrayList<>();
        try {
            SQLExprParser parser = new ElasticSqlExprParser(sql);
            SQLExpr expr = parser.expr();
            SQLQueryExpr queryExpr = (SQLQueryExpr) expr;
//            if (parser.getLexer().token() != Token.EOF) {
//                throw new ParserException("illegal sql expr : " + sql);
//            }
            Select select = null;
            select = new SqlParser().parseSelect(queryExpr);
            //通过抽象语法树,封装成自定义的Select,包含了select、from、where group、limit等
            AggregationQueryAction action = null;
            DefaultQueryAction queryAction = null;
            SqlElasticSearchRequestBuilder requestBuilder = null;
            if (select.isAgg) {
                //包含计算的的排序分组的
                action = new AggregationQueryAction(elasticFactory.getTransportClient(), select);
                requestBuilder = action.explain();
            } else {
                //封装成自己的Select对象
                Client client = elasticFactory.getTransportClient();
                queryAction = new DefaultQueryAction(client, select);
                requestBuilder = queryAction.explain();
            }
            SearchResponse response = (SearchResponse) requestBuilder.get();
            Object queryResult = null;
            if(sql.toUpperCase().indexOf("GROUP")!=-1||sql.toUpperCase().indexOf("SUM")!=-1){
                queryResult = response.getAggregations();
            }else{
                queryResult = response.getHits();
            }
            ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
            List<String> heads = temp.getHeaders();
            for(List<Object> one:temp.getLines()){
//            temp.getLines().stream().forEach(one -> {
                try {
                    DataModel dataModel = new DataModel();
                    for (int i = 0; i < one.size(); i++) {
                        String key = null;
                        Object value = one.get(i);
                        if(heads.get(i).startsWith("_")){
                            continue;
                        }
                        if (heads.get(i).contains("date_histogram")) {
                            key = "setQuotaDate";
                            value=DateUtil.strToDate(String.valueOf(value),"yyyy-MM-dd HH:mm:ss");
                        } else {
                            key = "set" + UpFirstStr(heads.get(i));
                        }
                        try {
                            if (value instanceof String) {
                                 DataModel.class.getMethod(key, String.class).invoke(dataModel, value);
                            } else if (value instanceof Integer) {
                                DataModel.class.getMethod(key, Integer.class).invoke(dataModel, value);
                            } else if (value instanceof Double) {
                                DataModel.class.getMethod(key, Double.class).invoke(dataModel, value);
                            } else if (value instanceof java.util.Date) {
                                DataModel.class.getMethod(key, java.util.Date.class).invoke(dataModel, value);
                            }
                        }catch (Exception e){
                            logger.error(e.getMessage());
                        }
                    }
                    saveModels.add(dataModel);
                } catch (Exception e) {
                    e.printStackTrace();
                }}
//            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return saveModels;
    }
    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    private String UpFirstStr(String str) {
        return str.replaceFirst(str.substring(0, 1), str.substring(0, 1).toUpperCase());
    }
}

+ 128 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/HttpClientUtil.java

@ -0,0 +1,128 @@
package com.yihu.wlyy.statistics.util;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class HttpClientUtil {
	/**
	 * 发送post请求
	 * @param url 请求地址
	 * @param params 请求参数
	 * @param chatSet 编码格式
	 * @return
	 */
	public static String post(String url, List<NameValuePair> params, String chatSet) {
		// 创建默认的httpClient实例.
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建httppost
		HttpPost httppost = new HttpPost(url);
		UrlEncodedFormEntity uefEntity;
		try {
			uefEntity = new UrlEncodedFormEntity(params, chatSet);
			httppost.setEntity(uefEntity);
			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					return EntityUtils.toString(entity, chatSet);
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 发送get请求
	 * @param url 请求地址
	 * @param chatSet 编码格式
	 * @return
	 */
	public static String get(String url, String chatSet) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			// 创建httpget.
			HttpGet httpget = new HttpGet(url);
			// 执行get请求.
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				// 获取响应实体
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					return EntityUtils.toString(entity, chatSet);
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	public static String postBody(String url,JSONObject params){
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
		String ret = restTemplate.postForObject(url, formEntity, String.class);
		return ret;
	}
	public static void putBody(String url,JSONObject params){
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
		restTemplate.put(url, formEntity, String.class);
	}
}

+ 162 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/IdCardUtil.java

@ -0,0 +1,162 @@
package com.yihu.wlyy.statistics.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * Created by Administrator on 2016.08.17.
 * 身份证工具类
 */
public class IdCardUtil {
    //    /**
//     * 根据身份证的号码算出当前身份证持有者的年龄
//     *
//     * @param
//     * @throws Exception
//     */
//    public static int getAgeForIdcard(String card)
//            throws Exception {
//        int age = 0;
//        if (card.length() == 18) {
//            String year = card.substring(6).substring(0, 4);// 得到年份
//            String yue = card.substring(10).substring(0, 2);// 得到月份
//            // String day=CardCode.substring(12).substring(0,2);//得到日
//            Date date = new Date();// 得到当前的系统时间
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//            String fyear = format.format(date).substring(0, 4);// 当前年份
//            String fyue = format.format(date).substring(5, 7);// 月份
//            if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
//                age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
//            } else {// 当前用户还没过生
//                age = Integer.parseInt(fyear) - Integer.parseInt(year);
//            }
//        } else if (card.length() == 15) {
//            String uyear = "19" + card.substring(6, 8);// 年份
//            String uyue = card.substring(8, 10);// 月份
//            Date date = new Date();// 得到当前的系统时间
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//            String fyear = format.format(date).substring(0, 4);// 当前年份
//            String fyue = format.format(date).substring(5, 7);// 月份
//            // String fday=format.format(date).substring(8,10);
//            if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
//                age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
//            } else {// 当前用户还没过生
//                age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
//            }
//
//        }
//        return age;
//    }
    public static void main(String[] args) throws Exception {
        System.out.println(getSexForIdcard("350206199109092018"));
    }
    /**
     * 根据身份证的号码算出当前身份证持有者的性别
     * 1 女 2 男 3未知
     *
     * @return
     * @throws Exception
     */
    public static String getSexForIdcard(String CardCode)
            throws Exception {
        try {
            String sex = "2";
            if (CardCode.length() == 18) {
                if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
                    // modifid by lyr 2016-09-29
                    // sex =  Constant.level_sex_2;
                    sex = "1";
                    // modifid by lyr 2016-09-29
                } else {
                    // modifid by lyr 2016-09-29
                    // sex =  Constant.level_sex_1;
                    sex = "2";
                    // modifid by lyr 2016-09-29
                }
            } else if (CardCode.length() == 15) {
                String usex = CardCode.substring(14, 15);// 用户的性别
                if (Integer.parseInt(usex) % 2 == 0) {
                    // sex =  Constant.level_sex_2;
                    sex = "1";
                } else {
                    // sex =  Constant.level_sex_1;
                    sex = "2";
                }
            }
            return sex;
        } catch (Exception e) {
            return "1";
        }
    }
    /**
     * 根据身份证的号码算出当前身份证持有者的年龄
     *
     * @param
     * @throws Exception
     */
    public static int getAgeForIdcard(String idcard) {
        try {
            int age = 0;
            if (org.springframework.util.StringUtils.isEmpty(idcard)) {
                return age;
            }
            String birth = "";
            if (idcard.length() == 18) {
                birth = idcard.substring(6, 14);
            } else if (idcard.length() == 15) {
                birth = "19" + idcard.substring(6, 12);
            } else {
                return 0;
            }
            int year = Integer.valueOf(birth.substring(0, 4));
            int month = Integer.valueOf(birth.substring(4, 6));
            int day = Integer.valueOf(birth.substring(6));
            Calendar cal = Calendar.getInstance();
            age = cal.get(Calendar.YEAR) - year;
            //周岁计算
            if (cal.get(Calendar.MONTH) < (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) < day)) {
                age--;
            }
            return age;
        } catch (Exception e) {
            return 0;
        }
    }
    /**
     * 身份证提取出身日期
     *
     * @param card
     * @return
     * @throws Exception
     */
    public static Date getBirthdayForIdcard(String card)
            throws Exception {
        Date b = null;
        if (card.length() == 18) {
            String year = card.substring(6).substring(0, 4);// 得到年份
            String yue = card.substring(10).substring(0, 2);// 得到月份
            String ri = card.substring(12).substring(0, 2);// 得到日
            // String day=CardCode.substring(12).substring(0,2);//得到日
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            b = format.parse(year + "-" + yue + "-" + ri);
        } else if (card.length() == 15) {
            String uyear = "19" + card.substring(6, 8);// 年份
            String uyue = card.substring(8, 10);// 月份
            String uri = card.substring(10, 12);// 得到日
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            b = format.parse(uyear + "-" + uyue + "-" + uri);
        }
        return b;
    }
}

+ 45 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/JsonDateValueProcessor.java

@ -0,0 +1,45 @@
package com.yihu.wlyy.statistics.util;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
 * Created by Administrator on 2016.10.20.
 */
public class JsonDateValueProcessor implements JsonValueProcessor {
    private String format ="yyyy-MM-dd HH:mm:ss";
    public JsonDateValueProcessor() {
        super();
    }
    public JsonDateValueProcessor(String format) {
        super();
        this.format = format;
    }
    @Override
    public Object processArrayValue(Object paramObject,
                                    JsonConfig paramJsonConfig) {
        return process(paramObject);
    }
    @Override
    public Object processObjectValue(String paramString, Object paramObject,
                                     JsonConfig paramJsonConfig) {
        return process(paramObject);
    }
    private Object process(Object value){
        if(value instanceof Date){
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
            return sdf.format(value);
        }
        return value == null ? "" : value.toString();
    }
}

+ 64 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/MD5.java

@ -0,0 +1,64 @@
package com.yihu.wlyy.statistics.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
 * MD5 算法
 */
public class MD5 {
	// 全局数组
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
	public MD5() {
	}
	// 返回形式为数字跟字符串
	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		// System.out.println("iRet="+iRet);
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}
	// 返回形式只为数字
//	private static String byteToNum(byte bByte) {
//		int iRet = bByte;
//		System.out.println("iRet1=" + iRet);
//		if (iRet < 0) {
//			iRet += 256;
//		}
//		return String.valueOf(iRet);
//	}
	// 转换字节数组为16进制字串
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}
	public static String GetMD5Code(String strObj) {
		String resultString = null;
		try {
			resultString = new String(strObj);
			MessageDigest md = MessageDigest.getInstance("MD5");
			// md.digest() 该函数返回值为存放哈希值结果的byte数组
			resultString = byteToString(md.digest(strObj.getBytes()));
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return resultString;
	}
	public static void main(String[] args) {
		System.out.println(GetMD5Code("123456abcdefg"));
	}
}

+ 118 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/QuartzHelper.java

@ -0,0 +1,118 @@
package com.yihu.wlyy.statistics.util;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Map;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
@Component("quartzHelper")
public class QuartzHelper {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    private Scheduler scheduler = null;
    @PostConstruct
    public void init() {
        try {
            scheduler = schedulerFactoryBean.getScheduler();
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    public void addJob(Class jobClass, String cronString, String jobKey,
                       Map<String, Object> params) throws Exception {
        if (!CronExpression.isValidExpression(cronString)) {
            throw new Exception("cronExpression is not a valid Expression");
        }
        try {
            JobDetail job = JobBuilder.newJob(jobClass)
                    .withIdentity("job-id:" + jobKey, "job-group:" + jobKey)
                    .build();
            JobDataMap jobDataMap = job.getJobDataMap();
            jobDataMap.putAll(params);
            CronTrigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("trigger-name:" + jobKey, "trigger-group:" + jobKey)
                    .withSchedule(
                            CronScheduleBuilder
                                    .cronSchedule(cronString)
                                    //——以错过的第一个频率时间立刻开始执行
                                    //——重做错过的所有频率周期后
                                    //——当下一次触发频率发生时间大于当前时间后,再按照正常的Cron频率依次执行
                                    .withMisfireHandlingInstructionIgnoreMisfires()
                    )
                    .build();
            scheduler.scheduleJob(job, trigger);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    public void removeJob(String jobKeyString) throws Exception {
        TriggerKey triggerKey = new TriggerKey("trigger-name:" + jobKeyString, "trigger-group:" + jobKeyString);
        JobKey jobName = new JobKey("job-id:" + jobKeyString, "job-group:" + jobKeyString);
        scheduler.pauseTrigger(triggerKey);// 停止触发器
        scheduler.unscheduleJob(triggerKey);// 移除触发器
        scheduler.deleteJob(jobName);// 删除任务
    }
    public boolean isExistJob(String jobKey) throws SchedulerException {
        JobKey jk = new JobKey("job-id:" + jobKey, "job-group:" + jobKey);
        if (scheduler.checkExists(jk)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 立即执行任务。
     *
     * @param jobClass
     * @param id
     * @param params
     * @throws Exception
     */
    public void startNow(Class jobClass, String id, Map<String, String> params) throws Exception {
        startAt(new Date(), jobClass, id, params);
    }
    /**
     * 在指定时间点执行。
     *
     * @param time
     * @param jobClass
     * @param id
     * @param params
     * @throws Exception
     */
    public void startAt(Date time, Class jobClass, String id, Map<String, String> params) throws Exception {
        JobDetail job = JobBuilder.newJob(jobClass).
                withIdentity("job-id:" + id, "job-group:" + id)
                .build();
        JobDataMap jobDataMap = job.getJobDataMap();
        if (null != params) jobDataMap.putAll(params);
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger-id:" + id, "trigger-group:" + id)
                .startAt(time)
                .withSchedule(
                        simpleSchedule()
                                .withIntervalInSeconds(10)
                                .withRepeatCount(0)
                                .withMisfireHandlingInstructionIgnoreMisfires()
                )
                .build();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();
    }
}

+ 63 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/util/SpringUtil.java

@ -0,0 +1,63 @@
package com.yihu.wlyy.statistics.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
    }
    //获取applicationContext
    public static ApplicationContext
    getApplicationContext() {
        return applicationContext;
    }
    //通过name获取 Bean.
    public static Object
    getBean(String name) {
        return getApplicationContext().getBean(name);
    }
    //通过class获取Bean.
    public static<T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
    //通过name,以及Clazz返回指定的Bean
    public static<T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

+ 206 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/DataModel.java

@ -0,0 +1,206 @@
package com.yihu.wlyy.statistics.vo;
import org.elasticsearch.common.collect.HppcMaps;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by chenweida on 2017/6/1.
 */
public class DataModel {
    private String businessId;//业务id
    private String city;//城市代码 350200
    private String town;//区代码 350206
    private String hospital;//机构code
    private String team;//团队的code
    private String slaveKey1;//从维度  1级维度
    private String slaveKey2;//从维度  2级维度
    private String slaveKey3;//从维度  3级维度
    private String slaveKey4;//从维度  4级维度
    private String expensesStatus;//扣费状态
    private String patient;//患者code、
    private String idcard;// 身份证
    private String serverType;//服务类型
    private String healthLable;//健康标签
    private Double num = 1.0;//分数 如果是累加的计算 默认是1 如果是分数从数据库拿
    private String prescriptionCode;//处方code
    private String healthProblem;//诊断标签
    private Integer readStatus;//已读状态
    private Double result1 = 0.0; //从ES统计的时候的数目
    public String getHealthLable() {
        return healthLable;
    }
    public void setHealthLable(String healthLable) {
        this.healthLable = healthLable;
    }
    public String getBusinessId() {
        return businessId;
    }
    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }
    public String getTeam() {
        return team;
    }
    public void setTeam(String team) {
        this.team = team;
    }
    public String getHospital() {
        return hospital;
    }
    public void setHospital(String hospital) {
        this.hospital = hospital;
    }
    public String getTown() {
        return town;
    }
    public void setTown(String town) {
        this.town = town;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getSlaveKey1() {
        return slaveKey1;
    }
    public void setSlaveKey1(String slaveKey1) {
        this.slaveKey1 = slaveKey1;
    }
    public String getSlaveKey2() {
        return slaveKey2;
    }
    public void setSlaveKey2(String slaveKey2) {
        this.slaveKey2 = slaveKey2;
    }
    public String getSlaveKey3() {
        return slaveKey3;
    }
    public void setSlaveKey3(String slaveKey3) {
        this.slaveKey3 = slaveKey3;
    }
    public String getSlaveKey4() {
        return slaveKey4;
    }
    public void setSlaveKey4(String slaveKey4) {
        this.slaveKey4 = slaveKey4;
    }
    public String getExpensesStatus() {
        return expensesStatus;
    }
    public void setExpensesStatus(String expensesStatus) {
        this.expensesStatus = expensesStatus;
    }
    public String getIdcard() {
        return idcard;
    }
    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }
    public String getServerType() {
        return serverType;
    }
    public void setServerType(String serverType) {
        this.serverType = serverType;
    }
    public Double getNum() {
        return num;
    }
    public void setNum(Double num) {
        this.num = num;
    }
    public String isNull() {
//        if (StringUtils.isEmpty(this.getCity())) {
//            return "city";
//        }
//        if (StringUtils.isEmpty(this.getTown())) {
//            return "towm";
//        }
//        if (StringUtils.isEmpty(this.getHospital())) {
//            return "hospital";
//        }
        if (StringUtils.isEmpty(this.getTeam())) {
            return "team";
        }
        return null;
    }
    public String getPatient() {
        return patient;
    }
    public void setPatient(String patient) {
        this.patient = patient;
    }
    public String getPrescriptionCode() {
        return prescriptionCode;
    }
    public void setPrescriptionCode(String prescriptionCode) {
        this.prescriptionCode = prescriptionCode;
    }
    public String getHealthProblem() {
        return healthProblem;
    }
    public void setHealthProblem(String healthProblem) {
        this.healthProblem = healthProblem;
    }
    public Double getResult1() {
        return result1;
    }
    public void setResult1(Double result1) {
        this.result1 = result1;
    }
    public Integer getReadStatus() {
        return readStatus;
    }
    public void setReadStatus(Integer readStatus) {
        this.readStatus = readStatus;
    }
}

+ 25 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/DictModel.java

@ -0,0 +1,25 @@
package com.yihu.wlyy.statistics.vo;
/**
 * Created by chenweida on 2017/6/1.
 */
public class DictModel {
    private String code;//key
    private String name;//名称
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

+ 30 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/ErrModel.java

@ -0,0 +1,30 @@
package com.yihu.wlyy.statistics.vo;
/**
 * Created by chenweida on 2017/6/1.
 */
public class ErrModel {
    private String businessId;
    private String message;
    public ErrModel(String businessId, String message) {
        this.businessId = businessId;
        this.message = message;
    }
    public String getBusinessId() {
        return businessId;
    }
    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

+ 34 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/FilterModel.java

@ -0,0 +1,34 @@
package com.yihu.wlyy.statistics.vo;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by chenweida on 2017/6/1.
 */
public class FilterModel {
    private List<DataModel>  data;//可能是list 也可能是map
    private List<ErrModel> ErrorModels =new ArrayList<>();//
    public FilterModel(List<DataModel> data, List<ErrModel> errorModels) {
        this.data = data;
        ErrorModels = errorModels;
    }
    public List<DataModel> getData() {
        return data;
    }
    public void setData(List<DataModel> data) {
        this.data = data;
    }
    public List<ErrModel> getErrorModels() {
        return ErrorModels;
    }
    public void setErrorModels(List<ErrModel> ErrorModels) {
        this.ErrorModels = ErrorModels;
    }
}

+ 19 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/JobLogModel.java

@ -0,0 +1,19 @@
package com.yihu.wlyy.statistics.vo;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by chenweida on 2017/6/2.
 */
public class JobLogModel {
    private List<ErrModel> ErrorModels =new ArrayList<>();
    public List<ErrModel> getErrorModels() {
        return ErrorModels;
    }
    public void setErrorModels(List<ErrModel> errorModels) {
        ErrorModels = errorModels;
    }
}

+ 318 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/SaveModel.java

@ -0,0 +1,318 @@
package com.yihu.wlyy.statistics.vo;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.searchbox.annotations.JestId;
import org.elasticsearch.index.analysis.AnalysisSettingsRequired;
import org.springframework.data.annotation.CreatedDate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by chenweida on 2017/6/1.
 */
public class SaveModel {
    private final static String teamKey = "team";
    private final static String OrgKey = "hospital";
    private final static String townKey = "town";
    private final static String cityKey = "city";
    public final static String interval_day = "1";
    public final static String interval_week = "2";
    public final static String interval_month = "3";
    public final static String teamLevel = "5";
    public final static String OrgLevel = "4";
    public final static String townLevel = "3";
    public final static String cityLevel = "2";
    private static final Map<String, String> fieldsSithch = new HashMap<>();
    @JestId
    private String id;
    private String saasId;//saasId
    private String quotaCode;//指标code
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXX")
    @CreatedDate
    @JSONField(format = "yyyy-MM-dd'T'HH:mm:ssXX")
    private Date quotaDate;//统计时间
    private String city;//城市代码 350200
    private String cityName;//
    private String town;//区代码 350206
    private String townName;//
    private String hospital;//机构code
    private String hospitalName;//
    private String team;//团队的code
    private String teamName;//
    private String slaveKey1;//从维度  1级维度
    private String slaveKey1Name;
    private String slaveKey2;//从维度  2级维度
    private String slaveKey2Name;
    private String slaveKey3;//从维度  3级维度
    private String slaveKey3Name;
    private String slaveKey4;//从维度  4级维度
    private String slaveKey4Name;
    private Double result1 = 0.0;//统计结果  总的累加结果 如果是平均分 那就是总分 其他指标都是和result2一样
    private Double result2 = 0.0;//统计结果  次数
    private String areaLevel;// 1 省 2 市 3 区县 4 机构 5团队
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXX")
    @CreatedDate
    @JSONField(format = "yyyy-MM-dd'T'HH:mm:ssXX")
    private Date createTime;//创建时间
    private String timeLevel;// 1增量 2到达量
    public Date getQuotaDate() {
        return quotaDate;
    }
    public void setQuotaDate(Date quotaDate) {
        this.quotaDate = quotaDate;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getTown() {
        return town;
    }
    public void setTown(String town) {
        this.town = town;
    }
    //@Field(type = FieldType.String, analyzer="ngram_analyzer")//使用ngram进行单字分词
    public String getHospital() {
        return hospital;
    }
    public void setHospital(String hospital) {
        this.hospital = hospital;
    }
    public String getTeam() {
        return team;
    }
    public void setTeam(String team) {
        this.team = team;
    }
    public String getSlaveKey1() {
        return slaveKey1;
    }
    public void setSlaveKey1(String slaveKey1) {
        this.slaveKey1 = slaveKey1;
    }
    public String getSlaveKey2() {
        return slaveKey2;
    }
    public void setSlaveKey2(String slaveKey2) {
        this.slaveKey2 = slaveKey2;
    }
    public String getSlaveKey3() {
        return slaveKey3;
    }
    public void setSlaveKey3(String slaveKey3) {
        this.slaveKey3 = slaveKey3;
    }
    public String getSlaveKey4() {
        return slaveKey4;
    }
    public void setSlaveKey4(String slaveKey4) {
        this.slaveKey4 = slaveKey4;
    }
    public Double getResult1() {
        return result1;
    }
    public void setResult1(Double result1) {
        this.result1 = result1;
    }
    public Double getResult2() {
        return result2;
    }
    public void setResult2(Double result2) {
        this.result2 = result2;
    }
    public String getSlaveKey1Name() {
        return slaveKey1Name;
    }
    public void setSlaveKey1Name(String slaveKey1Name) {
        this.slaveKey1Name = slaveKey1Name;
    }
    public String getSlaveKey2Name() {
        return slaveKey2Name;
    }
    public void setSlaveKey2Name(String slaveKey2Name) {
        this.slaveKey2Name = slaveKey2Name;
    }
    public String getSlaveKey3Name() {
        return slaveKey3Name;
    }
    public void setSlaveKey3Name(String slaveKey3Name) {
        this.slaveKey3Name = slaveKey3Name;
    }
    public String getSlaveKey4Name() {
        return slaveKey4Name;
    }
    public void setSlaveKey4Name(String slaveKey4Name) {
        this.slaveKey4Name = slaveKey4Name;
    }
    public String getCityName() {
        return cityName;
    }
    public void setCityName(String cityName) {
        this.cityName = cityName;
    }
    public String getTownName() {
        return townName;
    }
    public void setTownName(String townName) {
        this.townName = townName;
    }
    public String getHospitalName() {
        return hospitalName;
    }
    public void setHospitalName(String hospitalName) {
        this.hospitalName = hospitalName;
    }
    public String getTeamName() {
        return teamName;
    }
    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }
    public String getQuotaCode() {
        return quotaCode;
    }
    public void setQuotaCode(String quotaCode) {
        this.quotaCode = quotaCode;
    }
    public String getAreaLevel() {
        return areaLevel;
    }
    public void setAreaLevel(String areaLevel) {
        this.areaLevel = areaLevel;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public String getSaasId() {
        return saasId;
    }
    public void setSaasId(String saasId) {
        this.saasId = saasId;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTimeLevel() {
        return timeLevel;
    }
    public void setTimeLevel(String timeLevel) {
        this.timeLevel = timeLevel;
    }
    /**
     * 根据级别得到key
     * 1 省 2 市 3 区县 4 机构 5团队
     *
     * @param level
     * @return
     */
    public static String getAreaLevelKey(String level) {
        switch (level) {
            case teamLevel: {
                return teamKey;
            }
            case OrgLevel: {
                return OrgKey;
            }
            case townLevel: {
                return townKey;
            }
            case cityLevel: {
                return cityKey;
            }
            default: {
                return teamKey;
            }
        }
    }
}

+ 174 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/WlyyJobConfigVO.java

@ -0,0 +1,174 @@
package com.yihu.wlyy.statistics.vo;
import java.io.Serializable;
/**
 * Created by Administrator on 2016/8/16.
 */
public class WlyyJobConfigVO implements  Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String quotaId;//指标id
    private String jobName;//任务名称
    private String jobInfo;//任务描述
    private String jobType;//任务类型(1 累加  2平均值
    private String jobClass;//任务执行的class
    private String quartzCron;//quartz表达式
    private String status;//1 启动 0停止
    private String del;//是否删除 1正常 0删除
    private String sql; //mysql的查询语句
    private String sqlCount;//mysql的查询数量语句,用在多线程抽取数据用
    private String sqlDay;//按日统计
    private String sqlYear;//按年统计
    private String cacheKey;//缓存的key
    private Integer incrementInterval;//增量的时间间隔(1天,2周,3月)
    private String timeLevel;//1增量 2到达量 3生成到达量也生成增量
    public WlyyJobConfigVO() {
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getQuotaId() {
        return quotaId;
    }
    public void setQuotaId(String quotaId) {
        this.quotaId = quotaId;
    }
    public String getJobName() {
        return jobName;
    }
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }
    public String getJobInfo() {
        return jobInfo;
    }
    public void setJobInfo(String jobInfo) {
        this.jobInfo = jobInfo;
    }
    public String getJobType() {
        return jobType;
    }
    public void setJobType(String jobType) {
        this.jobType = jobType;
    }
    public String getJobClass() {
        return jobClass;
    }
    public void setJobClass(String jobClass) {
        this.jobClass = jobClass;
    }
    public String getQuartzCron() {
        return quartzCron;
    }
    public void setQuartzCron(String quartzCron) {
        this.quartzCron = quartzCron;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getDel() {
        return del;
    }
    public void setDel(String del) {
        this.del = del;
    }
    @Override
    public String toString() {
        return "WlyyJobConfigVO{" +
                "id='" + id + '\'' +
                ", quotaId='" + quotaId + '\'' +
                ", jobName='" + jobName + '\'' +
                ", jobInfo='" + jobInfo + '\'' +
                ", jobType='" + jobType + '\'' +
                ", jobClass='" + jobClass + '\'' +
                ", quartzCron='" + quartzCron + '\'' +
                ", status='" + status + '\'' +
                ", del='" + del + '\'' +
                '}';
    }
    public String getSqlYear() {
        return sqlYear;
    }
    public void setSqlYear(String sqlYear) {
        this.sqlYear = sqlYear;
    }
    public String getSql() {
        return sql;
    }
    public void setSql(String sql) {
        this.sql = sql;
    }
    public String getSqlCount() {
        return sqlCount;
    }
    public void setSqlCount(String sqlCount) {
        this.sqlCount = sqlCount;
    }
    public String getSqlDay() {
        return sqlDay;
    }
    public void setSqlDay(String sqlDay) {
        this.sqlDay = sqlDay;
    }
    public String getCacheKey() {
        return cacheKey;
    }
    public void setCacheKey(String cacheKey) {
        this.cacheKey = cacheKey;
    }
    public Integer getIncrementInterval() {
        return incrementInterval;
    }
    public void setIncrementInterval(Integer incrementInterval) {
        this.incrementInterval = incrementInterval;
    }
    public String getTimeLevel() {
        return timeLevel;
    }
    public void setTimeLevel(String timeLevel) {
        this.timeLevel = timeLevel;
    }
}

+ 145 - 0
svr/svr-statistics/src/main/java/com/yihu/wlyy/statistics/vo/WlyyQuotaVO.java

@ -0,0 +1,145 @@
package com.yihu.wlyy.statistics.vo;
import java.io.Serializable;
import java.util.Date;
/**
 * Created by Administrator on 2016/8/16.
 */
public class WlyyQuotaVO implements Serializable {
    private static final long serialVersionUID = 2L;
    private String id;
    private String name;//指标名称
    private String cycle;//统计周期( 1:天 )
    private String type;//尺度(1 新增 2是累计)
    private Date createTime;//创建时间
    private Date modifyTime;//修改时间
    private String del;//1: 正常 0: 删除
    private String sql;//数据库查询语句
    private String countSql;//数据库数据数量查询语句
    private String level1;//1级维度 (1:患者性别 2:患者分组 3:患者年龄)
    private String level2;//2级维度 (1:患者性别 2:患者分组 3:患者年龄)
    private String level3;//3级维度 (1:患者性别 2:患者分组 3:患者年龄)
    private String level4;//4级维度 (1:患者性别 2:患者分组 3:患者年龄)
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLevel1() {
        return level1;
    }
    public void setLevel1(String level1) {
        this.level1 = level1;
    }
    public String getLevel2() {
        return level2;
    }
    public void setLevel2(String level2) {
        this.level2 = level2;
    }
    public String getCycle() {
        return cycle;
    }
    public void setCycle(String cycle) {
        this.cycle = cycle;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public Date getModifyTime() {
        return modifyTime;
    }
    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }
    public String getDel() {
        return del;
    }
    public void setDel(String del) {
        this.del = del;
    }
    @Override
    public String toString() {
        return "WlyyQuotaVO{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", level1='" + level1 + '\'' +
                ", level2='" + level2 + '\'' +
                ", cycle='" + cycle + '\'' +
                ", type='" + type + '\'' +
                ", createTime=" + createTime +
                ", modifyTime=" + modifyTime +
                ", del='" + del + '\'' +
                ", sql='" + sql + '\'' +
                '}';
    }
    public String getSql() {
        return sql;
    }
    public void setSql(String sql) {
        this.sql = sql;
    }
    public String getCountSql() {
        return countSql;
    }
    public void setCountSql(String countSql) {
        this.countSql = countSql;
    }
    public String getLevel4() {
        return level4;
    }
    public void setLevel4(String level4) {
        this.level4 = level4;
    }
    public String getLevel3() {
        return level3;
    }
    public void setLevel3(String level3) {
        this.level3 = level3;
    }
}

+ 55 - 0
svr/svr-statistics/src/main/resources/application-dev.yml

@ -0,0 +1,55 @@
spring:
  profiles: dev
  datasource:
    primaryReadWrite: #只读库 主库
      url: jdbc:mysql://172.19.103.77/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: root
      password: 123456
#       url: jdbc:mysql://172.19.103.85/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#       username: linzhou
#       password: linzhou
    primaryRead: #只读库 从库
      url: jdbc:mysql://172.19.103.77/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: root
      password: 123456
#       url: jdbc:mysql://172.19.103.77/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#       username: linzhou
#       password: linzhou
    im: #im库
      url: jdbc:mysql://172.19.103.77/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: root
      password: 123456
  redis:
    host: 172.19.103.88 # Redis server host.
    port: 6379 # Redis server port.
  followupMessage:
    cron : 0 5 0 * * ?
    jobId : FOLLOWUP_PLAN_JOB
fv:
  jdbc:
    driver: com.mysql.jdbc.Driver
    url: jdbc:mysql://172.19.103.77/fv?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
    username: root
    password: 123456
systemConfig:
  msg_push_server: http://172.19.103.76:3000
es:
  index: wlyy_quota_test
  type: wlyy_quota_test
  host:  http://172.19.103.68:9200
  tHost: 172.19.103.68:9300
  clusterName: jkzl
wlyy:
  im:
    databaseName: im_new
quartz:
  name: schedulerFactoryBean_dev

+ 60 - 0
svr/svr-statistics/src/main/resources/application-devtest.yml

@ -0,0 +1,60 @@
spring:
  profiles: devtest
  datasource:
    primaryReadWrite:
      url: jdbc:mysql://172.19.103.85/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: linzhou
      password: linzhou
    primaryRead:
      url: jdbc:mysql://172.19.103.85/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: linzhou
      password: linzhou
    im: #im库
      url: jdbc:mysql://172.19.103.85/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: linzhou
      password: linzhou
#    primaryReadWrite:
#      url: jdbc:mysql://172.19.103.77/wlyy_quota?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#      username: root
#      password: 123456
#    primaryRead:
#      url: jdbc:mysql://172.19.103.77/wlyy_quota?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#      username: root
#      password: 123456
#    im: #im库
#      url: jdbc:mysql://172.19.103.77/wlyy_quota?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#      username: root
#      password: 123456
  redis:
    host: 172.19.103.88 # Redis server host.
    port: 6379 # Redis server port.
  followupMessage:
    cron : 0 5 0 * * ?
    jobId : FOLLOWUP_PLAN_JOB
fv:
  jdbc:
    driver: com.mysql.jdbc.Driver
    url: jdbc:mysql://172.17.110.160/fv?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
    username: ssgg
    password: ssgg
systemConfig:
  msg_push_server: http://127.0.0.1:3000
es:
  index: wlyy_quota_test
  type: wlyy_quota_test
  host:  http://172.19.103.68:9200
  tHost: 172.19.103.68:9300
  clusterName: jkzl
wlyy:
  im:
    databaseName: im_new
quartz:
  name: schedulerFactoryBean_cwd

+ 42 - 0
svr/svr-statistics/src/main/resources/application-prod.yml

@ -0,0 +1,42 @@
spring:
  profiles: prod
  datasource:
    primaryReadWrite:
      url: jdbc:mysql://59.61.92.90:9069/wlyy?useUnicode=true&characterEncoding=utf-8&amp;autoReconnect=true
      username: wlyy
      password: jkzlehr@123
    primaryRead:
      url: jdbc:mysql://59.61.92.90:8079/wlyy?useUnicode=true&characterEncoding=utf-8&amp;autoReconnect=true
      username: wlyy
      password: jkzlehr@123
    im: #im库:
      url: jdbc:mysql://59.61.92.90:8079/im?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: im
      password: im!)123
  redis:
     host: 59.61.92.90 # Redis server host.
     port: 9054  # Redis server port.
     password: jkzlehr
  followupMessage:
    cron : 0 5 0 * * ?
    jobId : FOLLOWUP_PLAN_JOB
systemConfig:
  msg_push_server: http://127.0.0.1:3000
es:
  index: wlyy_quota_prod
  type: wlyy_quota_prod
  host:  http://59.61.92.90:9065,http://59.61.92.90:9067
  tHost: 59.61.92.90:9066,59.61.92.90:9068
  clusterName: jkzl
wlyy:
  im:
    databaseName: im
quartz:
  name: schedulerFactoryBean_es_prod

+ 60 - 0
svr/svr-statistics/src/main/resources/application-test.yml

@ -0,0 +1,60 @@
spring:
  profiles: test
  datasource:
    primaryReadWrite:
      url: jdbc:mysql://172.19.103.85/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: linzhou
      password: linzhou
    primaryRead:
      url: jdbc:mysql://172.19.103.85/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: linzhou
      password: linzhou
    im: #im库
      url: jdbc:mysql://172.19.103.85/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: linzhou
      password: linzhou
#    primaryReadWrite:
#      url: jdbc:mysql://172.19.103.77/wlyy_quota?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#      username: root
#      password: 123456
#    primaryRead:
#      url: jdbc:mysql://172.19.103.77/wlyy_quota?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#      username: root
#      password: 123456
#    im: #im库
#      url: jdbc:mysql://172.19.103.77/wlyy_quota?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#      username: root
#      password: 123456
  redis:
    host: 172.19.103.88 # Redis server host.
    port: 6379 # Redis server port.
  followupMessage:
    cron : 0 5 0 * * ?
    jobId : FOLLOWUP_PLAN_JOB
fv:
  jdbc:
    driver: com.mysql.jdbc.Driver
    url: jdbc:mysql://172.17.110.160/fv?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
    username: ssgg
    password: ssgg
systemConfig:
  msg_push_server: http://127.0.0.1:3000
es:
  index: wlyy_quota_test
  type: wlyy_quota_test
  host:  http://172.19.103.68:9200
  tHost: 172.19.103.68:9300
  clusterName: jkzl
wlyy:
  im:
    databaseName: im_new
quartz:
  name: schedulerFactoryBean_cwd

+ 72 - 0
svr/svr-statistics/src/main/resources/application.yml

@ -0,0 +1,72 @@
server:
  port: 8081
spring:
  application:
    name: quota-es
  boot:
    admin:
      url: http://localhost:8082
      username: jkzl
      password: jkzlehr
      client:
        service-url: http://localhost:8082
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    initialSize: 50
    maxActive: 50
    minIdle: 50 #最大空闲连接
    maxWait: 50
    validationQuery: SELECT 1 #SQL 查询, 用来验证从连接池取出的连接, 在将连接返回给调用者之前。 如果指定, 则查询必须是一个SQL SELECT 并且必须返回至少一行记录
    testOnBorrow: true #指明是否在从池中取出连接前进行检验, 如果检验失败, 则从池中去除连接并尝试取出另一个。注意: 设置为true 后如果要生效,validationQuery 参数必须设置为非空字符串
    testOnReturn: true #指明是否在归还到池中前进行检验 注意: 设置为true 后如果要生效validationQuery 参数必须设置为非空字符串
    testWhileIdle: true #指明连接是否被空闲连接回收器(如果有)进行检验,如果检测失败,则连接将被从池中去除
    minEvictableIdleTimeMillis: 3600000 #连接池中连接,在时间段内一直空闲,被逐出连接池的时间(1000*60*60),以毫秒为单位
    timeBetweenEvictionRunsMillis: 300000 #在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位,一般比minEvictableIdleTimeMillis小
    poolPreparedStatements: true # 打开PSCache,并且指定每个连接上PSCache的大小
    maxPoolPreparedStatementPerConnectionSize: 50
    removeAbandoned: false #超过时间限制是否回收
    removeAbandonedTimeout: 7200 #超时时间;单位为秒。180秒=3分钟
    logAbandoned: false #关闭abanded连接时输出错误日志
    filters: stat,wall,logback #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
    # REDIS
  redis:
    database: 0 # Database index used by the connection factory.
    password: # Login password of the redis server.
    timeout: 0 # Connection timeout in milliseconds.
      #sentinel:
      #  master: # Name of Redis server.
      #  nodes: # Comma-separated list of host:port pairs.
    pool:
      max-active: 8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
      max-idle: 8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
      max-wait: -1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
      min-idle: 1 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
multipart:
  max-file-size: 100MB
  max-request-size: 100MB
security:
  basic:
    username: jkzl
    password: jkzlehr
logging:
  level:
    root: INFO
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
JobService:
  sleepTime: 40000

+ 4 - 0
svr/svr-statistics/src/main/resources/logback.xml

@ -0,0 +1,4 @@
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <jmxConfigurator/>
</configuration>

+ 41 - 0
svr/svr-statistics/src/main/resources/quartz.properties

@ -0,0 +1,41 @@
# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.misfireThreshold: 60000
#============================================================================
# Configure JobStore
#============================================================================
# RAM
# org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
# Configure JobStore Cluster
org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#datasource׺
org.quartz.jobStore.tablePrefix:QRTZ_
#org.quartz.jobStore.dataSource:qzDS
#
##============================================================================
## Configure Datasources
##============================================================================
##datasource
#org.quartz.dataSource.qzDS.driver: com.mysql.jdbc.Driver
#org.quartz.dataSource.qzDS.URL: jdbc:mysql://172.19.103.85/wlyy?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#org.quartz.dataSource.qzDS.user: root
#org.quartz.dataSource.qzDS.password: 123456
org.quartz.jobGroupName = RS_JOBGROUP_NAME
org.quartz.triggerGroupName = RS_TRIGGERGROUP_NAME