diff --git a/mallinkAdmin/src/main/java/com/simple/controller/WxCUserBasicInfoController.java b/mallinkAdmin/src/main/java/com/simple/controller/WxCUserBasicInfoController.java index ba527055a..b281d108f 100644 --- a/mallinkAdmin/src/main/java/com/simple/controller/WxCUserBasicInfoController.java +++ b/mallinkAdmin/src/main/java/com/simple/controller/WxCUserBasicInfoController.java @@ -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 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 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; - } - + } diff --git a/mallinkAdmin/src/main/java/com/simple/controller/WxUserStructureController.java b/mallinkAdmin/src/main/java/com/simple/controller/WxUserStructureController.java new file mode 100644 index 000000000..80f591e7c --- /dev/null +++ b/mallinkAdmin/src/main/java/com/simple/controller/WxUserStructureController.java @@ -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 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 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 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 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 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; + } + + +} diff --git a/mallinkService/src/main/java/com/simple/domain/vo/UserStructureVo.java b/mallinkService/src/main/java/com/simple/domain/vo/UserStructureVo.java index 84db123d6..21a641ff0 100644 --- a/mallinkService/src/main/java/com/simple/domain/vo/UserStructureVo.java +++ b/mallinkService/src/main/java/com/simple/domain/vo/UserStructureVo.java @@ -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() { diff --git a/mallinkService/src/main/java/com/simple/mapper/WxCUserMapper.java b/mallinkService/src/main/java/com/simple/mapper/WxCUserMapper.java index 222053d6e..bf502cd19 100644 --- a/mallinkService/src/main/java/com/simple/mapper/WxCUserMapper.java +++ b/mallinkService/src/main/java/com/simple/mapper/WxCUserMapper.java @@ -15,5 +15,5 @@ public interface WxCUserMapper extends CommonMapper { WxCUser findByToken(String token); - long findCountBySex(WxCuerBasicInfoDto dto); + long findCount(WxCuerBasicInfoDto dto); } diff --git a/mallinkService/src/main/java/com/simple/service/WxCUserService.java b/mallinkService/src/main/java/com/simple/service/WxCUserService.java index 1dcb1e883..691cc301a 100644 --- a/mallinkService/src/main/java/com/simple/service/WxCUserService.java +++ b/mallinkService/src/main/java/com/simple/service/WxCUserService.java @@ -55,9 +55,9 @@ public interface WxCUserService { void deleteById(Long id); /** - * 根据性别统计数量 + * 统计数量 * @param dto * @return */ - long findCountBySex(WxCuerBasicInfoDto dto); + long findCount(WxCuerBasicInfoDto dto); } diff --git a/mallinkService/src/main/java/com/simple/service/impl/WxCUserServiceImpl.java b/mallinkService/src/main/java/com/simple/service/impl/WxCUserServiceImpl.java index e1dbc5676..b2a7c686e 100644 --- a/mallinkService/src/main/java/com/simple/service/impl/WxCUserServiceImpl.java +++ b/mallinkService/src/main/java/com/simple/service/impl/WxCUserServiceImpl.java @@ -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); } + + + } diff --git a/mallinkService/src/main/resources/mapper/WxCUserMapper.xml b/mallinkService/src/main/resources/mapper/WxCUserMapper.xml index c247c6fe4..9b2aa3965 100644 --- a/mallinkService/src/main/resources/mapper/WxCUserMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxCUserMapper.xml @@ -184,8 +184,11 @@ where `token` = #{token} - + select count(id) from wx_c_user where 1=1 + + and gender =#{sex} + and create_date >= #{startTime}