Commit 138d81f3 138d81f33d6c02e0585df1f5d30b7c91ad73b837 by 张云鹏

内容访问统计

1 parent c42d214b
Showing 18 changed files with 941 additions and 0 deletions
package com.topdraw.business.module.content.all.domain;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author XiangHan
* @date 2022-02-26
*/
@Entity
@Data
@EntityListeners(AuditingEntityListener.class)
@Accessors(chain = true)
@Table(name="content_pv_uv")
public class ContentPVUV implements Serializable {
// ID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
// 活动按钮id
@Column(name = "template_param_value_id", nullable = false)
private Long templateParamValueId;
@Column(name = "btn_name", nullable = false)
private String btnName;
// 显示用名称,表示哪个按钮或者内容
@Column(name = "code", nullable = false)
private String code;
// 显示用名称,表示哪个按钮或者内容
@Column(name = "name", nullable = false)
private String name;
// pv值
@Column(name = "pv", nullable = false)
private Integer pv;
// uv值
@Column(name = "uv", nullable = false)
private Integer uv;
@Column(name = "lp_id")
private Long lpId;
@Column(name = "site_id")
private Long siteId;
// 创建时间
@CreatedDate
@Column(name = "create_time")
private Timestamp createTime;
// 更新时间
@LastModifiedDate
@Column(name = "update_time")
private Timestamp updateTime;
public void copy(ContentPVUV source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}
package com.topdraw.business.module.content.all.repository;
import com.topdraw.business.module.content.all.domain.ContentPVUV;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.Optional;
public interface ContentPVUVRepository extends JpaRepository<ContentPVUV, Long>, JpaSpecificationExecutor<ContentPVUV> {
Optional<ContentPVUV> findByTemplateParamValueIdAndCode(Long templateParamValueId, String code);
}
package com.topdraw.business.module.content.all.service;
import com.topdraw.business.module.content.all.domain.ContentPVUV;
public interface ContentPVUVService {
void create(ContentPVUV resources);
void update(ContentPVUV resources);
ContentPVUV findByBtnIdAndCode(Long btnId, String code);
}
package com.topdraw.business.module.content.all.service.impl;
import com.topdraw.business.module.content.all.domain.ContentPVUV;
import com.topdraw.business.module.content.all.repository.ContentPVUVRepository;
import com.topdraw.business.module.content.all.service.ContentPVUVService;
import com.topdraw.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ContentPVUVServiceImpl implements ContentPVUVService {
@Autowired
private ContentPVUVRepository contentPVUVRepository;
@Override
@Transactional(rollbackFor = Exception.class)
public void create(ContentPVUV resources) {
contentPVUVRepository.save(resources);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(ContentPVUV resources) {
ContentPVUV PvUv = contentPVUVRepository.findById(resources.getId()).orElseGet(ContentPVUV::new);
ValidationUtil.isNull( PvUv.getId(),"PvUv","id",resources.getId());
PvUv.copy(resources);
contentPVUVRepository.save(PvUv);
}
public ContentPVUV findByBtnIdAndCode(Long btnId, String code){
return contentPVUVRepository.findByTemplateParamValueIdAndCode(btnId, code).orElseGet(ContentPVUV::new);
}
}
package com.topdraw.business.module.content.day.domain;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author XiangHan
* @date 2022-02-26
*/
@Entity
@Data
@EntityListeners(AuditingEntityListener.class)
@Accessors(chain = true)
@Table(name="content_pv_uv_day")
public class ContentPVUVDay implements Serializable {
// ID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
// 活动按钮id
@Column(name = "template_param_value_id", nullable = false)
private Long templateParamValueId;
@Column(name = "btn_name", nullable = false)
private String btnName;
// 显示用名称,表示哪个按钮或者内容
@Column(name = "code", nullable = false)
private String code;
// 显示用名称,表示哪个按钮或者内容
@Column(name = "name", nullable = false)
private String name;
// pv值
@Column(name = "pv", nullable = false)
private Integer pv;
// uv值
@Column(name = "uv", nullable = false)
private Integer uv;
@Column(name = "lp_id")
private Long lpId;
@Column(name = "site_id")
private Long siteId;
@Column(name = "day")
private String day;
// 创建时间
@CreatedDate
@Column(name = "create_time")
private Timestamp createTime;
// 更新时间
@LastModifiedDate
@Column(name = "update_time")
private Timestamp updateTime;
public void copy(ContentPVUVDay source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}
package com.topdraw.business.module.content.day.repository;
import com.topdraw.business.module.content.day.domain.ContentPVUVDay;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.Optional;
public interface ContentPVUVDayRepository extends JpaRepository<ContentPVUVDay, Long>, JpaSpecificationExecutor<ContentPVUVDay> {
Optional<ContentPVUVDay> findByTemplateParamValueIdAndCodeAndDay(Long templateParamValueId, String code, String day);
}
package com.topdraw.business.module.content.day.service;
import com.topdraw.business.module.content.day.domain.ContentPVUVDay;
public interface ContentPVUVDayService {
void create(ContentPVUVDay resources);
void update(ContentPVUVDay resources);
ContentPVUVDay findByBtnIdAndCodeAndDay(Long btnId, String code, String day);
}
package com.topdraw.business.module.content.day.service.impl;
import com.topdraw.business.module.content.day.domain.ContentPVUVDay;
import com.topdraw.business.module.content.day.repository.ContentPVUVDayRepository;
import com.topdraw.business.module.content.day.service.ContentPVUVDayService;
import com.topdraw.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ContentPVUVDayServiceImpl implements ContentPVUVDayService {
@Autowired
private ContentPVUVDayRepository contentPVUVRepository;
@Override
@Transactional(rollbackFor = Exception.class)
public void create(ContentPVUVDay resources) {
contentPVUVRepository.save(resources);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(ContentPVUVDay resources) {
ContentPVUVDay PvUv = contentPVUVRepository.findById(resources.getId()).orElseGet(ContentPVUVDay::new);
ValidationUtil.isNull( PvUv.getId(),"PvUv","id",resources.getId());
PvUv.copy(resources);
contentPVUVRepository.save(PvUv);
}
public ContentPVUVDay findByBtnIdAndCodeAndDay(Long btnId, String code, String day){
return contentPVUVRepository.findByTemplateParamValueIdAndCodeAndDay(btnId, code, day).orElseGet(ContentPVUVDay::new);
}
}
package com.topdraw.business.module.content.hour.domain;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author XiangHan
* @date 2022-02-26
*/
@Entity
@Data
@EntityListeners(AuditingEntityListener.class)
@Accessors(chain = true)
@Table(name="content_pv_uv_hour")
public class ContentPVUVHour implements Serializable {
// ID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
// 活动按钮id
@Column(name = "template_param_value_id", nullable = false)
private Long templateParamValueId;
@Column(name = "btn_name", nullable = false)
private String btnName;
// 显示用名称,表示哪个按钮或者内容
@Column(name = "code", nullable = false)
private String code;
// 显示用名称,表示哪个按钮或者内容
@Column(name = "name", nullable = false)
private String name;
// pv值
@Column(name = "pv", nullable = false)
private Integer pv;
// uv值
@Column(name = "uv", nullable = false)
private Integer uv;
@Column(name = "lp_id")
private Long lpId;
@Column(name = "site_id")
private Long siteId;
@Column(name = "day")
private String day;
@Column(name = "hour")
private Integer hour;
// 创建时间
@CreatedDate
@Column(name = "create_time")
private Timestamp createTime;
// 更新时间
@LastModifiedDate
@Column(name = "update_time")
private Timestamp updateTime;
public void copy(ContentPVUVHour source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}
package com.topdraw.business.module.content.hour.repository;
import com.topdraw.business.module.content.hour.domain.ContentPVUVHour;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.Optional;
public interface ContentPVUVHourRepository extends JpaRepository<ContentPVUVHour, Long>, JpaSpecificationExecutor<ContentPVUVHour> {
Optional<ContentPVUVHour> findByTemplateParamValueIdAndCodeAndDayAndHour(Long templateParamValueId, String code, String day, Integer hour);
}
package com.topdraw.business.module.content.hour.service;
import com.topdraw.business.module.content.hour.domain.ContentPVUVHour;
public interface ContentPVUVHourService {
void create(ContentPVUVHour resources);
void update(ContentPVUVHour resources);
ContentPVUVHour findByBtnIdAndCodeAndDayAndHour(Long btnId, String code, String day, Integer hour);
}
package com.topdraw.business.module.content.hour.service.impl;
import com.topdraw.business.module.content.hour.domain.ContentPVUVHour;
import com.topdraw.business.module.content.hour.repository.ContentPVUVHourRepository;
import com.topdraw.business.module.content.hour.service.ContentPVUVHourService;
import com.topdraw.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ContentPVUVHourServiceImpl implements ContentPVUVHourService {
@Autowired
private ContentPVUVHourRepository contentPVUVRepository;
@Override
@Transactional(rollbackFor = Exception.class)
public void create(ContentPVUVHour resources) {
contentPVUVRepository.save(resources);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(ContentPVUVHour resources) {
ContentPVUVHour PvUv = contentPVUVRepository.findById(resources.getId()).orElseGet(ContentPVUVHour::new);
ValidationUtil.isNull( PvUv.getId(),"PvUv","id",resources.getId());
PvUv.copy(resources);
contentPVUVRepository.save(PvUv);
}
public ContentPVUVHour findByBtnIdAndCodeAndDayAndHour(Long btnId, String code, String day, Integer hour){
return contentPVUVRepository.findByTemplateParamValueIdAndCodeAndDayAndHour(btnId, code, day, hour).orElseGet(ContentPVUVHour::new);
}
}
package com.topdraw.business.module.template.domain;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author zhang sheng xiang
* @date 2021-09-07
*/
@Entity
@Data
@EntityListeners(AuditingEntityListener.class)
@Accessors(chain = true)
@Table(name = "tp_template_param_value")
public class TemplateParamValue implements Serializable {
// ID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
// 名称
@Column(name = "name")
private String name;
// 标识
@Column(name = "code")
private String code;
//模板参数id
@Column(name = "template_param_id")
private Long templateParamId;
// 模板关联实体类型 1: 活动类型;2:专题类型;3:页面类型
@Column(name = "content_type")
private Integer contentType;
// 模板关联实体id
@Column(name = "content_id")
private Long contentId;
// 参数类型 0:实体;1:常量;2:虚拟实体;3:图片
@Column(name = "type", nullable = false)
private Integer type;
// 组键
@Column(name = "`group`")
private String group;
// 组名称
@Column(name = "group_name")
private String groupName;
// 实体类型(仅当参数类型为实体时有效)
@Column(name = "entity")
private String entity;
// 值(实体类型下为实体id,常量类型下为常量值,虚拟实体和图片此属性无效)
@Column(name = "`value`")
private String value;
// 资源标志符,供模板引擎识别调度
@Column(name = "`uri`")
private String uri;
// 对象标识
@Column(name = "obj_code")
private String objCode;
// 描述
@Column(name = "description")
private String description;
// 图片
@Column(name = "image")
private String image;
// 图片
@Column(name = "images")
private String images;
// 顺序
@Column(name = "sequence", nullable = false)
private Integer sequence;
@Column(name = "component_data")
private String componentData;
// 组内顺序
@Column(name = "group_index")
private Integer groupIndex;
//事件类型 0:抽奖;1:投票;2:答题;3:闯关;4:签到(当entity是activity时生效)
@Column(name = "activity_type")
private Integer activity_type;
//当前页面标识
@Column(name = "page_code")
private String pageCode;
@Column(name = "page_title")
private String pageTitle;
//展现形式
@Column(name = "display_form")
private Integer displayForm;
@Column(name = "top")
private Integer top;
@Column(name = "`left`")
private Integer left;
@Column(name = "width")
private Integer width;
@Column(name = "height")
private Integer height;
// 创建时间
@CreatedDate
@Column(name = "create_time")
private Timestamp createTime;
// 更新时间
@LastModifiedDate
@Column(name = "update_time")
private Timestamp updateTime;
public void copy(TemplateParamValue source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}
package com.topdraw.business.module.template.repository;
import com.topdraw.business.module.template.domain.TemplateParamValue;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
public interface TemplateParamValueRepository extends JpaRepository<TemplateParamValue, Long>, JpaSpecificationExecutor<TemplateParamValue> {
List<TemplateParamValue> findByIdIn(List<Long> ids);
}
package com.topdraw.business.module.template.service;
import java.util.List;
import java.util.Map;
public interface TemplateParamValueService {
Map<Long, String> btnNames(List<Long> btnIds);
}
package com.topdraw.business.module.template.service.impl;
import com.topdraw.business.module.template.domain.TemplateParamValue;
import com.topdraw.business.module.template.repository.TemplateParamValueRepository;
import com.topdraw.business.module.template.service.TemplateParamValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class TemplateParamValueServiceImpl implements TemplateParamValueService {
@Autowired
private TemplateParamValueRepository templateParamValueRepository;
public Map<Long, String> btnNames(List<Long> btnIds){
List<TemplateParamValue> list = templateParamValueRepository.findByIdIn(btnIds);
Map<Long, String> result = list.stream().collect(Collectors.toMap(TemplateParamValue::getId, TemplateParamValue::getName));
return result;
}
}
package com.topdraw.business.process.calculate.task;
import com.topdraw.business.module.content.all.domain.ContentPVUV;
import com.topdraw.business.module.content.all.service.ContentPVUVService;
import com.topdraw.business.module.content.day.domain.ContentPVUVDay;
import com.topdraw.business.module.content.day.service.ContentPVUVDayService;
import com.topdraw.business.module.content.hour.domain.ContentPVUVHour;
import com.topdraw.business.module.content.hour.service.ContentPVUVHourService;
import com.topdraw.business.module.template.service.TemplateParamValueService;
import com.topdraw.business.module.uv.all.domain.PvUv;
import com.topdraw.business.module.uv.all.service.PvUvService;
import com.topdraw.business.module.uv.day.domain.PvUvDay;
import com.topdraw.business.module.uv.day.service.PvUvDayService;
import com.topdraw.business.module.uv.hour.domain.PvUvHour;
import com.topdraw.business.module.uv.hour.service.PvUvHourService;
import com.topdraw.util.TimestampUtil;
import com.topdraw.utils.RedisUtils;
import com.topdraw.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
@Slf4j
public class ContentCalculateTask {
@Autowired
private RedisUtils redisUtils;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private ContentPVUVService contentPVUVService;
@Autowired
private ContentPVUVDayService contentPVUVDayService;
@Autowired
private ContentPVUVHourService contentPVUVHourService;
@Autowired
private TemplateParamValueService templateParamValueService;
public boolean calculateRedisData2Mysql(){
//先查询所有内容信息(从redis中获取)
//循环所有内容,
Map<Object, Object> allContents = this.getAllContents();
Set<Object> contentCodes = allContents.keySet();
List<Long> btnIds = contentCodes.stream().map(item -> {return Long.parseLong(item.toString().split("#")[0]);}).collect(Collectors.toList());
Map<Long, String> btnNames = templateParamValueService.btnNames(btnIds);
//查询btn name集合
if (CollectionUtils.isNotEmpty(contentCodes)) {
for (Object item : contentCodes) {
String key = String.valueOf(item);
String[] split = key.split("#");
if (split.length < 2) {
continue;
}
Long btnId = Long.parseLong(split[0]);
String contentCode = split[1];
String name = allContents.get(item).toString();
name = StringUtils.isNotEmpty(name) ? name : btnNames.get(btnId);
String day = LocalDate.now().toString();
Integer hour = LocalDateTime.now().getHour();
// 小时pv、uv值
ContentPVUVHour pvUvHour1 =
this.contentPVUVHourService.
findByBtnIdAndCodeAndDayAndHour(btnId, contentCode, day, hour);
if (Objects.isNull(pvUvHour1.getId())) {
Integer hourPv = this.getHourPV(key);
Integer hourUv = this.getHourUV(key);
log.info("hourPv ==>> {}", hourPv);
log.info("hourUv ==>> {}", hourUv);
ContentPVUVHour pvUvHour = new ContentPVUVHour();
pvUvHour.setDay(LocalDate.now().toString());
pvUvHour.setHour(LocalDateTime.now().getHour());
pvUvHour.setPv(hourPv);
pvUvHour.setUv(hourUv);
pvUvHour.setHour(hour);
pvUvHour.setDay(day);
pvUvHour.setTemplateParamValueId(btnId);
pvUvHour.setBtnName(btnNames.get(btnId));
pvUvHour.setCode(contentCode);
pvUvHour.setName(name);
log.info("PvUvHour不存在,创建PvUvHour: {}", pvUvHour);
this.contentPVUVHourService.create(pvUvHour);
}
else {
Integer hourPv = this.getHourPV(key);
Integer hourUv = this.getHourUV(key);
log.info("访问每小时次数更新 redisPV: {}, dbPV: {}, redisUV: {}, dbUV: {}", hourPv, pvUvHour1.getPv(), hourUv, pvUvHour1.getUv());
if (pvUvHour1.getPv() != hourPv || pvUvHour1.getUv() != hourUv) {
if (pvUvHour1.getPv() >= hourPv) {
hourPv = pvUvHour1.getPv();
}
if (pvUvHour1.getUv() >= hourUv) {
hourUv = pvUvHour1.getUv();
}
pvUvHour1.setPv(hourPv);
pvUvHour1.setUv(hourUv);
pvUvHour1.setUpdateTime(TimestampUtil.now());
this.contentPVUVHourService.update(pvUvHour1);
}
}
// day pv、uv值
ContentPVUVDay pvUvDay1 = this.contentPVUVDayService.findByBtnIdAndCodeAndDay(btnId, contentCode, day);
if(Objects.isNull(pvUvDay1.getId())){
ContentPVUVDay pvUvDay = new ContentPVUVDay();
pvUvDay.setTemplateParamValueId(btnId);
pvUvDay.setBtnName(btnNames.get(btnId));
pvUvDay.setCode(contentCode);
pvUvDay.setName(name);
pvUvDay.setDay(day);
Integer dayPv = this.getDayPV(key);
Integer dayUv = this.getDayUV(key);
log.info("dayPv ==>> {}", dayPv);
log.info("dayUv ==>> {}", dayUv);
pvUvDay.setPv(dayPv);
pvUvDay.setUv(dayUv);
log.info("PvUvDay不存在,创建PvUvDay: {}", pvUvDay);
this.contentPVUVDayService.create(pvUvDay);
}
else {
Integer dayPv = this.getDayPV(key);
Integer dayUv = this.getDayUV(key);
log.info("dayPv ==>> {}", dayPv);
log.info("dayUv ==>> {}", dayUv);
log.info("访问每日次数更新 redisPV: {}, dbPV: {}, redisUV: {}, dbUV: {}", dayPv, pvUvDay1.getPv(), dayUv, pvUvDay1.getUv());
if (pvUvDay1.getPv() != dayPv || pvUvDay1.getUv() != dayUv) {
if (pvUvDay1.getPv() >= dayPv) {
dayPv = pvUvDay1.getPv();
}
if (pvUvDay1.getUv() >= dayUv) {
dayUv = pvUvDay1.getUv();
}
pvUvDay1.setPv(dayPv);
pvUvDay1.setUv(dayUv);
pvUvDay1.setUpdateTime(TimestampUtil.now());
this.contentPVUVDayService.update(pvUvDay1);
}
}
// all
ContentPVUV pvUv1 = this.contentPVUVService.findByBtnIdAndCode(btnId, contentCode);
if(Objects.isNull(pvUv1.getId())){
ContentPVUV pvUv = new ContentPVUV();
Integer allPV = this.getAllPV(key);
Integer allUv = this.getAllUV(key);
log.info("allPV ==>> {}", allPV);
log.info("allUv ==>> {}", allUv);
pvUv.setPv(allPV);
pvUv.setUv(allUv);
pvUv.setTemplateParamValueId(btnId);
pvUv.setBtnName(btnNames.get(btnId));
pvUv.setCode(contentCode);
pvUv.setName(name);
log.info("PvUv不存在,创建PvUvALL: {}", pvUv);
this.contentPVUVService.create(pvUv);
}
else {
Integer allPV = this.getAllPV(key);
Integer allUv = this.getAllUV(key);
log.info("访问总次数更新 redisPV: {}, dbPV: {}, redisUV: {}, dbUV: {}", allPV, pvUv1.getPv(), allUv, pvUv1.getUv());
if (pvUv1.getPv() != allPV || pvUv1.getUv() != allUv) {
if (pvUv1.getPv() >= allPV) {
allPV = pvUv1.getPv();
}
if (pvUv1.getUv() >= allUv) {
allUv = pvUv1.getUv();
}
pvUv1.setPv(allPV);
pvUv1.setUv(allUv);
pvUv1.setUpdateTime(TimestampUtil.now());
this.contentPVUVService.update(pvUv1);
}
}
}
}
return true;
}
private Integer getHourPV(String item){
int hour = LocalDateTime.now().getHour();
String hourStr = "";
if (hour < 10) {
hourStr = "0"+hour;
} else {
hourStr = String.valueOf(hour);
}
log.info("getHourPV ==>> key ==>> {}", "CONTENT_PV" + "|" + item +"|" + LocalDate.now()+"|"+hourStr);
Object hourBtnClickPV = this.redisUtils.hget("CONTENT_PV" + "|" + item +"|" + LocalDate.now()+"|"+hourStr, "ALL");
if (Objects.nonNull(hourBtnClickPV)) {
return Integer.valueOf(hourBtnClickPV.toString());
}
return 0;
}
private Integer getDayPV(String item){
log.info("getDayPV ==>> key ==>> {}", "CONTENT_PV" + "|" + item + "|" + LocalDate.now());
Object hourBtnClickPV = this.redisUtils.hget("CONTENT_PV" + "|" + item + "|" + LocalDate.now(), "ALL");
if (Objects.nonNull(hourBtnClickPV)) {
return Integer.valueOf(hourBtnClickPV.toString());
}
return 0;
}
private Integer getAllPV(String item){
log.info("getAllPV ==>> key ==>> {}", "CONTENT_PV" + "|" + item);
Object hourBtnClickPV = this.redisUtils.hget("CONTENT_PV" + "|" + item, "ALL");
if (Objects.nonNull(hourBtnClickPV)) {
return Integer.valueOf(hourBtnClickPV.toString());
}
return 0;
}
private Integer getHourUV(String item){
int hour = LocalDateTime.now().getHour();
String hourStr = "";
if (hour < 10) {
hourStr = "0"+hour;
} else {
hourStr = String.valueOf(hour);
}
log.info("getHourUV ==>> key ==>> {}", "CONTENT_UV" + "|" + item + "|" + LocalDate.now()+"|"+hourStr);
Object hourBtnClickUV = this.redisUtils.hget("CONTENT_UV" + "|" + item + "|" + LocalDate.now()+"|"+hourStr, "ALL");
if (Objects.nonNull(hourBtnClickUV)) {
return Integer.valueOf(hourBtnClickUV.toString());
}
return 0;
}
private Integer getDayUV(String item){
log.info("getDayUV ==>> key ==>> {}", "CONTENT_UV" + "|" + item + "|" + LocalDate.now());
Object hourBtnClickUV = this.redisUtils.hget("CONTENT_UV" + "|" + item + "|" + LocalDate.now(), "ALL");
if (Objects.nonNull(hourBtnClickUV)) {
return Integer.valueOf(hourBtnClickUV.toString());
}
return 0;
}
private Integer getAllUV(String item){
log.info("getAllUV ==>> key ==>> {}", "CONTENT_UV" + "|" + item);
Object hourBtnClickUV = this.redisUtils.hget("CONTENT_UV" + "|" + item, "ALL");
if (Objects.nonNull(hourBtnClickUV)) {
return Integer.valueOf(hourBtnClickUV.toString());
}
return 0;
}
private Map<Object,Object> getAllContents(){
log.info("getAllContents ==>> key ==>> {}", "CONTENT_BTN_CODE");
Map<Object, Object> allContents = this.redisUtils.hmget("CONTENT_BTN_CODE");
return allContents;
}
}
package com.topdraw.schedule;
import com.topdraw.business.process.calculate.task.ContentCalculateTask;
import com.topdraw.business.process.calculate.task.PvUvCalculateTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@Slf4j
@EnableScheduling
public class TransferContentScheduleTask {
@Autowired
private ContentCalculateTask contentCalculateTask;
@Value("${cronContent:0 0/5 * * * ?}")
private String cron;
public String cron(){
return cron;
}
/**
* 统计pv、uv
*/
@Scheduled(cron = "#{transferContentScheduleTask.cron()}")
public void calculatePvUv2Mysql(){
log.info("专题内容pv、uv统计 ===>>> 开始 !!!" + LocalDateTime.now());
this.contentCalculateTask.calculateRedisData2Mysql();
}
}