Browse Source

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

Conflicts:
	common/common-entity/src/main/java/com/yihu/jw/entity/specialist/rehabilitation/RehabilitationTemplateDetailDO.java
yeshijie 3 years ago
parent
commit
80c902695a
91 changed files with 11582 additions and 0 deletions
  1. 170 0
      svr/svr-statistics-es/doc/es/QuartzJobConfig.java
  2. 127 0
      svr/svr-statistics-es/doc/es/QuartzJobConfig1.java
  3. 116 0
      svr/svr-statistics-es/doc/es/统计新增索引.txt
  4. 192 0
      svr/svr-statistics-es/doc/sql/统计相关表数据备份20171121.txt
  5. BIN
      svr/svr-statistics-es/doc/业务文档/统计指标流程.docx
  6. BIN
      svr/svr-statistics-es/doc/部署文档/正式库/后台/tomcat部署.docx
  7. 455 0
      svr/svr-statistics-es/pom.xml
  8. 120 0
      svr/svr-statistics-es/readme.MD
  9. 56 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/Application.java
  10. 49 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/AsyncConfig.java
  11. 240 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/DataSourceConfig.java
  12. 30 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/MVCConfig.java
  13. 61 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/SwaggerConfig.java
  14. 55 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/WebSecurityConfig.java
  15. 27 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/jpa/HibernateProperties.java
  16. 58 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/jpa/WlyyJpa.java
  17. 25 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/quartz/JobFactory.java
  18. 57 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/quartz/SchedulerConfig.java
  19. 15 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/war/ServletInitializer.java
  20. 464 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/controller/BaseController.java
  21. 81 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/controller/ExtractDataController.java
  22. 372 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/controller/JobController.java
  23. 278 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/controller/QueryController.java
  24. 17 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/JobConfigDao.java
  25. 33 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/QuartzJobConfigDao.java
  26. 11 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/QuartzJobLogDao.java
  27. 11 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/QuotaDao.java
  28. 17 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/WlyyDimensionQuotaDao.java
  29. 30 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/WlyyQuotaResultDao.java
  30. 28 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/cache/Cache.java
  31. 245 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/compute/ComputeHelper.java
  32. 24 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/Convert.java
  33. 50 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/ConvertHelper.java
  34. 68 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/wlyy/ConsultTypeConvert.java
  35. 33 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/wlyy/model/HealthLable.java
  36. 26 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/wlyy/model/PrescriptionDisease.java
  37. 25 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/wlyy/model/ServerType.java
  38. 96 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/ExtractHelper.java
  39. 67 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/db/DBExtract.java
  40. 189 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/db/DBPageExtract.java
  41. 158 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/db/Data2Save.java
  42. 70 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/db/ESExtract.java
  43. 47 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/filter/FilterHelper.java
  44. 34 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/filter/filter/NotNullFilter.java
  45. 29 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/save/SaveHelper.java
  46. 149 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/save/es/ElasticFactory.java
  47. 112 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/save/es/ElastricSearchSave.java
  48. 29 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/job/business/CacheCleanJob.java
  49. 476 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/job/business/CurrentMysqlToEsQuotaJob.java
  50. 305 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/job/business/EsToEsQuotaJob.java
  51. 351 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/job/business/MysqlToEsQuotaJob.java
  52. 42 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/job/business/ScheduleStatictisJob.java
  53. 43 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/service/ElasticsearchService.java
  54. 295 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/service/ExtractDataService.java
  55. 595 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/service/JobService.java
  56. 139 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/task/PushMsgTask.java
  57. 177 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/Contant.java
  58. 833 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/DateUtil.java
  59. 606 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/ElasticsearchUtil.java
  60. 128 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/HttpClientUtil.java
  61. 162 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/IdCardUtil.java
  62. 221 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/ImUtill.java
  63. 45 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/JsonDateValueProcessor.java
  64. 64 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/MD5.java
  65. 118 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/QuartzHelper.java
  66. 63 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/SpringUtil.java
  67. 83 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/hibernate/HibenateDemo.java
  68. 246 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/hibernate/HibenateUtils.java
  69. 116 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/BaseDimension.java
  70. 70 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/BaseDimensionQuota.java
  71. 94 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/BaseDoctorDO.java
  72. 231 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/DataModel.java
  73. 25 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/DictModel.java
  74. 30 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/ErrModel.java
  75. 34 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/FilterModel.java
  76. 19 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/JobLogModel.java
  77. 343 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/SaveModel.java
  78. 174 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/WlyyJobConfigVO.java
  79. 145 0
      svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/WlyyQuotaVO.java
  80. 75 0
      svr/svr-statistics-es/src/main/resources/application-dev.yml
  81. 69 0
      svr/svr-statistics-es/src/main/resources/application-devtest.yml
  82. 75 0
      svr/svr-statistics-es/src/main/resources/application-jwOracleProd.yml
  83. 52 0
      svr/svr-statistics-es/src/main/resources/application-jwprod.yml
  84. 67 0
      svr/svr-statistics-es/src/main/resources/application-orctest.yml
  85. 51 0
      svr/svr-statistics-es/src/main/resources/application-prod.yml
  86. 59 0
      svr/svr-statistics-es/src/main/resources/application-test.yml
  87. 52 0
      svr/svr-statistics-es/src/main/resources/application-tnJwprod.yml
  88. 53 0
      svr/svr-statistics-es/src/main/resources/application-xzprod.yml
  89. 65 0
      svr/svr-statistics-es/src/main/resources/application.yml
  90. 4 0
      svr/svr-statistics-es/src/main/resources/logback.xml
  91. 41 0
      svr/svr-statistics-es/src/main/resources/quartz.properties

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

@ -0,0 +1,170 @@
package com.yihu.jw.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-es/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;
//	}
//}

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

@ -0,0 +1,116 @@
创建索引
POST  http://172.19.103.68:9200/hlw_quota_test
查询索引
GET http://172.19.103.68:9200/hlw_quota_test/
给索引加mapping
POST http://172.19.103.68:9200/hlw_quota_test/hlw_quota_test/_mapping
{
    "hlw_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"
                },
                "dept": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "deptName": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "doctor": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "doctorName": {
                    "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/hlw_quota_test/_settings
{
  "index": {
    "max_result_window": 500000
  }
}

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


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


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


+ 455 - 0
svr/svr-statistics-es/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-es</artifactId>
    <version>1.0.0</version>
    <modelVersion>4.0.0</modelVersion>
    <!--打成war包需要的配置-->
    <packaging>jar</packaging>
    <name>svr-statistics-es</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <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>5.0.12.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.tomcat-embed>8.0.28</version.tomcat-embed>
        <version.elasticsearch>2.4.6</version.elasticsearch>
        <version.jest>2.4.0</version.jest>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.yihu.jw</groupId>
            <artifactId>common-entity</artifactId>
            <version>2.0.0</version>
        </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>
        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>shield</artifactId>
            <version>${version.elasticsearch}</version>
        </dependency>
        <!--elasticsearch end-->
        <!--Apache commons library-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.4.1</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>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </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-devtools</artifactId>-->
        <!--<version>${version.spring-boot}</version>-->
        <!--</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>de.codecentric</groupId>-->
        <!--<artifactId>spring-boot-admin-starter-client</artifactId>-->
        <!--<version>1.3.5</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>
            <!--<scope>provided</scope>-->
        </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>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--<dependency>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-data-elasticsearch</artifactId>-->
        <!--<version>1.3.5.RELEASE</version>-->
        <!--</dependency>-->
        <!--oracle驱动-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>svr-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-es/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-es/src/main/java/com/yihu/jw/Application.java

@ -0,0 +1,56 @@
package com.yihu.jw;
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
}

+ 49 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/AsyncConfig.java

@ -0,0 +1,49 @@
package com.yihu.jw.statistics.config;
import java.util.concurrent.Executor;
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;
/**
 * 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-es/src/main/java/com/yihu/jw/statistics/config/DataSourceConfig.java

@ -0,0 +1,240 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/config/MVCConfig.java

@ -0,0 +1,30 @@
package com.yihu.jw.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);
    }
}

+ 61 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/SwaggerConfig.java

@ -0,0 +1,61 @@
package com.yihu.jw.statistics.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
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;
    }
}

+ 55 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/WebSecurityConfig.java

@ -0,0 +1,55 @@
package com.yihu.jw.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()
                .antMatchers("/job/**").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-es/src/main/java/com/yihu/jw/statistics/config/jpa/HibernateProperties.java

@ -0,0 +1,27 @@
package com.yihu.jw.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;
    }
}

+ 58 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/config/jpa/WlyyJpa.java

@ -0,0 +1,58 @@
package com.yihu.jw.statistics.config.jpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.jw.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.jw");
        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-es/src/main/java/com/yihu/jw/statistics/config/quartz/JobFactory.java

@ -0,0 +1,25 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/config/quartz/SchedulerConfig.java

@ -0,0 +1,57 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/config/war/ServletInitializer.java

@ -0,0 +1,15 @@
package com.yihu.jw.statistics.config.war;
import com.yihu.jw.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);
    }
}

+ 464 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/controller/BaseController.java

@ -0,0 +1,464 @@
package com.yihu.jw.statistics.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.entity.iot.gateway.IdEntity;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BaseController {
	private static Logger logger = LoggerFactory.getLogger(BaseController.class);
	@Autowired
	protected HttpServletRequest request;
	/**
	 * 獲取髮送請求用戶的uid
	 * @return
	 */
	public String getUID() {
		try {
			String userAgent = request.getHeader("userAgent");
			if(StringUtils.isEmpty(userAgent)){
				userAgent = request.getHeader("User-Agent");
			}
			JSONObject json = new JSONObject(userAgent);
			return json.getString("uid");
			
		} catch (Exception e) {
			return null;
		}
	}
	public String getOpenid() {
		try {
			String userAgent = request.getHeader("userAgent");
			if(StringUtils.isEmpty(userAgent)){
				userAgent = request.getHeader("User-Agent");
			}
			JSONObject json = new JSONObject(userAgent);
			return json.getString("openid");
			
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 获取用户ID
	 * @return
	 */
	public long getId() {
		try {
			String userAgent = request.getHeader("userAgent");
			if(StringUtils.isEmpty(userAgent)){
				userAgent = request.getHeader("User-Agent");
			}
			JSONObject json = new JSONObject(userAgent);
			return json.getLong("id");
		} catch (Exception e) {
			return 0;
		}
	}
	public String getIMEI() {
		try {
			String userAgent = request.getHeader("userAgent");
			if(StringUtils.isEmpty(userAgent)){
				userAgent = request.getHeader("User-Agent");
			}
			JSONObject json = new JSONObject(userAgent);
			return json.getString("imei");
		} catch (Exception e) {
			return null;
		}
	}
	
	public String getToken() {
		try {
			String userAgent = request.getHeader("userAgent");
			if(StringUtils.isEmpty(userAgent)){
				userAgent = request.getHeader("User-Agent");
			}
			JSONObject json = new JSONObject(userAgent);
			return json.getString("token");
		} catch (Exception e) {
			return null;
		}
	}
	public void error(Exception e) {
		logger.error(getClass().getName() + ":", e.getMessage());
		e.printStackTrace();
	}
	public void warn(Exception e) {
		logger.warn(getClass().getName() + ":", e.getMessage());
		e.printStackTrace();
	}
	/**
	 * 返回接口处理结果
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String error(int code, String msg) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", code);
			map.put("msg", msg);
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return null;
		}
	}
	/**
	 * 接口处理成功
	 * @param msg
	 * @return
	 */
	public String success(String msg) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", 200);
			map.put("msg", msg);
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return null;
		}
	}
	public String write(int code, String msg) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", code);
			map.put("msg", msg);
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return null;
		}
	}
	/**
	 * 返回接口处理结果
	 * 
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, String key, List<?> list) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", code);
			map.put("msg", msg);
			map.put(key, list);
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	 * 返回接口处理结果
	 * 
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, String key, JSONObject value) {
		try {
			JSONObject json = new JSONObject();
			json.put("status", code);
			json.put("msg", msg);
			json.put(key, value);
			return json.toString();
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	 * 返回接口处理结果
	 * 
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, String key, JSONArray value) {
		try {
			JSONObject json = new JSONObject();
			json.put("status", code);
			json.put("msg", msg);
			json.put(key, value);
			return json.toString();
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	 * 返回接口处理结果
	 * 
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param total 总数
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, int total, String key, JSONArray value) {
		try {
			JSONObject json = new JSONObject();
			json.put("status", code);
			json.put("msg", msg);
			json.put("total", total);
			json.put(key, value);
			return json.toString();
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	 * 返回接口处理结果
	 * 
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, String key, Object value) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", code);
			map.put("msg", msg);
			map.put(key, value);
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	 * 返回接口处理结果
	 * 
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, String key, Page<?> list) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", code);
			map.put("msg", msg);
			// 是否为第一页
			map.put("isFirst", list.isFirst());
			// 是否为最后一页
			map.put("isLast", list.isLast());
			// 总条数
			map.put("total", list.getTotalElements());
			// 总页数
			map.put("totalPages", list.getTotalPages());
			map.put(key, list.getContent());
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	 * 返回接口处理结果
	 *
	 *
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, String key, Page<?> page, JSONArray array) {
		try {
			JSONObject json = new JSONObject();
			json.put("status", code);
			json.put("msg", msg);
			// 是否为第一页
			json.put("isFirst", page.isFirst());
			// 是否为最后一页
			json.put("isLast", page.isLast());
			// 总条数
			json.put("total", page.getTotalElements());
			// 总页数
			json.put("totalPages", page.getTotalPages());
			json.put(key, array);
			return json.toString();
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	 * 返回接口处理结果
	 * 
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, String key, Map<?, ?> value) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", code);
			map.put("msg", msg);
			map.put(key, value);
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	* 返回接口处理结果
	*
	* @param code 结果码,成功为200
	* @param msg 结果提示信息
	* @param value 结果数据
	* @return
	*/
	public String write(int code, String msg, String key, String value) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", code);
			map.put("msg", msg);
			map.put(key, value);
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	* 返回接口处理结果
	*
	*
	* @param code 结果码,成功为200
	* @param msg 结果提示信息
	* @param value 结果数据
	* @return
	*/
	public String write(int code, String msg, String key, IdEntity entity) {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ObjectMapper mapper = new ObjectMapper();
			map.put("status", code);
			map.put("msg", msg);
			map.put(key, entity);
			return mapper.writeValueAsString(map);
		} catch (Exception e) {
			error(e);
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	/**
	 * 返回接口处理结果
	 * 
	 * 
	 * @param code 结果码,成功为200
	 * @param msg 结果提示信息
	 * @param value 结果数据
	 * @return
	 */
	public String write(int code, String msg, boolean isFirst, boolean isLast, long total, int totalPages, String key, Object values) {
		try {
			JSONObject json = new JSONObject();
			json.put("status", code);
			json.put("msg", msg);
			// 是否为第一页
			json.put("isFirst", isFirst);
			// 是否为最后一页
			json.put("isLast", isLast);
			// 总条数
			json.put("total", total);
			// 总页数
			json.put("totalPages", totalPages);
			json.put(key, values);
			return json.toString();
		} catch (Exception e) {
			logger.error("BaseController:", e.getMessage());
			return error(-1, "服务器异常,请稍候再试!");
		}
	}
	public String trimEnd(String param, String trimChars) {
		if (param.endsWith(trimChars)) {
			param = param.substring(0, param.length() - trimChars.length());
		}
		return param;
	}
	/**
	 * 无效用户消息返回
	 * @param e
	 * @param defaultCode
	 * @param defaultMsg
	 * @return
	 */
	public String invalidUserException(Exception e, int defaultCode, String defaultMsg) {
		try {
			// if (e instanceof UndeclaredThrowableException) {
			// UndeclaredThrowableException ute = (UndeclaredThrowableException) e;
			// InvalidUserException iue = (InvalidUserException) ute.getUndeclaredThrowable();
			// if (iue != null) {
			// return error(iue.getCode(), iue.getMsg());
			// }
			// }
			return error(defaultCode, defaultMsg);
		} catch (Exception e2) {
			return null;
		}
	}
}

