Selaa lähdekoodia

feat:appim发课、会员管理等

caoliqin 1 kuukausi sitten
vanhempi
commit
92eef70879
100 muutettua tiedostoa jossa 10397 lisäystä ja 0 poistoa
  1. 152 0
      fs-app-task-course/pom.xml
  2. 14 0
      fs-app-task-course/src/main/java/com/fs/FSServletInitializer.java
  3. 24 0
      fs-app-task-course/src/main/java/com/fs/FsAppTaskCourseApplication.java
  4. 29 0
      fs-app-task-course/src/main/java/com/fs/app/controller/CommonController.java
  5. 51 0
      fs-app-task-course/src/main/java/com/fs/app/exception/FSException.java
  6. 81 0
      fs-app-task-course/src/main/java/com/fs/app/exception/FSExceptionHandler.java
  7. 90 0
      fs-app-task-course/src/main/java/com/fs/app/task/AppCourseWatchLogScheduler.java
  8. 12 0
      fs-app-task-course/src/main/java/com/fs/app/taskService/SopLogsTaskService.java
  9. 309 0
      fs-app-task-course/src/main/java/com/fs/app/taskService/impl/SopLogsTaskServiceImpl.java
  10. 171 0
      fs-app-task-course/src/main/java/com/fs/framework/aspectj/DataScopeAspect.java
  11. 73 0
      fs-app-task-course/src/main/java/com/fs/framework/aspectj/DataSourceAspect.java
  12. 219 0
      fs-app-task-course/src/main/java/com/fs/framework/aspectj/LogAspect.java
  13. 117 0
      fs-app-task-course/src/main/java/com/fs/framework/aspectj/RateLimiterAspect.java
  14. 31 0
      fs-app-task-course/src/main/java/com/fs/framework/config/ApplicationConfig.java
  15. 58 0
      fs-app-task-course/src/main/java/com/fs/framework/config/ArrayStringTypeHandler.java
  16. 85 0
      fs-app-task-course/src/main/java/com/fs/framework/config/CaptchaConfig.java
  17. 119 0
      fs-app-task-course/src/main/java/com/fs/framework/config/DataSourceConfig.java
  18. 123 0
      fs-app-task-course/src/main/java/com/fs/framework/config/DruidConfig.java
  19. 72 0
      fs-app-task-course/src/main/java/com/fs/framework/config/FastJson2JsonRedisSerializer.java
  20. 59 0
      fs-app-task-course/src/main/java/com/fs/framework/config/FilterConfig.java
  21. 76 0
      fs-app-task-course/src/main/java/com/fs/framework/config/KaptchaTextCreator.java
  22. 150 0
      fs-app-task-course/src/main/java/com/fs/framework/config/MyBatisConfig.java
  23. 153 0
      fs-app-task-course/src/main/java/com/fs/framework/config/RedisConfig.java
  24. 76 0
      fs-app-task-course/src/main/java/com/fs/framework/config/ResourcesConfig.java
  25. 11 0
      fs-app-task-course/src/main/java/com/fs/framework/config/RetryConfig.java
  26. 158 0
      fs-app-task-course/src/main/java/com/fs/framework/config/SecurityConfig.java
  27. 33 0
      fs-app-task-course/src/main/java/com/fs/framework/config/ServerConfig.java
  28. 121 0
      fs-app-task-course/src/main/java/com/fs/framework/config/SwaggerConfig.java
  29. 102 0
      fs-app-task-course/src/main/java/com/fs/framework/config/ThreadPoolConfig.java
  30. 77 0
      fs-app-task-course/src/main/java/com/fs/framework/config/properties/DruidProperties.java
  31. 27 0
      fs-app-task-course/src/main/java/com/fs/framework/datasource/DynamicDataSource.java
  32. 45 0
      fs-app-task-course/src/main/java/com/fs/framework/datasource/DynamicDataSourceContextHolder.java
  33. 115 0
      fs-app-task-course/src/main/java/com/fs/framework/exception/GlobalExceptionHandler.java
  34. 56 0
      fs-app-task-course/src/main/java/com/fs/framework/interceptor/RepeatSubmitInterceptor.java
  35. 126 0
      fs-app-task-course/src/main/java/com/fs/framework/interceptor/impl/SameUrlDataInterceptor.java
  36. 56 0
      fs-app-task-course/src/main/java/com/fs/framework/manager/AsyncManager.java
  37. 40 0
      fs-app-task-course/src/main/java/com/fs/framework/manager/ShutdownManager.java
  38. 106 0
      fs-app-task-course/src/main/java/com/fs/framework/manager/factory/AsyncFactory.java
  39. 69 0
      fs-app-task-course/src/main/java/com/fs/framework/security/LoginBody.java
  40. 255 0
      fs-app-task-course/src/main/java/com/fs/framework/security/LoginUser.java
  41. 89 0
      fs-app-task-course/src/main/java/com/fs/framework/security/SecurityUtils.java
  42. 47 0
      fs-app-task-course/src/main/java/com/fs/framework/security/filter/JwtAuthenticationTokenFilter.java
  43. 35 0
      fs-app-task-course/src/main/java/com/fs/framework/security/handle/AuthenticationEntryPointImpl.java
  44. 54 0
      fs-app-task-course/src/main/java/com/fs/framework/security/handle/LogoutSuccessHandlerImpl.java
  45. 92 0
      fs-app-task-course/src/main/java/com/fs/framework/service/CompanyLoginService.java
  46. 66 0
      fs-app-task-course/src/main/java/com/fs/framework/service/CompanyPermissionService.java
  47. 170 0
      fs-app-task-course/src/main/java/com/fs/framework/service/PermissionService.java
  48. 236 0
      fs-app-task-course/src/main/java/com/fs/framework/service/TokenService.java
  49. 75 0
      fs-app-task-course/src/main/java/com/fs/framework/service/UserDetailsServiceImpl.java
  50. 1 0
      fs-app-task-course/src/main/resources/META-INF/spring-devtools.properties
  51. 10 0
      fs-app-task-course/src/main/resources/application.yml
  52. 2 0
      fs-app-task-course/src/main/resources/banner.txt
  53. 37 0
      fs-app-task-course/src/main/resources/i18n/messages.properties
  54. 93 0
      fs-app-task-course/src/main/resources/logback.xml
  55. 19 0
      fs-app-task-course/src/main/resources/mybatis/mybatis-config.xml
  56. 144 0
      fs-app-task/pom.xml
  57. 22 0
      fs-app-task/src/main/java/com/fs/FsAppTaskApplication.java
  58. 42 0
      fs-app-task/src/main/java/com/fs/app/config/ConsumerThreadPoolConfig.java
  59. 12 0
      fs-app-task/src/main/java/com/fs/app/config/ScheduleTaskConfig.java
  60. 177 0
      fs-app-task/src/main/java/com/fs/app/controller/AppApiController.java
  61. 46 0
      fs-app-task/src/main/java/com/fs/app/core/ConditionalOnLoadControl.java
  62. 86 0
      fs-app-task/src/main/java/com/fs/app/core/LoadControlCondition.java
  63. 15 0
      fs-app-task/src/main/java/com/fs/app/init/CacheHolder.java
  64. 41 0
      fs-app-task/src/main/java/com/fs/app/init/DictHolder.java
  65. 50 0
      fs-app-task/src/main/java/com/fs/app/properties/AppTaskProperties.java
  66. 176 0
      fs-app-task/src/main/java/com/fs/app/task/consumer/customer/UserBindOrUnbindCustomerConsumer.java
  67. 486 0
      fs-app-task/src/main/java/com/fs/app/task/consumer/sop/AppSopLogsConsumer.java
  68. 402 0
      fs-app-task/src/main/java/com/fs/app/task/consumer/sop/AppSopTagCustomerConsumer.java
  69. 173 0
      fs-app-task/src/main/java/com/fs/app/task/consumer/tag/UserBindOrUnbindTagConsumer.java
  70. 432 0
      fs-app-task/src/main/java/com/fs/app/task/consumer/welcome/SendWelcomeTask.java
  71. 228 0
      fs-app-task/src/main/java/com/fs/app/task/other/ai/AiChatTimingRemind.java
  72. 92 0
      fs-app-task/src/main/java/com/fs/app/task/other/calibration/BatchIdCalibration.java
  73. 111 0
      fs-app-task/src/main/java/com/fs/app/task/other/clear/ClearTask.java
  74. 242 0
      fs-app-task/src/main/java/com/fs/app/task/producer/sop/AppSopLogsProducer.java
  75. 64 0
      fs-app-task/src/main/java/com/fs/app/task/producer/urgentclass/SyncWatchLog2UrgentClass.java
  76. 190 0
      fs-app-task/src/main/java/com/fs/common/redis/RedisBatchUtil.java
  77. 202 0
      fs-app-task/src/main/java/com/fs/common/redis/RedisSetProcessor.java
  78. 171 0
      fs-app-task/src/main/java/com/fs/framework/aspectj/DataScopeAspect.java
  79. 73 0
      fs-app-task/src/main/java/com/fs/framework/aspectj/DataSourceAspect.java
  80. 219 0
      fs-app-task/src/main/java/com/fs/framework/aspectj/LogAspect.java
  81. 117 0
      fs-app-task/src/main/java/com/fs/framework/aspectj/RateLimiterAspect.java
  82. 31 0
      fs-app-task/src/main/java/com/fs/framework/config/ApplicationConfig.java
  83. 58 0
      fs-app-task/src/main/java/com/fs/framework/config/ArrayStringTypeHandler.java
  84. 85 0
      fs-app-task/src/main/java/com/fs/framework/config/CaptchaConfig.java
  85. 135 0
      fs-app-task/src/main/java/com/fs/framework/config/DataSourceConfig.java
  86. 123 0
      fs-app-task/src/main/java/com/fs/framework/config/DruidConfig.java
  87. 72 0
      fs-app-task/src/main/java/com/fs/framework/config/FastJson2JsonRedisSerializer.java
  88. 59 0
      fs-app-task/src/main/java/com/fs/framework/config/FilterConfig.java
  89. 76 0
      fs-app-task/src/main/java/com/fs/framework/config/KaptchaTextCreator.java
  90. 148 0
      fs-app-task/src/main/java/com/fs/framework/config/MyBatisConfig.java
  91. 153 0
      fs-app-task/src/main/java/com/fs/framework/config/RedisConfig.java
  92. 76 0
      fs-app-task/src/main/java/com/fs/framework/config/ResourcesConfig.java
  93. 11 0
      fs-app-task/src/main/java/com/fs/framework/config/RetryConfig.java
  94. 158 0
      fs-app-task/src/main/java/com/fs/framework/config/SecurityConfig.java
  95. 33 0
      fs-app-task/src/main/java/com/fs/framework/config/ServerConfig.java
  96. 120 0
      fs-app-task/src/main/java/com/fs/framework/config/SwaggerConfig.java
  97. 130 0
      fs-app-task/src/main/java/com/fs/framework/config/ThreadPoolConfig.java
  98. 77 0
      fs-app-task/src/main/java/com/fs/framework/config/properties/DruidProperties.java
  99. 27 0
      fs-app-task/src/main/java/com/fs/framework/datasource/DynamicDataSource.java
  100. 45 0
      fs-app-task/src/main/java/com/fs/framework/datasource/DynamicDataSourceContextHolder.java

+ 152 - 0
fs-app-task-course/pom.xml

@@ -0,0 +1,152 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>fs</artifactId>
+        <groupId>com.fs</groupId>
+        <version>1.1.0</version>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.fs</groupId>
+    <artifactId>fs-app-task-course</artifactId>
+    <description>
+        APP课程状态定时任务
+    </description>
+
+    <dependencies>
+        <!-- spring-boot-devtools -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-devtools</artifactId>
+            <optional>true</optional> <!-- 表示依赖不会传递 -->
+        </dependency>
+        <!-- swagger2-->
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger2</artifactId>
+        </dependency>
+
+        <!-- swagger2-UI-->
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger-ui</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.github.xiaoymin</groupId>
+            <artifactId>swagger-bootstrap-ui</artifactId>
+            <version>1.9.3</version>
+        </dependency>
+
+
+        <!-- Mysql驱动包 -->
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+        </dependency>
+
+        <!-- SpringBoot Web容器 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <!-- SpringBoot 拦截器 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
+        <!-- 阿里数据库连接池 -->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid-spring-boot-starter</artifactId>
+        </dependency>
+
+        <!--clickhouse-->
+        <dependency>
+            <groupId>com.clickhouse</groupId>
+            <artifactId>clickhouse-jdbc</artifactId>
+            <version>0.4.6</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.retry</groupId>
+            <artifactId>spring-retry</artifactId>
+            <version>1.3.1</version>
+        </dependency>
+
+        <!--        <dependency>-->
+<!--            <groupId>ru.yandex.clickhouse</groupId>-->
+<!--            <artifactId>clickhouse-jdbc</artifactId>-->
+<!--            <version>0.3.2</version>-->
+<!--        </dependency>-->
+
+        <!-- 验证码 -->
+        <dependency>
+            <groupId>com.github.penggle</groupId>
+            <artifactId>kaptcha</artifactId>
+            <exclusions>
+                <exclusion>
+                    <artifactId>javax.servlet-api</artifactId>
+                    <groupId>javax.servlet</groupId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <!-- 获取系统信息 -->
+        <dependency>
+            <groupId>com.github.oshi</groupId>
+            <artifactId>oshi-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.fs</groupId>
+            <artifactId>fs-service</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>2.1.1.RELEASE</version>
+                <configuration>
+                    <includeSystemScope>true</includeSystemScope>
+                    <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>3.1.0</version>
+                <configuration>
+                    <failOnMissingWebXml>false</failOnMissingWebXml>
+                    <warName>${project.artifactId}</warName>
+                </configuration>
+            </plugin>
+        </plugins>
+        <finalName>${project.artifactId}</finalName>
+    </build>
+
+
+</project>

+ 14 - 0
fs-app-task-course/src/main/java/com/fs/FSServletInitializer.java

@@ -0,0 +1,14 @@
+package com.fs;
+
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
+
+
+public class FSServletInitializer extends SpringBootServletInitializer
+{
+    @Override
+    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
+    {
+        return application.sources(FsAppTaskCourseApplication.class);
+    }
+}

+ 24 - 0
fs-app-task-course/src/main/java/com/fs/FsAppTaskCourseApplication.java

@@ -0,0 +1,24 @@
+package com.fs;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+/**
+ * 启动程序
+ */
+@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
+@EnableTransactionManagement
+@EnableAsync
+@EnableScheduling
+public class FsAppTaskCourseApplication
+{
+    public static void main(String[] args){
+        // System.setProperty("spring.devtools.restart.enabled", "false");
+        SpringApplication.run(FsAppTaskCourseApplication.class, args);
+        System.out.println("appTask启动成功");
+    }
+}

+ 29 - 0
fs-app-task-course/src/main/java/com/fs/app/controller/CommonController.java

@@ -0,0 +1,29 @@
+package com.fs.app.controller;
+
+
+
+import com.fs.app.task.AppCourseWatchLogScheduler;
+import com.fs.common.core.domain.R;
+import io.swagger.annotations.Api;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+
+
+@Api("公共接口")
+@RestController
+@RequestMapping(value="/app/common")
+@Slf4j
+public class CommonController {
+
+    @Autowired
+    private AppCourseWatchLogScheduler appCourseWatchLogScheduler;
+    @GetMapping("/test")
+    public void test(){
+        appCourseWatchLogScheduler.checkWatchStatus();
+    }
+
+}

+ 51 - 0
fs-app-task-course/src/main/java/com/fs/app/exception/FSException.java

@@ -0,0 +1,51 @@
+package com.fs.app.exception;
+
+/**
+ * 自定义异常
+ */
+public class FSException extends RuntimeException {
+	private static final long serialVersionUID = 1L;
+
+    private String msg;
+    private int code = 500;
+
+    public FSException(String msg) {
+		super(msg);
+		this.msg = msg;
+	}
+
+	public FSException(String msg, Throwable e) {
+		super(msg, e);
+		this.msg = msg;
+	}
+
+	public FSException(String msg, int code) {
+		super(msg);
+		this.msg = msg;
+		this.code = code;
+	}
+
+	public FSException(String msg, int code, Throwable e) {
+		super(msg, e);
+		this.msg = msg;
+		this.code = code;
+	}
+
+	public String getMsg() {
+		return msg;
+	}
+
+	public void setMsg(String msg) {
+		this.msg = msg;
+	}
+
+	public int getCode() {
+		return code;
+	}
+
+	public void setCode(int code) {
+		this.code = code;
+	}
+
+
+}

+ 81 - 0
fs-app-task-course/src/main/java/com/fs/app/exception/FSExceptionHandler.java

@@ -0,0 +1,81 @@
+package com.fs.app.exception;
+
+
+
+
+import com.fs.common.core.domain.R;
+import com.fs.common.exception.CustomException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.dao.DuplicateKeyException;
+import org.springframework.security.access.AccessDeniedException;
+import org.springframework.validation.BindException;
+import org.springframework.validation.FieldError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.servlet.NoHandlerFoundException;
+
+
+/**
+ * 异常处理器
+ */
+@RestControllerAdvice
+public class FSExceptionHandler {
+	private Logger logger = LoggerFactory.getLogger(getClass());
+
+	/**
+	 * 处理自定义异常
+	 */
+	@ExceptionHandler(FSException.class)
+	public R handleRRException(FSException e){
+		R r = new R();
+		r.put("code", e.getCode());
+		r.put("msg", e.getMessage());
+
+		return r;
+	}
+
+	@ExceptionHandler(NoHandlerFoundException.class)
+	public R handlerNoFoundException(Exception e) {
+		logger.error(e.getMessage(), e);
+		return R.error(404, "路径不存在,请检查路径是否正确");
+	}
+
+	@ExceptionHandler(DuplicateKeyException.class)
+	public R handleDuplicateKeyException(DuplicateKeyException e){
+		logger.error(e.getMessage(), e);
+		return R.error("数据库中已存在该记录");
+	}
+
+
+	@ExceptionHandler(Exception.class)
+	public R handleException(Exception e){
+		logger.error(e.getMessage(), e);
+		return R.error();
+	}
+	@ExceptionHandler(AccessDeniedException.class)
+	public R handleAccessDeniedException(AccessDeniedException e){
+		logger.error(e.getMessage(), e);
+		return R.error("没有权限");
+	}
+
+	@ExceptionHandler(BindException.class)
+	public R bindExceptionHandler(BindException e) {
+		FieldError error = e.getFieldError();
+		String message = String.format("%s",  error.getDefaultMessage());
+		return R.error(message);
+	}
+
+	@ExceptionHandler(MethodArgumentNotValidException.class)
+	public R exceptionHandler(MethodArgumentNotValidException e) {
+		FieldError error = e.getBindingResult().getFieldError();
+		String message = String.format("%s",  error.getDefaultMessage());
+		return R.error(message);
+	}
+	@ExceptionHandler(CustomException.class)
+	public R handleException(CustomException e){
+
+		return R.error(e.getMessage());
+	}
+}

+ 90 - 0
fs-app-task-course/src/main/java/com/fs/app/task/AppCourseWatchLogScheduler.java

@@ -0,0 +1,90 @@
+package com.fs.app.task;
+
+import com.fs.app.course.service.IAppCourseLinkService;
+import com.fs.app.taskService.SopLogsTaskService;
+import com.fs.app.watchlog.service.IAppCourseWatchLogService;
+import com.fs.common.core.redis.RedisCache;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.Calendar;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+@Component("AppCourseWatchLogScheduler")
+@Slf4j
+public class AppCourseWatchLogScheduler {
+    private final AtomicBoolean isRunning1 = new AtomicBoolean(false);
+    @Autowired
+    RedisCache redisCache;
+
+    @Autowired
+    private IAppCourseWatchLogService courseWatchLogService;
+
+    @Autowired
+    private SopLogsTaskService sopLogsTaskService;
+
+
+    @Autowired
+    private IAppCourseLinkService appCourseLinkService;
+
+
+    @Scheduled(fixedRate = 60000) // 每分钟执行一次
+    public void checkWatchStatus() {
+        // 尝试设置标志为 true,表示任务开始执行
+        if (!isRunning1.compareAndSet(false, true)) {
+            log.info("检查看课中任务执行 - 上一个任务尚未完成,跳过此次执行");
+            return;
+        }
+
+        try {
+            log.info("检查看课中任务执行>>>>>>>>>>>>");
+            courseWatchLogService.scheduleBatchUpdateToDatabase();
+            courseWatchLogService.checkWatchStatus();
+            log.info("检查看课中任务执行完成>>>>>>>>>>>>");
+
+            // 检查当前时间是否为整五分钟(0, 5, 10, 15, ... 55分钟)
+            Calendar calendar = Calendar.getInstance();
+            int minute = calendar.get(Calendar.MINUTE);
+
+            // 只有当分钟数是5的倍数时才执行创建完课消息
+            if (minute % 5 == 0) {
+            try {
+                long startTime = System.currentTimeMillis();
+                log.info("创建完课消息 - 定时任务开始" + System.currentTimeMillis());
+                sopLogsTaskService.createCourseFinishMsg();
+                long endTime = System.currentTimeMillis();
+                long duration = endTime - startTime;
+                log.info("创建完课消息 - 定时任务成功完成" + duration);
+            } catch (Exception e) {
+                log.error("创建完课消息 - 定时任务执行失败", ExceptionUtils.getStackTrace(e));
+            }
+            }
+        } catch (Exception e) {
+            log.error("检查看课中任务执行完成 - 定时任务执行失败", ExceptionUtils.getStackTrace(e));
+        } finally {
+            // 重置标志为 false,表示任务已完成
+            isRunning1.set(false);
+        }
+    }
+
+    /**
+     * 每天删除过期短链
+     */
+    @Scheduled(cron = "0 5 0 * * ?")  // 0点5分0秒执行
+    public void delCourseExpireLink() {
+        try {
+            log.info("删除过期短链 - 定时任务开始");
+            appCourseLinkService.delCourseExpireLinkApp();
+//            courseLinkService.delCourseExpireLink();
+            log.info("删除过期短链 - 定时任务成功完成");
+        } catch (Exception e) {
+            log.error("删除过期短链 - 定时任务执行失败", e);
+        }
+
+    }
+
+}
+

+ 12 - 0
fs-app-task-course/src/main/java/com/fs/app/taskService/SopLogsTaskService.java

@@ -0,0 +1,12 @@
+package com.fs.app.taskService;
+
+import java.time.LocalDateTime;
+
+public interface SopLogsTaskService {
+
+    /**
+     * 创建完课消息
+     */
+    void createCourseFinishMsg();
+
+}

+ 309 - 0
fs-app-task-course/src/main/java/com/fs/app/taskService/impl/SopLogsTaskServiceImpl.java

@@ -0,0 +1,309 @@
+package com.fs.app.taskService.impl;
+
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.fs.app.course.domain.AppCourseFinishTemp;
+import com.fs.app.course.service.IAppCourseFinishTempService;
+import com.fs.app.sop.domain.AppSopLogs;
+import com.fs.app.sop.result.AppSopCourseFinishTempSetting;
+import com.fs.app.sop.service.IAppSopLogsService;
+import com.fs.app.taskService.SopLogsTaskService;
+import com.fs.app.watchlog.domain.AppCourseWatchLog;
+import com.fs.app.watchlog.mapper.AppCourseWatchLogMapper;
+import com.fs.his.domain.FsUser;
+import com.fs.his.mapper.FsUserMapper;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.*;
+
+@Service
+@Slf4j
+public class SopLogsTaskServiceImpl implements SopLogsTaskService {
+
+    @Autowired
+    private AppCourseWatchLogMapper fsCourseWatchLogMapper;
+
+    @Autowired
+    private IAppSopLogsService appSopLogsService;
+
+    @Autowired
+    private FsUserMapper fsUserMapper;
+
+    @Autowired
+    private IAppCourseFinishTempService appCourseFinishTempService;
+
+
+    @Value("${config.batch-size:500}")
+    private Integer batchSize;
+
+
+    @Override
+    public void createCourseFinishMsg() {
+        long startTime = System.currentTimeMillis();
+        log.info("创建完课消息 - 定时任务开始 {}", startTime);
+        // 线程池配置
+        int threadPoolSize = 4;
+        ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);
+
+        // 用于收集所有处理结果的队列
+        BlockingQueue<List<AppCourseWatchLog>> batchQueue = new LinkedBlockingQueue<>();
+
+        try {
+            // 查询当天日期范围
+            LocalDate today = LocalDate.now();
+            Date startDate = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
+            Date endDate = Date.from(today.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
+
+            // 启动生产者线程 - 流式分批查询数据
+            executorService.submit(() -> {
+                try {
+                    int batchSize = 1000;
+                    long maxId = 0;
+                    boolean hasMore = true;
+
+                    while (hasMore) {
+                        // 查询当前批次数据
+                        List<AppCourseWatchLog> batch = fsCourseWatchLogMapper.selectFsCourseWatchLogFinishBatchByDate(
+                                startDate, endDate, maxId, batchSize);
+
+                        if (!batch.isEmpty()) {
+                            // 将批次放入队列
+                            batchQueue.put(batch);
+                            // 更新maxId为当前批次的最后一个ID
+                            maxId = batch.get(batch.size() - 1).getLogId();
+                            log.debug("已生产批次数据,最后logId: {}, 数量: {}", maxId, batch.size());
+                        }
+
+                        if (batch.size() < batchSize) {
+                            hasMore = false;
+                            batchQueue.put(Collections.emptyList());// 结束标志
+                            log.info("数据生产完成,最后logId: {}", maxId);
+                        }
+                    }
+                } catch (Exception e) {
+                    log.error("生产数据时出错", e);
+                    try {
+                        batchQueue.put(Collections.emptyList()); // 确保消费者能退出
+                    } catch (InterruptedException ie) {
+                        Thread.currentThread().interrupt();
+                    }
+                }
+            });
+
+            // 消费者线程处理数据
+            List<Future<?>> futures = new ArrayList<>();
+            for (int i = 0; i < threadPoolSize; i++) {
+                futures.add(executorService.submit(() -> {
+                    try {
+                        while (true) {
+                            List<AppCourseWatchLog> batch = batchQueue.take();
+
+                            // 空列表表示处理结束
+                            if (batch.isEmpty()) {
+                                batchQueue.put(Collections.emptyList()); // 传递给其他消费者
+                                break;
+                            }
+                            log.info("开始处理批次数据");
+                            processBatch(batch); // 处理批次数据
+                        }
+                    } catch (InterruptedException e) {
+                        Thread.currentThread().interrupt();
+                        log.error("处理数据时被中断", e);
+                    } catch (Exception e) {
+                        log.error("处理数据时出错", e);
+                    }
+                }));
+            }
+
+            // 等待所有任务完成
+            for (Future<?> future : futures) {
+                try {
+                    future.get();
+                } catch (InterruptedException | ExecutionException e) {
+                    log.error("等待任务完成时出错", e);
+                    Thread.currentThread().interrupt();
+                }
+            }
+
+            log.info("所有批次处理完成,总耗时: {}ms", System.currentTimeMillis() - startTime);
+
+        } finally {
+            executorService.shutdown();
+            try {
+                if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
+                    executorService.shutdownNow();
+                }
+            } catch (InterruptedException e) {
+                executorService.shutdownNow();
+                Thread.currentThread().interrupt();
+            }
+
+            batchQueue.clear();
+        }
+    }
+
+    // 处理单个批次的方法
+    private void processBatch(List<AppCourseWatchLog> batch) {
+        List<AppCourseWatchLog> finishLogsToUpdate = new ArrayList<>();
+        List<AppSopLogs> sopLogsToInsert = new ArrayList<>();
+        log.info("开始执行处理批次方法-数量:{}", batch.size());
+        Long batchId = IdUtil.getSnowflake(0, 0).nextId();
+        for (AppCourseWatchLog finishLog : batch) {
+            try {
+                FsUser fsUser = fsUserMapper.selectFsUserByUserId(finishLog.getUserId());
+                if (fsUser == null) {
+                    log.error("fsUser不存在: {}", finishLog.getUserId());
+                    continue;
+                }
+
+                // 设置 finishLog 为已发送状态,并加入批量更新列表
+                finishLog.setSendFinishMsg(1L);
+                finishLogsToUpdate.add(finishLog);
+                //完课模板
+                AppCourseFinishTemp finishTemp = this.appCourseFinishTempService.lambdaQuery()
+                        .eq(AppCourseFinishTemp::getCourseId, finishLog.getCourseId())
+                        .eq(AppCourseFinishTemp::getVideoId, finishLog.getVideoId())
+                        .eq(AppCourseFinishTemp::getStatus, 1)
+                        .orderByDesc(AppCourseFinishTemp::getCreateTime)
+                        .last(" LIMIT 1")
+                        .one();
+                if (ObjectUtil.isEmpty(finishTemp)) {
+                    log.error("完课模板不存在: 用户:{}, 课程:{},课节:{}", finishLog.getUserId(), finishLog.getCourseId(), finishLog.getVideoId());
+                    continue;
+                }
+                String meta;
+                //如果当前完课模板配置了客服(组)
+                if (ObjectUtil.isNotEmpty(meta = finishTemp.getAppCustomerIds())) {
+                    //看课记录绑定的也是客服组
+                    String target = "," + finishLog.getAppCustomerId() + ",";
+                    meta = "," + meta + ",";
+                    //当前看课记录的客服不再本次完课模板的客服中
+                    if (!meta.contains(target)) {
+                        log.error("当前看课记录对应的客服:{},不再当前完课模板(ID):{}配置的客服:{}中",
+                                finishLog.getAppCustomerId(), finishTemp.getId(), finishTemp.getAppCustomerIds());
+                        continue;
+                    }
+                }
+
+                // 构建 sopLogs 对象
+                AppSopLogs sopLogs = buildSopLogs(finishLog, finishTemp, batchId);
+                if (ObjectUtil.isEmpty(sopLogs)) {
+                    log.error("生成完课发送记录为空-:{}", finishLog.getUserId());
+                    continue;
+                }
+
+                // 如果客户状态有效,则加入批量插入列表
+                if (isValidUser(fsUser)) {
+                    sopLogsToInsert.add(sopLogs);
+                } else {
+                    log.info("完课消息-客户信息有误,不生成完课消息: {}", finishLog.getUserId());
+                }
+//                try {
+//                    fsUserCompanyBindService.finishApp(fsUser.getUserId(), finishLog);
+//                } catch (Exception e) {
+//                    log.error("更新重粉看课状态失败", e);
+//                }//todo 需新建表,优化全局重粉相关 //qw未处理,暂时不处理
+            } catch (Exception e) {
+                log.error("处理完课记录失败: {}", finishLog.getLogId(), e);
+            }
+        }
+
+        // 批量更新和插入
+        if (!finishLogsToUpdate.isEmpty()) {
+            try {
+                fsCourseWatchLogMapper.batchUpdateWatchLogSendMsg(finishLogsToUpdate);
+                log.info("批量更新 finishLog 成功,数量: {}", finishLogsToUpdate.size());
+            } catch (Exception e) {
+                log.error("批量更新 finishLog 失败", e);
+            }
+        }
+
+        if (!sopLogsToInsert.isEmpty()) {
+            try {
+                this.appSopLogsService.saveBatch(sopLogsToInsert, batchSize);
+//                appSopLogsServiceImpl.batchInsertQwSopLogs(sopLogsToInsert);
+//                qwSopLogsService.batchInsertQwSopLogs(sopLogsToInsert);
+                log.info("批量插入 sopLogs 成功,数量: {}", sopLogsToInsert.size());
+            } catch (Exception e) {
+                log.error("批量插入 sopLogs 失败", e);
+            }
+        }
+        log.info("结束处理批次方法-数量:{}", batch.size());
+    }
+
+    /**
+     * 构建 AppSopLogs 对象
+     */
+    private AppSopLogs buildSopLogs(AppCourseWatchLog finishLog, AppCourseFinishTemp finishTemp, Long batchId) {
+        // 解析模板设置
+        List<AppSopCourseFinishTempSetting.Setting> settings = JSONArray.parseArray(finishTemp.getSetting(), AppSopCourseFinishTempSetting.Setting.class);
+        if (settings == null) {
+            return null;
+        }
+        LocalDateTime currentTime = LocalDateTime.now();
+        LocalDateTime newTime = currentTime.plusMinutes(3);
+        Date newTimeDate = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
+        AppSopLogs sopLogs = new AppSopLogs();
+        sopLogs.setSendTime(newTimeDate);
+        sopLogs.setSendStatus(3);//原发送状态
+        sopLogs.setReceivingStatus(0);//接收状态
+        sopLogs.setAppSendStatus(0);//app专用发送状态
+        sopLogs.setIsHaveApp(1);
+        sopLogs.setSort(40000000);
+        sopLogs.setSopId(finishLog.getSopId());
+        sopLogs.setCreateTime(new Date());
+        sopLogs.setSopId(finishLog.getSopId());
+        sopLogs.setFsUserId(finishLog.getUserId());
+        sopLogs.setUserLogsId(finishLog.getSopPeriodId());
+        sopLogs.setSourceFrom(2);//来源:完课回复生成
+        sopLogs.setAppCustomerId(finishLog.getAppCustomerId());
+        AppSopCourseFinishTempSetting setting = new AppSopCourseFinishTempSetting();
+        setting.setVideoId(finishTemp.getVideoId());
+        setting.setCourseId(finishTemp.getCourseId());
+        setting.setType(2);//课程
+        setting.setCourseType(finishTemp.getCourseType());
+        setting.setSetting(settings);
+        sopLogs.setBatchId(batchId);
+        sopLogs.setContentJson(JSON.toJSONString(setting));
+        return sopLogs;
+    }
+
+
+    /**
+     * 解析模板设置
+     */
+    private static List<AppSopCourseFinishTempSetting.Setting> parseSettings(String jsonData) {
+        try {
+            if (jsonData.startsWith("[") && jsonData.endsWith("]")) {
+                return JSONArray.parseArray(jsonData, AppSopCourseFinishTempSetting.Setting.class);
+            } else {
+                String fixedJson = JSON.parseObject(jsonData, String.class);
+                return JSONArray.parseArray(fixedJson, AppSopCourseFinishTempSetting.Setting.class);
+            }
+        } catch (Exception e) {
+            log.error("解析模板设置失败", e);
+            return null;
+        }
+
+
+    }
+
+    /**
+     * 检查状态是否有效
+     */
+    private boolean isValidUser(FsUser fsUser) {
+        return fsUser.getStatus() == 1;
+    }
+}

+ 171 - 0
fs-app-task-course/src/main/java/com/fs/framework/aspectj/DataScopeAspect.java

@@ -0,0 +1,171 @@
+package com.fs.framework.aspectj;
+
+import com.fs.common.annotation.DataScope;
+import com.fs.common.core.domain.BaseEntity;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.spring.SpringUtils;
+import com.fs.company.domain.CompanyRole;
+import com.fs.company.domain.CompanyUser;
+import com.fs.framework.security.LoginUser;
+import com.fs.framework.service.TokenService;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+
+/**
+ * 数据过滤处理
+ *
+
+ */
+@Aspect
+@Component
+public class DataScopeAspect
+{
+    /**
+     * 全部数据权限
+     */
+    public static final String DATA_SCOPE_ALL = "1";
+
+    /**
+     * 自定数据权限
+     */
+    public static final String DATA_SCOPE_CUSTOM = "2";
+
+    /**
+     * 部门数据权限
+     */
+    public static final String DATA_SCOPE_DEPT = "3";
+
+    /**
+     * 部门及以下数据权限
+     */
+    public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";
+
+    /**
+     * 仅本人数据权限
+     */
+    public static final String DATA_SCOPE_SELF = "5";
+
+    /**
+     * 数据权限过滤关键字
+     */
+    public static final String DATA_SCOPE = "dataScope";
+
+    // 配置织入点
+    @Pointcut("@annotation(com.fs.common.annotation.DataScope)")
+    public void dataScopePointCut()
+    {
+    }
+
+    @Before("dataScopePointCut()")
+    public void doBefore(JoinPoint point) throws Throwable
+    {
+        handleDataScope(point);
+    }
+
+    protected void handleDataScope(final JoinPoint joinPoint)
+    {
+        // 获得注解
+        DataScope controllerDataScope = getAnnotationLog(joinPoint);
+        if (controllerDataScope == null)
+        {
+            return;
+        }
+        // 获取当前的用户
+        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
+        if (StringUtils.isNotNull(loginUser))
+        {
+            CompanyUser currentUser = loginUser.getUser();
+            // 如果是超级管理员,则不过滤数据
+            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
+            {
+                dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(),
+                        controllerDataScope.userAlias());
+            }
+        }
+    }
+
+    /**
+     * 数据范围过滤
+     *
+     * @param joinPoint 切点
+     * @param user 用户
+     * @param userAlias 别名
+     */
+    public static void dataScopeFilter(JoinPoint joinPoint, CompanyUser user, String deptAlias, String userAlias)
+    {
+        StringBuilder sqlString = new StringBuilder();
+
+        for (CompanyRole role : user.getRoles())
+        {
+            String dataScope = role.getDataScope();
+            if (DATA_SCOPE_ALL.equals(dataScope))
+            {
+                sqlString = new StringBuilder();
+                break;
+            }
+            else if (DATA_SCOPE_CUSTOM.equals(dataScope))
+            {
+                sqlString.append(StringUtils.format(
+                        " OR {}.dept_id IN ( SELECT dept_id FROM company_role_dept WHERE role_id = {} ) ", deptAlias,
+                        role.getRoleId()));
+            }
+            else if (DATA_SCOPE_DEPT.equals(dataScope))
+            {
+                sqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId()));
+            }
+            else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
+            {
+                sqlString.append(StringUtils.format(
+                        " OR {}.dept_id IN ( SELECT dept_id FROM company_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )",
+                        deptAlias, user.getDeptId(), user.getDeptId()));
+            }
+            else if (DATA_SCOPE_SELF.equals(dataScope))
+            {
+                if (StringUtils.isNotBlank(userAlias))
+                {
+                    sqlString.append(StringUtils.format(" OR {}.user_id = {} ", userAlias, user.getUserId()));
+                }
+                else
+                {
+                    // 数据权限为仅本人且没有userAlias别名不查询任何数据
+                    //sqlString.append(" OR 1=0 ");
+                        sqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId()));
+                }
+            }
+        }
+
+        if (StringUtils.isNotBlank(sqlString.toString()))
+        {
+            Object params = joinPoint.getArgs()[0];
+            if (StringUtils.isNotNull(params) && params instanceof BaseEntity)
+            {
+                BaseEntity baseEntity = (BaseEntity) params;
+                baseEntity.getParams().put(DATA_SCOPE, " AND (" + sqlString.substring(4) + ")");
+            }
+        }
+    }
+
+    /**
+     * 是否存在注解,如果存在就获取
+     */
+    private DataScope getAnnotationLog(JoinPoint joinPoint)
+    {
+        Signature signature = joinPoint.getSignature();
+        MethodSignature methodSignature = (MethodSignature) signature;
+        Method method = methodSignature.getMethod();
+
+        if (method != null)
+        {
+            return method.getAnnotation(DataScope.class);
+        }
+        return null;
+    }
+}

+ 73 - 0
fs-app-task-course/src/main/java/com/fs/framework/aspectj/DataSourceAspect.java

@@ -0,0 +1,73 @@
+package com.fs.framework.aspectj;
+
+import com.fs.common.annotation.DataSource;
+import com.fs.common.utils.StringUtils;
+import com.fs.framework.datasource.DynamicDataSourceContextHolder;
+import org.aspectj.lang.ProceedingJoinPoint;
+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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.util.Objects;
+
+/**
+ * 多数据源处理
+ *
+
+ */
+@Aspect
+@Order(1)
+@Component
+public class DataSourceAspect
+{
+    protected Logger logger = LoggerFactory.getLogger(getClass());
+
+    @Pointcut("@annotation(com.fs.common.annotation.DataSource)"
+            + "|| @within(com.fs.common.annotation.DataSource)")
+    public void dsPointCut()
+    {
+
+    }
+
+    @Around("dsPointCut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable
+    {
+        DataSource dataSource = getDataSource(point);
+
+        if (StringUtils.isNotNull(dataSource))
+        {
+            DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
+        }
+
+        try
+        {
+            return point.proceed();
+        }
+        finally
+        {
+            // 销毁数据源 在执行方法之后
+            DynamicDataSourceContextHolder.clearDataSourceType();
+        }
+    }
+
+    /**
+     * 获取需要切换的数据源
+     */
+    public DataSource getDataSource(ProceedingJoinPoint point)
+    {
+        MethodSignature signature = (MethodSignature) point.getSignature();
+        DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);
+        if (Objects.nonNull(dataSource))
+        {
+            return dataSource;
+        }
+
+        return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);
+    }
+}

+ 219 - 0
fs-app-task-course/src/main/java/com/fs/framework/aspectj/LogAspect.java

@@ -0,0 +1,219 @@
+package com.fs.framework.aspectj;
+
+import com.alibaba.fastjson.JSON;
+import com.fs.common.annotation.Log;
+import com.fs.common.enums.BusinessStatus;
+import com.fs.common.enums.HttpMethod;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.ip.IpUtils;
+import com.fs.common.utils.spring.SpringUtils;
+import com.fs.company.domain.CompanyOperLog;
+import com.fs.framework.manager.AsyncManager;
+import com.fs.framework.manager.factory.AsyncFactory;
+import com.fs.framework.security.LoginUser;
+import com.fs.framework.service.TokenService;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.AfterReturning;
+import org.aspectj.lang.annotation.AfterThrowing;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.servlet.HandlerMapping;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+/**
+ * 操作日志记录处理
+ *
+
+ */
+@Aspect
+@Component
+public class LogAspect
+{
+    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
+
+    // 配置织入点
+    @Pointcut("@annotation(com.fs.common.annotation.Log)")
+    public void logPointCut()
+    {
+    }
+
+    /**
+     * 处理完请求后执行
+     *
+     * @param joinPoint 切点
+     */
+    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
+    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult)
+    {
+        handleLog(joinPoint, null, jsonResult);
+    }
+
+    /**
+     * 拦截异常操作
+     *
+     * @param joinPoint 切点
+     * @param e 异常
+     */
+    @AfterThrowing(value = "logPointCut()", throwing = "e")
+    public void doAfterThrowing(JoinPoint joinPoint, Exception e)
+    {
+        handleLog(joinPoint, e, null);
+    }
+
+    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult)
+    {
+        try
+        {
+            // 获得注解
+            Log controllerLog = getAnnotationLog(joinPoint);
+            if (controllerLog == null)
+            {
+                return;
+            }
+
+            // 获取当前的用户
+            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
+
+            // *========数据库日志=========*//
+            CompanyOperLog operLog = new CompanyOperLog();
+            operLog.setCompanyId(loginUser.getCompany().getCompanyId());
+            operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
+            // 请求的地址
+            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
+            operLog.setOperIp(ip);
+            // 返回参数
+            operLog.setJsonResult(JSON.toJSONString(jsonResult));
+
+            operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
+            if (loginUser != null)
+            {
+                operLog.setOperName(loginUser.getUsername());
+            }
+
+            if (e != null)
+            {
+                operLog.setStatus(BusinessStatus.FAIL.ordinal());
+                operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
+            }
+            // 设置方法名称
+            String className = joinPoint.getTarget().getClass().getName();
+            String methodName = joinPoint.getSignature().getName();
+            operLog.setMethod(className + "." + methodName + "()");
+            // 设置请求方式
+            operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
+            // 处理设置注解上的参数
+            getControllerMethodDescription(joinPoint, controllerLog, operLog);
+            // 保存数据库
+            AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
+        }
+        catch (Exception exp)
+        {
+            // 记录本地异常日志
+            log.error("==前置通知异常==");
+            log.error("异常信息:{}", exp.getMessage());
+            exp.printStackTrace();
+        }
+    }
+
+    /**
+     * 获取注解中对方法的描述信息 用于Controller层注解
+     *
+     * @param log 日志
+     * @param operLog 操作日志
+     * @throws Exception
+     */
+    public void getControllerMethodDescription(JoinPoint joinPoint, Log log, CompanyOperLog operLog) throws Exception
+    {
+        // 设置action动作
+        operLog.setBusinessType(log.businessType().ordinal());
+        // 设置标题
+        operLog.setTitle(log.title());
+        // 设置操作人类别
+        operLog.setOperatorType(log.operatorType().ordinal());
+        // 是否需要保存request,参数和值
+        if (log.isSaveRequestData())
+        {
+            // 获取参数的信息,传入到数据库中。
+            setRequestValue(joinPoint, operLog);
+        }
+    }
+
+    /**
+     * 获取请求的参数,放到log中
+     *
+     * @param operLog 操作日志
+     * @throws Exception 异常
+     */
+    private void setRequestValue(JoinPoint joinPoint, CompanyOperLog operLog) throws Exception
+    {
+        String requestMethod = operLog.getRequestMethod();
+        if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod))
+        {
+            String params = argsArrayToString(joinPoint.getArgs());
+            operLog.setOperParam(StringUtils.substring(params, 0, 2000));
+        }
+        else
+        {
+            Map<?, ?> paramsMap = (Map<?, ?>) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
+            operLog.setOperParam(StringUtils.substring(paramsMap.toString(), 0, 2000));
+        }
+    }
+
+    /**
+     * 是否存在注解,如果存在就获取
+     */
+    private Log getAnnotationLog(JoinPoint joinPoint) throws Exception
+    {
+        Signature signature = joinPoint.getSignature();
+        MethodSignature methodSignature = (MethodSignature) signature;
+        Method method = methodSignature.getMethod();
+
+        if (method != null)
+        {
+            return method.getAnnotation(Log.class);
+        }
+        return null;
+    }
+
+    /**
+     * 参数拼装
+     */
+    private String argsArrayToString(Object[] paramsArray)
+    {
+        String params = "";
+        if (paramsArray != null && paramsArray.length > 0)
+        {
+            for (int i = 0; i < paramsArray.length; i++)
+            {
+                if (!isFilterObject(paramsArray[i]))
+                {
+                    Object jsonObj = JSON.toJSON(paramsArray[i]);
+                    params += jsonObj.toString() + " ";
+                }
+            }
+        }
+        return params.trim();
+    }
+
+    /**
+     * 判断是否需要过滤的对象。
+     *
+     * @param o 对象信息。
+     * @return 如果是需要过滤的对象,则返回true;否则返回false。
+     */
+    public boolean isFilterObject(final Object o)
+    {
+        return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse;
+    }
+}

+ 117 - 0
fs-app-task-course/src/main/java/com/fs/framework/aspectj/RateLimiterAspect.java

@@ -0,0 +1,117 @@
+package com.fs.framework.aspectj;
+
+import com.fs.common.annotation.RateLimiter;
+import com.fs.common.enums.LimitType;
+import com.fs.common.exception.ServiceException;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.ip.IpUtils;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.script.RedisScript;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * 限流处理
+ *
+
+ */
+@Aspect
+@Component
+public class RateLimiterAspect
+{
+    private static final Logger log = LoggerFactory.getLogger(RateLimiterAspect.class);
+
+    private RedisTemplate<Object, Object> redisTemplate;
+
+    private RedisScript<Long> limitScript;
+
+    @Autowired
+    public void setRedisTemplate1(RedisTemplate<Object, Object> redisTemplate)
+    {
+        this.redisTemplate = redisTemplate;
+    }
+
+    @Autowired
+    public void setLimitScript(RedisScript<Long> limitScript)
+    {
+        this.limitScript = limitScript;
+    }
+
+    // 配置织入点
+    @Pointcut("@annotation(com.fs.common.annotation.RateLimiter)")
+    public void rateLimiterPointCut()
+    {
+    }
+
+    @Before("rateLimiterPointCut()")
+    public void doBefore(JoinPoint point) throws Throwable
+    {
+        RateLimiter rateLimiter = getAnnotationRateLimiter(point);
+        String key = rateLimiter.key();
+        int time = rateLimiter.time();
+        int count = rateLimiter.count();
+
+        String combineKey = getCombineKey(rateLimiter, point);
+        List<Object> keys = Collections.singletonList(combineKey);
+        try
+        {
+            Long number = redisTemplate.execute(limitScript, keys, count, time);
+            if (StringUtils.isNull(number) || number.intValue() > count)
+            {
+                throw new ServiceException("访问过于频繁,请稍后再试");
+            }
+            log.info("限制请求'{}',当前请求'{}',缓存key'{}'", count, number.intValue(), key);
+        }
+        catch (ServiceException e)
+        {
+            throw e;
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("服务器限流异常,请稍后再试");
+        }
+    }
+
+    /**
+     * 是否存在注解,如果存在就获取
+     */
+    private RateLimiter getAnnotationRateLimiter(JoinPoint joinPoint)
+    {
+        Signature signature = joinPoint.getSignature();
+        MethodSignature methodSignature = (MethodSignature) signature;
+        Method method = methodSignature.getMethod();
+
+        if (method != null)
+        {
+            return method.getAnnotation(RateLimiter.class);
+        }
+        return null;
+    }
+
+    public String getCombineKey(RateLimiter rateLimiter, JoinPoint point)
+    {
+        StringBuffer stringBuffer = new StringBuffer(rateLimiter.key());
+        if (rateLimiter.limitType() == LimitType.IP)
+        {
+            stringBuffer.append(IpUtils.getIpAddr(ServletUtils.getRequest()));
+        }
+        MethodSignature signature = (MethodSignature) point.getSignature();
+        Method method = signature.getMethod();
+        Class<?> targetClass = method.getDeclaringClass();
+        stringBuffer.append("-").append(targetClass.getName()).append("- ").append(method.getName());
+        return stringBuffer.toString();
+    }
+}

+ 31 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/ApplicationConfig.java

@@ -0,0 +1,31 @@
+package com.fs.framework.config;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+
+import java.util.TimeZone;
+
+/**
+ * 程序注解配置
+ *
+
+ */
+@Configuration
+// 表示通过aop框架暴露该代理对象,AopContext能够访问
+@EnableAspectJAutoProxy(exposeProxy = true)
+// 指定要扫描的Mapper类的包的路径
+@MapperScan("com.fs.**.mapper")
+public class ApplicationConfig
+{
+    /**
+     * 时区配置
+     */
+    @Bean
+    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization()
+    {
+        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
+    }
+}

+ 58 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/ArrayStringTypeHandler.java

@@ -0,0 +1,58 @@
+package com.fs.framework.config;
+
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+import org.springframework.context.annotation.Configuration;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+
+@Configuration
+public class ArrayStringTypeHandler extends BaseTypeHandler<List<String>> {
+
+    @Override
+    public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
+        // 将 List<String> 转换为字符串,ClickHouse 支持的格式为 "['item1', 'item2']"
+        StringBuilder sb = new StringBuilder();
+        sb.append("[");
+        for (int j = 0; j < parameter.size(); j++) {
+            sb.append("'").append(parameter.get(j)).append("'");
+            if (j < parameter.size() - 1) {
+                sb.append(",");
+            }
+        }
+        sb.append("]");
+        ps.setString(i, sb.toString());
+    }
+
+    @Override
+    public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
+        // 处理查询结果,将其转换为 List<String>
+        String result = rs.getString(columnName);
+        return parseArray(result);
+    }
+
+    @Override
+    public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+        String result = rs.getString(columnIndex);
+        return parseArray(result);
+    }
+
+    @Override
+    public List<String> getNullableResult(java.sql.CallableStatement cs, int columnIndex) throws SQLException {
+        String result = cs.getString(columnIndex);
+        return parseArray(result);
+    }
+
+    private List<String> parseArray(String arrayStr) {
+        // 将 ClickHouse 的 Array 字符串转换为 List<String>
+        if (arrayStr == null || arrayStr.isEmpty()) {
+            return null;
+        }
+        arrayStr = arrayStr.substring(1, arrayStr.length() - 1);  // 去掉 "[" 和 "]"
+        String[] elements = arrayStr.split(",");
+        return java.util.Arrays.asList(elements);
+    }
+}

+ 85 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/CaptchaConfig.java

@@ -0,0 +1,85 @@
+package com.fs.framework.config;
+
+import com.google.code.kaptcha.impl.DefaultKaptcha;
+import com.google.code.kaptcha.util.Config;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.Properties;
+
+import static com.google.code.kaptcha.Constants.*;
+
+/**
+ * 验证码配置
+ *
+
+ */
+@Configuration
+public class CaptchaConfig
+{
+    @Bean(name = "captchaProducer")
+    public DefaultKaptcha getKaptchaBean()
+    {
+        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
+        Properties properties = new Properties();
+        // 是否有边框 默认为true 我们可以自己设置yes,no
+        properties.setProperty(KAPTCHA_BORDER, "yes");
+        // 验证码文本字符颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
+        // 验证码图片宽度 默认为200
+        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
+        // 验证码图片高度 默认为50
+        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
+        // 验证码文本字符大小 默认为40
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
+        // KAPTCHA_SESSION_KEY
+        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
+        // 验证码文本字符长度 默认为5
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
+        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
+        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
+        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
+        Config config = new Config(properties);
+        defaultKaptcha.setConfig(config);
+        return defaultKaptcha;
+    }
+
+    @Bean(name = "captchaProducerMath")
+    public DefaultKaptcha getKaptchaBeanMath()
+    {
+        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
+        Properties properties = new Properties();
+        // 是否有边框 默认为true 我们可以自己设置yes,no
+        properties.setProperty(KAPTCHA_BORDER, "yes");
+        // 边框颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
+        // 验证码文本字符颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
+        // 验证码图片宽度 默认为200
+        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
+        // 验证码图片高度 默认为50
+        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
+        // 验证码文本字符大小 默认为40
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
+        // KAPTCHA_SESSION_KEY
+        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
+        // 验证码文本生成器
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.fs.framework.config.KaptchaTextCreator");
+        // 验证码文本字符间距 默认为2
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
+        // 验证码文本字符长度 默认为5
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
+        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
+        // 验证码噪点颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
+        // 干扰实现类
+        properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
+        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
+        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
+        Config config = new Config(properties);
+        defaultKaptcha.setConfig(config);
+        return defaultKaptcha;
+    }
+}

+ 119 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/DataSourceConfig.java

@@ -0,0 +1,119 @@
+package com.fs.framework.config;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
+import com.alibaba.druid.util.Utils;
+import com.fs.common.enums.DataSourceType;
+import com.fs.framework.datasource.DynamicDataSource;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+
+import javax.servlet.*;
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+@Configuration
+public class DataSourceConfig {
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.sop.druid.master")
+    public DataSource sopDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.sop.druid.read")
+    public DataSource sopReadDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.clickhouse")
+    public DataSource clickhouseDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.mysql.druid.master")
+    public DataSource masterDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.mysql.druid.slave")
+    public DataSource slaveDataSource() {
+        return new DruidDataSource();
+    }
+
+
+
+    @Bean
+    @Primary
+    public DynamicDataSource dataSource(@Qualifier("clickhouseDataSource") DataSource clickhouseDataSource,
+                                        @Qualifier("masterDataSource") DataSource masterDataSource,
+                                        @Qualifier("sopDataSource") DataSource sopDataSource,
+                                        @Qualifier("slaveDataSource") DataSource slaveDataSource,
+                                        @Qualifier("sopReadDataSource") DataSource sopReadDataSource
+                                        ) {
+        Map<Object, Object> targetDataSources = new HashMap<>();
+        targetDataSources.put(DataSourceType.MASTER, masterDataSource);
+
+        targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
+        targetDataSources.put(DataSourceType.SOP.name(), sopDataSource);
+        targetDataSources.put(DataSourceType.SopREAD.name(), sopReadDataSource);
+        targetDataSources.put(DataSourceType.CLICKHOUSE.name(), clickhouseDataSource); // Ensure matching key
+        return new DynamicDataSource(masterDataSource, targetDataSources);
+    }
+
+    /**
+     * 去除监控页面底部的广告
+     */
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Bean
+    @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
+    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
+    {
+        // 获取web监控页面的参数
+        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
+        // 提取common.js的配置路径
+        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
+        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
+        final String filePath = "support/http/resources/js/common.js";
+        // 创建filter进行过滤
+        Filter filter = new Filter()
+        {
+            @Override
+            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
+            {
+            }
+            @Override
+            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+                    throws IOException, ServletException
+            {
+                chain.doFilter(request, response);
+                // 重置缓冲区,响应头不会被重置
+                response.resetBuffer();
+                // 获取common.js
+                String text = Utils.readFromResource(filePath);
+                // 正则替换banner, 除去底部的广告信息
+                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
+                text = text.replaceAll("powered.*?shrek.wang</a>", "");
+                response.getWriter().write(text);
+            }
+            @Override
+            public void destroy()
+            {
+            }
+        };
+        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
+        registrationBean.setFilter(filter);
+        registrationBean.addUrlPatterns(commonJsPattern);
+        return registrationBean;
+    }
+}

+ 123 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/DruidConfig.java

@@ -0,0 +1,123 @@
+package com.fs.framework.config;//package com.fs.framework.config;
+//
+//import com.alibaba.druid.pool.DruidDataSource;
+//import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
+//import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
+//import com.alibaba.druid.util.Utils;
+//import com.fs.framework.datasource.DynamicDataSource;
+//import com.fs.common.enums.DataSourceType;
+//import com.fs.common.utils.spring.SpringUtils;
+//import com.fs.framework.config.properties.DruidProperties;
+//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+//import org.springframework.boot.context.properties.ConfigurationProperties;
+//import org.springframework.boot.web.servlet.FilterRegistrationBean;
+//import org.springframework.context.annotation.Bean;
+//import org.springframework.context.annotation.Configuration;
+//import org.springframework.context.annotation.Primary;
+//
+//import javax.servlet.*;
+//import javax.sql.DataSource;
+//import java.io.IOException;
+//import java.util.HashMap;
+//import java.util.Map;
+//
+///**
+// * druid 配置多数据源
+// *
+//
+// */
+//@Configuration
+//public class DruidConfig
+//{
+//    @Bean
+//    @ConfigurationProperties("spring.datasource.druid.master")
+//    public DataSource masterDataSource(DruidProperties druidProperties)
+//    {
+//        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
+//        return druidProperties.dataSource(dataSource);
+//    }
+//
+//    @Bean
+//    @ConfigurationProperties("spring.datasource.druid.slave")
+//    @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
+//    public DataSource slaveDataSource(DruidProperties druidProperties)
+//    {
+//        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
+//        return druidProperties.dataSource(dataSource);
+//    }
+//
+//    @Bean(name = "dynamicDataSource")
+//    @Primary
+//    public DynamicDataSource dataSource(DataSource masterDataSource)
+//    {
+//        Map<Object, Object> targetDataSources = new HashMap<>();
+//        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
+//        setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
+//        return new DynamicDataSource(masterDataSource, targetDataSources);
+//    }
+//
+//    /**
+//     * 设置数据源
+//     *
+//     * @param targetDataSources 备选数据源集合
+//     * @param sourceName 数据源名称
+//     * @param beanName bean名称
+//     */
+//    public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
+//    {
+//        try
+//        {
+//            DataSource dataSource = SpringUtils.getBean(beanName);
+//            targetDataSources.put(sourceName, dataSource);
+//        }
+//        catch (Exception e)
+//        {
+//        }
+//    }
+//
+//    /**
+//     * 去除监控页面底部的广告
+//     */
+//    @SuppressWarnings({ "rawtypes", "unchecked" })
+//    @Bean
+//    @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
+//    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
+//    {
+//        // 获取web监控页面的参数
+//        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
+//        // 提取common.js的配置路径
+//        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
+//        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
+//        final String filePath = "support/http/resources/js/common.js";
+//        // 创建filter进行过滤
+//        Filter filter = new Filter()
+//        {
+//            @Override
+//            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
+//            {
+//            }
+//            @Override
+//            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+//                    throws IOException, ServletException
+//            {
+//                chain.doFilter(request, response);
+//                // 重置缓冲区,响应头不会被重置
+//                response.resetBuffer();
+//                // 获取common.js
+//                String text = Utils.readFromResource(filePath);
+//                // 正则替换banner, 除去底部的广告信息
+//                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
+//                text = text.replaceAll("powered.*?shrek.wang</a>", "");
+//                response.getWriter().write(text);
+//            }
+//            @Override
+//            public void destroy()
+//            {
+//            }
+//        };
+//        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
+//        registrationBean.setFilter(filter);
+//        registrationBean.addUrlPatterns(commonJsPattern);
+//        return registrationBean;
+//    }
+//}

+ 72 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/FastJson2JsonRedisSerializer.java

@@ -0,0 +1,72 @@
+package com.fs.framework.config;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.parser.ParserConfig;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.data.redis.serializer.SerializationException;
+import org.springframework.util.Assert;
+
+import java.nio.charset.Charset;
+
+/**
+ * Redis使用FastJson序列化
+ *
+
+ */
+public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
+{
+    @SuppressWarnings("unused")
+    private ObjectMapper objectMapper = new ObjectMapper();
+
+    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
+
+    private Class<T> clazz;
+
+    static
+    {
+        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
+    }
+
+    public FastJson2JsonRedisSerializer(Class<T> clazz)
+    {
+        super();
+        this.clazz = clazz;
+    }
+
+    @Override
+    public byte[] serialize(T t) throws SerializationException
+    {
+        if (t == null)
+        {
+            return new byte[0];
+        }
+        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
+    }
+
+    @Override
+    public T deserialize(byte[] bytes) throws SerializationException
+    {
+        if (bytes == null || bytes.length <= 0)
+        {
+            return null;
+        }
+        String str = new String(bytes, DEFAULT_CHARSET);
+
+        return JSON.parseObject(str, clazz);
+    }
+
+    public void setObjectMapper(ObjectMapper objectMapper)
+    {
+        Assert.notNull(objectMapper, "'objectMapper' must not be null");
+        this.objectMapper = objectMapper;
+    }
+
+    protected JavaType getJavaType(Class<?> clazz)
+    {
+        return TypeFactory.defaultInstance().constructType(clazz);
+    }
+}

+ 59 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/FilterConfig.java

@@ -0,0 +1,59 @@
+package com.fs.framework.config;
+
+import com.fs.common.filter.RepeatableFilter;
+import com.fs.common.filter.XssFilter;
+import com.fs.common.utils.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.servlet.DispatcherType;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Filter配置
+ *
+
+ */
+@Configuration
+@ConditionalOnProperty(value = "xss.enabled", havingValue = "true")
+public class FilterConfig
+{
+    @Value("${xss.excludes}")
+    private String excludes;
+
+    @Value("${xss.urlPatterns}")
+    private String urlPatterns;
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Bean
+    public FilterRegistrationBean xssFilterRegistration()
+    {
+        FilterRegistrationBean registration = new FilterRegistrationBean();
+        registration.setDispatcherTypes(DispatcherType.REQUEST);
+        registration.setFilter(new XssFilter());
+        registration.addUrlPatterns(StringUtils.split(urlPatterns, ","));
+        registration.setName("xssFilter");
+        registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
+        Map<String, String> initParameters = new HashMap<String, String>();
+        initParameters.put("excludes", excludes);
+        registration.setInitParameters(initParameters);
+        return registration;
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Bean
+    public FilterRegistrationBean someFilterRegistration()
+    {
+        FilterRegistrationBean registration = new FilterRegistrationBean();
+        registration.setFilter(new RepeatableFilter());
+        registration.addUrlPatterns("/*");
+        registration.setName("repeatableFilter");
+        registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
+        return registration;
+    }
+
+}

+ 76 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/KaptchaTextCreator.java

@@ -0,0 +1,76 @@
+package com.fs.framework.config;
+
+import com.google.code.kaptcha.text.impl.DefaultTextCreator;
+
+import java.util.Random;
+
+/**
+ * 验证码文本生成器
+ *
+
+ */
+public class KaptchaTextCreator extends DefaultTextCreator
+{
+    private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
+
+    @Override
+    public String getText()
+    {
+        Integer result = 0;
+        Random random = new Random();
+        int x = random.nextInt(10);
+        int y = random.nextInt(10);
+        StringBuilder suChinese = new StringBuilder();
+        int randomoperands = (int) Math.round(Math.random() * 2);
+        if (randomoperands == 0)
+        {
+            result = x * y;
+            suChinese.append(CNUMBERS[x]);
+            suChinese.append("*");
+            suChinese.append(CNUMBERS[y]);
+        }
+        else if (randomoperands == 1)
+        {
+            if (!(x == 0) && y % x == 0)
+            {
+                result = y / x;
+                suChinese.append(CNUMBERS[y]);
+                suChinese.append("/");
+                suChinese.append(CNUMBERS[x]);
+            }
+            else
+            {
+                result = x + y;
+                suChinese.append(CNUMBERS[x]);
+                suChinese.append("+");
+                suChinese.append(CNUMBERS[y]);
+            }
+        }
+        else if (randomoperands == 2)
+        {
+            if (x >= y)
+            {
+                result = x - y;
+                suChinese.append(CNUMBERS[x]);
+                suChinese.append("-");
+                suChinese.append(CNUMBERS[y]);
+            }
+            else
+            {
+                result = y - x;
+                suChinese.append(CNUMBERS[y]);
+                suChinese.append("-");
+                suChinese.append(CNUMBERS[x]);
+            }
+        }
+        else
+        {
+            result = x + y;
+            suChinese.append(CNUMBERS[x]);
+            suChinese.append("+");
+            suChinese.append(CNUMBERS[y]);
+        }
+        suChinese.append("=?@" + result);
+        return suChinese.toString();
+    }
+}

+ 150 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/MyBatisConfig.java

@@ -0,0 +1,150 @@
+package com.fs.framework.config;
+
+import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
+import com.fs.common.utils.StringUtils;
+import org.apache.ibatis.io.VFS;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+import org.springframework.core.io.DefaultResourceLoader;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.core.io.support.ResourcePatternResolver;
+import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
+import org.springframework.core.type.classreading.MetadataReader;
+import org.springframework.core.type.classreading.MetadataReaderFactory;
+import org.springframework.util.ClassUtils;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+/**
+ * Mybatis支持*匹配扫描包
+ *
+
+ */
+@Configuration
+public class MyBatisConfig
+{
+    @Autowired
+    private Environment env;
+
+    static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
+
+    public static String setTypeAliasesPackage(String typeAliasesPackage)
+    {
+        ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
+        MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
+        List<String> allResult = new ArrayList<String>();
+        try
+        {
+            for (String aliasesPackage : typeAliasesPackage.split(","))
+            {
+                List<String> result = new ArrayList<String>();
+                aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+                        + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
+                Resource[] resources = resolver.getResources(aliasesPackage);
+                if (resources != null && resources.length > 0)
+                {
+                    MetadataReader metadataReader = null;
+                    for (Resource resource : resources)
+                    {
+                        if (resource.isReadable())
+                        {
+                            metadataReader = metadataReaderFactory.getMetadataReader(resource);
+                            try
+                            {
+                                result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
+                            }
+                            catch (ClassNotFoundException e)
+                            {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                }
+                if (result.size() > 0)
+                {
+                    HashSet<String> hashResult = new HashSet<String>(result);
+                    allResult.addAll(hashResult);
+                }
+            }
+            if (allResult.size() > 0)
+            {
+                typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0]));
+            }
+            else
+            {
+                throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
+            }
+        }
+        catch (IOException e)
+        {
+            e.printStackTrace();
+        }
+        return typeAliasesPackage;
+    }
+
+    public Resource[] resolveMapperLocations(String[] mapperLocations)
+    {
+        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
+        List<Resource> resources = new ArrayList<Resource>();
+        if (mapperLocations != null)
+        {
+            for (String mapperLocation : mapperLocations)
+            {
+                try
+                {
+                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
+                    resources.addAll(Arrays.asList(mappers));
+                }
+                catch (IOException e)
+                {
+                    // ignore
+                }
+            }
+        }
+        return resources.toArray(new Resource[resources.size()]);
+    }
+
+//    @Bean
+//    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
+//    {
+//        String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
+//        String mapperLocations = env.getProperty("mybatis.mapperLocations");
+//        String configLocation = env.getProperty("mybatis.configLocation");
+//        typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
+//        VFS.addImplClass(SpringBootVFS.class);
+//
+//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
+//        sessionFactory.setDataSource(dataSource);
+//        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
+//        sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
+//        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
+//        return sessionFactory.getObject();
+//    }
+    @Bean
+    public SqlSessionFactory sqlSessionFactorys(DataSource dataSource) throws Exception
+    {
+        String typeAliasesPackage = env.getProperty("mybatis-plus.typeAliasesPackage");
+        String mapperLocations = env.getProperty("mybatis-plus.mapperLocations");
+        String configLocation = env.getProperty("mybatis-plus.configLocation");
+        typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
+        VFS.addImplClass(SpringBootVFS.class);
+
+        final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
+        sessionFactory.setDataSource(dataSource);
+        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
+        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
+        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
+        return sessionFactory.getObject();
+    }
+}

+ 153 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/RedisConfig.java

@@ -0,0 +1,153 @@
+package com.fs.framework.config;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
+import org.springframework.cache.annotation.CachingConfigurerSupport;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.script.DefaultRedisScript;
+import org.springframework.data.redis.serializer.GenericToStringSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.math.BigDecimal;
+
+/**
+ * redis配置
+ *
+
+ */
+@Configuration
+@EnableCaching
+public class RedisConfig extends CachingConfigurerSupport
+{
+    @Bean
+    @SuppressWarnings(value = { "unchecked", "rawtypes" })
+    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
+    {
+        RedisTemplate<Object, Object> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
+
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
+        serializer.setObjectMapper(mapper);
+
+        // 使用StringRedisSerializer来序列化和反序列化redis的key值
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(serializer);
+
+        // Hash的key也采用StringRedisSerializer的序列化方式
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(serializer);
+
+        template.afterPropertiesSet();
+        return template;
+    }
+    @Bean
+    public RedisTemplate<String, Boolean> redisTemplateForBoolean(RedisConnectionFactory connectionFactory) {
+        RedisTemplate<String, Boolean> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        // 使用StringRedisSerializer来序列化和反序列化redis的key值
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(new GenericToStringSerializer<>(Boolean.class));
+
+        // Hash的key也采用StringRedisSerializer的序列化方式
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(new GenericToStringSerializer<>(Boolean.class));
+
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    @Bean(name = "redisTemplateForInteger")
+    public RedisTemplate<String, Integer> redisTemplateForInteger(RedisConnectionFactory connectionFactory) {
+        RedisTemplate<String, Integer> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(new GenericToStringSerializer<>(Integer.class));
+
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(new GenericToStringSerializer<>(Integer.class));
+
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    @Bean(name = "redisTemplateForBigDecimal")
+    public RedisTemplate<String, BigDecimal> redisTemplateForBigDecimal(RedisConnectionFactory connectionFactory) {
+        RedisTemplate<String, BigDecimal> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(new GenericToStringSerializer<>(BigDecimal.class));
+
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(new GenericToStringSerializer<>(BigDecimal.class));
+
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    @Bean
+    @SuppressWarnings(value = { "unchecked", "rawtypes" })
+    public RedisTemplate<String, Object> redisTemplateForObject(RedisConnectionFactory connectionFactory) {
+        RedisTemplate<String, Object> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
+
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
+        serializer.setObjectMapper(mapper);
+
+        // 使用StringRedisSerializer来序列化和反序列化redis的key值
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(serializer);
+
+        // Hash的key也采用StringRedisSerializer的序列化方式
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(serializer);
+
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    @Bean
+    public DefaultRedisScript<Long> limitScript()
+    {
+        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
+        redisScript.setScriptText(limitScriptText());
+        redisScript.setResultType(Long.class);
+        return redisScript;
+    }
+
+    /**
+     * 限流脚本
+     */
+    private String limitScriptText()
+    {
+        return "local key = KEYS[1]\n" +
+                "local count = tonumber(ARGV[1])\n" +
+                "local time = tonumber(ARGV[2])\n" +
+                "local current = redis.call('get', key);\n" +
+                "if current and tonumber(current) > count then\n" +
+                "    return current;\n" +
+                "end\n" +
+                "current = redis.call('incr', key)\n" +
+                "if tonumber(current) == 1 then\n" +
+                "    redis.call('expire', key, time)\n" +
+                "end\n" +
+                "return current;";
+    }
+}

+ 76 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/ResourcesConfig.java

@@ -0,0 +1,76 @@
+package com.fs.framework.config;
+
+import com.fs.common.config.FSConfig;
+import com.fs.common.constant.Constants;
+import com.fs.framework.interceptor.RepeatSubmitInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.format.FormatterRegistry;
+import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+import java.time.format.DateTimeFormatter;
+
+/**
+ * 通用配置
+ *
+
+ */
+@Configuration
+public class ResourcesConfig implements WebMvcConfigurer
+{
+    @Autowired
+    private RepeatSubmitInterceptor repeatSubmitInterceptor;
+
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry)
+    {
+        /** 本地文件上传路径 */
+        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + FSConfig.getProfile() + "/");
+
+        /** swagger配置 */
+        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
+    }
+
+    /**
+     * 自定义拦截规则
+     */
+    @Override
+    public void addInterceptors(InterceptorRegistry registry)
+    {
+        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
+    }
+
+    /**
+     * 跨域配置
+     */
+    @Bean
+    public CorsFilter corsFilter()
+    {
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        CorsConfiguration config = new CorsConfiguration();
+        config.setAllowCredentials(true);
+        // 设置访问源地址
+        config.addAllowedOrigin("*");
+        // 设置访问源请求头
+        config.addAllowedHeader("*");
+        // 设置访问源请求方法
+        config.addAllowedMethod("*");
+        // 对接口配置跨域设置
+        source.registerCorsConfiguration("/**", config);
+        return new CorsFilter(source);
+    }
+
+    @Override
+    public void addFormatters(FormatterRegistry registry) {
+        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
+        registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 统一日期格式
+        registrar.registerFormatters(registry);
+    }
+}

+ 11 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/RetryConfig.java

@@ -0,0 +1,11 @@
+package com.fs.framework.config;
+
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.retry.annotation.EnableRetry;
+
+@Configuration
+@EnableRetry
+public class RetryConfig {
+
+}

+ 158 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/SecurityConfig.java

@@ -0,0 +1,158 @@
+package com.fs.framework.config;
+
+
+import com.fs.framework.security.filter.JwtAuthenticationTokenFilter;
+import com.fs.framework.security.handle.AuthenticationEntryPointImpl;
+import com.fs.framework.security.handle.LogoutSuccessHandlerImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.http.HttpMethod;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.config.http.SessionCreationPolicy;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
+import org.springframework.security.web.authentication.logout.LogoutFilter;
+import org.springframework.web.filter.CorsFilter;
+
+/**
+ * spring security配置
+ *
+
+ */
+@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
+public class SecurityConfig extends WebSecurityConfigurerAdapter
+{
+    /**
+     * 自定义用户认证逻辑
+     */
+    @Autowired
+    private UserDetailsService userDetailsService;
+
+    /**
+     * 认证失败处理类
+     */
+    @Autowired
+    private AuthenticationEntryPointImpl unauthorizedHandler;
+
+    /**
+     * 退出处理类
+     */
+    @Autowired
+    private LogoutSuccessHandlerImpl logoutSuccessHandler;
+
+    /**
+     * token认证过滤器
+     */
+    @Autowired
+    private JwtAuthenticationTokenFilter authenticationTokenFilter;
+
+    /**
+     * 跨域过滤器
+     */
+    @Autowired
+    private CorsFilter corsFilter;
+
+    /**
+     * 解决 无法直接注入 AuthenticationManager
+     *
+     * @return
+     * @throws Exception
+     */
+    @Bean
+    @Override
+    public AuthenticationManager authenticationManagerBean() throws Exception
+    {
+        return super.authenticationManagerBean();
+    }
+
+    /**
+     * anyRequest          |   匹配所有请求路径
+     * access              |   SpringEl表达式结果为true时可以访问
+     * anonymous           |   匿名可以访问
+     * denyAll             |   用户不能访问
+     * fullyAuthenticated  |   用户完全认证可以访问(非remember-me下自动登录)
+     * hasAnyAuthority     |   如果有参数,参数表示权限,则其中任何一个权限可以访问
+     * hasAnyRole          |   如果有参数,参数表示角色,则其中任何一个角色可以访问
+     * hasAuthority        |   如果有参数,参数表示权限,则其权限可以访问
+     * hasIpAddress        |   如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问
+     * hasRole             |   如果有参数,参数表示角色,则其角色可以访问
+     * permitAll           |   用户可以任意访问
+     * rememberMe          |   允许通过remember-me登录的用户访问
+     * authenticated       |   用户登录后可访问
+     */
+    @Override
+    protected void configure(HttpSecurity httpSecurity) throws Exception
+    {
+        httpSecurity
+                // CSRF禁用,因为不使用session
+                .csrf().disable()
+                // 认证失败处理类
+                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
+                // 基于token,所以不需要session
+                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
+                // 过滤请求
+                .authorizeRequests()
+                // 对于登录login 注册register 验证码captchaImage 允许匿名访问
+                .antMatchers("/chat/upload/**","/login", "/register", "/captchaImage").anonymous()
+                .antMatchers(
+                        HttpMethod.GET,
+                        "/",
+                        "/*.html",
+                        "/**/*.html",
+                        "/**/*.css",
+                        "/**/*.js",
+                        "/profile/**"
+                ).permitAll()
+                .antMatchers("**/errorLogUpload").anonymous()
+                .antMatchers("**").anonymous()
+                .antMatchers("/qw/getJsapiTicket/**").anonymous()
+                .antMatchers("/msg/**").anonymous()
+                .antMatchers("/msg/**/**").anonymous()
+                .antMatchers("/msg").anonymous()
+                .antMatchers("/app/common/**").anonymous()
+                .antMatchers("/common/getId**").anonymous()
+                .antMatchers("/common/uploadOSS**").anonymous()
+                .antMatchers("/common/uploadWang**").anonymous()
+                .antMatchers("/common/download**").anonymous()
+                .antMatchers("/common/download/resource**").anonymous()
+                .antMatchers("/swagger-ui.html").anonymous()
+                .antMatchers("/swagger-resources/**").anonymous()
+                .antMatchers("/webjars/**").anonymous()
+                .antMatchers("/*/api-docs").anonymous()
+                .antMatchers("/druid/**").anonymous()
+                .antMatchers("/qw/data/**").anonymous()
+                // 除上面外的所有请求全部需要鉴权认证
+                .anyRequest().authenticated()
+                .and()
+                .headers().frameOptions().disable();
+        httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
+        // 添加JWT filter
+        httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
+        // 添加CORS filter
+        httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
+        httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
+    }
+
+    /**
+     * 强散列哈希加密实现
+     */
+    @Bean
+    public BCryptPasswordEncoder bCryptPasswordEncoder()
+    {
+        return new BCryptPasswordEncoder();
+    }
+
+    /**
+     * 身份认证接口
+     */
+    @Override
+    protected void configure(AuthenticationManagerBuilder auth) throws Exception
+    {
+        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
+    }
+}

+ 33 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/ServerConfig.java

@@ -0,0 +1,33 @@
+package com.fs.framework.config;
+
+import com.fs.common.utils.ServletUtils;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 服务相关配置
+ *
+
+ */
+@Component
+public class ServerConfig
+{
+    /**
+     * 获取完整的请求路径,包括:域名,端口,上下文访问路径
+     *
+     * @return 服务地址
+     */
+    public String getUrl()
+    {
+        HttpServletRequest request = ServletUtils.getRequest();
+        return getDomain(request);
+    }
+
+    public static String getDomain(HttpServletRequest request)
+    {
+        StringBuffer url = request.getRequestURL();
+        String contextPath = request.getServletContext().getContextPath();
+        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
+    }
+}

+ 121 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/SwaggerConfig.java

@@ -0,0 +1,121 @@
+package com.fs.framework.config;
+
+import com.fs.common.config.FSConfig;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.models.auth.In;
+import org.springframework.beans.factory.annotation.Autowired;
+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 java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Swagger2的接口配置
+ *
+
+ */
+@Configuration
+public class SwaggerConfig
+{
+    /** 系统基础配置 */
+    @Autowired
+    private FSConfig fsConfig;
+
+    /** 是否开启swagger */
+    @Value("${swagger.enabled}")
+    private boolean enabled;
+
+    /** 设置请求的统一前缀 */
+    @Value("${swagger.pathMapping}")
+    private String pathMapping;
+
+    /**
+     * 创建API
+     */
+    @Bean
+    public Docket createRestApi()
+    {
+        return new Docket(DocumentationType.SWAGGER_2)
+                // 是否启用Swagger
+                .enable(enabled)
+                // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
+                .apiInfo(apiInfo())
+                // 设置哪些接口暴露给Swagger展示
+                .select()
+                // 扫描所有有注解的api,用这种方式更灵活
+                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
+                // 扫描指定包中的swagger注解
+                // .apis(RequestHandlerSelectors.basePackage("com.fs.project.tool.swagger"))
+                // 扫描所有 .apis(RequestHandlerSelectors.any())
+                .paths(PathSelectors.any())
+                .build()
+                /* 设置安全模式,swagger可以设置访问token */
+                .securitySchemes(securitySchemes())
+                .securityContexts(securityContexts())
+                .pathMapping(pathMapping);
+    }
+
+    /**
+     * 安全模式,这里指定token通过Authorization头请求头传递
+     */
+    private List<ApiKey> securitySchemes()
+    {
+        List<ApiKey> apiKeyList = new ArrayList<ApiKey>();
+        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
+        return apiKeyList;
+    }
+
+    /**
+     * 安全上下文
+     */
+    private List<SecurityContext> securityContexts()
+    {
+        List<SecurityContext> securityContexts = new ArrayList<>();
+        securityContexts.add(
+                SecurityContext.builder()
+                        .securityReferences(defaultAuth())
+                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
+                        .build());
+        return securityContexts;
+    }
+
+    /**
+     * 默认的安全上引用
+     */
+    private List<SecurityReference> defaultAuth()
+    {
+        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
+        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
+        authorizationScopes[0] = authorizationScope;
+        List<SecurityReference> securityReferences = new ArrayList<>();
+        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
+        return securityReferences;
+    }
+
+    /**
+     * 添加摘要信息
+     */
+    private ApiInfo apiInfo()
+    {
+        // 用ApiInfoBuilder进行定制
+        return new ApiInfoBuilder()
+                // 设置标题
+                .title("标题:FS管理系统_接口文档")
+                // 描述
+                .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
+                // 作者信息
+                .contact(new Contact(fsConfig.getName(), null, null))
+                // 版本
+                .version("版本号:" + fsConfig.getVersion())
+                .build();
+    }
+}

+ 102 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/ThreadPoolConfig.java

@@ -0,0 +1,102 @@
+package com.fs.framework.config;
+
+import com.fs.common.utils.Threads;
+import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+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.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * 线程池配置
+ *
+
+ **/
+@Configuration
+@EnableAsync
+public class ThreadPoolConfig
+{
+    // 核心线程池大小
+    private int corePoolSize = 50;
+
+    // 最大可创建的线程数
+    private int maxPoolSize = 300;
+
+    // 队列最大长度
+    private int queueCapacity = 1000;
+
+    // 线程池维护线程所允许的空闲时间
+    private int keepAliveSeconds = 300;
+
+    @Bean(name = "threadPoolTaskExecutor")
+    public ThreadPoolTaskExecutor threadPoolTaskExecutor()
+    {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setMaxPoolSize(maxPoolSize);
+        executor.setCorePoolSize(corePoolSize);
+        executor.setQueueCapacity(queueCapacity);
+        executor.setKeepAliveSeconds(keepAliveSeconds);
+        // 线程池对拒绝任务(无线程可用)的处理策略
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        return executor;
+    }
+
+    /**
+     * 执行周期性或定时任务
+     */
+    @Bean(name = "scheduledExecutorService")
+    protected ScheduledExecutorService scheduledExecutorService()
+    {
+        return new ScheduledThreadPoolExecutor(corePoolSize,
+                new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build())
+        {
+            @Override
+            protected void afterExecute(Runnable r, Throwable t)
+            {
+                super.afterExecute(r, t);
+                Threads.printException(r, t);
+            }
+        };
+    }
+
+    @Bean(name = "sopTaskExecutor")
+    public Executor sopTaskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(16); // 根据需求调整
+        executor.setMaxPoolSize(32);  // 根据需求调整
+        executor.setQueueCapacity(800); // 根据需求调整
+        executor.setThreadNamePrefix("SopTask-");
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+
+    @Bean(name = "sopChatTaskExecutor")
+    public Executor batchInsertExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(16);
+        executor.setMaxPoolSize(32);
+        executor.setQueueCapacity(800);
+        executor.setThreadNamePrefix("BatchInsert-");
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+
+    @Bean(name = "sopRatingExecutor")
+    public Executor sopRatingExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(16); // 根据需求调整
+        executor.setMaxPoolSize(32);  // 根据需求调整
+        executor.setQueueCapacity(800); // 根据需求调整
+        executor.setThreadNamePrefix("SopRating-");
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+}

+ 77 - 0
fs-app-task-course/src/main/java/com/fs/framework/config/properties/DruidProperties.java

@@ -0,0 +1,77 @@
+package com.fs.framework.config.properties;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * druid 配置属性
+ *
+
+ */
+@Configuration
+public class DruidProperties
+{
+    @Value("${spring.datasource.mysql.druid.initialSize}")
+    private int initialSize;
+
+    @Value("${spring.datasource.mysql.druid.minIdle}")
+    private int minIdle;
+
+    @Value("${spring.datasource.mysql.druid.maxActive}")
+    private int maxActive;
+
+    @Value("${spring.datasource.mysql.druid.maxWait}")
+    private int maxWait;
+
+    @Value("${spring.datasource.mysql.druid.timeBetweenEvictionRunsMillis}")
+    private int timeBetweenEvictionRunsMillis;
+
+    @Value("${spring.datasource.mysql.druid.minEvictableIdleTimeMillis}")
+    private int minEvictableIdleTimeMillis;
+
+    @Value("${spring.datasource.mysql.druid.maxEvictableIdleTimeMillis}")
+    private int maxEvictableIdleTimeMillis;
+
+    @Value("${spring.datasource.mysql.druid.validationQuery}")
+    private String validationQuery;
+
+    @Value("${spring.datasource.mysql.druid.testWhileIdle}")
+    private boolean testWhileIdle;
+
+    @Value("${spring.datasource.mysql.druid.testOnBorrow}")
+    private boolean testOnBorrow;
+
+    @Value("${spring.datasource.mysql.druid.testOnReturn}")
+    private boolean testOnReturn;
+
+    public DruidDataSource dataSource(DruidDataSource datasource)
+    {
+        /** 配置初始化大小、最小、最大 */
+        datasource.setInitialSize(initialSize);
+        datasource.setMaxActive(maxActive);
+        datasource.setMinIdle(minIdle);
+
+        /** 配置获取连接等待超时的时间 */
+        datasource.setMaxWait(maxWait);
+
+        /** 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 */
+        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
+
+        /** 配置一个连接在池中最小、最大生存的时间,单位是毫秒 */
+        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
+        datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis);
+
+        /**
+         * 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
+         */
+        datasource.setValidationQuery(validationQuery);
+        /** 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 */
+        datasource.setTestWhileIdle(testWhileIdle);
+        /** 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */
+        datasource.setTestOnBorrow(testOnBorrow);
+        /** 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */
+        datasource.setTestOnReturn(testOnReturn);
+        return datasource;
+    }
+}

+ 27 - 0
fs-app-task-course/src/main/java/com/fs/framework/datasource/DynamicDataSource.java

@@ -0,0 +1,27 @@
+package com.fs.framework.datasource;
+
+import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
+
+import javax.sql.DataSource;
+import java.util.Map;
+
+/**
+ * 动态数据源
+ *
+
+ */
+public class DynamicDataSource extends AbstractRoutingDataSource
+{
+    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources)
+    {
+        super.setDefaultTargetDataSource(defaultTargetDataSource);
+        super.setTargetDataSources(targetDataSources);
+        super.afterPropertiesSet();
+    }
+
+    @Override
+    protected Object determineCurrentLookupKey()
+    {
+        return DynamicDataSourceContextHolder.getDataSourceType();
+    }
+}

+ 45 - 0
fs-app-task-course/src/main/java/com/fs/framework/datasource/DynamicDataSourceContextHolder.java

@@ -0,0 +1,45 @@
+package com.fs.framework.datasource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 数据源切换处理
+ *
+
+ */
+public class DynamicDataSourceContextHolder
+{
+    public static final Logger log = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
+
+    /**
+     * 使用ThreadLocal维护变量,ThreadLocal为每个使用该变量的线程提供独立的变量副本,
+     *  所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。
+     */
+    private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
+
+    /**
+     * 设置数据源的变量
+     */
+    public static void setDataSourceType(String dsType)
+    {
+//        log.info("切换到{}数据源", dsType);
+        CONTEXT_HOLDER.set(dsType);
+    }
+
+    /**
+     * 获得数据源的变量
+     */
+    public static String getDataSourceType()
+    {
+        return CONTEXT_HOLDER.get();
+    }
+
+    /**
+     * 清空数据源变量
+     */
+    public static void clearDataSourceType()
+    {
+        CONTEXT_HOLDER.remove();
+    }
+}

+ 115 - 0
fs-app-task-course/src/main/java/com/fs/framework/exception/GlobalExceptionHandler.java

@@ -0,0 +1,115 @@
+package com.fs.framework.exception;
+
+import com.fs.common.constant.HttpStatus;
+import com.fs.common.core.domain.AjaxResult;
+import com.fs.common.exception.DemoModeException;
+import com.fs.common.exception.ServiceException;
+import com.fs.common.utils.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.security.access.AccessDeniedException;
+import org.springframework.validation.BindException;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 全局异常处理器
+ *
+
+ */
+@RestControllerAdvice
+public class GlobalExceptionHandler
+{
+    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
+
+    /**
+     * 权限校验异常
+     */
+    @ExceptionHandler(AccessDeniedException.class)
+    public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request)
+    {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
+        return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
+    }
+
+    /**
+     * 请求方式不支持
+     */
+    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
+    public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
+            HttpServletRequest request)
+    {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
+        return AjaxResult.error(e.getMessage());
+    }
+
+    /**
+     * 业务异常
+     */
+    @ExceptionHandler(ServiceException.class)
+    public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
+    {
+        log.error(e.getMessage(), e);
+        Integer code = e.getCode();
+        return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
+    }
+
+    /**
+     * 拦截未知的运行时异常
+     */
+    @ExceptionHandler(RuntimeException.class)
+    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
+    {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',发生未知异常.", requestURI, e);
+        return AjaxResult.error(e.getMessage());
+    }
+
+    /**
+     * 系统异常
+     */
+    @ExceptionHandler(Exception.class)
+    public AjaxResult handleException(Exception e, HttpServletRequest request)
+    {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',发生系统异常.", requestURI, e);
+        return AjaxResult.error(e.getMessage());
+    }
+
+    /**
+     * 自定义验证异常
+     */
+    @ExceptionHandler(BindException.class)
+    public AjaxResult handleBindException(BindException e)
+    {
+        log.error(e.getMessage(), e);
+        String message = e.getAllErrors().get(0).getDefaultMessage();
+        return AjaxResult.error(message);
+    }
+
+    /**
+     * 自定义验证异常
+     */
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
+    {
+        log.error(e.getMessage(), e);
+        String message = e.getBindingResult().getFieldError().getDefaultMessage();
+        return AjaxResult.error(message);
+    }
+
+    /**
+     * 演示模式异常
+     */
+    @ExceptionHandler(DemoModeException.class)
+    public AjaxResult handleDemoModeException(DemoModeException e)
+    {
+        return AjaxResult.error("演示模式,不允许操作");
+    }
+}

+ 56 - 0
fs-app-task-course/src/main/java/com/fs/framework/interceptor/RepeatSubmitInterceptor.java

@@ -0,0 +1,56 @@
+package com.fs.framework.interceptor;
+
+import com.alibaba.fastjson.JSONObject;
+import com.fs.common.annotation.RepeatSubmit;
+import com.fs.common.core.domain.AjaxResult;
+import com.fs.common.utils.ServletUtils;
+import org.springframework.stereotype.Component;
+import org.springframework.web.method.HandlerMethod;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.lang.reflect.Method;
+
+/**
+ * 防止重复提交拦截器
+ *
+
+ */
+@Component
+public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
+{
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
+    {
+        if (handler instanceof HandlerMethod)
+        {
+            HandlerMethod handlerMethod = (HandlerMethod) handler;
+            Method method = handlerMethod.getMethod();
+            RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
+            if (annotation != null)
+            {
+                if (this.isRepeatSubmit(request))
+                {
+                    AjaxResult ajaxResult = AjaxResult.error("不允许重复提交,请稍后再试");
+                    ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
+                    return false;
+                }
+            }
+            return true;
+        }
+        else
+        {
+            return super.preHandle(request, response, handler);
+        }
+    }
+
+    /**
+     * 验证是否重复提交由子类实现具体的防重复提交的规则
+     *
+     * @param request
+     * @return
+     * @throws Exception
+     */
+    public abstract boolean isRepeatSubmit(HttpServletRequest request);
+}

+ 126 - 0
fs-app-task-course/src/main/java/com/fs/framework/interceptor/impl/SameUrlDataInterceptor.java

@@ -0,0 +1,126 @@
+package com.fs.framework.interceptor.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.fs.common.constant.Constants;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.filter.RepeatedlyRequestWrapper;
+import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.http.HttpHelper;
+import com.fs.framework.interceptor.RepeatSubmitInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 判断请求url和数据是否和上一次相同,
+ * 如果和上次相同,则是重复提交表单。 有效时间为10秒内。
+ *
+
+ */
+@Component
+public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
+{
+    public final String REPEAT_PARAMS = "repeatParams";
+
+    public final String REPEAT_TIME = "repeatTime";
+
+    // 令牌自定义标识
+    @Value("${token.header}")
+    private String header;
+
+    @Autowired
+    private RedisCache redisCache;
+
+    /**
+     * 间隔时间,单位:秒 默认10秒
+     *
+     * 两次相同参数的请求,如果间隔时间大于该参数,系统不会认定为重复提交的数据
+     */
+    private int intervalTime = 10;
+
+    public void setIntervalTime(int intervalTime)
+    {
+        this.intervalTime = intervalTime;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public boolean isRepeatSubmit(HttpServletRequest request)
+    {
+        String nowParams = "";
+        if (request instanceof RepeatedlyRequestWrapper)
+        {
+            RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request;
+            nowParams = HttpHelper.getBodyString(repeatedlyRequest);
+        }
+
+        // body参数为空,获取Parameter的数据
+        if (StringUtils.isEmpty(nowParams))
+        {
+            nowParams = JSONObject.toJSONString(request.getParameterMap());
+        }
+        Map<String, Object> nowDataMap = new HashMap<String, Object>();
+        nowDataMap.put(REPEAT_PARAMS, nowParams);
+        nowDataMap.put(REPEAT_TIME, System.currentTimeMillis());
+
+        // 请求地址(作为存放cache的key值)
+        String url = request.getRequestURI();
+
+        // 唯一值(没有消息头则使用请求地址)
+        String submitKey = request.getHeader(header);
+        if (StringUtils.isEmpty(submitKey))
+        {
+            submitKey = url;
+        }
+
+        // 唯一标识(指定key + 消息头)
+        String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + submitKey;
+
+        Object sessionObj = redisCache.getCacheObject(cacheRepeatKey);
+        if (sessionObj != null)
+        {
+            Map<String, Object> sessionMap = (Map<String, Object>) sessionObj;
+            if (sessionMap.containsKey(url))
+            {
+                Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url);
+                if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap))
+                {
+                    return true;
+                }
+            }
+        }
+        Map<String, Object> cacheMap = new HashMap<String, Object>();
+        cacheMap.put(url, nowDataMap);
+        redisCache.setCacheObject(cacheRepeatKey, cacheMap, intervalTime, TimeUnit.SECONDS);
+        return false;
+    }
+
+    /**
+     * 判断参数是否相同
+     */
+    private boolean compareParams(Map<String, Object> nowMap, Map<String, Object> preMap)
+    {
+        String nowParams = (String) nowMap.get(REPEAT_PARAMS);
+        String preParams = (String) preMap.get(REPEAT_PARAMS);
+        return nowParams.equals(preParams);
+    }
+
+    /**
+     * 判断两次间隔时间
+     */
+    private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap)
+    {
+        long time1 = (Long) nowMap.get(REPEAT_TIME);
+        long time2 = (Long) preMap.get(REPEAT_TIME);
+        if ((time1 - time2) < (this.intervalTime * 1000))
+        {
+            return true;
+        }
+        return false;
+    }
+}

+ 56 - 0
fs-app-task-course/src/main/java/com/fs/framework/manager/AsyncManager.java

@@ -0,0 +1,56 @@
+package com.fs.framework.manager;
+
+import com.fs.common.utils.Threads;
+import com.fs.common.utils.spring.SpringUtils;
+
+import java.util.TimerTask;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 异步任务管理器
+ *
+
+ */
+public class AsyncManager
+{
+    /**
+     * 操作延迟10毫秒
+     */
+    private final int OPERATE_DELAY_TIME = 10;
+
+    /**
+     * 异步操作任务调度线程池
+     */
+    private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");
+
+    /**
+     * 单例模式
+     */
+    private AsyncManager(){}
+
+    private static AsyncManager me = new AsyncManager();
+
+    public static AsyncManager me()
+    {
+        return me;
+    }
+
+    /**
+     * 执行任务
+     *
+     * @param task 任务
+     */
+    public void execute(TimerTask task)
+    {
+        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
+    }
+
+    /**
+     * 停止任务线程池
+     */
+    public void shutdown()
+    {
+        Threads.shutdownAndAwaitTermination(executor);
+    }
+}

+ 40 - 0
fs-app-task-course/src/main/java/com/fs/framework/manager/ShutdownManager.java

@@ -0,0 +1,40 @@
+package com.fs.framework.manager;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PreDestroy;
+
+/**
+ * 确保应用退出时能关闭后台线程
+ *
+
+ */
+@Component
+public class ShutdownManager
+{
+    private static final Logger logger = LoggerFactory.getLogger("sys-user");
+
+    @PreDestroy
+    public void destroy()
+    {
+        shutdownAsyncManager();
+    }
+
+    /**
+     * 停止异步执行任务
+     */
+    private void shutdownAsyncManager()
+    {
+        try
+        {
+            logger.info("====关闭后台任务任务线程池====");
+            AsyncManager.me().shutdown();
+        }
+        catch (Exception e)
+        {
+            logger.error(e.getMessage(), e);
+        }
+    }
+}

+ 106 - 0
fs-app-task-course/src/main/java/com/fs/framework/manager/factory/AsyncFactory.java

@@ -0,0 +1,106 @@
+package com.fs.framework.manager.factory;
+
+import com.fs.common.constant.Constants;
+import com.fs.common.utils.LogUtils;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.ip.AddressUtils;
+import com.fs.common.utils.ip.IpUtils;
+import com.fs.common.utils.spring.SpringUtils;
+import com.fs.company.domain.CompanyLogininfor;
+import com.fs.company.domain.CompanyOperLog;
+import com.fs.company.service.ICompanyLogininforService;
+import com.fs.company.service.ICompanyOperLogService;
+import eu.bitwalker.useragentutils.UserAgent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Date;
+import java.util.TimerTask;
+
+/**
+ * 异步工厂(产生任务用)
+ *
+
+ */
+public class AsyncFactory
+{
+    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");
+
+    /**
+     * 记录登录信息
+     *
+     * @param username 用户名
+     * @param status 状态
+     * @param message 消息
+     * @param args 列表
+     * @return 任务task
+     */
+    public static TimerTask recordLogininfor(final Long companyId,final String username, final String status, final String message,
+            final Object... args)
+    {
+        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
+        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
+        return new TimerTask()
+        {
+            @Override
+            public void run()
+            {
+                String address = AddressUtils.getRealAddressByIP(ip);
+                StringBuilder s = new StringBuilder();
+                s.append(LogUtils.getBlock(ip));
+                s.append(address);
+                s.append(LogUtils.getBlock(username));
+                s.append(LogUtils.getBlock(status));
+                s.append(LogUtils.getBlock(message));
+                // 打印信息到日志
+                sys_user_logger.info(s.toString(), args);
+                // 获取客户端操作系统
+                String os = userAgent.getOperatingSystem().getName();
+                // 获取客户端浏览器
+                String browser = userAgent.getBrowser().getName();
+                // 封装对象
+                CompanyLogininfor logininfor = new CompanyLogininfor();
+                logininfor.setCompanyId(companyId);
+                logininfor.setUserName(username);
+                logininfor.setIpaddr(ip);
+                logininfor.setLoginLocation(address);
+                logininfor.setBrowser(browser);
+                logininfor.setOs(os);
+                logininfor.setMsg(message);
+                // 日志状态
+                if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status))
+                {
+                    logininfor.setStatus(Constants.SUCCESS);
+                }
+                else if (Constants.LOGIN_FAIL.equals(status))
+                {
+                    logininfor.setStatus(Constants.FAIL);
+                }
+                logininfor.setLoginTime(new Date());
+                // 插入数据
+                SpringUtils.getBean(ICompanyLogininforService.class).insertCompanyLogininfor(logininfor);
+            }
+        };
+    }
+
+    /**
+     * 操作日志记录
+     *
+     * @param operLog 操作日志信息
+     * @return 任务task
+     */
+    public static TimerTask recordOper(final CompanyOperLog operLog)
+    {
+        return new TimerTask()
+        {
+            @Override
+            public void run()
+            {
+                // 远程查询操作地点
+                operLog.setOperTime(new Date());
+                operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
+                SpringUtils.getBean(ICompanyOperLogService.class).insertCompanyOperLog(operLog);
+            }
+        };
+    }
+}

+ 69 - 0
fs-app-task-course/src/main/java/com/fs/framework/security/LoginBody.java

@@ -0,0 +1,69 @@
+package com.fs.framework.security;
+
+/**
+ * 用户登录对象
+ *
+
+ */
+public class LoginBody
+{
+    /**
+     * 用户名
+     */
+    private String username;
+
+    /**
+     * 用户密码
+     */
+    private String password;
+
+    /**
+     * 验证码
+     */
+    private String code;
+
+    /**
+     * 唯一标识
+     */
+    private String uuid = "";
+
+    public String getUsername()
+    {
+        return username;
+    }
+
+    public void setUsername(String username)
+    {
+        this.username = username;
+    }
+
+    public String getPassword()
+    {
+        return password;
+    }
+
+    public void setPassword(String password)
+    {
+        this.password = password;
+    }
+
+    public String getCode()
+    {
+        return code;
+    }
+
+    public void setCode(String code)
+    {
+        this.code = code;
+    }
+
+    public String getUuid()
+    {
+        return uuid;
+    }
+
+    public void setUuid(String uuid)
+    {
+        this.uuid = uuid;
+    }
+}

+ 255 - 0
fs-app-task-course/src/main/java/com/fs/framework/security/LoginUser.java

@@ -0,0 +1,255 @@
+package com.fs.framework.security;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fs.company.domain.Company;
+import com.fs.company.domain.CompanyUser;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.userdetails.UserDetails;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * 登录用户身份权限
+ *
+
+ */
+public class LoginUser implements UserDetails
+{
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 用户唯一标识
+     */
+    private String token;
+
+    /**
+     * 登录时间
+     */
+    private Long loginTime;
+
+    /**
+     * 过期时间
+     */
+    private Long expireTime;
+
+    /**
+     * 登录IP地址
+     */
+    private String ipaddr;
+
+    /**
+     * 登录地点
+     */
+    private String loginLocation;
+
+    /**
+     * 浏览器类型
+     */
+    private String browser;
+
+    /**
+     * 操作系统
+     */
+    private String os;
+
+
+
+    /**
+     * 权限列表
+     */
+    private Set<String> permissions;
+
+    /**
+     * 用户信息
+     */
+    private CompanyUser user;
+
+    private Company company;
+
+
+
+    public static long getSerialVersionUID() {
+        return serialVersionUID;
+    }
+
+
+
+    public Company getCompany() {
+        return company;
+    }
+
+    public void setCompany(Company company) {
+        this.company = company;
+    }
+
+    public String getToken()
+    {
+        return token;
+    }
+
+    public void setToken(String token)
+    {
+        this.token = token;
+    }
+
+    public LoginUser()
+    {
+    }
+    public LoginUser(CompanyUser user, Set<String> permissions, Company company)
+    {
+        this.user = user;
+        this.permissions = permissions;
+        this.company=company;
+    }
+    public LoginUser(CompanyUser user, Set<String> permissions)
+    {
+        this.user = user;
+        this.permissions = permissions;
+    }
+
+    @JsonIgnore
+    @Override
+    public String getPassword()
+    {
+        return user.getPassword();
+    }
+
+    @Override
+    public String getUsername()
+    {
+        return user.getUserName();
+    }
+
+    /**
+     * 账户是否未过期,过期无法验证
+     */
+    @JsonIgnore
+    @Override
+    public boolean isAccountNonExpired()
+    {
+        return true;
+    }
+
+    /**
+     * 指定用户是否解锁,锁定的用户无法进行身份验证
+     *
+     * @return
+     */
+    @JsonIgnore
+    @Override
+    public boolean isAccountNonLocked()
+    {
+        return true;
+    }
+
+    /**
+     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
+     *
+     * @return
+     */
+    @JsonIgnore
+    @Override
+    public boolean isCredentialsNonExpired()
+    {
+        return true;
+    }
+
+    /**
+     * 是否可用 ,禁用的用户不能身份验证
+     *
+     * @return
+     */
+    @JsonIgnore
+    @Override
+    public boolean isEnabled()
+    {
+        return true;
+    }
+
+    public Long getLoginTime()
+    {
+        return loginTime;
+    }
+
+    public void setLoginTime(Long loginTime)
+    {
+        this.loginTime = loginTime;
+    }
+
+    public String getIpaddr()
+    {
+        return ipaddr;
+    }
+
+    public void setIpaddr(String ipaddr)
+    {
+        this.ipaddr = ipaddr;
+    }
+
+    public String getLoginLocation()
+    {
+        return loginLocation;
+    }
+
+    public void setLoginLocation(String loginLocation)
+    {
+        this.loginLocation = loginLocation;
+    }
+
+    public String getBrowser()
+    {
+        return browser;
+    }
+
+    public void setBrowser(String browser)
+    {
+        this.browser = browser;
+    }
+
+    public String getOs()
+    {
+        return os;
+    }
+
+    public void setOs(String os)
+    {
+        this.os = os;
+    }
+
+    public Long getExpireTime()
+    {
+        return expireTime;
+    }
+
+    public void setExpireTime(Long expireTime)
+    {
+        this.expireTime = expireTime;
+    }
+
+    public Set<String> getPermissions()
+    {
+        return permissions;
+    }
+
+    public void setPermissions(Set<String> permissions)
+    {
+        this.permissions = permissions;
+    }
+
+    public CompanyUser getUser()
+    {
+        return user;
+    }
+
+    public void setUser(CompanyUser user)
+    {
+        this.user = user;
+    }
+
+    @Override
+    public Collection<? extends GrantedAuthority> getAuthorities()
+    {
+        return null;
+    }
+}

+ 89 - 0
fs-app-task-course/src/main/java/com/fs/framework/security/SecurityUtils.java

@@ -0,0 +1,89 @@
+package com.fs.framework.security;
+
+import com.fs.common.constant.HttpStatus;
+import com.fs.common.exception.CustomException;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+
+/**
+ * 安全服务工具类
+ *
+
+ */
+public class SecurityUtils
+{
+    /**
+     * 获取用户账户
+     **/
+    public static String getUsername()
+    {
+        try
+        {
+            return getLoginUser().getUsername();
+        }
+        catch (Exception e)
+        {
+            throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
+        }
+    }
+
+    /**
+     * 获取用户
+     **/
+    public static LoginUser getLoginUser()
+    {
+        try
+        {
+            return (LoginUser) getAuthentication().getPrincipal();
+        }
+        catch (Exception e)
+        {
+            throw new CustomException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
+        }
+    }
+
+    /**
+     * 获取Authentication
+     */
+    public static Authentication getAuthentication()
+    {
+        return SecurityContextHolder.getContext().getAuthentication();
+    }
+
+    /**
+     * 生成BCryptPasswordEncoder密码
+     *
+     * @param password 密码
+     * @return 加密字符串
+     */
+    public static String encryptPassword(String password)
+    {
+        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
+        return passwordEncoder.encode(password);
+    }
+
+    /**
+     * 判断密码是否相同
+     *
+     * @param rawPassword 真实密码
+     * @param encodedPassword 加密后字符
+     * @return 结果
+     */
+    public static boolean matchesPassword(String rawPassword, String encodedPassword)
+    {
+        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
+        return passwordEncoder.matches(rawPassword, encodedPassword);
+    }
+
+    /**
+     * 是否为管理员
+     *
+     * @param userId 用户ID
+     * @return 结果
+     */
+    public static boolean isAdmin(Long userId)
+    {
+        return userId != null && 1L == userId;
+    }
+}

+ 47 - 0
fs-app-task-course/src/main/java/com/fs/framework/security/filter/JwtAuthenticationTokenFilter.java

@@ -0,0 +1,47 @@
+package com.fs.framework.security.filter;
+
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.utils.StringUtils;
+import com.fs.framework.security.LoginUser;
+import com.fs.framework.security.SecurityUtils;
+import com.fs.framework.service.TokenService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * token过滤器 验证token有效性
+ *
+
+ */
+@Component
+public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
+{
+    @Autowired
+    private TokenService tokenService;
+    @Autowired
+    private RedisCache redisCache;
+    @Override
+    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
+            throws ServletException, IOException
+    {
+        LoginUser loginUser = tokenService.getLoginUser(request);
+        if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
+        {
+            tokenService.verifyToken(loginUser);
+            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
+            authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
+            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
+        }
+        chain.doFilter(request, response);
+    }
+}

+ 35 - 0
fs-app-task-course/src/main/java/com/fs/framework/security/handle/AuthenticationEntryPointImpl.java

@@ -0,0 +1,35 @@
+package com.fs.framework.security.handle;
+
+import com.alibaba.fastjson.JSON;
+import com.fs.common.constant.HttpStatus;
+import com.fs.common.core.domain.AjaxResult;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.web.AuthenticationEntryPoint;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.Serializable;
+
+/**
+ * 认证失败处理类 返回未授权
+ *
+
+ */
+@Component
+public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable
+{
+    private static final long serialVersionUID = -8970718410437077606L;
+
+    @Override
+    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
+            throws IOException
+    {
+        int code = HttpStatus.UNAUTHORIZED;
+        String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", request.getRequestURI());
+        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg)));
+    }
+}

+ 54 - 0
fs-app-task-course/src/main/java/com/fs/framework/security/handle/LogoutSuccessHandlerImpl.java

@@ -0,0 +1,54 @@
+package com.fs.framework.security.handle;
+
+import com.alibaba.fastjson.JSON;
+import com.fs.common.constant.Constants;
+import com.fs.common.constant.HttpStatus;
+import com.fs.common.core.domain.AjaxResult;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.framework.manager.AsyncManager;
+import com.fs.framework.manager.factory.AsyncFactory;
+import com.fs.framework.security.LoginUser;
+import com.fs.framework.service.TokenService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * 自定义退出处理类 返回成功
+ *
+
+ */
+@Configuration
+public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
+{
+    @Autowired
+    private TokenService tokenService;
+
+    /**
+     * 退出处理
+     *
+     * @return
+     */
+    @Override
+    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
+            throws IOException, ServletException
+    {
+        LoginUser loginUser = tokenService.getLoginUser(request);
+        if (StringUtils.isNotNull(loginUser))
+        {
+            String userName = loginUser.getUsername();
+            // 删除用户缓存记录
+            tokenService.delLoginUser(loginUser.getToken());
+            // 记录用户退出日志
+            AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginUser.getCompany().getCompanyId(),userName, Constants.LOGOUT, "退出成功"));
+        }
+        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.SUCCESS, "退出成功")));
+    }
+}

+ 92 - 0
fs-app-task-course/src/main/java/com/fs/framework/service/CompanyLoginService.java

@@ -0,0 +1,92 @@
+package com.fs.framework.service;
+
+import com.fs.common.constant.Constants;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.exception.ServiceException;
+import com.fs.common.exception.user.CaptchaException;
+import com.fs.common.exception.user.CaptchaExpireException;
+import com.fs.common.exception.user.UserPasswordNotMatchException;
+import com.fs.common.utils.MessageUtils;
+import com.fs.framework.manager.AsyncManager;
+import com.fs.framework.manager.factory.AsyncFactory;
+import com.fs.framework.security.LoginUser;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.BadCredentialsException;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 登录校验方法
+ *
+
+ */
+@Component
+public class CompanyLoginService
+{
+    @Autowired
+    private TokenService tokenService;
+
+    @Resource
+    private AuthenticationManager authenticationManager;
+
+    @Autowired
+    private RedisCache redisCache;
+
+    /**
+     * 登录验证
+     *
+     * @param username 用户名
+     * @param password 密码
+     * @param code 验证码
+     * @param uuid 唯一标识
+     * @return 结果
+     */
+    public String login(String username, String password, String code, String uuid)
+    {
+        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
+        String captcha = redisCache.getCacheObject(verifyKey);
+        redisCache.deleteObject(verifyKey);
+        if (captcha == null)
+        {
+            AsyncManager.me().execute(AsyncFactory.recordLogininfor(0l,username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
+            throw new CaptchaExpireException();
+        }
+        if (!code.equalsIgnoreCase(captcha))
+        {
+            AsyncManager.me().execute(AsyncFactory.recordLogininfor(0l,username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
+            throw new CaptchaException();
+        }
+        // 用户验证
+        Authentication authentication = null;
+        try
+        {
+            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
+            authentication = authenticationManager
+                    .authenticate(new UsernamePasswordAuthenticationToken(username, password));
+        }
+        catch (Exception e)
+        {
+            if (e instanceof BadCredentialsException)
+            {
+                AsyncManager.me().execute(AsyncFactory.recordLogininfor(0l,username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
+                throw new UserPasswordNotMatchException();
+            }
+            else
+            {
+                AsyncManager.me().execute(AsyncFactory.recordLogininfor(0l,username, Constants.LOGIN_FAIL, e.getMessage()));
+                throw new ServiceException(e.getMessage());
+            }
+        }
+        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
+        AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginUser.getUser().getCompanyId(),username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
+        redisCache.setCacheObject("companyId:"+loginUser.getUser().getUserId(),loginUser.getUser().getCompanyId(),604800, TimeUnit.SECONDS);
+        // 生成token
+        return tokenService.createToken(loginUser);
+    }
+
+}

+ 66 - 0
fs-app-task-course/src/main/java/com/fs/framework/service/CompanyPermissionService.java

@@ -0,0 +1,66 @@
+package com.fs.framework.service;
+
+import com.fs.company.domain.CompanyUser;
+import com.fs.company.service.ICompanyMenuService;
+import com.fs.company.service.ICompanyRoleService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * 用户权限处理
+ *
+
+ */
+@Component
+public class CompanyPermissionService
+{
+    @Autowired
+    private ICompanyRoleService roleService;
+    @Autowired
+    private ICompanyMenuService menuService;
+
+    /**
+     * 获取角色数据权限
+     *
+     * @param user 用户信息
+     * @return 角色权限信息
+     */
+    public Set<String> getRolePermission(CompanyUser user)
+    {
+        Set<String> roles = new HashSet<String>();
+        // 管理员拥有所有权限
+        if (user.isAdmin())
+        {
+            roles.add("admin");
+        }
+        else
+        {
+            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
+        }
+        return roles;
+    }
+
+    /**
+     * 获取菜单数据权限
+     *
+     * @param user 用户信息
+     * @return 菜单权限信息
+     */
+    public Set<String> getMenuPermission(CompanyUser user)
+    {
+        Set<String> perms = new HashSet<String>();
+        // 管理员拥有所有权限
+        if (user.isAdmin())
+        {
+            perms.add("*:*:*");
+        }
+        else
+        {
+            perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
+        }
+        return perms;
+    }
+}

+ 170 - 0
fs-app-task-course/src/main/java/com/fs/framework/service/PermissionService.java

@@ -0,0 +1,170 @@
+package com.fs.framework.service;
+
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.company.domain.CompanyRole;
+import com.fs.framework.security.LoginUser;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.util.Set;
+
+/**
+ * 自定义权限实现,ss取自SpringSecurity首字母
+ */
+@Service("ss")
+public class PermissionService
+{
+    /** 所有权限标识 */
+    private static final String ALL_PERMISSION = "*:*:*";
+
+    /** 管理员角色权限标识 */
+    private static final String SUPER_ADMIN = "admin";
+
+    private static final String ROLE_DELIMETER = ",";
+
+    private static final String PERMISSION_DELIMETER = ",";
+
+    @Autowired
+    private TokenService tokenService;
+
+    /**
+     * 验证用户是否具备某权限
+     *
+     * @param permission 权限字符串
+     * @return 用户是否具备某权限
+     */
+    public boolean hasPermi(String permission)
+    {
+        if (StringUtils.isEmpty(permission))
+        {
+            return false;
+        }
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getPermissions()))
+        {
+            return false;
+        }
+        return hasPermissions(loginUser.getPermissions(), permission);
+    }
+
+    /**
+     * 验证用户是否不具备某权限,与 hasPermi逻辑相反
+     *
+     * @param permission 权限字符串
+     * @return 用户是否不具备某权限
+     */
+    public boolean lacksPermi(String permission)
+    {
+        return hasPermi(permission) != true;
+    }
+
+    /**
+     * 验证用户是否具有以下任意一个权限
+     *
+     * @param permissions 以 PERMISSION_NAMES_DELIMETER 为分隔符的权限列表
+     * @return 用户是否具有以下任意一个权限
+     */
+    public boolean hasAnyPermi(String permissions)
+    {
+        if (StringUtils.isEmpty(permissions))
+        {
+            return false;
+        }
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getPermissions()))
+        {
+            return false;
+        }
+        Set<String> authorities = loginUser.getPermissions();
+        for (String permission : permissions.split(PERMISSION_DELIMETER))
+        {
+            if (permission != null && hasPermissions(authorities, permission))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 判断用户是否拥有某个角色
+     *
+     * @param role 角色字符串
+     * @return 用户是否具备某角色
+     */
+    public boolean hasRole(String role)
+    {
+        if (StringUtils.isEmpty(role))
+        {
+            return false;
+        }
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles()))
+        {
+            return false;
+        }
+        for (CompanyRole sysRole : loginUser.getUser().getRoles())
+        {
+            String roleKey = sysRole.getRoleKey();
+            if (SUPER_ADMIN.contains(roleKey) || roleKey.contains(StringUtils.trim(role)))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 验证用户是否不具备某角色,与 isRole逻辑相反。
+     *
+     * @param role 角色名称
+     * @return 用户是否不具备某角色
+     */
+    public boolean lacksRole(String role)
+    {
+        return hasRole(role) != true;
+    }
+
+    /**
+     * 验证用户是否具有以下任意一个角色
+     *
+     * @param roles 以 ROLE_NAMES_DELIMETER 为分隔符的角色列表
+     * @return 用户是否具有以下任意一个角色
+     */
+    public boolean hasAnyRoles(String roles)
+    {
+        if (StringUtils.isEmpty(roles))
+        {
+            return false;
+        }
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles()))
+        {
+            return false;
+        }
+        for (String role : roles.split(ROLE_DELIMETER))
+        {
+            if (hasRole(role))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 判断是否包含权限
+     *
+     * @param permissions 权限列表
+     * @param permission 权限字符串
+     * @return 用户是否具备某权限
+     */
+    private boolean hasPermissions(Set<String> permissions, String permission)
+    {
+        return permissions.contains(ALL_PERMISSION) || permissions.contains(StringUtils.trim(permission));
+    }
+
+
+}

+ 236 - 0
fs-app-task-course/src/main/java/com/fs/framework/service/TokenService.java

@@ -0,0 +1,236 @@
+package com.fs.framework.service;
+
+import com.fs.common.constant.Constants;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.ip.AddressUtils;
+import com.fs.common.utils.ip.IpUtils;
+import com.fs.common.utils.uuid.IdUtils;
+import com.fs.framework.security.LoginUser;
+import eu.bitwalker.useragentutils.UserAgent;
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * token验证处理
+ *
+
+ */
+@Component
+public class TokenService
+{
+    // 令牌自定义标识
+    @Value("${token.header}")
+    private String header;
+
+    // 令牌秘钥
+    @Value("${token.secret}")
+    private String secret;
+
+    // 令牌有效期(默认30分钟)
+    @Value("${token.expireTime}")
+    private int expireTime;
+
+    protected static final long MILLIS_SECOND = 1000;
+
+    protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
+
+    private static final Long MILLIS_MINUTE_TEN = 20 * 60 * 100000000000L;
+
+    @Autowired
+    private RedisCache redisCache;
+
+    /**
+     * 获取用户身份信息
+     *
+     * @return 用户信息
+     */
+    public LoginUser getLoginUser(HttpServletRequest request)
+    {
+        // 获取请求携带的令牌
+        String token = getToken(request);
+        if (StringUtils.isNotEmpty(token))
+        {
+            Claims claims = parseToken(token);
+            // 解析对应的权限以及用户信息
+            String uuid = (String) claims.get(Constants.COMPANY_LOGIN_USER_KEY);
+            String userKey = getTokenKey(uuid);
+            LoginUser user = redisCache.getCacheObject(userKey);
+            return user;
+        }
+        token=getUrlToken(request);
+        if (StringUtils.isNotEmpty(token))
+        {
+            Claims claims = parseToken(token);
+            // 解析对应的权限以及用户信息
+            String uuid = (String) claims.get(Constants.COMPANY_LOGIN_USER_KEY);
+            String userKey = getTokenKey(uuid);
+            LoginUser user = redisCache.getCacheObject(userKey);
+            return user;
+        }
+
+        return null;
+    }
+
+    /**
+     * 设置用户身份信息
+     */
+    public void setLoginUser(LoginUser loginUser)
+    {
+        if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken()))
+        {
+            refreshToken(loginUser);
+        }
+    }
+
+    /**
+     * 删除用户身份信息
+     */
+    public void delLoginUser(String token)
+    {
+        if (StringUtils.isNotEmpty(token))
+        {
+            String userKey = getTokenKey(token);
+            redisCache.deleteObject(userKey);
+        }
+    }
+
+    /**
+     * 创建令牌
+     *
+     * @param loginUser 用户信息
+     * @return 令牌
+     */
+    public String createToken(LoginUser loginUser)
+    {
+        String token = IdUtils.fastUUID();
+        loginUser.setToken(token);
+        setUserAgent(loginUser);
+        refreshToken(loginUser);
+
+        Map<String, Object> claims = new HashMap<>();
+        claims.put(Constants.COMPANY_LOGIN_USER_KEY, token);
+        return createToken(claims);
+    }
+
+    /**
+     * 验证令牌有效期,相差不足20分钟,自动刷新缓存
+     *
+     * @param loginUser
+     * @return 令牌
+     */
+    public void verifyToken(LoginUser loginUser)
+    {
+        long expireTime = loginUser.getExpireTime();
+        long currentTime = System.currentTimeMillis();
+        if (expireTime - currentTime <= MILLIS_MINUTE_TEN)
+        {
+            refreshToken(loginUser);
+        }
+    }
+
+    /**
+     * 刷新令牌有效期
+     *
+     * @param loginUser 登录信息
+     */
+    public void refreshToken(LoginUser loginUser)
+    {
+        loginUser.setLoginTime(System.currentTimeMillis());
+        loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
+        // 根据uuid将loginUser缓存
+        String userKey = getTokenKey(loginUser.getToken());
+        redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
+    }
+
+    /**
+     * 设置用户代理信息
+     *
+     * @param loginUser 登录信息
+     */
+    public void setUserAgent(LoginUser loginUser)
+    {
+        UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
+        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
+        loginUser.setIpaddr(ip);
+        loginUser.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
+        loginUser.setBrowser(userAgent.getBrowser().getName());
+        loginUser.setOs(userAgent.getOperatingSystem().getName());
+    }
+
+    /**
+     * 从数据声明生成令牌
+     *
+     * @param claims 数据声明
+     * @return 令牌
+     */
+    private String createToken(Map<String, Object> claims)
+    {
+        String token = Jwts.builder()
+                .setClaims(claims)
+                .signWith(SignatureAlgorithm.HS512, secret).compact();
+        return token;
+    }
+
+    /**
+     * 从令牌中获取数据声明
+     *
+     * @param token 令牌
+     * @return 数据声明
+     */
+    private Claims parseToken(String token)
+    {
+        return Jwts.parser()
+                .setSigningKey(secret)
+                .parseClaimsJws(token)
+                .getBody();
+    }
+
+    /**
+     * 从令牌中获取用户名
+     *
+     * @param token 令牌
+     * @return 用户名
+     */
+    public String getUsernameFromToken(String token)
+    {
+        Claims claims = parseToken(token);
+        return claims.getSubject();
+    }
+
+    /**
+     * 获取请求token
+     *
+     * @param request
+     * @return token
+     */
+    private String getToken(HttpServletRequest request)
+    {
+        String token = request.getHeader(header);
+        if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
+        {
+            token = token.replace(Constants.TOKEN_PREFIX, "");
+        }
+        return token;
+    }
+    private String getUrlToken(HttpServletRequest request)
+    {
+        String token = request.getParameter("token");
+        return token;
+    }
+
+    private String getTokenKey(String uuid)
+    {
+        return Constants.COMPANY_LOGIN_TOKEN_KEY + uuid;
+    }
+}

+ 75 - 0
fs-app-task-course/src/main/java/com/fs/framework/service/UserDetailsServiceImpl.java

@@ -0,0 +1,75 @@
+package com.fs.framework.service;
+
+
+import com.fs.common.enums.UserStatus;
+import com.fs.common.exception.CustomException;
+import com.fs.common.utils.StringUtils;
+import com.fs.company.domain.Company;
+import com.fs.company.domain.CompanyUser;
+import com.fs.company.service.ICompanyService;
+import com.fs.company.service.ICompanyUserService;
+import com.fs.framework.security.LoginUser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.stereotype.Service;
+
+/**
+ * 用户验证处理
+ *
+
+ */
+@Service
+public class UserDetailsServiceImpl implements UserDetailsService
+{
+    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
+
+    @Autowired
+    private ICompanyUserService userService;
+
+    @Autowired
+    private CompanyPermissionService permissionService;
+
+    @Autowired
+    private ICompanyService companyService;
+
+
+
+
+    @Override
+    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
+    {
+
+
+        CompanyUser user = userService.selectUserByUserName(username);
+        if (StringUtils.isNull(user))
+        {
+            log.info("登录用户:{} 不存在.", username);
+            throw new UsernameNotFoundException("登录用户:" + username + " 不存在");
+        }
+        Company company=companyService.selectCompanyById(user.getCompanyId()) ;
+        if(company==null||company.getStatus()==0||company.getIsDel()==1){
+            throw new CustomException("此用户所属公司不存在或已停用");
+        }
+        if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
+        {
+            log.info("登录用户:{} 已被删除.", username);
+            throw new CustomException("对不起,您的账号:" + username + " 已被删除");
+        }
+        else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
+        {
+            log.info("登录用户:{} 已被停用.", username);
+            throw new CustomException("对不起,您的账号:" + username + " 已停用");
+        }
+
+        return createLoginUser(user);
+    }
+
+    public UserDetails createLoginUser(CompanyUser user)
+    {
+        return new LoginUser(user, permissionService.getMenuPermission(user),companyService.selectCompanyById(user.getCompanyId()));
+    }
+}

+ 1 - 0
fs-app-task-course/src/main/resources/META-INF/spring-devtools.properties

@@ -0,0 +1 @@
+restart.include.json=/com.alibaba.fastjson.*.jar

+ 10 - 0
fs-app-task-course/src/main/resources/application.yml

@@ -0,0 +1,10 @@
+server:
+  # 服务器的HTTP端口,默认为8080
+  port: 8889
+# Spring配置
+spring:
+  profiles:
+    #    active: druid-bjzm-test
+    #    active: druid-hdt
+    #    active: dev
+    active: druid-hst-test

+ 2 - 0
fs-app-task-course/src/main/resources/banner.txt

@@ -0,0 +1,2 @@
+Application Version: ${fs.version}
+Spring Boot Version: ${spring-boot.version}

+ 37 - 0
fs-app-task-course/src/main/resources/i18n/messages.properties

@@ -0,0 +1,37 @@
+#错误消息
+not.null=* 必须填写
+user.jcaptcha.error=验证码错误
+user.jcaptcha.expire=验证码已失效
+user.not.exists=用户不存在/密码错误
+user.password.not.match=用户不存在/密码错误
+user.password.retry.limit.count=密码输入错误{0}次
+user.password.retry.limit.exceed=密码输入错误{0}次,帐户锁定10分钟
+user.password.delete=对不起,您的账号已被删除
+user.blocked=用户已封禁,请联系管理员
+role.blocked=角色已封禁,请联系管理员
+user.logout.success=退出成功
+
+length.not.valid=长度必须在{min}到{max}个字符之间
+
+user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成,且必须以非数字开头
+user.password.not.valid=* 5-50个字符
+
+user.email.not.valid=邮箱格式错误
+user.mobile.phone.number.not.valid=手机号格式错误
+user.login.success=登录成功
+user.register.success=注册成功
+user.notfound=请重新登录
+user.forcelogout=管理员强制退出,请重新登录
+user.unknown.error=未知错误,请重新登录
+
+##文件上传消息
+upload.exceed.maxSize=上传的文件大小超出限制的文件大小!<br/>允许的文件最大大小是:{0}MB!
+upload.filename.exceed.length=上传的文件名最长{0}个字符
+
+##权限
+no.permission=您没有数据的权限,请联系管理员添加权限 [{0}]
+no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}]
+no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}]
+no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}]
+no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}]
+no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}]

+ 93 - 0
fs-app-task-course/src/main/resources/logback.xml

@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+    <!-- 日志存放路径 -->
+	<property name="log.path" value="/home/fs-qw-task-course/logs" />
+    <!-- 日志输出格式 -->
+	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
+
+	<!-- 控制台输出 -->
+	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+		<encoder>
+			<pattern>${log.pattern}</pattern>
+		</encoder>
+	</appender>
+
+	<!-- 系统日志输出 -->
+	<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
+	    <file>${log.path}/sys-info.log</file>
+        <!-- 循环政策:基于时间创建日志文件 -->
+		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <!-- 日志文件名格式 -->
+			<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
+			<!-- 日志最大的历史 60天 -->
+			<maxHistory>60</maxHistory>
+		</rollingPolicy>
+		<encoder>
+			<pattern>${log.pattern}</pattern>
+		</encoder>
+		<filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <!-- 过滤的级别 -->
+            <level>INFO</level>
+            <!-- 匹配时的操作:接收(记录) -->
+            <onMatch>ACCEPT</onMatch>
+            <!-- 不匹配时的操作:拒绝(不记录) -->
+            <onMismatch>DENY</onMismatch>
+        </filter>
+	</appender>
+
+	<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
+	    <file>${log.path}/sys-error.log</file>
+        <!-- 循环政策:基于时间创建日志文件 -->
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <!-- 日志文件名格式 -->
+            <fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
+			<!-- 日志最大的历史 60天 -->
+			<maxHistory>60</maxHistory>
+        </rollingPolicy>
+        <encoder>
+            <pattern>${log.pattern}</pattern>
+        </encoder>
+        <filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <!-- 过滤的级别 -->
+            <level>ERROR</level>
+			<!-- 匹配时的操作:接收(记录) -->
+            <onMatch>ACCEPT</onMatch>
+			<!-- 不匹配时的操作:拒绝(不记录) -->
+            <onMismatch>DENY</onMismatch>
+        </filter>
+    </appender>
+
+	<!-- 用户访问日志输出  -->
+    <appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
+		<file>${log.path}/sys-user.log</file>
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <!-- 按天回滚 daily -->
+            <fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
+            <!-- 日志最大的历史 60天 -->
+            <maxHistory>60</maxHistory>
+        </rollingPolicy>
+        <encoder>
+            <pattern>${log.pattern}</pattern>
+        </encoder>
+    </appender>
+
+	<!-- 系统模块日志级别控制  -->
+	<logger name="com.fs" level="info" />
+	<!-- Spring日志级别控制  -->
+	<logger name="org.springframework" level="warn" />
+
+	<root level="info">
+		<appender-ref ref="console" />
+	</root>
+
+	<!--系统操作日志-->
+    <root level="info">
+        <appender-ref ref="file_info" />
+        <appender-ref ref="file_error" />
+    </root>
+
+	<!--系统用户操作日志-->
+    <logger name="sys-user" level="info">
+        <appender-ref ref="sys-user"/>
+    </logger>
+</configuration>

+ 19 - 0
fs-app-task-course/src/main/resources/mybatis/mybatis-config.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE configuration
+PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+"http://mybatis.org/dtd/mybatis-3-config.dtd">
+<configuration>
+
+	<settings>
+		<setting name="cacheEnabled"             value="true" />  <!-- 全局映射器启用缓存 -->
+		<setting name="useGeneratedKeys"         value="true" />  <!-- 允许 JDBC 支持自动生成主键 -->
+		<setting name="defaultExecutorType"      value="REUSE" /> <!-- 配置默认的执行器 -->
+		<setting name="logImpl"                  value="SLF4J" /> <!-- 指定 MyBatis 所用日志的具体实现 -->
+		 <setting name="mapUnderscoreToCamelCase" value="true"/>
+	</settings>
+
+	<typeHandlers>
+		<typeHandler handler="com.fs.framework.config.ArrayStringTypeHandler"/>
+	</typeHandlers>
+
+</configuration>

+ 144 - 0
fs-app-task/pom.xml

@@ -0,0 +1,144 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.fs</groupId>
+        <artifactId>fs</artifactId>
+        <version>1.1.0</version>
+    </parent>
+
+    <groupId>com.fs.app</groupId>
+    <artifactId>fs-app-task</artifactId>
+
+    <dependencies>
+        <!-- spring-boot-devtools -->
+<!--        <dependency>-->
+<!--            <groupId>org.springframework.boot</groupId>-->
+<!--            <artifactId>spring-boot-devtools</artifactId>-->
+<!--            <optional>true</optional> &lt;!&ndash; 表示依赖不会传递 &ndash;&gt;-->
+<!--        </dependency>-->
+        <!-- swagger2-->
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger2</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.retry</groupId>
+            <artifactId>spring-retry</artifactId>
+            <version>1.3.1</version>
+        </dependency>
+
+        <!-- swagger2-UI-->
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger-ui</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.github.xiaoymin</groupId>
+            <artifactId>swagger-bootstrap-ui</artifactId>
+            <version>1.9.3</version>
+        </dependency>
+
+
+        <!-- Mysql驱动包 -->
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+        </dependency>
+
+        <!-- SpringBoot Web容器 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <!-- SpringBoot 拦截器 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
+        <!-- 阿里数据库连接池 -->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid-spring-boot-starter</artifactId>
+        </dependency>
+
+        <!--clickhouse-->
+        <dependency>
+            <groupId>com.clickhouse</groupId>
+            <artifactId>clickhouse-jdbc</artifactId>
+            <version>0.4.6</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.retry</groupId>
+            <artifactId>spring-retry</artifactId>
+            <version>1.3.1</version>
+        </dependency>
+
+        <!--        <dependency>-->
+        <!--            <groupId>ru.yandex.clickhouse</groupId>-->
+        <!--            <artifactId>clickhouse-jdbc</artifactId>-->
+        <!--            <version>0.3.2</version>-->
+        <!--        </dependency>-->
+
+        <!-- 验证码 -->
+        <dependency>
+            <groupId>com.github.penggle</groupId>
+            <artifactId>kaptcha</artifactId>
+            <exclusions>
+                <exclusion>
+                    <artifactId>javax.servlet-api</artifactId>
+                    <groupId>javax.servlet</groupId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <!-- 获取系统信息 -->
+        <dependency>
+            <groupId>com.github.oshi</groupId>
+            <artifactId>oshi-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.fs</groupId>
+            <artifactId>fs-service</artifactId>
+            <!-- 与父工程版本一致,避免子模块未进 reactor 时解析到不完整/过期的本地 fs-service -->
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>2.1.1.RELEASE</version>
+                <configuration>
+                    <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>3.1.0</version>
+                <configuration>
+                    <failOnMissingWebXml>false</failOnMissingWebXml>
+                    <warName>${project.artifactId}</warName>
+                </configuration>
+            </plugin>
+        </plugins>
+        <finalName>${project.artifactId}</finalName>
+    </build>
+
+</project>

+ 22 - 0
fs-app-task/src/main/java/com/fs/FsAppTaskApplication.java

@@ -0,0 +1,22 @@
+package com.fs;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
+@EnableTransactionManagement
+@EnableAsync
+@Slf4j
+public class FsAppTaskApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(FsAppTaskApplication.class, args);
+        log.info("{} 启动成功!!!", FsAppTaskApplication.class.getSimpleName());
+    }
+
+}

+ 42 - 0
fs-app-task/src/main/java/com/fs/app/config/ConsumerThreadPoolConfig.java

@@ -0,0 +1,42 @@
+package com.fs.app.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.ThreadPoolExecutor;
+
+@Configuration
+public class ConsumerThreadPoolConfig {
+
+    @Value("${task.consumer.thread-pool.core-pool-size:50}")
+    private Integer corePoolSize;
+
+    @Value("${task.consumer.thread-pool.max-pool-size:100}")
+    private Integer maxPoolSize;
+
+    @Value("${task.consumer.thread-pool.thread-name-prefix}")
+    private String threadNamePrefix;
+
+    @Bean(name = "consumerThreadPool", destroyMethod = "shutdown")
+    public ThreadPoolTaskExecutor customThreadPool() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        // 核心线程数
+        executor.setCorePoolSize(corePoolSize);
+        // 最大线程数
+        executor.setMaxPoolSize(maxPoolSize);
+        // 线程名前缀
+        executor.setThreadNamePrefix(threadNamePrefix);
+        // 拒绝策略:直接丢弃新任务
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
+        // 非核心线程空闲存活时间(秒)
+        executor.setKeepAliveSeconds(60);
+        // 等待所有任务完成后关闭线程池
+        executor.setWaitForTasksToCompleteOnShutdown(true);
+        // 初始化
+        executor.initialize();
+        return executor;
+    }
+
+}

+ 12 - 0
fs-app-task/src/main/java/com/fs/app/config/ScheduleTaskConfig.java

@@ -0,0 +1,12 @@
+package com.fs.app.config;
+
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
+
+@EnableScheduling
+@Configuration
+@ConditionalOnProperty(prefix = "task", name = "enabled", havingValue = "true")
+public class ScheduleTaskConfig {
+}

+ 177 - 0
fs-app-task/src/main/java/com/fs/app/controller/AppApiController.java

@@ -0,0 +1,177 @@
+package com.fs.app.controller;
+
+import com.alibaba.fastjson.JSON;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fs.app.cusrole.mapper.AppCustomerRoleMapper;
+import com.fs.app.invitation.dto.AppUserInvitationCodeDTO;
+import com.fs.app.invitation.service.IAppUserInvitationCodeService;
+import com.fs.app.sender.context.SpringContext;
+import com.fs.app.sender.im.ImHolder;
+import com.fs.app.sop.service.IAppSopTaskService;
+import com.fs.app.sop.service.impl.AppSopUserLogsInfoHandle;
+import com.fs.app.task.producer.sop.AppSopLogsProducer;
+import com.fs.app.task.service.IAppWorkTaskService;
+import com.fs.common.core.domain.R;
+import com.fs.im.dto.OpenImMsgCallBackResponse;
+import com.fs.im.service.impl.OpenImAsyncService;
+import com.fs.im.vo.OpenImMsgCallBackVO;
+import com.google.gson.Gson;
+import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.List;
+
+@RestController
+@RequestMapping("/app/api")
+@RequiredArgsConstructor
+public class AppApiController {
+
+    private final IAppSopTaskService appSopTaskService;
+
+    private final IAppWorkTaskService appWorkTaskService;
+
+    private final OpenImAsyncService openImAsyncService;
+
+    private final IAppUserInvitationCodeService appUserInvitationCodeService;
+
+    /**
+     * 生成用户营期
+     *
+     * @return
+     */
+    @GetMapping("/genUserLogs")
+    public R generateUserLog() {
+        appSopTaskService.generateUserLogs();
+        return R.ok();
+    }
+
+    /**
+     * 清除用户(满足条件的)营期
+     *
+     * @return
+     */
+    @GetMapping("/clearUserLogs")
+    public R clearUserLogs() {
+        appSopTaskService.clearUserLogs();
+        return R.ok();
+    }
+
+    /**
+     * 根据营期生成待发记录
+     *
+     * @return
+     * @throws InterruptedException
+     */
+    @GetMapping("/genUserLogsInfoFragmentation")
+    public R genUserLogsInfoFragmentation(@RequestParam Long batchId, @RequestParam List<String> sopIds) throws InterruptedException {
+        appSopTaskService.userLogsFragmentation(batchId, sopIds);
+        return R.ok();
+    }
+
+    /**
+     * 根据营期生成待发记录
+     *
+     * @return
+     * @throws InterruptedException
+     */
+    @GetMapping("/genSopLogs")
+    public R genSopLogs() throws InterruptedException {
+        AppSopLogsProducer appSopLogsProducer = SpringContext.getBean(AppSopLogsProducer.class);
+        appSopLogsProducer.generateSopLogs();
+        return R.ok();
+    }
+
+    /**
+     * 同步看课记录到催课看板
+     *
+     * @return
+     * @throws InterruptedException
+     */
+    @GetMapping("/syncWatchLog2UrgentClass")
+    public R syncWatchLog2UrgentClass() {
+        appWorkTaskService.syncWatchLog2UrgentClass();
+        return R.ok();
+    }
+
+    @GetMapping("/getIMUser")
+    public String getIMUser(String userId) {
+        ImHolder imHolder = SpringContext.getBean(ImHolder.class);
+        return JSON.toJSONString(imHolder.getImUserInfo(userId));
+    }
+
+    @PostMapping("/updateIMUser")
+    public String updateIMUser(@RequestBody ImHolder.UpdateUserInfo updateUserInfo) {
+        ImHolder imHolder = SpringContext.getBean(ImHolder.class);
+        return JSON.toJSONString(imHolder.updateImUserInfo(updateUserInfo));
+    }
+
+    @PostMapping("/handleFriendApply")
+    public String handleFriendApply(@RequestBody ImHolder.FriendApplyInfo friendApplyInfo) {
+        ImHolder imHolder = SpringContext.getBean(ImHolder.class);
+        return imHolder.handleFriendApply(friendApplyInfo);
+    }
+
+    @PostMapping("/getFriendList")
+    public String getFriendList(@RequestBody ImHolder.FriendList friendList) {
+        ImHolder imHolder = SpringContext.getBean(ImHolder.class);
+        return imHolder.getFriendList(friendList);
+    }
+
+    @PostMapping("/deleteFriend")
+    public String deleteFriend(@RequestBody ImHolder.DeleteFriend deleteFriend) {
+        ImHolder imHolder = SpringContext.getBean(ImHolder.class);
+        return imHolder.deleteFriend(deleteFriend);
+    }
+
+    @PostMapping("/checkFriendCountIsOverMaxFriendCount")
+    public Boolean checkFriendCountIsOverMaxFriendCount(String userId) {
+        ImHolder imHolder = SpringContext.getBean(ImHolder.class);
+        return imHolder.checkFriendCountIsOverMaxFriendCount(userId, 1000);
+    }
+
+    @PostMapping("/bindInvForUser")
+    public R bindInvForUser(@RequestBody AppUserInvitationCodeDTO reqData) {
+        return appUserInvitationCodeService.bindInvForUser(reqData);
+    }
+
+    @PostMapping(value = "/callbackAfterSendSingleMsgCommand")
+    public OpenImMsgCallBackResponse openImMsgCallBack(@RequestBody String body, HttpServletRequest request) throws JsonProcessingException {
+
+        Gson gson = new Gson();
+        OpenImMsgCallBackVO messageInfo = gson.fromJson(body, OpenImMsgCallBackVO.class);
+        openImAsyncService.handleCallback(messageInfo);
+//        log.info("收到的参数{}", JSON.toJSONString(messageInfo));
+        OpenImMsgCallBackResponse response = new OpenImMsgCallBackResponse();
+        response.setActionCode(0);
+        response.setErrCode(0);
+        response.setErrMsg("");
+        response.setErrDlt("");
+        response.setNextCode(0);
+        return response;
+    }
+
+
+
+    private final AppCustomerRoleMapper appCustomerRoleMapper;
+
+    private final AppSopUserLogsInfoHandle appSopUserLogsInfoHandle;
+
+    /**
+     * 加入或创建营期
+     *
+     * @param userIds
+     * @return
+     */
+    @GetMapping("/joinOrCreateUserLogs/{userIds}")
+    public R joinOrCreateUserLogs(@PathVariable List<Long> userIds) {
+        for (Long userId : userIds) {
+            List<Long> customerIds = appCustomerRoleMapper.getCustomerIdsByUserId(userId);
+            customerIds.forEach(customerId -> {
+                this.appSopUserLogsInfoHandle.joinOrCreateSopUserLogsHandle(customerId, userId);
+            });
+        }
+        return R.ok();
+    }
+
+}

+ 46 - 0
fs-app-task/src/main/java/com/fs/app/core/ConditionalOnLoadControl.java

@@ -0,0 +1,46 @@
+package com.fs.app.core;
+
+import org.springframework.context.annotation.Conditional;
+
+import java.lang.annotation.*;
+
+/**
+ * 控制当前组件是否加载
+ */
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Conditional(LoadControlCondition.class)//自定义加载规则类
+public @interface ConditionalOnLoadControl {
+
+    /**
+     * 当前类的匹配标识(如 class1、class2)
+     */
+    String[] value();
+
+    /**
+     * 多个value的时候,true;全部匹配,false:任一匹配
+     */
+    boolean matchAll() default false;
+
+    /**
+     * 配置项的前缀(默认 my.load)
+     */
+    String prefix() default "config.load";
+
+    /**
+     * 配置项的名称(默认 include)
+     */
+    String include() default "include";
+
+    /**
+     * 排除加载,包含在内的组件不加载
+     */
+    String exclude() default "exclude";
+
+    /**
+     * 缺失配置是否加载
+     */
+    boolean matchIfMissing() default false;
+
+}

+ 86 - 0
fs-app-task/src/main/java/com/fs/app/core/LoadControlCondition.java

@@ -0,0 +1,86 @@
+package com.fs.app.core;
+
+import cn.hutool.core.util.ObjectUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.springframework.context.annotation.Condition;
+import org.springframework.context.annotation.ConditionContext;
+import org.springframework.core.env.Environment;
+import org.springframework.core.type.AnnotatedTypeMetadata;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * 条件加载控制
+ */
+@Slf4j
+public class LoadControlCondition implements Condition {
+
+    @Override
+    public boolean matches(@NonNull ConditionContext context, AnnotatedTypeMetadata metadata) {
+        Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnLoadControl.class.getName());
+        if (null == attributes) {
+            return false;
+        }
+        // 获取注解属性:当前类的匹配标识
+        String[] targetValue = (String[]) attributes.get("value");
+        if (ObjectUtil.isEmpty(targetValue)) {
+            return false;
+        }
+        //缺失配置是否加载
+        boolean matchIfMissing = attributes.get("matchIfMissing").equals(true);
+        log.info("当前类的加载条件,缺失配置是否加载:{}", matchIfMissing);
+        //是否全匹配
+        boolean matchAll = attributes.get("matchAll").equals(true);
+        log.info("当前类的加载条件是否需要全部匹配:{}", matchAll);
+        //当前类加载条件
+        List<String> valueList = Arrays.stream(targetValue).collect(Collectors.toList());
+        log.info("当前类的加载条件为:{}", valueList);
+        // 获取配置项前缀和名称
+        String prefix = attributes.get("prefix") + "";
+        String includeName = attributes.get("include") + "";
+        String excludeName = attributes.get("exclude") + "";
+        String includeKey = prefix + "." + includeName;
+        String excludeKey = prefix + "." + excludeName;
+        // 解析YML配置
+        Environment environment = context.getEnvironment();
+        //不需要加载的组件
+        String[] excludeValues = environment.getProperty(excludeKey, String[].class);
+        List<String> excludeList = Optional.ofNullable(excludeValues)
+                .map(Arrays::asList)
+                .orElse(new ArrayList<>());
+        if (ObjectUtil.isNotEmpty(excludeList)
+                && (excludeList.contains("*") || valueList.stream().anyMatch(excludeList::contains))) {
+            log.info("当前类被排除加载");
+            return false;
+        }
+        //需要加载的组件
+        String[] includeValues = environment.getProperty(includeKey, String[].class);
+        List<String> includeList = Optional.ofNullable(includeValues)
+                .map(Arrays::asList)
+                .orElse(new ArrayList<>());
+        //缺失了yml配置,且当前条件加载开启了缺失加载
+        if (ObjectUtil.isEmpty(includeList) && matchIfMissing) {
+            return true;
+        }
+        //配置为 * → 加载所有
+        boolean isLoad1 = includeList.contains("*") || (ObjectUtil.isEmpty(includeList) && "*".equals(environment.getProperty(includeKey)));
+        log.info("加载场景1,加载匹配结果:{}", isLoad1);
+        if (isLoad1) {
+            return true;
+        }
+        // 配置为字符串/数组 → 匹配目标值
+        boolean isLoad2;
+        //全匹配
+        if (matchAll) {
+            isLoad2 = new HashSet<>(includeList).containsAll(valueList);
+        }
+        //任一匹配
+        else {
+            isLoad2 = valueList.stream().anyMatch(includeList::contains);
+        }
+        log.info("加载场景2,加载匹配结果:{}", isLoad2);
+        return isLoad2;
+    }
+}

+ 15 - 0
fs-app-task/src/main/java/com/fs/app/init/CacheHolder.java

@@ -0,0 +1,15 @@
+package com.fs.app.init;
+
+import com.fs.app.cusrole.domain.AppCustomerRoleMember;
+import lombok.Data;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+@Component
+@Data
+public class CacheHolder {
+
+    private List<AppCustomerRoleMember> roleMemberList;
+
+}

+ 41 - 0
fs-app-task/src/main/java/com/fs/app/init/DictHolder.java

@@ -0,0 +1,41 @@
+package com.fs.app.init;
+
+import com.fs.app.cusrole.domain.AppCustomerRoleMember;
+import com.fs.app.cusrole.service.IAppCustomerRoleMemberService;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.List;
+
+@Component
+@RequiredArgsConstructor
+@Slf4j
+public class DictHolder {
+
+    private final IAppCustomerRoleMemberService appCustomerRoleMemberService;
+
+    private final CacheHolder cacheHolder;
+
+    @PostConstruct
+    public void load() {
+        loadCustomer();
+    }
+
+    public void loadCustomer() {
+        List<AppCustomerRoleMember> roleMemberList = this.appCustomerRoleMemberService.lambdaQuery()
+                .eq(AppCustomerRoleMember::getIsDelete, 0)
+                .list();
+        cacheHolder.setRoleMemberList(roleMemberList);
+//        redisCache.setCacheObject(AppConstantEnum.APP_DICT_CUSTOMER_KEY.getValue(), roleMemberList);
+//        log.info("加载字典:{}成功", AppConstantEnum.APP_DICT_CUSTOMER_KEY.getValue());
+    }
+
+    @Scheduled(cron = "0 0 */6 * * *")
+    public void scheduleLoad() {
+        loadCustomer();
+    }
+
+}

+ 50 - 0
fs-app-task/src/main/java/com/fs/app/properties/AppTaskProperties.java

@@ -0,0 +1,50 @@
+package com.fs.app.properties;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "task")
+public class AppTaskProperties {
+
+    /**
+     * 消费者相关配置
+     */
+    private Consumer consumer;
+
+    private Producer producer;
+
+    /**
+     * 营期用户分片大小
+     */
+    private Long userLogsInfoFragmentationCount = 2000L;
+
+    @Data
+    public static class Consumer {
+
+        /**
+         * 因可能存在多个消费者,所以需要从名义上区分开消费节点,不可重复
+         */
+        private String node = "0";
+
+        /**
+         * future 等待时长
+         */
+        private Integer futureTimeoutSeconds = 30;
+
+        /**
+         * 针对单条数据处理最大时长,超过则算作超时,单位分钟
+         */
+        private Integer consumeMaxHandleMinutes = 60;
+    }
+
+    @Data
+    public static class Producer {
+
+        private String node = "0";
+
+    }
+
+}

+ 176 - 0
fs-app-task/src/main/java/com/fs/app/task/consumer/customer/UserBindOrUnbindCustomerConsumer.java

@@ -0,0 +1,176 @@
+package com.fs.app.task.consumer.customer;
+
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.cusrole.mapper.AppCustomerRoleMapper;
+import com.fs.app.properties.AppTaskProperties;
+import com.fs.app.sender.properties.ConfigProperties;
+import com.fs.app.sop.domain.AppBindUnbindTagCustomer;
+import com.fs.app.sop.service.IAppBindUnbindTagCustomerService;
+import com.fs.app.sop.service.impl.AppSopUserLogsInfoHandle;
+import com.fs.app.tag.domain.FsUserCustomer;
+import com.fs.app.tag.mapper.FsUserCustomerMapper;
+import com.fs.app.welcome.service.IAppWelcomeService;
+import com.fs.common.redis.RedisBatchUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+@Component
+@Slf4j
+@RequiredArgsConstructor
+@ConditionalOnLoadControl(
+        value = {
+                "bindOrUnbindCustomer",
+                "consumer"
+        }
+)
+public class UserBindOrUnbindCustomerConsumer {
+
+    private final RedisBatchUtil redisBatchUtil;
+    private final FsUserCustomerMapper fsUserCustomerMapper;
+    private final IAppWelcomeService appWelcomeService;
+    private final AppTaskProperties appTaskProperties;
+    private final ConfigProperties configProperties;
+    private final AppCustomerRoleMapper appCustomerRoleMapper;
+    private final AppSopUserLogsInfoHandle appSopUserLogsInfoHandle;
+    private final IAppBindUnbindTagCustomerService appBindUnbindTagCustomerService;
+
+    @Value("${server.port}")
+    private String port;
+
+    // 节点唯一标识
+    private String nodeId;
+
+    // 初始化节点 ID
+    @PostConstruct
+    public void init() {
+        log.info("配置参数:{}", configProperties);
+        String node = appTaskProperties.getConsumer().getNode();
+        nodeId = node + "-" + port;//节点编号+端口生成唯一节点编号
+        if (!redisBatchUtil.consumerNodeExists(nodeId)) {
+            redisBatchUtil.setConsumerNode(nodeId);
+            log.info("当前消费者节点:{}加载成功", nodeId);
+        } else {
+            log.warn("当前消费者节点:{}已存在", nodeId);
+        }
+        log.info("执行用户 绑定/解绑 客服任务已装载!!!!");
+    }
+
+    /**
+     * 用户绑定客服
+     */
+    @Scheduled(fixedDelayString = "${task.consumer.consume-bind-customer-fixed-delay:5000}", initialDelay = 10000)
+    public void userBindCustomer() {
+        try {
+            StringBuilder nodeFlag = new StringBuilder(nodeId).append("@compensate");
+            Date handleTime = new Date();
+            //锁定范围数据
+            this.appBindUnbindTagCustomerService.lambdaUpdate()
+                    .set(AppBindUnbindTagCustomer::getHandleStatus, 1)
+                    .set(AppBindUnbindTagCustomer::getHandleTime, handleTime)
+                    .set(AppBindUnbindTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppBindUnbindTagCustomer::getHandleStatus, 0)
+                    .eq(AppBindUnbindTagCustomer::getType, 2)
+                    .isNull(AppBindUnbindTagCustomer::getHandleNode)
+                    .last("ORDER BY id LIMIT " + configProperties.getBatchSize())
+                    .update();
+            // 查询需要处理的数据
+            List<AppBindUnbindTagCustomer> pendingList = appBindUnbindTagCustomerService.lambdaQuery()
+                    .eq(AppBindUnbindTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppBindUnbindTagCustomer::getHandleStatus, 1)
+                    .eq(AppBindUnbindTagCustomer::getType, 2)
+                    .list();
+            if (ObjectUtil.isEmpty(pendingList)) {
+                return;
+            }
+            List<FsUserCustomer> fsUserCustomers = new ArrayList<>(pendingList.size());
+            Long batchId = IdUtil.getSnowflake(0, 0).nextId();
+            pendingList.forEach(appBindUnbindTagCustomer -> {
+                String[] customerIds = appBindUnbindTagCustomer.getIds().split(",");
+                for (String customer : customerIds) {
+                    FsUserCustomer fsUserCustomer = new FsUserCustomer();
+                    fsUserCustomer.setFsUserId(appBindUnbindTagCustomer.getFsUserId());
+                    fsUserCustomer.setCustomerRoleId(Long.parseLong(customer));
+                    fsUserCustomer.setBatchId(batchId);
+                    fsUserCustomers.add(fsUserCustomer);
+                }
+                appBindUnbindTagCustomer.setHandleStatus(2);
+            });
+            this.fsUserCustomerMapper.batchBindUserCustomer(fsUserCustomers, true, batchId);
+            for (FsUserCustomer fsUserCustomer : fsUserCustomers) {
+                // 获取当前用户绑定的所有客服
+                List<Long> customerIds = appCustomerRoleMapper.getCustomerIdsByUserId(fsUserCustomer.getFsUserId());
+                //检查是否需要加入对应的营期内
+                customerIds.forEach(cId -> this.appSopUserLogsInfoHandle.joinOrCreateSopUserLogsHandle(cId, fsUserCustomer.getFsUserId()));
+                //发送欢迎语
+                this.appWelcomeService.saveWaitSendLogs(fsUserCustomer.getFsUserId(), batchId);
+            }
+            //设置状态为已处理
+            this.appBindUnbindTagCustomerService.updateBatchById(pendingList);
+        } catch (Exception e) {
+            log.error("节点[{}]执行绑定用户客服失败:{}", this.nodeId, e.getMessage());
+        }
+    }
+
+    /**
+     * 用户解绑客服
+     */
+    @Scheduled(fixedDelayString = "${task.consumer.consume-unbind-customer-fixed-delay:10000}", initialDelay = 0)
+    public void userUnbindCustomer() {
+        try {
+            StringBuilder nodeFlag = new StringBuilder(nodeId).append("@compensate");
+            Date handleTime = new Date();
+            //锁定范围数据
+            this.appBindUnbindTagCustomerService.lambdaUpdate()
+                    .set(AppBindUnbindTagCustomer::getHandleStatus, 1)
+                    .set(AppBindUnbindTagCustomer::getHandleTime, handleTime)
+                    .set(AppBindUnbindTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppBindUnbindTagCustomer::getHandleStatus, 0)
+                    .eq(AppBindUnbindTagCustomer::getType, 3)
+                    .isNull(AppBindUnbindTagCustomer::getHandleNode)
+                    .last("ORDER BY id LIMIT " + configProperties.getBatchSize())
+                    .update();
+            // 查询需要处理的数据
+            List<AppBindUnbindTagCustomer> pendingList = appBindUnbindTagCustomerService.lambdaQuery()
+                    .eq(AppBindUnbindTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppBindUnbindTagCustomer::getHandleStatus, 1)
+                    .eq(AppBindUnbindTagCustomer::getType, 3)
+                    .list();
+            if (ObjectUtil.isEmpty(pendingList)) {
+                return;
+            }
+            List<FsUserCustomer> fsUserCustomers = new ArrayList<>(pendingList.size());
+            Long batchId = IdUtil.getSnowflake(0, 0).nextId();
+            pendingList.forEach(appBindUnbindTagCustomer -> {
+                String[] customerIds = appBindUnbindTagCustomer.getIds().split(",");
+                for (String customer : customerIds) {
+                    FsUserCustomer fsUserCustomer = new FsUserCustomer();
+                    fsUserCustomer.setFsUserId(appBindUnbindTagCustomer.getFsUserId());
+                    fsUserCustomer.setCustomerRoleId(Long.parseLong(customer));
+                    fsUserCustomer.setBatchId(batchId);
+                    fsUserCustomers.add(fsUserCustomer);
+                }
+                appBindUnbindTagCustomer.setHandleStatus(2);
+            });
+            this.fsUserCustomerMapper.batchUnBindUserCustomer(fsUserCustomers);
+            for (FsUserCustomer fsUserCustomer : fsUserCustomers) {
+                //检查是否需要从的营期内移除
+                this.appSopUserLogsInfoHandle.quitSopUserLogsHandle(fsUserCustomer.getCustomerRoleId(), fsUserCustomer.getFsUserId());
+            }
+            //设置状态为已处理
+            this.appBindUnbindTagCustomerService.updateBatchById(pendingList);
+        } catch (Exception e) {
+            log.error("节点[{}]执行解绑用户客服失败:{}", this.nodeId, e.getMessage());
+        }
+    }
+
+}

+ 486 - 0
fs-app-task/src/main/java/com/fs/app/task/consumer/sop/AppSopLogsConsumer.java

@@ -0,0 +1,486 @@
+package com.fs.app.task.consumer.sop;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONException;
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.properties.AppTaskProperties;
+import com.fs.app.sender.AbstractAppMessageSender;
+import com.fs.app.sender.MessageSenderResult;
+import com.fs.app.sender.MessageSenderResultInfo;
+import com.fs.app.sender.MessageUserPayload;
+import com.fs.app.sender.enums.SenderType;
+import com.fs.app.sender.factory.AppSopLogsSenderFactory;
+import com.fs.app.sender.properties.ConfigProperties;
+import com.fs.app.sop.domain.AppSop;
+import com.fs.app.sop.domain.AppSopLogs;
+import com.fs.app.sop.dto.AutoSopTimeParam;
+import com.fs.app.sop.result.AppSopCourseFinishTempSetting;
+import com.fs.app.sop.service.IAppSopLogsService;
+import com.fs.app.sop.service.IAppSopService;
+import com.fs.app.watchlog.domain.AppCourseWatchLog;
+import com.fs.app.watchlog.service.IAppCourseWatchLogService;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.redis.RedisBatchUtil;
+import com.fs.common.utils.date.DateUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.time.LocalDateTime;
+import java.util.*;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.stream.Collectors;
+
+@Component
+@Slf4j
+@RequiredArgsConstructor
+@ConditionalOnLoadControl(
+        value = {
+                "sendSopLogs",
+                "consumer"
+        }
+)
+public class AppSopLogsConsumer {
+
+    private final IAppSopService appSopService;
+    private final IAppCourseWatchLogService appCourseWatchLogService;
+    private final IAppSopLogsService appSopLogsService;
+    private final ThreadPoolTaskExecutor consumerThreadPool;
+    private final RedisBatchUtil redisBatchUtil;
+    private final RedisCache redisCache;
+    private final AppSopLogsSenderFactory senderFactory;
+    private final AppTaskProperties appTaskProperties;
+    private final ConfigProperties configProperties;
+
+    // 当前正在发送的 SOP
+    private static final String CONSUMER_SOP_SEND_ID_KEY_PREFIX = "app:sop:consumer:send:id:";
+
+    @Value("${server.port}")
+    private String port;
+
+    // 节点唯一标识
+    private String nodeId;
+
+    // 初始化节点 ID
+    @PostConstruct
+    public void init() {
+        log.info("配置参数:{}", configProperties);
+        String node = appTaskProperties.getConsumer().getNode();
+        nodeId = node + "-" + port;//节点编号+端口生成唯一节点编号
+        if (!redisBatchUtil.consumerNodeExists(nodeId)) {
+            redisBatchUtil.setConsumerNode(nodeId);
+            log.info("当前消费者节点:{}加载成功", nodeId);
+        } else {
+            log.warn("当前消费者节点:{}已存在", nodeId);
+        }
+        log.info("发送sop待发记录任务已装载!!!!");
+    }
+
+    /**
+     * 消费逻辑
+     */
+    @Scheduled(fixedDelayString = "${task.consumer.compensate-fixed-delay:10000}", initialDelay = 10000)
+    public void sendSopLogs() {
+        try {
+            StringBuilder nodeFlag = new StringBuilder(nodeId).append("@compensate");
+            Date sendTime = new Date();
+            //锁定范围数据
+            appSopLogsService.lambdaUpdate()
+                    .set(AppSopLogs::getHandleStatus, 1)
+                    .set(AppSopLogs::getHandleTime, new Date())
+                    .set(AppSopLogs::getHandleNode, nodeFlag.toString())
+                    .eq(AppSopLogs::getHandleStatus, 0)
+                    .eq(AppSopLogs::getAppSendStatus, 0)
+                    .le(AppSopLogs::getSendTime, sendTime)
+                    .last("ORDER BY id LIMIT " + configProperties.getBatchSize())
+                    .update();
+
+            // 查询需要补偿的数据
+            List<AppSopLogs> pendingSops = appSopLogsService.lambdaQuery()
+                    .eq(AppSopLogs::getAppSendStatus, 0)
+                    .eq(AppSopLogs::getHandleStatus, 1)
+                    .eq(AppSopLogs::getHandleNode, nodeFlag.toString())
+                    .le(AppSopLogs::getSendTime, sendTime)
+                    .list();
+
+            if (ObjectUtil.isEmpty(pendingSops)) {
+                log.info("节点[{}],暂无需要补偿的数据,跳过", nodeId);
+                return;
+            }
+
+            // 异步处理发送逻辑
+            List<MessageSenderResult> mssList = new ArrayList<>();
+            List<Future<MessageSenderResult>> futureList = new ArrayList<>();
+            for (AppSopLogs sopLogs : pendingSops) {
+                Future<MessageSenderResult> future = consumerThreadPool.submit(() -> execute(sopLogs));
+                futureList.add(future);
+            }
+
+            // 收集结果
+            for (Future<MessageSenderResult> future : futureList) {
+                try {
+                    MessageSenderResult result = future.get(appTaskProperties.getConsumer().getFutureTimeoutSeconds(), TimeUnit.SECONDS);
+                    if (ObjectUtil.isNotEmpty(result)) {
+                        mssList.add(result);
+                    }
+                } catch (TimeoutException e) {
+                    log.error("节点[{}]:补偿任务超时,取消任务", nodeId, e);
+                    future.cancel(true);
+                } catch (Exception e) {
+                    log.error("节点[{}]:补偿处理失败:{}", nodeId, e.getMessage());
+                }
+            }
+
+            // 批次更新状态
+            if (ObjectUtil.isNotEmpty(mssList)) {
+                batchUpdateStatus(mssList);
+            }
+        } catch (Exception e) {
+            log.error("节点[{}]:补偿处理异常:{}", nodeId, e.getMessage(), e);
+        }
+    }
+
+    /**
+     * 批次更新日志状态
+     */
+    public void batchUpdateStatus(List<MessageSenderResult> mssList) {
+        if (ObjectUtil.isEmpty(mssList)) {
+            log.warn("批次更新状态:无待更新数据");
+            return;
+        }
+        List<AppSopLogs> updateList = new ArrayList<>(mssList.size());
+        for (MessageSenderResult result : mssList) {
+            if (ObjectUtil.isEmpty(result) || ObjectUtil.isEmpty(result.getId()) || ObjectUtil.isEmpty(result.getInfo())) {
+                log.warn("跳过空结果的状态更新");
+                continue;
+            }
+            int infoSize = result.getInfo().size();
+            // 获取成功的子项,因为一条sop_logs里面可能会有多个消息要发送
+            long successCount = Optional.of(result)
+                    .map(MessageSenderResult::getInfo)
+                    .orElse(Collections.emptyList())
+                    .stream()
+                    .filter(MessageSenderResultInfo::isSuccess)
+                    .count();
+            int sendStatus = 2;//默认全部失败
+            String message = "APP推送消息失败[全部]";
+            if (successCount == infoSize) {
+                sendStatus = 1;//全部成功
+                message = "APP推送消息成功[全部]";
+            } else if (successCount < infoSize) {
+                sendStatus = 4;//部分成功
+                message = "APP推送消息成功[部分]";
+            }
+            AppSopLogs updateEntity = new AppSopLogs();
+            updateEntity.setId(result.getId());
+            updateEntity.setIsHaveApp(1);
+            updateEntity.setRealSendTime(new Date());
+            updateEntity.setAppSendStatus(sendStatus);
+            updateEntity.setAppSendRemark(message);
+            updateEntity.setRemarkDetail(JSON.toJSONString(result.getInfo()));
+            updateEntity.setHandleStatus(2);//处理完成
+            updateList.add(updateEntity);
+        }
+        try {
+            appSopLogsService.updateBatchById(updateList);
+            log.info("批次更新状态,共{}条记录", updateList.size());
+        } catch (Exception e) {
+            log.error("批次更新状态失败", e);
+            throw e;
+        }
+    }
+
+    /**
+     * 执行发送逻辑
+     */
+    private MessageSenderResult execute(AppSopLogs sopLogs) {
+        String redisKey = CONSUMER_SOP_SEND_ID_KEY_PREFIX + sopLogs.getId();
+        MessageSenderResult result = new MessageSenderResult();
+        result.setId(sopLogs.getId());
+        try {
+            // 检查是否有节点正在处理
+            String nodeSendTime = redisCache.getCacheObject(redisKey);
+            if (nodeSendTime != null) {
+                String[] arr = nodeSendTime.split("@");
+                log.warn("消息ID:{}已被节点{}锁定处理中(锁定时间:{}),本次处理跳过",
+                        sopLogs.getId(), arr[0], new Date(Long.parseLong(arr[1])));
+                return null;
+            }
+
+            if (ObjectUtil.isEmpty(sopLogs.getContentJson())) {
+                log.error("节点[{}]消息ID:{} ContentJson为空,发送失败", nodeId, sopLogs.getId());
+                return null;
+            }
+
+            AppSopCourseFinishTempSetting setting;
+            try {
+                setting = JSON.parseObject(sopLogs.getContentJson(), AppSopCourseFinishTempSetting.class);
+            } catch (JSONException e) {
+                log.error("节点[{}]消息ID:{} JSON解析失败", nodeId, sopLogs.getId(), e);
+                return null;
+            }
+
+            //检查是否满足发送条件
+            if (!this.checkIsNeedSend(sopLogs, setting)) {
+                return null;
+            }
+
+            // 无配置则跳过
+            List<AppSopCourseFinishTempSetting.Setting> allContent = setting.getSetting();
+            if (ObjectUtil.isEmpty(allContent)) {
+                log.warn("节点[{}]消息ID:{}无发送配置,跳过", nodeId, sopLogs.getId());
+                return null;
+            }
+
+            // 标记发送中
+            redisCache.setCacheObject(redisKey, (nodeId + "@" + System.currentTimeMillis()), 30, TimeUnit.SECONDS);
+
+            // 课程消息(contentType=9)
+            List<AppSopCourseFinishTempSetting.Setting> courseList = allContent.stream()
+                    .filter(e -> "9".equals(e.getContentType()))
+                    .collect(Collectors.toList());
+
+            MessageUserPayload mu = new MessageUserPayload(sopLogs.getAppCustomerId(), sopLogs.getFsUserId());
+            if (ObjectUtil.isNotEmpty(courseList)) {
+                log.info("节点[{}]开始发送消息ID:{}的APP课程消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+                AbstractAppMessageSender courseSender = senderFactory.getSender(SenderType.COURSE.getType());
+                result.getInfo().addAll(courseSender.sendForSop(courseList, mu));
+            }
+
+            // 文本消息(contentType=11)
+            List<AppSopCourseFinishTempSetting.Setting> txtList = allContent.stream()
+                    .filter(e -> "11".equals(e.getContentType()))
+                    .collect(Collectors.toList());
+
+            if (ObjectUtil.isNotEmpty(txtList)) {
+                log.info("节点[{}]开始发送消息ID:{}的APP文本消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+                AbstractAppMessageSender textSender = senderFactory.getSender(SenderType.TXT.getType());
+                result.getInfo().addAll(textSender.sendForSop(txtList, mu));
+            }
+
+            // 语音消息(contentType=12)
+            List<AppSopCourseFinishTempSetting.Setting> voiceList = allContent.stream()
+                    .filter(e -> "12".equals(e.getContentType()))
+                    .collect(Collectors.toList());
+            if (ObjectUtil.isNotEmpty(voiceList)) {
+                log.info("节点[{}]开始发送消息ID:{}的APP语音消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+                AbstractAppMessageSender voiceSender = senderFactory.getSender(SenderType.VOICE.getType());
+                result.getInfo().addAll(voiceSender.sendForSop(voiceList, mu));
+            }
+
+            // 疗法消息(contentType=20)
+//            List<AppSopCourseFinishTempSetting.Setting> pkgList = allContent.stream()
+//                    .filter(e -> "20".equals(e.getContentType()))
+//                    .collect(Collectors.toList());
+//            if (ObjectUtil.isNotEmpty(pkgList)) {
+//                log.info("节点[{}]开始发送消息ID:{}的APP疗法消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+//                AbstractAppMessageSender pkgSender = senderFactory.getSender(SenderType.PACKAGE.getType());
+//                result.getInfo().addAll(pkgSender.sendForSop(pkgList, mu));
+//            }
+//
+//            // 图片消息(contentType=2)
+            List<AppSopCourseFinishTempSetting.Setting> imgList = allContent.stream()
+                    .filter(e -> "2".equals(e.getContentType()))
+                    .collect(Collectors.toList());
+            if (ObjectUtil.isNotEmpty(imgList)) {
+                log.info("节点[{}]开始发送消息ID:{}的APP图片消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+                AbstractAppMessageSender imgSender = senderFactory.getSender(SenderType.IMG.getType());
+                result.getInfo().addAll(imgSender.sendForSop(imgList, mu));
+            }
+//
+//            // 直播消息(contentType=21)
+//            List<AppSopCourseFinishTempSetting.Setting> liveList = allContent.stream()
+//                    .filter(e -> "21".equals(e.getContentType()))
+//                    .collect(Collectors.toList());
+//            if (ObjectUtil.isNotEmpty(liveList)) {
+//                log.info("节点[{}]开始发送消息ID:{}的APP直播消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+//                AbstractAppMessageSender liveSender = senderFactory.getSender(SenderType.LIVE.getType());
+//                result.getInfo().addAll(liveSender.sendForSop(liveList, mu));
+//            }
+//
+//            // 民品消息(contentType=22)
+//            List<AppSopCourseFinishTempSetting.Setting> productList = allContent.stream()
+//                    .filter(e -> "22".equals(e.getContentType()))
+//                    .collect(Collectors.toList());
+//            if (ObjectUtil.isNotEmpty(productList)) {
+//                log.info("节点[{}]开始发送消息ID:{}的APP民品消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+//                AbstractAppMessageSender productSender = senderFactory.getSender(SenderType.PRODUCT.getType());
+//                result.getInfo().addAll(productSender.sendForSop(productList, mu));
+//            }
+//
+//            // 药品消息(contentType=23)
+//            List<AppSopCourseFinishTempSetting.Setting> medicinesList = allContent.stream()
+//                    .filter(e -> "23".equals(e.getContentType()))
+//                    .collect(Collectors.toList());
+//            if (ObjectUtil.isNotEmpty(medicinesList)) {
+//                log.info("节点[{}]开始发送消息ID:{}的APP药品消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+//                AbstractAppMessageSender medicinesSender = senderFactory.getSender(SenderType.MEDICINES.getType());
+//                result.getInfo().addAll(medicinesSender.sendForSop(medicinesList, mu));
+//            }
+//
+//            // 短视频消息(contentType=24)
+//            List<AppSopCourseFinishTempSetting.Setting> shortVideoList = allContent.stream()
+//                    .filter(e -> "24".equals(e.getContentType()))
+//                    .collect(Collectors.toList());
+//            if (ObjectUtil.isNotEmpty(shortVideoList)) {
+//                log.info("节点[{}]开始发送消息ID:{}的APP短视频消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+//                AbstractAppMessageSender shortVideoSender = senderFactory.getSender(SenderType.SHORT_VIDEO.getType());
+//                result.getInfo().addAll(shortVideoSender.sendForSop(shortVideoList, mu));
+//            }
+//
+//            // 文章消息(contentType=25)
+//            List<AppSopCourseFinishTempSetting.Setting> articleList = allContent.stream()
+//                    .filter(e -> "25".equals(e.getContentType()))
+//                    .collect(Collectors.toList());
+//            if (ObjectUtil.isNotEmpty(articleList)) {
+//                log.info("节点[{}]开始发送消息ID:{}的APP文章消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+//                AbstractAppMessageSender articleSender = senderFactory.getSender(SenderType.ARTICLE.getType());
+//                result.getInfo().addAll(articleSender.sendForSop(articleList, mu));
+//            }
+//
+//            // 文章消息(contentType=26)
+//            List<AppSopCourseFinishTempSetting.Setting> openClassVideoList = allContent.stream()
+//                    .filter(e -> "26".equals(e.getContentType()))
+//                    .collect(Collectors.toList());
+//            if (ObjectUtil.isNotEmpty(openClassVideoList)) {
+//                log.info("节点[{}]开始发送消息ID:{}的APP公开课消息,用户:{}", nodeId, sopLogs.getId(), sopLogs.getFsUserId());
+//                AbstractAppMessageSender openClassVideoSender = senderFactory.getSender(SenderType.OPEN_CLASS.getType());
+//                result.getInfo().addAll(openClassVideoSender.sendForSop(openClassVideoList, mu));
+//            }
+
+            log.info("节点[{}]消息ID:{}发送完成,子项数量:{}", nodeId, sopLogs.getId(), result.getInfo().size());
+            return result;
+        } catch (Exception e) {
+            log.error("节点[{}]消息ID:{}发送逻辑异常", nodeId, sopLogs.getId(), e);
+            return result;
+        } finally {
+            // 清理Redis防重发key
+            try {
+                redisCache.deleteObject(redisKey);
+            } catch (Exception e) {
+                log.error("节点[{}]消息ID:{}清理Redis key失败", nodeId, sopLogs.getId(), e);
+            }
+        }
+    }
+
+    /**
+     * 检查是否满足发送条件
+     *
+     * @param sopLogs
+     * @return
+     */
+    private boolean checkIsNeedSend(AppSopLogs sopLogs, AppSopCourseFinishTempSetting setting) {
+        AppSop sop = this.appSopService.lambdaQuery()
+                .eq(AppSop::getId, sopLogs.getSopId())
+                .one();
+        if (ObjectUtil.isEmpty(sop) || ObjectUtil.isEmpty(sop.getExpiryTime())) {
+            // 作废消息,未配置过期消息
+            log.info("待发记录,LOG_ID:{}, SOP任务被删除或未配置过期时间", sopLogs.getId());
+            this.setAppSopLogsNotSend(sopLogs.getId(), "SOP任务被删除/或配置错误");
+            return false;
+        }
+        //消息发送时间
+        LocalDateTime sendTime = DateUtil.dateToLocalDateTime(sopLogs.getSendTime());
+        LocalDateTime expiryDateTime = sendTime.plusHours(sop.getExpiryTime());
+        if (ObjectUtil.isNotEmpty(sop.getAutoSopTime())) {
+            AutoSopTimeParam param = JSON.parseObject(sop.getAutoSopTime(), AutoSopTimeParam.class);
+            //已过期,且sop配置的过期消息是否发送为否
+            if (LocalDateTime.now().isAfter(expiryDateTime) && 2 == param.getAutoSopSend()) {
+                // 作废消息
+                log.info("待发记录,LOG_ID:{}, 已过期,不发送", sopLogs.getId());
+                this.setAppSopLogsNotSend(sopLogs.getId(), "已过期,不发送");
+                return false;
+            }
+        }
+        Integer courseType;
+        //模板未指定消息类型
+        if (ObjectUtil.isEmpty(courseType = setting.getCourseType())) {
+            log.info("待发记录,LOG_ID:{}, 模板未选消息类型,不发送", sopLogs.getId());
+            this.setAppSopLogsNotSend(sopLogs.getId(), "模板未选消息类型,不发送");
+            return false;
+        }
+        Integer cacheValue = redisCache.getCacheObject("sopCourse:video:isPause:" + setting.getVideoId());
+        int isPause = (cacheValue != null) ? cacheValue : 0;
+        log.info("待发记录,LOG_ID:{},判断课程({})小节({})当前状态:{}", sopLogs.getId(), setting.getCourseId(), setting.getVideoId(), isPause);
+        //课节被暂停
+        if (isPause == 1) {
+            log.info("待发记录,LOG_ID:{}, 课节暂停,不发送", sopLogs.getId());
+            this.setAppSopLogsNotSend(sopLogs.getId(), "课节暂停,不发送");
+            return false;
+        }
+        if (courseType != 0) {// 非普通消息,进行复杂的条件判断
+            AppCourseWatchLog watchLog = this.appCourseWatchLogService.lambdaQuery()
+                    .eq(AppCourseWatchLog::getVideoId, setting.getVideoId())//课节id
+                    .eq(AppCourseWatchLog::getAppCustomerId, sopLogs.getAppCustomerId())//客服id
+                    .eq(AppCourseWatchLog::getUserId, sopLogs.getFsUserId())//用户
+                    .eq(AppCourseWatchLog::getSourceType, 0)//来源:sop
+                    .one();
+            log.debug("待发记录,LOG_ID:{}-看课记录参数:videoId:{}, customerRoleId:{}, fsUserId:{}",
+                    sopLogs.getId(), setting.getVideoId(), sopLogs.getAppCustomerId(), sopLogs.getFsUserId());
+            log.debug("待发记录,LOG_ID:{}-看课记录:{}", sopLogs.getId(), watchLog);
+            if (ObjectUtil.isNotEmpty(watchLog)) {
+                //课程类型
+                if (!isCourseTypeValid(courseType, watchLog.getLogType())) {
+                    // 作废消息
+                    log.info("待发记录,LOG_ID:{}, 看课状态未满足,不发送", sopLogs.getId());
+                    this.setAppSopLogsNotSend(sopLogs.getId(), "看课状态未满足,不发送");
+                    return false;
+                }
+            } else {
+                log.info("待发记录,LOG_ID:{}, 无观看记录,不发送", sopLogs.getId());
+                this.setAppSopLogsNotSend(sopLogs.getId(), "无观看记录,不发送");
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     *
+     * @param courseType 模板消息类型(0-普通消息,1-待看课消息,2-看课中断消息,3-已完课消息,4-待看和中断消息)
+     * @param logType    看课记录类型(1-看课中 2-完课 3-待看课 4-看课中断)
+     * @return boolean
+     */
+    private boolean isCourseTypeValid(Integer courseType, Long logType) {
+        switch (courseType) {
+            case 0://普通消息
+                return true;  // courseType == 0 直接返回 true
+            case 1://1-待看课消息
+                return logType == 3;  // 对应看课记录类型:待看课 4看课中断
+            case 2://2-看课中断消息
+                return logType == 4;  // 对应看课记录类型:看课中断
+            case 3://3-已完课消息
+                return logType == 2;  // 对应看课记录类型:完课
+            case 4://4-待看和中断消息
+                return logType == 3 || logType == 4;
+            default:
+                return false;  // 其他情况返回 false
+        }
+    }
+
+    /**
+     * 设置待发记录状态为不发送
+     *
+     * @param sopLogsId
+     * @param remark
+     */
+    private void setAppSopLogsNotSend(Long sopLogsId, String remark) {
+        this.appSopLogsService.lambdaUpdate()
+                .set(AppSopLogs::getSendStatus, 5)//已作废
+                .set(AppSopLogs::getReceivingStatus, 4)//
+                .set(AppSopLogs::getAppSendStatus, 3)//不发送
+                .set(AppSopLogs::getHandleStatus, 3)//不再处理
+                .set(AppSopLogs::getRemark, remark)
+                .set(AppSopLogs::getAppSendRemark, remark)
+                .set(AppSopLogs::getUpdateTime, new Date())
+                .eq(AppSopLogs::getId, sopLogsId)
+                .update();
+    }
+}

+ 402 - 0
fs-app-task/src/main/java/com/fs/app/task/consumer/sop/AppSopTagCustomerConsumer.java

@@ -0,0 +1,402 @@
+package com.fs.app.task.consumer.sop;
+
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSON;
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.cusrole.mapper.AppCustomerRoleMapper;
+import com.fs.app.properties.AppTaskProperties;
+import com.fs.app.sender.properties.ConfigProperties;
+import com.fs.app.sop.domain.AppSopTagCustomer;
+import com.fs.app.sop.service.IAppSopTagCustomerService;
+import com.fs.app.sop.service.impl.AppSopUserLogsInfoHandle;
+import com.fs.app.tag.domain.FsUserCustomer;
+import com.fs.app.tag.domain.FsUserTag;
+import com.fs.app.tag.mapper.FsUserCustomerMapper;
+import com.fs.app.tag.service.IFsUserTagService;
+import com.fs.app.welcome.service.IAppWelcomeService;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.redis.RedisBatchUtil;
+import lombok.Builder;
+import lombok.Data;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+@Component
+@Slf4j
+@RequiredArgsConstructor
+@ConditionalOnLoadControl(
+        value = {
+                "bindOrUnbindTagOrCustomer",
+                "consumer"
+        }
+)
+public class AppSopTagCustomerConsumer {
+
+
+    private final IAppSopTagCustomerService appSopTagCustomerService;
+    private final IFsUserTagService fsUserTagsService;
+    private final IAppWelcomeService appWelcomeService;
+    private final AppCustomerRoleMapper appCustomerRoleMapper;
+    private final FsUserCustomerMapper fsUserCustomerMapper;
+    private final AppSopUserLogsInfoHandle appSopUserLogsInfoHandle;
+    private final ThreadPoolTaskExecutor consumerThreadPool;
+    private final RedisBatchUtil redisBatchUtil;
+    private final RedisCache redisCache;
+    private final AppTaskProperties appTaskProperties;
+    private final ConfigProperties configProperties;
+
+    // 当前正在发送的 SOP
+    private static final String CONSUMER_SOP_EXECUTE_ID_KEY_PREFIX = "app:sop:tag_customer_consumer:execute:id:";
+
+    @Value("${server.port}")
+    private String port;
+
+    // 节点唯一标识
+    private String nodeId;
+
+    // 初始化节点 ID
+    @PostConstruct
+    public void init() {
+        log.info("配置参数:{}", configProperties);
+        String node = appTaskProperties.getConsumer().getNode();
+        nodeId = node + "-" + port;//节点编号+端口生成唯一节点编号
+        if (!redisBatchUtil.consumerNodeExists(nodeId)) {
+            redisBatchUtil.setConsumerNode(nodeId);
+            log.info("当前消费者节点:{}加载成功", nodeId);
+        } else {
+            log.warn("当前消费者节点:{}已存在", nodeId);
+        }
+        log.info("sop绑定/解绑 - 标签/客服任务已装载!!!!");
+    }
+
+    //TODO:数据量大时可以效仿sop发送消息/课程那边做多节点批次消费再结合以下补偿数据
+
+    /**
+     * 补偿消费数据:遍历未完成批次,补漏空批次/失败批次
+     */
+    @Scheduled(fixedDelayString = "${task.consumer.compensate-fixed-delay:5000}", initialDelay = 15000)
+    public void compensateSopTagCustomerTask() {
+        try {
+            StringBuilder nodeFlag = new StringBuilder(nodeId).append("@compensate");
+            Date executeTime = new Date();
+            // 清除毫秒(关键代码)
+            executeTime.setTime(executeTime.getTime() / 1000 * 1000);
+            //锁定范围数据
+            appSopTagCustomerService.lambdaUpdate()
+                    .set(AppSopTagCustomer::getExecStatus, 1)
+                    .set(AppSopTagCustomer::getHandleNode, nodeFlag.toString())
+                    .set(AppSopTagCustomer::getHandleTime, executeTime)
+                    .eq(AppSopTagCustomer::getExecStatus, 0)
+                    .le(AppSopTagCustomer::getExpectExecTime, executeTime)
+                    .last(" ORDER BY id LIMIT " + configProperties.getBatchSize())
+                    .update();
+            // 查询需要补偿的数据
+            List<AppSopTagCustomer> pendingSops = appSopTagCustomerService.lambdaQuery()
+                    .eq(AppSopTagCustomer::getExecStatus, 1)
+                    .eq(AppSopTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppSopTagCustomer::getHandleTime, executeTime)
+                    .list();
+            if (ObjectUtil.isEmpty(pendingSops)) {
+                log.info("节点[{}]-[sop绑定/解绑-标签-客服],暂无需要补偿的数据,跳过", nodeId);
+                return;
+            }
+            // 异步处理发送逻辑
+            List<ExecuteInfo> mssList = new ArrayList<>();
+            List<Future<ExecuteInfo>> futureList = new ArrayList<>();
+            for (AppSopTagCustomer sopTagCustomer : pendingSops) {
+                Future<ExecuteInfo> future = consumerThreadPool.submit(() -> execute(sopTagCustomer));
+                futureList.add(future);
+            }
+            // 收集结果
+            for (Future<ExecuteInfo> future : futureList) {
+                try {
+                    ExecuteInfo result = future.get(appTaskProperties.getConsumer().getFutureTimeoutSeconds(), TimeUnit.SECONDS);
+                    if (ObjectUtil.isNotEmpty(result)) {
+                        mssList.add(result);
+                    }
+                } catch (TimeoutException e) {
+                    log.error("节点[{}]-[sop绑定/解绑-标签-客服]:补偿任务超时,取消任务", nodeId, e);
+                    future.cancel(true);
+                } catch (Exception e) {
+                    log.error("节点[{}]-[sop绑定/解绑-标签-客服]:补偿处理失败:{}", nodeId, e.getMessage());
+                }
+            }
+
+            // 批次更新状态
+            if (ObjectUtil.isNotEmpty(mssList)) {
+                batchUpdateStatus(mssList);
+            }
+        } catch (Exception e) {
+            log.error("节点[{}]执行自动打标签异常:", nodeId, e);
+        }
+    }
+
+    /**
+     * 批次更新待执行状态
+     */
+    public void batchUpdateStatus(List<ExecuteInfo> mssList) {
+        if (ObjectUtil.isEmpty(mssList)) {
+            log.warn("批次更新状态:无待更新数据");
+            return;
+        }
+        List<AppSopTagCustomer> updateList = new ArrayList<>(mssList.size());
+        for (ExecuteInfo result : mssList) {
+            if (ObjectUtil.isEmpty(result) || ObjectUtil.isEmpty(result.getId())) {
+                log.warn("跳过空结果的状态更新");
+                continue;
+            }
+            // 判断是否有发送失败的子项
+            boolean hasFailed = result.getExecuteDetails() != null
+                    && result.getExecuteDetails().stream().anyMatch(r -> !r.isSuccess());
+            AppSopTagCustomer updateEntity = new AppSopTagCustomer();
+            updateEntity.setId(result.getId());
+            updateEntity.setExecStatus(2);
+            updateEntity.setActualExecTime(new Date());
+            updateEntity.setExecSuccess(hasFailed ? 0 : 1);
+            updateEntity.setErrorMessage(hasFailed ? "SOP改状态失败" : "SOP改状态成功");
+            updateEntity.setExecDetail(JSON.toJSONString(result.getExecuteDetails()));
+            updateList.add(updateEntity);
+        }
+        try {
+            appSopTagCustomerService.updateBatchById(updateList);
+            log.info("批次更新状态,共{}条记录", updateList.size());
+        } catch (Exception e) {
+            log.error("批次更新状态失败", e);
+            throw e;
+        }
+    }
+
+    /**
+     * 执行 添加/移除 标签逻辑
+     */
+    private ExecuteInfo execute(AppSopTagCustomer sopTagCustomer) {
+        String redisKey = CONSUMER_SOP_EXECUTE_ID_KEY_PREFIX + sopTagCustomer.getId();
+        ExecuteInfo result = new ExecuteInfo();
+        result.setId(sopTagCustomer.getId());
+        try {
+            // 检查是否有节点正在处理
+            String check = redisCache.getCacheObject(redisKey);
+            if (check != null) {
+                String[] arr = check.split("@");
+                log.warn("[sop绑定/解绑-标签-客服]-ID:{}已被节点{}锁定处理中(锁定时间:{}),本次处理跳过",
+                        sopTagCustomer.getId(), arr[0], new Date(Long.parseLong(arr[1])));
+                return null;
+            }
+            // 标记处理中
+            redisCache.setCacheObject(redisKey, (nodeId + "@" + System.currentTimeMillis()), 30, TimeUnit.SECONDS);
+            //处理打标签
+            if (ObjectUtil.isNotEmpty(sopTagCustomer.getAddTag())) {
+                result.getExecuteDetails().add(this.bindTag(sopTagCustomer.getFsUserId(), sopTagCustomer.getAddTag()));
+            }
+            //处理移除标签
+            if (ObjectUtil.isNotEmpty(sopTagCustomer.getDelTag())) {
+                result.getExecuteDetails().add(this.unBindTag(sopTagCustomer.getFsUserId(), sopTagCustomer.getDelTag()));
+            }
+            //处理绑定客服
+            if (ObjectUtil.isNotEmpty(sopTagCustomer.getAddCustomer())) {
+                result.getExecuteDetails().add(this.bindCustomer(sopTagCustomer.getFsUserId(), sopTagCustomer.getAddCustomer()));
+            }
+            //处理解绑客服
+            if (ObjectUtil.isNotEmpty(sopTagCustomer.getDelCustomer())) {
+                result.getExecuteDetails().add(this.unbindCustomer(sopTagCustomer.getFsUserId(), sopTagCustomer.getDelCustomer()));
+            }
+            log.info("节点[{}]-[sop绑定/解绑-标签-客服]-ID:{}执行完成,子项数量:{}", nodeId, sopTagCustomer.getId(), result.getExecuteDetails().size());
+            return result;
+        } catch (Exception e) {
+            log.error("节点[{}]-[sop绑定/解绑-标签-客服]-ID:{}执行逻辑异常", nodeId, sopTagCustomer.getId(), e);
+            return result;
+        } finally {
+            // 清理Redis防重发key
+            try {
+                redisCache.deleteObject(redisKey);
+            } catch (Exception e) {
+                log.error("节点[{}]-[sop绑定/解绑-标签-客服]-ID:{}清理Redis key失败", nodeId, sopTagCustomer.getId(), e);
+            }
+        }
+    }
+
+    /**
+     * 绑定标签
+     *
+     * @param userId 待绑定用户
+     * @param tags   需要绑定的标签
+     */
+    private ExecuteDetail bindTag(Long userId, String tags) {
+        String message;
+        try {
+            String[] tagArray = tags.split(",");
+            List<FsUserTag> fsUserTagList = new ArrayList<>(tagArray.length);
+            for (String tag : tagArray) {
+                FsUserTag fsUserTag = new FsUserTag();
+                fsUserTag.setUserId(userId);
+                fsUserTag.setTagId(Long.valueOf(tag));
+                fsUserTag.setSourceType(4);//sop添加
+                fsUserTagList.add(fsUserTag);
+            }
+            this.fsUserTagsService.batchBindUserTag(fsUserTagList, true);
+            for (FsUserTag fsUserTag : fsUserTagList) {
+                // 获取当前用户绑定的所有客服
+                List<Long> customerIds = appCustomerRoleMapper.getCustomerIdsByUserId(fsUserTag.getUserId());
+                //检查是否需要加入对应的营期内
+                customerIds.forEach(customerId -> this.appSopUserLogsInfoHandle.joinOrCreateSopUserLogsHandle(customerId, fsUserTag.getUserId()));
+            }
+            message = String.format("用户[%s]绑定标签[%s]成功", userId, tags);
+        } catch (Exception e) {
+            log.error("sop绑定标签失败", e);
+            message = String.format("用户[%s]绑定标签[%s]失败: %s", userId, tags, e.getMessage());
+        }
+        return ExecuteDetail.builder()
+                .type(0)
+                .success(true)
+                .message(message)
+                .build();
+    }
+
+    /**
+     * 解绑标签
+     *
+     * @param userId 待解绑用户
+     * @param tags   待解绑标签
+     */
+    private ExecuteDetail unBindTag(Long userId, String tags) {
+        String message;
+        try {
+            String[] tagArray = tags.split(",");
+            List<FsUserTag> fsUserTagList = new ArrayList<>(tagArray.length);
+            for (String tag : tags.split(",")) {
+                FsUserTag fsUserTag = new FsUserTag();
+                fsUserTag.setUserId(userId);
+                fsUserTag.setTagId(Long.valueOf(tag));
+                fsUserTag.setSourceType(4);//sop添加
+                fsUserTagList.add(fsUserTag);
+            }
+            this.fsUserTagsService.batchUnbindUserTag(fsUserTagList);
+            for (FsUserTag fsUserTag : fsUserTagList) {
+                // 获取当前用户绑定的所有客服
+                List<Long> customerIds = appCustomerRoleMapper.getCustomerIdsByUserId(fsUserTag.getUserId());
+                //检查是否需要退出对应的营期内
+                customerIds.forEach(customerId -> this.appSopUserLogsInfoHandle.quitSopUserLogsHandle(customerId, fsUserTag.getUserId()));
+            }
+            message = String.format("用户[%s]解绑标签[%s]成功", userId, tags);
+        } catch (Exception e) {
+            log.error("sop绑定标签失败", e);
+            message = String.format("用户[%s]解绑标签[%s]失败: %s", userId, tags, e.getMessage());
+        }
+        return ExecuteDetail.builder()
+                .type(1)
+                .success(true)
+                .message(message)
+                .build();
+    }
+
+    /**
+     * 绑定客服
+     *
+     * @param userId    待绑定用户
+     * @param customers 待绑定客服
+     */
+    private ExecuteDetail bindCustomer(Long userId, String customers) {
+        String message;
+        try {
+            String[] customerArray = customers.split(",");
+            List<FsUserCustomer> fsUserCustomerList = new ArrayList<>(customerArray.length);
+            for (String customerId : customerArray) {
+                FsUserCustomer fsUserCustomer = new FsUserCustomer();
+                fsUserCustomer.setFsUserId(userId);
+                fsUserCustomer.setCustomerRoleId(Long.valueOf(customerId));
+                fsUserCustomerList.add(fsUserCustomer);
+            }
+            Long batchId = IdUtil.getSnowflake(0, 0).nextId();
+            this.fsUserCustomerMapper.batchBindUserCustomer(fsUserCustomerList, true, batchId);
+            for (FsUserCustomer fsUserCustomer : fsUserCustomerList) {
+                // 获取当前用户绑定的所有客服
+                List<Long> customerIds = appCustomerRoleMapper.getCustomerIdsByUserId(fsUserCustomer.getFsUserId());
+                //检查是否需要加入对应的营期内
+                customerIds.forEach(cId -> this.appSopUserLogsInfoHandle.joinOrCreateSopUserLogsHandle(cId, fsUserCustomer.getFsUserId()));
+                //发送欢迎语
+                this.appWelcomeService.saveWaitSendLogs(fsUserCustomer.getFsUserId(), batchId);
+            }
+            message = String.format("用户[%s]绑定客服[%s]成功", userId, customers);
+        } catch (Exception e) {
+            log.error("sop绑定客服失败", e);
+            message = String.format("用户[%s]绑定客服[%s]失败: %s", userId, customers, e.getMessage());
+        }
+        return ExecuteDetail.builder()
+                .type(2)
+                .success(true)
+                .message(message)
+                .build();
+    }
+
+    /**
+     * 解绑客服
+     *
+     * @param userId    待解绑用户
+     * @param customers 待解绑客服
+     */
+    private ExecuteDetail unbindCustomer(Long userId, String customers) {
+        String message;
+        try {
+            String[] customerArray = customers.split(",");
+            List<FsUserCustomer> fsUserCustomerList = new ArrayList<>(customerArray.length);
+            for (String customerId : customerArray) {
+                FsUserCustomer fsUserCustomer = new FsUserCustomer();
+                fsUserCustomer.setFsUserId(userId);
+                fsUserCustomer.setCustomerRoleId(Long.valueOf(customerId));
+                fsUserCustomerList.add(fsUserCustomer);
+            }
+            this.fsUserCustomerMapper.batchUnBindUserCustomer(fsUserCustomerList);
+            for (FsUserCustomer fsUserCustomer : fsUserCustomerList) {
+                //检查是否需要从的营期内移除
+                this.appSopUserLogsInfoHandle.quitSopUserLogsHandle(fsUserCustomer.getCustomerRoleId(), fsUserCustomer.getFsUserId());
+            }
+            message = String.format("用户[%s]解绑客服[%s]成功", userId, customers);
+        } catch (Exception e) {
+            log.error("sop绑定客服失败", e);
+            message = String.format("用户[%s]解绑客服[%s]失败: %s", userId, customers, e.getMessage());
+        }
+        return ExecuteDetail.builder()
+                .type(3)
+                .success(true)
+                .message(message)
+                .build();
+    }
+
+    @Data
+    public static class ExecuteInfo {
+
+        private Long id;
+
+        private List<ExecuteDetail> executeDetails = new ArrayList<>();
+
+    }
+
+    @Data
+    @Builder
+    private static class ExecuteDetail {
+
+        /**
+         * 0-绑定标签,1-解绑标签,2-绑定客服,3-解绑客服
+         */
+        private Integer type;
+
+        private boolean success;
+
+        private String message;
+
+    }
+
+}

+ 173 - 0
fs-app-task/src/main/java/com/fs/app/task/consumer/tag/UserBindOrUnbindTagConsumer.java

@@ -0,0 +1,173 @@
+package com.fs.app.task.consumer.tag;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.cusrole.mapper.AppCustomerRoleMapper;
+import com.fs.app.properties.AppTaskProperties;
+import com.fs.app.sender.properties.ConfigProperties;
+import com.fs.app.sop.domain.AppBindUnbindTagCustomer;
+import com.fs.app.sop.service.IAppBindUnbindTagCustomerService;
+import com.fs.app.sop.service.impl.AppSopUserLogsInfoHandle;
+import com.fs.app.tag.domain.FsUserTag;
+import com.fs.app.tag.service.IFsUserTagService;
+import com.fs.common.redis.RedisBatchUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * 用户 绑定/解绑 标签消费者
+ */
+@Component
+@Slf4j
+@RequiredArgsConstructor
+@ConditionalOnLoadControl(
+        value = {
+                "bindOrUnbindTag",
+                "consumer"
+        }
+)
+public class UserBindOrUnbindTagConsumer {
+
+    private final RedisBatchUtil redisBatchUtil;
+    private final IFsUserTagService fsUserTagService;
+    private final AppTaskProperties appTaskProperties;
+    private final ConfigProperties configProperties;
+    private final AppCustomerRoleMapper appCustomerRoleMapper;
+    private final AppSopUserLogsInfoHandle appSopUserLogsInfoHandle;
+    private final IAppBindUnbindTagCustomerService appBindUnbindTagCustomerService;
+
+
+    @Value("${server.port}")
+    private String port;
+
+    // 节点唯一标识
+    private String nodeId;
+
+    // 初始化节点 ID
+    @PostConstruct
+    public void init() {
+        log.info("配置参数:{}", configProperties);
+        String node = appTaskProperties.getConsumer().getNode();
+        nodeId = node + "-" + port;//节点编号+端口生成唯一节点编号
+        if (!redisBatchUtil.consumerNodeExists(nodeId)) {
+            redisBatchUtil.setConsumerNode(nodeId);
+            log.info("当前消费者节点:{}加载成功", nodeId);
+        } else {
+            log.warn("当前消费者节点:{}已存在", nodeId);
+        }
+        log.info("用户 绑定/解绑 标签任务已装载!!!!");
+    }
+
+    /**
+     * 用户绑定标签
+     */
+    @Scheduled(fixedDelayString = "${task.consumer.consume-bind-tag-fixed-delay:5000}", initialDelay = 15000)
+    public void userBindTag() {
+        try {
+            StringBuilder nodeFlag = new StringBuilder(nodeId).append("@compensate");
+            Date handleTime = new Date();
+            //锁定范围数据
+            this.appBindUnbindTagCustomerService.lambdaUpdate()
+                    .set(AppBindUnbindTagCustomer::getHandleStatus, 1)
+                    .set(AppBindUnbindTagCustomer::getHandleTime, handleTime)
+                    .set(AppBindUnbindTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppBindUnbindTagCustomer::getHandleStatus, 0)
+                    .eq(AppBindUnbindTagCustomer::getType, 0)
+                    .isNull(AppBindUnbindTagCustomer::getHandleNode)
+                    .last("ORDER BY id LIMIT " + configProperties.getBatchSize())
+                    .update();
+            // 查询需要处理的数据
+            List<AppBindUnbindTagCustomer> pendingList = appBindUnbindTagCustomerService.lambdaQuery()
+                    .eq(AppBindUnbindTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppBindUnbindTagCustomer::getHandleStatus, 1)
+                    .eq(AppBindUnbindTagCustomer::getType, 0)
+                    .list();
+            if (ObjectUtil.isEmpty(pendingList)) {
+                return;
+            }
+            List<FsUserTag> fsUserTagList = new ArrayList<>(pendingList.size());
+            pendingList.forEach(appBindUnbindTagCustomer -> {
+                String[] tags = appBindUnbindTagCustomer.getIds().split(",");
+                for (String tagId : tags) {
+                    FsUserTag fsUserTag = new FsUserTag();
+                    fsUserTag.setUserId(appBindUnbindTagCustomer.getFsUserId());
+                    fsUserTag.setTagId(Long.parseLong(tagId));
+                    fsUserTag.setSourceType(1);
+                    fsUserTagList.add(fsUserTag);
+                }
+                appBindUnbindTagCustomer.setHandleStatus(2);
+            });
+            this.fsUserTagService.batchBindUserTag(fsUserTagList, true);
+            for (FsUserTag fsUserTag : fsUserTagList) {
+                // 获取当前用户绑定的所有客服
+                List<Long> customerIds = appCustomerRoleMapper.getCustomerIdsByUserId(fsUserTag.getUserId());
+                //检查是否需要加入对应的营期内
+                customerIds.forEach(customerId -> this.appSopUserLogsInfoHandle.joinOrCreateSopUserLogsHandle(customerId, fsUserTag.getUserId()));
+            }
+            //设置状态为已处理
+            this.appBindUnbindTagCustomerService.updateBatchById(pendingList);
+        } catch (Exception e) {
+            log.error("节点[{}]执行绑定用户标签失败:{}", this.nodeId, e.getMessage());
+        }
+    }
+
+    /**
+     * 用户解绑标签
+     */
+    @Scheduled(fixedDelayString = "${task.consumer.consume-unbind-tag-fixed-delay:5000}", initialDelay = 0)
+    public void uerUnbindTag() {
+        try {
+            StringBuilder nodeFlag = new StringBuilder(nodeId).append("@compensate");
+            Date handleTime = new Date();
+            //锁定范围数据
+            this.appBindUnbindTagCustomerService.lambdaUpdate()
+                    .set(AppBindUnbindTagCustomer::getHandleStatus, 1)
+                    .set(AppBindUnbindTagCustomer::getHandleTime, handleTime)
+                    .set(AppBindUnbindTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppBindUnbindTagCustomer::getHandleStatus, 0)
+                    .eq(AppBindUnbindTagCustomer::getType, 1)
+                    .isNull(AppBindUnbindTagCustomer::getHandleNode)
+                    .last("ORDER BY id LIMIT " + configProperties.getBatchSize())
+                    .update();
+            // 查询需要处理的数据
+            List<AppBindUnbindTagCustomer> pendingList = appBindUnbindTagCustomerService.lambdaQuery()
+                    .eq(AppBindUnbindTagCustomer::getHandleNode, nodeFlag.toString())
+                    .eq(AppBindUnbindTagCustomer::getHandleStatus, 1)
+                    .eq(AppBindUnbindTagCustomer::getType, 1)
+                    .list();
+            if (ObjectUtil.isEmpty(pendingList)) {
+                return;
+            }
+            List<FsUserTag> fsUserTagList = new ArrayList<>(pendingList.size());
+            pendingList.forEach(appBindUnbindTagCustomer -> {
+                String[] tags = appBindUnbindTagCustomer.getIds().split(",");
+                for (String tagId : tags) {
+                    FsUserTag fsUserTag = new FsUserTag();
+                    fsUserTag.setUserId(appBindUnbindTagCustomer.getFsUserId());
+                    fsUserTag.setTagId(Long.parseLong(tagId));
+                    fsUserTag.setSourceType(1);
+                    fsUserTagList.add(fsUserTag);
+                }
+                appBindUnbindTagCustomer.setHandleStatus(2);
+            });
+            this.fsUserTagService.batchUnbindUserTag(fsUserTagList);
+            for (FsUserTag fsUserTag : fsUserTagList) {
+                // 获取当前用户绑定的所有客服
+                List<Long> customerIds = appCustomerRoleMapper.getCustomerIdsByUserId(fsUserTag.getUserId());
+                //将不满足sop任务条件的从对应的营期移除
+                customerIds.forEach(customerId -> this.appSopUserLogsInfoHandle.quitSopUserLogsHandle(customerId, fsUserTag.getUserId()));
+            }//设置状态为已处理
+            this.appBindUnbindTagCustomerService.updateBatchById(pendingList);
+        } catch (Exception e) {
+            log.error("节点[{}]执行解绑用户标签失败:{}", this.nodeId, e.getMessage());
+        }
+    }
+}

+ 432 - 0
fs-app-task/src/main/java/com/fs/app/task/consumer/welcome/SendWelcomeTask.java

@@ -0,0 +1,432 @@
+package com.fs.app.task.consumer.welcome;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.course.dto.AppCourseRealLinkDTO;
+import com.fs.app.course.mapper.AppFsUserCourseMapper;
+import com.fs.app.course.vo.AppUserCourseVideoVO;
+import com.fs.app.cusrole.domain.AppCustomerRoleMember;
+import com.fs.app.enums.MessageBodyType;
+import com.fs.app.init.CacheHolder;
+import com.fs.app.properties.AppTaskProperties;
+import com.fs.app.sender.AbstractAppMessageSender;
+import com.fs.app.sender.MessageSenderResult;
+import com.fs.app.sender.MessageSenderResultInfo;
+import com.fs.app.sender.MessageUserPayload;
+import com.fs.app.sender.course.MessageCoursePayload;
+import com.fs.app.sender.enums.AppUriEnum;
+import com.fs.app.sender.enums.SenderType;
+import com.fs.app.sender.factory.AppSopLogsSenderFactory;
+import com.fs.app.sender.image.MessageImgPayLoad;
+//import com.fs.app.sender.live.MessageLivePayload;
+import com.fs.app.sender.pkg.MessagePkgPayload;
+import com.fs.app.sender.properties.ConfigProperties;
+import com.fs.app.sender.txt.MessageTxtPayload;
+import com.fs.app.sender.voice.MessageVoicePayload;
+import com.fs.app.sendlogs.domain.AppMessageSendLogs;
+import com.fs.app.sendlogs.service.IAppMessageSendLogsService;
+import com.fs.app.watchlog.domain.AppCourseWatchLog;
+import com.fs.app.watchlog.service.IAppCourseWatchLogService;
+import com.fs.common.BeanCopyUtils;
+import com.fs.common.redis.RedisBatchUtil;
+import com.fs.course.domain.FsCourseLink;
+import com.fs.course.domain.FsCourseSopAppLink;
+import com.fs.course.mapper.FsCourseLinkMapper;
+import com.fs.course.mapper.FsCourseSopAppLinkMapper;
+import com.fs.voice.utils.StringUtil;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.RequiredArgsConstructor;
+import lombok.experimental.Accessors;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.*;
+
+import static com.fs.course.utils.LinkUtil.generateRandomStringWithLock;
+
+/**
+ * 发送欢迎语任务
+ */
+@Component
+@RequiredArgsConstructor
+@Slf4j
+@ConditionalOnLoadControl(
+        value = {
+                "sendWelcome",
+                "consumer"
+        }
+)
+public class SendWelcomeTask {
+
+    private final IAppMessageSendLogsService appMessageSendLogsService;
+
+    private final ConfigProperties configProperties;
+
+    private final IAppCourseWatchLogService appCourseWatchLogService;
+
+    private final AppSopLogsSenderFactory senderFactory;
+
+    private final FsCourseLinkMapper fsCourseLinkMapper;
+
+    private final FsCourseSopAppLinkMapper fsCourseSopAppLinkMapper;
+
+    private final AppFsUserCourseMapper appFsUserCourseMapper;
+
+    private final CacheHolder cacheHolder;
+
+    private final RedisBatchUtil redisBatchUtil;
+
+    private final AppTaskProperties appTaskProperties;
+
+    @Value("${server.port}")
+    private String port;
+
+    // 节点唯一标识
+    private String nodeId;
+
+    // 初始化节点 ID
+    @PostConstruct
+    public void init() {
+        log.info("配置参数:{}", configProperties);
+        String node = appTaskProperties.getConsumer().getNode();
+        nodeId = node + "-" + port;//节点编号+端口生成唯一节点编号
+        if (!redisBatchUtil.consumerNodeExists(nodeId)) {
+            redisBatchUtil.setConsumerNode(nodeId);
+            log.info("当前消费者节点:{}加载成功", nodeId);
+        } else {
+            log.warn("当前消费者节点:{}已存在", nodeId);
+        }
+        log.info("发送欢迎语任务已装载!!!!");
+    }
+
+    /**
+     * 执行发送
+     */
+    @Scheduled(fixedDelayString = "${task.consumer.consume-send-welcome-fixed-delay:10000}", initialDelay = 30000)
+    public void send() {
+        try {
+            StringBuilder nodeFlag = new StringBuilder(nodeId).append("@compensate");
+            appMessageSendLogsService.lambdaUpdate()
+                    .set(AppMessageSendLogs::getHandleStatus, 1)
+                    .set(AppMessageSendLogs::getHandleNode, nodeFlag.toString())
+                    .set(AppMessageSendLogs::getHandleTime, new Date())
+                    .isNull(AppMessageSendLogs::getHandleNode)
+                    .eq(AppMessageSendLogs::getHandleStatus, 0)
+                    .eq(AppMessageSendLogs::getSendStatus, 0)
+                    .last("ORDER BY id LIMIT " + configProperties.getBatchSize())
+                    .update();
+            List<AppMessageSendLogs> list = appMessageSendLogsService.lambdaQuery()
+                    .eq(AppMessageSendLogs::getSendStatus, 0)
+                    .eq(AppMessageSendLogs::getSourceFrom, 0)
+                    .eq(AppMessageSendLogs::getHandleStatus, 1)
+                    .eq(AppMessageSendLogs::getHandleNode, nodeFlag.toString())
+                    .last(" LIMIT " + configProperties.getBatchSize())
+                    .list();
+            if (ObjectUtil.isNotEmpty(list)) {
+                AbstractAppMessageSender textSender = senderFactory.getSender(SenderType.TXT.getType());
+                List<MessageContent> messagePayloads;
+                MessageUserPayload mu;
+                List<MessageSenderResult> mssList = new ArrayList<>(list.size());
+                for (AppMessageSendLogs sendLogs : list) {
+                    if (ObjectUtil.isNotEmpty(sendLogs.getMessageBody())) {
+                        MessageSenderResult result = new MessageSenderResult();
+                        result.setId(sendLogs.getId());
+                        mu = new MessageUserPayload(sendLogs.getSendUserId(), sendLogs.getReceiveUserId());
+                        List<MessageBody> messageBodyList = JSONArray.parseArray(sendLogs.getMessageBody(), MessageBody.class);
+                        for (MessageBody messageBody : messageBodyList) {
+                            //文本
+                            if (MessageBodyType.TEXT.getValue().equals(messageBody.getType())) {
+                                result.getInfo().addAll(textSender.send(Collections.singletonList(new MessageTxtPayload("11", messageBody.getValue())), mu));
+                            }
+                            //非文本类
+                            else if (MessageBodyType.ATTACHMENTS.getValue().equals(messageBody.getType())) {
+                                messagePayloads = JSONArray.parseArray(messageBody.getValue(), MessageContent.class);
+                                result.getInfo().addAll(this.buildSender(messagePayloads, mu));
+                            }
+                        }
+                        mssList.add(result);
+                    }
+                }
+                this.batchUpdateStatus(mssList);
+            }
+        } catch (Exception e) {
+            log.error("执行发送欢迎语异常:", e);
+        }
+    }
+
+    /**
+     * 批次更新日志状态
+     */
+    public void batchUpdateStatus(List<MessageSenderResult> mssList) {
+        if (ObjectUtil.isEmpty(mssList)) {
+            log.warn("批次更新状态:无待更新数据");
+            return;
+        }
+        List<AppMessageSendLogs> updateList = new ArrayList<>(mssList.size());
+        Date sendTime = new Date();
+        for (MessageSenderResult result : mssList) {
+            if (ObjectUtil.isEmpty(result) || ObjectUtil.isEmpty(result.getId())) {
+                log.warn("跳过空结果的状态更新");
+                continue;
+            }
+            // 判断是否有发送失败的子项
+            boolean hasFailed = result.getInfo() != null
+                    && result.getInfo().stream().anyMatch(r -> !r.isSuccess());
+            AppMessageSendLogs sendLogs = new AppMessageSendLogs();
+            sendLogs.setId(result.getId());
+            sendLogs.setSendTime(sendTime);
+            sendLogs.setSendStatus(1);
+            sendLogs.setHandleStatus(2);
+            sendLogs.setIsSuccess(hasFailed ? 0 : 1);//1-成功,0-失败
+            sendLogs.setRemark(hasFailed ? "推送消息失败" : "推送消息成功");
+            sendLogs.setMeta(JSON.toJSONString(result.getInfo()));
+            updateList.add(sendLogs);
+        }
+        try {
+            appMessageSendLogsService.updateBatchById(updateList);
+            log.info("批次更新状态,共{}条记录", updateList.size());
+        } catch (Exception e) {
+            log.error("批次更新状态失败", e);
+            throw e;
+        }
+    }
+
+    /**
+     * 构建发送
+     *
+     * @param messageContents 消息内容
+     * @param mu              消息涉及人员载体
+     */
+    public List<MessageSenderResultInfo> buildSender(List<MessageContent> messageContents, MessageUserPayload mu) {
+        AbstractAppMessageSender courseSender = senderFactory.getSender(SenderType.COURSE.getType());
+        AbstractAppMessageSender voiceSender = senderFactory.getSender(SenderType.VOICE.getType());
+        AbstractAppMessageSender imgSender = senderFactory.getSender(SenderType.IMG.getType());
+        AbstractAppMessageSender pkgSender = senderFactory.getSender(SenderType.PACKAGE.getType());
+        AbstractAppMessageSender liveSender = senderFactory.getSender(SenderType.LIVE.getType());
+        List<MessageSenderResultInfo> resultInfoList = new ArrayList<>();
+        for (MessageContent messageContent : messageContents) {
+            // 课程
+            if (9 == messageContent.getContentType()) {
+                CourseContent course = messageContent.getCourse();
+                AppUserCourseVideoVO videoInfo = appFsUserCourseMapper.findVideoInfoById(course.getVideoId());
+                this.addWatchLog(course, mu.getSender(), mu.getReceiver());
+                LinkInfo linkInfo = this.addCourseLink(videoInfo, mu.getSender(), course.getExpireDays());
+                String courseTitle = course.getCourseTitle();
+                MessageCoursePayload payload = MessageCoursePayload
+                        .builder().build()
+                        .setCourseId(course.getCourseId())
+                        .setVideoId(course.getVideoId())
+                        .setExpireDays(course.getExpireDays())
+                        .setLinkTitle(ObjectUtil.isNotEmpty(courseTitle) ? courseTitle : videoInfo.getTitle())
+                        .setLinkDescribe(ObjectUtil.isNotEmpty(courseTitle) ? courseTitle : videoInfo.getTitle())//课程链接描述(小卡片描述)
+                        .setLinkImageUrl(videoInfo.getThumbnail())
+                        .setAppLinkUrl(linkInfo.getAppLinkUrl())
+                        .setLinkUrl(linkInfo.getLinkUrl());
+                resultInfoList.addAll(courseSender.send(Collections.singletonList(payload), mu));
+            }
+            //语音
+            else if (12 == messageContent.getContentType()) {
+                MessageVoicePayload voicePayload = BeanCopyUtils.copy(messageContent.getVoice(), MessageVoicePayload.class);
+                resultInfoList.addAll(voiceSender.send(Collections.singletonList(voicePayload), mu));
+            }
+            //图片
+            else if (2 == messageContent.getContentType()) {
+                MessageImgPayLoad imgPayLoad = BeanCopyUtils.copy(messageContent.getImage(), MessageImgPayLoad.class);
+                resultInfoList.addAll(imgSender.send(Collections.singletonList(imgPayLoad), mu));
+            }
+            //疗法
+            else if (20 == messageContent.getContentType()) {
+                MessagePkgPayload pkgPayload = BeanCopyUtils.copy(messageContent.getPkg(), MessagePkgPayload.class);
+                resultInfoList.addAll(pkgSender.send(Collections.singletonList(pkgPayload), mu));
+            }
+            //直播
+//            else if (21 == messageContent.getContentType()) {
+//                MessageLivePayload livePayload = BeanCopyUtils.copy(messageContent.getLive(), MessageLivePayload.class);
+//                resultInfoList.addAll(liveSender.send(Collections.singletonList(livePayload), mu));
+//            }
+        }
+        return resultInfoList;
+    }
+
+    /**
+     * 添加看课记录
+     *
+     * @param customerId 客服
+     * @param fsUserId   接收人
+     */
+    private void addWatchLog(CourseContent payload, Long customerId, Long fsUserId) {
+        AppCourseWatchLog watchLog = new AppCourseWatchLog();
+        watchLog.setVideoId(payload.getVideoId());
+        watchLog.setDuration(0L);
+        watchLog.setCourseId(payload.getCourseId());
+        watchLog.setAppCustomerId(customerId);//客服id
+        watchLog.setCreateTime(new Date());
+        watchLog.setUpdateTime(new Date());
+        watchLog.setLogType(3L);//1-看课中 2-完课 3-待看课 4-看课中断
+        watchLog.setUserId(fsUserId);//接收人fs_user_id
+        watchLog.setSourceType(1);//欢迎语
+        appCourseWatchLogService.saveBatch(Collections.singletonList(watchLog), Arrays.asList("update_time", "source_type"));
+    }
+
+    /**
+     * 添加课程链接信息
+     *
+     * @param payload    消息载体
+     * @param customerId 客服
+     */
+    private LinkInfo addCourseLink(AppUserCourseVideoVO payload, Long customerId, Integer expireDays) {
+        FsCourseLink link = new FsCourseLink();
+        link.setVideoId(payload.getVideoId());
+        link.setCourseId(payload.getCourseId());
+        link.setLinkType(4);
+        link.setUNo(UUID.randomUUID().toString());
+        String randomString = generateRandomStringWithLock();
+        if (StringUtil.strIsNullOrEmpty(randomString)) {
+            link.setLink(UUID.randomUUID().toString().replace("-", ""));
+        } else {
+            link.setLink(randomString);
+        }
+        link.setCreateTime(new Date());
+        AppCourseRealLinkDTO courseMap = new AppCourseRealLinkDTO();
+        BeanUtils.copyProperties(link, courseMap);
+        courseMap.setMode(1);//区分是app发课
+        courseMap.setAppCustomerId(customerId);//客服
+        String courseJson = JSON.toJSONString(courseMap);
+        String realLinkFull = AppUriEnum.APP_LINK_PREFIX.getUri() + courseJson;
+        link.setQwUserId(customerId);
+        link.setRealLink(realLinkFull);
+        LocalDateTime now = LocalDateTime.now();
+        LocalDateTime expireDateTime = now.plusDays(expireDays - 1);
+        expireDateTime = expireDateTime.toLocalDate().atTime(23, 59, 59);
+        Date updateTime = Date.from(expireDateTime.atZone(ZoneId.systemDefault()).toInstant());
+        link.setUpdateTime(updateTime);
+        FsCourseSopAppLink sopLink = new FsCourseSopAppLink();
+        sopLink.setLink(link.getLink());
+        sopLink.setCreateTime(new Date());
+        sopLink.setUpdateTime(updateTime);
+        sopLink.setCourseId(payload.getCourseId());
+        sopLink.setCourseTitle(payload.getTitle());
+        sopLink.setCourseUrl(payload.getThumbnail());
+        sopLink.setVideoId(payload.getVideoId());
+        sopLink.setQwUserId(customerId);
+        List<AppCustomerRoleMember> cacheObject = cacheHolder.getRoleMemberList();
+        String customerName = cacheObject.stream()
+                .filter(ro -> Objects.equals(customerId, ro.getId()))
+                .findFirst()
+                .map(AppCustomerRoleMember::getMemberName)
+                .orElse(null);
+        sopLink.setQwUserName(customerName);
+        sopLink.setVideoTitle(ObjectUtil.isNotEmpty(payload.getDescription()) ? payload.getDescription() : payload.getTitle());
+        String appLinkUrl = AppUriEnum.APP_COURSE_PARSER_LINK.getUri() + link.getLink();
+        sopLink.setAppRealLink(appLinkUrl);
+        String linkUrl = AppUriEnum.APP_LINK.getUri() + link.getLink() + "&videoId=" + payload.getVideoId();
+        fsCourseLinkMapper.insertFsCourseLinkBatch(Collections.singletonList(link));
+        fsCourseSopAppLinkMapper.insertFsCourseSopAppLinkBatch(Collections.singletonList(sopLink));
+        return LinkInfo
+                .builder().build()
+                .setAppLinkUrl(appLinkUrl)
+                .setLinkUrl(linkUrl);
+    }
+
+    @Data
+    @Accessors(chain = true)
+    @Builder
+    public static class LinkInfo {
+
+        private String appLinkUrl;
+
+        private String linkUrl;
+
+    }
+
+    @Data
+    public static class MessageContent {
+
+        private Integer contentType;
+
+        private CourseContent course;
+
+        private VoiceContent voice;
+
+        private Img image;
+
+        private Pkg pkg;
+
+        private Live live;
+
+    }
+
+    @Data
+    public static class CourseContent {
+
+        private Long videoId;
+
+        private Long courseId;
+
+        private String courseTitle;
+
+        private Integer expireDays;
+
+    }
+
+    @Data
+    public static class VoiceContent {
+
+        private String voiceUrl;
+
+        private String value;
+
+        private String voiceDuration;
+
+    }
+
+    @Data
+    public static class Img {
+
+        private String imgUrl;
+
+    }
+
+    @Data
+    public static class Pkg {
+
+        private Long packageId;
+
+        private String packageName;
+
+        private String packageImgUrl;
+
+    }
+
+    @Data
+    public static class Live {
+
+        private Long liveId;
+
+        private String liveTitle;
+
+        private String liveImgUrl;
+
+    }
+
+    @Data
+    @Accessors(chain = true)
+    @NoArgsConstructor
+    public static class MessageBody {
+
+        private String type;
+
+        private String value;
+
+    }
+}

+ 228 - 0
fs-app-task/src/main/java/com/fs/app/task/other/ai/AiChatTimingRemind.java

@@ -0,0 +1,228 @@
+package com.fs.app.task.other.ai;
+
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.json.JSONUtil;
+import com.fs.app.chat.handler.ChatHandler;
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.cusrole.domain.AppCustomerRoleMember;
+import com.fs.app.cusrole.domain.AppFastGptRole;
+import com.fs.app.cusrole.service.IAppCustomerRoleMemberService;
+import com.fs.app.cusrole.service.IAppFastGptRoleService;
+import com.fs.app.sender.enums.SenderType;
+import com.fs.app.sender.properties.ConfigProperties;
+import com.fs.app.user.service.IAppUserService;
+import com.fs.app.user.vo.AppUserVO;
+import com.fs.common.core.domain.R;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.redis.RedisBatchUtil;
+import com.fs.fastGpt.config.ModeConfig;
+import com.fs.fastgptApi.param.ChatParam;
+import com.fs.fastgptApi.result.ChatDetailTStreamFResult;
+import com.fs.fastgptApi.service.ChatService;
+import com.fs.im.domain.ImChatSession;
+import com.fs.im.service.IImChatSessionService;
+import com.fs.im.vo.OpenImMsgCallBackVO;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * ai对话定时触达
+ */
+@Component
+@RequiredArgsConstructor
+@Slf4j
+@ConditionalOnLoadControl(
+        value = {
+                "aiChatTimingRemind",
+                "consumer"
+        }
+)
+public class AiChatTimingRemind {
+
+    private final IImChatSessionService imChatSessionService;
+
+    private final IAppCustomerRoleMemberService roleMemberService;
+
+    private final IAppFastGptRoleService appRoleService;
+
+    private final IAppUserService appUserService;
+
+    private final RedisCache redisCache;
+
+    private final ChatHandler chatHandler;
+
+    private final ChatService chatService;
+
+    private final RedisBatchUtil redisBatchUtil;
+
+    private final ConfigProperties configProperties;
+
+    private static final String APP_AI_CHAT_TIMING_REMIND_LOCK_KEY = "app:ai:chat:timing:remind:lock";
+
+    @PostConstruct
+    public void init() {
+        log.info("ai对话用户定时触达任务已装载!!!!");
+    }
+
+    /**
+     * 执行AI对话定时触达任务
+     */
+    @Scheduled(fixedDelayString = "${task.consumer.consume-auto-remind-ai-fixed-delay:30000}", initialDelay = 17000)
+    public void execute() {
+        // 尝试获取分布式锁,防止多节点重复执行
+        boolean tryLock = redisBatchUtil.tryLock(
+                APP_AI_CHAT_TIMING_REMIND_LOCK_KEY,
+                10,
+                TimeUnit.SECONDS
+        );
+        if (!tryLock) {
+            log.info("当前节点未获取到处理用户定时触达锁,本次执行跳过!");
+            return;
+        }
+        try {
+            // 查询需要主动触达的聊天会话(限制批量处理数量)
+            List<ImChatSession> chatSessions = this.imChatSessionService.lambdaQuery()
+                    .eq(ImChatSession::getRemindStatus, 1)
+                    .le(ImChatSession::getRemindTime, new Date(System.currentTimeMillis() + 60 * 1000))
+                    .last(" LIMIT " + this.configProperties.getBatchSize())
+                    .list();
+            log.info("需要处理的主动触达记录条数:{}", chatSessions.size());
+            Date current = new Date();
+            List<ImChatSession> updateList = new ArrayList<>(chatSessions.size());
+            for (ImChatSession chatSession : chatSessions) {
+                //过了需要提醒的时间
+                if (chatSession.getRemindTime().before(current)) {
+                    Integer replyCount;
+                    if ((replyCount = redisCache.getCacheObject("IM:appReply:" + chatSession.getChatId())) == null) {
+                        //用户首次发送消息
+                        redisCache.setCacheObject("IM:appReply:" + chatSession.getChatId(), 1, 5, TimeUnit.MINUTES);
+                    } else {
+                        redisCache.setCacheObject("IM:appReply:" + chatSession.getChatId(), replyCount + 1, 5, TimeUnit.MINUTES);
+                    }
+                    if (this.remindToAI(chatSession)) {
+                        updateList.add(chatSession);
+                    }
+                }
+            }
+            if (ObjectUtil.isNotEmpty(updateList)) {
+                this.batchUpdateStatus(updateList);
+            }
+        } catch (Exception e) {
+            log.error("当前节点执行用户定时触达失败:", e);
+        } finally {
+            // 释放分布式锁
+            redisBatchUtil.unlock(APP_AI_CHAT_TIMING_REMIND_LOCK_KEY);
+        }
+    }
+
+
+    /**
+     * 向AI发起提醒信息
+     *
+     * @param chatSession
+     * @return
+     */
+    private boolean remindToAI(ImChatSession chatSession) {
+        AppCustomerRoleMember roleMember = roleMemberService.selectAppCustomerRoleMemberById(Long.valueOf(chatSession.getKfId()));
+        if (ObjectUtil.isEmpty(roleMember)) {
+            return false;
+        }
+        ChatParam param = new ChatParam();
+        param.setChatId(chatSession.getChatId());
+        param.setStream(false);
+        param.setDetail(true);
+        ChatParam.Variables variables = new ChatParam.Variables();
+        variables.setUid(this.configProperties.getSenderPrefix() + chatSession.getKfId());//客服接收
+        variables.setName(roleMember.getMemberName());
+        List<ChatParam.Message> messageList = new ArrayList<>();
+        param.setMessages(messageList);
+        AppFastGptRole role = appRoleService.selectAppFastGptRoleById(this.configProperties.isDebug() ? 142L : roleMember.getAppFastgptRoleId());//调试模式固定141
+        ModeConfig config = JSONUtil.toBean(role.getModeConfigJson(), ModeConfig.class);
+        if (StringUtils.isEmpty(config.getAPPKey())) {
+            return false;
+        }
+        Long batchId = IdUtil.getSnowflake(0, 0).nextId();
+        AppUserVO appUser = appUserService.getById(Long.parseLong(chatSession.getUserId()));
+        log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<设置看课记录>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
+        //更新当前会话的看课记录
+        ImChatSession imChatSession = this.chatHandler.setChatSessionCourseWatchLog(appUser, chatSession.getChatId(), roleMember);
+        log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<开始组装历史聊天记录,以及看课状态>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
+        // 带上历史聊天记录,便于ai更精准的根据关键词分析
+        this.chatHandler.addAppHistoryIMKeyWord(
+                role.getContactInfo(), appUser, messageList,
+                role.getReminderWords(), "AI主动触达", Optional.ofNullable(imChatSession).orElse(chatSession),
+                chatSession.getChatId(), roleMember,
+                System.currentTimeMillis(), batchId
+        );
+        R r = chatService.initiatingTakeChat(param, "http://154.8.194.176:3000/api", config.getAPPKey());
+        Integer reply = redisCache.getCacheObject("IM:appReply:" + chatSession.getChatId());
+        if (reply != 1) {
+            this.chatHandler.notifyWorkTask(roleMember, roleMember.getMemberName(), " AI报错", appUser.getUserId());
+        }
+        if (r.get("code").equals(200)) {
+            ChatDetailTStreamFResult result = (ChatDetailTStreamFResult) r.get("data");
+            if (!result.getChoices().isEmpty()) {
+                OpenImMsgCallBackVO openImMsgDTO = buildImMsg(chatSession);
+                // 回复消息 和 内容处理
+                this.chatHandler.dealWithAIReplyResult(
+                        result, roleMember, openImMsgDTO,
+                        appUser.getUserId(), chatSession, batchId
+                );
+            }
+        }
+        return true;
+    }
+
+    private OpenImMsgCallBackVO buildImMsg(ImChatSession chatSession) {
+        OpenImMsgCallBackVO openImMsgDTO = new OpenImMsgCallBackVO();
+        //发送人是用户
+        openImMsgDTO.setSendID(configProperties.getReceiverPrefix() + chatSession.getUserId());
+        //接收人是ai客服
+        openImMsgDTO.setRecvID(configProperties.getSenderPrefix() + chatSession.getKfId());
+        openImMsgDTO.setContentType(SenderType.TXT.getImContentTypeCode());
+        openImMsgDTO.setSenderPlatformID(5);//web
+        openImMsgDTO.setSessionType(1);//单聊
+        openImMsgDTO.setSendTime(System.currentTimeMillis());//发送时间
+        return openImMsgDTO;
+    }
+
+    /**
+     * 批次更新日志状态
+     */
+    public void batchUpdateStatus(List<ImChatSession> sessions) {
+        if (ObjectUtil.isEmpty(sessions)) {
+            log.warn("批次更新状态:无待更新数据");
+            return;
+        }
+        List<ImChatSession> updateList = new ArrayList<>(sessions.size());
+        for (ImChatSession session : sessions) {
+            if (ObjectUtil.isEmpty(session)) {
+                log.warn("跳过空结果的状态更新");
+                continue;
+            }
+            session.setRemindStatus(0);//关闭定时触达状态
+            session.setRemindCount(Optional.ofNullable(session.getRemindCount()).orElse(0) + 1);
+            session.setUpdateTime(new Date());
+            updateList.add(session);
+        }
+        try {
+            this.imChatSessionService.updateBatchById(updateList);
+            log.info("批次更新状态,共{}条记录", updateList.size());
+        } catch (Exception e) {
+            log.error("批次更新状态失败", e);
+            throw e;
+        }
+    }
+
+}

+ 92 - 0
fs-app-task/src/main/java/com/fs/app/task/other/calibration/BatchIdCalibration.java

@@ -0,0 +1,92 @@
+package com.fs.app.task.other.calibration;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.fs.app.sender.properties.ConfigProperties;
+import com.fs.app.sop.domain.AppSopLogs;
+import com.fs.app.sop.service.IAppSopLogsService;
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.common.redis.RedisBatchUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 批次号校准定时任务:扫描未处理数据最小ID,校准Redis批次号
+ */
+@Component
+@Slf4j
+@RequiredArgsConstructor
+@ConditionalOnLoadControl(
+        value = {
+                "batchIdCalibration",
+                "consumer"
+        }
+)
+public class BatchIdCalibration {
+
+    private final IAppSopLogsService appSopLogsService;
+    private final RedisBatchUtil redisBatchUtil;
+    private final ConfigProperties configProperties;
+
+    // 校准任务分布式锁key
+    private static final String BATCH_CALIBRATION_LOCK = "app:sop:consumer:batch:calibration:lock";
+
+    @PostConstruct
+    public void init() {
+        log.info("定时校准消费sop待发记录批次号任务已装载!!!!");
+    }
+
+    /**
+     * 定时校准批次号:每5分钟执行一次,避开核心消费逻辑的高峰
+     */
+    @Scheduled(cron = "${task.consumer.caiibration-task-cron:0 */5 * * * *}")
+    public void calibrateBatchId() {
+        // 抢占分布式锁,避免多节点重复执行校准
+        boolean lockSuccess = redisBatchUtil.tryLock(
+                BATCH_CALIBRATION_LOCK,
+                5,
+                TimeUnit.SECONDS
+        );
+        if (!lockSuccess) {
+            log.info("批次号校准任务:其他节点正在执行,本次跳过");
+            return;
+        }
+        try {
+            AppSopLogs maxIdLog = appSopLogsService.lambdaQuery()
+                    .eq(AppSopLogs::getAppSendStatus, 0) // 待发送
+                    .eq(AppSopLogs::getHandleStatus, 0) // 待处理
+                    .orderByDesc(AppSopLogs::getId) // 按ID降序
+                    .last("LIMIT 1") // 取第一条(最大ID)
+                    .one();
+            // 无数据,直接返回
+            if (ObjectUtil.isEmpty(maxIdLog)) {
+                log.info("批次号校准任务:表中无满足条件数据,无需校准");
+                return;
+            }
+            Long maxId = maxIdLog.getId();
+            log.info("批次号校准任务:扫描到表中满足条件的数据最大ID={}", maxId);
+            // 反算最大ID对应的批次号
+            int batchSize = configProperties.getBatchSize();
+            long calibratedBatchId = (maxId - 1) / batchSize + 1;
+            log.info("批次号校准任务:扫描到满足条件数据的最大ID={} 对应校准批次号={}(批次大小={})", maxId, calibratedBatchId, batchSize);
+
+            // 原子校准Redis全局批次号
+            boolean calibrateSuccess = redisBatchUtil.calibrateGlobalBatchIdToMax(calibratedBatchId, 50);
+            if (calibrateSuccess) {
+                log.info("批次号校准任务:成功将全局批次号更新为{}(满足条件数据的最大ID对应批次号)", calibratedBatchId);
+            } else {
+                long currentGen = redisBatchUtil.getGeneratedMaxBatch();
+                log.info("批次号校准任务:当前全局批次号={} ≥ 满足条件数据的最大ID对应批次号={},无需校准", currentGen, calibratedBatchId);
+            }
+        } catch (Exception e) {
+            log.error("批次号校准任务执行异常", e);
+        } finally {
+            // 释放锁
+            redisBatchUtil.unlock(BATCH_CALIBRATION_LOCK);
+        }
+    }
+}

+ 111 - 0
fs-app-task/src/main/java/com/fs/app/task/other/clear/ClearTask.java

@@ -0,0 +1,111 @@
+package com.fs.app.task.other.clear;
+
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.sendlogs.domain.AppMessageSendLogs;
+import com.fs.app.sendlogs.service.IAppMessageSendLogsService;
+import com.fs.app.sop.domain.AppSopLogs;
+import com.fs.app.sop.service.IAppSopLogsService;
+import com.fs.common.redis.RedisBatchUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 清理任务
+ */
+@Component
+@RequiredArgsConstructor
+@Slf4j
+@ConditionalOnLoadControl(
+        value = {
+                "clear",
+                "consumer"
+        }
+)
+public class ClearTask {
+
+    private final RedisBatchUtil redisBatchUtil;
+    private final IAppSopLogsService appSopLogsService;
+    private final IAppMessageSendLogsService appMessageSendLogsService;
+
+    private static final String CLEAR_APP_SOP_LOGS_LOCK_KEY = "app:sop:clear:lock";
+    private static final String CLEAR_WELCOME_LOCK_KEY = "app:welcome:clear:lock";
+
+    @PostConstruct
+    public void init() {
+        log.info("清理任务已装载!!!!");
+    }
+
+    /**
+     * 清理app_sop_logs
+     * 清理逻辑:截至当日,清理发送成功两天前的数据
+     * 清理频率:每天一次
+     */
+    @Scheduled(cron = "${task.consumer.consume-clear-app-sop-logs-cron:0 0 1 * * *}")
+    public void clearAppSopLogs() {
+        boolean tryLock = redisBatchUtil.tryLock(
+                CLEAR_APP_SOP_LOGS_LOCK_KEY,
+                10,
+                TimeUnit.SECONDS
+        );
+        if (!tryLock) {
+            log.info("当前节点未获取到清理sop执行详情锁,本次执行跳过!");
+            return;
+        }
+        try {
+            LocalDate localDate = LocalDate.now();
+            LocalDateTime before2Datetime = localDate.minusDays(2).atTime(23, 59, 59);
+            Date dt = Date.from(before2Datetime.atZone(ZoneId.systemDefault()).toInstant());
+            this.appSopLogsService.lambdaUpdate()
+                    .in(AppSopLogs::getAppSendStatus, new ArrayList<>(Arrays.asList(1, 2, 3)))//发送成功,失败,不发送
+                    .le(AppSopLogs::getRealSendTime, dt)//两天前的数据
+                    .remove();
+        } catch (Exception e) {
+            log.error("当前节点执行清理sop执行详情失败:", e);
+        } finally {
+            redisBatchUtil.unlock(CLEAR_APP_SOP_LOGS_LOCK_KEY);
+        }
+    }
+
+    /**
+     * 清理app_message_send_logs
+     * 清理逻辑:截至当日,清理发送成功两天前的数据
+     * 清理频率:每天一次
+     */
+    @Scheduled(cron = "${task.consumer.consume-clear-app-welcome-send-logs-cron:0 5 1 * * *}")
+    public void clearWelcomeSendLogs() {
+        boolean tryLock = redisBatchUtil.tryLock(
+                CLEAR_WELCOME_LOCK_KEY,
+                10,
+                TimeUnit.SECONDS
+        );
+        if (!tryLock) {
+            log.info("当前节点未获取到清理欢迎语发送记录锁,本次执行跳过!");
+            return;
+        }
+        try {
+            LocalDate localDate = LocalDate.now();
+            LocalDateTime before2Datetime = localDate.minusDays(2).atTime(23, 59, 59);
+            Date dt = Date.from(before2Datetime.atZone(ZoneId.systemDefault()).toInstant());
+            this.appMessageSendLogsService.lambdaUpdate()
+                    .eq(AppMessageSendLogs::getIsSuccess, 1)//发送成功
+                    .le(AppMessageSendLogs::getSendTime, dt)//
+                    .remove();
+        } catch (Exception e) {
+            log.error("当前节点执行清理欢迎语发送记录失败:", e);
+        } finally {
+            redisBatchUtil.unlock(CLEAR_WELCOME_LOCK_KEY);
+        }
+    }
+
+}

+ 242 - 0
fs-app-task/src/main/java/com/fs/app/task/producer/sop/AppSopLogsProducer.java

@@ -0,0 +1,242 @@
+package com.fs.app.task.producer.sop;
+
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSONObject;
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.properties.AppTaskProperties;
+import com.fs.app.sender.properties.ConfigProperties;
+import com.fs.app.sop.domain.AppGeneratedUserLogsInfo;
+import com.fs.app.sop.domain.AppSopUserLogsInfos;
+import com.fs.app.sop.domain.AppUserLogsInfoFragmentation;
+import com.fs.app.sop.result.AppSopTempSettingResult;
+import com.fs.app.sop.service.IAppGeneratedUserLogsInfoService;
+import com.fs.app.sop.service.IAppSopTaskService;
+import com.fs.app.sop.service.IAppSopUserLogInfoService;
+import com.fs.app.sop.service.IAppUserLogsInfoFragmentationService;
+import com.fs.app.sop.vo.AppSopTaskUserLogVO;
+import com.fs.common.redis.RedisBatchUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+@Component
+@Slf4j
+@RequiredArgsConstructor
+@ConditionalOnLoadControl(
+        value = {
+                "generateSopLogs",
+                "producer"
+        }
+)
+public class AppSopLogsProducer {
+
+    private final IAppSopTaskService appSopTaskService;
+
+    private final RedisBatchUtil redisBatchUtil;
+
+    private final ConfigProperties configProperties;
+
+    private final AppTaskProperties appTaskProperties;
+
+    private final IAppSopUserLogInfoService appSopUserLogInfoService;
+
+    private static final String PRODUCER_NODE_GENERATE_USER_LOGS_LOCK = "app:sop:producer:generate:userLogs:lock";
+
+    private static final String PRODUCER_NODE_GENERATE_SOP_LOGS_LOCK = "app:sop:producer:generate:sopLogs:lock";
+
+    private static final String PRODUCER_NODE_HANDLE_REPEAT_DATA_LOCK = "app:sop:producer:handle:repeat:data:lock";
+
+    private final IAppGeneratedUserLogsInfoService appGeneratedUserLogsInfoService;
+
+    private final IAppUserLogsInfoFragmentationService appUserLogsInfoFragmentationService;
+
+    @Value("${server.port}")
+    private String port;
+
+    // 节点唯一标识
+    private String nodeId;
+
+    @PostConstruct
+    public void init() {
+        log.info("配置参数:{}", configProperties);
+        String node = appTaskProperties.getProducer().getNode();
+        nodeId = node + "-" + port;//节点编号+端口生成唯一节点编号
+        if (!redisBatchUtil.producerNodeExists(nodeId)) {
+            redisBatchUtil.setProducerNode(nodeId);
+            log.info("当前生产者节点:{}加载成功", nodeId);
+        } else {
+            log.warn("当前生产者节点:{}已存在", nodeId);
+        }
+        log.info("生成用户营期和待发记录任务已装载!!!!");
+    }
+
+    /**
+     * 生成营期记录,app_sop_user_log和营期详情app_sop_user_log_info
+     * fixedDelay:60000
+     */
+    @Scheduled(fixedDelayString = "${task.producer.gen-user-logs-delay:60000}", initialDelay = 5000)
+    public void generateUserLogs() {
+        // 抢占分布式锁,避免多节点同时生成
+        boolean lockSuccess = redisBatchUtil.tryLock(
+                PRODUCER_NODE_GENERATE_USER_LOGS_LOCK,
+                5,
+                TimeUnit.SECONDS
+        );
+        if (!lockSuccess) {
+            log.info("生成用户营期任务:其他节点正在执行,本次跳过!!!!");
+            return;
+        }
+        log.info("生成用户营期任务:当前节点获取到锁,开始生成!!!!");
+        try {
+            appSopTaskService.generateUserLogs();
+        } catch (Exception e) {
+            log.error("生成用户营期失败:{}", e.getMessage());
+        } finally {
+            redisBatchUtil.unlock(PRODUCER_NODE_GENERATE_USER_LOGS_LOCK);
+        }
+    }
+
+    /**
+     * 营期分片任务
+     * 多节点仅需有一个抢占执行任务即可
+     */
+    @Scheduled(cron = "${task.producer.gen-user-logs-info-fragmentation-cron:0 5 * * * *}")
+    public void userLogsFragmentation() {
+        // 抢占分布式锁,避免多节点同时生成
+        boolean lockSuccess = redisBatchUtil.tryLock(
+                PRODUCER_NODE_GENERATE_SOP_LOGS_LOCK,
+                5,
+                TimeUnit.SECONDS
+        );
+        if (!lockSuccess) {
+            log.info("根据营期生成待发记录任务:其他节点正在执行,本次跳过!!!!");
+            return;
+        }
+        log.info("根据营期生成待发记录任务:当前节点获取到锁,开始生成!!!!");
+        try {
+            Long batchId = IdUtil.getSnowflake(0, 0).nextId();
+            appSopTaskService.userLogsFragmentation(batchId, null);
+        } catch (InterruptedException e) {
+            log.error("生成待发记录失败:{}", e.getMessage());
+        } finally {
+            redisBatchUtil.unlock(PRODUCER_NODE_GENERATE_SOP_LOGS_LOCK);
+        }
+    }
+
+    @Scheduled(fixedDelayString = "${task.producer.handle-repeat-data-delay:30000}", initialDelay = 5 * 60 * 1000)
+    public void handleRepeatData() {
+        // 抢占分布式锁,避免多节点同时生成
+        boolean lockSuccess = redisBatchUtil.tryLock(
+                PRODUCER_NODE_HANDLE_REPEAT_DATA_LOCK,
+                5,
+                TimeUnit.SECONDS
+        );
+        if (!lockSuccess) {
+            log.info("处理重复发送记录:其他节点正在执行,本次跳过!!!!");
+            return;
+        }
+        log.info("处理重复发送记录:当前节点获取到锁,开始处理!!!!");
+        try {
+            int i = appSopUserLogInfoService.handleRepeatData();
+            log.info("本次共处理:{}条重复数据", i);
+        } catch (Exception e) {
+            log.error("处理重复发送记录失败:{}", e.getMessage());
+        } finally {
+            redisBatchUtil.unlock(PRODUCER_NODE_HANDLE_REPEAT_DATA_LOCK);
+        }
+    }
+
+    /**
+     * 根据分片生成实际的待执行记录
+     */
+    @Scheduled(fixedDelayString = "${task.producer.gen-sop-logs-delay:5000}")
+    public void generateSopLogs() {
+        try {
+            StringBuilder nodeFlag = new StringBuilder(nodeId).append("@compensate");
+            //锁定范围数据
+            this.appGeneratedUserLogsInfoService.lambdaUpdate()
+                    .set(AppGeneratedUserLogsInfo::getHandleNode, nodeFlag.toString())
+                    .set(AppGeneratedUserLogsInfo::getHandleTime, new Date())
+                    .set(AppGeneratedUserLogsInfo::getHandleStatus, 1)
+                    .eq(AppGeneratedUserLogsInfo::getHandleStatus, 0)
+                    .isNull(AppGeneratedUserLogsInfo::getHandleNode)
+                    .orderByAsc(AppGeneratedUserLogsInfo::getId)
+                    .last(" LIMIT " + configProperties.getBatchSize())
+                    .update();
+            //具体营期数据
+            List<AppGeneratedUserLogsInfo> logsInfos = this.appGeneratedUserLogsInfoService.lambdaQuery()
+                    .eq(AppGeneratedUserLogsInfo::getHandleStatus, 1)
+                    .eq(AppGeneratedUserLogsInfo::getHandleNode, nodeFlag.toString())
+                    .orderByAsc(AppGeneratedUserLogsInfo::getId)
+                    .list();
+            if (ObjectUtil.isEmpty(logsInfos)) {
+                return;
+            }
+            List<Long> fragmentationIds = logsInfos.stream()
+                    .map(AppGeneratedUserLogsInfo::getFragmentationId)
+                    .filter(Objects::nonNull)
+                    .distinct()
+                    .collect(Collectors.toList());
+            List<AppUserLogsInfoFragmentation> fragments = this.appUserLogsInfoFragmentationService.lambdaQuery()
+                    .in(AppUserLogsInfoFragmentation::getId, fragmentationIds)
+                    .list();
+            if (ObjectUtil.isEmpty(fragments)) {
+                return;
+            }
+            //分片组
+            Map<Long, AppUserLogsInfoFragmentation> fragmentationMap = fragments.stream()
+                    .collect(Collectors.toMap(
+                            AppUserLogsInfoFragmentation::getId,
+                            tmp -> {
+                                AppUserLogsInfoFragmentation result = new AppUserLogsInfoFragmentation();
+                                result.setId(tmp.getId());
+                                result.setSendTime(tmp.getSendTime());
+                                result.setRuleInfo(JSONObject.parseObject(tmp.getContent(), AppSopTempSettingResult.Content.class));
+                                result.setLogVoInfo(JSONObject.parseObject(tmp.getLogvo(), AppSopTaskUserLogVO.class));
+                                result.setBatchId(tmp.getBatchId());
+                                return result;
+                            },
+                            (o, n) -> o // 重复key保留旧值
+                    ));
+            //根据分片分组
+            Map<Long, List<Long>> collect = logsInfos.stream()
+                    .collect(Collectors.groupingBy(
+                            AppGeneratedUserLogsInfo::getFragmentationId,
+                            Collectors.mapping(AppGeneratedUserLogsInfo::getSopUserLogsInfoId, Collectors.toList())
+                    ));
+            collect.forEach((fragId, logs) -> {
+                AppUserLogsInfoFragmentation infoFragmentation = fragmentationMap.get(fragId);
+                List<AppSopUserLogsInfos> target = appSopUserLogInfoService.lambdaQuery()
+                        .in(AppSopUserLogsInfos::getId, logs)
+                        .list();
+                appSopTaskService.generateSopLogs(target, infoFragmentation.getLogVoInfo(), infoFragmentation.getSendTime(),
+                        infoFragmentation.getRuleInfo(), infoFragmentation.getBatchId()
+                );
+            });
+            List<Long> generatedIds = logsInfos.stream()
+                    .map(AppGeneratedUserLogsInfo::getId)
+                    .filter(Objects::nonNull)
+                    .distinct()
+                    .collect(Collectors.toList());
+            //更新处理状态
+            this.appGeneratedUserLogsInfoService.lambdaUpdate()
+                    .set(AppGeneratedUserLogsInfo::getHandleStatus, 2)
+                    .in(AppGeneratedUserLogsInfo::getId, generatedIds)
+                    .update();
+        } catch (Exception e) {
+            log.error("节点[{}]根据营期用户分片记录生成待发记录失败:", nodeId, e);
+        }
+    }
+
+}

+ 64 - 0
fs-app-task/src/main/java/com/fs/app/task/producer/urgentclass/SyncWatchLog2UrgentClass.java

@@ -0,0 +1,64 @@
+package com.fs.app.task.producer.urgentclass;
+
+import com.fs.app.core.ConditionalOnLoadControl;
+import com.fs.app.task.service.IAppWorkTaskService;
+import com.fs.common.redis.RedisBatchUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 同步看课记录到催课看板
+ */
+@Component
+@Slf4j
+@RequiredArgsConstructor
+@ConditionalOnLoadControl(
+        value = {
+                "generateUrgentClass",
+                "producer"
+        }
+)
+public class SyncWatchLog2UrgentClass {
+
+    private final IAppWorkTaskService appWorkTaskService;
+
+    private final RedisBatchUtil redisBatchUtil;
+
+    /**
+     * 同步看课记录到催课看板
+     */
+    private final static String PRODUCER_NODE_SYNC_WATCH_LOG_URGENT_CLASS_LOCK = "app:sync:watch-log-to-urgent-class:lock";
+
+    @PostConstruct
+    public void init() {
+        log.info("同步看课记录到催课看板任务已装载!!!!!");
+    }
+
+
+    @Scheduled(cron = "${task.producer.sync-watchlog-to-urgent-class-cron:0 0 0/1 * * *}")
+    public void syncWatchLog2UrgentClass() {
+        // 预防多节点同时处理
+        boolean lock = redisBatchUtil.tryLock(
+                PRODUCER_NODE_SYNC_WATCH_LOG_URGENT_CLASS_LOCK,
+                5,
+                TimeUnit.SECONDS
+        );
+        if (!lock) {
+            log.info("同步看课记录到催课看板任务:当前任务已被其他节点处理,本次处理跳过!");
+            return;
+        }
+        try {
+            appWorkTaskService.syncWatchLog2UrgentClass();
+        } catch (Exception e) {
+            log.error("同步看课记录到催课看板任务失败:{}", e.getMessage());
+        } finally {
+            redisBatchUtil.unlock(PRODUCER_NODE_SYNC_WATCH_LOG_URGENT_CLASS_LOCK);
+        }
+    }
+
+}

+ 190 - 0
fs-app-task/src/main/java/com/fs/common/redis/RedisBatchUtil.java

@@ -0,0 +1,190 @@
+package com.fs.common.redis;
+
+import cn.hutool.core.util.ObjectUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.ValueOperations;
+import org.springframework.data.redis.core.script.DefaultRedisScript;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class RedisBatchUtil {
+
+    // Redis key前缀:消费者节点名称前缀
+    private static final String CONSUMER_NODE_KEY_PREFIX = "app:sop:consumer:node:";
+
+    private static final String PRODUCER_NODE_KEY_PREFIX = "app:sop:producer:node:";
+
+    // Redis key:全局已生成的最大批次号
+    private static final String GLOBAL_GENERATED_BATCH_KEY = "app:sop:consumer:batch:generated";
+
+    // Redis key:已完成的最大批次号
+    private static final String COMPLETED_MAX_BATCH_KEY = "app:sop:consumer:completed:max-batch";
+
+    // Redis key:待检查ID起始值
+    private static final String CURRENT_CHECK_START_ID_KEY = "app:sop:consumer:check:start-id";
+
+    private final RedisTemplate<String, String> redisTemplate;
+
+    private final RedissonClient redissonClient;
+
+    // 初始化Redis Key
+    @PostConstruct
+    public void initRedisKeys() {
+        ValueOperations<String, String> ops = redisTemplate.opsForValue();
+        // 初始化已生成批次号(若不存在则设为0)
+        if (ObjectUtil.isEmpty(ops.get(GLOBAL_GENERATED_BATCH_KEY))) {
+            ops.set(GLOBAL_GENERATED_BATCH_KEY, "0");
+        }
+        // 初始化已完成批次号(若不存在则设为0)
+        if (ObjectUtil.isEmpty(ops.get(COMPLETED_MAX_BATCH_KEY))) {
+            ops.set(COMPLETED_MAX_BATCH_KEY, "0");
+        }
+        // 初始化待检查ID起始值为1
+        if (ObjectUtil.isEmpty(ops.get(CURRENT_CHECK_START_ID_KEY))) {
+            ops.set(CURRENT_CHECK_START_ID_KEY, "1");
+        }
+    }
+
+    /**
+     * 设置消费节点
+     */
+    public void setConsumerNode(String node) {
+        try {
+            redisTemplate.opsForValue().set(CONSUMER_NODE_KEY_PREFIX + node, String.valueOf(System.currentTimeMillis()));
+        } catch (Exception e) {
+            log.error("设置消费节点失败", e);
+        }
+    }
+
+    /**
+     * 设置生产者节点
+     */
+    public void setProducerNode(String node) {
+        try {
+            redisTemplate.opsForValue().set(PRODUCER_NODE_KEY_PREFIX + node, String.valueOf(System.currentTimeMillis()));
+        } catch (Exception e) {
+            log.error("设置生产节点失败", e);
+        }
+    }
+
+    /**
+     * 消费节点是否已存在
+     */
+    public boolean consumerNodeExists(String node) {
+        try {
+            return redisTemplate.hasKey(CONSUMER_NODE_KEY_PREFIX + node);
+        } catch (Exception e) {
+            log.error("检查消费节点是否存在失败", e);
+            return false;
+        }
+    }
+
+    /**
+     * 生产节点是否已存在
+     */
+    public boolean producerNodeExists(String node) {
+        try {
+            return redisTemplate.hasKey(PRODUCER_NODE_KEY_PREFIX + node);
+        } catch (Exception e) {
+            log.error("检查生产节点是否存在失败", e);
+            return false;
+        }
+    }
+
+    /**
+     * 原子校准全局生成批次号到最大值(仅当差值超过阈值时才更新)
+     *
+     * @param calibratedBatchId 校准目标批次号(最大ID对应的批次号)
+     * @param threshold         差值阈值:仅当 calibratedBatchId - currentGen > threshold 时才校准
+     * @return 是否校准成功
+     */
+    public boolean calibrateGlobalBatchIdToMax(long calibratedBatchId, int threshold) {
+        if (threshold < 0) {
+            log.warn("校准阈值不能为负数,threshold={}", threshold);
+            return false;
+        }
+        if (calibratedBatchId < 1) {
+            log.warn("校准批次号不能小于1,calibratedBatchId={}", calibratedBatchId);
+            return false;
+        }
+        String luaScript = "local currentGen = tonumber(redis.call('get', KEYS[1]) or 0)\n" +
+                "local calibrated = tonumber(ARGV[1])\n" +
+                "local threshold = tonumber(ARGV[2])\n" +
+                "-- 核心条件:当前批次号 < 校准批次号 且 差值超过阈值\n" +
+                "if currentGen > calibrated and (currentGen - calibrated) > threshold then\n" +
+                "    redis.call('set', KEYS[1], calibrated)\n" +
+                "    return 1\n" +
+                "end\n" +
+                "return 0";
+        try {
+            Long result = redisTemplate.execute(
+                    new DefaultRedisScript<>(luaScript, Long.class),
+                    Collections.singletonList(GLOBAL_GENERATED_BATCH_KEY),
+                    String.valueOf(calibratedBatchId),
+                    String.valueOf(threshold)
+            );
+            // 新增:打印Lua脚本执行结果,排查null问题
+            log.debug("Lua脚本执行结果:{}", result);
+            if (result == null) {
+                log.error("Lua脚本执行返回null,Redis连接/脚本可能异常");
+                return false;
+            }
+            boolean success = result == 1;
+            // 简化日志,避免重复计算差值
+            if (success) {
+                log.info("Lua脚本:批次号校准成功,校准为{}", calibratedBatchId);
+            } else {
+                log.debug("Lua脚本:无需校准,阈值={}", threshold);
+            }
+            return success;
+        } catch (Exception e) {
+            log.error("原子提升批次号失败,calibratedBatchId={},threshold={}", calibratedBatchId, threshold, e);
+            return false;
+        }
+    }
+
+    /**
+     * 获取最大生成批次号
+     */
+    public long getGeneratedMaxBatch() {
+        String generatedMax = redisTemplate.opsForValue().get(GLOBAL_GENERATED_BATCH_KEY);
+        return generatedMax == null ? 0 : Long.parseLong(generatedMax);
+    }
+
+    /**
+     * 通用分布式锁抢占
+     */
+    public boolean tryLock(String lockKey, long waitTime, TimeUnit timeUnit) {
+        try {
+            RLock rLock = redissonClient.getLock(lockKey);
+            return rLock.tryLock(waitTime, timeUnit);
+        } catch (Exception e) {
+            log.error("获取锁{}失败", lockKey, e);
+            return false;
+        }
+    }
+
+    /**
+     * 通用分布式锁释放
+     */
+    public void unlock(String lockKey) {
+        try {
+            RLock lock = redissonClient.getLock(lockKey);
+            if (lock.isHeldByCurrentThread()) {
+                lock.unlock();
+            }
+        } catch (Exception e) {
+            log.error("释放锁{}失败", lockKey, e);
+        }
+    }
+}

+ 202 - 0
fs-app-task/src/main/java/com/fs/common/redis/RedisSetProcessor.java

@@ -0,0 +1,202 @@
+package com.fs.common.redis;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.RedisConnection;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.Cursor;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.ScanOptions;
+import org.springframework.data.redis.core.script.DefaultRedisScript;
+import org.springframework.stereotype.Component;
+
+import java.util.*;
+
+@Component
+@Slf4j
+public class RedisSetProcessor {
+
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
+
+    // Lua脚本:删除指定key的指定元素,删除后若Set为空则删除key(原子操作)
+    private static final String DELETE_ELEMENTS_CLEAN_EMPTY_KEY_LUA =
+            "local key = KEYS[1]\n" +
+                    "local elements = ARGV\n" +
+                    "-- 删除指定元素\n" +
+                    "local delCount = redis.call('SREM', key, unpack(elements))\n" +
+                    "-- 检查Set是否为空,为空则删key\n" +
+                    "local setSize = redis.call('SCARD', key)\n" +
+                    "local isDelKey = 0\n" +
+                    "if setSize == 0 then\n" +
+                    "    redis.call('DEL', key)\n" +
+                    "    isDelKey = 1\n" +
+                    "end\n" +
+                    "-- 返回:[删除元素数, 是否删除了key]\n" +
+                    "return {delCount, isDelKey}";
+
+    /**
+     * 扫描指定前缀开头的Set类型Key,返回指定条数的Key集合
+     */
+    public Set<String> scanKeysByPrefix(String prefix, int batchSize, String type, boolean needDelete) {
+        // 参数校验
+        if (prefix == null) {
+            log.warn("扫描前缀不能为空");
+            return Collections.emptySet();
+        }
+        if (batchSize <= 0) {
+            log.warn("扫描条数必须大于0,当前值:{}", batchSize);
+            return Collections.emptySet();
+        }
+        Set<String> setKeys = new HashSet<>(batchSize); // 初始化容量,提升性能
+        RedisConnection connection = null;
+        Cursor<byte[]> cursor = null;
+        try {
+            // 获取Redis连接并构建扫描参数
+            RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
+            if (factory == null) {
+                log.error("Redis连接工厂获取失败");
+                return Collections.emptySet();
+            }
+            connection = factory.getConnection();
+            // SCAN参数:匹配前缀Key,count设为1000(Redis单次扫描的Key数,非返回结果数)
+            ScanOptions scanOptions = ScanOptions.scanOptions()
+                    .match(prefix + "*")
+                    .count(200)
+                    .build();
+            cursor = connection.scan(scanOptions);
+            // 遍历游标,筛选Set类型Key,直到达到指定条数
+            while (cursor.hasNext() && setKeys.size() < batchSize) {
+                byte[] keyBytes = cursor.next();
+                String key = redisTemplate.getStringSerializer().deserialize(keyBytes);
+                // 过滤空Key + 校验是否为Set类型
+                if (key == null || !isTargetType(key, type)) {
+                    continue;
+                }
+                if (needDelete) {
+                    redisTemplate.delete(key); // 删除 key
+                    log.info("[Redis删除] key = {}", key);
+                }
+                // 加入结果集合
+                setKeys.add(key);
+            }
+            log.info("Set Key扫描完成:前缀={},请求条数={},实际返回Set Key数={}",
+                    prefix, batchSize, setKeys.size());
+        } catch (Exception e) {
+            log.error("扫描Redis Set类型Key失败,前缀={}", prefix, e);
+            setKeys.clear(); // 异常时返回空集合
+        } finally {
+            // 释放Redis资源,避免泄漏
+            closeCursor(cursor);
+            closeConnection(connection);
+        }
+        return setKeys;
+    }
+
+    /**
+     * 判断指定Key是否为Set类型
+     */
+    private boolean isTargetType(String key, String type) {
+        try {
+            return type.equalsIgnoreCase(String.valueOf(redisTemplate.type(key)));
+        } catch (Exception e) {
+            log.error("判断Key[{}]类型失败", key, e);
+            return false;
+        }
+    }
+
+    /**
+     * 安全获取Set Key的随机元素(避免randomMembers的类型转换异常)
+     *
+     * @param key   Set Key
+     * @param count 要获取的元素数
+     * @return 随机元素列表(数量≤count)
+     */
+    public List<Object> getRandomSetElements(String key, int count) {
+        // 获取Set所有元素(原生返回Set,无类型冲突)
+        Set<Object> allElements = redisTemplate.opsForSet().members(key);
+        if (allElements == null || allElements.isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        List<Object> elementList = new ArrayList<>(allElements);
+        int totalSize = elementList.size();
+
+        // 如果需要的数量≥总元素数,直接返回全部
+        if (count >= totalSize) {
+            return elementList;
+        }
+
+        // 随机打乱后截取前count个(等价于randomMembers的随机效果)
+        Collections.shuffle(elementList, new Random());
+        return elementList.subList(0, count);
+    }
+
+    /**
+     * 关闭Cursor(资源释放)
+     */
+    private void closeCursor(Cursor<byte[]> cursor) {
+        if (cursor != null) {
+            try {
+                cursor.close();
+            } catch (Exception e) {
+                log.error("关闭Redis Cursor失败", e);
+            }
+        }
+    }
+
+    /**
+     * 关闭Redis Connection(资源释放)
+     */
+    private void closeConnection(RedisConnection connection) {
+        if (connection != null) {
+            try {
+                connection.close();
+            } catch (Exception e) {
+                log.error("关闭Redis Connection失败", e);
+            }
+        }
+    }
+
+
+    /**
+     * 原子删除指定key的指定元素,若删除后Set为空则删除该key(Lua保证原子性)
+     *
+     * @param key      Set的key(如user:tag1)
+     * @param elements 要删除的元素列表
+     * @return 结果数组:[0]实际删除的元素数,[1]是否删除了空Set的key(1=是,0=否)
+     */
+    public Long[] deleteSetElements(String key, List<Object> elements) {
+        // 参数校验
+        if (key == null || key.isEmpty() || elements == null || elements.isEmpty()) {
+            log.warn("删除参数异常:key={}, elements={}", key, elements);
+            return new Long[]{0L, 0L};
+        }
+
+        // 初始化Lua脚本
+        DefaultRedisScript<List> script = new DefaultRedisScript<>();
+        script.setScriptText(DELETE_ELEMENTS_CLEAN_EMPTY_KEY_LUA);
+        script.setResultType(List.class);
+
+        try {
+            // 执行Lua脚本(原子操作)
+            List<Long> result = redisTemplate.execute(
+                    script,
+                    Collections.singletonList(key),
+                    elements.toArray()
+            );
+            // 处理返回结果
+            if (result.size() == 2) {
+                Long delCount = result.get(0);
+                Long isDelKey = result.get(1);
+                log.info("删除SetKey:{},实际删除元素数:{},是否删除空key:{}", key, delCount, isDelKey == 1);
+                return new Long[]{delCount, isDelKey};
+            } else {
+                log.warn("Lua脚本返回结果异常:{}", result);
+            }
+        } catch (Exception e) {
+            log.error("删除Set元素失败,key={}", key, e);
+        }
+        return new Long[]{0L, 0L};
+    }
+}

+ 171 - 0
fs-app-task/src/main/java/com/fs/framework/aspectj/DataScopeAspect.java

@@ -0,0 +1,171 @@
+package com.fs.framework.aspectj;
+
+import com.fs.common.annotation.DataScope;
+import com.fs.common.core.domain.BaseEntity;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.spring.SpringUtils;
+import com.fs.company.domain.CompanyRole;
+import com.fs.company.domain.CompanyUser;
+import com.fs.framework.security.LoginUser;
+import com.fs.framework.service.TokenService;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+
+/**
+ * 数据过滤处理
+ *
+
+ */
+@Aspect
+@Component
+public class DataScopeAspect
+{
+    /**
+     * 全部数据权限
+     */
+    public static final String DATA_SCOPE_ALL = "1";
+
+    /**
+     * 自定数据权限
+     */
+    public static final String DATA_SCOPE_CUSTOM = "2";
+
+    /**
+     * 部门数据权限
+     */
+    public static final String DATA_SCOPE_DEPT = "3";
+
+    /**
+     * 部门及以下数据权限
+     */
+    public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";
+
+    /**
+     * 仅本人数据权限
+     */
+    public static final String DATA_SCOPE_SELF = "5";
+
+    /**
+     * 数据权限过滤关键字
+     */
+    public static final String DATA_SCOPE = "dataScope";
+
+    // 配置织入点
+    @Pointcut("@annotation(com.fs.common.annotation.DataScope)")
+    public void dataScopePointCut()
+    {
+    }
+
+    @Before("dataScopePointCut()")
+    public void doBefore(JoinPoint point) throws Throwable
+    {
+        handleDataScope(point);
+    }
+
+    protected void handleDataScope(final JoinPoint joinPoint)
+    {
+        // 获得注解
+        DataScope controllerDataScope = getAnnotationLog(joinPoint);
+        if (controllerDataScope == null)
+        {
+            return;
+        }
+        // 获取当前的用户
+        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
+        if (StringUtils.isNotNull(loginUser))
+        {
+            CompanyUser currentUser = loginUser.getUser();
+            // 如果是超级管理员,则不过滤数据
+            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
+            {
+                dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(),
+                        controllerDataScope.userAlias());
+            }
+        }
+    }
+
+    /**
+     * 数据范围过滤
+     *
+     * @param joinPoint 切点
+     * @param user 用户
+     * @param userAlias 别名
+     */
+    public static void dataScopeFilter(JoinPoint joinPoint, CompanyUser user, String deptAlias, String userAlias)
+    {
+        StringBuilder sqlString = new StringBuilder();
+
+        for (CompanyRole role : user.getRoles())
+        {
+            String dataScope = role.getDataScope();
+            if (DATA_SCOPE_ALL.equals(dataScope))
+            {
+                sqlString = new StringBuilder();
+                break;
+            }
+            else if (DATA_SCOPE_CUSTOM.equals(dataScope))
+            {
+                sqlString.append(StringUtils.format(
+                        " OR {}.dept_id IN ( SELECT dept_id FROM company_role_dept WHERE role_id = {} ) ", deptAlias,
+                        role.getRoleId()));
+            }
+            else if (DATA_SCOPE_DEPT.equals(dataScope))
+            {
+                sqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId()));
+            }
+            else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
+            {
+                sqlString.append(StringUtils.format(
+                        " OR {}.dept_id IN ( SELECT dept_id FROM company_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )",
+                        deptAlias, user.getDeptId(), user.getDeptId()));
+            }
+            else if (DATA_SCOPE_SELF.equals(dataScope))
+            {
+                if (StringUtils.isNotBlank(userAlias))
+                {
+                    sqlString.append(StringUtils.format(" OR {}.user_id = {} ", userAlias, user.getUserId()));
+                }
+                else
+                {
+                    // 数据权限为仅本人且没有userAlias别名不查询任何数据
+                    //sqlString.append(" OR 1=0 ");
+                        sqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId()));
+                }
+            }
+        }
+
+        if (StringUtils.isNotBlank(sqlString.toString()))
+        {
+            Object params = joinPoint.getArgs()[0];
+            if (StringUtils.isNotNull(params) && params instanceof BaseEntity)
+            {
+                BaseEntity baseEntity = (BaseEntity) params;
+                baseEntity.getParams().put(DATA_SCOPE, " AND (" + sqlString.substring(4) + ")");
+            }
+        }
+    }
+
+    /**
+     * 是否存在注解,如果存在就获取
+     */
+    private DataScope getAnnotationLog(JoinPoint joinPoint)
+    {
+        Signature signature = joinPoint.getSignature();
+        MethodSignature methodSignature = (MethodSignature) signature;
+        Method method = methodSignature.getMethod();
+
+        if (method != null)
+        {
+            return method.getAnnotation(DataScope.class);
+        }
+        return null;
+    }
+}

+ 73 - 0
fs-app-task/src/main/java/com/fs/framework/aspectj/DataSourceAspect.java

@@ -0,0 +1,73 @@
+package com.fs.framework.aspectj;
+
+import com.fs.common.annotation.DataSource;
+import com.fs.common.utils.StringUtils;
+import com.fs.framework.datasource.DynamicDataSourceContextHolder;
+import org.aspectj.lang.ProceedingJoinPoint;
+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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.util.Objects;
+
+/**
+ * 多数据源处理
+ * 
+ 
+ */
+@Aspect
+@Order(1)
+@Component
+public class DataSourceAspect
+{
+    protected Logger logger = LoggerFactory.getLogger(getClass());
+
+    @Pointcut("@annotation(com.fs.common.annotation.DataSource)"
+            + "|| @within(com.fs.common.annotation.DataSource)")
+    public void dsPointCut()
+    {
+
+    }
+
+    @Around("dsPointCut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable
+    {
+        DataSource dataSource = getDataSource(point);
+
+        if (StringUtils.isNotNull(dataSource))
+        {
+            DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
+        }
+
+        try
+        {
+            return point.proceed();
+        }
+        finally
+        {
+            // 销毁数据源 在执行方法之后
+            DynamicDataSourceContextHolder.clearDataSourceType();
+        }
+    }
+
+    /**
+     * 获取需要切换的数据源
+     */
+    public DataSource getDataSource(ProceedingJoinPoint point)
+    {
+        MethodSignature signature = (MethodSignature) point.getSignature();
+        DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);
+        if (Objects.nonNull(dataSource))
+        {
+            return dataSource;
+        }
+
+        return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);
+    }
+}

+ 219 - 0
fs-app-task/src/main/java/com/fs/framework/aspectj/LogAspect.java

@@ -0,0 +1,219 @@
+package com.fs.framework.aspectj;
+
+import com.alibaba.fastjson.JSON;
+import com.fs.common.annotation.Log;
+import com.fs.common.enums.BusinessStatus;
+import com.fs.common.enums.HttpMethod;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.ip.IpUtils;
+import com.fs.common.utils.spring.SpringUtils;
+import com.fs.company.domain.CompanyOperLog;
+import com.fs.framework.manager.AsyncManager;
+import com.fs.framework.manager.factory.AsyncFactory;
+import com.fs.framework.security.LoginUser;
+import com.fs.framework.service.TokenService;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.AfterReturning;
+import org.aspectj.lang.annotation.AfterThrowing;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.servlet.HandlerMapping;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+/**
+ * 操作日志记录处理
+ * 
+
+ */
+@Aspect
+@Component
+public class LogAspect
+{
+    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
+
+    // 配置织入点
+    @Pointcut("@annotation(com.fs.common.annotation.Log)")
+    public void logPointCut()
+    {
+    }
+
+    /**
+     * 处理完请求后执行
+     *
+     * @param joinPoint 切点
+     */
+    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
+    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult)
+    {
+        handleLog(joinPoint, null, jsonResult);
+    }
+
+    /**
+     * 拦截异常操作
+     * 
+     * @param joinPoint 切点
+     * @param e 异常
+     */
+    @AfterThrowing(value = "logPointCut()", throwing = "e")
+    public void doAfterThrowing(JoinPoint joinPoint, Exception e)
+    {
+        handleLog(joinPoint, e, null);
+    }
+
+    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult)
+    {
+        try
+        {
+            // 获得注解
+            Log controllerLog = getAnnotationLog(joinPoint);
+            if (controllerLog == null)
+            {
+                return;
+            }
+
+            // 获取当前的用户
+            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
+
+            // *========数据库日志=========*//
+            CompanyOperLog operLog = new CompanyOperLog();
+            operLog.setCompanyId(loginUser.getCompany().getCompanyId());
+            operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
+            // 请求的地址
+            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
+            operLog.setOperIp(ip);
+            // 返回参数
+            operLog.setJsonResult(JSON.toJSONString(jsonResult));
+
+            operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
+            if (loginUser != null)
+            {
+                operLog.setOperName(loginUser.getUsername());
+            }
+
+            if (e != null)
+            {
+                operLog.setStatus(BusinessStatus.FAIL.ordinal());
+                operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
+            }
+            // 设置方法名称
+            String className = joinPoint.getTarget().getClass().getName();
+            String methodName = joinPoint.getSignature().getName();
+            operLog.setMethod(className + "." + methodName + "()");
+            // 设置请求方式
+            operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
+            // 处理设置注解上的参数
+            getControllerMethodDescription(joinPoint, controllerLog, operLog);
+            // 保存数据库
+            AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
+        }
+        catch (Exception exp)
+        {
+            // 记录本地异常日志
+            log.error("==前置通知异常==");
+            log.error("异常信息:{}", exp.getMessage());
+            exp.printStackTrace();
+        }
+    }
+
+    /**
+     * 获取注解中对方法的描述信息 用于Controller层注解
+     * 
+     * @param log 日志
+     * @param operLog 操作日志
+     * @throws Exception
+     */
+    public void getControllerMethodDescription(JoinPoint joinPoint, Log log, CompanyOperLog operLog) throws Exception
+    {
+        // 设置action动作
+        operLog.setBusinessType(log.businessType().ordinal());
+        // 设置标题
+        operLog.setTitle(log.title());
+        // 设置操作人类别
+        operLog.setOperatorType(log.operatorType().ordinal());
+        // 是否需要保存request,参数和值
+        if (log.isSaveRequestData())
+        {
+            // 获取参数的信息,传入到数据库中。
+            setRequestValue(joinPoint, operLog);
+        }
+    }
+
+    /**
+     * 获取请求的参数,放到log中
+     * 
+     * @param operLog 操作日志
+     * @throws Exception 异常
+     */
+    private void setRequestValue(JoinPoint joinPoint, CompanyOperLog operLog) throws Exception
+    {
+        String requestMethod = operLog.getRequestMethod();
+        if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod))
+        {
+            String params = argsArrayToString(joinPoint.getArgs());
+            operLog.setOperParam(StringUtils.substring(params, 0, 2000));
+        }
+        else
+        {
+            Map<?, ?> paramsMap = (Map<?, ?>) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
+            operLog.setOperParam(StringUtils.substring(paramsMap.toString(), 0, 2000));
+        }
+    }
+
+    /**
+     * 是否存在注解,如果存在就获取
+     */
+    private Log getAnnotationLog(JoinPoint joinPoint) throws Exception
+    {
+        Signature signature = joinPoint.getSignature();
+        MethodSignature methodSignature = (MethodSignature) signature;
+        Method method = methodSignature.getMethod();
+
+        if (method != null)
+        {
+            return method.getAnnotation(Log.class);
+        }
+        return null;
+    }
+
+    /**
+     * 参数拼装
+     */
+    private String argsArrayToString(Object[] paramsArray)
+    {
+        String params = "";
+        if (paramsArray != null && paramsArray.length > 0)
+        {
+            for (int i = 0; i < paramsArray.length; i++)
+            {
+                if (!isFilterObject(paramsArray[i]))
+                {
+                    Object jsonObj = JSON.toJSON(paramsArray[i]);
+                    params += jsonObj.toString() + " ";
+                }
+            }
+        }
+        return params.trim();
+    }
+
+    /**
+     * 判断是否需要过滤的对象。
+     * 
+     * @param o 对象信息。
+     * @return 如果是需要过滤的对象,则返回true;否则返回false。
+     */
+    public boolean isFilterObject(final Object o)
+    {
+        return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse;
+    }
+}

+ 117 - 0
fs-app-task/src/main/java/com/fs/framework/aspectj/RateLimiterAspect.java

@@ -0,0 +1,117 @@
+package com.fs.framework.aspectj;
+
+import com.fs.common.annotation.RateLimiter;
+import com.fs.common.enums.LimitType;
+import com.fs.common.exception.ServiceException;
+import com.fs.common.utils.ServletUtils;
+import com.fs.common.utils.StringUtils;
+import com.fs.common.utils.ip.IpUtils;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.script.RedisScript;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * 限流处理
+ *
+
+ */
+@Aspect
+@Component
+public class RateLimiterAspect
+{
+    private static final Logger log = LoggerFactory.getLogger(RateLimiterAspect.class);
+
+    private RedisTemplate<Object, Object> redisTemplate;
+
+    private RedisScript<Long> limitScript;
+
+    @Autowired
+    public void setRedisTemplate1(RedisTemplate<Object, Object> redisTemplate)
+    {
+        this.redisTemplate = redisTemplate;
+    }
+
+    @Autowired
+    public void setLimitScript(RedisScript<Long> limitScript)
+    {
+        this.limitScript = limitScript;
+    }
+
+    // 配置织入点
+    @Pointcut("@annotation(com.fs.common.annotation.RateLimiter)")
+    public void rateLimiterPointCut()
+    {
+    }
+
+    @Before("rateLimiterPointCut()")
+    public void doBefore(JoinPoint point) throws Throwable
+    {
+        RateLimiter rateLimiter = getAnnotationRateLimiter(point);
+        String key = rateLimiter.key();
+        int time = rateLimiter.time();
+        int count = rateLimiter.count();
+
+        String combineKey = getCombineKey(rateLimiter, point);
+        List<Object> keys = Collections.singletonList(combineKey);
+        try
+        {
+            Long number = redisTemplate.execute(limitScript, keys, count, time);
+            if (StringUtils.isNull(number) || number.intValue() > count)
+            {
+                throw new ServiceException("访问过于频繁,请稍后再试");
+            }
+            log.info("限制请求'{}',当前请求'{}',缓存key'{}'", count, number.intValue(), key);
+        }
+        catch (ServiceException e)
+        {
+            throw e;
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("服务器限流异常,请稍后再试");
+        }
+    }
+
+    /**
+     * 是否存在注解,如果存在就获取
+     */
+    private RateLimiter getAnnotationRateLimiter(JoinPoint joinPoint)
+    {
+        Signature signature = joinPoint.getSignature();
+        MethodSignature methodSignature = (MethodSignature) signature;
+        Method method = methodSignature.getMethod();
+
+        if (method != null)
+        {
+            return method.getAnnotation(RateLimiter.class);
+        }
+        return null;
+    }
+
+    public String getCombineKey(RateLimiter rateLimiter, JoinPoint point)
+    {
+        StringBuffer stringBuffer = new StringBuffer(rateLimiter.key());
+        if (rateLimiter.limitType() == LimitType.IP)
+        {
+            stringBuffer.append(IpUtils.getIpAddr(ServletUtils.getRequest()));
+        }
+        MethodSignature signature = (MethodSignature) point.getSignature();
+        Method method = signature.getMethod();
+        Class<?> targetClass = method.getDeclaringClass();
+        stringBuffer.append("-").append(targetClass.getName()).append("- ").append(method.getName());
+        return stringBuffer.toString();
+    }
+}

+ 31 - 0
fs-app-task/src/main/java/com/fs/framework/config/ApplicationConfig.java

@@ -0,0 +1,31 @@
+package com.fs.framework.config;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+
+import java.util.TimeZone;
+
+/**
+ * 程序注解配置
+ *
+
+ */
+@Configuration
+// 表示通过aop框架暴露该代理对象,AopContext能够访问
+@EnableAspectJAutoProxy(exposeProxy = true)
+// 指定要扫描的Mapper类的包的路径
+@MapperScan("com.fs.**.mapper")
+public class ApplicationConfig
+{
+    /**
+     * 时区配置
+     */
+    @Bean
+    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization()
+    {
+        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
+    }
+}

+ 58 - 0
fs-app-task/src/main/java/com/fs/framework/config/ArrayStringTypeHandler.java

@@ -0,0 +1,58 @@
+package com.fs.framework.config;
+
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+import org.springframework.context.annotation.Configuration;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+
+@Configuration
+public class ArrayStringTypeHandler extends BaseTypeHandler<List<String>> {
+
+    @Override
+    public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
+        // 将 List<String> 转换为字符串,ClickHouse 支持的格式为 "['item1', 'item2']"
+        StringBuilder sb = new StringBuilder();
+        sb.append("[");
+        for (int j = 0; j < parameter.size(); j++) {
+            sb.append("'").append(parameter.get(j)).append("'");
+            if (j < parameter.size() - 1) {
+                sb.append(",");
+            }
+        }
+        sb.append("]");
+        ps.setString(i, sb.toString());
+    }
+
+    @Override
+    public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
+        // 处理查询结果,将其转换为 List<String>
+        String result = rs.getString(columnName);
+        return parseArray(result);
+    }
+
+    @Override
+    public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+        String result = rs.getString(columnIndex);
+        return parseArray(result);
+    }
+
+    @Override
+    public List<String> getNullableResult(java.sql.CallableStatement cs, int columnIndex) throws SQLException {
+        String result = cs.getString(columnIndex);
+        return parseArray(result);
+    }
+
+    private List<String> parseArray(String arrayStr) {
+        // 将 ClickHouse 的 Array 字符串转换为 List<String>
+        if (arrayStr == null || arrayStr.isEmpty()) {
+            return null;
+        }
+        arrayStr = arrayStr.substring(1, arrayStr.length() - 1);  // 去掉 "[" 和 "]"
+        String[] elements = arrayStr.split(",");
+        return java.util.Arrays.asList(elements);
+    }
+}

+ 85 - 0
fs-app-task/src/main/java/com/fs/framework/config/CaptchaConfig.java

@@ -0,0 +1,85 @@
+package com.fs.framework.config;
+
+import com.google.code.kaptcha.impl.DefaultKaptcha;
+import com.google.code.kaptcha.util.Config;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.Properties;
+
+import static com.google.code.kaptcha.Constants.*;
+
+/**
+ * 验证码配置
+ * 
+
+ */
+@Configuration
+public class CaptchaConfig
+{
+    @Bean(name = "captchaProducer")
+    public DefaultKaptcha getKaptchaBean()
+    {
+        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
+        Properties properties = new Properties();
+        // 是否有边框 默认为true 我们可以自己设置yes,no
+        properties.setProperty(KAPTCHA_BORDER, "yes");
+        // 验证码文本字符颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
+        // 验证码图片宽度 默认为200
+        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
+        // 验证码图片高度 默认为50
+        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
+        // 验证码文本字符大小 默认为40
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
+        // KAPTCHA_SESSION_KEY
+        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
+        // 验证码文本字符长度 默认为5
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
+        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
+        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
+        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
+        Config config = new Config(properties);
+        defaultKaptcha.setConfig(config);
+        return defaultKaptcha;
+    }
+
+    @Bean(name = "captchaProducerMath")
+    public DefaultKaptcha getKaptchaBeanMath()
+    {
+        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
+        Properties properties = new Properties();
+        // 是否有边框 默认为true 我们可以自己设置yes,no
+        properties.setProperty(KAPTCHA_BORDER, "yes");
+        // 边框颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
+        // 验证码文本字符颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
+        // 验证码图片宽度 默认为200
+        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
+        // 验证码图片高度 默认为50
+        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
+        // 验证码文本字符大小 默认为40
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
+        // KAPTCHA_SESSION_KEY
+        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
+        // 验证码文本生成器
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.fs.framework.config.KaptchaTextCreator");
+        // 验证码文本字符间距 默认为2
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
+        // 验证码文本字符长度 默认为5
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
+        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
+        // 验证码噪点颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
+        // 干扰实现类
+        properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
+        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
+        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
+        Config config = new Config(properties);
+        defaultKaptcha.setConfig(config);
+        return defaultKaptcha;
+    }
+}

+ 135 - 0
fs-app-task/src/main/java/com/fs/framework/config/DataSourceConfig.java

@@ -0,0 +1,135 @@
+package com.fs.framework.config;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
+import com.alibaba.druid.util.Utils;
+import com.fs.common.enums.DataSourceType;
+import com.fs.framework.datasource.DynamicDataSource;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+
+import javax.servlet.*;
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+@Configuration
+public class DataSourceConfig {
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.sop.druid.master")
+    public DataSource sopDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.sop.druid.read")
+    public DataSource sopReadDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.clickhouse")
+    public DataSource clickhouseDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.mysql.druid.master")
+    public DataSource masterDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.mysql.druid.slave")
+    public DataSource slaveDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.live.druid.master")
+    public DataSource liveDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.zmlive.druid.master")
+    public DataSource zmLiveDataSource() {
+        return new DruidDataSource();
+    }
+
+
+
+    @Bean
+    @Primary
+    public DynamicDataSource dataSource(@Qualifier("clickhouseDataSource") DataSource clickhouseDataSource,
+                                        @Qualifier("masterDataSource") DataSource masterDataSource,
+                                        @Qualifier("sopDataSource") DataSource sopDataSource,
+                                        @Qualifier("slaveDataSource") DataSource slaveDataSource,
+                                        @Qualifier("sopReadDataSource") DataSource sopReadDataSource,
+                                        @Qualifier("liveDataSource") DataSource liveDataSource,
+                                        @Qualifier("zmLiveDataSource") DataSource zmLiveDataSource
+                                        ) {
+        Map<Object, Object> targetDataSources = new HashMap<>();
+        targetDataSources.put(DataSourceType.MASTER, masterDataSource);
+
+        targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
+        targetDataSources.put(DataSourceType.SOP.name(), sopDataSource);
+        targetDataSources.put(DataSourceType.SopREAD.name(), sopReadDataSource);
+        targetDataSources.put(DataSourceType.CLICKHOUSE.name(), clickhouseDataSource); // Ensure matching key
+//        targetDataSources.put(DataSourceType.LIVE.name(), liveDataSource);
+//        targetDataSources.put(DataSourceType.ZMLIVE.name(), zmLiveDataSource);
+        return new DynamicDataSource(masterDataSource, targetDataSources);
+    }
+
+    /**
+     * 去除监控页面底部的广告
+     */
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Bean
+    @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
+    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
+    {
+        // 获取web监控页面的参数
+        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
+        // 提取common.js的配置路径
+        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
+        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
+        final String filePath = "support/http/resources/js/common.js";
+        // 创建filter进行过滤
+        Filter filter = new Filter()
+        {
+            @Override
+            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
+            {
+            }
+            @Override
+            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+                    throws IOException, ServletException
+            {
+                chain.doFilter(request, response);
+                // 重置缓冲区,响应头不会被重置
+                response.resetBuffer();
+                // 获取common.js
+                String text = Utils.readFromResource(filePath);
+                // 正则替换banner, 除去底部的广告信息
+                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
+                text = text.replaceAll("powered.*?shrek.wang</a>", "");
+                response.getWriter().write(text);
+            }
+            @Override
+            public void destroy()
+            {
+            }
+        };
+        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
+        registrationBean.setFilter(filter);
+        registrationBean.addUrlPatterns(commonJsPattern);
+        return registrationBean;
+    }
+}

+ 123 - 0
fs-app-task/src/main/java/com/fs/framework/config/DruidConfig.java

@@ -0,0 +1,123 @@
+package com.fs.framework.config;//package com.fs.framework.config;
+//
+//import com.alibaba.druid.pool.DruidDataSource;
+//import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
+//import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
+//import com.alibaba.druid.util.Utils;
+//import com.fs.framework.datasource.DynamicDataSource;
+//import com.fs.common.enums.DataSourceType;
+//import com.fs.common.utils.spring.SpringUtils;
+//import com.fs.framework.config.properties.DruidProperties;
+//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+//import org.springframework.boot.context.properties.ConfigurationProperties;
+//import org.springframework.boot.web.servlet.FilterRegistrationBean;
+//import org.springframework.context.annotation.Bean;
+//import org.springframework.context.annotation.Configuration;
+//import org.springframework.context.annotation.Primary;
+//
+//import javax.servlet.*;
+//import javax.sql.DataSource;
+//import java.io.IOException;
+//import java.util.HashMap;
+//import java.util.Map;
+//
+///**
+// * druid 配置多数据源
+// *
+//
+// */
+//@Configuration
+//public class DruidConfig
+//{
+//    @Bean
+//    @ConfigurationProperties("spring.datasource.druid.master")
+//    public DataSource masterDataSource(DruidProperties druidProperties)
+//    {
+//        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
+//        return druidProperties.dataSource(dataSource);
+//    }
+//
+//    @Bean
+//    @ConfigurationProperties("spring.datasource.druid.slave")
+//    @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
+//    public DataSource slaveDataSource(DruidProperties druidProperties)
+//    {
+//        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
+//        return druidProperties.dataSource(dataSource);
+//    }
+//
+//    @Bean(name = "dynamicDataSource")
+//    @Primary
+//    public DynamicDataSource dataSource(DataSource masterDataSource)
+//    {
+//        Map<Object, Object> targetDataSources = new HashMap<>();
+//        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
+//        setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
+//        return new DynamicDataSource(masterDataSource, targetDataSources);
+//    }
+//
+//    /**
+//     * 设置数据源
+//     *
+//     * @param targetDataSources 备选数据源集合
+//     * @param sourceName 数据源名称
+//     * @param beanName bean名称
+//     */
+//    public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
+//    {
+//        try
+//        {
+//            DataSource dataSource = SpringUtils.getBean(beanName);
+//            targetDataSources.put(sourceName, dataSource);
+//        }
+//        catch (Exception e)
+//        {
+//        }
+//    }
+//
+//    /**
+//     * 去除监控页面底部的广告
+//     */
+//    @SuppressWarnings({ "rawtypes", "unchecked" })
+//    @Bean
+//    @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
+//    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
+//    {
+//        // 获取web监控页面的参数
+//        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
+//        // 提取common.js的配置路径
+//        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
+//        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
+//        final String filePath = "support/http/resources/js/common.js";
+//        // 创建filter进行过滤
+//        Filter filter = new Filter()
+//        {
+//            @Override
+//            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
+//            {
+//            }
+//            @Override
+//            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+//                    throws IOException, ServletException
+//            {
+//                chain.doFilter(request, response);
+//                // 重置缓冲区,响应头不会被重置
+//                response.resetBuffer();
+//                // 获取common.js
+//                String text = Utils.readFromResource(filePath);
+//                // 正则替换banner, 除去底部的广告信息
+//                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
+//                text = text.replaceAll("powered.*?shrek.wang</a>", "");
+//                response.getWriter().write(text);
+//            }
+//            @Override
+//            public void destroy()
+//            {
+//            }
+//        };
+//        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
+//        registrationBean.setFilter(filter);
+//        registrationBean.addUrlPatterns(commonJsPattern);
+//        return registrationBean;
+//    }
+//}

+ 72 - 0
fs-app-task/src/main/java/com/fs/framework/config/FastJson2JsonRedisSerializer.java

@@ -0,0 +1,72 @@
+package com.fs.framework.config;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.parser.ParserConfig;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.data.redis.serializer.SerializationException;
+import org.springframework.util.Assert;
+
+import java.nio.charset.Charset;
+
+/**
+ * Redis使用FastJson序列化
+ * 
+
+ */
+public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
+{
+    @SuppressWarnings("unused")
+    private ObjectMapper objectMapper = new ObjectMapper();
+
+    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
+
+    private Class<T> clazz;
+
+    static
+    {
+        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
+    }
+
+    public FastJson2JsonRedisSerializer(Class<T> clazz)
+    {
+        super();
+        this.clazz = clazz;
+    }
+
+    @Override
+    public byte[] serialize(T t) throws SerializationException
+    {
+        if (t == null)
+        {
+            return new byte[0];
+        }
+        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
+    }
+
+    @Override
+    public T deserialize(byte[] bytes) throws SerializationException
+    {
+        if (bytes == null || bytes.length <= 0)
+        {
+            return null;
+        }
+        String str = new String(bytes, DEFAULT_CHARSET);
+
+        return JSON.parseObject(str, clazz);
+    }
+
+    public void setObjectMapper(ObjectMapper objectMapper)
+    {
+        Assert.notNull(objectMapper, "'objectMapper' must not be null");
+        this.objectMapper = objectMapper;
+    }
+
+    protected JavaType getJavaType(Class<?> clazz)
+    {
+        return TypeFactory.defaultInstance().constructType(clazz);
+    }
+}

+ 59 - 0
fs-app-task/src/main/java/com/fs/framework/config/FilterConfig.java

@@ -0,0 +1,59 @@
+package com.fs.framework.config;
+
+import com.fs.common.filter.RepeatableFilter;
+import com.fs.common.filter.XssFilter;
+import com.fs.common.utils.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.servlet.DispatcherType;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Filter配置
+ *
+
+ */
+@Configuration
+@ConditionalOnProperty(value = "xss.enabled", havingValue = "true")
+public class FilterConfig
+{
+    @Value("${xss.excludes}")
+    private String excludes;
+
+    @Value("${xss.urlPatterns}")
+    private String urlPatterns;
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Bean
+    public FilterRegistrationBean xssFilterRegistration()
+    {
+        FilterRegistrationBean registration = new FilterRegistrationBean();
+        registration.setDispatcherTypes(DispatcherType.REQUEST);
+        registration.setFilter(new XssFilter());
+        registration.addUrlPatterns(StringUtils.split(urlPatterns, ","));
+        registration.setName("xssFilter");
+        registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
+        Map<String, String> initParameters = new HashMap<String, String>();
+        initParameters.put("excludes", excludes);
+        registration.setInitParameters(initParameters);
+        return registration;
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Bean
+    public FilterRegistrationBean someFilterRegistration()
+    {
+        FilterRegistrationBean registration = new FilterRegistrationBean();
+        registration.setFilter(new RepeatableFilter());
+        registration.addUrlPatterns("/*");
+        registration.setName("repeatableFilter");
+        registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
+        return registration;
+    }
+
+}

+ 76 - 0
fs-app-task/src/main/java/com/fs/framework/config/KaptchaTextCreator.java

@@ -0,0 +1,76 @@
+package com.fs.framework.config;
+
+import com.google.code.kaptcha.text.impl.DefaultTextCreator;
+
+import java.util.Random;
+
+/**
+ * 验证码文本生成器
+ * 
+
+ */
+public class KaptchaTextCreator extends DefaultTextCreator
+{
+    private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
+
+    @Override
+    public String getText()
+    {
+        Integer result = 0;
+        Random random = new Random();
+        int x = random.nextInt(10);
+        int y = random.nextInt(10);
+        StringBuilder suChinese = new StringBuilder();
+        int randomoperands = (int) Math.round(Math.random() * 2);
+        if (randomoperands == 0)
+        {
+            result = x * y;
+            suChinese.append(CNUMBERS[x]);
+            suChinese.append("*");
+            suChinese.append(CNUMBERS[y]);
+        }
+        else if (randomoperands == 1)
+        {
+            if (!(x == 0) && y % x == 0)
+            {
+                result = y / x;
+                suChinese.append(CNUMBERS[y]);
+                suChinese.append("/");
+                suChinese.append(CNUMBERS[x]);
+            }
+            else
+            {
+                result = x + y;
+                suChinese.append(CNUMBERS[x]);
+                suChinese.append("+");
+                suChinese.append(CNUMBERS[y]);
+            }
+        }
+        else if (randomoperands == 2)
+        {
+            if (x >= y)
+            {
+                result = x - y;
+                suChinese.append(CNUMBERS[x]);
+                suChinese.append("-");
+                suChinese.append(CNUMBERS[y]);
+            }
+            else
+            {
+                result = y - x;
+                suChinese.append(CNUMBERS[y]);
+                suChinese.append("-");
+                suChinese.append(CNUMBERS[x]);
+            }
+        }
+        else
+        {
+            result = x + y;
+            suChinese.append(CNUMBERS[x]);
+            suChinese.append("+");
+            suChinese.append(CNUMBERS[y]);
+        }
+        suChinese.append("=?@" + result);
+        return suChinese.toString();
+    }
+}

+ 148 - 0
fs-app-task/src/main/java/com/fs/framework/config/MyBatisConfig.java

@@ -0,0 +1,148 @@
+package com.fs.framework.config;
+
+import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
+import org.apache.ibatis.io.VFS;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+import org.springframework.core.io.DefaultResourceLoader;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.core.io.support.ResourcePatternResolver;
+import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
+import org.springframework.core.type.classreading.MetadataReader;
+import org.springframework.core.type.classreading.MetadataReaderFactory;
+import org.springframework.util.ClassUtils;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+/**
+ * Mybatis支持*匹配扫描包
+ * 
+
+ */
+@Configuration
+public class MyBatisConfig
+{
+    @Autowired
+    private Environment env;
+
+    static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
+
+    public static String setTypeAliasesPackage(String typeAliasesPackage)
+    {
+        ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
+        MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
+        List<String> allResult = new ArrayList<String>();
+        try
+        {
+            for (String aliasesPackage : typeAliasesPackage.split(","))
+            {
+                List<String> result = new ArrayList<String>();
+                aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+                        + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
+                Resource[] resources = resolver.getResources(aliasesPackage);
+                if (resources != null && resources.length > 0)
+                {
+                    MetadataReader metadataReader = null;
+                    for (Resource resource : resources)
+                    {
+                        if (resource.isReadable())
+                        {
+                            metadataReader = metadataReaderFactory.getMetadataReader(resource);
+                            try
+                            {
+                                result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
+                            }
+                            catch (ClassNotFoundException e)
+                            {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                }
+                if (result.size() > 0)
+                {
+                    HashSet<String> hashResult = new HashSet<String>(result);
+                    allResult.addAll(hashResult);
+                }
+            }
+            if (allResult.size() > 0)
+            {
+                typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0]));
+            }
+            else
+            {
+                throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
+            }
+        }
+        catch (IOException e)
+        {
+            e.printStackTrace();
+        }
+        return typeAliasesPackage;
+    }
+
+    public Resource[] resolveMapperLocations(String[] mapperLocations)
+    {
+        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
+        List<Resource> resources = new ArrayList<Resource>();
+        if (mapperLocations != null)
+        {
+            for (String mapperLocation : mapperLocations)
+            {
+                try
+                {
+                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
+                    resources.addAll(Arrays.asList(mappers));
+                }
+                catch (IOException e)
+                {
+                    // ignore
+                }
+            }
+        }
+        return resources.toArray(new Resource[resources.size()]);
+    }
+
+//    @Bean
+//    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
+//    {
+//        String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
+//        String mapperLocations = env.getProperty("mybatis.mapperLocations");
+//        String configLocation = env.getProperty("mybatis.configLocation");
+//        typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
+//        VFS.addImplClass(SpringBootVFS.class);
+//
+//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
+//        sessionFactory.setDataSource(dataSource);
+//        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
+//        sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
+//        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
+//        return sessionFactory.getObject();
+//    }
+    @Bean
+    public SqlSessionFactory sqlSessionFactorys(DataSource dataSource) throws Exception
+    {
+        String typeAliasesPackage = env.getProperty("mybatis-plus.typeAliasesPackage");
+        String mapperLocations = env.getProperty("mybatis-plus.mapperLocations");
+        String configLocation = env.getProperty("mybatis-plus.configLocation");
+        typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
+        VFS.addImplClass(SpringBootVFS.class);
+
+        final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
+        sessionFactory.setDataSource(dataSource);
+        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
+        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
+        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
+        return sessionFactory.getObject();
+    }
+}

+ 153 - 0
fs-app-task/src/main/java/com/fs/framework/config/RedisConfig.java

@@ -0,0 +1,153 @@
+package com.fs.framework.config;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
+import org.springframework.cache.annotation.CachingConfigurerSupport;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.script.DefaultRedisScript;
+import org.springframework.data.redis.serializer.GenericToStringSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.math.BigDecimal;
+
+/**
+ * redis配置
+ *
+
+ */
+@Configuration
+@EnableCaching
+public class RedisConfig extends CachingConfigurerSupport
+{
+    @Bean
+    @SuppressWarnings(value = { "unchecked", "rawtypes" })
+    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
+    {
+        RedisTemplate<Object, Object> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
+
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
+        serializer.setObjectMapper(mapper);
+
+        // 使用StringRedisSerializer来序列化和反序列化redis的key值
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(serializer);
+
+        // Hash的key也采用StringRedisSerializer的序列化方式
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(serializer);
+
+        template.afterPropertiesSet();
+        return template;
+    }
+    @Bean
+    public RedisTemplate<String, Boolean> redisTemplateForBoolean(RedisConnectionFactory connectionFactory) {
+        RedisTemplate<String, Boolean> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        // 使用StringRedisSerializer来序列化和反序列化redis的key值
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(new GenericToStringSerializer<>(Boolean.class));
+
+        // Hash的key也采用StringRedisSerializer的序列化方式
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(new GenericToStringSerializer<>(Boolean.class));
+
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    @Bean(name = "redisTemplateForInteger")
+    public RedisTemplate<String, Integer> redisTemplateForInteger(RedisConnectionFactory connectionFactory) {
+        RedisTemplate<String, Integer> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(new GenericToStringSerializer<>(Integer.class));
+
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(new GenericToStringSerializer<>(Integer.class));
+
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    @Bean(name = "redisTemplateForBigDecimal")
+    public RedisTemplate<String, BigDecimal> redisTemplateForBigDecimal(RedisConnectionFactory connectionFactory) {
+        RedisTemplate<String, BigDecimal> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(new GenericToStringSerializer<>(BigDecimal.class));
+
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(new GenericToStringSerializer<>(BigDecimal.class));
+
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    @Bean
+    @SuppressWarnings(value = { "unchecked", "rawtypes" })
+    public RedisTemplate<String, Object> redisTemplateForObject(RedisConnectionFactory connectionFactory) {
+        RedisTemplate<String, Object> template = new RedisTemplate<>();
+        template.setConnectionFactory(connectionFactory);
+
+        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
+
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
+        serializer.setObjectMapper(mapper);
+
+        // 使用StringRedisSerializer来序列化和反序列化redis的key值
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(serializer);
+
+        // Hash的key也采用StringRedisSerializer的序列化方式
+        template.setHashKeySerializer(new StringRedisSerializer());
+        template.setHashValueSerializer(serializer);
+
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    @Bean
+    public DefaultRedisScript<Long> limitScript()
+    {
+        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
+        redisScript.setScriptText(limitScriptText());
+        redisScript.setResultType(Long.class);
+        return redisScript;
+    }
+
+    /**
+     * 限流脚本
+     */
+    private String limitScriptText()
+    {
+        return "local key = KEYS[1]\n" +
+                "local count = tonumber(ARGV[1])\n" +
+                "local time = tonumber(ARGV[2])\n" +
+                "local current = redis.call('get', key);\n" +
+                "if current and tonumber(current) > count then\n" +
+                "    return current;\n" +
+                "end\n" +
+                "current = redis.call('incr', key)\n" +
+                "if tonumber(current) == 1 then\n" +
+                "    redis.call('expire', key, time)\n" +
+                "end\n" +
+                "return current;";
+    }
+}

+ 76 - 0
fs-app-task/src/main/java/com/fs/framework/config/ResourcesConfig.java

@@ -0,0 +1,76 @@
+package com.fs.framework.config;
+
+import com.fs.common.config.FSConfig;
+import com.fs.common.constant.Constants;
+import com.fs.framework.interceptor.RepeatSubmitInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.format.FormatterRegistry;
+import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+import java.time.format.DateTimeFormatter;
+
+/**
+ * 通用配置
+ *
+
+ */
+@Configuration
+public class ResourcesConfig implements WebMvcConfigurer
+{
+    @Autowired
+    private RepeatSubmitInterceptor repeatSubmitInterceptor;
+
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry)
+    {
+        /** 本地文件上传路径 */
+        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + FSConfig.getProfile() + "/");
+
+        /** swagger配置 */
+        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
+    }
+
+    /**
+     * 自定义拦截规则
+     */
+    @Override
+    public void addInterceptors(InterceptorRegistry registry)
+    {
+        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
+    }
+
+    /**
+     * 跨域配置
+     */
+    @Bean
+    public CorsFilter corsFilter()
+    {
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        CorsConfiguration config = new CorsConfiguration();
+        config.setAllowCredentials(true);
+        // 设置访问源地址
+        config.addAllowedOrigin("*");
+        // 设置访问源请求头
+        config.addAllowedHeader("*");
+        // 设置访问源请求方法
+        config.addAllowedMethod("*");
+        // 对接口配置跨域设置
+        source.registerCorsConfiguration("/**", config);
+        return new CorsFilter(source);
+    }
+
+    @Override
+    public void addFormatters(FormatterRegistry registry) {
+        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
+        registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 统一日期格式
+        registrar.registerFormatters(registry);
+    }
+}

+ 11 - 0
fs-app-task/src/main/java/com/fs/framework/config/RetryConfig.java

@@ -0,0 +1,11 @@
+package com.fs.framework.config;
+
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.retry.annotation.EnableRetry;
+
+@Configuration
+@EnableRetry
+public class RetryConfig {
+
+}

+ 158 - 0
fs-app-task/src/main/java/com/fs/framework/config/SecurityConfig.java

@@ -0,0 +1,158 @@
+package com.fs.framework.config;
+
+
+import com.fs.framework.security.filter.JwtAuthenticationTokenFilter;
+import com.fs.framework.security.handle.AuthenticationEntryPointImpl;
+import com.fs.framework.security.handle.LogoutSuccessHandlerImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.http.HttpMethod;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.config.http.SessionCreationPolicy;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
+import org.springframework.security.web.authentication.logout.LogoutFilter;
+import org.springframework.web.filter.CorsFilter;
+
+/**
+ * spring security配置
+ *
+
+ */
+@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
+public class SecurityConfig extends WebSecurityConfigurerAdapter
+{
+    /**
+     * 自定义用户认证逻辑
+     */
+    @Autowired
+    private UserDetailsService userDetailsService;
+
+    /**
+     * 认证失败处理类
+     */
+    @Autowired
+    private AuthenticationEntryPointImpl unauthorizedHandler;
+
+    /**
+     * 退出处理类
+     */
+    @Autowired
+    private LogoutSuccessHandlerImpl logoutSuccessHandler;
+
+    /**
+     * token认证过滤器
+     */
+    @Autowired
+    private JwtAuthenticationTokenFilter authenticationTokenFilter;
+
+    /**
+     * 跨域过滤器
+     */
+    @Autowired
+    private CorsFilter corsFilter;
+
+    /**
+     * 解决 无法直接注入 AuthenticationManager
+     *
+     * @return
+     * @throws Exception
+     */
+    @Bean
+    @Override
+    public AuthenticationManager authenticationManagerBean() throws Exception
+    {
+        return super.authenticationManagerBean();
+    }
+
+    /**
+     * anyRequest          |   匹配所有请求路径
+     * access              |   SpringEl表达式结果为true时可以访问
+     * anonymous           |   匿名可以访问
+     * denyAll             |   用户不能访问
+     * fullyAuthenticated  |   用户完全认证可以访问(非remember-me下自动登录)
+     * hasAnyAuthority     |   如果有参数,参数表示权限,则其中任何一个权限可以访问
+     * hasAnyRole          |   如果有参数,参数表示角色,则其中任何一个角色可以访问
+     * hasAuthority        |   如果有参数,参数表示权限,则其权限可以访问
+     * hasIpAddress        |   如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问
+     * hasRole             |   如果有参数,参数表示角色,则其角色可以访问
+     * permitAll           |   用户可以任意访问
+     * rememberMe          |   允许通过remember-me登录的用户访问
+     * authenticated       |   用户登录后可访问
+     */
+    @Override
+    protected void configure(HttpSecurity httpSecurity) throws Exception
+    {
+        httpSecurity
+                // CSRF禁用,因为不使用session
+                .csrf().disable()
+                // 认证失败处理类
+                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
+                // 基于token,所以不需要session
+                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
+                // 过滤请求
+                .authorizeRequests()
+                // 对于登录login 注册register 验证码captchaImage 允许匿名访问
+                .antMatchers("/chat/upload/**","/login", "/register", "/captchaImage").anonymous()
+                .antMatchers(
+                        HttpMethod.GET,
+                        "/",
+                        "/*.html",
+                        "/**/*.html",
+                        "/**/*.css",
+                        "/**/*.js",
+                        "/profile/**"
+                ).permitAll()
+                .antMatchers("**/errorLogUpload").anonymous()
+                .antMatchers("**").anonymous()
+                .antMatchers("/qw/getJsapiTicket/**").anonymous()
+                .antMatchers("/msg/**").anonymous()
+                .antMatchers("/msg/**/**").anonymous()
+                .antMatchers("/msg").anonymous()
+                .antMatchers("/app/common/**").anonymous()
+                .antMatchers("/common/getId**").anonymous()
+                .antMatchers("/common/uploadOSS**").anonymous()
+                .antMatchers("/common/uploadWang**").anonymous()
+                .antMatchers("/common/download**").anonymous()
+                .antMatchers("/common/download/resource**").anonymous()
+                .antMatchers("/swagger-ui.html").anonymous()
+                .antMatchers("/swagger-resources/**").anonymous()
+                .antMatchers("/webjars/**").anonymous()
+                .antMatchers("/*/api-docs").anonymous()
+                .antMatchers("/druid/**").anonymous()
+                .antMatchers("/qw/data/**").anonymous()
+                // 除上面外的所有请求全部需要鉴权认证
+                .anyRequest().authenticated()
+                .and()
+                .headers().frameOptions().disable();
+        httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
+        // 添加JWT filter
+        httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
+        // 添加CORS filter
+        httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
+        httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
+    }
+
+    /**
+     * 强散列哈希加密实现
+     */
+    @Bean
+    public BCryptPasswordEncoder bCryptPasswordEncoder()
+    {
+        return new BCryptPasswordEncoder();
+    }
+
+    /**
+     * 身份认证接口
+     */
+    @Override
+    protected void configure(AuthenticationManagerBuilder auth) throws Exception
+    {
+        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
+    }
+}

+ 33 - 0
fs-app-task/src/main/java/com/fs/framework/config/ServerConfig.java

@@ -0,0 +1,33 @@
+package com.fs.framework.config;
+
+import com.fs.common.utils.ServletUtils;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 服务相关配置
+ *
+
+ */
+@Component
+public class ServerConfig
+{
+    /**
+     * 获取完整的请求路径,包括:域名,端口,上下文访问路径
+     *
+     * @return 服务地址
+     */
+    public String getUrl()
+    {
+        HttpServletRequest request = ServletUtils.getRequest();
+        return getDomain(request);
+    }
+
+    public static String getDomain(HttpServletRequest request)
+    {
+        StringBuffer url = request.getRequestURL();
+        String contextPath = request.getServletContext().getContextPath();
+        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
+    }
+}

+ 120 - 0
fs-app-task/src/main/java/com/fs/framework/config/SwaggerConfig.java

@@ -0,0 +1,120 @@
+package com.fs.framework.config;
+
+import com.fs.common.config.FSConfig;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+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 java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Swagger2的接口配置
+ * 
+
+ */
+@Configuration
+public class SwaggerConfig
+{
+    /** 系统基础配置 */
+    @Autowired
+    private FSConfig fsConfig;
+
+    /** 是否开启swagger */
+    @Value("${swagger.enabled}")
+    private boolean enabled;
+
+    /** 设置请求的统一前缀 */
+    @Value("${swagger.pathMapping}")
+    private String pathMapping;
+
+    /**
+     * 创建API
+     */
+    @Bean
+    public Docket createRestApi()
+    {
+        return new Docket(DocumentationType.SWAGGER_2)
+                // 是否启用Swagger
+                .enable(enabled)
+                // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
+                .apiInfo(apiInfo())
+                // 设置哪些接口暴露给Swagger展示
+                .select()
+                // 扫描所有有注解的api,用这种方式更灵活
+                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
+                // 扫描指定包中的swagger注解
+                // .apis(RequestHandlerSelectors.basePackage("com.fs.project.tool.swagger"))
+                // 扫描所有 .apis(RequestHandlerSelectors.any())
+                .paths(PathSelectors.any())
+                .build()
+                /* 设置安全模式,swagger可以设置访问token */
+                .securitySchemes(securitySchemes())
+                .securityContexts(securityContexts())
+                .pathMapping(pathMapping);
+    }
+
+    /**
+     * 安全模式,这里指定token通过Authorization头请求头传递
+     */
+    private List<ApiKey> securitySchemes()
+    {
+        List<ApiKey> apiKeyList = new ArrayList<ApiKey>();
+        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
+        return apiKeyList;
+    }
+
+    /**
+     * 安全上下文
+     */
+    private List<SecurityContext> securityContexts()
+    {
+        List<SecurityContext> securityContexts = new ArrayList<>();
+        securityContexts.add(
+                SecurityContext.builder()
+                        .securityReferences(defaultAuth())
+                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
+                        .build());
+        return securityContexts;
+    }
+
+    /**
+     * 默认的安全上引用
+     */
+    private List<SecurityReference> defaultAuth()
+    {
+        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
+        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
+        authorizationScopes[0] = authorizationScope;
+        List<SecurityReference> securityReferences = new ArrayList<>();
+        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
+        return securityReferences;
+    }
+
+    /**
+     * 添加摘要信息
+     */
+    private ApiInfo apiInfo()
+    {
+        // 用ApiInfoBuilder进行定制
+        return new ApiInfoBuilder()
+                // 设置标题
+                .title("标题:FS管理系统_接口文档")
+                // 描述
+                .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
+                // 作者信息
+                .contact(new Contact(fsConfig.getName(), null, null))
+                // 版本
+                .version("版本号:" + fsConfig.getVersion())
+                .build();
+    }
+}

+ 130 - 0
fs-app-task/src/main/java/com/fs/framework/config/ThreadPoolConfig.java

@@ -0,0 +1,130 @@
+package com.fs.framework.config;
+
+import com.fs.common.utils.Threads;
+import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.TaskScheduler;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * 线程池配置
+ *
+
+ **/
+@Configuration
+@EnableAsync
+public class ThreadPoolConfig
+{
+    // 核心线程池大小
+    private int corePoolSize = 50;
+
+    // 最大可创建的线程数
+    private int maxPoolSize = 300;
+
+    // 队列最大长度
+    private int queueCapacity = 1000;
+
+    // 线程池维护线程所允许的空闲时间
+    private int keepAliveSeconds = 300;
+
+
+    @Bean
+    public TaskScheduler taskScheduler(){
+        ThreadPoolTaskScheduler scheduler=new ThreadPoolTaskScheduler();
+        scheduler.setPoolSize(18);
+        scheduler.setThreadNamePrefix("app-scheduled-task-");
+        scheduler.setAwaitTerminationSeconds(60);
+        scheduler.setWaitForTasksToCompleteOnShutdown(true);
+        return scheduler;
+    }
+
+    @Bean(name = "threadPoolTaskExecutor")
+    public ThreadPoolTaskExecutor threadPoolTaskExecutor()
+    {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setMaxPoolSize(maxPoolSize);
+        executor.setCorePoolSize(corePoolSize);
+        executor.setQueueCapacity(queueCapacity);
+        executor.setKeepAliveSeconds(keepAliveSeconds);
+        // 线程池对拒绝任务(无线程可用)的处理策略
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+
+
+
+        return executor;
+    }
+
+    /**
+     * 执行周期性或定时任务
+     */
+    @Bean(name = "scheduledExecutorService")
+    protected ScheduledExecutorService scheduledExecutorService()
+    {
+        return new ScheduledThreadPoolExecutor(corePoolSize,
+                new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build())
+        {
+            @Override
+            protected void afterExecute(Runnable r, Throwable t)
+            {
+                super.afterExecute(r, t);
+                Threads.printException(r, t);
+            }
+        };
+    }
+
+    @Bean(name = "sopTaskExecutor")
+    public Executor sopTaskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(16); // 根据需求调整
+        executor.setMaxPoolSize(32);  // 根据需求调整
+        executor.setQueueCapacity(800); // 根据需求调整
+        executor.setThreadNamePrefix("SopTask-");
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+
+    @Bean(name = "sopChatTaskExecutor")
+    public Executor batchInsertExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(16);
+        executor.setMaxPoolSize(32);
+        executor.setQueueCapacity(800);
+        executor.setThreadNamePrefix("BatchInsert-");
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+
+    @Bean(name = "appSopTaskExecutor")
+    public Executor appSopTaskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(16); // 根据需求调整
+        executor.setMaxPoolSize(32);  // 根据需求调整
+        executor.setQueueCapacity(800); // 根据需求调整
+        executor.setThreadNamePrefix("AppSopTask-");
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+
+    @Bean(name = "sopRatingExecutor")
+    public Executor sopRatingExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(16); // 根据需求调整
+        executor.setMaxPoolSize(32);  // 根据需求调整
+        executor.setQueueCapacity(800); // 根据需求调整
+        executor.setThreadNamePrefix("SopRating-");
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+}

+ 77 - 0
fs-app-task/src/main/java/com/fs/framework/config/properties/DruidProperties.java

@@ -0,0 +1,77 @@
+package com.fs.framework.config.properties;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * druid 配置属性
+ *
+
+ */
+@Configuration
+public class DruidProperties
+{
+    @Value("${spring.datasource.mysql.druid.initialSize}")
+    private int initialSize;
+
+    @Value("${spring.datasource.mysql.druid.minIdle}")
+    private int minIdle;
+
+    @Value("${spring.datasource.mysql.druid.maxActive}")
+    private int maxActive;
+
+    @Value("${spring.datasource.mysql.druid.maxWait}")
+    private int maxWait;
+
+    @Value("${spring.datasource.mysql.druid.timeBetweenEvictionRunsMillis}")
+    private int timeBetweenEvictionRunsMillis;
+
+    @Value("${spring.datasource.mysql.druid.minEvictableIdleTimeMillis}")
+    private int minEvictableIdleTimeMillis;
+
+    @Value("${spring.datasource.mysql.druid.maxEvictableIdleTimeMillis}")
+    private int maxEvictableIdleTimeMillis;
+
+    @Value("${spring.datasource.mysql.druid.validationQuery}")
+    private String validationQuery;
+
+    @Value("${spring.datasource.mysql.druid.testWhileIdle}")
+    private boolean testWhileIdle;
+
+    @Value("${spring.datasource.mysql.druid.testOnBorrow}")
+    private boolean testOnBorrow;
+
+    @Value("${spring.datasource.mysql.druid.testOnReturn}")
+    private boolean testOnReturn;
+
+    public DruidDataSource dataSource(DruidDataSource datasource)
+    {
+        /** 配置初始化大小、最小、最大 */
+        datasource.setInitialSize(initialSize);
+        datasource.setMaxActive(maxActive);
+        datasource.setMinIdle(minIdle);
+
+        /** 配置获取连接等待超时的时间 */
+        datasource.setMaxWait(maxWait);
+
+        /** 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 */
+        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
+
+        /** 配置一个连接在池中最小、最大生存的时间,单位是毫秒 */
+        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
+        datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis);
+
+        /**
+         * 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
+         */
+        datasource.setValidationQuery(validationQuery);
+        /** 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 */
+        datasource.setTestWhileIdle(testWhileIdle);
+        /** 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */
+        datasource.setTestOnBorrow(testOnBorrow);
+        /** 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */
+        datasource.setTestOnReturn(testOnReturn);
+        return datasource;
+    }
+}

+ 27 - 0
fs-app-task/src/main/java/com/fs/framework/datasource/DynamicDataSource.java

@@ -0,0 +1,27 @@
+package com.fs.framework.datasource;
+
+import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
+
+import javax.sql.DataSource;
+import java.util.Map;
+
+/**
+ * 动态数据源
+ *
+
+ */
+public class DynamicDataSource extends AbstractRoutingDataSource
+{
+    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources)
+    {
+        super.setDefaultTargetDataSource(defaultTargetDataSource);
+        super.setTargetDataSources(targetDataSources);
+        super.afterPropertiesSet();
+    }
+
+    @Override
+    protected Object determineCurrentLookupKey()
+    {
+        return DynamicDataSourceContextHolder.getDataSourceType();
+    }
+}

+ 45 - 0
fs-app-task/src/main/java/com/fs/framework/datasource/DynamicDataSourceContextHolder.java

@@ -0,0 +1,45 @@
+package com.fs.framework.datasource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 数据源切换处理
+ *
+
+ */
+public class DynamicDataSourceContextHolder
+{
+    public static final Logger log = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
+
+    /**
+     * 使用ThreadLocal维护变量,ThreadLocal为每个使用该变量的线程提供独立的变量副本,
+     *  所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。
+     */
+    private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
+
+    /**
+     * 设置数据源的变量
+     */
+    public static void setDataSourceType(String dsType)
+    {
+//        log.info("切换到{}数据源", dsType);
+        CONTEXT_HOLDER.set(dsType);
+    }
+
+    /**
+     * 获得数据源的变量
+     */
+    public static String getDataSourceType()
+    {
+        return CONTEXT_HOLDER.get();
+    }
+
+    /**
+     * 清空数据源变量
+     */
+    public static void clearDataSourceType()
+    {
+        CONTEXT_HOLDER.remove();
+    }
+}

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä