Commit 138d81f3 138d81f33d6c02e0585df1f5d30b7c91ad73b837 by 张云鹏

内容访问统计

1 parent c42d214b
Showing 18 changed files with 941 additions and 0 deletions
1 package com.topdraw.business.module.content.all.domain;
2
3 import cn.hutool.core.bean.BeanUtil;
4 import cn.hutool.core.bean.copier.CopyOptions;
5 import lombok.Data;
6 import lombok.experimental.Accessors;
7 import org.springframework.data.annotation.CreatedDate;
8 import org.springframework.data.annotation.LastModifiedDate;
9 import org.springframework.data.jpa.domain.support.AuditingEntityListener;
10
11 import javax.persistence.*;
12 import java.io.Serializable;
13 import java.sql.Timestamp;
14
15 /**
16 * @author XiangHan
17 * @date 2022-02-26
18 */
19 @Entity
20 @Data
21 @EntityListeners(AuditingEntityListener.class)
22 @Accessors(chain = true)
23 @Table(name="content_pv_uv")
24 public class ContentPVUV implements Serializable {
25
26 // ID
27 @Id
28 @GeneratedValue(strategy = GenerationType.IDENTITY)
29 @Column(name = "id")
30 private Long id;
31
32 // 活动按钮id
33 @Column(name = "template_param_value_id", nullable = false)
34 private Long templateParamValueId;
35
36 @Column(name = "btn_name", nullable = false)
37 private String btnName;
38
39 // 显示用名称,表示哪个按钮或者内容
40 @Column(name = "code", nullable = false)
41 private String code;
42
43 // 显示用名称,表示哪个按钮或者内容
44 @Column(name = "name", nullable = false)
45 private String name;
46
47 // pv值
48 @Column(name = "pv", nullable = false)
49 private Integer pv;
50
51 // uv值
52 @Column(name = "uv", nullable = false)
53 private Integer uv;
54
55 @Column(name = "lp_id")
56 private Long lpId;
57
58 @Column(name = "site_id")
59 private Long siteId;
60
61 // 创建时间
62 @CreatedDate
63 @Column(name = "create_time")
64 private Timestamp createTime;
65
66 // 更新时间
67 @LastModifiedDate
68 @Column(name = "update_time")
69 private Timestamp updateTime;
70
71 public void copy(ContentPVUV source){
72 BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
73 }
74 }
1 package com.topdraw.business.module.content.all.repository;
2
3 import com.topdraw.business.module.content.all.domain.ContentPVUV;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
6
7 import java.util.Optional;
8
9 public interface ContentPVUVRepository extends JpaRepository<ContentPVUV, Long>, JpaSpecificationExecutor<ContentPVUV> {
10
11 Optional<ContentPVUV> findByTemplateParamValueIdAndCode(Long templateParamValueId, String code);
12 }
1 package com.topdraw.business.module.content.all.service;
2
3 import com.topdraw.business.module.content.all.domain.ContentPVUV;
4
5 public interface ContentPVUVService {
6
7 void create(ContentPVUV resources);
8
9 void update(ContentPVUV resources);
10
11 ContentPVUV findByBtnIdAndCode(Long btnId, String code);
12
13 }
1 package com.topdraw.business.module.content.all.service.impl;
2
3 import com.topdraw.business.module.content.all.domain.ContentPVUV;
4 import com.topdraw.business.module.content.all.repository.ContentPVUVRepository;
5 import com.topdraw.business.module.content.all.service.ContentPVUVService;
6 import com.topdraw.utils.ValidationUtil;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Service;
9 import org.springframework.transaction.annotation.Transactional;
10
11 @Service
12 public class ContentPVUVServiceImpl implements ContentPVUVService {
13
14 @Autowired
15 private ContentPVUVRepository contentPVUVRepository;
16
17 @Override
18 @Transactional(rollbackFor = Exception.class)
19 public void create(ContentPVUV resources) {
20 contentPVUVRepository.save(resources);
21 }
22
23 @Override
24 @Transactional(rollbackFor = Exception.class)
25 public void update(ContentPVUV resources) {
26 ContentPVUV PvUv = contentPVUVRepository.findById(resources.getId()).orElseGet(ContentPVUV::new);
27 ValidationUtil.isNull( PvUv.getId(),"PvUv","id",resources.getId());
28 PvUv.copy(resources);
29 contentPVUVRepository.save(PvUv);
30 }
31
32 public ContentPVUV findByBtnIdAndCode(Long btnId, String code){
33 return contentPVUVRepository.findByTemplateParamValueIdAndCode(btnId, code).orElseGet(ContentPVUV::new);
34 }
35
36 }
1 package com.topdraw.business.module.content.day.domain;
2
3 import cn.hutool.core.bean.BeanUtil;
4 import cn.hutool.core.bean.copier.CopyOptions;
5 import lombok.Data;
6 import lombok.experimental.Accessors;
7 import org.springframework.data.annotation.CreatedDate;
8 import org.springframework.data.annotation.LastModifiedDate;
9 import org.springframework.data.jpa.domain.support.AuditingEntityListener;
10
11 import javax.persistence.*;
12 import java.io.Serializable;
13 import java.sql.Timestamp;
14
15 /**
16 * @author XiangHan
17 * @date 2022-02-26
18 */
19 @Entity
20 @Data
21 @EntityListeners(AuditingEntityListener.class)
22 @Accessors(chain = true)
23 @Table(name="content_pv_uv_day")
24 public class ContentPVUVDay implements Serializable {
25
26 // ID
27 @Id
28 @GeneratedValue(strategy = GenerationType.IDENTITY)
29 @Column(name = "id")
30 private Long id;
31
32 // 活动按钮id
33 @Column(name = "template_param_value_id", nullable = false)
34 private Long templateParamValueId;
35
36 @Column(name = "btn_name", nullable = false)
37 private String btnName;
38
39 // 显示用名称,表示哪个按钮或者内容
40 @Column(name = "code", nullable = false)
41 private String code;
42
43 // 显示用名称,表示哪个按钮或者内容
44 @Column(name = "name", nullable = false)
45 private String name;
46
47 // pv值
48 @Column(name = "pv", nullable = false)
49 private Integer pv;
50
51 // uv值
52 @Column(name = "uv", nullable = false)
53 private Integer uv;
54
55 @Column(name = "lp_id")
56 private Long lpId;
57
58 @Column(name = "site_id")
59 private Long siteId;
60
61 @Column(name = "day")
62 private String day;
63
64 // 创建时间
65 @CreatedDate
66 @Column(name = "create_time")
67 private Timestamp createTime;
68
69 // 更新时间
70 @LastModifiedDate
71 @Column(name = "update_time")
72 private Timestamp updateTime;
73
74 public void copy(ContentPVUVDay source){
75 BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
76 }
77 }
1 package com.topdraw.business.module.content.day.repository;
2
3 import com.topdraw.business.module.content.day.domain.ContentPVUVDay;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
6
7 import java.util.Optional;
8
9 public interface ContentPVUVDayRepository extends JpaRepository<ContentPVUVDay, Long>, JpaSpecificationExecutor<ContentPVUVDay> {
10
11 Optional<ContentPVUVDay> findByTemplateParamValueIdAndCodeAndDay(Long templateParamValueId, String code, String day);
12 }
1 package com.topdraw.business.module.content.day.service;
2
3 import com.topdraw.business.module.content.day.domain.ContentPVUVDay;
4
5 public interface ContentPVUVDayService {
6
7 void create(ContentPVUVDay resources);
8
9 void update(ContentPVUVDay resources);
10
11 ContentPVUVDay findByBtnIdAndCodeAndDay(Long btnId, String code, String day);
12
13 }
1 package com.topdraw.business.module.content.day.service.impl;
2
3 import com.topdraw.business.module.content.day.domain.ContentPVUVDay;
4 import com.topdraw.business.module.content.day.repository.ContentPVUVDayRepository;
5 import com.topdraw.business.module.content.day.service.ContentPVUVDayService;
6 import com.topdraw.utils.ValidationUtil;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Service;
9 import org.springframework.transaction.annotation.Transactional;
10
11 @Service
12 public class ContentPVUVDayServiceImpl implements ContentPVUVDayService {
13
14 @Autowired
15 private ContentPVUVDayRepository contentPVUVRepository;
16
17 @Override
18 @Transactional(rollbackFor = Exception.class)
19 public void create(ContentPVUVDay resources) {
20 contentPVUVRepository.save(resources);
21 }
22
23 @Override
24 @Transactional(rollbackFor = Exception.class)
25 public void update(ContentPVUVDay resources) {
26 ContentPVUVDay PvUv = contentPVUVRepository.findById(resources.getId()).orElseGet(ContentPVUVDay::new);
27 ValidationUtil.isNull( PvUv.getId(),"PvUv","id",resources.getId());
28 PvUv.copy(resources);
29 contentPVUVRepository.save(PvUv);
30 }
31
32 public ContentPVUVDay findByBtnIdAndCodeAndDay(Long btnId, String code, String day){
33 return contentPVUVRepository.findByTemplateParamValueIdAndCodeAndDay(btnId, code, day).orElseGet(ContentPVUVDay::new);
34 }
35
36 }
1 package com.topdraw.business.module.content.hour.domain;
2
3 import cn.hutool.core.bean.BeanUtil;
4 import cn.hutool.core.bean.copier.CopyOptions;
5 import lombok.Data;
6 import lombok.experimental.Accessors;
7 import org.springframework.data.annotation.CreatedDate;
8 import org.springframework.data.annotation.LastModifiedDate;
9 import org.springframework.data.jpa.domain.support.AuditingEntityListener;
10
11 import javax.persistence.*;
12 import java.io.Serializable;
13 import java.sql.Timestamp;
14
15 /**
16 * @author XiangHan
17 * @date 2022-02-26
18 */
19 @Entity
20 @Data
21 @EntityListeners(AuditingEntityListener.class)
22 @Accessors(chain = true)
23 @Table(name="content_pv_uv_hour")
24 public class ContentPVUVHour implements Serializable {
25
26 // ID
27 @Id
28 @GeneratedValue(strategy = GenerationType.IDENTITY)
29 @Column(name = "id")
30 private Long id;
31
32 // 活动按钮id
33 @Column(name = "template_param_value_id", nullable = false)
34 private Long templateParamValueId;
35
36 @Column(name = "btn_name", nullable = false)
37 private String btnName;
38
39 // 显示用名称,表示哪个按钮或者内容
40 @Column(name = "code", nullable = false)
41 private String code;
42
43 // 显示用名称,表示哪个按钮或者内容
44 @Column(name = "name", nullable = false)
45 private String name;
46
47 // pv值
48 @Column(name = "pv", nullable = false)
49 private Integer pv;
50
51 // uv值
52 @Column(name = "uv", nullable = false)
53 private Integer uv;
54
55 @Column(name = "lp_id")
56 private Long lpId;
57
58 @Column(name = "site_id")
59 private Long siteId;
60
61 @Column(name = "day")
62 private String day;
63
64 @Column(name = "hour")
65 private Integer hour;
66
67 // 创建时间
68 @CreatedDate
69 @Column(name = "create_time")
70 private Timestamp createTime;
71
72 // 更新时间
73 @LastModifiedDate
74 @Column(name = "update_time")
75 private Timestamp updateTime;
76
77 public void copy(ContentPVUVHour source){
78 BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
79 }
80 }
1 package com.topdraw.business.module.content.hour.repository;
2
3 import com.topdraw.business.module.content.hour.domain.ContentPVUVHour;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
6
7 import java.util.Optional;
8
9 public interface ContentPVUVHourRepository extends JpaRepository<ContentPVUVHour, Long>, JpaSpecificationExecutor<ContentPVUVHour> {
10
11 Optional<ContentPVUVHour> findByTemplateParamValueIdAndCodeAndDayAndHour(Long templateParamValueId, String code, String day, Integer hour);
12 }
1 package com.topdraw.business.module.content.hour.service;
2
3 import com.topdraw.business.module.content.hour.domain.ContentPVUVHour;
4
5 public interface ContentPVUVHourService {
6
7 void create(ContentPVUVHour resources);
8
9 void update(ContentPVUVHour resources);
10
11 ContentPVUVHour findByBtnIdAndCodeAndDayAndHour(Long btnId, String code, String day, Integer hour);
12
13 }
1 package com.topdraw.business.module.content.hour.service.impl;
2
3 import com.topdraw.business.module.content.hour.domain.ContentPVUVHour;
4 import com.topdraw.business.module.content.hour.repository.ContentPVUVHourRepository;
5 import com.topdraw.business.module.content.hour.service.ContentPVUVHourService;
6 import com.topdraw.utils.ValidationUtil;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Service;
9 import org.springframework.transaction.annotation.Transactional;
10
11 @Service
12 public class ContentPVUVHourServiceImpl implements ContentPVUVHourService {
13
14 @Autowired
15 private ContentPVUVHourRepository contentPVUVRepository;
16
17 @Override
18 @Transactional(rollbackFor = Exception.class)
19 public void create(ContentPVUVHour resources) {
20 contentPVUVRepository.save(resources);
21 }
22
23 @Override
24 @Transactional(rollbackFor = Exception.class)
25 public void update(ContentPVUVHour resources) {
26 ContentPVUVHour PvUv = contentPVUVRepository.findById(resources.getId()).orElseGet(ContentPVUVHour::new);
27 ValidationUtil.isNull( PvUv.getId(),"PvUv","id",resources.getId());
28 PvUv.copy(resources);
29 contentPVUVRepository.save(PvUv);
30 }
31
32 public ContentPVUVHour findByBtnIdAndCodeAndDayAndHour(Long btnId, String code, String day, Integer hour){
33 return contentPVUVRepository.findByTemplateParamValueIdAndCodeAndDayAndHour(btnId, code, day, hour).orElseGet(ContentPVUVHour::new);
34 }
35
36 }
1 package com.topdraw.business.module.template.domain;
2
3 import cn.hutool.core.bean.BeanUtil;
4 import cn.hutool.core.bean.copier.CopyOptions;
5 import lombok.Data;
6 import lombok.experimental.Accessors;
7 import org.springframework.data.annotation.CreatedDate;
8 import org.springframework.data.annotation.LastModifiedDate;
9 import org.springframework.data.jpa.domain.support.AuditingEntityListener;
10
11 import javax.persistence.*;
12 import java.io.Serializable;
13 import java.sql.Timestamp;
14
15 /**
16 * @author zhang sheng xiang
17 * @date 2021-09-07
18 */
19 @Entity
20 @Data
21 @EntityListeners(AuditingEntityListener.class)
22 @Accessors(chain = true)
23 @Table(name = "tp_template_param_value")
24 public class TemplateParamValue implements Serializable {
25
26 // ID
27 @Id
28 @GeneratedValue(strategy = GenerationType.IDENTITY)
29 @Column(name = "id")
30 private Long id;
31
32 // 名称
33 @Column(name = "name")
34 private String name;
35
36 // 标识
37 @Column(name = "code")
38 private String code;
39
40 //模板参数id
41 @Column(name = "template_param_id")
42 private Long templateParamId;
43
44 // 模板关联实体类型 1: 活动类型;2:专题类型;3:页面类型
45 @Column(name = "content_type")
46 private Integer contentType;
47
48 // 模板关联实体id
49 @Column(name = "content_id")
50 private Long contentId;
51
52 // 参数类型 0:实体;1:常量;2:虚拟实体;3:图片
53 @Column(name = "type", nullable = false)
54 private Integer type;
55
56 // 组键
57 @Column(name = "`group`")
58 private String group;
59
60 // 组名称
61 @Column(name = "group_name")
62 private String groupName;
63
64 // 实体类型(仅当参数类型为实体时有效)
65 @Column(name = "entity")
66 private String entity;
67
68 // 值(实体类型下为实体id,常量类型下为常量值,虚拟实体和图片此属性无效)
69 @Column(name = "`value`")
70 private String value;
71
72 // 资源标志符,供模板引擎识别调度
73 @Column(name = "`uri`")
74 private String uri;
75
76 // 对象标识
77 @Column(name = "obj_code")
78 private String objCode;
79
80 // 描述
81 @Column(name = "description")
82 private String description;
83
84 // 图片
85 @Column(name = "image")
86 private String image;
87
88 // 图片
89 @Column(name = "images")
90 private String images;
91
92 // 顺序
93 @Column(name = "sequence", nullable = false)
94 private Integer sequence;
95
96 @Column(name = "component_data")
97 private String componentData;
98
99 // 组内顺序
100 @Column(name = "group_index")
101 private Integer groupIndex;
102
103 //事件类型 0:抽奖;1:投票;2:答题;3:闯关;4:签到(当entity是activity时生效)
104 @Column(name = "activity_type")
105 private Integer activity_type;
106
107 //当前页面标识
108 @Column(name = "page_code")
109 private String pageCode;
110
111 @Column(name = "page_title")
112 private String pageTitle;
113
114 //展现形式
115 @Column(name = "display_form")
116 private Integer displayForm;
117
118 @Column(name = "top")
119 private Integer top;
120
121 @Column(name = "`left`")
122 private Integer left;
123
124 @Column(name = "width")
125 private Integer width;
126
127 @Column(name = "height")
128 private Integer height;
129
130 // 创建时间
131 @CreatedDate
132 @Column(name = "create_time")
133 private Timestamp createTime;
134
135 // 更新时间
136 @LastModifiedDate
137 @Column(name = "update_time")
138 private Timestamp updateTime;
139
140 public void copy(TemplateParamValue source){
141 BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
142 }
143 }
1 package com.topdraw.business.module.template.repository;
2
3 import com.topdraw.business.module.template.domain.TemplateParamValue;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
6
7 import java.util.List;
8
9 public interface TemplateParamValueRepository extends JpaRepository<TemplateParamValue, Long>, JpaSpecificationExecutor<TemplateParamValue> {
10
11 List<TemplateParamValue> findByIdIn(List<Long> ids);
12 }
1 package com.topdraw.business.module.template.service;
2
3 import java.util.List;
4 import java.util.Map;
5
6 public interface TemplateParamValueService {
7
8 Map<Long, String> btnNames(List<Long> btnIds);
9 }
1 package com.topdraw.business.module.template.service.impl;
2
3 import com.topdraw.business.module.template.domain.TemplateParamValue;
4 import com.topdraw.business.module.template.repository.TemplateParamValueRepository;
5 import com.topdraw.business.module.template.service.TemplateParamValueService;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service;
8
9 import java.util.List;
10 import java.util.Map;
11 import java.util.stream.Collectors;
12
13 @Service
14 public class TemplateParamValueServiceImpl implements TemplateParamValueService {
15
16 @Autowired
17 private TemplateParamValueRepository templateParamValueRepository;
18
19 public Map<Long, String> btnNames(List<Long> btnIds){
20 List<TemplateParamValue> list = templateParamValueRepository.findByIdIn(btnIds);
21 Map<Long, String> result = list.stream().collect(Collectors.toMap(TemplateParamValue::getId, TemplateParamValue::getName));
22 return result;
23 }
24
25 }
1 package com.topdraw.business.process.calculate.task;
2
3 import com.topdraw.business.module.content.all.domain.ContentPVUV;
4 import com.topdraw.business.module.content.all.service.ContentPVUVService;
5 import com.topdraw.business.module.content.day.domain.ContentPVUVDay;
6 import com.topdraw.business.module.content.day.service.ContentPVUVDayService;
7 import com.topdraw.business.module.content.hour.domain.ContentPVUVHour;
8 import com.topdraw.business.module.content.hour.service.ContentPVUVHourService;
9 import com.topdraw.business.module.template.service.TemplateParamValueService;
10 import com.topdraw.business.module.uv.all.domain.PvUv;
11 import com.topdraw.business.module.uv.all.service.PvUvService;
12 import com.topdraw.business.module.uv.day.domain.PvUvDay;
13 import com.topdraw.business.module.uv.day.service.PvUvDayService;
14 import com.topdraw.business.module.uv.hour.domain.PvUvHour;
15 import com.topdraw.business.module.uv.hour.service.PvUvHourService;
16 import com.topdraw.util.TimestampUtil;
17 import com.topdraw.utils.RedisUtils;
18 import com.topdraw.utils.StringUtils;
19 import lombok.extern.slf4j.Slf4j;
20 import org.apache.commons.collections4.CollectionUtils;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.jdbc.core.JdbcTemplate;
23 import org.springframework.stereotype.Service;
24 import org.springframework.transaction.annotation.Propagation;
25 import org.springframework.transaction.annotation.Transactional;
26
27 import java.time.LocalDate;
28 import java.time.LocalDateTime;
29 import java.util.*;
30 import java.util.stream.Collectors;
31
32 @Service
33 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
34 @Slf4j
35 public class ContentCalculateTask {
36
37 @Autowired
38 private RedisUtils redisUtils;
39 @Autowired
40 private JdbcTemplate jdbcTemplate;
41 @Autowired
42 private ContentPVUVService contentPVUVService;
43 @Autowired
44 private ContentPVUVDayService contentPVUVDayService;
45 @Autowired
46 private ContentPVUVHourService contentPVUVHourService;
47 @Autowired
48 private TemplateParamValueService templateParamValueService;
49
50 public boolean calculateRedisData2Mysql(){
51 //先查询所有内容信息(从redis中获取)
52 //循环所有内容,
53 Map<Object, Object> allContents = this.getAllContents();
54 Set<Object> contentCodes = allContents.keySet();
55 List<Long> btnIds = contentCodes.stream().map(item -> {return Long.parseLong(item.toString().split("#")[0]);}).collect(Collectors.toList());
56 Map<Long, String> btnNames = templateParamValueService.btnNames(btnIds);
57 //查询btn name集合
58 if (CollectionUtils.isNotEmpty(contentCodes)) {
59 for (Object item : contentCodes) {
60 String key = String.valueOf(item);
61 String[] split = key.split("#");
62 if (split.length < 2) {
63 continue;
64 }
65 Long btnId = Long.parseLong(split[0]);
66 String contentCode = split[1];
67 String name = allContents.get(item).toString();
68 name = StringUtils.isNotEmpty(name) ? name : btnNames.get(btnId);
69
70 String day = LocalDate.now().toString();
71 Integer hour = LocalDateTime.now().getHour();
72
73 // 小时pv、uv值
74 ContentPVUVHour pvUvHour1 =
75 this.contentPVUVHourService.
76 findByBtnIdAndCodeAndDayAndHour(btnId, contentCode, day, hour);
77 if (Objects.isNull(pvUvHour1.getId())) {
78
79 Integer hourPv = this.getHourPV(key);
80 Integer hourUv = this.getHourUV(key);
81 log.info("hourPv ==>> {}", hourPv);
82 log.info("hourUv ==>> {}", hourUv);
83
84 ContentPVUVHour pvUvHour = new ContentPVUVHour();
85 pvUvHour.setDay(LocalDate.now().toString());
86 pvUvHour.setHour(LocalDateTime.now().getHour());
87 pvUvHour.setPv(hourPv);
88 pvUvHour.setUv(hourUv);
89 pvUvHour.setHour(hour);
90 pvUvHour.setDay(day);
91 pvUvHour.setTemplateParamValueId(btnId);
92 pvUvHour.setBtnName(btnNames.get(btnId));
93 pvUvHour.setCode(contentCode);
94 pvUvHour.setName(name);
95 log.info("PvUvHour不存在,创建PvUvHour: {}", pvUvHour);
96 this.contentPVUVHourService.create(pvUvHour);
97
98 }
99 else {
100
101 Integer hourPv = this.getHourPV(key);
102 Integer hourUv = this.getHourUV(key);
103
104 log.info("访问每小时次数更新 redisPV: {}, dbPV: {}, redisUV: {}, dbUV: {}", hourPv, pvUvHour1.getPv(), hourUv, pvUvHour1.getUv());
105
106 if (pvUvHour1.getPv() != hourPv || pvUvHour1.getUv() != hourUv) {
107
108 if (pvUvHour1.getPv() >= hourPv) {
109 hourPv = pvUvHour1.getPv();
110 }
111
112 if (pvUvHour1.getUv() >= hourUv) {
113 hourUv = pvUvHour1.getUv();
114 }
115
116 pvUvHour1.setPv(hourPv);
117 pvUvHour1.setUv(hourUv);
118 pvUvHour1.setUpdateTime(TimestampUtil.now());
119 this.contentPVUVHourService.update(pvUvHour1);
120 }
121
122 }
123
124 // day pv、uv值
125 ContentPVUVDay pvUvDay1 = this.contentPVUVDayService.findByBtnIdAndCodeAndDay(btnId, contentCode, day);
126 if(Objects.isNull(pvUvDay1.getId())){
127 ContentPVUVDay pvUvDay = new ContentPVUVDay();
128 pvUvDay.setTemplateParamValueId(btnId);
129 pvUvDay.setBtnName(btnNames.get(btnId));
130 pvUvDay.setCode(contentCode);
131 pvUvDay.setName(name);
132 pvUvDay.setDay(day);
133
134 Integer dayPv = this.getDayPV(key);
135 Integer dayUv = this.getDayUV(key);
136 log.info("dayPv ==>> {}", dayPv);
137 log.info("dayUv ==>> {}", dayUv);
138 pvUvDay.setPv(dayPv);
139 pvUvDay.setUv(dayUv);
140 log.info("PvUvDay不存在,创建PvUvDay: {}", pvUvDay);
141 this.contentPVUVDayService.create(pvUvDay);
142 }
143 else {
144
145 Integer dayPv = this.getDayPV(key);
146 Integer dayUv = this.getDayUV(key);
147 log.info("dayPv ==>> {}", dayPv);
148 log.info("dayUv ==>> {}", dayUv);
149
150 log.info("访问每日次数更新 redisPV: {}, dbPV: {}, redisUV: {}, dbUV: {}", dayPv, pvUvDay1.getPv(), dayUv, pvUvDay1.getUv());
151
152 if (pvUvDay1.getPv() != dayPv || pvUvDay1.getUv() != dayUv) {
153
154 if (pvUvDay1.getPv() >= dayPv) {
155 dayPv = pvUvDay1.getPv();
156 }
157
158 if (pvUvDay1.getUv() >= dayUv) {
159 dayUv = pvUvDay1.getUv();
160 }
161
162 pvUvDay1.setPv(dayPv);
163 pvUvDay1.setUv(dayUv);
164 pvUvDay1.setUpdateTime(TimestampUtil.now());
165 this.contentPVUVDayService.update(pvUvDay1);
166 }
167
168 }
169
170 // all
171 ContentPVUV pvUv1 = this.contentPVUVService.findByBtnIdAndCode(btnId, contentCode);
172 if(Objects.isNull(pvUv1.getId())){
173 ContentPVUV pvUv = new ContentPVUV();
174
175 Integer allPV = this.getAllPV(key);
176 Integer allUv = this.getAllUV(key);
177 log.info("allPV ==>> {}", allPV);
178 log.info("allUv ==>> {}", allUv);
179 pvUv.setPv(allPV);
180 pvUv.setUv(allUv);
181 pvUv.setTemplateParamValueId(btnId);
182 pvUv.setBtnName(btnNames.get(btnId));
183 pvUv.setCode(contentCode);
184 pvUv.setName(name);
185 log.info("PvUv不存在,创建PvUvALL: {}", pvUv);
186 this.contentPVUVService.create(pvUv);
187
188 }
189 else {
190
191 Integer allPV = this.getAllPV(key);
192 Integer allUv = this.getAllUV(key);
193
194 log.info("访问总次数更新 redisPV: {}, dbPV: {}, redisUV: {}, dbUV: {}", allPV, pvUv1.getPv(), allUv, pvUv1.getUv());
195
196 if (pvUv1.getPv() != allPV || pvUv1.getUv() != allUv) {
197
198 if (pvUv1.getPv() >= allPV) {
199 allPV = pvUv1.getPv();
200 }
201
202 if (pvUv1.getUv() >= allUv) {
203 allUv = pvUv1.getUv();
204 }
205
206 pvUv1.setPv(allPV);
207 pvUv1.setUv(allUv);
208 pvUv1.setUpdateTime(TimestampUtil.now());
209 this.contentPVUVService.update(pvUv1);
210 }
211
212 }
213
214 }
215
216 }
217 return true;
218 }
219
220 private Integer getHourPV(String item){
221 int hour = LocalDateTime.now().getHour();
222 String hourStr = "";
223 if (hour < 10) {
224 hourStr = "0"+hour;
225 } else {
226 hourStr = String.valueOf(hour);
227 }
228 log.info("getHourPV ==>> key ==>> {}", "CONTENT_PV" + "|" + item +"|" + LocalDate.now()+"|"+hourStr);
229 Object hourBtnClickPV = this.redisUtils.hget("CONTENT_PV" + "|" + item +"|" + LocalDate.now()+"|"+hourStr, "ALL");
230 if (Objects.nonNull(hourBtnClickPV)) {
231 return Integer.valueOf(hourBtnClickPV.toString());
232 }
233
234 return 0;
235 }
236
237 private Integer getDayPV(String item){
238 log.info("getDayPV ==>> key ==>> {}", "CONTENT_PV" + "|" + item + "|" + LocalDate.now());
239 Object hourBtnClickPV = this.redisUtils.hget("CONTENT_PV" + "|" + item + "|" + LocalDate.now(), "ALL");
240 if (Objects.nonNull(hourBtnClickPV)) {
241 return Integer.valueOf(hourBtnClickPV.toString());
242 }
243
244 return 0;
245 }
246
247 private Integer getAllPV(String item){
248 log.info("getAllPV ==>> key ==>> {}", "CONTENT_PV" + "|" + item);
249 Object hourBtnClickPV = this.redisUtils.hget("CONTENT_PV" + "|" + item, "ALL");
250 if (Objects.nonNull(hourBtnClickPV)) {
251 return Integer.valueOf(hourBtnClickPV.toString());
252 }
253
254 return 0;
255 }
256
257 private Integer getHourUV(String item){
258 int hour = LocalDateTime.now().getHour();
259 String hourStr = "";
260 if (hour < 10) {
261 hourStr = "0"+hour;
262 } else {
263 hourStr = String.valueOf(hour);
264 }
265 log.info("getHourUV ==>> key ==>> {}", "CONTENT_UV" + "|" + item + "|" + LocalDate.now()+"|"+hourStr);
266 Object hourBtnClickUV = this.redisUtils.hget("CONTENT_UV" + "|" + item + "|" + LocalDate.now()+"|"+hourStr, "ALL");
267 if (Objects.nonNull(hourBtnClickUV)) {
268 return Integer.valueOf(hourBtnClickUV.toString());
269 }
270
271 return 0;
272 }
273
274 private Integer getDayUV(String item){
275 log.info("getDayUV ==>> key ==>> {}", "CONTENT_UV" + "|" + item + "|" + LocalDate.now());
276 Object hourBtnClickUV = this.redisUtils.hget("CONTENT_UV" + "|" + item + "|" + LocalDate.now(), "ALL");
277 if (Objects.nonNull(hourBtnClickUV)) {
278 return Integer.valueOf(hourBtnClickUV.toString());
279 }
280
281 return 0;
282 }
283
284 private Integer getAllUV(String item){
285 log.info("getAllUV ==>> key ==>> {}", "CONTENT_UV" + "|" + item);
286 Object hourBtnClickUV = this.redisUtils.hget("CONTENT_UV" + "|" + item, "ALL");
287 if (Objects.nonNull(hourBtnClickUV)) {
288 return Integer.valueOf(hourBtnClickUV.toString());
289 }
290
291 return 0;
292 }
293
294 private Map<Object,Object> getAllContents(){
295 log.info("getAllContents ==>> key ==>> {}", "CONTENT_BTN_CODE");
296 Map<Object, Object> allContents = this.redisUtils.hmget("CONTENT_BTN_CODE");
297 return allContents;
298 }
299
300 }
1 package com.topdraw.schedule;
2
3 import com.topdraw.business.process.calculate.task.ContentCalculateTask;
4 import com.topdraw.business.process.calculate.task.PvUvCalculateTask;
5 import lombok.extern.slf4j.Slf4j;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.beans.factory.annotation.Value;
8 import org.springframework.scheduling.annotation.EnableScheduling;
9 import org.springframework.scheduling.annotation.Scheduled;
10 import org.springframework.stereotype.Component;
11
12 import java.time.LocalDateTime;
13
14 @Component
15 @Slf4j
16 @EnableScheduling
17 public class TransferContentScheduleTask {
18
19 @Autowired
20 private ContentCalculateTask contentCalculateTask;
21
22 @Value("${cronContent:0 0/5 * * * ?}")
23 private String cron;
24
25 public String cron(){
26 return cron;
27 }
28
29 /**
30 * 统计pv、uv
31 */
32 @Scheduled(cron = "#{transferContentScheduleTask.cron()}")
33 public void calculatePvUv2Mysql(){
34 log.info("专题内容pv、uv统计 ===>>> 开始 !!!" + LocalDateTime.now());
35 this.contentCalculateTask.calculateRedisData2Mysql();
36 }
37
38 }