Commit 98cac6df by shenjunjie

首次提交

parents
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/log
*.log
/target
generatorConfig.xml
This diff is collapsed. Click to expand it.
package com.zhiwei.brandkbs2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author sjj
* @version 1.0
* @description
* @date 2022年4月18日9:54:31
*/
@EnableScheduling
@EnableCaching
/** mongoDB自定义启动 */
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, DataSourceAutoConfiguration.class})
@EnableAspectJAutoProxy
public class Brandkbs2Application {
public static void main(String[] args) {
SpringApplication.run(Brandkbs2Application.class, args);
}
}
package com.zhiwei.brandkbs2.auth;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import java.lang.annotation.*;
/**
* @author zzm
* @version V1.0
* @description 权限注解
* @date 2018/7/13 15:47
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Auth {
RoleEnum role();
}
package com.zhiwei.brandkbs2.auth;
import com.alibaba.fastjson.JSON;
import com.zhiwei.brandkbs2.common.GenericAttribute;
import com.zhiwei.brandkbs2.model.CommonCodeEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.pojo.UserInfo;
import com.zhiwei.brandkbs2.service.IUserService;
import com.zhiwei.middleware.auth.util.JwtUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
/**
* @author sjj
* @version 1.0
* @description 权限切面
* @date 2022年4月18日11:02:02
*/
@Aspect
@Component
public class AuthAspect {
@Value("${jwt.key}")
private String jwtKey;
@Resource(name = "userServiceImpl")
private IUserService iUserService;
@Pointcut("execution(com.zhiwei.brandkbs2.model.ResponseResult com.zhiwei.brandkbs2.controller..*.*(..))")
// @Pointcut("within(com.zhiwei.brandkbs2.controller..*)")
public void auth() {
}
@Around("auth()")
public Object aroundCheckToken(ProceedingJoinPoint joinPoint) throws Throwable {
Signature signature = joinPoint.getSignature();
Method method = ((MethodSignature) signature).getMethod();
Class<?> classTarget = joinPoint.getTarget().getClass();
// 优先使用方法权限
Auth auth = method.getAnnotation(Auth.class);
if (null == auth) {
auth = classTarget.getAnnotation(Auth.class);
}
// 不需要验证权限
boolean noAuth = null == auth;
ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
HttpServletRequest request = servletRequestAttributes.getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
String token = request.getHeader(jwtKey);
// 不存在token
if (null == token) {
return unAuthenticatedResponse(response, noAuth, joinPoint);
}
Map<String, Object> tokenInfo = JwtUtil.unsign(token, Map.class);
// 解析失败
if (null == tokenInfo) {
return unAuthenticatedResponse(response, noAuth, joinPoint);
}
String uid = tokenInfo.get(GenericAttribute.USER_ID).toString();
UserInfo userInfo = iUserService.queryUserInfo(uid, request.getHeader("pid"));
if (!noAuth && null == userInfo) {
return unAuthoriseResponse(response);
}
if (noAuth || (userInfo.getRoleId() <= auth.role().getState())) {
if (null == userInfo) {
userInfo = new UserInfo().setUserId(uid).setProjectId(request.getHeader("pid"));
}
UserThreadLocal.set(userInfo);
Object proceed = joinPoint.proceed();
UserThreadLocal.clear();
return proceed;
}
return unAuthoriseResponse(response);
}
// private Object localTestResponse(ProceedingJoinPoint joinPoint) throws Throwable {
// Map<String, Object> userInfo = new HashMap<>();
// userInfo.put("uid", "62734429af874c48ca5f7408");
// userInfo.put("rid", 1);
// userInfo.put("pid", "62661d5a06af3c654db15f53");
// UserThreadLocal.set(userInfo);
// Object proceed = joinPoint.proceed();
// UserThreadLocal.clear();
// return proceed;
// }
private Object unAuthenticatedResponse(HttpServletResponse response, boolean noAuth, ProceedingJoinPoint joinPoint) throws Throwable {
if (noAuth) {
return joinPoint.proceed();
}
if (null != response) {
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=UTF-8");
response.setStatus(200);
PrintWriter printWriter = response.getWriter();
String json = JSON.toJSONString(new ResponseResult(CommonCodeEnum.UNAUTHENTICATED, Collections.EMPTY_LIST));
printWriter.write(json);
printWriter.flush();
printWriter.close();
}
return null;
}
private Object unAuthoriseResponse(HttpServletResponse response) throws Exception {
if (null != response) {
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=UTF-8");
response.setStatus(200);
PrintWriter printWriter = response.getWriter();
String json = JSON.toJSONString(new ResponseResult(CommonCodeEnum.UN_AUTHORISE, Collections.EMPTY_LIST));
printWriter.write(json);
printWriter.flush();
printWriter.close();
}
return null;
}
}
package com.zhiwei.brandkbs2.auth;
import com.zhiwei.brandkbs2.pojo.UserInfo;
import java.util.Map;
/**
* @author sjj
* @version V1.0
* @description 存储当前线程的用户信息
* @date 2022年4月18日11:11:31
**/
public class UserThreadLocal {
private static final ThreadLocal<UserInfo> TOKEN_USER_INFO = new ThreadLocal<>();
public static Map<String, Object> get() {
return TOKEN_USER_INFO.get().toMap();
}
public static String getNickname() {
return TOKEN_USER_INFO.get().getNickname();
}
public static String getUserId() {
return TOKEN_USER_INFO.get().getUserId();
}
public static String getProjectId() {
return TOKEN_USER_INFO.get().getProjectId();
}
public static int getRoleId() {
return TOKEN_USER_INFO.get().getRoleId();
}
public static void set(UserInfo userInfo) {
TOKEN_USER_INFO.set(userInfo);
}
public static void clear() {
TOKEN_USER_INFO.remove();
}
}
package com.zhiwei.brandkbs2.common;
import com.zhiwei.middleware.mark.service.MarkerClient;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.TimeUnit;
/**
* @author sjj
* @version 1.0
* @description 项目的配置
* @date 2022年4月24日11:10:50
**/
@Configuration
public class CommonConfig {
@Value("${application.name}")
private String appName;
@Value("${mark.registry.address}")
private String clientRegistry;
@Value("${mark.provider.group}")
private String providerGroup;
@Bean
public RestTemplate restTemplate() {
final OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
return new RestTemplate(new OkHttp3ClientHttpRequestFactory(client));
}
/**
* 获取标注中间件客户端
*
* @return 标注中间件客户端
*/
@Bean
public MarkerClient getMarkerClient() {
return MarkerClient.getService(clientRegistry, providerGroup, appName);
}
}
package com.zhiwei.brandkbs2.common;
import com.zhiwei.brandkbs2.es.ITaskService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @ClassName: ControlCenter
* @Description 主控中心
* @author: sjj
* @date: 2022-06-08 17:39
*/
@Component
public class ControlCenter {
public static final Logger log = LogManager.getLogger(ControlCenter.class);
@Resource(name = "taskServiceImpl")
private ITaskService taskService;
@Async("scheduledExecutor")
@Scheduled(cron = "0 0 1 * * ?")
public void messageFlowCount() {
log.info("定时按天录入各小时渠道进量-启动");
try {
taskService.channelRecordFromEs(1);
} catch (Exception e) {
log.error("定时按天录入各小时渠道进量-出错", e);
} finally {
log.info("定时按天录入各小时渠道进量-结束");
}
}
}
package com.zhiwei.brandkbs2.common;
/**
* @ClassName: GenericAttribute
* @Description 一般属性值
* @author: sjj
* @date: 2022-04-21 10:10
*/
public class GenericAttribute {
private GenericAttribute() {
}
/**
* es index
*/
public static final String ES_INDEX_PRE = "brandkbs2";
public static final String ES_INDEX_TEST = "brandkbs_test";
/** es c2 **/
public static final String ES_C2 = "c2";
/** es c5 **/
public static final String ES_C5 = "c5";
/** es foreign **/
public static final String ES_FOREIGN = "foreign";
/** es source **/
public static final String ES_SOURCE = "source";
/** es real_source **/
public static final String ES_REAL_SOURCE = "real_source";
/** es time **/
public static final String ES_TIME = "time";
/** es ctime **/
public static final String ES_CTIME = "ctime";
/** es mtime **/
public static final String ES_MTIME = "mtime";
/** es mtag **/
public static final String ES_MTAG = "mtag";
/** es mgroup **/
public static final String ES_MGROUP = "mgroup";
/** es forward **/
public static final String ES_FORWARD = "forward";
/** es brandkbs_cache_maps **/
public static final String ES_BRANDKBS_CACHE_MAPS = "brandkbs_cache_maps";
/** es mark_cache_maps **/
public static final String ES_MARK_CACHE_MAPS = "mark_cache_maps";
public static final String LINKED_GROUP_ID = "linkedGroupId";
// public static final String PLATFORM = "platform";
/**
* 用户属性
*/
public static final String NICK_NAME = "nickname";
public static final String USER_ID = "userId";
public static final String ROLE_ID = "roleId";
public static final String PROJECT_ID = "projectId";
public static final String AVATAR_URL= "avatarUrl";
/**
* 标签相关
*/
public static final String EMOTION_LABEL_KEY = "情感倾向";
public static final String BRAND_LABEL_KEY = "品牌归属";
/**
* 报告相关
*/
public static final String MONTH_REPORT = "月报";
public static final String WEEK_REPORT = "周报";
// public enum ChannelParam{
// 负面稿件数("negativeArticles"),
// 参与负面事件("negativeEvents"),
// 特殊稿件("specialArticles"),
// 经验判断("experience"),
// 正面_中性("positiveNeutral"),
// 正面_负面("positiveNegative"),
// 参与正面事件("positiveEvents");
//
// final String value;
// ChannelParam(String value){
// this.value = value;
// }
// public String value(){
// return value;
// }
// }
}
package com.zhiwei.brandkbs2.common;
import com.zhiwei.brandkbs2.service.ISystemInfoService;
import com.zhiwei.qbjc.bean.pojo.common.MessagePlatform;
import com.zhiwei.qbjc.bean.pojo.common.Tag;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @ClassName: GlobalPojo
* @Description 全局对象类
* @author: sjj
* @date: 2022-06-16 14:14
*/
@Component
public class GlobalPojo {
public static final Logger log = LogManager.getLogger(GlobalPojo.class);
@Resource(name = "systemInfoServiceImpl")
private ISystemInfoService systemInfoService;
/**
* 监测系统平台
**/
public static List<MessagePlatform> PLATFORMS;
/**
* 监测系统平台
**/
public static Map<String, List<Tag>> TAGS;
@PostConstruct
public void start() {
try {
updatePojo("启动获取");
} catch (Exception e) {
log.info("启动获取-出错", e);
}
}
@Async("scheduledExecutor")
@Scheduled(cron = "0 0/10 * * * ?")
public void updatePojo() {
try {
updatePojo("每十分钟");
} catch (Exception e) {
log.info("每10分钟更新任务失败", e);
}
}
private void updatePojo(String logMsg) {
PLATFORMS = systemInfoService.getPlatforms();
TAGS = systemInfoService.getTags().stream().collect(Collectors.groupingBy(Tag::getGroupName));
}
}
package com.zhiwei.brandkbs2.common;
import com.zhiwei.brandkbs2.auth.UserThreadLocal;
import java.util.Objects;
/**
* @ClassName: RedisKeyPrefix
* @Description 缓存Key前缀整合类
* @author: sjj
* @date: 2022-05-23 14:28
*/
public class RedisKeyPrefix {
private RedisKeyPrefix() {
}
private static final String SEPARATOR = ":";
/**
* 稿件数据上传缓存key
*/
private static final String EVENT_DATA_UPLOAD_PROGRESS = "brandkbs:event:eventData:progress:";
/**
* 舆情事件导入进展
*/
private static final String YUQING_PROGRESS = "brandkbs:event:yuqingImport:progress:";
/**
* 舆情事件分析进度
*/
private static final String EVENT_ANALYSIS_PROGRESS = "brandkbs:event:analysis:progress:";
public static String eventAnalysisProgress(String eventId) {
return RedisKeyPrefix.generateRedisKey(RedisKeyPrefix.EVENT_ANALYSIS_PROGRESS, UserThreadLocal.getProjectId(), eventId);
}
public static String yuqingProgressKey(String linkedGroupId) {
return yuqingProgressKey(UserThreadLocal.getProjectId(), linkedGroupId);
}
public static String yuqingProgressKey(String projectId, String linkedGroupId) {
return RedisKeyPrefix.generateRedisKey(RedisKeyPrefix.YUQING_PROGRESS, projectId, linkedGroupId);
}
public static String eventDataProgressKey(String ticket) {
return RedisKeyPrefix.generateRedisKey(RedisKeyPrefix.EVENT_DATA_UPLOAD_PROGRESS, UserThreadLocal.getProjectId(), ticket);
}
private static String generateRedisKey(String... keys) {
Objects.requireNonNull(keys);
StringBuilder sb = new StringBuilder(keys[0]);
for (int i = 1; i < keys.length; i++) {
sb.append(SEPARATOR).append(keys[i]);
}
return sb.toString();
}
}
package com.zhiwei.brandkbs2.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Description:
* @Author: shentao
* @Date: 2020/4/27 13:47
*/
@Component
@Data
@ConfigurationProperties(prefix = "es")
public class EsProperties {
/**
* httpClusterNodes
*/
private String httpClusterNodes;
/**
* clusterName 集群名
*/
private String clusterName;
/**
* 集群节点s
*/
private String clusterNodes;
private String username;
private String password;
}
package com.zhiwei.brandkbs2.config;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
/**
* MongoConfig
*
* @ClassName: MongoConfig
* @Description MongoConfig
* @author shentao
* @date 2019年7月8日 上午10:32:02
*/
@Configuration
public class MongoConfig {
public static final Logger log = LogManager.getLogger(MongoConfig.class);
@Value("${mongo.connectionsPerHost}")
private int connectionsPerHost;
@Value("${mongo.threadsAllowedToBlockForConnectionMultiplier}")
private int threadsAllowedToBlockForConnectionMultiplier;
@Value("${mongo.connectTimeout}")
private int connectTimeout;
@Value("${mongo.maxWaitTime}")
private int maxWaitTime;
@Value("${mongo.autoConnectRetry}")
private boolean autoConnectRetry;
@Value("${mongo.socketKeepAlive}")
private boolean socketKeepAlive;
@Value("${mongo.socketTimeout}")
private int socketTimeout;
@Value("${mongo.slaveOk}")
private boolean slaveOk;
@Value("${primary.uri}")
private String uri;
@Value("${secondary.uri}")
private String uri2;
private MongoDbFactory mongoDbFactory() {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectionsPerHost(connectionsPerHost);
builder.connectTimeout(connectTimeout);
builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
builder.maxWaitTime(maxWaitTime);
return new SimpleMongoDbFactory(new MongoClientURI(uri, builder));
}
@Primary
@Bean(name = "primaryMongoTemplate")
public MongoTemplate getMongoTemplate() {
log.info("@Primary");
MongoDbFactory mongoDbFactory = mongoDbFactory();
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, new MongoMappingContext());
// 不插入_class
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return new MongoTemplate(mongoDbFactory(), converter);
}
@Primary
@Bean(name = "secondaryMongoTemplate")
public MongoTemplate getMongoTemplate2() {
log.info("@Secondary");
MongoDbFactory mongoDbFactory = mongoDbFactory2();
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, new MongoMappingContext());
// 不插入_class
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return new MongoTemplate(mongoDbFactory2(), converter);
}
private MongoDbFactory mongoDbFactory2() {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectionsPerHost(connectionsPerHost);
builder.connectTimeout(connectTimeout);
builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
builder.maxWaitTime(maxWaitTime);
return new SimpleMongoDbFactory(new MongoClientURI(uri2, builder));
}
}
package com.zhiwei.brandkbs2.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: Swagger2Config
* @Description Swagger2Config
* @author: sjj
* @date: 2022-05-11 11:21
*/
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
@Value("${jwt.key}")
private String jwtKey;
private static final String PID_KEY = "pid";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zhiwei.brandkbs2.controller"))
.paths(PathSelectors.any())
.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContextList = new ArrayList<>();
List<SecurityReference> securityReferenceList = new ArrayList<>();
securityReferenceList.add(new SecurityReference(jwtKey, scopes()));
securityReferenceList.add(new SecurityReference(PID_KEY, scopes()));
securityContextList.add(SecurityContext
.builder()
.securityReferences(securityReferenceList)
.forPaths(PathSelectors.any())
.build()
);
return securityContextList;
}
private AuthorizationScope[] scopes() {
return new AuthorizationScope[]{new AuthorizationScope("global", "accessAnything")};
}
private List<ApiKey> securitySchemes() {
List<ApiKey> apiKeyList = new ArrayList<>();
apiKeyList.add(new ApiKey(jwtKey, jwtKey, "header"));
apiKeyList.add(new ApiKey(PID_KEY, PID_KEY, "header"));
return apiKeyList;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("品牌知识库2.0api文档")
.description("品牌知识库2.0api文档")
// .termsOfServiceUrl("/")
.version("1.0")
.build();
}
}
package com.zhiwei.brandkbs2.config;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @ClassName: TaskPoolConfig
* @Description 线程池配置类
* @author: sjj
* @date: 2022-06-16 10:01
*/
@Configuration
@EnableAsync
public class TaskPoolConfig {
private static final Logger log = LogManager.getLogger(TaskPoolConfig.class);
@Bean
public Executor scheduledExecutor() {
log.info("start scheduledExecutor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 配置核心线程数
executor.setCorePoolSize(5);
// 配置最大线程数
executor.setMaxPoolSize(5);
// 配置队列大小
executor.setQueueCapacity(10);
// 配置线程池中的线程的名称前缀
executor.setThreadNamePrefix("scheduled-");
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 执行初始化
executor.initialize();
return executor;
}
@Bean
public ThreadPoolTaskExecutor esSearchExecutor() {
log.info("start esSearchExecutor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 配置核心线程数
executor.setCorePoolSize(8);
// 配置最大线程数
executor.setMaxPoolSize(8);
// 配置队列大小
executor.setQueueCapacity(16);
// 配置线程池中的线程的名称前缀
executor.setThreadNamePrefix("esSearch-");
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 执行初始化
executor.initialize();
return executor;
}
}
package com.zhiwei.brandkbs2.controller;
import com.zhiwei.brandkbs2.auth.Auth;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName: AppArticleController
* @Description 提供前台稿件相关信息展示
* @author: sjj
* @date: 2022-06-20 09:27
*/
@RestController
@RequestMapping("/app/articles")
@Api(tags = "前台稿件展示接口", description = "提供前台稿件相关信息展示")
@Auth(role = RoleEnum.CUSTOMER)
public class AppArticleController extends BaseController{
}
package com.zhiwei.brandkbs2.controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author sjj
* @version 1.0
* @description 基础Controller
* @date 2022年4月25日17:31:54
**/
public class BaseController {
/**
* http请求
*/
protected HttpServletRequest request;
/**
* http响应
*/
protected HttpServletResponse response;
@ModelAttribute
public void setRequestAndResponse(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
}
}
\ No newline at end of file
package com.zhiwei.brandkbs2.controller;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.auth.Auth;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.service.ICommonService;
import com.zhiwei.brandkbs2.service.IProjectService;
import com.zhiwei.middleware.mark.pojo.enums.TagField;
import com.zhiwei.middleware.mark.vo.MarkerTag;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @ClassName: CommonController
* @Description 基础信息接口
* @author: sjj
* @date: 2022-06-02 10:02
*/
@RestController
@RequestMapping("/common")
@Api(tags = "基础信息接口", description = "提供基础信息查询")
@Auth(role = RoleEnum.COMMON_ADMIN)
public class CommonController extends BaseController {
@Value("${qbjc.platform.url}")
private String qbjcPlatformUrl;
@Autowired
private RestTemplate restTemplate;
@Resource(name = "commonServiceImpl")
ICommonService commonService;
@ApiOperation("获取情感倾向标签信息")
@ApiImplicitParam(name = "linkedGroupId", value = "关联项目ID", required = true, paramType = "path", dataType = "string")
@GetMapping("/get/tag/emotion/{linkedGroupId}")
public ResponseResult getTagsWithEmotion(@PathVariable(value = "linkedGroupId") String linkedGroupId) {
List<String> res = new ArrayList<>();
List<MarkerTag> tags = commonService.getQbjcTags(linkedGroupId, TagField.GROUP_NAME.is("情感倾向"));
if (null != tags) {
res = tags.stream().map(MarkerTag::getName).collect(Collectors.toList());
}
return ResponseResult.success(res);
}
@ApiOperation("获取平台类型")
@GetMapping("/get/platform")
public ResponseResult getPlatform() {
try {
HttpEntity<JSONObject> entity = restTemplate.getForEntity(qbjcPlatformUrl, JSONObject.class);
return ResponseResult.success(Objects.requireNonNull(entity.getBody()).getJSONArray("data").toJavaList(JSONObject.class).stream().map(json -> json.getString("name")).collect(Collectors.toList()));
} catch (Exception e) {
return ResponseResult.failure(e.getMessage());
}
}
@ApiOperation("测试接口")
@GetMapping("/test")
public ResponseResult test() {
return ResponseResult.success("true");
}
}
package com.zhiwei.brandkbs2.controller;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.auth.Auth;
import com.zhiwei.brandkbs2.auth.UserThreadLocal;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.service.IProjectService;
import com.zhiwei.brandkbs2.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @ClassName: LoginController
* @Description 登录接口
* @author: sjj
* @date: 2022-05-05 13:56
*/
@RestController
@Api(tags = "用户登录接口", description = "实现用户登录")
public class LoginController extends BaseController {
@Value("${jwt.key}")
private String jwtKey;
@Value("${jwt.hour}")
private int jwtHour;
@Resource(name = "userServiceImpl")
private IUserService iUserService;
@Resource(name = "projectServiceImpl")
private IProjectService iProjectService;
@ApiOperation("用户登录")
@PostMapping("/user/login")
public ResponseResult login() {
return ResponseResult.success(iUserService.login().toMap());
}
@ApiOperation("用户信息获取")
@GetMapping("/user/getLoginInfo")
@Auth(role = RoleEnum.CUSTOMER)
public ResponseResult getLoginInfo() {
return ResponseResult.success(UserThreadLocal.get());
}
@ApiOperation("重置绑定关系(本地测试)")
@ApiImplicitParams(
@ApiImplicitParam(name = "username", value = "用户名", required = false, paramType = "query", dataType = "string"))
@PostMapping("/user/bind/reset")
public ResponseResult resetBind(@RequestBody JSONObject json) {
iUserService.resetBind(json.getString("username"));
return ResponseResult.success();
}
@ApiOperation("老用户账号绑定")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "用户名", required = false, paramType = "query", dataType = "string"),
@ApiImplicitParam(name = "password", value = "密码", required = false, paramType = "query", dataType = "string")})
@PostMapping("/user/bind")
public ResponseResult bindUser(@RequestBody JSONObject json) {
String username = json.getString("username");
String password = json.getString("password");
return ResponseResult.success(iUserService.bindUser(username, password));
}
@ApiOperation("跳过绑定(赋默认权限)")
@PostMapping("/user/bind/skip")
public ResponseResult skipBindUser() {
return ResponseResult.success(iUserService.skipBindUser());
}
@ApiOperation("校验用户是否已有绑定关系")
@GetMapping("/user/login/checkBind")
public ResponseResult checkBind() {
return ResponseResult.success(iUserService.checkUserRoles());
}
@ApiOperation("获取当前用户拥有的所有项目(含过期)")
@GetMapping("/user/login/getUserAllProjects")
public ResponseResult getLoginUserAllProjects() {
return ResponseResult.success(iProjectService.getLoginUserAllProjects());
}
}
package com.zhiwei.brandkbs2.controller.admin;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.auth.Auth;
import com.zhiwei.brandkbs2.auth.UserThreadLocal;
import com.zhiwei.brandkbs2.controller.BaseController;
import com.zhiwei.brandkbs2.easyexcel.EasyExcelUtil;
import com.zhiwei.brandkbs2.easyexcel.dto.ExportHighWordDTO;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.pojo.dto.TagFilterDTO;
import com.zhiwei.brandkbs2.pojo.vo.ProjectVO;
import com.zhiwei.brandkbs2.service.IHighWordService;
import com.zhiwei.brandkbs2.service.IProjectService;
import com.zhiwei.brandkbs2.service.ITagFilterService;
import com.zhiwei.brandkbs2.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.List;
/**
* @ClassName: BaseModuleController
* @Description 基础模块接口
* @author: sjj
* @date: 2022-05-31 16:06
*/
@RestController
@RequestMapping("/admin/module")
@Api(tags = "基础模块接口", description = "提供基础模块配置")
@Auth(role = RoleEnum.COMMON_ADMIN)
public class BaseModuleController extends BaseController {
@Resource(name = "tagFilterServiceImpl")
ITagFilterService tagFilterService;
@Resource(name = "highWordServiceImpl")
IHighWordService highWordService;
@Resource(name = "projectServiceImpl")
IProjectService projectService;
@ApiOperation("获取在用筛选器列表")
@GetMapping("/tagFilter")
public ResponseResult getTagFilter() {
return ResponseResult.success(tagFilterService.getTagFilter());
}
@ApiOperation("获取所有可选筛选器列表")
@GetMapping("/tagFilter/all")
public ResponseResult getTagFilterAll() {
return ResponseResult.success(tagFilterService.getTagFilterAll());
}
@ApiOperation("编辑筛选器列表")
@PutMapping("/tagFilter/update")
public ResponseResult updateTagFilter(@RequestBody List<TagFilterDTO> tagFilterDTOs) {
tagFilterService.updateTagFilter(tagFilterDTOs);
return ResponseResult.success();
}
@ApiOperation("获取高频关键词列表")
@ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"), @ApiImplicitParam(name = "size", value = "每页记录数", defaultValue = "10", paramType = "query", dataType = "int"), @ApiImplicitParam(name = "keyword", value = "搜索关键字", paramType = "query", dataType = "string")})
@GetMapping("/highWord/list")
public ResponseResult findHighWordList(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size, @RequestParam(value = "keyword", defaultValue = "") String keyword) {
return ResponseResult.success(highWordService.findHighWord(page, size, keyword));
}
@ApiOperation("添加高频关键词列表")
@ApiImplicitParams(@ApiImplicitParam(name = "list", value = "高频词列表", paramType = "body", dataType = "list"))
@PostMapping("/highWord/add")
public ResponseResult addHighWordList(@RequestBody JSONObject info) {
List<String> highWords = info.getJSONArray("list").toJavaList(String.class);
highWordService.addHighWordList(highWords);
return ResponseResult.success();
}
@ApiOperation("高频关键词删除")
@DeleteMapping(value = "/highWord/delete/{id}")
public ResponseResult deleteHighWord(@PathVariable String id) {
highWordService.deleteHighWord(id);
return ResponseResult.success();
}
@ApiOperation("高频关键词excel解析")
@PostMapping(value = "/highWord/excel/parse", headers = "content-type=multipart/form-data")
public ResponseResult parseExcelHighWord(@RequestParam("file") MultipartFile file) {
return Tools.parseExcelInfo(file);
}
@ApiOperation("高频关键词下载")
@GetMapping(value = "/highWord/download")
public ResponseResult downloadHighWord() {
List<ExportHighWordDTO> list = highWordService.downloadHighWord();
ProjectVO projectVO = projectService.getProjectVOById(UserThreadLocal.getProjectId());
EasyExcelUtil.download(projectVO.getBrandName() + "_高频关键词", "sheet1", ExportHighWordDTO.class, list, response);
return ResponseResult.success();
}
}
package com.zhiwei.brandkbs2.controller.admin;
import com.zhiwei.brandkbs2.auth.Auth;
import com.zhiwei.brandkbs2.auth.UserThreadLocal;
import com.zhiwei.brandkbs2.controller.BaseController;
import com.zhiwei.brandkbs2.easyexcel.EasyExcelUtil;
import com.zhiwei.brandkbs2.easyexcel.dto.ExportBehaviorDTO;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.service.IBehaviorService;
import com.zhiwei.brandkbs2.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @ClassName: BehaviorController
* @Description 用户行为管理接口
* @author: sjj
* @date: 2022-05-27 10:51
*/
@RestController
@RequestMapping("/admin/behavior")
@Api(tags = "用户行为管理接口", description = "提供用户行为的查询和导出功能")
@Auth(role = RoleEnum.COMMON_ADMIN)
public class BehaviorController extends BaseController {
@Resource(name = "behaviorServiceImpl")
private IBehaviorService behaviorService;
@Resource(name = "projectServiceImpl")
private IProjectService iProjectService;
@ApiOperation("分页查询用户行为列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
@ApiImplicitParam(name = "size", value = "每页记录数", defaultValue = "10", paramType = "query", dataType = "int"),
@ApiImplicitParam(name = "startTime", value = "开始时间", required = true, paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "endTime", value = "结束时间", required = true, paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "behavior", value = "行为所属(前台=false,后台=true)", defaultValue = "true", paramType = "query", dataType =
"boolean"),
@ApiImplicitParam(name = "keyword", value = "关键词",defaultValue = "", paramType = "query", dataType = "string"),
})
@GetMapping("/list")
public ResponseResult findList(@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "10") int size,
@RequestParam("startTime") long startTime,
@RequestParam("endTime") long endTime,
@RequestParam(value = "behavior", defaultValue = "true") boolean behavior,
@RequestParam(value = "keyword", defaultValue = "") String keyword) {
return ResponseResult.success(behaviorService.findBehaviorList(page, size, startTime, endTime, behavior, keyword));
}
@ApiOperation("导出用户行为列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "startTime", value = "开始时间", required = true, paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "endTime", value = "结束时间", required = true, paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "behavior", value = "行为所属(前台=false,后台=true)", defaultValue = "true", paramType = "query", dataType =
"boolean")
})
@GetMapping("/download")
public ResponseResult download(@RequestParam("startTime") long startTime,
@RequestParam("endTime") long endTime,
@RequestParam(value = "behavior", defaultValue = "true") boolean behavior) {
List<ExportBehaviorDTO> downloadList = behaviorService.download(startTime, endTime, behavior);
String behaviorName = behavior ? "后台" : "前台";
String sheetName = iProjectService.getProjectVOById(UserThreadLocal.getProjectId()).getProjectName() + "_" + behaviorName;
EasyExcelUtil.download(sheetName + "用户行为", sheetName, ExportBehaviorDTO.class, downloadList, response);
return ResponseResult.success();
}
}
package com.zhiwei.brandkbs2.controller.admin;
import com.zhiwei.brandkbs2.auth.Auth;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.pojo.dto.ReportSettingsDTO;
import com.zhiwei.brandkbs2.service.IReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author lxj
* @version 1.0
* @description 简报配置接口
* @date 2020/10/22 8:52
*/
@RestController
@RequestMapping("/admin/module/report")
@Api(tags = "简报配置接口", description = "提供简报配置功能")
@Auth(role= RoleEnum.COMMON_ADMIN)
public class ReportController {
@Resource(name = "reportServiceImpl")
private IReportService reportService;
@ApiOperation("获取简报设置")
@GetMapping("/settings")
public ResponseResult getReportSettings(){
return ResponseResult.success(reportService.getReportSettings());
}
@ApiOperation("添加或修改简报配置")
@PostMapping("/settings")
public ResponseResult upsertReportSettings(@RequestBody ReportSettingsDTO reportSettingsDTO) {
reportService.upsertReportSettings(reportSettingsDTO);
return ResponseResult.success();
}
}
package com.zhiwei.brandkbs2.controller.admin;
import com.zhiwei.brandkbs2.auth.Auth;
import com.zhiwei.brandkbs2.controller.BaseController;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.pojo.Behavior;
import com.zhiwei.brandkbs2.pojo.dto.UserDTO;
import com.zhiwei.brandkbs2.service.IBehaviorService;
import com.zhiwei.brandkbs2.service.IUserService;
import com.zhiwei.middleware.auth.pojo.CenterUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @ClassName: UserController
* @Description 用户管理接口
* @author: sjj
* @date: 2022-04-28 14:21
*/
@RestController
@RequestMapping("/admin/user")
@Api(tags = "用户管理接口", description = "提供用户的增、删、改、查")
@Auth(role = RoleEnum.COMMON_ADMIN)
public class UserController extends BaseController {
@Resource(name = "userServiceImpl")
private IUserService iUserService;
@Resource(name = "behaviorServiceImpl")
private IBehaviorService behaviorService;
private static final Behavior.Operation OPERATION = new Behavior.Operation("用户管理", true);
@Autowired
private com.zhiwei.middleware.auth.core.UserInfoClient userInfoClient;
@ApiOperation("分页查询用户列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
@ApiImplicitParam(name = "size", value = "每页记录数", required = false, defaultValue = "10", paramType = "query", dataType = "int"),
@ApiImplicitParam(name = "keyword", value = "搜索关键字", required = false, defaultValue = "", paramType = "query", dataType = "string"),
@ApiImplicitParam(name = "pid", value = "搜索项目ID", required = true, paramType = "query", dataType = "string"),
@ApiImplicitParam(name = "role", value = "角色类型", required = false, defaultValue = "-1", paramType = "query", dataType = "string")
})
@GetMapping("/list")
public ResponseResult findList(@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "10") int size,
@RequestParam(value = "keyword", defaultValue = "") String keyword,
@RequestParam(value = "pid") String pid,
@RequestParam(value = "role", defaultValue = "-1") int role) {
return ResponseResult.success(iUserService.findUserList(page, size, keyword, pid, role));
}
@ApiOperation("根据手机号搜索用户信息")
@ApiImplicitParam(name = "phoneNumber", value = "手机号", required = true, paramType = "path", dataType = "long")
@GetMapping("/search/{phoneNumber}")
public ResponseResult searchUserDTO(@PathVariable("phoneNumber") long phoneNumber) {
CenterUser centerUser = userInfoClient.getUserByPhone(String.valueOf(phoneNumber));
if (null == centerUser) {
return ResponseResult.failure("无匹配用户");
}
UserDTO userDTO = new UserDTO();
userDTO.setId(String.valueOf(centerUser.getId()));
userDTO.setUsername(centerUser.getName());
userDTO.setNickname(centerUser.getNickName());
userDTO.setPhoneNumber(Long.parseLong(centerUser.getPhone()));
return ResponseResult.success(userDTO);
}
@ApiOperation("添加用户")
@PostMapping("/add")
public ResponseResult addUser(@RequestBody UserDTO userDTO) {
iUserService.addUser(userDTO);
behaviorService.pushBehavior(OPERATION, "添加用户:" + userDTO.getId(), request);
return ResponseResult.success();
}
@ApiOperation("删除用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "uid", value = "用户ID", required = true, paramType = "path", dataType = "string"),
@ApiImplicitParam(name = "pid", value = "项目ID", required = true, paramType = "path", dataType = "string")})
@DeleteMapping("/delete")
@Auth(role = RoleEnum.ADMIN)
public ResponseResult deleteUser(@RequestParam(value = "uid") String userId, @RequestParam(value = "pid") String pid) {
iUserService.deleteUser(userId, pid);
behaviorService.pushBehavior(OPERATION, "删除用户:" + userId, request);
return ResponseResult.success();
}
@ApiOperation("编辑用户")
@PutMapping("/update")
public ResponseResult updateUser(@RequestBody UserDTO userDTO) {
iUserService.updateUser(userDTO);
behaviorService.pushBehavior(OPERATION, "编辑用户:" + userDTO, request);
return ResponseResult.success();
}
@ApiOperation("分页查询超级管理员")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
@ApiImplicitParam(name = "size", value = "每页记录数", required = false, defaultValue = "10", paramType = "query", dataType = "int"),
@ApiImplicitParam(name = "keyword", value = "搜索关键字", required = false, defaultValue = "", paramType = "query", dataType = "string")
})
@GetMapping("/list/superAdmin")
@Auth(role = RoleEnum.SUPER_ADMIN)
public ResponseResult findSuperAdminList(@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "10") int size,
@RequestParam(value = "keyword", defaultValue = "") String keyword) {
return ResponseResult.success(iUserService.findSuperAdminList(page, size, keyword));
}
@ApiOperation("添加超级管理员")
@PostMapping("/add/superAdmin")
@Auth(role = RoleEnum.SUPER_ADMIN)
public ResponseResult addSuperAdmin(@RequestBody UserDTO userDTO) {
iUserService.addSuperAdmin(userDTO);
behaviorService.pushBehavior(OPERATION, "添加超级管理员:" + userDTO.getId(), request);
return ResponseResult.success();
}
@ApiOperation("删除超级管理员")
@ApiImplicitParams(
@ApiImplicitParam(name = "uid", value = "用户ID", required = true, paramType = "path", dataType = "string"))
@DeleteMapping("/delete/superAdmin")
@Auth(role = RoleEnum.SUPER_ADMIN)
public ResponseResult deleteSuperAdmin(@RequestParam(value = "uid") String userId) {
iUserService.deleteSuperAdmin(userId);
behaviorService.pushBehavior(OPERATION, "删除超级管理员:" + userId, request);
return ResponseResult.success();
}
}
\ No newline at end of file
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.AbstractBaseMongo;
import com.zhiwei.brandkbs2.pojo.ChannelIndex;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
/**
* @ClassName: BaseMongoDao
* @Description 基础mongoDao实现
* @author: sjj
* @date: 2022-04-29 14:45
*/
public interface IBaseMongoDao<T extends AbstractBaseMongo> {
/**
* 插入一条数据
*/
void insertOne(T t, String... collectionNames);
/**
* 插入多条数据
*/
void insertMany(List<T> tList, String... collectionNames);
/**
* 插入一条数据(_id数据库自生成)
*/
void insertOneWithoutId(T t, String... collectionNames);
/**
* 通过id更新全量数据
*/
void updateOne(T t, String... collectionNames);
/**
* 通过id更新部分字段
*
* @param id 数据库唯一id
* @param update 需要更新部分的更新请求
*/
void updateOneByIdWithField(String id, Update update, String... collectionNames);
/**
* 通过id删除数据
*
* @param id 数据库唯一id
*/
void deleteOneById(String id, String... collectionNames);
/**
* 通过id查找单条数据
*
* @param id 数据库唯一id
* @return T
*/
T findOneById(String id, String... collectionNames);
/**
* 通过k-v查询单条数据
*
* @param key 属性名
* @param value 属性值
* @return T
*/
T findOne(String key, Object value, String... collectionNames);
/**
* 通过关键字模糊查询list
*
* @param keyword 模糊关键词
* @param fuzzKeys 需要模糊匹配的属性名
* @return list<T>
*/
List<T> findListByKeywordFuzz(String keyword, String[] fuzzKeys, String... collectionNames);
/**
* 通过关键字模糊查询list
*
* @param preQuery 查询请求
* @param keyword 模糊关键词
* @param fuzzKeys 需要模糊匹配的属性名
* @return list<T>
*/
List<T> findListByKeywordFuzz(Query preQuery, String keyword, String[] fuzzKeys, String... collectionNames);
/**
* 查询list
*
* @param preQuery 查询请求
* @return list<T>
*/
List<T> findList(Query preQuery,String... collectionNames);
/**
* 量查询
*
* @param preQuery 查询请求
* @return long
*/
long count(Query preQuery, String... collectionNames);
/**
* 添加模糊查询条件
*
* @param query query
* @param keyword 关键字
* @param fuzzKeys 匹配keys
*/
void addKeywordFuzz(Query query,String keyword,String... fuzzKeys);
/**
* 添加排序
*
* @param query query
* @param sorter 排序字段
*/
void addSort(Query query, String sorter);
/**
* 渠道唯一标识查询
*
* @param channelIndex 渠道标识
* @return 查询请求
*/
default Criteria addChannelIndex(ChannelIndex channelIndex) {
Criteria criteria = new Criteria();
criteria.and("platform").is(channelIndex.getPlatform());
criteria.and("realSource").is(channelIndex.getRealSource());
criteria.and("source").is(channelIndex.getSource());
criteria.and("linkedGroupId").is(channelIndex.getLinkedGroupId());
return criteria;
}
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.Behavior;
/**
* @ClassName: IBehaviorDao
* @Description IBehaviorDao
* @author: sjj
* @date: 2022-05-27 13:46
*/
public interface IBehaviorDao extends IBaseMongoDao<Behavior>, IShardingMongo {
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.Channel;
import com.zhiwei.brandkbs2.pojo.ChannelIndex;
/**
* @ClassName: IChannelDao
* @Description IChannelDao
* @author: sjj
* @date: 2022-06-16 15:30
*/
public interface IChannelDao extends IBaseMongoDao<Channel>{
Channel queryUnique(ChannelIndex channelIndex);
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.ChannelLabel;
import java.util.List;
/**
* @ClassName: ChannelLabelDao
* @Description 渠道标签Dao
* @author: sjj
* @date: 2022-06-20 16:53
*/
public interface IChannelLabelDao extends IBaseMongoDao<ChannelLabel>{
List<String> getChannelLabelType();
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.Event;
/**
* @ClassName: IEventDao
* @Description IEventDao
* @author: sjj
* @date: 2022-05-18 14:35
*/
public interface IEventDao extends IBaseMongoDao<Event>{
/**
* 是否已存在事件
*
* @param yqEventId 舆情事件id
* @param projectId 项目id
* @param linkedGroupId 关联项目组id
* @return 是否存在
*/
boolean existEventByUniqueIds(String yqEventId, String projectId, String linkedGroupId);
/**
* 根据联合id查询事件
*
* @param yqEventId 舆情事件id
* @param projectId 项目id
* @param linkedGroupId 关联项目id
* @return 事件
*/
Event getEventByUniqueIds(String yqEventId,String projectId,String linkedGroupId);
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.ChannelIndex;
import com.zhiwei.brandkbs2.pojo.Event;
import com.zhiwei.brandkbs2.pojo.EventData;
import java.util.Date;
import java.util.List;
/**
* @ClassName: IEventDataDao
* @Description IEventDataDao
* @author: sjj
* @date: 2022-05-19 16:08
*/
public interface IEventDataDao extends IBaseMongoDao<EventData>, IShardingMongo {
/**
* 获取首发稿件
*
* @return EventData
*/
EventData findFirstData(String eventId, String collectionName);
/**
* 获取参与事件数
*
* @param channelIndex 渠道标识
* @return 参与事件数
*/
List<String> getEventCount(ChannelIndex channelIndex);
/**
* 获取传播量
*
* @param event 事件
* @return 传播量
*/
long getEventArticleCount(Event event);
/**
* 获取渠道参与传播量
*
* @param event 事件
* @return 传播量
*/
long getEventArticleWithChannelCount(Event event, ChannelIndex channelIndex);
/**
* 删除事件ID关联数据
*/
void deleteByEventId(String eventId, String collectionName);
/**
* 查询符合时间段的所有数据
*
* @param eventId 事件id
* @param startTime 起始时间
* @param endTime 结束时间
* @param collectionName 集合名
* @return list
*/
List<EventData> findEventDataListByTime(String eventId, Date startTime, Date endTime, String collectionName);
/**
* 查询事件下的所有数据量
*
* @param eventId 事件id
* @param collectionName 集合名
* @return list
*/
long findEventDataCount(String eventId, String collectionName);
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.HighWord;
/**
* @ClassName: IHighWordDao
* @Description IHighWordDao
* @author: sjj
* @date: 2022-06-06 09:47
*/
public interface IHighWordDao extends IBaseMongoDao<HighWord>{
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.Project;
/**
* @author sjj
* @version 1.0
* @description IProjectDao
* @date 2022年4月20日17:38:54
*/
public interface IProjectDao extends IBaseMongoDao<Project>{
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.qbjc.bean.pojo.common.MessagePlatform;
import com.zhiwei.qbjc.bean.pojo.common.Tag;
import java.util.List;
/**
* IQbjcPojoDao-interface
*
* @ClassName: IQbjcPojoDao
* @Description IQbjcPojoDao-interface
* @author sjj
* @date 2022年6月16日14:23:36
*/
public interface IQbjcPojoDao {
/**
* 获取qbjcPlatform
*
* @return platforms
*/
List<MessagePlatform> findMessagePlatformAll();
/**
* 获取qbjcTag
*
* @return tags
*/
List<Tag> findTagAll();
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.Report;
/**
* @ClassName: IReportDao
* @Description IReportDao
* @author: sjj
* @date: 2022-05-31 18:13
*/
public interface IReportDao extends IBaseMongoDao<Report> {
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.ReportSettings;
/**
* @ClassName: IReportSettingsDao
* @Description IReportSettingsDao
* @author: sjj
* @date: 2022-05-31 18:13
*/
public interface IReportSettingsDao extends IBaseMongoDao<ReportSettings> {
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.util.IndexUtil;
import java.util.Date;
/**
* @ClassName: IShardingMongo
* @Description 分库mongo接口
* @author: sjj
* @date: 2022-05-27 11:29
*/
public interface IShardingMongo {
String collectionPrefix();
String timePattern();
default String generateCollectionName() {
return IndexUtil.getIndex(collectionPrefix(), timePattern());
}
default String[] generateCollectionNames(Date startTime, Date endTime) {
return IndexUtil.getIndexes(collectionPrefix(), timePattern(), startTime, endTime).toArray(new String[0]);
}
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.TagFilter;
/**
* @ClassName: ITagFilterDao
* @Description ITagFilterDao
* @author: sjj
* @date: 2022-06-01 13:52
*/
public interface ITagFilterDao extends IBaseMongoDao<TagFilter>{
}
package com.zhiwei.brandkbs2.dao;
import com.zhiwei.brandkbs2.pojo.User;
/**
* @ClassName: IUserDao
* @Description 用户相关接口
* @author: sjj
* @date: 2022-04-28 18:10
*/
public interface IUserDao extends IBaseMongoDao<User>{
}
package com.zhiwei.brandkbs2.dao.impl;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.dao.IBaseMongoDao;
import com.zhiwei.brandkbs2.pojo.AbstractBaseMongo;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* @ClassName: BaseMongoDaoImpl
* @Description mongo基础操作实现类
* @author: sjj
* @date: 2022-04-29 15:11
*/
public class BaseMongoDaoImpl<T extends AbstractBaseMongo> implements IBaseMongoDao<T> {
protected static final String ID = "_id";
private final String collectionName;
protected final Class<T> clazz;
@Resource(name = "primaryMongoTemplate")
protected MongoTemplate mongoTemplate;
public BaseMongoDaoImpl(String collectionName) {
this.collectionName = collectionName;
this.clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
@Override
public void insertOne(T t, String... collectionNames) {
mongoTemplate.insert(t, getCollections(collectionNames)[0]);
}
@Override
public void insertMany(List<T> tList, String... collectionNames) {
String[] collections = getCollections(collectionNames);
if (collections.length > 1) {
throw new IllegalArgumentException("collection cant not greater than 1");
}
mongoTemplate.insert(tList, collections[0]);
}
@Override
public void insertOneWithoutId(T t, String... collectionNames) {
t.setId(null);
mongoTemplate.insert(t, getCollections(collectionNames)[0]);
}
@Override
public void updateOne(T t, String... collectionNames) {
Document document = (Document) mongoTemplate.getConverter().convertToMongoType(t);
if (null != document) {
mongoTemplate.upsert(new Query(Criteria.where(ID).is(t.getId())), Update.fromDocument(document), getCollections(collectionNames)[0]);
}
}
@Override
public void updateOneByIdWithField(String id, Update update, String... collectionNames) {
mongoTemplate.upsert(new Query(Criteria.where(ID).is(id)), update, getCollections(collectionNames)[0]);
}
@Override
public void deleteOneById(String id, String... collectionNames) {
mongoTemplate.remove(new Query(Criteria.where(ID).is(id)), getCollections(collectionNames)[0]);
}
@Override
public T findOneById(String id, String... collectionNames) {
return mongoTemplate.findOne(new Query(Criteria.where(ID).is(id)), clazz, getCollections(collectionNames)[0]);
}
@Override
public T findOne(String key, Object value, String... collectionNames) {
return mongoTemplate.findOne(new Query(Criteria.where(key).is(value)), clazz, getCollections(collectionNames)[0]);
}
@Override
public List<T> findListByKeywordFuzz(String keyword, String[] fuzzKeys, String... collectionNames) {
return findListByKeywordFuzz(null, keyword, fuzzKeys, collectionNames);
}
@Override
public List<T> findListByKeywordFuzz(Query preQuery, String keyword, String[] fuzzKeys, String... collectionNames) {
String[] collections = getCollections(collectionNames);
Query query = new Query();
if (null != preQuery) {
query = preQuery;
}
List<T> res = new ArrayList<>();
if (StringUtils.isEmpty(keyword) || fuzzKeys.length == 0) {
for (String collection : collections) {
res.addAll(mongoTemplate.find(query, clazz, collection));
}
return res;
}
addKeywordFuzz(query, keyword, fuzzKeys);
for (String collection : collections) {
res.addAll(mongoTemplate.find(query, clazz, collection));
}
return res;
}
@Override
public List<T> findList(Query preQuery, String... collectionNames) {
return findListByKeywordFuzz(preQuery, null, null, collectionNames);
}
@Override
public long count(Query preQuery, String... collectionNames) {
Query query = new Query();
if (null != preQuery) {
query = preQuery;
}
long count = 0;
for (String collection : getCollections(collectionNames)) {
count += mongoTemplate.count(query, collection);
}
return count;
}
@Override
public void addKeywordFuzz(Query query, String keyword, String... fuzzKeys) {
if (StringUtils.isEmpty(keyword)) {
return;
}
Pattern pattern = Pattern.compile("^.*" + keyword + ".*$", Pattern.CASE_INSENSITIVE);
Criteria regex = Criteria.where(fuzzKeys[0]).regex(pattern);
for (int i = 1; i < fuzzKeys.length; i++) {
// 多字段模糊查询
regex.orOperator(Criteria.where(fuzzKeys[i]).regex(pattern));
}
query.addCriteria(regex);
}
@Override
public void addSort(Query query, String sorter) {
if (StringUtils.isEmpty(sorter)) {
return;
}
for (Map.Entry<String, Object> entry : JSONObject.parseObject(sorter).entrySet()) {
if (entry.getValue().toString().contains("desc")) {
query.with(Sort.by(Sort.Order.desc(entry.getKey())));
} else {
query.with(Sort.by(Sort.Order.asc(entry.getKey())));
}
}
}
private String[] getCollections(String... collectionNames) {
// 优先返回预设集合名
if (null != collectionName) {
return new String[]{collectionName};
}
if (null != collectionNames && collectionNames.length != 0) {
return collectionNames;
}
throw new IllegalArgumentException("collection can not be null");
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IBehaviorDao;
import com.zhiwei.brandkbs2.pojo.Behavior;
import org.springframework.stereotype.Component;
/**
* @ClassName: BehaviorDaoImpl
* @Description BehaviorDaoImpl
* @author: sjj
* @date: 2022-05-27 13:48
*/
@Component("behaviorDao")
public class BehaviorDaoImpl extends BaseMongoDaoImpl<Behavior> implements IBehaviorDao {
private static final String COLLECTION_PREFIX = "brandkbs_behavior";
private static final String TIME_PATTERN = "yyyyMM";
public BehaviorDaoImpl() {
super(null);
}
@Override
public String collectionPrefix() {
return COLLECTION_PREFIX;
}
@Override
public String timePattern() {
return TIME_PATTERN;
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IChannelDao;
import com.zhiwei.brandkbs2.pojo.Channel;
import com.zhiwei.brandkbs2.pojo.ChannelIndex;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
/**
* @ClassName: ChannelDaoImpl
* @Description ChannelDaoImpl
* @author: sjj
* @date: 2022-06-16 15:31
*/
@Component("channelDao")
public class ChannelDaoImpl extends BaseMongoDaoImpl<Channel> implements IChannelDao {
private static final String COLLECTION_PREFIX = "brandkbs_channel";
public ChannelDaoImpl() {
super(COLLECTION_PREFIX);
}
@Override
public Channel queryUnique(ChannelIndex channelIndex) {
Query query = Query.query(Criteria.where("linkedGroupId").is(channelIndex.getLinkedGroupId()).and("platform").is(channelIndex.getPlatform()).and(
"realSource").is(channelIndex.getRealSource()).and("source").is(channelIndex.getSource()));
return mongoTemplate.findOne(query, clazz);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.dao.IChannelLabelDao;
import com.zhiwei.brandkbs2.pojo.ChannelLabel;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName: ChannelLabelDaoImpl
* @Description ChannelLabelDaoImpl
* @author: sjj
* @date: 2022-06-20 16:56
*/
@Component("channelLabelDao")
public class ChannelLabelDaoImpl extends BaseMongoDaoImpl<ChannelLabel> implements IChannelLabelDao {
private static final String COLLECTION_NAME = "brandkbs_channel_label";
public ChannelLabelDaoImpl() {
super(COLLECTION_NAME);
}
@Override
public List<String> getChannelLabelType() {
// 分组
Aggregation agg = Aggregation.newAggregation(Aggregation.group("type"));
AggregationResults<JSONObject> aggregate = mongoTemplate.aggregate(agg, COLLECTION_NAME, JSONObject.class);
List<JSONObject> mappedResults = aggregate.getMappedResults();
return mappedResults.stream().map(json -> json.getString("_id")).collect(Collectors.toList());
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IEventDao;
import com.zhiwei.brandkbs2.pojo.Event;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
/**
* @ClassName: EventDaoImpl
* @Description 事件业务实现类
* @author: sjj
* @date: 2022-05-18 14:45
*/
@Component("eventDao")
public class EventDaoImpl extends BaseMongoDaoImpl<Event> implements IEventDao {
private static final String COLLECTION_NAME = "brandkbs_event";
public EventDaoImpl() {
super(COLLECTION_NAME);
}
@Override
public boolean existEventByUniqueIds(String yqEventId, String projectId, String linkedGroupId) {
return null != getEventByUniqueIds(yqEventId, projectId, linkedGroupId);
}
@Override
public Event getEventByUniqueIds(String yqEventId, String projectId, String linkedGroupId) {
Query query = new Query();
query.addCriteria(Criteria.where("yqEventId").is(yqEventId).and("projectId").is(projectId).and("linkedGroupId").is(linkedGroupId));
return mongoTemplate.findOne(query, Event.class);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.dao.IEventDataDao;
import com.zhiwei.brandkbs2.pojo.ChannelIndex;
import com.zhiwei.brandkbs2.pojo.Event;
import com.zhiwei.brandkbs2.pojo.EventData;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName: EventDataDaoImpl
* @Description 事件数据业务实现类
* @author: sjj
* @date: 2022-05-19 17:42
*/
@Component("eventDataDao")
public class EventDataDaoImpl extends BaseMongoDaoImpl<EventData> implements IEventDataDao {
private static final String COLLECTION_PREFIX = "brandkbs_event_data";
private static final String TIME_PATTERN = "yyyy";
public EventDataDaoImpl() {
super(null);
}
@Override
public String collectionPrefix() {
return COLLECTION_PREFIX;
}
@Override
public String timePattern() {
return TIME_PATTERN;
}
@Override
public EventData findFirstData(String eventId, String collectionName) {
Query query = new Query();
query.addCriteria(Criteria.where("eventId").is(eventId));
query.with(Sort.by(Sort.Order.asc("time")));
query.limit(1);
return mongoTemplate.findOne(query, clazz, collectionName);
}
@Override
public List<String> getEventCount(ChannelIndex channelIndex) {
// 添加渠道唯一标识
Criteria criteria = addChannelIndex(channelIndex);
// 分组
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), Aggregation.group("eventId").count().as("eventCount"));
AggregationResults<JSONObject> aggregate = mongoTemplate.aggregate(agg, "brandkbs_event_data_2022", JSONObject.class);
List<JSONObject> mappedResults = aggregate.getMappedResults();
return mappedResults.stream().map(json -> json.getString("_id")).collect(Collectors.toList());
}
@Override
public long getEventArticleCount(Event event) {
return count(Query.query(Criteria.where("eventId").is(event.getId())), event.getCollectionName());
}
@Override
public long getEventArticleWithChannelCount(Event event, ChannelIndex channelIndex) {
return count(Query.query(Criteria.where("eventId").is(event.getId()).and("source").is(channelIndex.getSource()).and("realSource").is(channelIndex.getRealSource()).and("platform").is(channelIndex.getPlatform())), event.getCollectionName());
}
@Override
public void deleteByEventId(String eventId, String collectionName) {
mongoTemplate.remove(new Query(Criteria.where("eventId").is(eventId)), collectionName);
}
@Override
public List<EventData> findEventDataListByTime(String eventId, Date startTime, Date endTime, String collectionName) {
Query query = Query.query(Criteria.where("eventId").is(eventId));
query.addCriteria(Criteria.where("time").gte(startTime).lt(endTime));
return mongoTemplate.find(query, EventData.class, collectionName);
}
@Override
public long findEventDataCount(String eventId, String collectionName) {
Query query = Query.query(Criteria.where("eventId").is(eventId));
return mongoTemplate.count(query, EventData.class, collectionName);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IHighWordDao;
import com.zhiwei.brandkbs2.pojo.HighWord;
import org.springframework.stereotype.Component;
/**
* @ClassName: HighWordDaoImpl
* @Description HighWordDaoImpl
* @author: sjj
* @date: 2022-06-06 09:48
*/
@Component("highWordDao")
public class HighWordDaoImpl extends BaseMongoDaoImpl<HighWord> implements IHighWordDao {
private static final String COLLECTION_NAME = "brandkbs_high_word";
public HighWordDaoImpl() {
super(COLLECTION_NAME);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IProjectDao;
import com.zhiwei.brandkbs2.pojo.Project;
import org.springframework.stereotype.Component;
/**
* @ClassName: ProjectDaoImlp
* @Description 项目业务接口实现类
* @author: sjj
* @date: 2022-04-21 13:51
*/
@Component("projectDao")
public class ProjectDaoImlp extends BaseMongoDaoImpl<Project> implements IProjectDao {
private static final String COLLECTION_NAME = "brandkbs_project";
public ProjectDaoImlp() {
super(COLLECTION_NAME);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IQbjcPojoDao;
import com.zhiwei.qbjc.bean.pojo.common.MessagePlatform;
import com.zhiwei.qbjc.bean.pojo.common.Tag;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* @ClassName: QbjcPojoDaoImpl
* @Description QbjcPojoDaoImpl
* @author: sjj
* @date: 2022-06-16 14:24
*/
@Component("qbjcPojoDao")
public class QbjcPojoDao implements IQbjcPojoDao {
@Resource(name = "secondaryMongoTemplate")
protected MongoTemplate mongoTemplate;
@Override
public List<MessagePlatform> findMessagePlatformAll() {
return mongoTemplate.find(new Query(), MessagePlatform.class);
}
@Override
public List<Tag> findTagAll() {
return mongoTemplate.find(new Query(), Tag.class);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IReportDao;
import com.zhiwei.brandkbs2.pojo.Report;
import org.springframework.stereotype.Component;
/**
* @ClassName: ReportDaoImpl
* @Description ReportDaoImpl
* @author: sjj
* @date: 2022-05-31 18:14
*/
@Component("reportDao")
public class ReportDaoImpl extends BaseMongoDaoImpl<Report> implements IReportDao {
private static final String COLLECTION_NAME = "brandkbs_report";
public ReportDaoImpl() {
super(COLLECTION_NAME);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IReportSettingsDao;
import com.zhiwei.brandkbs2.pojo.ReportSettings;
import org.springframework.stereotype.Component;
/**
* @ClassName: ReportSettingsDaoImpl
* @Description ReportSettingsDaoImpl
* @author: sjj
* @date: 2022-05-31 18:14
*/
@Component("reportSettingsDao")
public class ReportSettingsDaoImpl extends BaseMongoDaoImpl<ReportSettings> implements IReportSettingsDao {
private static final String COLLECTION_NAME = "brandkbs_report_settings";
public ReportSettingsDaoImpl() {
super(COLLECTION_NAME);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.ITagFilterDao;
import com.zhiwei.brandkbs2.pojo.TagFilter;
import org.springframework.stereotype.Component;
/**
* @ClassName: TagFilterDaoImpl
* @Description TagFilterDaoImpl
* @author: sjj
* @date: 2022-06-01 14:26
*/
@Component("tagFilterDao")
public class TagFilterDaoImpl extends BaseMongoDaoImpl<TagFilter> implements ITagFilterDao {
private static final String COLLECTION_NAME = "brandkbs_tag_filter";
public TagFilterDaoImpl() {
super(COLLECTION_NAME);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.dao.IUserDao;
import com.zhiwei.brandkbs2.pojo.User;
import org.springframework.stereotype.Component;
/**
* @ClassName: UserDao
* @Description 用户相关实现类
* @author: sjj
* @date: 2022-04-28 18:10
*/
@Component("userDao")
public class UserDaoImpl extends BaseMongoDaoImpl<User> implements IUserDao {
private static final String COLLECTION_NAME = "brandkbs_user";
public UserDaoImpl() {
super(COLLECTION_NAME);
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.pojo.AbstractBaseMongo;
import com.zhiwei.brandkbs2.util.Md5Util;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
/**
* @ClassName: UserOldDaoImpl
* @Description 用户旧表
* @author: sjj
* @date: 2022-05-09 16:20
*/
@Component("userOldDao")
public class UserOldDaoImpl extends BaseMongoDaoImpl<UserOldDaoImpl.UserOld> {
private static final String collectionName = "brandkbs_user_old";
public UserOldDaoImpl() {
super(collectionName);
}
public UserOldDaoImpl.UserOld findOneByUsernameAndPassword(String username, String password) {
Query query = new Query();
query.addCriteria(Criteria.where("username").is(username).and("password").is(Md5Util.encodeByMd5(password)));
return mongoTemplate.findOne(query, UserOld.class);
}
@Setter
@Getter
@Document("brandkbs_user_old")
public static class UserOld extends AbstractBaseMongo {
String nick;
String username;
String password;
boolean superAdmin;
boolean bindUser;
}
}
package com.zhiwei.brandkbs2.dao.impl;
import com.zhiwei.brandkbs2.pojo.AbstractBaseMongo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @ClassName: UserProjectOldImpl
* @Description 用户项目关联旧表
* @author: sjj
* @date: 2022-05-09 17:02
*/
@Component("userProjectOldDao")
public class UserProjectOldDaoImpl extends BaseMongoDaoImpl<UserProjectOldDaoImpl.UserProjectOld>{
private static final String collectionName = "brandkbs_user_project_old";
public UserProjectOldDaoImpl() {
super(collectionName);
}
@Setter
@Getter
@Document("brandkbs_user_project_old")
public static class UserProjectOld extends AbstractBaseMongo{
String userId;
Integer roleId;
Date expiredTime;
Integer exportAmount;
String projectName;
}
}
package com.zhiwei.brandkbs2.easyexcel;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.zhiwei.brandkbs2.easyexcel.config.ReadExcelDTO;
import com.zhiwei.brandkbs2.easyexcel.config.WriteExcelDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* @author sjj
* @version 1.0
* @description easyExcel解析工具类
* @date 2022年4月20日09:34:12
*/
@Slf4j
public class EasyExcelUtil {
private EasyExcelUtil() {
}
/**
* 读取单个sheet
*
* @param filePath 文件路径
* @param readExcelDTO 读excel封装类
*/
public static void read(String filePath, ReadExcelDTO readExcelDTO) {
try {
EasyExcel.read(filePath, readExcelDTO.getClazz(), readExcelDTO.getAnalysisEventListener()).sheet().doRead();
} catch (Exception e) {
log.error("file:{},analysis error:", filePath, e);
}
}
/**
* 读取单个sheet
*
* @param file 上传的文件
* @param readExcelDTO 读excel封装类
*/
public static void read(MultipartFile file, ReadExcelDTO readExcelDTO) {
try {
EasyExcel.read(file.getInputStream(), readExcelDTO.getClazz(), readExcelDTO.getAnalysisEventListener()).sheet().doRead();
} catch (Exception e) {
log.error("file:{},analysis error:", file.getName(), e);
}
}
/**
* 读取多个sheet
*
* @param filePath 文件路径
* @param dataList 读excel封装类集合
*/
public static void read(String filePath, List<ReadExcelDTO<Object>> dataList) {
try {
ExcelReader excelReader = EasyExcel.read(filePath).build();
List<ReadSheet> readSheets = new ArrayList<>(dataList.size());
dataList.forEach(readExcelDTO -> {
ExcelReaderSheetBuilder sheetBuilder;
//优先根据sheet名称获取表格内容
if (StringUtils.isEmpty(readExcelDTO.getSheetName())) {
sheetBuilder = EasyExcel.readSheet(readExcelDTO.getSheetIndex());
} else {
sheetBuilder = EasyExcel.readSheet(readExcelDTO.getSheetName());
}
ReadSheet readSheet = sheetBuilder.head(readExcelDTO.getClazz())
.registerReadListener(readExcelDTO.getAnalysisEventListener()).build();
readSheets.add(readSheet);
});
excelReader.read(readSheets);
excelReader.finish();
} catch (Exception e) {
log.error("file:{},analysis error:", filePath, e);
}
}
/**
* 写单个sheet
*
* @param filePath 文件路径
* @param sheetName sheet名称
* @param clazz 字节码对象
* @param datas 返回数据
* @param <T> 指定写的类型
*/
public static <T> void write(String filePath, String sheetName, Class<T> clazz, List<T> datas) {
try {
EasyExcel.write(filePath, clazz).sheet(sheetName).doWrite(datas);
} catch (Exception e) {
log.error("file:{},write error:", filePath, e);
}
}
/**
* 写多个sheet
*
* @param filePath 文件路径
* @param dataList sheet名称和数据封装对象
*/
public static void write(String filePath, List<WriteExcelDTO> dataList) {
try {
ExcelWriter excelWriter = EasyExcel.write(filePath).build();
int size = dataList.size();
for (int i = 0; i < size; i++) {
WriteExcelDTO writeExcel = dataList.get(i);
WriteSheet writeSheet = EasyExcel.writerSheet(i, writeExcel.getSheetName()).head(writeExcel.getClazz()).build();
excelWriter.write(writeExcel.getDatas(), writeSheet);
}
excelWriter.finish();
} catch (Exception e) {
log.error("file:{},write error:", filePath, e);
}
}
/**
* 动态表头写表格
*
* @param filePath 文件路径
* @param sheetName sheet名称
* @param head 表头
* @param datas 数据
*/
public static void dynamicHeadWrite(String filePath, String sheetName, List<List<String>> head, List<List<Object>> datas) {
try {
EasyExcel.write(filePath).head(head).sheet(sheetName).doWrite(datas);
} catch (Exception e) {
log.error("file:{},write error:", filePath, e);
}
}
/**
* 单sheet下载
*
* @param fileName 文件名
* @param sheetName sheet名称
* @param clazz 字节码对象
* @param datas 返回数据
* @param response http响应
* @param <T> 指定解析的类型
*/
public static <T> void download(String fileName, String sheetName, Class<T> clazz, List<T> datas, HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(datas);
} catch (Exception e) {
log.error("file:{},download error:", fileName, e);
}
}
/**
* 多sheet下载
*
* @param fileName 文件名
* @param dataList sheet名称和数据封装对象
* @param response http响应
*/
public static void download(String fileName, List<WriteExcelDTO<Object>> dataList, HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
for (int i = 0; i < dataList.size(); i++) {
WriteExcelDTO<Object> writeExcel = dataList.get(i);
WriteSheet writeSheet = EasyExcel.writerSheet(i, writeExcel.getSheetName()).head(writeExcel.getClazz()).build();
excelWriter.write(writeExcel.getDatas(), writeSheet);
}
excelWriter.finish();
} catch (Exception e) {
log.error("file:{},download error:", fileName, e);
}
}
}
package com.zhiwei.brandkbs2.easyexcel.config;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.ToString;
/**
* @author sjj
* @version 1.0
* @description 读excel配置类
* @date 2022年4月20日09:36:09
*/
@Data
@ToString
public class ReadExcelDTO<T> {
/**
* sheet索引
*/
private Integer sheetIndex;
/**
* sheet名称
*/
private String sheetName;
/**
* 类字节码
*/
private Class<T> clazz;
/**
* 解析监听器
*/
private AnalysisEventListener<T> analysisEventListener;
}
package com.zhiwei.brandkbs2.easyexcel.config;
import lombok.Data;
import lombok.ToString;
import java.util.List;
/**
* @author sjj
* @version 1.0
* @description 写excel配置类
* @date 2022年4月20日09:39:28
*/
@Data
@ToString
public class WriteExcelDTO<T> {
/**
* sheet名称
*/
private String sheetName;
/**
* 字节码对象
*/
private Class<T> clazz;
/**
* sheet数据
*/
private List<T> datas;
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
* @author sjj
* @version 1.0
* @description 后台导出渠道稿件实体类
* @date 2022年6月20日10:48:31
*/
@Data
@ToString
public class ExportAdminChannelArticleDTO {
@ExcelProperty("发布时间")
private Date time;
@ExcelProperty("链接")
private String url;
@ExcelProperty("标题")
private String title;
@ExcelProperty("平台")
private String platform;
@ExcelProperty("来源")
private String realSource;
@ExcelProperty("渠道")
private String source;
@ExcelProperty("是否首发")
private String first;
@ExcelProperty("情感倾向")
private String emotion;
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
* @author sjj
* @version 1.0
* @description 导出渠道事件实体类
* @date 2022年6月20日14:07:06
*/
@Data
@ToString
public class ExportAdminChannelEventDTO {
@ExcelProperty("开始时间")
private Date startTime;
@ExcelProperty("结束时间")
private Date endTime;
@ExcelProperty("事件名")
private String title;
@ExcelProperty("首发平台")
private String firstPlatform;
@ExcelProperty("首发来源")
private String firstRealSource;
@ExcelProperty("首发渠道")
private String firstSource;
@ExcelProperty("情感倾向")
private String emotion;
@ExcelProperty("事件类型")
private String eventType;
@ExcelProperty("传播量")
private Long eventArticleCount;
@ExcelProperty("渠道传播量")
private Long channelArticleCount;
@ExcelProperty("影响力")
private Double influence;
@ExcelProperty("关键词")
private String keyword;
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
* @author lxj
* @version 1.0
* @description 导出用户行为实体类
* @date 2019/11/12 15:14
*/
@Data
@ToString
public class ExportBehaviorDTO {
@ExcelProperty("用户名")
private String nickname;
@ExcelProperty("IP")
private String ip;
@ExcelProperty("操作时间")
private Date time;
@ExcelProperty("访问页面")
private String page;
@ExcelProperty("操作模块")
private String module;
@ExcelProperty("用户身份")
private String role;
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.zhiwei.brandkbs2.pojo.Channel;
import com.zhiwei.brandkbs2.util.Tools;
import lombok.Data;
/**
* @ClassName: ExportChannelDTO
* @Description 导出渠道实体
* @author: sjj
* @date: 2022-06-17 13:44
*/
@Data
public class ExportChannelDTO {
@ExcelProperty("平台")
private String platform;
@ExcelProperty("来源")
private String realSource;
@ExcelProperty("渠道名")
private String source;
@ExcelProperty("稿件数")
private Integer articleCount;
@ExcelProperty("事件数")
private Integer eventCount;
@ExcelProperty("渠道倾向")
private String emotion;
@ExcelProperty("渠道指数")
private Double emotionIndex;
@ExcelProperty("经验评级")
private String experienceLevel;
public static ExportChannelDTO createFromChannel(Channel channel) {
return Tools.convertMap(channel, ExportChannelDTO.class);
}
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
* @author lxj
* @version 1.0
* @description 导出事件实体类
* @date 2019/11/12 14:04
*/
@Data
@ToString
public class ExportEventDTO {
@ExcelProperty("开始时间")
private Date startTime;
@ExcelProperty("结束时间")
private Date endTime;
@ExcelProperty("事件名")
private String title;
@ExcelProperty("首发平台")
private String firstPlatform;
@ExcelProperty("首发来源")
private String firstRealSource;
@ExcelProperty("首发渠道")
private String firstSource;
@ExcelProperty("情感倾向")
private String emotion;
@ExcelProperty("事件类型")
private String eventType;
@ExcelProperty("传播量")
private Long articleCount;
@ExcelProperty("影响力")
private Double influence;
@ExcelProperty("关键词")
private String keyword;
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
* @author sjj
* @version 1.0
* @description 导出事件数据实体类
* @date 2022年5月25日15:13:08
*/
@Data
@ToString
public class ExportEventDataDTO {
@ExcelProperty("时间")
private Date time;
@ExcelProperty("链接")
private String url;
@ExcelProperty("稿件名")
private String title;
@ExcelProperty("平台")
private String platform;
@ExcelProperty("来源")
private String realSource;
@ExcelProperty("渠道名")
private String source;
@ExcelProperty("是否首发")
private String first;
@ExcelProperty("情感倾向")
private String emotion;
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.zhiwei.brandkbs2.pojo.HighWord;
import com.zhiwei.brandkbs2.util.Tools;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
* @ClassName: ExportHighWordDTO
* @Description 高频词导出传输实体
* @author: sjj
* @date: 2022-06-06 17:16
*/
@Data
@ToString
public class ExportHighWordDTO {
/**
* 名称
*/
@ExcelProperty("关键词")
private String keyword;
/**
* 创建时间
*/
@ExcelProperty("创建时间")
private Date cTime;
/**
* 上传人
*/
@ExcelProperty("上传人")
private String submitter;
public static ExportHighWordDTO createFromHighWord(HighWord highWord){
return Tools.convertMap(highWord,ExportHighWordDTO.class);
}
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
* @author lxj
* @version 1.0
* @description 解析事件数据上传文件
* @date 2019/9/9 16:22
*/
@Data
@ToString
public class UploadEventDTO {
@ExcelProperty("开始时间")
private Date startTime;
@ExcelProperty("结束时间")
private Date endTime;
@ExcelProperty("标题")
private String title;
@ExcelProperty("关键词")
private String keyword;
@ExcelProperty("情感倾向")
private String emotion;
@ExcelProperty("事件类型")
private String eventType;
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.common.GenericAttribute;
import com.zhiwei.brandkbs2.pojo.Event;
import com.zhiwei.brandkbs2.pojo.EventData;
import com.zhiwei.brandkbs2.util.Tools;
import lombok.Data;
import lombok.ToString;
import org.apache.logging.log4j.util.Strings;
import java.util.Date;
import java.util.Optional;
/**
* @ClassName: UploadEventDataDTO
* @Description 事件数据导入传输类
* @author: sjj
* @date: 2022-05-20 14:07
*/
@Data
@ToString
public class UploadEventDataDTO {
@ExcelProperty("事件id")
private String eventId;
@ExcelProperty("时间")
private Date time;
@ExcelProperty("标题")
private String title;
@ExcelProperty("文本")
private String content;
@ExcelProperty("地址")
private String url;
@ExcelProperty("平台")
private String platform;
/**
* 第二级平台
*/
@ExcelProperty("来源")
private String realSource;
@ExcelProperty("渠道")
private String source;
@ExcelProperty("情感倾向")
private String emotion;
@ExcelProperty("品牌归属")
private String brand;
@ExcelProperty("专列事件")
private String eventTitle;
@ExcelProperty("原创/转发(微博平台)")
private boolean isForward;
/**
* 表格解析后的数据转换为事件数据传输格式
*
* @return 事件数据传输格式
*/
public EventData createEventData(Event event) {
EventData eventData = new EventData();
eventData.setPlatform(this.getPlatform());
eventData.setRealSource(this.getRealSource());
eventData.setSource(this.getSource());
eventData.setUrl(this.getUrl());
eventData.setTime(this.getTime());
//如果标题不为空,取前64个字符作为标题,如果为空,取内容的前32位作为标题
eventData.setTitle(Strings.isNotBlank(title) ? (title.length() > 64 ? title.substring(0, 64) : title) : content.length() > 32 ? content.substring(0, 32) : content);
eventData.setAggTitle(Tools.filterSpecialCharacter(title));
eventData.setContent(content);
eventData.setTime(this.getTime());
JSONObject tagInfo = new JSONObject();
Optional.ofNullable(this.getEmotion()).ifPresent(emotion -> tagInfo.put(GenericAttribute.EMOTION_LABEL_KEY, emotion));
Optional.ofNullable(this.getBrand()).ifPresent(brand -> tagInfo.put(GenericAttribute.BRAND_LABEL_KEY, brand));
eventData.setTagInfo(JSON.toJSONString(tagInfo));
eventData.setEmotion(this.getEmotion());
eventData.setForward(this.isForward);
eventData.setEventId(event.getId());
eventData.setProjectId(event.getProjectId());
eventData.setLinkedGroupId(event.getLinkedGroupId());
eventData.setCTime(new Date());
// TODO Type字段类型是否需要???
return eventData;
}
}
package com.zhiwei.brandkbs2.easyexcel.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.collections4.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author sjj
* @version 1.0
* @description 解析关键词上传文件
* @date 2022年4月20日09:57:21
*/
@Data
@ToString
public class UploadKeywordDTO {
@ExcelProperty("关键词")
private String keyword;
public static List<UploadKeywordDTO> change2This(List<String> list) {
if (CollectionUtils.isEmpty(list)) {
return Collections.emptyList();
} else {
return list.stream().map(str -> {
UploadKeywordDTO dto = new UploadKeywordDTO();
dto.setKeyword(str);
return dto;
}).collect(Collectors.toList());
}
}
}
package com.zhiwei.brandkbs2.easyexcel.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhiwei.brandkbs2.dao.IEventDao;
import com.zhiwei.brandkbs2.dao.IEventDataDao;
import com.zhiwei.brandkbs2.easyexcel.dto.UploadEventDataDTO;
import com.zhiwei.brandkbs2.pojo.Event;
import com.zhiwei.brandkbs2.pojo.EventData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
/**
* @ClassName: EventDataListener
* @Description 事件数据上传监听类
* @author: sjj
* @date: 2022-05-23 13:54
*/
@Slf4j
public class EventDataListener extends AnalysisEventListener<UploadEventDataDTO> {
private final IEventDao eventDao;
private final IEventDataDao eventDataDao;
private final StringRedisTemplate stringRedisTemplate;
private final String linkedGroupId;
private final String redisKey;
public EventDataListener(IEventDao eventDao, IEventDataDao eventDataDao, StringRedisTemplate stringRedisTemplate, String linkedGroupId,
String redisKey) {
this.eventDao = eventDao;
this.eventDataDao = eventDataDao;
this.stringRedisTemplate = stringRedisTemplate;
this.linkedGroupId = linkedGroupId;
this.redisKey = redisKey;
}
@Override
public void invoke(UploadEventDataDTO eventDataDTO, AnalysisContext analysisContext) {
Event event = eventDao.findOneById(eventDataDTO.getEventId());
// 不允许上传其他关联组数据
if (event.getLinkedGroupId().equals(linkedGroupId)) {
EventData eventData = eventDataDTO.createEventData(event);
eventDataDao.insertOneWithoutId(eventData);
}
int progress = analysisContext.readRowHolder().getRowIndex() * 100 / (analysisContext.readSheetHolder().getApproximateTotalRowNumber() - 1);
stringRedisTemplate.opsForValue().set(redisKey, String.valueOf(progress));
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("redisKey:{},事件数据上传完成,共上传:{}条数据", redisKey, analysisContext.readRowHolder().getRowIndex());
}
}
package com.zhiwei.brandkbs2.easyexcel.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhiwei.brandkbs2.easyexcel.dto.UploadEventDTO;
import com.zhiwei.brandkbs2.service.IEventService;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
/**
* @author lxj
* @version 1.0
* @description 解析事件文件监听器
* @date 2019/11/12 9:00
*/
@Slf4j
public class EventFileListener extends AnalysisEventListener<UploadEventDTO> {
/**
* 每隔5条执行保存更新事件信息,然后清理list,方便内存回收
*/
private static final int BATCH_COUNT = 5;
/**
* 储存解析数据集合
*/
private final List<UploadEventDTO> datas = new ArrayList<>(BATCH_COUNT);
private final IEventService iEventService;
private final String projectId;
private final String linkedGroupId;
public EventFileListener(IEventService iEventService, String projectId, String linkedGroupId) {
this.iEventService = iEventService;
this.projectId = projectId;
this.linkedGroupId = linkedGroupId;
}
/**
* 每一条数据解析都会来调用
*
* @param data one row value.is same as {@link AnalysisContext#readRowHolder()}
* @param context 解析上下文
*/
@Override
public void invoke(UploadEventDTO data, AnalysisContext context) {
datas.add(data);
if (datas.size() >= BATCH_COUNT) {
addFileEvent();
datas.clear();
}
}
/**
* 所有数据解析完成了 都会来调用
*
* @param context 解析上下文
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
addFileEvent();
log.info("事件文件解析完成,此次更新:{} 条数据", context.readRowHolder().getRowIndex());
}
/**
* 保存更新事件信息逻辑
*/
private void addFileEvent() {
datas.forEach(eventDTO -> iEventService.addFileEvent(projectId, linkedGroupId, eventDTO));
}
}
package com.zhiwei.brandkbs2.enmus;
import java.util.Objects;
/**
* @author lxj
* @version 1.0
* @description 情绪枚举类
* @date 2019/8/29 9:52
*/
public enum EmotionEnum {
/**
* 全部
*/
ALL(-1),
/**
* 未定义
*/
UNDEFINED(0),
/**
* 正面的
*/
POSITIVE(1),
/**
* 中性的
*/
NEUTRAL(2),
/**
* 负面的
*/
NEGATIVE(3);
private final int state;
public static final String EMOTION_LABEL_KEY = "情感倾向";
public static final String POSITIVE_LABEL = "正面";
public static final String NEUTRAL_LABEL = "中性";
public static final String NEGATIVE_LABEL = "负面";
public static final String SENSITIVE_LABEL = "敏感";
public static final String SPECIAL_PR_LABEL = "PR";
public static final String SPECIAL_TS_LABEL = "投诉";
public static final String SPECIAL_NEGATIVE_LABEL = "关联负面";
EmotionEnum(int state) {
this.state = state;
}
public int getState() {
return state;
}
/**
* 获取事件数据情感倾向
*
* @param emotion 情感倾向
* @return 情感对应的数值
*/
public static int parseEventDataEmotion(String emotion) {
int value;
//防止报空指针异常
if (Objects.isNull(emotion)) {
value = EmotionEnum.UNDEFINED.getState();
} else {
switch (emotion) {
case SPECIAL_PR_LABEL:
case POSITIVE_LABEL:
value = EmotionEnum.POSITIVE.getState();
break;
case NEUTRAL_LABEL:
value = EmotionEnum.NEUTRAL.getState();
break;
case NEGATIVE_LABEL:
case SENSITIVE_LABEL:
case SPECIAL_TS_LABEL:
case SPECIAL_NEGATIVE_LABEL:
value = EmotionEnum.NEGATIVE.getState();
break;
default:
value = EmotionEnum.UNDEFINED.getState();
}
}
return value;
}
/**
* 获取事件情感倾向
*
* @param value 情感倾向
* @return 情感对应的数值
*/
public static int parseEventEmotion(String value) {
int emotion = EmotionEnum.NEUTRAL.getState();
if (Objects.nonNull(value)) {
switch (value) {
case "正面":
emotion = EmotionEnum.POSITIVE.getState();
break;
case "负面":
emotion = EmotionEnum.NEGATIVE.getState();
break;
case "中性":
default:
break;
}
}
return emotion;
}
}
package com.zhiwei.brandkbs2.enmus;
import com.alibaba.fastjson.JSONObject;
/**
* @ClassName: EventTag
* @Description 事件标签枚举类
* @author: sjj
* @date: 2022-05-26 15:51
*/
public enum EventTagEnum{
EVENT_ATTRIBUTE("事件属性"),
EVENT_TYPE("事件类型");
private final String name;
EventTagEnum(String name){
this.name = name;
}
public String getName() {
return name;
}
public static JSONObject eventType(String value){
JSONObject json = new JSONObject();
json.put(EventTagEnum.EVENT_TYPE.getName(), value);
return json;
}
}
package com.zhiwei.brandkbs2.enmus;
/**
* @ClassName: ExperienceEnum
* @Description 经验判断枚举类
* @author: sjj
* @date: 2022-06-17 14:39
*/
public enum ExperienceEnum {
FRIENDLY_ONE("友好1级","friendlyOne"),
FRIENDLY_TWO("友好2级","friendlyTwo"),
FRIENDLY_THREE("友好3级","friendlyThree"),
UNFRIENDLY_ONE("不友好1级","unfriendlyOne"),
UNFRIENDLY_TWO("不友好2级","unfriendlyTwo"),
UNFRIENDLY_THREE("不友好3级","unfriendlyThree"),
NEUTRAL("中性渠道","neutral");
private final String value;
private final String databaseName;
ExperienceEnum(String value,String databaseName) {
this.value = value;
this.databaseName = databaseName;
}
public String getValue() {
return this.value;
}
public String getDatabaseName(){
return this.databaseName;
}
public static String getDatabaseNameFromValue(String value) {
for (ExperienceEnum experienceEnum : ExperienceEnum.values()) {
if (experienceEnum.value.equals(value)) {
return experienceEnum.databaseName;
}
}
return null;
}
public static String getValueFromDataBaseName(String databaseName) {
for (ExperienceEnum experienceEnum : ExperienceEnum.values()) {
if (experienceEnum.databaseName.equals(databaseName)) {
return experienceEnum.value;
}
}
return null;
}
}
package com.zhiwei.brandkbs2.enmus;
/**
* @author sjj
* @version 1.0
* @description 用户角色枚举类
* @date 2022年4月18日10:56:45
*/
public enum RoleEnum {
/**
* 超级管理员
*/
SUPER_ADMIN(1, "超级管理员"),
/**
* 管理员
*/
ADMIN(2, "管理员"),
/**
* 维护人员
*/
COMMON_ADMIN(3, "维护人员"),
/**
* 客户
*/
CUSTOMER(4, "客户");
private final int state;
private final String name;
RoleEnum(int state, String name) {
this.state = state;
this.name = name;
}
public int getState() {
return state;
}
public static String getNameByState(int state) {
for (RoleEnum roleEnum : RoleEnum.values()) {
if (roleEnum.state == state) {
return roleEnum.name;
}
}
return "未知";
}
}
package com.zhiwei.brandkbs2.enmus.response;
import com.zhiwei.brandkbs2.model.ResultCode;
/**
* @author sjj
* @version 1.0
* @description 项目错误状态码及信息
* @date 2022年4月20日16:57:49
*/
public enum ProjectCodeEnum implements ResultCode {
/**
* 项目名已存在
*/
PROJECT_EXISTSNAME_ERROR(false, 1201, "项目名已存在!", 200),
/**
* 渠道计算比例错误
*/
PROJECT_CHANNEL_PROPORTION_ERROR(false, 1203, "渠道计算比例错误,请重新调整", 200),
/**
* 无法删除正在登录的项目
*/
PROJECT_DELETE_ERROR(false, 1204, "无法删除正在登录的项目", 200);
/**
* 操作是否成功
*/
final boolean success;
/**
* 操作代码
*/
final int code;
/**
* 提示信息
*/
final String message;
/**
* 聚合状态码
*/
final int aggCode;
ProjectCodeEnum(boolean success, int code, String message, int aggCode) {
this.success = success;
this.code = code;
this.message = message;
this.aggCode = aggCode;
}
@Override
public boolean success() {
return success;
}
@Override
public int code() {
return code;
}
@Override
public String message() {
return message;
}
@Override
public int aggCode() {
return aggCode;
}
}
package com.zhiwei.brandkbs2.es;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.common.GenericAttribute;
import com.zhiwei.brandkbs2.pojo.ChannelIndex;
import com.zhiwei.brandkbs2.pojo.Event;
import com.zhiwei.brandkbs2.util.Tools;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.ClearScrollRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
* @ClassName: EsClientDao
* @Description EsClientDao
* @author: sjj
* @date: 2022-06-10 14:38
*/
@Component("esClientDao")
public class EsClientDao {
private static final Logger log = LogManager.getLogger(EsClientDao.class);
private static final FastDateFormat DF = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
private static final String[] CHANNEL_RECORD_FETCH_SOURCE = new String[]{"c5", "foreign", "real_source", "source", "mtime", "time", "brandkbs_cache_maps"};
private static final String[] EVENT_FETCH_SOURCE = new String[]{"ind_full_text", "c5", "real_source", "source", "mtime", "time", "url", "mtag"};
private static final Long ONE_HOUR = 60 * 60 * 1000L;
// 滚动查询超时时间
private static final TimeValue TIME_VALUE = TimeValue.timeValueMinutes(8);
@Value("${test}")
private boolean test;
@Resource(name = "esClient")
private RestHighLevelClient esClient;
@Resource(name = "esSearchExecutor")
private ThreadPoolTaskExecutor executor;
public Map<String, JSONObject> searchByIds(Collection<String> queryIds) throws IOException {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds(queryIds.toArray(new String[0]));
sourceBuilder.query(queryBuilder).size(queryIds.size());
SearchResponse searchResponse = esClient.search(new SearchRequest(getIndexes()).source(sourceBuilder), RequestOptions.DEFAULT);
return Arrays.stream(searchResponse.getHits().getHits()).collect(Collectors.toMap(SearchHit::getId, hit -> new JSONObject(hit.getSourceAsMap())));
}
/**
* 搜索近几天的数据
*
* @param day 几天前天数
* @return 渠道记录结果
*/
public Map<ChannelIndex, ChannelIndex.ChannelRecord> searchChannelRecordRecentDay(int day) {
Map<ChannelIndex, ChannelIndex.ChannelRecord> res = new HashMap<>();
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTime().getTime();
calendar.add(Calendar.DAY_OF_MONTH, -day);
long startTime = calendar.getTime().getTime();
List<Long[]> cutTimes = Tools.cutTimeRange(startTime, endTime, ONE_HOUR);
List<CompletableFuture<Map<ChannelIndex, ChannelIndex.ChannelRecord>>> futures = new ArrayList<>(cutTimes.size());
cutTimes.forEach(times -> futures.add(CompletableFuture.supplyAsync(() -> searchChannelRecord(times[0], times[1]), executor)));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).whenComplete((r, e) -> {
futures.forEach(f -> {
Map<ChannelIndex, ChannelIndex.ChannelRecord> channelIndexMap = f.join();
channelIndexMap.forEach((channelIndex, record) -> {
res.compute(channelIndex, (k, v) -> {
if (null == v) {
return record;
}
return v.mergeRecord(record);
});
});
});
}).join();
return res;
}
public Map<ChannelIndex, ChannelIndex.ChannelRecord> searchChannelRecord(long startTime, long endTime) {
Map<ChannelIndex, ChannelIndex.ChannelRecord> res = new HashMap<>();
try {
QueryBuilder queryBuilder = QueryBuilders.rangeQuery("mtime").gte(startTime).lt(endTime);
List<Map<String, Object>> results = searchScroll(queryBuilder, 10000, CHANNEL_RECORD_FETCH_SOURCE);
for (Map<String, Object> result : results) {
for (ChannelIndex channelIndex : ChannelIndex.getChannelIndexes(result)) {
res.compute(channelIndex, (k, v) -> {
if (null == v) {
v = new ChannelIndex.ChannelRecord();
}
return v.mergeRecord(new ChannelIndex.ChannelRecord(new Date((long) result.get(GenericAttribute.ES_TIME)), String.valueOf(result.get(
"id"))));
});
}
}
} catch (IOException e) {
log.error("searchChannelRecord-", e);
}
log.info("startTime:{},endTime:{},size:{}", DF.format(startTime), DF.format(endTime), res.size());
return res;
}
/**
* 搜索符合事件数据
*
* @param event
* @return
*/
public List<Map<String, Object>> searchByEvent(Event event) {
try {
RangeQueryBuilder timeBuilder = QueryBuilders.rangeQuery("time").gte(event.getStartTime());
if (event.isEndStatus()) {
timeBuilder.lt(event.getEndTime());
}
return searchScroll(timeBuilder, 2000, EVENT_FETCH_SOURCE);
} catch (IOException e) {
log.error("searchByEvent-", e);
}
return Collections.emptyList();
}
/**
* 滚动查询
*
* @param queryBuilder 查询条件
* @param size 每次滚动查询的量
* @param fetchSource 包含的属性阈
* @throws IOException
*/
private List<Map<String, Object>> searchScroll(QueryBuilder queryBuilder, int size, String[] fetchSource) throws IOException {
List<Map<String, Object>> res = new ArrayList<>();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder).size(size)
.fetchSource(fetchSource, null);
SearchResponse searchResponse = esClient.search(
new SearchRequest(getIndexes()).source(sourceBuilder).scroll(TIME_VALUE),
RequestOptions.DEFAULT);
while (true) {
if (0 == searchResponse.getHits().getHits().length) {
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(searchResponse.getScrollId());
esClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
break;
}
res.addAll(Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(Collectors.toList()));
SearchScrollRequest scrollRequest = new SearchScrollRequest(searchResponse.getScrollId());
scrollRequest.scroll(TIME_VALUE);
searchResponse = esClient.scroll(scrollRequest, RequestOptions.DEFAULT);
}
return res;
}
private String[] getIndexes() {
return getIndexList().toArray(new String[0]);
}
private List<String> getIndexList() {
List<String> res = new ArrayList<>();
if (test) {
res.add(GenericAttribute.ES_INDEX_TEST);
return res;
}
// 近三年数据库
Calendar date = Calendar.getInstance();
int year = date.get(Calendar.YEAR);
res.add(GenericAttribute.ES_INDEX_PRE + year);
res.add(GenericAttribute.ES_INDEX_PRE + (year - 1));
res.add(GenericAttribute.ES_INDEX_PRE + (year - 2));
return res;
}
}
package com.zhiwei.brandkbs2.es;
import com.zhiwei.brandkbs2.config.EsProperties;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @ClassName: EsRestClient
* @Description es客户端
* @author: sjj
* @date: 2022-05-26 17:08
*/
@Configuration
public class EsRestClient {
private static final Logger log = LogManager.getLogger(EsRestClient.class);
private static final String COLON = ":";
private static final String COMMA = ",";
private final EsProperties esProperties;
public EsRestClient(EsProperties esProperties) {
this.esProperties = esProperties;
}
@Bean(name = "esClient")
public RestHighLevelClient esClient() {
List<HttpHost> httpHostList = new ArrayList<>();
try {
Assert.hasText(esProperties.getClusterNodes(), "Cluster nodes source must not be null or empty!");
String[] nodes = StringUtils.delimitedListToStringArray(esProperties.getClusterNodes(), COMMA);
Arrays.stream(nodes).forEach(node -> {
String[] segments = StringUtils.delimitedListToStringArray(node, COLON);
Assert.isTrue(segments.length == 2,
() -> String.format("Invalid cluster node %s in %s! Must be in the format host:port!", node,
esProperties.getClusterNodes()));
String host = segments[0].trim();
String port = segments[1].trim();
Assert.hasText(host, () -> String.format("No host name given cluster node %s!", node));
Assert.hasText(port, () -> String.format("No port given in cluster node %s!", node));
httpHostList.add(new HttpHost(host, Integer.parseInt(port)));
});
} catch (Exception e) {
log.error("es client初始化异常", e);
}
HttpHost[] httpHosts = httpHostList.toArray(new HttpHost[0]);
// 判断,如果未配置用户名,则进行无用户名密码连接,配置了用户名,则进行用户名密码连接
RestHighLevelClient client;
if (StringUtils.isEmpty(esProperties.getUsername())) {
client = new RestHighLevelClient(RestClient.builder(httpHosts));
} else {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
// es账号密码
new UsernamePasswordCredentials(esProperties.getUsername(), esProperties.getPassword()));
client = new RestHighLevelClient(
RestClient.builder(httpHosts).setHttpClientConfigCallback((httpClientBuilder) -> {
// 这里可以设置一些参数,比如cookie存储、代理等等
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}));
}
return client;
}
}
package com.zhiwei.brandkbs2.es;
/**
* @ClassName: ITaskService
* @Description 定时服务抽象类
* @author: sjj
* @date: 2022-06-16 15:27
*/
public interface ITaskService{
/**
* 渠道记录
*/
void channelRecordFromEs(int day);
}
package com.zhiwei.brandkbs2.exception;
import com.zhiwei.brandkbs2.model.ResultCode;
/**
* @author sjj
* @version 1.0
* @description 自定义异常类
* @date 2022年4月20日16:54:59
*/
public class CustomException extends RuntimeException {
/**
* 错误代码
*/
private final ResultCode resultCode;
public CustomException(String errorReason,ResultCode resultCode) {
super(errorReason);
this.resultCode = resultCode;
}
public CustomException(ResultCode resultCode){
this.resultCode = resultCode;
}
public ResultCode getResultCode(){
return resultCode;
}
}
package com.zhiwei.brandkbs2.exception;
import com.zhiwei.brandkbs2.model.ResultCode;
/**
* @author sjj
* @version 1.0
* @description 自定义异常捕获类
* @date 2022年4月20日16:55:19
*/
public class ExceptionCast {
public static void cast(ResultCode resultCode){
throw new CustomException(resultCode);
}
}
package com.zhiwei.brandkbs2.exception;
import com.google.common.collect.ImmutableMap;
import com.zhiwei.brandkbs2.model.CommonCodeEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.model.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.Collections;
import java.util.Objects;
/**
* 控制器增强
*
* @author sjj
* @version 1.0
* @description 统一异常处理类
* @date 2022年4月21日15:00:40
*/
@RestControllerAdvice
@Slf4j
public class ExceptionCatch {
/**
* 定义map,配置异常类型所对应的错误代码
*/
private static ImmutableMap<Class<? extends Throwable>, ResultCode> EXCEPTIONS;
/**
* 定义map的builder对象,去构建ImmutableMap
*/
protected static ImmutableMap.Builder<Class<? extends Throwable>, ResultCode> builder = ImmutableMap.builder();
/**
* 捕获CustomException此类异常
*
* @param customException 自定义异常类型
* @return 响应结果
*/
@ExceptionHandler(CustomException.class)
public ResponseResult customException(CustomException customException) {
ResultCode resultCode = customException.getResultCode();
//记录日志
log.error("catch customException:{}", resultCode.message());
return new ResponseResult(resultCode, Collections.EMPTY_LIST);
}
/**
* 捕获Exception此类异常
*
* @param exception Exception异常类型
* @return 响应结果
*/
@ExceptionHandler(Exception.class)
public ResponseResult exception(Exception exception) {
//记录日志
log.error("catch exception:", exception);
if (Objects.isNull(EXCEPTIONS)) {
//EXCEPTIONS构建成功
EXCEPTIONS = builder.build();
}
//从EXCEPTIONS中找异常类型所对应的错误代码
ResultCode resultCode = EXCEPTIONS.get(exception.getClass());
if (Objects.nonNull(resultCode)) {
return new ResponseResult(resultCode, Collections.EMPTY_LIST);
} else {
//返回500系统繁忙异常
return new ResponseResult(CommonCodeEnum.SERVER_ERROR, Collections.EMPTY_LIST);
}
}
static {
//定义异常类型所对应的错误代码
builder.put(HttpMessageNotReadableException.class, CommonCodeEnum.INVALID_PARAM);
builder.put(NullPointerException.class, CommonCodeEnum.NULL_POINTER_EXCEPTION);
}
}
package com.zhiwei.brandkbs2.listener;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.concurrent.*;
/**
* @author lxj
* @version 1.0
* @description 项目启动监听类
* @date 2019/9/2 14:00
*/
@Component
@Slf4j
public class ApplicationProjectListener {
private static final int CORE_POOL_SIZE = 50;
private static final int MAX_POOL_SIZE = 200;
private static final int QUEUE_SIZE = 128;
private static final long KEEP_ALIVE_TIME = 0L;
/**
* 项目共用线程池
*/
private static final ThreadPoolExecutor PROJECT_THREAD_POOL;
static {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(false).setNameFormat("project-asny-%d").build();
PROJECT_THREAD_POOL = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(QUEUE_SIZE), threadFactory,
//配置拒绝策略,抛出java.util.concurrent.RejectedExecutionException异常
new ThreadPoolExecutor.AbortPolicy());
}
/**
* 获取公用线程池
*
* @return 公用线程池
*/
public static ThreadPoolExecutor getThreadPool() {
return PROJECT_THREAD_POOL;
}
}
package com.zhiwei.brandkbs2.model;
/**
* @author lxj
* @version 1.0
* @description 全局状态码及信息
* @date 2019/8/5 10:12
*/
public enum CommonCodeEnum implements ResultCode {
/**
* 非法参数
*/
INVALID_PARAM(false, 403, "非法参数!", 200),
/**
* 操作成功
*/
SUCCESS(true, 200, "操作成功!", 200),
/**
* 操作失败
*/
FAIL(false, 400, "操作失败!", 200),
/**
* 未登录
*/
UNAUTHENTICATED(false, 401, "此操作需要登陆系统!", 200),
/**
* 权限不足
*/
UN_AUTHORISE(false, 402, "权限不足,无权操作!", 200),
/**
* 系统异常
*/
SERVER_ERROR(false, 500, "抱歉,系统繁忙,请稍后重试!", 200),
/**
* 空指针异常
*/
NULL_POINTER_EXCEPTION(false, 666, "空指针异常", 200);
/**
* 操作是否成功
*/
final boolean success;
/**
* 操作代码
*/
final int code;
/**
* 提示信息
*/
String message;
/**
* 聚合状态码
*/
final int aggCode;
CommonCodeEnum(boolean success, int code, String message, int aggCode) {
this.success = success;
this.code = code;
this.message = message;
this.aggCode = aggCode;
}
public CommonCodeEnum message(String message){
this.message = message;
return this;
}
@Override
public boolean success() {
return success;
}
@Override
public int code() {
return code;
}
@Override
public String message() {
return message;
}
@Override
public int aggCode() {
return aggCode;
}
}
package com.zhiwei.brandkbs2.model;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.pojo.vo.PageVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.*;
/**
* @author lxj
* @version 1.0
* @description 统一响应结果类
* @date 2019/8/5 10:14
*/
@Data
@ToString
@NoArgsConstructor
@ApiModel("响应结果实体类")
public class ResponseResult {
/**
* 操作是否成功
*/
@ApiModelProperty("操作是否成功")
private boolean success;
/**
* 操作状态码
*/
@ApiModelProperty("操作状态码")
private int code;
/**
* 提示信息
*/
@ApiModelProperty("提示信息")
private String message;
/**
* 聚合状态码
*/
@ApiModelProperty("聚合状态码")
private int aggCode;
/**
* 返回数据
*/
@ApiModelProperty("返回数据")
private Object data;
public ResponseResult(ResultCode resultCode, Object data) {
this.success = resultCode.success();
this.code = resultCode.code();
this.message = resultCode.message();
this.aggCode = resultCode.aggCode();
this.data = data;
}
/**
* 操作成功带返回数据
*
* @param data 返回数据
* @return 操作成功带返回数据
*/
public static ResponseResult success(Object data) {
return new ResponseResult(CommonCodeEnum.SUCCESS, data);
}
/**
* 操作成功不带返回数据
*
* @return 操作成功不带返回数据
*/
public static ResponseResult success() {
return success(Collections.EMPTY_LIST);
}
/**
* 操作失败带返回数据
*
* @param data 返回数据
* @return 操作失败带返回数据
*/
public static ResponseResult failure(Object data) {
return new ResponseResult(CommonCodeEnum.FAIL, data);
}
/**
* 舆情外部接口转换返回
*
* @param json
* @return
*/
public static ResponseResult convertFromYuQingInterface(JSONObject json, Integer pageSize) {
if (null == json) {
return ResponseResult.failure(null);
}
// 接口调用失败
if (!Boolean.TRUE.equals(json.getBooleanValue("status")) || 200 != json.getInteger("code")) {
return ResponseResult.failure(json.getString("message"));
}
// 非列表数据
if (null == pageSize) {
return ResponseResult.success(json.get("data"));
}
JSONObject data = json.getJSONObject("data");
if (null == data) {
return ResponseResult.success();
}
long total = Optional.ofNullable(data.getLong("total")).orElse(0L);
int page = Optional.ofNullable(data.getInteger("page")).orElse(0);
long totalNum = Optional.ofNullable(data.getLong("totalNum")).orElse(0L);
List<JSONObject> list = data.getJSONArray("list").toJavaList(JSONObject.class);
return ResponseResult.success(PageVO.createPageVo(totalNum, page, total, pageSize, list));
}
}
package com.zhiwei.brandkbs2.model;
/**
* @author sjj
* @version 1.0
* @description 状态码和信息
* 1000-- 登录错误代码
* 1100-- 用户错误代码
* 1200-- 项目错误代码
* 1300-- 标签错误代码
* 1400-- 事件错误代码
* 1500-- 渠道错误代码
* 1600-- 报道错误代码
* 1700-- 收藏错误代码
* 1800-- 预警事件错误代码
* 1900-- 稿件错误代码
* 2000-- 自定义事件错误代码
* @date 2022年4月18日11:02:53
*/
public interface ResultCode {
/**
* 操作是否成功,true为成功,false操作失败
*
* @return 操作是否成功
*/
boolean success();
/**
* 操作状态码
*
* @return 操作状态码
*/
int code();
/**
* 提示信息
*
* @return 提示信息
*/
String message();
/**
* 聚合状态码
*
* @return 聚合状态码
*/
int aggCode();
}
package com.zhiwei.brandkbs2.pojo;
import lombok.Data;
import org.springframework.data.annotation.Id;
/**
* @ClassName: AbstractBaseMongo
* @Description mongo抽象类
* @author: sjj
* @date: 2022-04-29 14:52
*/
@Data
public abstract class AbstractBaseMongo {
/**
* 主键ID
*/
@Id
String id;
}
package com.zhiwei.brandkbs2.pojo;
import com.zhiwei.middleware.mark.vo.MarkerTag;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* @author sjj
* @version 1.0
* @description 项目属性抽象类
* @date 2022年4月18日15:04:29
*/
@Setter
@Getter
public abstract class AbstractProject extends AbstractBaseMongo{
/**
* 项目名
*/
private String projectName;
/**
* 品牌名称
*/
private String brandName;
/**
* 品牌关联项目组
*/
private String brandLinkedGroup;
/**
* 品牌关联项目组ID
*/
private String brandLinkedGroupId;
/**
* 命中标签
*/
private List<MarkerTag> hitTags;
/**
* 命中关键词
*/
private List<String> hitKeywords;
/**
* 高频关键词
*/
private List<String> highKeywords;
/**
* 是否包含敏感
*/
private boolean isMergeSensitive;
/**
* 头像地址
*/
private String avatarUrl;
}
package com.zhiwei.brandkbs2.pojo;
import lombok.Getter;
import lombok.Setter;
/**
* @ClassName: BaseMap
* @Description 基础字段
* @author: sjj
* @date: 2022-06-20 11:21
*/
@Setter
@Getter
public class BaseMap {
/**
* url
*/
private String url;
/**
* title
*/
private String title;
/**
* content
*/
private String content;
/**
* time
*/
private Long time;
/**
* 平台
*/
private String platform;
/**
* 来源
*/
private String realSource;
/**
* 渠道
*/
private String source;
/**
* 是否转发
*/
private boolean forward;
/**
* 情感倾向
*/
private String emotion;
}
package com.zhiwei.brandkbs2.pojo;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
/**
* @ClassName: Behavior
* @Description 用户行为实体类
* @author: sjj
* @date: 2022-05-27 11:11
*/
@Getter
@Setter
public class Behavior extends AbstractBaseMongo {
/**
* 用户ID
*/
private String userId;
/**
* 项目ID
*/
private String projectId;
/**
* IP地址
*/
private String ip;
/**
* 创建时间
*/
private Date cTime;
/**
* 访问页面
*/
private String page;
/**
* 操作模块
*/
private String module;
/**
* false:前台,true:后台
*/
private boolean backstage;
@Getter
public static class Operation {
private final String page;
private final boolean backstage;
public Operation(String page, boolean backstage) {
this.page = page;
this.backstage = backstage;
}
}
}
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