+ 81 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/controller/ExtractDataController.java

@ -0,0 +1,81 @@
package com.yihu.jw.statistics.controller;
import com.yihu.jw.statistics.service.ExtractDataService;
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.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;
/**
 * Created by chenweida on 2017/10/16.
 */
@RestController
@RequestMapping("/extractDataToEs")
@Api(description = "抽取mysql数据到es")
public class ExtractDataController extends BaseController {
    @Autowired
    private ExtractDataService extractDataService;
    @ApiOperation(value = "从mysql抽取某一天的全部指标数据到ES")
    @RequestMapping(value = "/extractOneDate", method = RequestMethod.POST)
    public String extractOneDate(
            @ApiParam(name = "date", value = "时间(yyyy-MM-dd)", required = true) @RequestParam(value = "date", required = true) String date) {
        try {
            extractDataService.extractOneDate(date);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    @ApiOperation(value = "从mysql抽取某一天的某一个指标数据到ES")
    @RequestMapping(value = "/extractOneDateWithId", method = RequestMethod.POST)
    public String extractOneDateWithId(
            @ApiParam(name = "date", value = "时间(yyyy-MM-dd)", required = true) @RequestParam(value = "date", required = true) String date,
            @ApiParam(name = "quotaId", value = "指标ID", required = true) @RequestParam(value = "quotaId", required = true) String quotaId) {
        try {
            extractDataService.extractOneDateWithId(date,quotaId);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    @ApiOperation(value = "从mysql抽取某一天到某一天的全部指标数据到ES")
    @RequestMapping(value = "/extractDate2Date", method = RequestMethod.POST)
    public String extractDate2Date(
            @ApiParam(name = "startDate", value = "开始时间包含头(yyyy-MM-dd)", required = true) @RequestParam(value = "startDate", required = true) String startDate,
            @ApiParam(name = "endDate", value = "结束时间包含尾(yyyy-MM-dd)", required = true) @RequestParam(value = "endDate", required = true) String endDate) {
        try {
            extractDataService.extractDate2Date(startDate,endDate);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    @ApiOperation(value = "从mysql抽取某一天到某一天的某一个指标数据到ES")
    @RequestMapping(value = "/extractDate2DateWithId", method = RequestMethod.POST)
    public String extractDate2DateWithId(
            @ApiParam(name = "startDate", value = "开始时间包含头(yyyy-MM-dd)", required = true) @RequestParam(value = "startDate", required = true) String startDate,
            @ApiParam(name = "endDate", value = "结束时间包含尾(yyyy-MM-dd)", required = true) @RequestParam(value = "endDate", required = true) String endDate,
            @ApiParam(name = "quotaId", value = "指标ID", required = true) @RequestParam(value = "quotaId", required = true) String quotaId) {
        try {
            extractDataService.extractDate2DateWithId(startDate,endDate,quotaId);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
}

+ 372 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/controller/JobController.java

@ -0,0 +1,372 @@
package com.yihu.jw.statistics.controller;
import com.yihu.jw.statistics.service.JobService;
import com.yihu.jw.statistics.util.hibernate.HibenateDemo;
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 BaseController {
    @Autowired
    private  JobService jobService;
    @Autowired
    private HibenateDemo hibenateDemo;
    /**
     * 启动任务
     *
     * @param id id
     * @return
     */
    @ApiOperation(value = "根据ID立即单个任务")
    @RequestMapping(value = "startNowById", method = RequestMethod.GET)
    public String startNowById(
            @ApiParam(name = "id", value = "任务ID", required = true)@RequestParam(value = "id", required = true) String id) {
        try {
            jobService.startNowById(id);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 生成过去几天的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去几天的数据")
    @RequestMapping(value = "productDataByDay", method = RequestMethod.GET)
    public String productDataByDay(  @ApiParam(name = "day", value = "距离今天的天数(如果是要生成昨天的数据,day=1)")@RequestParam(value = "day", required = true) int day) {
        try {
            jobService.productDataByDay(day);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 生成过去某一天的全部的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去某一天的全部的数据")
    @RequestMapping(value = "productDataByOneDay", method = RequestMethod.GET)
    public String productDataByOneDay( @ApiParam(name = "day", value = "yyyy-MM-dd")@RequestParam(value = "day", required = true)String day) {
        try {
            jobService.productDataByOneDay(day);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 生成过去某一天到某一天的全部的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的全部的数据(包含头尾)")
    @RequestMapping(value = "productDataByDayToDay", method = RequestMethod.GET)
    public String 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) {
        try {
            jobService.productDataByDayToDay(start,end);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 生成过去某一天到某一天的某个指标的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的某个指标的数据(包含头尾)")
    @RequestMapping(value = "productDataByDayToDayAndId", method = RequestMethod.GET)
    public String 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) {
        try {
            
            jobService.productDataByDayToDayAndId(start,end,ids);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 生成过去某一天的某一个指标的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去某一天的全部的数据")
    @RequestMapping(value = "productDataByOneDayWithId", method = RequestMethod.GET)
    public String 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) {
        try {
            jobService.productDataByOneDayWithId(day, id);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 生成过去到现在的全部的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去到现在的全部的数据")
    @RequestMapping(value = "productDataByDayAndId", method = RequestMethod.GET)
    public String 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) {
        try {
            jobService.productDataByDayAndId(day, id);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 启动任务
     *
     * @param id id
     * @return
    */
    @ApiOperation(value = "启动单个任务")
    @RequestMapping(value = "startById", method = RequestMethod.GET)
    public String startById(
            @ApiParam(name="id",value="任务id",required=true)@RequestParam(value = "id", required = true) String id) {
        try {
            jobService.startById(id);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 停止任务
     *
     * @param id id
     * @return
     */
    @ApiOperation(value = "停止单个任务")
    @RequestMapping(value = "stopById", method = RequestMethod.GET)
    public String stopById(@ApiParam(name="id",value="任务id",required=true)@RequestParam(value = "id", required = true)String id) {
        try {
            jobService.stopById(id);
            return success("停止成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 停止所有任务
     *
     * @return
     */
    @ApiOperation(value = "停止所有任务")
    @RequestMapping(value = "stopAll", method = RequestMethod.GET)
    public String stopAll() {
        try {
            jobService.stopAll();
            return success("停止成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 启动所有任务
     *
     * @return
     */
    @ApiOperation(value = "启动所有任务")
    @RequestMapping(value = "startAll", method = RequestMethod.GET)
    public String startAll() {
        try {
            jobService.startAll();
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    @ApiOperation(value = "清除緩存")
    @RequestMapping(value = "cleanCache", method = RequestMethod.GET)
    public String cleanCache() {
        try {
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 启动判断的任务
     *
     * @return
     */
    @ApiOperation(value = "启动清除缓存的任务")
    @RequestMapping(value = "startCleanCacheJob", method = RequestMethod.GET)
    public String startCleanCacheJob() {
        try {
            jobService.startCleanCacheJob();
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 停止判断的任务
     *
     * @return
     */
    @ApiOperation(value = "停止清除缓存的任务")
    @RequestMapping(value = "stopCleanCacheJob", method = RequestMethod.GET)
    public String stopCleanCacheJob() {
        try {
            jobService.stopCleanCacheJob();
            return success("停止成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    //================================================没有休眠时间=============================================================
    /**
     * 生成过去某一天到某一天的某个指标的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的某个指标的数据(包含头尾)没有休眠时间")
    @RequestMapping(value = "productDataByDayToDayAndIdNoSleep", method = RequestMethod.GET)
    public String 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) {
        try {
            jobService.productDataByDayToDayAndIdNoSleep(start,end,id,sleepTime);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 生成过去某一天的全部的数据
     *
     * @param day
     * @return
     */
    @ApiOperation(value = "生成过去某一天的全部的数据")
    @RequestMapping(value = "productDataByOneDayNoSleep", method = RequestMethod.GET)
    public String 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) {
        try {
            jobService.productDataByOneDayNoSleep(day,sleepTime);
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /************************************************************************************/
    /**
     * 生成过去某一天到某一天的全部的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的全部的数据(包含头尾)按周或月统计")
    @RequestMapping(value = "productDataByDayToDay2", method = RequestMethod.GET)
    public String 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) {
        try {
            if(incrementInterval==2){
                jobService.productWeekByDayToDay(start,end,id);
            }else if(incrementInterval==3){
                jobService.productMonthByDayToDay(start,end,id);
            }else{
                return success("启动失败!");
            }
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    @ApiOperation(value = "測試oracle")
    @RequestMapping(value = "testOracle", method = RequestMethod.GET)
    public String testOracle(@ApiParam(name = "id", value = "id")
                                 @RequestParam(value = "id", required = false)String id,
                             @ApiParam(name = "name", value = "姓名")
                                 @RequestParam(value = "name", required = false)String name,
                             @ApiParam(name = "createTime", value = "创建时间")
                                 @RequestParam(value = "createTime", required = false)String createTime,
                             @ApiParam(name = "page", value = "第几页,1开始")
                                 @RequestParam(value = "page", required = true)Integer page,
                             @ApiParam(name = "size", value = "每页大小")
                                 @RequestParam(value = "size", required = true)Integer size) {
        try {
            return write(200,"启动成功!","data",hibenateDemo.findDoctorInfo(id,name,createTime,page,size));
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
}

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

@ -0,0 +1,278 @@
package com.yihu.jw.statistics.controller;
import com.yihu.jw.statistics.etl.cache.Cache;
import com.yihu.jw.statistics.etl.save.es.ElasticFactory;
import com.yihu.jw.statistics.util.DateUtil;
import com.yihu.jw.statistics.util.ElasticsearchUtil;
import com.yihu.jw.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();
            }
        }
    }
}

+ 17 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/JobConfigDao.java

@ -0,0 +1,17 @@
package com.yihu.jw.statistics.dao;
import com.yihu.jw.entity.job.QuartzJobConfig;
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 Administrator on 2016/8/16.
 */
public interface JobConfigDao extends PagingAndSortingRepository<QuartzJobConfig, String>,JpaSpecificationExecutor<QuartzJobConfig> {
    @Query(" from QuartzJobConfig p where p.status=?1 and p.del='1'")
    List<QuartzJobConfig> findByStatus(String s);
}

+ 33 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/QuartzJobConfigDao.java

@ -0,0 +1,33 @@
package com.yihu.jw.statistics.dao;
import com.yihu.jw.entity.job.QuartzJobConfig;
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<QuartzJobConfig, String>, JpaSpecificationExecutor<QuartzJobConfig> {
    @Query(" FROM QuartzJobConfig a WHERE a.id=?1 and a.status=?2 and a.del='1'")
    QuartzJobConfig findById(String code, String status);
    @Query(" FROM QuartzJobConfig a WHERE a.status=?1 and a.del='1'")
    List<QuartzJobConfig> findByAll(String s);
    @Query(" FROM QuartzJobConfig a WHERE  a.del='1' and a.id !=8 order by a.id asc")
    List<QuartzJobConfig> findByIds();
    @Query(" FROM QuartzJobConfig a WHERE a.id=?1 and a.del='1'")
    QuartzJobConfig findById(String id);
    @Modifying
    @Query(" update QuartzJobConfig a set a.status=?2 where a.id=?1 ")
    int updateStatus(String id, String status);
}

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

@ -0,0 +1,11 @@
package com.yihu.jw.statistics.dao;
import com.yihu.jw.entity.job.QuartzJobLog;
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<QuartzJobLog, String>, JpaSpecificationExecutor<QuartzJobLog> {
}

+ 11 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/QuotaDao.java

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

+ 17 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/WlyyDimensionQuotaDao.java

@ -0,0 +1,17 @@
package com.yihu.jw.statistics.dao;
import com.yihu.jw.statistics.vo.BaseDimensionQuota;
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<BaseDimensionQuota, Long>, JpaSpecificationExecutor<BaseDimensionQuota> {
    @Query("from BaseDimensionQuota w where w.quotaCode=?1 order by sort asc")
    List<BaseDimensionQuota> findDimensionQuotasByQuotaCode(String id);
}

+ 30 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/dao/WlyyQuotaResultDao.java

@ -0,0 +1,30 @@
package com.yihu.jw.statistics.dao;
import com.yihu.jw.entity.quota.WlyyQuotaResult;
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/16.
 */
public interface WlyyQuotaResultDao extends PagingAndSortingRepository<WlyyQuotaResult, String>, JpaSpecificationExecutor<WlyyQuotaResult> {
    @Query(" FROM WlyyQuotaResult a WHERE a.quotaDate =?1 and a.quatoCode=?2")
    List<WlyyQuotaResult> findByYesterday(String yesterday, String quatoCode);
    @Modifying
    @Query(" delete FROM WlyyQuotaResult a WHERE a.quotaDate =?1 and a.quatoCode=?2")
    void deleteByDateAndCode(String yesterday, String quatoCode);
    @Query(" FROM WlyyQuotaResult a WHERE a.level1Type =?1 and a.qkdoctorJobName != ?2 and a.quatoCode=1")
    List<WlyyQuotaResult> findByLevel1TypeAndQkdoctorJobName(String level1Type, String qkdoctorJobName);
    @Query(" FROM WlyyQuotaResult a WHERE a.quotaDate =?1 and a.level1Type = 1  and a.quatoCode !=18 and a.quatoCode !=19")
    List<WlyyQuotaResult> findByDate(String date);
    @Query(" FROM WlyyQuotaResult a WHERE a.quotaDate =?1 and a.quatoCode=?2  and a.level1Type = 1 and a.quatoCode !=18 and a.quatoCode !=19 ")
    List<WlyyQuotaResult> findByDateAndId(String date, String quotaId);
}

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

@ -0,0 +1,28 @@
package com.yihu.jw.statistics.etl.cache;
import com.yihu.jw.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();
    }
}

+ 245 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/compute/ComputeHelper.java

@ -0,0 +1,245 @@
package com.yihu.jw.statistics.etl.compute;
import com.yihu.jw.statistics.util.DateUtil;
import com.yihu.jw.statistics.vo.*;
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<BaseDimensionQuota> dimensionQuotas, String wlyyJobCongId, String endTime, String timeLevel) {
        try {
            //得到全部团队
            List<BaseDoctorDO> doctors = findAllDoctor();
            //计算数据 返回map key 根据团队和维度拼凑在map中的key   1-2-1  如果维度是长度是0返回团队id 1
            Map<String, List<DataModel>> computeMap = computeDataModel(dimensionQuotas, dataModels);
            //初始化map并且设置统计出来的数目
            List<SaveModel> savemodels = initAndSetResult(doctors, 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<BaseDimensionQuota> 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.getDoctor());
                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.getDoctor();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 得到全部医生
     *
     * @return
     */
    private List<BaseDoctorDO> findAllDoctor() {
        String sql = "SELECT " +
                "bd.id AS code , " +
                "bd.name ," +
                "bdh.dept_code," +
                "hd.name AS dept_name," +
                "bdh.org_code," +
                "bdh.org_name," +
                "o.town_code," +
                "o.town_name" +
                " FROM " +
                "base_doctor_hospital bdh, " +
                "base_doctor bd," +
                "base_org o," +
                "dict_hospital_dept hd " +
                " WHERE bdh.doctor_code = bd.id  " +
                "AND hd.code= bdh.dept_code " +
                "AND o.code = bdh.org_code " +
                "AND bdh.del=1 AND hd.CONSULT_DEPT_FLAG='1' AND bd.job_title_code IN('1201','1301','1401','1101','066','067','061','1310','061','065','1','2','3','4')";
        List<BaseDoctorDO> doctorDOS = jdbcTemplate.query(sql, new BeanPropertyRowMapper(BaseDoctorDO.class));
        return doctorDOS;
    }
    public static void main(String[] args) {
        System.out.println("3502060200".substring(0, 8));
    }
    /**
     * 初始化map
     */
    private List<SaveModel> initAndSetResult(List<BaseDoctorDO> doctors, List<BaseDimensionQuota> 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 < doctors.size(); i++) {
            BaseDoctorDO one = doctors.get(i);
            //排除测试机构
            if (one.getOrgCode().length() >= 11) {
                SaveModel saveModel = new SaveModel();
                saveModel.setCity("350200");
                saveModel.setCityName("厦门市");
                saveModel.setTown(one.getTownCode());
                saveModel.setTownName(one.getTownName());
                //把末尾不是00的机构转换成00
                String orgCode = one.getOrgCode();
                saveModel.setHospital(orgCode);
                saveModel.setHospitalName(one.getOrgName());
                saveModel.setDoctor(one.getCode());
                saveModel.setDoctorName(one.getName());
                saveModel.setDept(one.getDeptCode());
                saveModel.setDeptName(one.getDeptName());
                saveModel.setQuotaCode(wlyyJobCongId);
                saveModel.setCreateTime(new Date());
                saveModel.setTimeLevel(timeLevel);
                saveModel.setAreaLevel("6");//医生
                saveModel.setQuotaDate(DateUtil.strToDate(endTime, "yyyy-MM-dd"));
                lastMaps.put(one.getCode(), saveModel);
            }
        }
        //根据维度初始化数据
            for (int i = 0; i < dimensionQuotas.size(); i++) {
                Map<String, SaveModel> mapTemp = new HashMap<>();
                BaseDimensionQuota 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<>();
//            BaseDimensionQuota 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);
                    Double num = 0.0;
                    if (dataModel.getNum()!=null){
                        num=dataModel.getNum();
                    }else {
                        num=1.0;
                    }
                    result1 += num;
                    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;
    }
}

+ 24 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/Convert.java

@ -0,0 +1,24 @@
package com.yihu.jw.statistics.etl.convert;
import com.yihu.jw.statistics.vo.BaseDimensionQuota;
import com.yihu.jw.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, BaseDimensionQuota temp );
}

+ 50 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/ConvertHelper.java

@ -0,0 +1,50 @@
package com.yihu.jw.statistics.etl.convert;
import com.yihu.jw.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.jw.statistics.vo.*;
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<BaseDimensionQuota> dimensionQuotas) throws Exception {
        List<DataModel> data = filterModel.getData();
        for (int i = 0; i < dimensionQuotas.size(); i++) {
            BaseDimensionQuota 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, BaseDimensionQuota.class);
                data = (List<DataModel>) method.invoke(obj, jdbcTemplate, data, String.valueOf(i + 1), temp);
                filterModel.setData(data);
            }
        }
        return filterModel;
    }
}

+ 68 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/convert/wlyy/ConsultTypeConvert.java

@ -0,0 +1,68 @@
package com.yihu.jw.statistics.etl.convert.wlyy;
import com.yihu.jw.statistics.etl.convert.Convert;
import com.yihu.jw.statistics.vo.BaseDimensionQuota;
import com.yihu.jw.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 wangzhinan on 2020/2/17.
 */
public class ConsultTypeConvert implements Convert {
    /**
     * @param oneList    数据
     * @param slaveLevel 从1开始
     * @return
     */
    @Override
    public List<DataModel> convert(JdbcTemplate jdbcTemplate, List<DataModel> oneList, String slaveLevel, BaseDimensionQuota temp) {
        Map<String,Object> tempMap = new HashMap<>();
        List<DataModel> result = new ArrayList<>();
        for(DataModel one:oneList) {
            try {
                String key = getSlaveKey1(one.getSlaveKey1());
                DataModel.class.getMethod("setSlaveKey" + slaveLevel, String.class).invoke(one, key);
//                result.add(one);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        return oneList;
    }
    public String getSlaveKey1(String slaveKey1) {
        if (slaveKey1.equalsIgnoreCase("1")) {
            return "1";
        } if (slaveKey1.equalsIgnoreCase("15")) {
            return "1";
        } else {
            return slaveKey1;
        }
    }
    public String getSlaveKey1Name(String slaveKey1) {
        if (slaveKey1.equalsIgnoreCase("1")) {
            return "专家咨询";
        } if (slaveKey1.equalsIgnoreCase("15")) {
            return "专家咨询";
        } if (slaveKey1.equalsIgnoreCase("9")) {
            return "图文复诊";
        }if (slaveKey1.equalsIgnoreCase("12")) {
            return "专科协同";
        }if (slaveKey1.equalsIgnoreCase("16")) {
            return "视频复诊";
        }else {
            return "其他";
        }
    }
}

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

@ -0,0 +1,33 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/etl/convert/wlyy/model/PrescriptionDisease.java

@ -0,0 +1,26 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/etl/convert/wlyy/model/ServerType.java

@ -0,0 +1,25 @@
package com.yihu.jw.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;
    }
}

+ 96 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/ExtractHelper.java

@ -0,0 +1,96 @@
package com.yihu.jw.statistics.etl.extract;
import com.yihu.jw.entity.job.QuartzJobConfig;
import com.yihu.jw.statistics.etl.extract.db.DBExtract;
import com.yihu.jw.statistics.etl.extract.db.ESExtract;
import com.yihu.jw.statistics.util.SpringUtil;
import com.yihu.jw.statistics.vo.DataModel;
import org.apache.commons.collections.map.HashedMap;
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;
import java.util.Map;
/**
 * 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(QuartzJobConfig 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, year);
            String sqlCount = initSql(wlyyJobConfigVO.getSqlCount() + "  " + timeSql, year);
            logger.info(" sql: " + sql);
            logger.info(" sqlCount: " + sqlCount);
            Map<String,Object> params = new HashedMap();
            params.put("startTime", startTime);
            params.put("endTime", endTime);
            /*params.put("startTime", DateUtil.strToDate(startTime,"yyyy-MM-dd HH:mm:ss"));
            params.put("endTime", DateUtil.strToDate(endTime,"yyyy-MM-dd HH:mm:ss"));*/
            //如果是数据库从数据库抽取
            if (StringUtils.isEmpty(wlyyJobConfigVO.getExtractType())||"1".equals(wlyyJobConfigVO.getExtractType())){
                //抽取数据库
                return SpringUtil.getBean(DBExtract.class).extractByPage(
                        DataModel.class,
                        sql,
                        sqlCount,
                        true,
                        jdbcTemplate,params);
            }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 year) {
        return sql.replace("[year]", year).replace("[im]",imDatabaseName);
    }
}

+ 67 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/db/DBExtract.java

@ -0,0 +1,67 @@
package com.yihu.jw.statistics.etl.extract.db;
import com.yihu.jw.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;
import java.util.Map;
/**
 * 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,Map<String,Object> params)throws  Exception{
        return SpringUtil.getBean(DBPageExtract.class).extractByPage(clazz,sql,countSql,pageSize,isMultithreading,jdbcTemplate,params);
    }
    /**
     * 分页抽取
     * @param clazz
     * @param sql
     * @param isMultithreading 是否多线程抽取 默认否
     * @return
     */
    public List<T> extractByPage(Class<T> clazz, String sql, String countSql, Boolean isMultithreading, JdbcTemplate jdbcTemplate, Map<String,Object> params)throws  Exception{
        if(StringUtils.isEmpty(countSql)){
            return extract(clazz,sql,jdbcTemplate);
        }
        return SpringUtil.getBean(DBPageExtract.class).extractByPage(clazz,sql,countSql,pageSize,isMultithreading,jdbcTemplate,params);
    }
    /**
     * 分页抽取
     * @param clazz
     * @param sql
     * @return
     */
    public List<Object> extractByPage(Class<T> clazz,String sql,JdbcTemplate jdbcTemplate,Map<String,Object> params)throws  Exception{
        Boolean isMultithreading=false;
        String countSql="";
        return SpringUtil.getBean(DBPageExtract.class).extractByPage(clazz,sql,countSql,pageSize,isMultithreading,jdbcTemplate,params);
    }
}

+ 189 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/db/DBPageExtract.java

@ -0,0 +1,189 @@
package com.yihu.jw.statistics.etl.extract.db;
import com.yihu.jw.statistics.util.hibernate.HibenateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
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;
import java.util.Map;
/**
 * Created by Administrator on 2016.10.16.
 * 分页抽取器
 */
@Component
@Scope("prototype")
public class DBPageExtract<T> {
    private  List<T> returnList=new ArrayList<T>();
    @Autowired
    private HibenateUtils hibenateUtils;
    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,Map<String,Object> params) 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,params);
        }else{
            return noMultiThreadExtract(clazz, sql, pageSize,jdbcTemplate,params);
        }
    }
    /**
     * 不用多线程抽取
     * @param clazz
     * @param sql
     * @param pageSize
     * @return
     */
    private List<T> noMultiThreadExtract(Class<T> clazz, String sql, int pageSize,JdbcTemplate jdbcTemplate,Map<String,Object> params) throws Exception {
        List<T> returnList=new ArrayList<T>();
        int start=0;
        int page=1;
        while (true){
            List<Map<String,Object>> mapList = new ArrayList<>();
            if (sql.contains("startTime")||sql.contains("endTime")){
                mapList = hibenateUtils.createSQLQuery(sql,params,start,pageSize);
            }else {
                mapList = hibenateUtils.createSQLQuery(sql,start,pageSize);
            }
            List<T> listTemp = hibenateUtils.castMapToBean(mapList,clazz);
            /*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,Map<String,Object> params) throws Exception {
        try{
            //得到数据的总数
            Integer dataCount=getCount(countSql,jdbcTemplate,params);
            //根据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,params);
                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,params);
        }
    }
    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,Map<String,Object> params) {
        Long count = 0L;
        List<Map<String,Object>> total = new ArrayList<>();
        if (countSql.contains("startTime")||countSql.contains("endTime")){
            total = hibenateUtils.createSQLQuery(countSql,params);
        }else {
            total = hibenateUtils.createSQLQuery(countSql);
        }
        if(count!=null){
            //mysql 与 Oracle 聚合函数返回类型不一致,需要判断装换
            count = hibenateUtils.objTransformLong(total.get(0).get("total"));
        }
        Integer countMap = count.intValue();
        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,Map<String,Object> params) throws Exception {
 /*       String finalSql=sql+" limit "+start+","+pageSize; //拼凑分页的语句
        List<T> listTemp= jdbcTemplate.query(finalSql,new BeanPropertyRowMapper(clazz));*/
        List<Map<String,Object>> mapList = new ArrayList<>();
        start  =1;
        if (sql.contains("startTime")||sql.contains("endTime")){
            mapList = hibenateUtils.createSQLQuery(sql,params,start,pageSize);
        }else {
            mapList = hibenateUtils.createSQLQuery(sql,start,pageSize);
        }
        List<T> listTemp = hibenateUtils.castMapToBean(mapList,clazz);
        if (listTemp!=null&&listTemp.size()!=0){
            addData(listTemp);
        }
        return new AsyncResult<>(true);
    }
}

+ 158 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/db/Data2Save.java

@ -0,0 +1,158 @@
package com.yihu.jw.statistics.etl.extract.db;
import com.yihu.jw.entity.job.QuartzJobConfig;
import com.yihu.jw.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.jw.statistics.vo.*;
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, QuartzJobConfig quartzJobConfig, Date quotaDate, String timeLevel) throws Exception{
        //得到全部的指标
        List<BaseDimensionQuota> 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.getDoctor());
                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.getDoctor())==null) {
                    dataModelMap.put(sb.toString(), dataModel);
                }else{
                    double result1 = dataModelMap.get(dataModel.getDoctor()).getResult1();
                    dataModel.setResult1(dataModel.getResult1()+result1);
                    dataModelMap.put(sb.toString(), dataModel);
                }
            }
        }
        //得到全部团队
        List<BaseDoctorDO> doctors = findAllDoctor();
        List<SaveModel> savelist = new ArrayList<>();
        //没维度
        for (BaseDoctorDO doctorDO : doctors) {
            if(doctorDO.getOrgCode().length() != 10){
                continue;
            }
            SaveModel saveModel = new SaveModel();
            saveModel.setCity("350200");
            saveModel.setCityName("厦门市");
            saveModel.setTown(doctorDO.getTownCode());
            saveModel.setTownName(doctorDO.getTownName());
            saveModel.setHospital(doctorDO.getOrgCode());
            saveModel.setHospitalName(doctorDO.getOrgName());
            saveModel.setDoctor(doctorDO.getCode());
            saveModel.setDoctorName(doctorDO.getName());
            saveModel.setDept(doctorDO.getDeptCode());
            saveModel.setDeptName(doctorDO.getDeptName());
            saveModel.setQuotaCode(quartzJobConfig.getQuotaId());
            saveModel.setAreaLevel(SaveModel.doctorLevel);
            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(doctorDO.getCode());
                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.getDoctor());
                    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<BaseDoctorDO> findAllDoctor() {
        String sql = "SELECT " +
                "bd.id, " +
                "bd.`name`," +
                "bdh.dept_code," +
                "bdh.dept_name," +
                "bdh.org_code," +
                "bdh.org_name," +
                "o.town_code," +
                "o.town_name" +
                " FROM " +
                "`base_doctor_hospital` bdh, " +
                "base_doctor bd," +
                "base_org o " +
                "WHERE bdh.doctor_code = bd.id " +
                "AND o.`code` = bdh.org_code " +
                "AND bdh.del=1";
        List<BaseDoctorDO> doctorDOS = jdbcTemplate.query(sql, new BeanPropertyRowMapper(BaseDoctorDO.class));
        return doctorDOS;
    }
}

+ 70 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/etl/extract/db/ESExtract.java

@ -0,0 +1,70 @@
package com.yihu.jw.statistics.etl.extract.db;
import com.yihu.jw.entity.job.QuartzJobConfig;
import com.yihu.jw.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.jw.statistics.util.ElasticsearchUtil;
import com.yihu.jw.statistics.vo.BaseDimensionQuota;
import com.yihu.jw.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(QuartzJobConfig wlyyJobConfigVO) {
        //扩展维度
        String sql= initSql(wlyyJobConfigVO.getSql(),wlyyJobConfigVO.getStartTime(),wlyyJobConfigVO.getEndTime());
        String re = "";
        //得到该指标的维度
        List<BaseDimensionQuota> 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-es/src/main/java/com/yihu/jw/statistics/etl/filter/FilterHelper.java

@ -0,0 +1,47 @@
package com.yihu.jw.statistics.etl.filter;
import com.yihu.jw.statistics.vo.DataModel;
import com.yihu.jw.statistics.vo.ErrModel;
import com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/etl/filter/filter/NotNullFilter.java

@ -0,0 +1,34 @@
package com.yihu.jw.statistics.etl.filter.filter;
import com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/etl/save/SaveHelper.java

@ -0,0 +1,29 @@
package com.yihu.jw.statistics.etl.save;
import com.yihu.jw.statistics.etl.save.es.ElastricSearchSave;
import com.yihu.jw.statistics.util.SpringUtil;
import com.yihu.jw.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);
    }
}

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

@ -0,0 +1,149 @@
package com.yihu.jw.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.elasticsearch.shield.ShieldPlugin;
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;
    @Value("${es.securityUser}")
    private String securityUser;
    @Value("${es.pwflag}")
    private String pwflag;
    @Value("${es.user}")
    private String user;
    @Value("${es.password}")
    private String password;
//-----------------------------------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)
                .defaultCredentials(user,password)
                .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 {
            //1需要加密初始化
            if("1".equals(pwflag)){
                initTranClientPw();
            }else {
                initTranClient();
            }
            return transportClient;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 9300
     * 互联网医院版本需要密码
     * @throws UnknownHostException
     */
    private synchronized void initTranClientPw() throws Exception {
        if (transportClient == null) {
            String[] hosts = tHost.split(",");
            Settings settings = getSettings();
            transportClient = TransportClient.builder().addPlugin(ShieldPlugin.class).settings(settings).build();
            for (String oneHost : hosts) {
                String[] hostAndport = oneHost.split(":");
                transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndport[0]), Integer.valueOf(hostAndport[1])));
            }
        }
    }
    /**
     * 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])));
            }
        }
    }
    /**
     * 配置连接
     * @return
     * @throws Exception
     */
    private Settings getSettings() throws Exception {
        Settings.Builder settingBuilder = Settings.settingsBuilder();
        settingBuilder.put("cluster.name", clusterName);
        settingBuilder.put("shield.user", securityUser);
        settingBuilder.put("client.transport.sniff", false);
        settingBuilder.put("transport.address.list", tHost);
        return settingBuilder.build();
    }
}

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

@ -0,0 +1,112 @@
package com.yihu.jw.statistics.etl.save.es;
import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/job/business/CacheCleanJob.java

@ -0,0 +1,29 @@
package com.yihu.jw.statistics.job.business;
import com.yihu.jw.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");
    }
}

+ 476 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/job/business/CurrentMysqlToEsQuotaJob.java

@ -0,0 +1,476 @@
package com.yihu.jw.statistics.job.business;
import com.yihu.jw.entity.job.QuartzJobConfig;
import com.yihu.jw.entity.job.QuartzJobLog;
import com.yihu.jw.statistics.dao.QuartzJobConfigDao;
import com.yihu.jw.statistics.dao.QuartzJobLogDao;
import com.yihu.jw.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.jw.statistics.etl.cache.Cache;
import com.yihu.jw.statistics.etl.compute.ComputeHelper;
import com.yihu.jw.statistics.etl.convert.ConvertHelper;
import com.yihu.jw.statistics.etl.extract.ExtractHelper;
import com.yihu.jw.statistics.etl.extract.db.Data2Save;
import com.yihu.jw.statistics.etl.filter.FilterHelper;
import com.yihu.jw.statistics.etl.save.SaveHelper;
import com.yihu.jw.statistics.etl.save.es.ElasticFactory;
import com.yihu.jw.statistics.util.DateUtil;
import com.yihu.jw.statistics.util.SpringUtil;
import com.yihu.jw.statistics.vo.BaseDimensionQuota;
import com.yihu.jw.statistics.vo.DataModel;
import com.yihu.jw.statistics.vo.FilterModel;
import com.yihu.jw.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 QuartzJobConfig 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;//增量的时间间隔(天)
    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 = DateUtil.getStringDate("yyyy-MM-dd HH:mm:ss");
        this.startTime = map.getString("startTime");
        this.wlyyJobCongId = map.getString("jobConfig");
        this.quartzJobConfig = quartzJobConfigDao.findById(wlyyJobCongId);
        this.incrementInterval = map.get("incrementInterval")!=null?String.valueOf(map.get("incrementInterval")):(quartzJobConfig.getIncrementInterval()!=null?quartzJobConfig.getIncrementInterval()+"":"1");
        //为空默认是统计昨天的数据  统计昨天的数据是从 前天的下午17:00:00 到昨天的下午17:00:00
        //初始化结束时间
        if (StringUtils.isEmpty(endTime)) {
            endTime = new LocalDate(new DateTime().minusDays(1)).toString("yyyy-MM-dd") + " 23:59:59"; //2017-06-01 17:00:00
        } else if (endTime.length()==10){
            endTime = endTime + " 23:59:59";
        }
        if("2".equals(incrementInterval)){
            endTime =  DateUtil.getSundayOfThisDate(DateUtil.strToDateLong(endTime));
        }else if("3".equals(incrementInterval)){
            endTime=  DateUtil.getLastDayOfMonth((DateUtil.strToDateLong(endTime)));
        }
        //初始化统计年份
        this.year = getNowYearByDate(endTime);
        //初始化开始时间
        if ("2".equals(timeLevel)) {
            //按年度到达量
            startTime = this.year + "-01-01 23:59:59";
        } 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 + " 23:59:59";
            }
        }
        this.quotaDate = DateUtil.strToDate(endTime, "yyyy-MM-dd");
    }
    /**
     * 计算指标
     */
    private void computequota() {
        //清空缓存
        Cache.cleanCache();
        List<QuartzJobConfig> 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(QuartzJobConfig one) throws Exception{
        this.endTime = DateUtil.getStringDate("yyyy-MM-dd'T'HH:mm:ssZZZ");
        //初始化统计年份
        this.year = getNowYearByDate();
        //初始化开始时间
        if ("2".equals(timeLevel)) {
            //按年度到达量
            startTime = this.year + "-01-01T23:59:59+0800";
        } else {
            //增量
            this.startTime = new LocalDate(new DateTime().minusDays(1)).toString("yyyy-MM-dd") + "T23:59:59+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========================");
        QuartzJobLog tjQuotaLog = new QuartzJobLog();
        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(QuartzJobConfig one) {
        logger.info("========================quotaCode:" + one.getId() + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + ",timeLevel:" + timeLevel + " start========================");
        QuartzJobLog tjQuotaLog = new QuartzJobLog();
        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<BaseDimensionQuota> 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(QuartzJobConfig 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.getDoctor() + "-" + newSaveModel.getSlaveKey1() + "-" + newSaveModel.getSlaveKey2() + "-" + newSaveModel.getSlaveKey3() + "-" + newSaveModel.getSlaveKey4());
                    newSaveModelMaps.put(key.toString(), newSaveModel);
                }
                for (SaveModel esSavemodel : quarySaveModels) {
                    StringBuffer key = new StringBuffer(esSavemodel.getDoctor() + "-" + 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(QuartzJobLog tjQuotaLog) {
        quartzJobLogDao.save(tjQuotaLog);
    }
    private FilterModel convert(FilterModel dataModels, List<BaseDimensionQuota> 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<BaseDimensionQuota> dimensionQuotas, String timeLevel, QuartzJobConfig 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(QuartzJobConfig 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()) + "-01-01";
        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(1)).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";
//        }
//    }
    public void getStartTime() throws Exception{
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        if("1".equals(this.incrementInterval)){//日
            startTime = new LocalDate(new DateTime().minusDays(1)).toString("yyyy-MM-dd") + " 23:59:59";
        }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)+ " 23:59:59";
        }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)+ " 23:59:59";
        }
    }
    /**
     * 获取现在时间属于那个年度
     *
     * @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()) + "-01-01";
        Date startDate = simpleDateFormat.parse(startDateString);
        if (simpleDateFormat.parse(todayString).after(startDate)) {
            return (1900 + today.getYear()) + "";
        } else {
            return (1900 + today.getYear() - 1) + "";
        }
    }
}

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

@ -0,0 +1,305 @@
package com.yihu.jw.statistics.job.business;
import com.yihu.jw.entity.job.QuartzJobConfig;
import com.yihu.jw.entity.job.QuartzJobLog;
import com.yihu.jw.statistics.dao.QuartzJobConfigDao;
import com.yihu.jw.statistics.dao.QuartzJobLogDao;
import com.yihu.jw.statistics.etl.compute.ComputeHelper;
import com.yihu.jw.statistics.etl.convert.ConvertHelper;
import com.yihu.jw.statistics.etl.extract.ExtractHelper;
import com.yihu.jw.statistics.etl.extract.db.Data2Save;
import com.yihu.jw.statistics.etl.filter.FilterHelper;
import com.yihu.jw.statistics.etl.save.SaveHelper;
import com.yihu.jw.statistics.etl.save.es.ElasticFactory;
import com.yihu.jw.statistics.util.DateUtil;
import com.yihu.jw.statistics.util.SpringUtil;
import com.yihu.jw.statistics.vo.BaseDimensionQuota;
import com.yihu.jw.statistics.vo.DataModel;
import com.yihu.jw.statistics.vo.FilterModel;
import com.yihu.jw.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.Date;
import java.util.List;
/**
 * 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 QuartzJobConfig 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;
    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 + "-01-01T17: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(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========================");
            QuartzJobLog tjQuotaLog = new QuartzJobLog();
            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(QuartzJobLog tjQuotaLog) {
        quartzJobLogDao.save(tjQuotaLog);
    }
    private FilterModel convert(FilterModel dataModels, List<BaseDimensionQuota> 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<BaseDimensionQuota> 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()) + "-01-01";
        Date startDate = simpleDateFormat.parse(startDateString);
        if (simpleDateFormat.parse(todayString).after(startDate)) {
            return (1900 + today.getYear()) + "";
        } else {
            return (1900 + today.getYear() - 1) + "";
        }
    }
}

+ 351 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/job/business/MysqlToEsQuotaJob.java

@ -0,0 +1,351 @@
package com.yihu.jw.statistics.job.business;
import com.yihu.jw.entity.job.QuartzJobConfig;
import com.yihu.jw.entity.job.QuartzJobLog;
import com.yihu.jw.statistics.dao.QuartzJobConfigDao;
import com.yihu.jw.statistics.dao.QuartzJobLogDao;
import com.yihu.jw.statistics.dao.WlyyDimensionQuotaDao;
import com.yihu.jw.statistics.etl.cache.Cache;
import com.yihu.jw.statistics.etl.compute.ComputeHelper;
import com.yihu.jw.statistics.etl.convert.ConvertHelper;
import com.yihu.jw.statistics.etl.extract.ExtractHelper;
import com.yihu.jw.statistics.etl.filter.FilterHelper;
import com.yihu.jw.statistics.etl.save.SaveHelper;
import com.yihu.jw.statistics.etl.save.es.ElasticFactory;
import com.yihu.jw.statistics.util.DateUtil;
import com.yihu.jw.statistics.util.SpringUtil;
import com.yihu.jw.statistics.vo.BaseDimensionQuota;
import com.yihu.jw.statistics.vo.DataModel;
import com.yihu.jw.statistics.vo.FilterModel;
import com.yihu.jw.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 QuartzJobConfig 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;
    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.wlyyJobCongId = map.getString("jobConfig");
        this.quartzJobConfig = quartzJobConfigDao.findById(wlyyJobCongId);
        this.incrementInterval = map.get("incrementInterval")!=null?String.valueOf(map.get("incrementInterval")):(quartzJobConfig.getIncrementInterval()!=null?quartzJobConfig.getIncrementInterval()+"":"1");
        //为空默认是统计昨天的数据  统计昨天的数据是从 前天的下午17:00:00 到昨天的下午17:00:00
        //初始化结束时间
        if (StringUtils.isEmpty(endTime)) {
            endTime = new LocalDate(new DateTime().minusDays(1)).toString("yyyy-MM-dd") + " 23:59:59"; //2017-06-01 17:00:00
        } else {
            endTime = endTime + " 23:59:59";
        }
        if("2".equals(incrementInterval)){
            endTime =  DateUtil.getSundayOfThisDate(DateUtil.strToDateLong(endTime));
        }else if("3".equals(incrementInterval)){
            endTime=  DateUtil.getLastDayOfMonth((DateUtil.strToDateLong(endTime)));
        }
        //初始化统计年份
        this.year = getNowYearByDate(endTime);
        //初始化开始时间
        if ("2".equals(timeLevel)) {
            //按年度到达量
            startTime = this.year + "-01-01 23:59:59";
        } 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 + " 23:59:59";
            }
        }
        this.quotaDate = DateUtil.strToDate(endTime, "yyyy-MM-dd");
    }
    /**
     * 计算指标
     */
    private void computequota() {
        try {
            logger.info("========================quotaCode:" + wlyyJobCongId + "," + DateUtil.dateToStr(quotaDate, "yyyy-MM-dd") + ",timeLevel:" + timeLevel + " start========================");
            QuartzJobLog tjQuotaLog = new QuartzJobLog();
            tjQuotaLog.setJobId(wlyyJobCongId);
            tjQuotaLog.setJobStartTime(new Date());
            // 0 删除这天的数据
            deleteData(quotaDate, wlyyJobCongId, timeLevel);
            //1..抽取数据 如果是累加就是 List<DataModel>  如果是相除 Map<String,List<DataModel>>
        /*    if (quotaDate.equals(DateUtil.strToDate("2018-12-16", "yyyy-MM-dd"))){
*/
            List<DataModel> dataModels = extract();
            //2..根据规则过滤数据
            FilterModel filterModel = filter(dataModels);
            //得到该指标的维度
            List<BaseDimensionQuota> 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(QuartzJobLog tjQuotaLog) {
        quartzJobLogDao.save(tjQuotaLog);
    }
    private FilterModel convert(FilterModel dataModels, List<BaseDimensionQuota> 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<BaseDimensionQuota> 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()) + "-01-01";
        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") + " 23:59:59";
        }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)+ " 23:59:59";
        }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)+ " 23:59:59";
        }
    }
}

+ 42 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/job/business/ScheduleStatictisJob.java

@ -0,0 +1,42 @@
package com.yihu.jw.statistics.job.business;
import com.yihu.jw.statistics.service.JobService;
import com.yihu.jw.statistics.util.DateUtil;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 排班统计
 * Created by Reece on 2017/8/29/029.
 */
@DisallowConcurrentExecution//解决执行频率和间隔时间的问题。到了执行时间点前一任务还在执行中,不会执行下一次任务,直至该次任务完成
public class ScheduleStatictisJob implements Job {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleStatictisJob.class);
    @Autowired
    private JobService jobService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        logger.info("ScheduleStatictisJob start ..........");
        try {
            String startDate = DateUtil.getStringDate("yyyy-MM-dd");
            String endTime = DateUtil.getNextDay(startDate,10);
            jobService.productDataByDayToDayAndIdNoSleep(startDate,endTime,"10",0L);
            logger.info("ScheduleStatictisJob end ..........");
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("ScheduleStatictisJob error ..........,message:" + e.getMessage());
        }
    }
}

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

