Commit b0f19646 by 吴迪

添加点击统计

parent 8ed109be
package io.office.common.enumpack.manage;
/**
* @author wudi
* @date 2023/7/9
* @comment
*/
public enum ClickHitEnum {
;
private String code;
private String value;
ClickHitEnum(String code, String value) {
this.code = code;
this.value = value;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
......@@ -8,6 +8,7 @@ import io.office.common.utils.R;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
......@@ -34,6 +35,13 @@ public class RRExceptionHandler {
return r;
}
@ExceptionHandler(NoSuchBeanDefinitionException.class)
public R NoSuchBeanDefinitionException(Exception e) {
logger.error(e.getMessage(), e);
return R.error(403, "参数不正确,请检查参数是否正确");
}
@ExceptionHandler(NoHandlerFoundException.class)
public R handlerNoFoundException(Exception e) {
logger.error(e.getMessage(), e);
......
package io.office.modules.manage.controller;
import io.office.common.utils.R;
import io.office.modules.manage.entity.dto.ClickHitDTO;
import io.office.modules.manage.strategy.ClickHitStrategy;
import io.office.modules.manage.strategy.ClickHitStrategyContext;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wudi
* @date 2023/7/9
* @comment
*/
@RestController
@RequestMapping("/clickHit")
@RequiredArgsConstructor
public class ClickHitController {
private final ClickHitStrategyContext clickHitStrategyContext;
@RequestMapping("/api/save")
// @RequiresPermissions("datamanage:clickdetail:save")
public R save(@RequestBody ClickHitDTO clickHitDTO) {
ClickHitStrategy clickHitStrategy = clickHitStrategyContext.getStrategy(clickHitDTO.getBusinessCode(), ClickHitStrategy.class);
if (clickHitStrategy == null) {
return R.error("code错误!");
}
clickHitStrategy.addClickHit(clickHitDTO.getBusinessCode(),clickHitDTO.getId());
return R.ok();
}
}
package io.office.modules.manage.entity.dto;
import lombok.Data;
import java.io.Serializable;
/**
* @author wudi
* @date 2023/7/9
* @comment 点击量统计实体类
*/
@Data
public class ClickHitDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String businessCode;
private String id;
}
package io.office.modules.manage.strategy;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.office.common.exception.RRException;
import io.office.modules.manage.dao.NewsDao;
import io.office.modules.manage.entity.NewsEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
* @author wudi
* @date 2023/7/9
* {@code @comment} 中心咨询策略实现
*/
@Component(value = "centerNewsStrategy")
@RequiredArgsConstructor
public class CenterNewsStrategy implements ClickHitStrategy {
private final NewsDao newsDao;
/**
* 添加业务点击事件
*
* @param businessCode,id
*/
@Transactional
@Override
public void addClickHit(String businessCode, String id) {
/**
* 1.先获取知识详情
* 2.如果是有效状态就+1
*/
NewsEntity newsEntity = newsDao.selectById(Integer.parseInt(id));
if (newsEntity != null) {
//点击量+1
NewsEntity newsUpdate = new NewsEntity();
if (newsEntity.getHits() != null) {
newsUpdate.setHits(newsEntity.getHits() + 1);
} else {
newsUpdate.setHits(1);
}
UpdateWrapper<NewsEntity> updateWrapper = new UpdateWrapper();
updateWrapper.eq("id", Integer.parseInt(id));
newsDao.update(newsUpdate, updateWrapper);
} else {
throw new RRException("id不存在,请核实后重试!");
}
}
}
package io.office.modules.manage.strategy;
/**
* @author wudi
* @date 2023/7/9
* @comment 点击事件策略
*/
public interface ClickHitStrategy {
/**
* 添加业务点击事件
* @param businessCode
*/
void addClickHit(String businessCode, String id);
}
package io.office.modules.manage.strategy;
import io.office.common.utils.SpringContextUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
/**
* @author wudi
* @date 2023/7/9
* @comment 点击事件策略加载器
*/
@Component
public class ClickHitStrategyContext {
public <T> T getStrategy(String strategyId,Class<T> t) {
// strategyId beanId
if (StringUtils.isEmpty(strategyId)) {
return null;
}
return SpringContextUtils.getBean(strategyId, t);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment