Browse Source

[会员洞察][新增]会员数量统计

release_toaliyun_real
jinguo24@163.com 7 years ago
parent
commit
447d9fab69
7 changed files with 214 additions and 91 deletions
  1. +1
    -80
      mallinkAdmin/src/main/java/com/simple/controller/WxCUserBasicInfoController.java
  2. +197
    -0
      mallinkAdmin/src/main/java/com/simple/controller/WxUserStructureController.java
  3. +3
    -3
      mallinkService/src/main/java/com/simple/domain/vo/UserStructureVo.java
  4. +1
    -1
      mallinkService/src/main/java/com/simple/mapper/WxCUserMapper.java
  5. +2
    -2
      mallinkService/src/main/java/com/simple/service/WxCUserService.java
  6. +5
    -3
      mallinkService/src/main/java/com/simple/service/impl/WxCUserServiceImpl.java
  7. +5
    -2
      mallinkService/src/main/resources/mapper/WxCUserMapper.xml

+ 1
- 80
mallinkAdmin/src/main/java/com/simple/controller/WxCUserBasicInfoController.java View File

@@ -200,84 +200,5 @@ public class WxCUserBasicInfoController extends BaseController
return new ResultData(Result.SUCCESS,"查询成功",page);
}
@ApiOperation("查询会员性别结构")
@GetMapping("/findUserSexStructure")
public ResultData findUserSexStructure(
Date startTime,Date endTime
) {
WxCuerBasicInfoDto dto = new WxCuerBasicInfoDto();
dto.setStartTime(startTime);
dto.setEndTime(endTime);
//保密
dto.setSex(0);
long secrecy = getCount(dto);
dto.setSex(1);
long boy = getCount(dto);
dto.setSex(2);
long girl=getCount(dto);
Long all =secrecy+boy+girl;
List<UserStructureVo> vos = new ArrayList<>();
vos.add(getVo(boy, all, "男",1));
vos.add(getVo(girl, all, "女",2));
vos.add(getVo(secrecy, all, "保密",3));
return new ResultData(vos);
}
@ApiOperation("查询会员年龄结构")
@GetMapping("/findUserAgeStructure")
public ResultData findUserAgeStructure( Date startTime,Date endTime) {
WxCuerBasicInfoDto dto = new WxCuerBasicInfoDto();
dto.setStartTime(startTime);
dto.setEndTime(endTime);
long all =wxCUserBasicInfoService.findCountByAge(dto);
List<UserStructureVo> vos = new ArrayList<>();
Calendar c = Calendar.getInstance();
for(EnumAgeInfo a:EnumAgeInfo.values()) {
c.clear();
c.setTime(new Date());
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
long count = getCountByAge(a, c,dto);
vos.add(getVo(count, all, a.getDesc(),a.getSortNum()));
}
return new ResultData(vos);
}
private long getCountByAge(EnumAgeInfo a,Calendar c, WxCuerBasicInfoDto dto ) {
c.add(Calendar.YEAR, -a.getEnd());
Date startTime = c.getTime();
c.clear();
c.setTime(startTime);
c.add(Calendar.YEAR,a.getEnd()-a.getStart());
Date endTime = c.getTime();
dto.setBirthStartTime(startTime);
dto.setBirthEndTime(endTime);
return wxCUserBasicInfoService.findCountByAge(dto);
}
//通过性别获取数量
private long getCount(WxCuerBasicInfoDto dto) {
//TODO 考虑性能问题,暂不处理少量重复问题,后续可考虑大数据处理
return wxCUserBasicInfoService.findCountBySex(dto)+
wxCUserService.findCountBySex(dto);
}
private UserStructureVo getVo(long count,long all,String name,Integer num) {
UserStructureVo vo = new UserStructureVo();
vo.setSortNum(num);
vo.setName(name);
vo.setCount(count+"");
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(2);//控制保留小数点后几位,2:表示保留2位小数点
if(all>0) {
vo.setPercentage(nf.format(new Long(count).doubleValue()/new Long(all).doubleValue()));
}else {
vo.setPercentage("0.00%");
}
return vo;
}
}

+ 197
- 0
mallinkAdmin/src/main/java/com/simple/controller/WxUserStructureController.java View File

@@ -0,0 +1,197 @@
package com.simple.controller;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.simple.common.ResultData;
import com.simple.domain.dto.WxCuerBasicInfoDto;
import com.simple.domain.vo.UserStructureVo;
import com.simple.enums.EnumAgeInfo;
import com.simple.service.WxCUserBasicInfoService;
import com.simple.service.WxCUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(description="会员洞察")
@RequestMapping("userAnalysis")
public class WxUserStructureController {
@Autowired
private WxCUserBasicInfoService wxCUserBasicInfoService;
@Autowired
private WxCUserService wxCUserService;
@ApiOperation("查询会员性别结构")
@GetMapping("/findUserSexStructure")
public ResultData findUserSexStructure(
Date startTime,Date endTime
) {
WxCuerBasicInfoDto dto = new WxCuerBasicInfoDto();
dto.setStartTime(startTime);
if(endTime!=null) {
Calendar c = Calendar.getInstance();
c.setTime(endTime);
c.add(Calendar.DAY_OF_YEAR, 1);
endTime =c.getTime();
}
//保密
dto.setSex(0);
long secrecy = getCount(dto);
dto.setSex(1);
long boy = getCount(dto);
dto.setSex(2);
long girl=getCount(dto);
Long all =secrecy+boy+girl;
List<UserStructureVo> vos = new ArrayList<>();
vos.add(getVo(boy, all, "男",1));
vos.add(getVo(girl, all, "女",2));
vos.add(getVo(secrecy, all, "保密",3));
return new ResultData(vos);
}
@ApiOperation("查询会员年龄结构")
@GetMapping("/findUserAgeStructure")
public ResultData findUserAgeStructure( Date startTime,Date endTime) {
WxCuerBasicInfoDto dto = new WxCuerBasicInfoDto();
dto.setStartTime(startTime);
if(endTime!=null) {
Calendar c = Calendar.getInstance();
c.setTime(endTime);
c.add(Calendar.DAY_OF_YEAR, 1);
endTime =c.getTime();
}
dto.setEndTime(endTime);
long all =wxCUserBasicInfoService.findCountByAge(dto);
List<UserStructureVo> vos = new ArrayList<>();
Calendar c = Calendar.getInstance();
for(EnumAgeInfo a:EnumAgeInfo.values()) {
c.clear();
c.setTime(new Date());
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
long count = getCountByAge(a, c,dto);
vos.add(getVo(count, all, a.getDesc(),a.getSortNum()));
}
return new ResultData(vos);
}
@ApiOperation("查询会员数量")
@GetMapping("/findUserDataCount")
public ResultData findUserCount(Date startTime,Date endTime) {
WxCuerBasicInfoDto dto = new WxCuerBasicInfoDto();
dto.setStartTime(startTime);
if(endTime!=null) {
Calendar c = Calendar.getInstance();
c.setTime(endTime);
c.add(Calendar.DAY_OF_YEAR, 1);
endTime =c.getTime();
}
long allCount = wxCUserService.findCount(dto);//总量
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
Date today = c.getTime();
dto.setStartTime(today);
dto.setEndTime(null);
long todayCount= wxCUserService.findCount( dto);//今天新增
List<UserStructureVo> newCountVos = new ArrayList<>();//每日新增会员数
int j=1;
for(int i=29;i>=0;i--) {
c.clear();
c.setTime(today);
c.add(Calendar.DAY_OF_YEAR, -i);
dto.setStartTime(c.getTime());
c.add(Calendar.DAY_OF_YEAR, 1);
dto.setEndTime(c.getTime());
long count= wxCUserService.findCount(dto);
UserStructureVo vo = new UserStructureVo();
vo.setSortNum(j);
j++;
vo.setName(new SimpleDateFormat("MM-dd").format(dto.getStartTime()));
vo.setCount(count);
newCountVos.add(vo);
}
List<UserStructureVo> allCountVos = new ArrayList<>();//累计会员数
c.clear();
c.setTime(today);
c.add(Calendar.DAY_OF_YEAR, -30);
dto.setEndTime(c.getTime());
dto.setStartTime(null);
long firstDay = wxCUserService.findCount(dto);//统计的第一天总数
int i =0;
long sumCount=0;
for(UserStructureVo v:newCountVos) {
UserStructureVo vo = new UserStructureVo();
sumCount+=v.getCount();
vo.setCount(firstDay+sumCount);
vo.setName(v.getName());
vo.setSortNum(i+1);
allCountVos.add(vo);
i++;
}
Map<String,Object> map = new HashMap<>();
map.put("allCount", allCount);//累计会员总数
map.put("todayCount", todayCount);//今日新增会员数
map.put("allCountVos", allCountVos);//累计会员列表( 日期和数量list)
map.put("newCountVos", newCountVos);//新增会员列表(日期和数量list)
return new ResultData(map);
}
private long getCountByAge(EnumAgeInfo a,Calendar c, WxCuerBasicInfoDto dto ) {
c.add(Calendar.YEAR, -a.getEnd());
Date startTime = c.getTime();
c.clear();
c.setTime(startTime);
c.add(Calendar.YEAR,a.getEnd()-a.getStart());
Date endTime = c.getTime();
dto.setBirthStartTime(startTime);
dto.setBirthEndTime(endTime);
return wxCUserBasicInfoService.findCountByAge(dto);
}
//通过性别获取数量
private long getCount(WxCuerBasicInfoDto dto) {
// wxCUserBasicInfoService.findCountBySex(dto) basic表与cuser表示对应的,先有cuser 才有basic
//所有这里不需要再去查basic
return wxCUserService.findCount(dto);
}
private UserStructureVo getVo(long count,long all,String name,Integer num) {
UserStructureVo vo = new UserStructureVo();
vo.setSortNum(num);
vo.setName(name);
vo.setCount(count);
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(2);//控制保留小数点后几位,2:表示保留2位小数点
if(all>0) {
vo.setPercentage(nf.format(new Long(count).doubleValue()/new Long(all).doubleValue()));
}else {
vo.setPercentage("0.00%");
}
return vo;
}
}

