|
@@ -1,20 +1,27 @@
|
|
|
package com.gis.cms.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.gis.admin.entity.po.SysUserEntity;
|
|
|
import com.gis.admin.service.SysUserService;
|
|
|
+import com.gis.cms.entity.po.VillageCountEntity;
|
|
|
import com.gis.cms.entity.po.VillageEntity;
|
|
|
+import com.gis.cms.mapper.VillageCountMapper;
|
|
|
import com.gis.cms.mapper.VillageMapper;
|
|
|
import com.gis.cms.service.VillageService;
|
|
|
import com.gis.common.base.service.IBaseService;
|
|
|
+import com.gis.common.util.RedisUtil;
|
|
|
import com.gis.common.util.Result;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* Created by owen on 2022/5/19 0019 15:49
|
|
@@ -28,6 +35,15 @@ public class VillageServiceImpl extends ServiceImpl<VillageMapper, VillageEntity
|
|
|
|
|
|
@Autowired
|
|
|
SysUserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisUtil redisUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ VillageCountMapper villageCountMapper;
|
|
|
+
|
|
|
+ /**数据统计key*/
|
|
|
+ static final String STATISTICS_KEY = "statistics";
|
|
|
|
|
|
@Override
|
|
|
public Result getList() {
|
|
@@ -49,6 +65,9 @@ public class VillageServiceImpl extends ServiceImpl<VillageMapper, VillageEntity
|
|
|
@Override
|
|
|
public Result addVisit(String villageId) {
|
|
|
baseMapper.addVisit(villageId);
|
|
|
+ VillageCountEntity entity = new VillageCountEntity();
|
|
|
+ entity.setType("visit");
|
|
|
+ villageCountMapper.insert(entity);
|
|
|
return Result.success();
|
|
|
}
|
|
|
|
|
@@ -57,4 +76,52 @@ public class VillageServiceImpl extends ServiceImpl<VillageMapper, VillageEntity
|
|
|
baseMapper.addStar(villageId);
|
|
|
return Result.success();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result addShare(String villageId) {
|
|
|
+ baseMapper.addShare(villageId);
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getStatistics() {
|
|
|
+
|
|
|
+ if (redisUtil.hasKey(STATISTICS_KEY)){
|
|
|
+ log.info("统计接口走缓存");
|
|
|
+ Object cacheObject = redisUtil.getCacheObject(STATISTICS_KEY);
|
|
|
+ return Result.success(cacheObject);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<VillageEntity> list = this.list();
|
|
|
+ int visitCount = 0; // 总访问量
|
|
|
+ int shareCount = 0; // 总分享量
|
|
|
+ int starCount = 0; // 总点赞量
|
|
|
+
|
|
|
+ for (VillageEntity entity : list) {
|
|
|
+ visitCount += entity.getVisit();
|
|
|
+ shareCount += entity.getShare();
|
|
|
+ starCount += entity.getStar();
|
|
|
+ }
|
|
|
+
|
|
|
+ HashMap<Object, Object> map = new HashMap<>();
|
|
|
+ map.put("visitCount", visitCount);
|
|
|
+ map.put("shareCount", shareCount);
|
|
|
+ map.put("starCount", starCount);
|
|
|
+ map.put("visitToday", getVisitToday()); // 今日访问量
|
|
|
+ map.put("village", list);
|
|
|
+
|
|
|
+ redisUtil.setCacheObject(STATISTICS_KEY, map, 5, TimeUnit.MINUTES);
|
|
|
+
|
|
|
+ return Result.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer getVisitToday(){
|
|
|
+ LocalDate date = LocalDate.now();
|
|
|
+ log.info("日期: {}", date);
|
|
|
+ StringBuilder sql = new StringBuilder();
|
|
|
+ sql.append("select count(id) from tb_village_count where is_delete=0 and type='visit' ");
|
|
|
+ sql.append(StrUtil.format(" and DATE_FORMAT( create_time, '%Y%m%d' ) = DATE_FORMAT('{}' , '%Y%m%d' )", date));
|
|
|
+
|
|
|
+ return baseMapper.visitToday(sql.toString());
|
|
|
+ }
|
|
|
}
|