From a801724b75d4ec700f810880fcaad3657ea0161a Mon Sep 17 00:00:00 2001 From: "zhouj1203@hotmail.com" Date: Wed, 7 Aug 2024 11:48:50 +0800 Subject: [PATCH] =?UTF-8?q?redis=20=20=E8=AF=BE=E5=A0=82=E7=BA=AA=E5=BE=8B?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 30 +++++- .../cn/teammodel/config/ies/IESConfig.java | 13 +++ .../teammodel/config/redis/RedisConfig.java | 34 ++++++ .../admin/controller/AdminDutyController.java | 12 +++ .../admin/controller/RedisController.java | 26 +++++ .../admin/service/AdminIndexDutyService.java | 7 ++ .../impl/AdminIndexDutyServiceImpl.java | 73 +++++++++++++ .../model/dto/weekDuty/LessonRecordDto.java | 15 +++ .../java/cn/teammodel/test/ConfigCheck.java | 21 ++++ .../java/cn/teammodel/test/RedisService.java | 30 ++++++ src/main/resources/application.yml | 100 ++++++++++-------- 11 files changed, 317 insertions(+), 44 deletions(-) create mode 100644 src/main/java/cn/teammodel/config/ies/IESConfig.java create mode 100644 src/main/java/cn/teammodel/config/redis/RedisConfig.java create mode 100644 src/main/java/cn/teammodel/controller/admin/controller/RedisController.java create mode 100644 src/main/java/cn/teammodel/model/dto/weekDuty/LessonRecordDto.java create mode 100644 src/main/java/cn/teammodel/test/ConfigCheck.java create mode 100644 src/main/java/cn/teammodel/test/RedisService.java diff --git a/pom.xml b/pom.xml index 2451f1b..1835db2 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,18 @@ 3.14.9 + + org.slf4j + slf4j-api + 1.7.36 + + + + ch.qos.logback + logback-classic + 1.2.11 + + @@ -106,10 +118,20 @@ - + + + io.jsonwebtoken + jjwt + 0.9.1 + + + + org.springframework.boot + spring-boot-starter-data-redis @@ -131,6 +153,12 @@ RELEASE compile + + junit + junit + + test + diff --git a/src/main/java/cn/teammodel/config/ies/IESConfig.java b/src/main/java/cn/teammodel/config/ies/IESConfig.java new file mode 100644 index 0000000..0ce5b64 --- /dev/null +++ b/src/main/java/cn/teammodel/config/ies/IESConfig.java @@ -0,0 +1,13 @@ +package cn.teammodel.config.ies; + +import lombok.Getter; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +@Getter +@Configuration +public class IESConfig { + @Value("${ies.server-url}") + private String serverUrl; + +} diff --git a/src/main/java/cn/teammodel/config/redis/RedisConfig.java b/src/main/java/cn/teammodel/config/redis/RedisConfig.java new file mode 100644 index 0000000..1ef6791 --- /dev/null +++ b/src/main/java/cn/teammodel/config/redis/RedisConfig.java @@ -0,0 +1,34 @@ +package cn.teammodel.config.redis; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +public class RedisConfig { + + + + @Bean + public LettuceConnectionFactory dbConnectionFactory() { + RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("52.130.252.100", 6379); + config.setDatabase(8); // 设置数据库编号为8 + config.setPassword("habook"); + return new LettuceConnectionFactory(config); + } + + + @Bean + public StringRedisTemplate db1Template(LettuceConnectionFactory dbConnectionFactory) { + StringRedisTemplate template = new StringRedisTemplate(); + template.setConnectionFactory(dbConnectionFactory); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + return template; + } +} diff --git a/src/main/java/cn/teammodel/controller/admin/controller/AdminDutyController.java b/src/main/java/cn/teammodel/controller/admin/controller/AdminDutyController.java index dda8765..7a1ce27 100644 --- a/src/main/java/cn/teammodel/controller/admin/controller/AdminDutyController.java +++ b/src/main/java/cn/teammodel/controller/admin/controller/AdminDutyController.java @@ -4,19 +4,25 @@ import cn.teammodel.common.R; import cn.teammodel.controller.admin.service.AdminIndexDutyService; import cn.teammodel.model.dto.admin.appraise.TimeRangeDto; import cn.teammodel.model.dto.admin.weekduty.AdminFindDutyRecordDto; +import cn.teammodel.model.dto.weekDuty.LessonRecordDto; import cn.teammodel.model.vo.admin.AppraiseNodeRankVo; import cn.teammodel.model.vo.admin.DutyIndexData; import cn.teammodel.model.vo.admin.DutyNodeRankVo; import cn.teammodel.model.vo.admin.DutyRankPo; import cn.teammodel.model.vo.weekDuty.DutyRecordVo; import cn.teammodel.service.DutyService; +import com.google.gson.JsonElement; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import java.util.List; +import java.util.Map; /** * @author winter @@ -65,4 +71,10 @@ public class AdminDutyController { List res = adminIndexDutyService.appraiseNodeRank(timeRangeDto); return R.success(res); } + @PostMapping("getLessonRecord") + @ApiOperation("获取课堂记录") + public R > appraiseNodeRank(@Valid @RequestBody Map map , HttpServletRequest request) { + Map res = adminIndexDutyService.getLessonRecord(map,request); + return R.success(res); + } } diff --git a/src/main/java/cn/teammodel/controller/admin/controller/RedisController.java b/src/main/java/cn/teammodel/controller/admin/controller/RedisController.java new file mode 100644 index 0000000..36e79fb --- /dev/null +++ b/src/main/java/cn/teammodel/controller/admin/controller/RedisController.java @@ -0,0 +1,26 @@ +package cn.teammodel.controller.admin.controller; + +import cn.teammodel.common.R; +import cn.teammodel.test.RedisService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.Map; + +@RestController +@RequestMapping("/test") +public class RedisController { + + private final RedisService redisService; + + @Autowired + public RedisController(RedisService redisService) { + this.redisService = redisService; + } + @PostMapping("/redis") + public Map getValueByKey(@RequestBody @Valid String key) { + Map juri = redisService.getValueByKey(key); + return R.success(juri).getData(); + } +} diff --git a/src/main/java/cn/teammodel/controller/admin/service/AdminIndexDutyService.java b/src/main/java/cn/teammodel/controller/admin/service/AdminIndexDutyService.java index dd1eaf2..f5721bc 100644 --- a/src/main/java/cn/teammodel/controller/admin/service/AdminIndexDutyService.java +++ b/src/main/java/cn/teammodel/controller/admin/service/AdminIndexDutyService.java @@ -1,11 +1,15 @@ package cn.teammodel.controller.admin.service; import cn.teammodel.model.dto.admin.appraise.TimeRangeDto; +import cn.teammodel.model.dto.weekDuty.LessonRecordDto; import cn.teammodel.model.vo.admin.DutyIndexData; import cn.teammodel.model.vo.admin.DutyNodeRankVo; import cn.teammodel.model.vo.admin.DutyRankPo; +import com.google.gson.JsonElement; +import javax.servlet.http.HttpServletRequest; import java.util.List; +import java.util.Map; /** * @author winter @@ -19,4 +23,7 @@ public interface AdminIndexDutyService { List teacherRank(TimeRangeDto timeRangeDto); List appraiseNodeRank(TimeRangeDto timeRangeDto); + + Map getLessonRecord (Map map, HttpServletRequest request); + } diff --git a/src/main/java/cn/teammodel/controller/admin/service/impl/AdminIndexDutyServiceImpl.java b/src/main/java/cn/teammodel/controller/admin/service/impl/AdminIndexDutyServiceImpl.java index f283abe..29e98af 100644 --- a/src/main/java/cn/teammodel/controller/admin/service/impl/AdminIndexDutyServiceImpl.java +++ b/src/main/java/cn/teammodel/controller/admin/service/impl/AdminIndexDutyServiceImpl.java @@ -3,8 +3,10 @@ package cn.teammodel.controller.admin.service.impl; import cn.teammodel.common.ErrorCode; import cn.teammodel.common.PK; import cn.teammodel.config.exception.ServiceException; +import cn.teammodel.config.ies.IESConfig; import cn.teammodel.controller.admin.service.AdminIndexDutyService; import cn.teammodel.model.dto.admin.appraise.TimeRangeDto; +import cn.teammodel.model.dto.weekDuty.LessonRecordDto; import cn.teammodel.model.entity.User; import cn.teammodel.model.entity.school.ClassInfo; import cn.teammodel.model.entity.school.School; @@ -21,12 +23,31 @@ import cn.teammodel.repository.TeacherRepository; import cn.teammodel.security.utils.SecurityUtil; import cn.teammodel.utils.SchoolDateUtil; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import lombok.var; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.*; @@ -39,6 +60,7 @@ import static cn.teammodel.utils.SchoolDateUtil.calculateWeekNum; * @create 2024-02-28 15:07 */ @Service +@Slf4j public class AdminIndexDutyServiceImpl implements AdminIndexDutyService { @Resource @@ -52,6 +74,13 @@ public class AdminIndexDutyServiceImpl implements AdminIndexDutyService { @Resource private DutyRecordRepository dutyRecordRepository; + private final Environment env; + + @Autowired + public AdminIndexDutyServiceImpl(Environment env) { + this.env = env; + } + @Override public DutyIndexData getIndexData(String periodId) { User loginUser = SecurityUtil.getLoginUser(); @@ -193,4 +222,48 @@ public class AdminIndexDutyServiceImpl implements AdminIndexDutyService { } + + @Override + public Map getLessonRecord(Map map, HttpServletRequest request) { + + Map mapper = new HashMap<>(); + String apiUrl = env.getProperty("ies.server-url"); + + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + // 创建HttpPost对象 + HttpPost httpPost = new HttpPost(apiUrl); + + // 添加请求头 + httpPost.setHeader("Authorization", request.getHeader("Authorization")); + httpPost.setHeader("X-Auth-Authtoken", request.getHeader("X-Auth-Authtoken")); + httpPost.setHeader("Accept", "application/json"); + httpPost.setHeader("Content-Type", "application/json; charset=utf-8"); + + // 设置请求体 + //String requestBody = String.format("{\"school\":\"%s\"}","{\"name\":\"%s\"}","{\"periodId\":\"%s\"}", code,name,periodId); + httpPost.setEntity(new StringEntity(new Gson().toJson(map))); + + // 发送请求 + try (CloseableHttpResponse response = httpClient.execute(httpPost)) { + // 获取响应实体 + HttpEntity entity = response.getEntity(); + + if (entity != null) { + // 解析响应内容 + String jsonString = EntityUtils.toString(entity); + ObjectMapper objectMapper = new ObjectMapper(); + mapper = objectMapper.readValue(jsonString, TypeFactory.defaultInstance().constructMapType(Map.class, String.class, Object.class)); + } + + // 检查响应状态码 + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != 200) throw new RuntimeException("Failed : HTTP error code : " + statusCode); + } catch (IOException e) { + log.error("Failed : HTTP error code : {}", e.getMessage()); + } + } catch (IOException e) { + log.error("Failed : HTTP error code : {}", e.getMessage()); + } + return mapper; + } } diff --git a/src/main/java/cn/teammodel/model/dto/weekDuty/LessonRecordDto.java b/src/main/java/cn/teammodel/model/dto/weekDuty/LessonRecordDto.java new file mode 100644 index 0000000..fd7e336 --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/weekDuty/LessonRecordDto.java @@ -0,0 +1,15 @@ +package cn.teammodel.model.dto.weekDuty; + +import lombok.Data; + +@Data +public class LessonRecordDto { + private String code; + private String name; + private String periodId; + private Long startTime; + private Long endTime; + private String Authorization; + private String AuthToken; + private String continuationToken; +} diff --git a/src/main/java/cn/teammodel/test/ConfigCheck.java b/src/main/java/cn/teammodel/test/ConfigCheck.java new file mode 100644 index 0000000..eb59d18 --- /dev/null +++ b/src/main/java/cn/teammodel/test/ConfigCheck.java @@ -0,0 +1,21 @@ +package cn.teammodel.test; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +/*@Component +public class ConfigCheck implements CommandLineRunner { + + @Value("${spring.redis.host}") + private String redisHost; + + @Value("${spring.redis.port}") + private int redisPort; + + @Override + public void run(String... args) throws Exception { + System.out.println("Redis Host: " + redisHost); + System.out.println("Redis Port: " + redisPort); + } +}*/ diff --git a/src/main/java/cn/teammodel/test/RedisService.java b/src/main/java/cn/teammodel/test/RedisService.java new file mode 100644 index 0000000..7af4068 --- /dev/null +++ b/src/main/java/cn/teammodel/test/RedisService.java @@ -0,0 +1,30 @@ +package cn.teammodel.test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.Map; + +@Service +public class RedisService { + + private final StringRedisTemplate redisTemplate; + + + @Autowired + public RedisService(StringRedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + public void setKeyValue(String key, String value) { + //ruredisTemplate.slaveOf(); + redisTemplate.opsForValue().set(key, value); + } + + public Map getValueByKey(String key) { + System.out.println(redisTemplate.opsForHash().values("ArtArea:area:primary")); + return redisTemplate.opsForHash().entries("ArtArea:area:junior"); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index fe0b1d2..2a3fe23 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,43 +1,57 @@ -spring: - env: dev - mvc: - pathmatch: - matching-strategy: ant_path_matcher - - # 文件上传 - servlet: - multipart: - # 大小限制 - max-file-size: 10MB - - cloud: - azure: - cosmos: - endpoint: https://cdhabookdep-free.documents.azure.cn:443 - database: TEAMModelOS - key: JTUVk92Gjsx17L0xqxn0X4wX2thDPMKiw4daeTyV1HzPb6JmBeHdtFY1MF1jdctW1ofgzqkDMFOtcqS46by31A== - populate-query-metrics: true - - security: - oauth2: - resourceserver: - jwt: - issuer-uri: https://login.partner.microsoftonline.cn/4807e9cf-87b8-4174-aa5b-e76497d7392b/v2.0 - audiences: 72643704-b2e7-4b26-b881-bd5865e7a7a5 - -spark: - gpt: - endpoint: https://spark-api.xf-yun.com/v3.1/chat - appId: c49d1e24 - apiKey: 6c586e7dd1721ed1bb19bdb573b4ad34 - apiSecret: MDU1MTU1Nzg4MDg2ZTJjZWU3MmI4ZGU1 - cache_timeout: 1800000 # 30min - cache_context: 3 - - -jwt: - secret: fXO6ko/qyXeYrkecPeKdgXnuLXf9vMEtnBC9OB3s+aA= - -# 钉钉 webhook -ding: - server-url: https://oapi.dingtalk.com/robot/send?access_token=32d9b24f69c2c4fd7c2dab43268b6258a7214d2620e0805d7b6d1429003b64b6 \ No newline at end of file +spring: +# redis: +# dabase: 9 +# host: 52.130.252.100 +# port: 6379 +# password: habook +# ssl: false +# abort_connection: false +# write_timeout: 10240 + + + env: dev + mvc: + pathmatch: + matching-strategy: ant_path_matcher + + # 文件上传 + servlet: + multipart: + # 大小限制 + max-file-size: 10MB + + cloud: + azure: + cosmos: + endpoint: https://cdhabookdep-free.documents.azure.cn:443 + database: TEAMModelOS + key: JTUVk92Gjsx17L0xqxn0X4wX2thDPMKiw4daeTyV1HzPb6JmBeHdtFY1MF1jdctW1ofgzqkDMFOtcqS46by31A== + populate-query-metrics: true + + security: + oauth2: + resourceserver: + jwt: + issuer-uri: https://login.partner.microsoftonline.cn/4807e9cf-87b8-4174-aa5b-e76497d7392b/v2.0 + audiences: 72643704-b2e7-4b26-b881-bd5865e7a7a5 + +spark: + gpt: + endpoint: https://spark-api.xf-yun.com/v3.1/chat + appId: c49d1e24 + apiKey: 6c586e7dd1721ed1bb19bdb573b4ad34 + apiSecret: MDU1MTU1Nzg4MDg2ZTJjZWU3MmI4ZGU1 + cache_timeout: 1800000 # 30min + cache_context: 3 + + +jwt: + secret: fXO6ko/qyXeYrkecPeKdgXnuLXf9vMEtnBC9OB3s+aA= + + +# 钉钉 webhook +ding: + server-url: https://oapi.dingtalk.com/robot/send?access_token=32d9b24f69c2c4fd7c2dab43268b6258a7214d2620e0805d7b6d1429003b64b6 +#IES 课堂记录 +ies: + server-url: https://www.teammodel.cn/common/lesson-record/get-lesson-record \ No newline at end of file