From 464f4ba7c8f2c367c715f96da1a86e5e7c740748 Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Sat, 4 Jan 2025 17:49:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=86=E9=A2=91=20=E6=9F=A5=E7=9C=8B?= =?UTF-8?q?=E9=9B=86=20=E5=92=8C=20=E9=9B=86=E6=95=B0=E5=88=97=E8=A1=A8=20?= =?UTF-8?q?=E6=8B=86=E5=88=862=20=E9=85=8D=E7=BD=AE=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sqx/config/ShardingConfig.java | 20 ------- .../modules/course/dao/CourseDetailsDao.java | 11 ++-- .../impl/CourseDetailsServiceImpl.java | 59 ++++++++++++++----- .../service/impl/CourseServiceImpl.java | 2 + .../modules/course/vo/CourseDetailsSetVo.java | 2 + .../modules/course/vo/CourseDetailsVo.java | 3 +- src/main/resources/application.yml | 26 ++------ src/main/resources/logback.xml | 46 +++++++++++++++ .../mapper/course/CourseDetailsDao.xml | 18 +++--- 9 files changed, 118 insertions(+), 69 deletions(-) create mode 100644 src/main/resources/logback.xml diff --git a/src/main/java/com/sqx/config/ShardingConfig.java b/src/main/java/com/sqx/config/ShardingConfig.java index 298f5ce6..7cc0f0b7 100644 --- a/src/main/java/com/sqx/config/ShardingConfig.java +++ b/src/main/java/com/sqx/config/ShardingConfig.java @@ -153,24 +153,6 @@ public class ShardingConfig { for (String regionTable : regionTables) { TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration(regionTable, String.format(regionTablesDataNode, regionTable)); tableRuleConfig.setDatabaseShardingStrategyConfig(databaseShardingStrategyConfig); -// // 设置区域表使用雪花算法生成主键 -// switch (regionTable){ -// case "orders": -// tableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "orders_id")); -// break; -//// case "course_collect": -//// tableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "course_collect_id")); -//// break; -// case "course_user": -// tableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "course_user_id")); -// break; -// case "tb_user": -// tableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "user_id")); -// break; -// default: -// tableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "id")); -// break; -// } sets.add(tableRuleConfig); } @@ -178,8 +160,6 @@ public class ShardingConfig { // 定义区域表的分库规则 StandardShardingStrategyConfiguration courseDetailsShardingStrategyConfig = new StandardShardingStrategyConfiguration( courseDetailsShardingDatabaseColumn, new StandardShardingStrategyConf()); -// InlineShardingStrategyConfiguration courseDetailsShardingStrategyConfig = new InlineShardingStrategyConfiguration( -// courseDetailsShardingDatabaseColumn, courseDetailsShardingDatabaseAlgorithm); for (String regionTable : courseDetails) { TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration(regionTable, String.format(regionTablesDataNode, regionTable)); tableRuleConfig.setDatabaseShardingStrategyConfig(courseDetailsShardingStrategyConfig); diff --git a/src/main/java/com/sqx/modules/course/dao/CourseDetailsDao.java b/src/main/java/com/sqx/modules/course/dao/CourseDetailsDao.java index b65b5775..861a8741 100644 --- a/src/main/java/com/sqx/modules/course/dao/CourseDetailsDao.java +++ b/src/main/java/com/sqx/modules/course/dao/CourseDetailsDao.java @@ -6,14 +6,15 @@ import com.sqx.modules.course.vo.CourseDetailsSetVo; import com.sqx.modules.course.vo.CourseDetailsVo; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Select; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.Cacheable; import java.math.BigDecimal; import java.util.List; import java.util.Map; -import java.util.Set; @Mapper +@CacheConfig public interface CourseDetailsDao extends BaseMapper { /** * @param courseId 剧的id @@ -21,9 +22,10 @@ public interface CourseDetailsDao extends BaseMapper { * @param price 全剧价格 * @param wholesalePrice 10集价格 */ - List courseSets(@Param("courseId") Long courseId, boolean isPrice, BigDecimal price, BigDecimal wholesalePrice); + @Cacheable(cacheNames = "courseSets" ,key = "#courseId") + List courseSets(@Param("courseId") Long courseId, Integer isPrice, BigDecimal price, BigDecimal wholesalePrice); - List courseDetails(@Param("courseId") Long courseId, @Param("collect") Integer collect, + List courseDetails(@Param("courseId") Long courseId, @Param("collect") Long collect, @Param("starSort") Integer starSort, @Param("endSort") Integer endSort); List findByCourseId(@Param("id") Long id, @Param("userId") Long userId); @@ -42,6 +44,7 @@ public interface CourseDetailsDao extends BaseMapper { */ Map countCourse(Long courseId); + @Cacheable(cacheNames = "courseCount" ,key = "#courseId") Integer countCourseByCourseId(Long courseId); List countByCourseId(); diff --git a/src/main/java/com/sqx/modules/course/service/impl/CourseDetailsServiceImpl.java b/src/main/java/com/sqx/modules/course/service/impl/CourseDetailsServiceImpl.java index f3da39a5..da447a24 100644 --- a/src/main/java/com/sqx/modules/course/service/impl/CourseDetailsServiceImpl.java +++ b/src/main/java/com/sqx/modules/course/service/impl/CourseDetailsServiceImpl.java @@ -46,15 +46,13 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.math.BigDecimal; -import java.util.Date; -import java.util.List; -import java.util.Random; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; @Service @@ -84,6 +82,7 @@ public class CourseDetailsServiceImpl extends ServiceImpl courseDetailsSetVos; if (freeWatch || courseUser != null) { - courseDetailsSetVos = baseMapper.courseSets(courseId, false, null, null); + courseDetailsSetVos = baseMapper.courseSets(courseId, 2, null, null); } else { - courseDetailsSetVos = baseMapper.courseSets(courseId, true, bean.getPrice(), bean.getWholesalePrice()); +// courseDetailsSetVos = baseMapper.courseSets(courseId, 1, bean.getPrice(), bean.getWholesalePrice()); + courseDetailsSetVos = baseMapper.courseSets(courseId, 2, bean.getPrice(), bean.getWholesalePrice()); //查询用户是否单独购买了集 Set detailsId = courseUserDao.selectUserCourseDetailsId(courseId, userId); + if (courseCollect != null) { + for (CourseDetailsSetVo s : courseDetailsSetVos) { + if (s.getCourseDetailsId().equals(courseCollect.getCourseDetailsId())) { + s.setCurrent(1); + break; + } + } + } else { + courseDetailsSetVos.get(0).setCurrent(1); + } if (CollectionUtil.isNotEmpty(detailsId)) { courseDetailsSetVos = courseDetailsSetVos.stream() .peek(s -> { - if (s.getCourseDetailsId().equals(courseCollect.getCourseDetailsId())) { - s.setCurrent(1); - } if (detailsId.contains(s.getCourseDetailsId())) { s.setIsPrice(2); } }) - .filter(s -> s.getCurrent() == 1 || s.getIsPrice() == 2) .collect(Collectors.toList()); } } - return new Result().put("data", courseDetailsSetVos); + Map map = new HashMap<>(); + map.put("title", bean.getTitle()); + map.put("list", courseDetailsSetVos); + return new Result().put("data", map); } @Override @@ -198,9 +208,11 @@ public class CourseDetailsServiceImpl extends ServiceImpl().eq("course_id", courseId) .eq("user_id", userId).eq("classify", 3).last("limit 1")); - CourseDetails courseDetails = baseMapper.selectOne(new QueryWrapper() - .eq("course_details_id", courseCollect.getCourseDetailsId()).eq("course_id", courseCollect.getCourseId()).last("limit 1")); - sort = courseDetails.getSort(); + if (courseCollect != null) { + CourseDetails courseDetails = baseMapper.selectOne(new QueryWrapper() + .eq("course_details_id", courseCollect.getCourseDetailsId()).eq("course_id", courseCollect.getCourseId()).last("limit 1")); + sort = courseDetails.getSort(); + } } if (sort != null && sort > 2) { startSort = sort - 3; @@ -210,11 +222,28 @@ public class CourseDetailsServiceImpl extends ServiceImpl() .eq("user_id", userId).eq("course_id", courseId) .eq("classify", 1).last("limit 1")); - List courseDetailsVos = baseMapper.courseDetails(courseId, collect, startSort, endSort); + Set detailsId = new HashSet<>(); + //全剧免费 + if ((!freeWatch)) { + detailsId = courseUserDao.selectUserCourseDetailsId(courseId, userId); + } + List courseDetailsVos = baseMapper.courseDetails(courseId, collect == null ? 0L : 1L, startSort, endSort); + Set finalDetailsId = detailsId; courseDetailsVos.stream().forEach(s -> { + // 不免费 3集以后 (已买的不为空 并不在已买的包含) +// if (!freeWatch && s.getSort() > 3 && (CollectionUtil.isEmpty(finalDetailsId) || !finalDetailsId.contains(s.getCourseDetailsId()))) { +// s.setVideoUrl(null); +// } CourseCollect isGood = courseCollectDao.selectOne(new QueryWrapper() .eq("user_id", userId).eq("course_details_id", s.getCourseDetailsId()).eq("classify", 2).last("limit 1")); s.setIsGood(isGood == null ? 0 : 1); diff --git a/src/main/java/com/sqx/modules/course/service/impl/CourseServiceImpl.java b/src/main/java/com/sqx/modules/course/service/impl/CourseServiceImpl.java index d1fd2637..01efa8b7 100644 --- a/src/main/java/com/sqx/modules/course/service/impl/CourseServiceImpl.java +++ b/src/main/java/com/sqx/modules/course/service/impl/CourseServiceImpl.java @@ -49,6 +49,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -149,6 +150,7 @@ public class CourseServiceImpl extends ServiceImpl implements } @Override + @CacheEvict(cacheNames = {"courseSets", "courseCount"}, key = "#course.courseId") public Result updateCourse(Course course) { baseMapper.updateById(course); return Result.success("操作成功!"); diff --git a/src/main/java/com/sqx/modules/course/vo/CourseDetailsSetVo.java b/src/main/java/com/sqx/modules/course/vo/CourseDetailsSetVo.java index 5f923ded..bc2534c1 100644 --- a/src/main/java/com/sqx/modules/course/vo/CourseDetailsSetVo.java +++ b/src/main/java/com/sqx/modules/course/vo/CourseDetailsSetVo.java @@ -13,4 +13,6 @@ public class CourseDetailsSetVo { private Integer isPrice; private BigDecimal price; private BigDecimal wholesalePrice; + private BigDecimal countPrice; + private Integer sort; } diff --git a/src/main/java/com/sqx/modules/course/vo/CourseDetailsVo.java b/src/main/java/com/sqx/modules/course/vo/CourseDetailsVo.java index 5d506d75..1c5e9f9d 100644 --- a/src/main/java/com/sqx/modules/course/vo/CourseDetailsVo.java +++ b/src/main/java/com/sqx/modules/course/vo/CourseDetailsVo.java @@ -22,9 +22,10 @@ public class CourseDetailsVo { /** * 是否已追 */ - private Integer isCollect; + private Long isCollect; /** * 是否点赞 */ private Integer isGood; + private Integer sort; } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ebdfe071..6f9525be 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,9 @@ +#logging: +# file: +# name: logs/duanju.log +# 日志配置 logging: - file: - name: logs/duanju.log + config: classpath:logback.xml # Tomcat server: @@ -27,25 +30,6 @@ pagehelper: supportMethodsArguments: true params: count: countSql -#mybatis-plus: -# mapper-locations: classpath*:/mapper/**/*.xml -# #实体扫描,多个package用逗号或者分号分隔 -# typeAliasesPackage: com.sqx.modules.*.entity -# global-config: -# #数据库相关配置 -# db-config: -# #主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID"; -# id-type: AUTO -# logic-delete-value: -1 -# logic-not-delete-value: 0 -# banner: false -# #原生配置 -# configuration: -# map-underscore-to-camel-case: true -# cache-enabled: false -# call-setters-on-nulls: true -# jdbc-type-for-null: 'null' -## log-impl: org.apache.ibatis.logging.stdout.StdOutImpl sqx: redis: diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 00000000..417330bb --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,46 @@ + + + logback + + + + + + + + + ${p_console} + + + + + logs/duanju.log + + + logs/duanju.%d{yyyy-MM-dd}.%i.log.gz + + 30 + 100MB + + + + ${p_file} + UTF-8 + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/mapper/course/CourseDetailsDao.xml b/src/main/resources/mapper/course/CourseDetailsDao.xml index bcbe20a4..9acb74de 100644 --- a/src/main/resources/mapper/course/CourseDetailsDao.xml +++ b/src/main/resources/mapper/course/CourseDetailsDao.xml @@ -3,13 +3,14 @@