+ 3
- 3
mallinkService/src/main/java/com/simple/domain/vo/UserStructureVo.java View File

@@ -15,7 +15,7 @@ public class UserStructureVo implements Serializable{
//名称
private String name;
//数量
private String count;
private long count;
//百分比
private String percentage;
//序号
@@ -32,10 +32,10 @@ public class UserStructureVo implements Serializable{
public void setName(String name) {
this.name = name;
}
public String getCount() {
public long getCount() {
return count;
}
public void setCount(String count) {
public void setCount(long count) {
this.count = count;
}
public String getPercentage() {


+ 1
- 1
mallinkService/src/main/java/com/simple/mapper/WxCUserMapper.java View File

@@ -15,5 +15,5 @@ public interface WxCUserMapper extends CommonMapper<WxCUser, Long> {

WxCUser findByToken(String token);
long findCountBySex(WxCuerBasicInfoDto dto);
long findCount(WxCuerBasicInfoDto dto);
}

+ 2
- 2
mallinkService/src/main/java/com/simple/service/WxCUserService.java View File

@@ -55,9 +55,9 @@ public interface WxCUserService {
void deleteById(Long id);

/**
* 根据性别统计数量
* 统计数量
* @param dto
* @return
*/
long findCountBySex(WxCuerBasicInfoDto dto);
long findCount(WxCuerBasicInfoDto dto);
}

+ 5
- 3
mallinkService/src/main/java/com/simple/service/impl/WxCUserServiceImpl.java View File

@@ -63,10 +63,12 @@ public class WxCUserServiceImpl implements WxCUserService {
}

@Override
public long findCountBySex(WxCuerBasicInfoDto dto) {
return wxCUserMapper.findCountBySex(dto);
public long findCount(WxCuerBasicInfoDto dto) {
return wxCUserMapper.findCount(dto);
}
}

+ 5
- 2
mallinkService/src/main/resources/mapper/WxCUserMapper.xml View File

@@ -184,8 +184,11 @@
where `token` = #{token}
</select>
<select id="findCountBySex" parameterType="com.simple.domain.dto.WxCuerBasicInfoDto" resultType="java.lang.Long">
select count(id) from wx_c_user where gender =#{sex}
<select id="findCount" parameterType="com.simple.domain.dto.WxCuerBasicInfoDto" resultType="java.lang.Long">
select count(id) from wx_c_user where 1=1
<if test=" null != sex ">
and gender =#{sex}
</if>
<if test=" null != startTime ">
and create_date &gt;= #{startTime}
</if>


Loading…
Cancel
Save