@ -0,0 +1,43 @@
package com.yihu.jw.statistics.service;
import com.yihu.jw.statistics.util.ElasticsearchUtil;
import com.yihu.jw.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);
    }
}

+ 295 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/service/ExtractDataService.java

@ -0,0 +1,295 @@
package com.yihu.jw.statistics.service;
import com.yihu.jw.entity.quota.WlyyQuotaResult;
import com.yihu.jw.statistics.etl.save.es.ElasticFactory;
import com.yihu.jw.statistics.etl.save.es.ElastricSearchSave;
import com.yihu.jw.statistics.util.DateUtil;
import com.yihu.jw.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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.util.StringUtils;
import javax.sql.DataSource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
 * Created by chenweida on 2017/10/16.
 */
@Service
public class ExtractDataService {
    @Autowired
    private ElasticFactory elasticFactory;
    @Value("${es.type}")
    private String esType;
    @Value("${es.index}")
    private String esIndex;
    @Autowired
    private ElastricSearchSave elastricSearchSave;//只读库的jdbcTemplate
    private JdbcTemplate jdbcTemplate;
    /**
     * 构造注入只读库的数据源
     *
     * @param dataSource
     */
    @Autowired
    public void JdbcTemplateInit(
            @Qualifier(value = "primaryRead") DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate();
        this.jdbcTemplate.setDataSource(dataSource);
    }
    private Logger logger = LoggerFactory.getLogger(ExtractDataService.class);
    public void extractOneDate(String date) {
        String sql = "select * from wlyy_quota_result w where w.level1_type=1  and w.quato_code !=18 and w.quato_code !=19 and quota_date='" + date + "' ";
        List<WlyyQuotaResult> quotaResults = jdbcTemplate.query(sql, new BeanPropertyRowMapper(WlyyQuotaResult.class));
        save2es(quotaResults, null, date);
        logger.info("date:" + date + " all quota      success");
    }
    public void extractOneDateWithId(String date, String quotaId) {
        String sql = "select * from wlyy_quota_result w where w.level1_type=1  and w.quato_code !=18 and w.quato_code !=19 and quota_date='" + date + "' and quato_code='" + quotaId + "'";
        List<WlyyQuotaResult> quotaResults = jdbcTemplate.query(sql, new BeanPropertyRowMapper(WlyyQuotaResult.class));
        save2es(quotaResults, quotaId, date);
        logger.info("date:" + date + ",quotaId:" + quotaId + "      success");
    }
    public void extractDate2Date(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 + 1; i++) {
            System.out.println(getYesterday(i, startDate));
            extractOneDate(getYesterday(i, startDate));
        }
    }
    public void extractDate2DateWithId(String start, String end, String quotaId) 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 + 1; i++) {
            extractOneDateWithId(getYesterday(i, startDate), quotaId);
        }
    }
    private 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;
    }
    private 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));
    }
    private void deleteData(Date quotaDate, String quotaCode) {
        JestClient jestClient = null;
        try {
            jestClient = elasticFactory.getJestClient();
            //先根据条件查找出来
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(
                    new BoolQueryBuilder()
                            .must(QueryBuilders.matchQuery("quotaCode", quotaCode))
                            .must(QueryBuilders.matchQuery("quotaDate", quotaDate))
            ).size(50000);//一次取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());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jestClient != null) {
                jestClient.shutdownClient();
            }
        }
    }
    private void deleteData(Date quotaDate) {
        JestClient jestClient = null;
        try {
             jestClient = elasticFactory.getJestClient();
            //先根据条件查找出来
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(
                    new BoolQueryBuilder()
                            .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());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jestClient != null) {
                jestClient.shutdownClient();
            }
        }
    }
    private void save2es(List<WlyyQuotaResult> quotaResults, String quotaCode, String quotaDate) {
        List<SaveModel> saveModel = new ArrayList<>();
        if (quotaResults != null && quotaResults.size() > 0) {
            //查询ES是否有这天的数据有就删除
            if (StringUtils.isEmpty(quotaCode)) {
                deleteData(DateUtil.strToDate(quotaDate, "yyyy-MM-dd"));
            } else {
                deleteData(DateUtil.strToDate(quotaDate, "yyyy-MM-dd"), quotaCode);
            }
            //
            quotaResults.stream().forEach(one -> {
                SaveModel saveModelTemp = new SaveModel();
                if ("25".equals(one.getQuatoCode())) {
                    //25的咨询到达量 存到3的到达量去
                    saveModelTemp.setQuotaCode("3");
                } else {
                    saveModelTemp.setQuotaCode(one.getQuatoCode());
                }
                saveModelTemp.setQuotaDate(one.getQuotaDate());
                saveModelTemp.setCreateTime(one.getCreateTime());
                saveModelTemp.setCity(one.getCity());
                saveModelTemp.setCityName(one.getCityName());
                saveModelTemp.setTown(one.getTown());
                saveModelTemp.setTownName(one.getTownName());
                saveModelTemp.setHospital(one.getOrgCode());
                saveModelTemp.setHospitalName(one.getOrgName());
                saveModelTemp.setDoctor(one.getQkdoctorCode());
                saveModelTemp.setDoctorName(one.getQkdoctorName());
                saveModelTemp.setResult1(Double.valueOf(one.getResult()));
                saveModelTemp.setResult2(Double.valueOf(one.getResult()));
                saveModelTemp.setSlaveKey1(one.getLevel2Type());
                saveModelTemp.setSlaveKey1Name(one.getLevel2TypeName());
                saveModelTemp.setSlaveKey2(one.getLevel3Type());
                saveModelTemp.setSlaveKey2Name(one.getLevel3TypeName());
                if (
                        "1".equals(one.getQuatoCode()) ||
                                "2".equals(one.getQuatoCode()) ||
                                "6".equals(one.getQuatoCode()) ||
                                "7".equals(one.getQuatoCode()) ||
                                "8".equals(one.getQuatoCode()) ||
                                "9".equals(one.getQuatoCode()) ||
                                "10".equals(one.getQuatoCode()) ||
                                "12".equals(one.getQuatoCode()) ||
                                "13".equals(one.getQuatoCode()) ||
                                "14".equals(one.getQuatoCode()) ||
                                "15".equals(one.getQuatoCode()) ||
                                "16".equals(one.getQuatoCode()) ||
                                "17".equals(one.getQuatoCode()) ||
                                "20".equals(one.getQuatoCode()) ||
                                "21".equals(one.getQuatoCode()) ||
                                "22".equals(one.getQuatoCode()) ||
                                "23".equals(one.getQuatoCode()) ||
                                "24".equals(one.getQuatoCode()) ||
                                "25".equals(one.getQuatoCode()) ||
                                "26".equals(one.getQuatoCode()) ||
                                "29".equals(one.getQuatoCode()) ||
                                "30".equals(one.getQuatoCode()) ||
                                "31".equals(one.getQuatoCode()) ||
                                "32".equals(one.getQuatoCode()) ||
                                "33".equals(one.getQuatoCode()) ||
                                "34".equals(one.getQuatoCode()) ||
                                "35".equals(one.getQuatoCode()) ||
                                "36".equals(one.getQuatoCode()) ||
                                "37".equals(one.getQuatoCode()) ||
                                "38".equals(one.getQuatoCode()) ||
                                "39".equals(one.getQuatoCode()) ||
                                "40".equals(one.getQuatoCode()) ||
                                "41".equals(one.getQuatoCode()) ||
                                "42".equals(one.getQuatoCode()) ||
                                "43".equals(one.getQuatoCode()) ||
                                "44".equals(one.getQuatoCode()) ||
                                "45".equals(one.getQuatoCode()) ||
                                "46".equals(one.getQuatoCode()) ||
                                "47".equals(one.getQuatoCode()) ||
                                "48".equals(one.getQuatoCode()) ||
                                "49".equals(one.getQuatoCode()) ||
                                "50".equals(one.getQuatoCode()) ||
                                "51".equals(one.getQuatoCode()) ||
                                "52".equals(one.getQuatoCode()) ||
                                "53".equals(one.getQuatoCode()) ||
                                "54".equals(one.getQuatoCode()) ||
                                "55".equals(one.getQuatoCode()) ||
                                "56".equals(one.getQuatoCode()) ||
                                "57".equals(one.getQuatoCode())
                        ) {
                    saveModelTemp.setTimeLevel("2");//到达量
                }
                if (
                        "3".equals(one.getQuatoCode()) ||
                                "4".equals(one.getQuatoCode()) ||
                                "5".equals(one.getQuatoCode()) ||
                                "27".equals(one.getQuatoCode())
                        ) {
                    saveModelTemp.setTimeLevel("1");//增量
                }
                saveModelTemp.setAreaLevel("5");
                saveModel.add(saveModelTemp);
            });
            elastricSearchSave.save(saveModel);
        }
    }
}

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

