Browse Source

用户分析sql修改

dev_shibei_match
yinzuomei 4 years ago
parent
commit
e508b94d3a
  1. 7
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DateUtils.java
  2. 10
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/impl/StatsUserServiceImpl.java
  3. 11
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/user/impl/UserServiceImpl.java
  4. 54
      epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/user/UserDao.xml

7
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DateUtils.java

@ -424,6 +424,13 @@ public class DateUtils {
return DateUtils.format(date,DateUtils.DATE_PATTERN_YYYYMMDD); return DateUtils.format(date,DateUtils.DATE_PATTERN_YYYYMMDD);
} }
public static String getBeforeNDay(int beforDay,String format){
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, - beforDay);
Date date = c.getTime();
return DateUtils.format(date,format);
}
/** /**
* @return java.lang.String * @return java.lang.String
* @param beforMonth * @param beforMonth

10
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/impl/StatsUserServiceImpl.java

@ -1,6 +1,7 @@
package com.epmet.service.impl; package com.epmet.service.impl;
import com.alibaba.fastjson.JSON;
import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.utils.DateUtils; import com.epmet.commons.tools.utils.DateUtils;
import com.epmet.constant.ProjectConstant; import com.epmet.constant.ProjectConstant;
@ -58,10 +59,16 @@ public class StatsUserServiceImpl implements StatsUserService {
**/ **/
@Override @Override
public void partition(StatsFormDTO formDTO) { public void partition(StatsFormDTO formDTO) {
if(StringUtils.isBlank(formDTO.getDate())){
//如果定时任务没有指定参数,默认数据更新至t-1,包含t-1这一天内的数据
formDTO.setDate(DateUtils.getBeforeNDay(1,DateUtils.DATE_PATTERN));
}
int pageNo = NumConstant.ONE; int pageNo = NumConstant.ONE;
int pageSize = NumConstant.ONE_HUNDRED; int pageSize = NumConstant.ONE_HUNDRED;
List<String> customerIdList = null; List<String> customerIdList = null;
Date date = null; Date date = null;
if (StringUtils.isNotBlank(formDTO.getDate())) { if (StringUtils.isNotBlank(formDTO.getDate())) {
//如果指定了参数,转化为yyyy-MM-dd格式 //如果指定了参数,转化为yyyy-MM-dd格式
date = DateUtils.stringToDate(formDTO.getDate(), DateUtils.DATE_PATTERN); date = DateUtils.stringToDate(formDTO.getDate(), DateUtils.DATE_PATTERN);
@ -92,6 +99,7 @@ public class StatsUserServiceImpl implements StatsUserService {
* @date 2020.06.28 14:40 * @date 2020.06.28 14:40
**/ **/
void generate(String customerId,Date date){ void generate(String customerId,Date date){
log.info("customerId:"+customerId+";date:"+DateUtils.format(date,DateUtils.DATE_TIME_PATTERN));
//1.初始化时间参数 //1.初始化时间参数
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); calendar.setTime(new Date());
@ -103,7 +111,7 @@ public class StatsUserServiceImpl implements StatsUserService {
//2.初始化时间维度{"dateId":"20210325","monthId":"202103","quarterId":"2021Q1","weekId":"2021W13","yearId":"2021"} //2.初始化时间维度{"dateId":"20210325","monthId":"202103","quarterId":"2021Q1","weekId":"2021W13","yearId":"2021"}
DimIdGenerator.DimIdBean timeDimension = DimIdGenerator.getDimIdBean(null == date ? calendar.getTime() : date); DimIdGenerator.DimIdBean timeDimension = DimIdGenerator.getDimIdBean(null == date ? calendar.getTime() : date);
log.info("timeDimension:"+ JSON.toJSONString(timeDimension));
//3.初始化机关维度 //3.初始化机关维度
List<AgencySubTreeDto> agencies = dimAgencyService.getAllAgency(customerId); List<AgencySubTreeDto> agencies = dimAgencyService.getAllAgency(customerId);
//4.计算机关统计数据、生成唯一性统计数据 //4.计算机关统计数据、生成唯一性统计数据

11
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/user/impl/UserServiceImpl.java

@ -108,10 +108,10 @@ public class UserServiceImpl implements UserService {
//自上向下检索 //自上向下检索
Map<String, List<AgencySubTreeDto>> agencyMap = agencies.stream().collect(Collectors.groupingBy(AgencySubTreeDto::getAgencyId)); Map<String, List<AgencySubTreeDto>> agencyMap = agencies.stream().collect(Collectors.groupingBy(AgencySubTreeDto::getAgencyId));
if(null != agencyMap && agencyMap.size() > NumConstant.ZERO){ if(null != agencyMap && agencyMap.size() > NumConstant.ZERO){
agencyMap.forEach((k,v) -> { agencyMap.forEach((agencyId,v) -> {
if(null != v && v.size() > NumConstant.ZERO){ if(null != v && v.size() > NumConstant.ZERO){
queryUserData(ModuleConstant.DIM_BELONGING_GRID, queryUserData(ModuleConstant.DIM_BELONGING_GRID,
k, agencyId,
// v.get(NumConstant.ZERO).getPid(), // v.get(NumConstant.ZERO).getPid(),
v.get(NumConstant.ZERO).getCustomerId(), v.get(NumConstant.ZERO).getCustomerId(),
new HashSet<>(v.get(NumConstant.ZERO).getGridIds()), new HashSet<>(v.get(NumConstant.ZERO).getGridIds()),
@ -258,6 +258,9 @@ public class UserServiceImpl implements UserService {
//指定日期 OR T-1 //指定日期 OR T-1
calendar.setTime(targetDateCheck); calendar.setTime(targetDateCheck);
//calendar.get(Calendar.DATE) 当前日期数eg:2021-03-29 :29
//calendar.getActualMinimum(Calendar.DAY_OF_MONTH) :1
//如果目标日期不是是当月的第一天 //如果目标日期不是是当月的第一天
if(calendar.get(Calendar.DATE) != calendar.getActualMinimum(Calendar.DAY_OF_MONTH)){ if(calendar.get(Calendar.DATE) != calendar.getActualMinimum(Calendar.DAY_OF_MONTH)){
//求出这个月的第一天 //求出这个月的第一天
@ -269,7 +272,6 @@ public class UserServiceImpl implements UserService {
isMonthBeginning = false; isMonthBeginning = false;
} }
if(StringUtils.equals(ModuleConstant.DIM_SUB_AGENCY,relation)){ if(StringUtils.equals(ModuleConstant.DIM_SUB_AGENCY,relation)){
@ -401,8 +403,6 @@ public class UserServiceImpl implements UserService {
partiAgencyM.setWarmHeartedProportion(partiAgencyD.getWarmHeartedProportion()); partiAgencyM.setWarmHeartedProportion(partiAgencyD.getWarmHeartedProportion());
partiAgencyM.setCreatedBy(ModuleConstant.CREATED_BY_STATISTICAL_ROBOT); partiAgencyM.setCreatedBy(ModuleConstant.CREATED_BY_STATISTICAL_ROBOT);
//如果是月初第一天,不再做日期区间查询 //如果是月初第一天,不再做日期区间查询
if(isMonthBeginning) { if(isMonthBeginning) {
if (null == dataPacket.getRegAgencyMonthlyList()) { if (null == dataPacket.getRegAgencyMonthlyList()) {
@ -421,6 +421,7 @@ public class UserServiceImpl implements UserService {
} }
}else{ }else{
//如果不是月初第一天 //如果不是月初第一天
//targetDateCheck:定时任务参数中的date;calendar.getTime():date所属月第一天;
//本月注册用户增长数 //本月注册用户增长数
Integer regIncrMonthly = userDao.selectResiIncrWithinTimeRange(ModuleConstant.REG_OR_PARTI_FLAG_REG,gridIds,calendar.getTime(),targetDateCheck); Integer regIncrMonthly = userDao.selectResiIncrWithinTimeRange(ModuleConstant.REG_OR_PARTI_FLAG_REG,gridIds,calendar.getTime(),targetDateCheck);

54
epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/user/UserDao.xml

@ -40,11 +40,13 @@
</choose> </choose>
<choose> <choose>
<!-- 不为空,默认查询创建时间在 昨天的增量 -->
<when test='null != targetDate'> <when test='null != targetDate'>
AND CREATED_TIME <![CDATA[>=]]> #{targetDate} AND CREATED_TIME <![CDATA[<]]> DATE_SUB( #{targetDate}, INTERVAL - 1 DAY) and DATE_FORMAT(CREATED_TIME,"%Y-%m-%d") = DATE_FORMAT(#{targetDate},"%Y-%m-%d")
</when> </when>
<!-- 为空,默认查询昨天的增量 -->
<otherwise> <otherwise>
AND CREATED_TIME <![CDATA[<]]> CURDATE( ) AND CREATED_TIME <![CDATA[>=]]> DATE_SUB( CURDATE( ), INTERVAL 1 DAY ) and DATE_FORMAT(CREATED_TIME,"%Y-%m-%d") = DATE_FORMAT( DATE_SUB( CURDATE( ), INTERVAL 1 DAY ),"%Y-%m-%d")
</otherwise> </otherwise>
</choose> </choose>
) AS incr ) AS incr
@ -76,11 +78,13 @@
</otherwise> </otherwise>
</choose> </choose>
<choose> <choose>
<!-- 不为空,包含传进来的这一天 -->
<when test="null != targetDate"> <when test="null != targetDate">
AND CREATED_TIME <![CDATA[<]]> DATE_SUB( #{targetDate}, INTERVAL - 1 DAY ) and DATE_FORMAT(CREATED_TIME,"%Y-%m-%d") <![CDATA[<=]]> DATE_FORMAT(#{targetDate},"%Y-%m-%d")
</when> </when>
<!-- 为空,默认截止到昨天 -->
<otherwise> <otherwise>
AND CREATED_TIME <![CDATA[<]]> CURDATE( ) and DATE_FORMAT(CREATED_TIME,"%Y-%m-%d") <![CDATA[<=]]> DATE_FORMAT(DATE_SUB( CURDATE( ), INTERVAL 1 DAY ),"%Y-%m-%d")
</otherwise> </otherwise>
</choose> </choose>
@ -121,10 +125,10 @@
<choose> <choose>
<when test='null != targetDate'> <when test='null != targetDate'>
AND CREATED_TIME <![CDATA[>=]]> #{targetDate} AND CREATED_TIME <![CDATA[<]]> DATE_SUB( #{targetDate}, INTERVAL - 1 DAY) and DATE_FORMAT(CREATED_TIME,"%Y-%m-%d") = DATE_FORMAT(#{targetDate},"%Y-%m-%d")
</when> </when>
<otherwise> <otherwise>
AND CREATED_TIME <![CDATA[<]]> CURDATE( ) AND CREATED_TIME <![CDATA[>=]]> DATE_SUB( CURDATE( ), INTERVAL 1 DAY ) and DATE_FORMAT(CREATED_TIME,"%Y-%m-%d") = DATE_FORMAT(DATE_SUB( CURDATE( ), INTERVAL 1 DAY ),"%Y-%m-%d")
</otherwise> </otherwise>
</choose> </choose>
</select> </select>
@ -164,10 +168,10 @@
<choose> <choose>
<when test='targetDate != null'> <when test='targetDate != null'>
AND CREATED_TIME <![CDATA[<]]> DATE_SUB( #{targetDate}, INTERVAL - 1 DAY ) and DATE_FORMAT(CREATED_TIME,"%Y-%m-%d") <![CDATA[<=]]> DATE_FORMAT(#{targetDate},"%Y-%m-%d")
</when> </when>
<otherwise> <otherwise>
AND CREATED_TIME <![CDATA[<]]> CURDATE( ) and DATE_FORMAT(CREATED_TIME,"%Y-%m-%d") <![CDATA[<=]]> DATE_FORMAT(DATE_SUB( CURDATE( ), INTERVAL 1 DAY ),"%Y-%m-%d")
</otherwise> </otherwise>
</choose> </choose>
</select> </select>
@ -190,10 +194,10 @@
</foreach> </foreach>
<choose> <choose>
<when test='null != targetDate'> <when test='null != targetDate'>
AND urole.CREATED_TIME <![CDATA[>=]]> #{targetDate} AND urole.CREATED_TIME <![CDATA[<]]> DATE_SUB( #{targetDate}, INTERVAL - 1 DAY) and DATE_FORMAT(urole.CREATED_TIME,"%Y-%m-%d") = DATE_FORMAT(#{targetDate},"%Y-%m-%d")
</when> </when>
<otherwise> <otherwise>
AND urole.CREATED_TIME <![CDATA[<]]> CURDATE( ) AND urole.CREATED_TIME <![CDATA[>=]]> DATE_SUB( CURDATE( ), INTERVAL 1 DAY ) and DATE_FORMAT(urole.CREATED_TIME,"%Y-%m-%d") = DATE_FORMAT(DATE_SUB( CURDATE( ), INTERVAL 1 DAY ),"%Y-%m-%d")
</otherwise> </otherwise>
</choose> </choose>
@ -221,10 +225,10 @@
<choose> <choose>
<when test='null != targetDate'> <when test='null != targetDate'>
AND urole.CREATED_TIME <![CDATA[<]]> DATE_SUB( #{targetDate}, INTERVAL - 1 DAY ) and DATE_FORMAT( urole.CREATED_TIME,"%Y-%m-%d") <![CDATA[<=]]> DATE_FORMAT(#{targetDate},"%Y-%m-%d")
</when> </when>
<otherwise> <otherwise>
AND urole.CREATED_TIME <![CDATA[<]]> CURDATE( ) and DATE_FORMAT(urole.CREATED_TIME,"%Y-%m-%d") <![CDATA[<=]]> DATE_FORMAT(DATE_SUB( CURDATE( ), INTERVAL 1 DAY ),"%Y-%m-%d")
</otherwise> </otherwise>
</choose> </choose>
AND erole.ROLE_NAME = '党员' AND erole.ROLE_NAME = '党员'
@ -261,10 +265,10 @@
<choose> <choose>
<when test='null != targetDate'> <when test='null != targetDate'>
AND urole.CREATED_TIME <![CDATA[>=]]> #{targetDate} AND urole.CREATED_TIME <![CDATA[<]]> DATE_SUB( #{targetDate}, INTERVAL - 1 DAY) and DATE_FORMAT(urole.CREATED_TIME,"%Y-%m-%d")=DATE_FORMAT(#{targetDate},"%Y-%m-%d")
</when> </when>
<otherwise> <otherwise>
AND urole.CREATED_TIME <![CDATA[<]]> CURDATE( ) AND urole.CREATED_TIME <![CDATA[>=]]> DATE_SUB( CURDATE( ), INTERVAL 1 DAY ) and DATE_FORMAT(urole.CREATED_TIME,"%Y-%m-%d")=DATE_FORMAT(DATE_SUB( CURDATE( ), INTERVAL 1 DAY ),"%Y-%m-%d")
</otherwise> </otherwise>
</choose> </choose>
AND erole.ROLE_NAME = '热心居民' AND erole.ROLE_NAME = '热心居民'
@ -290,10 +294,10 @@
</foreach> </foreach>
<choose> <choose>
<when test='null != targetDate'> <when test='null != targetDate'>
AND urole.CREATED_TIME <![CDATA[<]]> DATE_SUB( #{targetDate}, INTERVAL - 1 DAY ) and DATE_FORMAT(urole.CREATED_TIME,"%Y-%m-%d") <![CDATA[<=]]> DATE_FORMAT(#{targetDate},"%Y-%m-%d")
</when> </when>
<otherwise> <otherwise>
AND urole.CREATED_TIME <![CDATA[<]]> CURDATE( ) and DATE_FORMAT(urole.CREATED_TIME,"%Y-%m-%d") <![CDATA[<=]]> DATE_FORMAT(DATE_SUB( CURDATE( ), INTERVAL 1 DAY ),"%Y-%m-%d")
</otherwise> </otherwise>
</choose> </choose>
AND erole.ROLE_NAME = '热心居民'; AND erole.ROLE_NAME = '热心居民';
@ -332,8 +336,8 @@
</foreach> </foreach>
</otherwise> </otherwise>
</choose> </choose>
AND CREATED_TIME <![CDATA[>=]]> DATE_FORMAT(#{startDate},"%Y-%m-%d")
AND CREATED_TIME <![CDATA[>=]]> #{startDate} AND CREATED_TIME <![CDATA[<]]> DATE_SUB( #{endDate}, INTERVAL - 1 DAY) AND CREATED_TIME <![CDATA[<=]]> DATE_FORMAT(#{endDate},"%Y-%m-%d")
</select> </select>
<!-- 查询指定时间范围内居民增量 传参:注册用户or参与用户 | 网格Id集合 | 时间区间--> <!-- 查询指定时间范围内居民增量 传参:注册用户or参与用户 | 网格Id集合 | 时间区间-->
@ -368,10 +372,8 @@
</foreach> </foreach>
</otherwise> </otherwise>
</choose> </choose>
AND CREATED_TIME <![CDATA[>=]]> DATE_FORMAT(#{startDate},"%Y-%m-%d")
AND CREATED_TIME <![CDATA[<=]]> DATE_FORMAT(#{endDate},"%Y-%m-%d")
AND CREATED_TIME <![CDATA[>=]]> #{startDate} AND CREATED_TIME <![CDATA[<]]> DATE_SUB( #{endDate}, INTERVAL - 1 DAY)
</select> </select>
<!-- 查询指定时间范围内党员的增量 传参:用户Id集合 | 时间区间 --> <!-- 查询指定时间范围内党员的增量 传参:用户Id集合 | 时间区间 -->
@ -394,9 +396,8 @@
</foreach> </foreach>
</otherwise> </otherwise>
</choose> </choose>
AND urole.CREATED_TIME <![CDATA[>=]]> DATE_FORMAT(#{startDate},"%Y-%m-%d")
AND urole.CREATED_TIME <![CDATA[<=]]> DATE_FORMAT(#{endDate},"%Y-%m-%d")
AND urole.CREATED_TIME <![CDATA[>=]]> #{startDate} AND urole.CREATED_TIME <![CDATA[<]]> DATE_SUB( #{endDate}, INTERVAL - 1 DAY)
AND erole.ROLE_NAME = '党员' AND erole.ROLE_NAME = '党员'
</select> </select>
@ -423,7 +424,8 @@
</foreach> </foreach>
</otherwise> </otherwise>
</choose> </choose>
AND urole.CREATED_TIME <![CDATA[>=]]> #{startDate} AND urole.CREATED_TIME <![CDATA[<]]> DATE_SUB( #{endDate}, INTERVAL - 1 DAY) AND urole.CREATED_TIME <![CDATA[>=]]> DATE_FORMAT(#{startDate},"%Y-%m-%d")
AND urole.CREATED_TIME <![CDATA[<=]]> DATE_FORMAT(#{endDate},"%Y-%m-%d")
AND erole.ROLE_NAME = '热心居民' AND erole.ROLE_NAME = '热心居民'
</select> </select>

Loading…
Cancel
Save