diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/datatower/WxUserStatisticsController.java b/mallinkAdmin/src/main/java/com/iformall/controller/datatower/WxUserStatisticsController.java new file mode 100644 index 000000000..04bcf4c7f --- /dev/null +++ b/mallinkAdmin/src/main/java/com/iformall/controller/datatower/WxUserStatisticsController.java @@ -0,0 +1,335 @@ +package com.iformall.controller.datatower; + +import com.iformall.annotation.SystemControllerLog; +import com.iformall.common.ResultData; +import com.iformall.controller.base.BaseController; +import com.iformall.domain.dto.WxCUserBasicInfoDto; +import com.iformall.domain.po.base.TenantEntity; +import com.iformall.domain.vo.UserCountVo; +import com.iformall.enums.*; +import com.iformall.service.*; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.math.BigDecimal; +import java.text.SimpleDateFormat; +import java.util.*; + +@RestController +@Api(description = "会员统计") +@RequestMapping("userStatistics") +public class WxUserStatisticsController extends BaseController { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private WxCUserBasicInfoService wxCUserBasicInfoService; + + @Autowired + private WxCUserService wxCUserService; + + @Autowired + private WxMallService wxMallService; + + @ApiOperation("查询微信用户数量") + @GetMapping("/findWxUserCount") + @SystemControllerLog(description = "查询微信用户数量") + public ResultData findWxUserCount() { + logger.debug("[" + getIpAddr() + "] userStatistics::findWxUserCount"); + + TenantEntity tenantEntity = getTenantInfo(); + List tenantEntitys = wxMallService.getTenantEntitys(tenantEntity); + //昨日同比 + Map map = new HashMap<>(); + WxCUserBasicInfoDto dto = new WxCUserBasicInfoDto(); + dto.updateTenantInfo(tenantEntity); + dto.setEndTime(new Date()); + //微信用户数据 + long wxTotalCount = wxCUserService.findCount(dto,tenantEntitys);//总量 + + map.put("wxTotalCount",wxTotalCount); + + 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 wxTodayCount = wxCUserService.findCount(dto, tenantEntitys);//今天新增 + + map.put("wxTodayCount",wxTodayCount); + + //昨日同比 + c.clear(); + c.setTime(today); + c.add(Calendar.DAY_OF_YEAR, -1); + dto.setStartTime(c.getTime()); + c.add(Calendar.DAY_OF_YEAR, 1); + dto.setEndTime(c.getTime()); + long wxYesterdayCount = wxCUserService.findCount(dto, tenantEntitys); + if (wxYesterdayCount > 0) { + double yesterday = (double) (wxTodayCount - wxYesterdayCount) / wxYesterdayCount * 100; + double zrhb = new BigDecimal(yesterday).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); + map.put("wxhb", zrhb + "%"); + } else { + map.put("wxhb", "--%"); + } + return new ResultData(map); + } + + @ApiOperation("查询会员数量") + @GetMapping("/findBasicUserCount") + @SystemControllerLog(description = "查询会员数量") + public ResultData findBasicUserCount() { + logger.debug("[" + getIpAddr() + "] WxUserStatisticsController::findBasicUserCount"); + + TenantEntity tenantEntity = getTenantInfo(); + List tenantEntitys = wxMallService.getTenantEntitys(tenantEntity); + //昨日同比 + Map map = new HashMap<>(); + WxCUserBasicInfoDto dto = new WxCUserBasicInfoDto(); + dto.updateTenantInfo(tenantEntity); + dto.setEndTime(new Date()); + //会员用户数据 + long memTotalCount = wxCUserBasicInfoService.findCount(ifParentUpdateTenantInfo(),dto);//总量 + + map.put("memTotalCount",memTotalCount); + + 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 memTodayCount = wxCUserBasicInfoService.findCount(ifParentUpdateTenantInfo(),dto);//今天新增 + + map.put("memTodayCount",memTodayCount); + + //昨日同比 + c.clear(); + c.setTime(today); + c.add(Calendar.DAY_OF_YEAR, -1); + dto.setStartTime(c.getTime()); + c.add(Calendar.DAY_OF_YEAR, 1); + dto.setEndTime(c.getTime()); + long memYesterdayCount = wxCUserBasicInfoService.findCount(ifParentUpdateTenantInfo(),dto); + if (memYesterdayCount > 0) { + double yesterday = (double) (memTodayCount - memYesterdayCount) / memYesterdayCount * 100; + double zrhb = new BigDecimal(yesterday).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); + map.put("memhb", zrhb + "%"); + } else { + map.put("memhb", "--%"); + } + + return new ResultData(map); + } + + @ApiOperation("查询微信用户趋势") + @GetMapping("/findWxUserTrend") + @SystemControllerLog(description = "查询微信用户趋势") + public ResultData findWxUserTrend(Date startTime, Date endTime, Integer reportType) { + logger.debug("[" + getIpAddr() + "] WxUserStatisticsController::findUserCount"); + + TenantEntity tenantEntity = getTenantInfo(); + List tenantEntitys = wxMallService.getTenantEntitys(tenantEntity); + + WxCUserBasicInfoDto dto = new WxCUserBasicInfoDto(); + dto.updateTenantInfo(tenantEntity); + + if (startTime == null || endTime == null) { + startTime = addMonth(new Date(), -1); + endTime = new Date(); + reportType = EnumUserCountReportType.DAY.getCode(); + } + + dto.updateTenantInfo(tenantEntity); + dto.setStartTime(startTime); + dto.setEndTime(endTime); + dto.setReportType(reportType); + + List wxCountList = new ArrayList<>();//每日新增会员数 + List wxCounts = wxCUserService.findCountHistory(dto,tenantEntitys);//每日新增会员数 + + SimpleDateFormat sdf; + SimpleDateFormat fsdf; + if (reportType.equals(EnumUserCountReportType.YEAR.getCode())) { + sdf = new SimpleDateFormat("yyyy"); + fsdf = new SimpleDateFormat("yyyy"); + }else if (reportType.equals(EnumUserCountReportType.MONTH.getCode())) { + sdf = new SimpleDateFormat("yyyy-MM"); + fsdf = new SimpleDateFormat("yyyy-MM"); + }else { + sdf = new SimpleDateFormat("yyyy-MM-dd"); + fsdf = new SimpleDateFormat("MM-dd"); + } + + Date curTime = new Date(startTime.getTime()); + do { + UserCountVo wxUc = new UserCountVo(); + wxUc.setReportTime(fsdf.format(curTime)); + + final Date ct = new Date(curTime.getTime()); + + if (ct.before(new Date()) || ct.equals(new Date())) { + //用户 + Optional opWxUserCountVo = + wxCounts.stream().filter(md -> md.getReportTime().equals(sdf.format(ct))).findFirst(); + if (opWxUserCountVo.isPresent()) { + wxUc.setIncCount(opWxUserCountVo.get().getIncCount()); + wxUc.setTotalCount(opWxUserCountVo.get().getTotalCount()); + } else { + wxUc.setIncCount(0L); + wxUc.setTotalCount(0L); + } + + } + wxCountList.add(wxUc); + if (reportType.equals(EnumUserCountReportType.YEAR.getCode())) { + curTime = addYear(curTime, 1); + } else if (reportType.equals(EnumUserCountReportType.MONTH.getCode())) { + curTime = addMonth(curTime, 1); + } else { + curTime = addDay(curTime, 1); + } + + }while (curTime.before(endTime) || curTime.equals(endTime)); + + + if (wxCountList.size() > 0 + && wxCountList.get(0).getTotalCount() != null + && wxCountList.get(0).getTotalCount() == 0) { + WxCUserBasicInfoDto pdto = new WxCUserBasicInfoDto(); + pdto.updateTenantInfo(tenantEntity); + pdto.setEndTime(startTime); + wxCountList.get(0).setTotalCount(wxCUserService.findCount(pdto, tenantEntitys)); + } + for (int i = 1; i < wxCountList.size(); i++){ + if (wxCountList.get(i).getTotalCount() != null && + wxCountList.get(i-1).getTotalCount() != null && + wxCountList.get(i).getTotalCount() < wxCountList.get(i-1).getTotalCount()){ + wxCountList.get(i).setTotalCount(wxCountList.get(i-1).getTotalCount()); + } + } + + return new ResultData(wxCountList); + } + + @ApiOperation("查询会员趋势") + @GetMapping("/findBasicUserTrend") + @SystemControllerLog(description = "查询会员趋势") + public ResultData findBasicUserTrend(Date startTime, Date endTime, Integer reportType) { + logger.debug("[" + getIpAddr() + "] WxUserStatisticsController::findBasicUserTrend"); + + TenantEntity tenantEntity = getTenantInfo(); + List tenantEntitys = wxMallService.getTenantEntitys(tenantEntity); + + WxCUserBasicInfoDto dto = new WxCUserBasicInfoDto(); + dto.updateTenantInfo(tenantEntity); + + if (startTime == null || endTime == null) { + startTime = addMonth(new Date(), -1); + endTime = new Date(); + reportType = EnumUserCountReportType.DAY.getCode(); + } + + dto.updateTenantInfo(tenantEntity); + dto.setStartTime(startTime); + dto.setEndTime(endTime); + dto.setReportType(reportType); + + List memCountList = new ArrayList<>();//每日新增会员数 + List memCounts = wxCUserBasicInfoService.findCountHistory(ifParentUpdateTenantInfo(),dto);//每日新增会员数 + + SimpleDateFormat sdf; + SimpleDateFormat fsdf; + if (reportType.equals(EnumUserCountReportType.YEAR.getCode())) { + sdf = new SimpleDateFormat("yyyy"); + fsdf = new SimpleDateFormat("yyyy"); + }else if (reportType.equals(EnumUserCountReportType.MONTH.getCode())) { + sdf = new SimpleDateFormat("yyyy-MM"); + fsdf = new SimpleDateFormat("yyyy-MM"); + }else { + sdf = new SimpleDateFormat("yyyy-MM-dd"); + fsdf = new SimpleDateFormat("MM-dd"); + } + + Date curTime = new Date(startTime.getTime()); + do { + UserCountVo memUc = new UserCountVo(); + memUc.setReportTime(fsdf.format(curTime)); + + final Date ct = new Date(curTime.getTime()); + + if (ct.before(new Date()) || ct.equals(new Date())) { + + //会员 + Optional opMemUserCountVo = + memCounts.stream().filter(md -> md.getReportTime().equals(sdf.format(ct))).findFirst(); + if (opMemUserCountVo.isPresent()) { + memUc.setIncCount(opMemUserCountVo.get().getIncCount()); + memUc.setTotalCount(opMemUserCountVo.get().getTotalCount()); + } else { + memUc.setIncCount(0L); + memUc.setTotalCount(0L); + } + } + memCountList.add(memUc); + if (reportType.equals(EnumUserCountReportType.YEAR.getCode())) { + curTime = addYear(curTime, 1); + } else if (reportType.equals(EnumUserCountReportType.MONTH.getCode())) { + curTime = addMonth(curTime, 1); + } else { + curTime = addDay(curTime, 1); + } + + }while (curTime.before(endTime) || curTime.equals(endTime)); + + if (memCountList.size() > 0 + && memCountList.get(0).getTotalCount() != null + && memCountList.get(0).getTotalCount() == 0) { + WxCUserBasicInfoDto pdto = new WxCUserBasicInfoDto(); + pdto.setEndTime(startTime); + memCountList.get(0).setTotalCount(wxCUserBasicInfoService.findCount(ifParentUpdateTenantInfo(),pdto)); + } + for (int i = 1; i < memCountList.size(); i++){ + if ( memCountList.get(i).getTotalCount() != null && + memCountList.get(i-1).getTotalCount() != null && + memCountList.get(i).getTotalCount() < memCountList.get(i-1).getTotalCount()){ + memCountList.get(i).setTotalCount(memCountList.get(i-1).getTotalCount()); + } + } + + return new ResultData(memCountList); + } + + + private Date addDay(Date date , int addDayAmount) { + Calendar c = Calendar.getInstance(); + c.setTime(date); + c.add(Calendar.DATE, addDayAmount); + Date result = c.getTime(); + return result; + } + + private Date addMonth(Date date, int addMountAmount) { + Calendar c = Calendar.getInstance(); + c.setTime(date); + c.add(Calendar.MONTH, addMountAmount); + Date result = c.getTime(); + return result; + } + + private Date addYear(Date date, int addYearAmount) { + Calendar c = Calendar.getInstance(); + c.setTime(date); + c.add(Calendar.YEAR, addYearAmount); + Date result = c.getTime(); + return result; + } + +}