@ -0,0 +1,595 @@
package com.yihu.jw.statistics.service;
import com.yihu.jw.entity.job.QuartzJobConfig;
import com.yihu.jw.statistics.dao.QuartzJobConfigDao;
import com.yihu.jw.statistics.etl.cache.Cache;
import com.yihu.jw.statistics.job.business.CacheCleanJob;
import com.yihu.jw.statistics.util.DateUtil;
import com.yihu.jw.statistics.util.QuartzHelper;
import com.yihu.jw.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 {
        QuartzJobConfig 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 {
        QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "0");
        if (quartzJobConfig != null) {
            startOneJob(quartzJobConfig);
        } else {
            throw new Exception("任务已经启动");
        }
    }
    @Transactional
    public void stopAll() throws Exception {
        List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("1");
        if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
            for (QuartzJobConfig 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<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("0");
        if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
            for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
                startOneJob(quartzJobConfig);
            }
        } else {
            throw new Exception("任务已经全部启动");
        }
    }
    /**
     * 启动单个任务
     *
     * @param quartzJobConfig
     * @throws Exception
     */
    @Transactional
    private void startOneJob(QuartzJobConfig 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 {
        QuartzJobConfig 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(QuartzJobConfig 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<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByIds();
        for (QuartzJobConfig 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<QuartzJobConfig> quartzJobConfigs = jdbcTemplate.query(sql, new BeanPropertyRowMapper(QuartzJobConfig.class));
        for (QuartzJobConfig 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(QuartzJobConfig 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());
        QuartzJobConfig 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 {
        QuartzJobConfig 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());
        QuartzJobConfig 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<QuartzJobConfig> quartzJobConfigs = jdbcTemplate.query(sql, new BeanPropertyRowMapper(QuartzJobConfig.class));
        for (QuartzJobConfig 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<QuartzJobConfig> quartzJobConfigs = jdbcTemplate.query(sql, new BeanPropertyRowMapper(QuartzJobConfig.class));
        for (QuartzJobConfig 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());
    }
}

+ 139 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/task/PushMsgTask.java

@ -0,0 +1,139 @@
package com.yihu.jw.statistics.task;
import com.yihu.jw.statistics.util.ImUtill;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.LinkedBlockingQueue;
public class PushMsgTask {
	public static String url="";
	private static Logger logger = LoggerFactory.getLogger(PushMsgTask.class);
	// 最大容量为50的数组堵塞队列
	private static LinkedBlockingQueue<JSONObject> queue = new LinkedBlockingQueue<JSONObject>();
	private static PushMsgTask instance;
	private static Object lock = new Object();
	public static PushMsgTask getInstance() {
		synchronized (lock) {
			if (instance == null) {
				instance = new PushMsgTask();
				instance.run();
			}
		}
		return instance;
	}
	/**
	 * 添加一条推送消息
	 * @param receiver 接收人
	 * @param type 消息类型
	 * @param title 消息标题
	 * @param msg 消息内容
	 * @param data 消息数据
	 */
	public void put(String receiver, String type, String title, String msg, String data) {
		try {
			JSONObject json = new JSONObject();
			json.put("receiver", receiver);
			json.put("type", type);
			json.put("title", title);
			json.put("msg", msg);
			json.put("data", data);
			queue.put(json);
		} catch (Exception e) {
			logger.error("添加到消息队列失败!", e);
			e.printStackTrace();
		}
	}
	public void put(JSONArray array) {
		if (array == null || array.size() == 0) {
			return;
		}
		for (int i = 0; i < array.size(); i++) {
			JSONObject json = array.getJSONObject(i);
			if (json == null) {
				continue;
			}
			try {
				queue.put(json);
			} catch (Exception e) {
				logger.error("批量添加到消息队列失败!", e);
			}
		}
	}
	private void run() {
		new Thread(new ConsumerTask()).start();
	}
	// 消费者
	class ConsumerTask implements Runnable {
		@Override
		public void run() {
			try {
				while (true) {
					// 如果queue为空,则当前线程会堵塞,直到有新数据加入
					JSONObject json = queue.take();
					logger.info("发送前:"+json);
					// 推送平台消息
					String receiver = json.containsKey("receiver") ? json.getString("receiver") : "";
					String type = json.containsKey("type") ? json.getString("type") : "";
					String title = json.containsKey("title") ? json.getString("title") : "";
					String msg = json.containsKey("msg") ? json.getString("msg") : "";
					String data = json.containsKey("data") ? json.getString("data") : "";
					boolean res = pushMessage(receiver, type, title, msg, data);
					if (res) {
						logger.info("消息推送成功!");
					} else {
						logger.error("消息推送失败!");
					}
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
	/**
	 * 消息推送
	 *
	 * @param receiver 消息接收人
	 * @param msgType 消息类型
	 * @param title 消息标题
	 * @param msg 消息内容
	 * @param data 消息数据
	 */
	public static boolean pushMessage(String receiver, String msgType, String title, String msg, String data) {
		try {
			//List<NameValuePair> params = new ArrayList<NameValuePair>();
			//params.add(new BasicNameValuePair("to", receiver));
			//params.add(new BasicNameValuePair("content", msg));
			//params.add(new BasicNameValuePair("contentType", msgType));
			//params.add(new BasicNameValuePair("title", title));
			//params.add(new BasicNameValuePair("summary", data));
			//String response = HttpClientUtil.post(url, params, "UTF-8");
			org.json.JSONObject participants = new org.json.JSONObject();
			participants.put("system",0);
			participants.put(receiver,0);
			org.json.JSONObject sessionObj  = ImUtill.createSession(url,participants,"0","系统消息","");
			if(sessionObj.getInt("status")==-1){
				throw  new RuntimeException(sessionObj.getString("message"));
			}
			org.json.JSONObject session = sessionObj.getJSONObject("data");
			ImUtill.sendImMsg(url,"system","系统",session.getString("id"),"1", msg,msgType);
			return true;
		} catch (Exception e) {
			logger.error("push message error:", e);
		}
		return false;
	}
}

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

@ -0,0 +1,177 @@
package com.yihu.jw.statistics.util;
/**
 * 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_dept = "9";//行政区划  科室
        public static final String area_doctor = "10";//行政区划  医生
    }
    /**
     * 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_dept = "5";//行政区划  科室
        public static final String area_doctor= "6";//行政区划  医生
        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_dept: {
                    return area_dept;
                }
                case main_dimension.area_doctor: {
                    return area_doctor;
                }
            }
            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="进行中";
    }
}

+ 833 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/DateUtil.java

@ -0,0 +1,833 @@
package com.yihu.jw.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 yyyy-MM-dd
	 */
	public static Date getSundayOfThisDateToDate(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 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;
	}
}

+ 606 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/ElasticsearchUtil.java

@ -0,0 +1,606 @@
package com.yihu.jw.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.jw.statistics.etl.save.es.ElasticFactory;
import com.yihu.jw.statistics.vo.DataModel;
import com.yihu.jw.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.lang.reflect.Field;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * 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科室 6医生
     * @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.doctorLevel.equals(areaLevel)) {
            sql.append("select doctor,doctorName,result1,result2 from "+esType+" where doctor='" + code + "'");
        }else if (SaveModel.deptLevel.equals(areaLevel)) {
            sql.append("select dept,deptName,sum(result1),sum(result2) from "+esType+" where dept='" + 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科室 6医生
     * @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.doctorLevel.equals(areaLevel)) {
            sql.append("select doctor,doctprName,result1,result2 from "+esType+" where doctor='" + code + "'");
        }else if (SaveModel.deptLevel.equals(areaLevel)) {
            sql.append("select dept,deptName,sum(result1),sum(result2) from "+esType+" where dept='" + 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 或者医生code
     * @param timeLevel 1增量 2到达量
     * @param areaLevel 1 省 2 市 3 区县 4 机构 5科室6医生
     * @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.doctorLevel.equals(areaLevel)) {
            sql.append("select doctor,doctorName,slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name,result1,result2 from "+esType+" where doctor='" + code + "'");
            groupBy.append("  group by slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name");
        }else if (SaveModel.deptLevel.equals(areaLevel)) {
            sql.append("select dept,deptName,slaveKey1,slaveKey1Name,slaveKey2,slaveKey2Name,sum(result1),sum(result2) from "+esType+" where dept='" + 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 或者医生code
     * @param timeLevel 1增量 2到达量
     * @param areaLevel 1 省 2 市 3 区县 4 机构 5科室 6医生
     * @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.doctorLevel.equals(areaLevel)) {
            sql.append("select doctor,doctorName,slaveKey1,slaveKey1Name,result1,result2 from "+esType+" where doctor='" + code + "'");
            groupBy.append("  group by doctor,doctorName,slaveKey1,slaveKey1Name");
        }else if (SaveModel.deptLevel.equals(areaLevel)) {
            sql.append("select dept,deptName,slaveKey1,slaveKey1Name,sum(result1),sum(result2) from "+esType+" where dept='" + code + "'");
            groupBy.append("  group by dept,deptName,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或者医生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.doctorLevel.equals(childAreaLevel)) {
            sql.append("select doctor,doctorName,result1,result2 from "+esType+" where  ");
        }else if (SaveModel.deptLevel.equals(childAreaLevel)) {
            sql.append("select dept,deptName,sum(result1),sum(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.doctorLevel.equals(areaLevel)) {
            sql.append(" and dcotor='" + code + "'");
        }else if (SaveModel.deptLevel.equals(areaLevel)) {
            sql.append(" and dept='" + 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) {
                            Field field = clazz.getDeclaredField(heads.get(i));
                            if(field.getGenericType().getTypeName().equals("java.lang.Long")) {
                                clazz.getMethod(key, Long.class).invoke(saveModel, Long.valueOf(value + ""));
                            } else {
                                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) {
                            Field field = clazz.getDeclaredField(heads.get(i));
                            if(field.getGenericType().getTypeName().equals("java.util.Set<java.lang.String>")){
                                Set<String> set = new HashSet<>();
                                for(Object s : (ArrayList) value){
                                    set.add(s + "");
                                }
                                clazz.getMethod(key, java.util.Set.class).invoke(saveModel, set);
                            }else {
                                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-es/src/main/java/com/yihu/jw/statistics/util/HttpClientUtil.java

@ -0,0 +1,128 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/util/IdCardUtil.java

@ -0,0 +1,162 @@
package com.yihu.jw.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;
    }
}

+ 221 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/ImUtill.java

@ -0,0 +1,221 @@
package com.yihu.jw.statistics.util;
import org.json.JSONObject;
/**
 * Created by 卓 on 2017/1/13.
 */
public class ImUtill {
    /**
     * 发送消息给IM
     *
     * @param from        来自
     * @param contentType 1文字 2图片消息
     * @param content     内容
     */
    public static String sendImMsg(String baseUrl,String from,String fromName, String sessionId, String contentType, String content,String businessType) {
        String imAddr = baseUrl + "/api/v2/sessions/"+sessionId+"/messages";
        JSONObject params = new JSONObject();
        params.put("sender_id", from);
        params.put("sender_name", fromName);
        params.put("content_type", contentType);
        params.put("content", content);
        params.put("session_id", sessionId);
        params.put("business_type", businessType);
        String response = HttpClientUtil.postBody(imAddr, params);
        return response;
    }
    /**
     * 发送消息给IM
     *
     * @param from        来自
     * @param contentType 1文字 2图片消息
     * @param content     内容
     */
    public static String sendTopicIM(String baseUrl,String from,String fromName, String topicId, String contentType, String content) {
        String url = baseUrl+ ("api/v2/sessions/topic/"+topicId+"/messages");
        JSONObject params = new JSONObject();
        params.put("sender_id", from);
        params.put("sender_name", fromName);
        params.put("content_type", contentType);
        params.put("content", content);
        params.put("topic_id", topicId);;
        String response = HttpClientUtil.postBody(url, params);
        return response;
    }
    /**
     * 结束议题
     *
     * @param topicId  议题ID
     * @param endUser  结束人
     * @param endUserName 结束人名字
     * @param sessionId 会话ID
     */
    public static JSONObject endTopics(String baseUrl,String sessionId,String endUser, String endUserName,String topicId) {
        String imAddr = baseUrl + "api/v2/sessions/"+sessionId+"/topics/"+topicId+"/ended";
        JSONObject params = new JSONObject();
        params.put("session_id", sessionId);
        params.put("end_user", endUser);
        params.put("end_user_name",endUserName);
        params.put("topic_id", topicId);
        String ret = HttpClientUtil.postBody(imAddr,params);
        JSONObject obj  = null;
        try{
            obj = new JSONObject(ret);
        }catch (Exception e){
            return null;
        }
        return obj;
    }
    /**
     * 议题邀请人员
     * @param user 结束人名字
     * @param sessionId 会话ID
     */
    public static void updateTopicUser(String baseUrl,String sessionId,String user) {
        String imAddr = baseUrl + "api/v2/sessions/"+sessionId+"/participants/"+user;
        JSONObject params = new JSONObject();
        params.put("user", user+":"+0);
        HttpClientUtil.putBody(imAddr,params);
    }
    /**
     * 创建议题
     *
     * @param topicId  议题ID
     * @param topicName  议题名称
     * @param participants 成员
     */
    public static JSONObject createTopics(String baseUrl,String sessionId, String topicId, String topicName, JSONObject participants, JSONObject messages, String sessionType) {
        String imAddr = baseUrl + "api/v2/sessions/"+topicId+"/topics";
        JSONObject params = new JSONObject();
        params.put("topic_id", topicId);
        params.put("topic_name", topicName);
        params.put("participants", participants.toString());
        params.put("messages", messages.toString());
        params.put("session_id", sessionId);
        params.put("session_type", sessionType);
        String ret = HttpClientUtil.postBody(imAddr,params);
        JSONObject obj  = null;
        try{
            obj = new JSONObject(ret);
        }catch (Exception e){
            return null;
        }
        return obj;
    }
    /**
     * 创建会话(system)
     *
     */
    public static JSONObject createSession(String baseUrl,JSONObject participants,String sessionType,String sessionName,String sessionId) {
        String imAddr = baseUrl + "api/v2/sessions";
        JSONObject params = new JSONObject();
        params.put("participants", participants.toString());
        params.put("session_name", sessionName);
        params.put("session_type", sessionType);
        params.put("session_id", sessionId);
        String ret = HttpClientUtil.postBody(imAddr,params);
        JSONObject obj  = null;
        try{
            obj = new JSONObject(ret);
        }catch (Exception e){
            return null;
        }
        return obj;
    }
    /**
     * 获取会话实例的消息对象
     * @param senderId
     * @param senderName
     * @param title
     * @param description
     * @param images
     * @return
     */
    public static JSONObject getCreateTopicMessage(String senderId,String senderName,String title,String description,String images){
        JSONObject messages = new JSONObject();
        messages.put("description",description);
        messages.put("title",title);
        messages.put("img",images);
        messages.put("sender_id",senderId);
        messages.put("sender_name",senderName);
        return messages;
    }
    public static JSONObject getTopicMessage(String baseUrl,String topicId,String startMsgId,String endMsgId,int page,int pagesize,String uid){
        String url = baseUrl
                + "api/v2/sessions/topic/"+topicId+"/messages?topic_id="+topicId+"&end="+startMsgId
                +"&start="+(endMsgId==null?"":endMsgId)+"&page="+page+"&pagesize="+pagesize+"&user="+uid;
        try{
            String ret = HttpClientUtil.get(url, "UTF-8");
            JSONObject obj = new JSONObject(ret);
            if(obj.getInt("status")==-1){
                throw new RuntimeException(obj.getString("message"));
            }else{
                return  obj.getJSONObject("data");
            }
        }catch (Exception e){
            return null;
        }
    }
    /**
     * 删除对应的成员信息在MUC模式中
     * @param userId
     * @param oldUserId
     * @param sessionId
     * @return
     */
    public static JSONObject deleteMucUser(String baseUrl,String userId,String oldUserId,String sessionId) throws  Exception{
        String url = baseUrl
                + "api/v2/sessions/"+sessionId+"/participant/update";
        try{
            JSONObject params = new JSONObject();
            params.put("user_id", userId);
            params.put("old_user_id", oldUserId);
            params.put("session_id", sessionId);
            String ret = HttpClientUtil.postBody(url,params);
            JSONObject obj = new JSONObject(ret);
            if(obj.getInt("status")==-1){
                throw new RuntimeException("人员更换失败!");
            }else{
                return obj;
            }
        }catch (Exception e){
           throw new RuntimeException("人员更换失败!");
        }
    }
    /**
     * 获取议题
     * @param topicId
     * @return
     */
    public static JSONObject getTopic(String baseUrl,String topicId) throws  Exception{
        String url =baseUrl
                + "api/v2/sessions/topics/"+topicId+"?topic_id="+topicId;
        try{
            String ret = HttpClientUtil.get(url,"utf-8");
            JSONObject obj = new JSONObject(ret);
            if(obj.getInt("status")==-1){
                throw new RuntimeException("获取议题失败!");
            }else{
                return obj;
            }
        }catch (Exception e){
            throw new RuntimeException("获取议题失败!");
        }
    }
}

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

@ -0,0 +1,45 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/util/MD5.java

@ -0,0 +1,64 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/util/QuartzHelper.java

@ -0,0 +1,118 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/util/SpringUtil.java

@ -0,0 +1,63 @@
package com.yihu.jw.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);
    }
}

+ 83 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/hibernate/HibenateDemo.java

@ -0,0 +1,83 @@
package com.yihu.jw.statistics.util.hibernate;
import com.yihu.jw.statistics.util.DateUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
 * Created by Trick on 2019/12/10.
 */
@Component
public class HibenateDemo {
    @Autowired
    private HibenateUtils hibenateUtils;
    /**
     * 特别注意,由于oracle 大小写不明感,表别名需要指定大小写(驼峰)的话需要加双引号
     * @param id
     * @param name
     * @param createTime
     * @param page
     * @param size
     * @return
     */
    public Map<String,Object> findDoctorInfo(String id, String name, String createTime, Integer page, Integer size){
        Map<String,Object> rs = new HashedMap();
        String sqlTotal ="SELECT " +
                " COUNT(1) AS \"total\" " +
                " FROM " +
                " base_doctor d " +
                " JOIN base_doctor_hospital h ON d.id = h.doctor_code " +
                " WHERE " +
                " 1 = 1";
        Map<String,Object> params = new HashedMap();
        if(StringUtils.isNotBlank(id)){
            sqlTotal+=" AND d.id =:id";
            params.put("id",id);
        }
        if(StringUtils.isNotBlank(name)){
            sqlTotal+=" AND d.name =:name";
            params.put("name",name);
        }
        if(StringUtils.isNotBlank(createTime)){
            sqlTotal+=" AND d.create_time >:createTime";
            params.put("createTime", DateUtil.strToDate(createTime,"yyyy-MM-dd"));
        }
        Long count = 0L;
        List<Map<String,Object>> total = hibenateUtils.createSQLQuery(sqlTotal,params);
        if(count!=null){
            //mysql 与 Oracle 聚合函数返回类型不一致,需要判断装换
            count = hibenateUtils.objTransformLong(total.get(0).get("total"));
        }
        rs.put("total",count);
        String sql ="SELECT " +
                " d.id AS \"id\", " +
                " d.NAME AS \"name\"," +
                " h.dept_name AS \"deptName\"" +
                " FROM " +
                " base_doctor d " +
                " JOIN base_doctor_hospital h ON d.id = h.doctor_code " +
                " WHERE " +
                " 1 = 1 ";
        if(StringUtils.isNotBlank(id)){
            sql+=" AND d.id =:id";
        }
        if(StringUtils.isNotBlank(name)){
            sql+=" AND d.name =:name";
        }
        if(StringUtils.isNotBlank(createTime)){
            sql+=" AND d.create_time >:createTime";
        }
        List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql,params,page,size);
        rs.put("list",list);
        return rs;
    }
}

+ 246 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/util/hibernate/HibenateUtils.java

@ -0,0 +1,246 @@
package com.yihu.jw.statistics.util.hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
/**
 * 数据库查询工具,mysql与Oracle 通用复诊查询
 * 支持原生SQL。联表,动态传参。分页查询
 * 不支持数据库特有的函数
 * <p>
 * Created by Trick on 2019/12/10.
 */
@Component
@Transactional
public class HibenateUtils {
    /**
     * JPA 数据源
     */
    @PersistenceContext
    private EntityManager entityManager;
    /**
     * 创建SQL查询,根据配置,获取数据库方言
     * sql为原生sql语句,查询,Oracle不区分大小写,返回值需要驼峰命名,字段别名需要加 双引号
     * 目前只支持通用的聚合函数,特有的聚合函数不支持
     *
     * @param sql
     * @param page 第几页,1开始
     * @param size 每页记录数
     * @return
     */
    public List<Map<String, Object>> createSQLQuery(String sql, Integer page, Integer size) {
        Session session = (Session) entityManager.getDelegate();
        SQLQuery query = session.createSQLQuery(sql);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        if(page!=null&&size!=null){
            query.setFirstResult((page - 1) * size);
            query.setMaxResults(size);
        }
        List<Map<String, Object>> list = query.list();
        return list;
    }
    /**
     * 创建SQL查询,根据配置,获取数据库方言
     * sql为原生sql语句,查询,Oracle不区分大小写,返回值需要驼峰命名,字段别名需要加 双引号
     * 目前只支持通用的聚合函数,特有的聚合函数不支持
     * 无分页
     * @param sql
     * @return
     */
    public List<Map<String, Object>> createSQLQuery(String sql) {
        Session session = (Session) entityManager.getDelegate();
        SQLQuery query = session.createSQLQuery(sql);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String, Object>> list = query.list();
        return list;
    }
    /**
     * 创建SQL查询,根据配置,获取数据库方言
     * sql为原生sql语句,查询,Oracle不区分大小写,返回值需要驼峰命名,字段别名需要加 双引号
     * 目前只支持通用的聚合函数,特有的聚合函数不支持
     * @param sql
     * @param params 动态占位符入参,例如 like:name
     * @param page 第几页,1开始
     * @param size 每页记录数
     * @return
     */
    public List<Map<String, Object>> createSQLQuery(String sql, Map<String, Object> params, Integer page, Integer size) {
        Session session = (Session) entityManager.getDelegate();
        SQLQuery query = session.createSQLQuery(sql);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        //设置查询参数
        setSqlQueryByMap(query, params);
        if(page!=null&&size!=null){
            query.setFirstResult((page - 1) * size);
            query.setMaxResults(size);
        }
        List<Map<String, Object>> list = query.list();
        return list;
    }
    /**
     * 创建SQL查询,根据配置,获取数据库方言
     * sql为原生sql语句,查询,Oracle不区分大小写,返回值需要驼峰命名,字段别名需要加 双引号
     * 目前只支持通用的聚合函数,特有的聚合函数不支持
     * @param sql
     * @param params 动态占位符入参,例如 like:name
     * @return
     */
    public List<Map<String, Object>> createSQLQuery(String sql, Map<String, Object> params) {
        Session session = (Session) entityManager.getDelegate();
        SQLQuery query = session.createSQLQuery(sql);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        //设置查询参数
        setSqlQueryByMap(query, params);
        List<Map<String, Object>> list = query.list();
        return list;
    }
    /**
     * 设置查询参数
     * @param sqlQuery
     * @param params
     * @return
     */
    private SQLQuery setSqlQueryByMap(SQLQuery sqlQuery, Map<String, Object> params) {
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                Object obj = params.get(key);
                if (obj instanceof Collection<?>) {
                    sqlQuery.setParameterList(key, (Collection<?>) obj);
                } else if (obj instanceof Object[]) {
                    sqlQuery.setParameterList(key, (Object[]) obj);
                } else {
                    sqlQuery.setParameter(key, obj);
                }
            }
        }
        return sqlQuery;
    }
    /**
     * 聚合函数类型装换
     * @param obj
     * @return
     */
    public Long objTransformLong(Object obj){
        //Oracle count(1) 聚合函数类型装换
        if(obj instanceof BigDecimal){
            return ((BigDecimal) obj).longValue();
            //Mysql count(1) 聚合函数类型装换
        }else if(obj instanceof BigInteger){
            return ((BigInteger)obj).longValue();
        }else{
            return 0L;
        }
    }
    /**
     * map - T
     * @param map
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T map2Object(Map<String, Object> map, Class<T> clazz) {
        if (map == null) {
            return null;
        }
        T obj = null;
        try {
            obj = clazz.newInstance();
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                field.setAccessible(true);
                String filedTypeName = field.getType().getName();
                if (filedTypeName.equalsIgnoreCase("java.util.date")) {
                    String datetimestamp = String.valueOf(map.get(field.getName()));
                    if (datetimestamp.equalsIgnoreCase("null")) {
                        field.set(obj, null);
                    } else {
                        field.set(obj, new Date(Long.parseLong(datetimestamp)));
                    }
                } else {
                    field.set(obj, map.get(field.getName()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    /**
     *  List<Map> -- List<T>
     * @param list
     * @param clazz
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> castMapToBean(List<Map<String, Object>> list, Class<T> clazz) throws Exception {
        if(list == null || list.size()==0) {
            return null;
        }
        List<T> tList = new ArrayList<T>();
        // 获取类中声明的所有字段
        Field[] fields = clazz.getDeclaredFields();
        T t;
        for(Map<String, Object> map : list) {
            // 每次都先初始化一遍,然后再设置值
            t = clazz.newInstance();
            for(Field field : fields) {
                // 把序列化的字段去除掉
                if(!"serialVersionUID".equals(field.getName())){
                    // 由于Field都是私有属性,所有需要允许修改
                    field.setAccessible(true);
                    Object value = map.get(field.getName());
                    if (value!=null){
                        if("java.math.BigDecimal".equals(value.getClass().getName())){
                            value=Double.parseDouble(value.toString());
                        }
                        if(value.getClass().getName().equals("java.lang.Integer") && field.getName().equals("slaveKey1")){
                            value = value.toString();
                        }
                        if(field.getName().equals("slaveKey2")){
                            value = value.toString();
                        }
                    }
                    // 设置值, 类型要和vo对应好,不然会报类型转换错误
                    field.set(t, value);
                }
            }
            tList.add(t);
        }
        return tList;
    }
}

+ 116 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/BaseDimension.java

@ -0,0 +1,116 @@
package com.yihu.jw.statistics.vo;
import com.yihu.jw.entity.iot.gateway.IdEntity;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;
/**
 * Created by chenweida on 2017/7/10.
 */
@Entity
@Table(name = "dimension")
public class BaseDimension extends IdEntity {
    private String code;
    private String type;
    private String name;
    private Date createTime;
    private String createUser;
    private String createUserName;
    private Date updateTime;
    private String updateUser;
    private String updateUserName;
    private Integer status;////-1 删除 0 禁用 可用
    private String remark;
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public String getCreateUser() {
        return createUser;
    }
    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }
    public String getCreateUserName() {
        return createUserName;
    }
    public void setCreateUserName(String createUserName) {
        this.createUserName = createUserName;
    }
    public Date getUpdateTime() {
        return updateTime;
    }
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
    public String getUpdateUser() {
        return updateUser;
    }
    public void setUpdateUser(String updateUser) {
        this.updateUser = updateUser;
    }
    public String getUpdateUserName() {
        return updateUserName;
    }
    public void setUpdateUserName(String updateUserName) {
        this.updateUserName = updateUserName;
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
}

+ 70 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/BaseDimensionQuota.java

@ -0,0 +1,70 @@
package com.yihu.jw.statistics.vo;
import com.yihu.jw.entity.iot.gateway.IdEntity;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
 * Created by chenweida on 2017/7/10.
 */
@Entity
@Table(name = "dimension_quota")
public class BaseDimensionQuota extends IdEntity {
    private String quotaCode;
    private String dimensionCode;
    private String dictSql;
    private Integer sort;
    private String convertClazz;
    private String key;
    public String getQuotaCode() {
        return quotaCode;
    }
    public void setQuotaCode(String quotaCode) {
        this.quotaCode = quotaCode;
    }
    public String getDimensionCode() {
        return dimensionCode;
    }
    public void setDimensionCode(String dimensionCode) {
        this.dimensionCode = dimensionCode;
    }
    public String getDictSql() {
        return dictSql;
    }
    public void setDictSql(String dictSql) {
        this.dictSql = dictSql;
    }
    public Integer getSort() {
        return sort;
    }
    public void setSort(Integer sort) {
        this.sort = sort;
    }
    public String getConvertClazz() {
        return convertClazz;
    }
    public void setConvertClazz(String convertClazz) {
        this.convertClazz = convertClazz;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
}

+ 94 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/BaseDoctorDO.java

@ -0,0 +1,94 @@
package com.yihu.jw.statistics.vo;
import com.yihu.jw.entity.UuidIdentityEntity;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* 医生基本信息实体
*
* @author Administrator on  2018年09月05日
*
*/
@Entity
@Table(name = "base_doctor")
public class BaseDoctorDO extends UuidIdentityEntity {
    private String code;
	private String name;
    private String deptCode;
    private String deptName;
    private String orgCode;
    private String orgName;
    private String townCode;
    private String townName;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDeptCode() {
        return deptCode;
    }
    public void setDeptCode(String deptCode) {
        this.deptCode = deptCode;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    public String getOrgCode() {
        return orgCode;
    }
    public void setOrgCode(String orgCode) {
        this.orgCode = orgCode;
    }
    public String getOrgName() {
        return orgName;
    }
    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }
    public String getTownCode() {
        return townCode;
    }
    public void setTownCode(String townCode) {
        this.townCode = townCode;
    }
    public String getTownName() {
        return townName;
    }
    public void setTownName(String townName) {
        this.townName = townName;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
}

+ 231 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/DataModel.java

@ -0,0 +1,231 @@
package com.yihu.jw.statistics.vo;
import org.springframework.util.StringUtils;
import java.util.Date;
/**
 * 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 dept;//科室code
    private String doctor;//医生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 Date date1;
    private Date date2;
    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 getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    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.getDoctor())) {
            return "doctor";
        }
        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;
    }
    public Date getDate1() {
        return date1;
    }
    public void setDate1(Date date1) {
        this.date1 = date1;
    }
    public Date getDate2() {
        return date2;
    }
    public void setDate2(Date date2) {
        this.date2 = date2;
    }
    public String getDoctor() {
        return doctor;
    }
    public void setDoctor(String doctor) {
        this.doctor = doctor;
    }
}

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

@ -0,0 +1,25 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/vo/ErrModel.java

@ -0,0 +1,30 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/vo/FilterModel.java

@ -0,0 +1,34 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/vo/JobLogModel.java

@ -0,0 +1,19 @@
package com.yihu.jw.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;
    }
}

+ 343 - 0
svr/svr-statistics-es/src/main/java/com/yihu/jw/statistics/vo/SaveModel.java

@ -0,0 +1,343 @@
package com.yihu.jw.statistics.vo;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.searchbox.annotations.JestId;
import org.springframework.data.annotation.CreatedDate;
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 doctorKey = "doctor";
    private final static String deptKey = "dept";
    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 doctorLevel = "6";
    public final static String deptLevel = "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 dept;//科室的code
    private String deptName;//科室名称
    private String doctor;//医生code
    private String doctorName;//医生名称
    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 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 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科室,6医生
     *
     * @param level
     * @return
     */
    public static String getAreaLevelKey(String level) {
        switch (level) {
            case doctorLevel: {
                return doctorKey;
            }
            case deptLevel: {
                return deptKey;
            }
            case OrgLevel: {
                return OrgKey;
            }
            case townLevel: {
                return townKey;
            }
            case cityLevel: {
                return cityKey;
            }
            default: {
                return doctorKey;
            }
        }
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    public String getDoctor() {
        return doctor;
    }
    public void setDoctor(String doctor) {
        this.doctor = doctor;
    }
    public String getDoctorName() {
        return doctorName;
    }
    public void setDoctorName(String doctorName) {
        this.doctorName = doctorName;
    }
}

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

@ -0,0 +1,174 @@
package com.yihu.jw.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-es/src/main/java/com/yihu/jw/statistics/vo/WlyyQuotaVO.java

@ -0,0 +1,145 @@
package com.yihu.jw.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;
    }
}

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

@ -0,0 +1,75 @@
spring:
  profiles: dev
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    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
#      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: 192.168.131.172 # 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:
  pwflag: 1 # 1需要密码,0不需要密码
  index: hlw_quota_test
  type: hlw_quota_test
  host: http://172.26.0.55:9200
  tHost: 172.26.0.55:9300
  clusterName: jkzl
  securityUser: elastic:changeme
wlyy:
  im:
    databaseName: im_new
quartz:
  name: schedulerFactoryBean_dev
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

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

@ -0,0 +1,69 @@
spring:
  profiles: devtest
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    primaryReadWrite:
      url: jdbc:mysql://172.26.0.104/base?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: ssgg
      password: ssgg@jkzl2019
    primaryRead:
      url: jdbc:mysql://172.26.0.104/base?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: ssgg
      password: ssgg@jkzl2019
    im: #im库
      url: jdbc:mysql://172.26.0.104/base?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: ssgg
      password: ssgg@jkzl2019
#    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.26.0.253 # 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:
  pwflag: 2 # 1需要密码,0不需要密码
  index: hlw_quota_test
  type: hlw_quota_test
  host:  http://172.26.0.112:9200
  tHost: 172.26.0.112:9300
  clusterName: jkzl
  securityUser: elastic:changeme
wlyy:
  im:
    databaseName: im_new
quartz:
  name: schedulerFactoryBean_cwd
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

+ 75 - 0
svr/svr-statistics-es/src/main/resources/application-jwOracleProd.yml

@ -0,0 +1,75 @@
spring:
  profiles: jwOracleProd
  datasource:
    driverClassName: oracle.jdbc.driver.OracleDriver
    primaryReadWrite:
      url: jdbc:oracle:thin:@10.10.10.16:1521/rac
      username: HLWYY
      password: Xmykzxhlwyy963
    primaryRead:
      url: jdbc:oracle:thin:@10.10.10.16:1521/rac
      username: HLWYY
      password: Xmykzxhlwyy963
    im: #im库
      url: jdbc:oracle:thin:@10.10.10.16:1521/rac
      username: HLWYY
      password: Xmykzxhlwyy963
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.Oracle10gDialect
        show_sql: true
    database: oracle
#    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: 192.168.33.198 # Redis server host.
    port: 6380 # 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:
  pwflag: 1 # 1需要密码,0不需要密码
  index: hlw_quota_prod
  type: hlw_quota_prod
  host:  http://192.168.33.198:9200
  tHost: 192.168.33.198:9300
  clusterName: jkzl
  securityUser: lion:jkzlehr
  user: lion
  password: jkzlehr
wlyy:
  im:
    databaseName: im_new
quartz:
  name: schedulerFactoryBean_cwd
hibernate:
  dialect: org.hibernate.dialect.Oracle10gDialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

+ 52 - 0
svr/svr-statistics-es/src/main/resources/application-jwprod.yml

@ -0,0 +1,52 @@
spring:
  profiles: jwprod
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    primaryReadWrite:
      url: jdbc:mysql://59.61.92.90:9409/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: wlyy
      password: qY#j2n5O
    primaryRead:
      url: jdbc:mysql://59.61.92.90:9409/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: wlyy
      password: qY#j2n5O
    im: #im库:
      url: jdbc:mysql://59.61.92.90:9409/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: wlyy
      password: qY#j2n5O
  redis:
    host: 59.61.92.90 # Redis server host.
    port: 9206 # 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:
  pwflag: 0 # 1需要密码,0不需要密码
  index: hlw_quota_prod
  type: hlw_quota_prod
  host:  http://59.61.92.90:9208
  tHost: 59.61.92.90:9209
  clusterName: jkzl
  securityUser: lion:jkzlehr
  user: lion
  password: jkzlehr
wlyy:
  im:
    databaseName: im
quartz:
  name: schedulerFactoryBean_es_prod
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

+ 67 - 0
svr/svr-statistics-es/src/main/resources/application-orctest.yml

@ -0,0 +1,67 @@
spring:
  profiles: orctest
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@172.26.0.141:1521:helowin
    username: normal
    password: normal
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.Oracle10gDialect
        show_sql: true
    database: oracle
#    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.26.0.253 # 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:
  pwflag: 1 # 1需要密码,0不需要密码
  index: hlw_quota_test
  type: hlw_quota_test
  host: http://172.26.0.55:9200
  tHost: 172.26.0.55:9300
  clusterName: jkzl
  securityUser: lion:jkzlehr
  user: lion
  password: jkzlehr
wlyy:
  im:
    databaseName: im_new
quartz:
  name: schedulerFactoryBean_cwd
hibernate:
  dialect: org.hibernate.dialect.Oracle10gDialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

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

@ -0,0 +1,51 @@
spring:
  profiles: jwprod
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    primaryReadWrite:
      url: jdbc:mysql://192.0.33.27:3306/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: im
      password: 2oEq3Kf7
    primaryRead:
      url: jdbc:mysql://192.0.33.27:3306/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: im
      password: 2oEq3Kf7
    im: #im库:
      url: jdbc:mysql://192.0.33.27:3306/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: im
      password: 2oEq3Kf7
  redis:
    host: 192.0.33.26 # Redis server host.
    port: 6390 # Redis server port.
    password: Kb6wKDQP1W4
  followupMessage:
    cron : 0 5 0 * * ?
    jobId : FOLLOWUP_PLAN_JOB
systemConfig:
  msg_push_server: http://127.0.0.1:3000
es:
  pwflag: 1 # 1需要密码,0不需要密码
  index: hlw_quota_prod
  type: hlw_quota_prod
  host:  http://192.0.33.26:9200
  tHost: 192.0.33.26:9300
  clusterName: jkzl
  securityUser: lion:jkzlehr
wlyy:
  im:
    databaseName: im
quartz:
  name: schedulerFactoryBean_es_prod
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

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

@ -0,0 +1,59 @@
spring:
  profiles: test
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    primaryReadWrite:
      url: jdbc:mysql://172.26.0.104/base?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: ssgg
      password: ssgg@jkzl2019
    primaryRead:
      url: jdbc:mysql://172.26.0.104/base?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: ssgg
      password: ssgg@jkzl2019
    im: #im库
      url: jdbc:mysql://172.26.0.104/base?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: ssgg
      password: ssgg@jkzl2019
  redis:
    host: 172.26.0.253 # 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:
  pwflag: 1 # 1需要密码,0不需要密码
  index: hlw_quota_test
  type: hlw_quota_test
  host: http://172.26.0.55:9000
  tHost: 172.26.0.55:9300
  clusterName: jkzl
  securityUser: lion:jkzlehr
  user: lion
  password: jkzlehr
wlyy:
  im:
    databaseName: im_new
quartz:
  name: schedulerFactoryBean_cwd
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

+ 52 - 0
svr/svr-statistics-es/src/main/resources/application-tnJwprod.yml

@ -0,0 +1,52 @@
spring:
  profiles: tnJwprod
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    primaryReadWrite:
      url: jdbc:mysql://10.9.1.247:3310/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: wlyy
      password: qY#j2n5O
    primaryRead:
      url: jdbc:mysql://10.9.1.247:3310/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: wlyy
      password: qY#j2n5O
    im: #im库:
      url: jdbc:mysql://10.9.1.247:3310/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: wlyy
      password: qY#j2n5O
  redis:
    host: 10.9.1.247 # Redis server host.
    port: 6380 # Redis server port.
    password: Kb6wKDQP1W4
  followupMessage:
    cron : 0 5 0 * * ?
    jobId : FOLLOWUP_PLAN_JOB
systemConfig:
  msg_push_server: http://127.0.0.1:3000
es:
  pwflag: 0 # 1需要密码,0不需要密码
  index: hlw_quota_prod
  type: hlw_quota_prod
  host:  http://10.9.1.247:9200
  tHost: 10.9.1.247:9300
  clusterName: jkzl
  securityUser: lion:jkzlehr
  user: lion
  password: jkzlehr
wlyy:
  im:
    databaseName: im
quartz:
  name: schedulerFactoryBean_es_prod
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

+ 53 - 0
svr/svr-statistics-es/src/main/resources/application-xzprod.yml

@ -0,0 +1,53 @@
spring:
  profiles: xzprod
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    primaryReadWrite:
      url: jdbc:mysql://172.16.100.240:7306/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: jkzl
      password: jkzl@2020
    primaryRead:
      url: jdbc:mysql://172.16.100.240:7306/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: jkzl
      password: jkzl@2020
    im: #im库:
      url: jdbc:mysql://172.16.100.240:7306/base?useUnicode:true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
      username: jkzl
      password: jkzl@2020
  redis:
    host: 172.16.100.240 # Redis server host.
    port: 6380 # Redis server port.
    password: q4YaQemf
  followupMessage:
    cron : 0 5 0 * * ?
    jobId : FOLLOWUP_PLAN_JOB
systemConfig:
  msg_push_server: http://127.0.0.1:3000
es:
  pwflag: 1 # 1需要密码,0不需要密码
  index: hlw_quota_prod
  type: hlw_quota_prod
  host:  http://172.16.100.240:9200
  tHost: 172.16.100.240:9300
  clusterName: jkzl
  securityUser: lion:jkzlehr
  user: lion
  password: jkzlehr
wlyy:
  im:
    databaseName: im
quartz:
  name: schedulerFactoryBean_es_prod
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  ejb:
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

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

@ -0,0 +1,65 @@
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:
    initialSize: 50
    maxActive: 50
    minIdle: 50 #最大空闲连接
    maxWait: 50
    validationQuery: SELECT 1 FROM DUAL #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
JobService:
  sleepTime: 40000

+ 4 - 0
svr/svr-statistics-es/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-es/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