From 5e57f76383946a05dbac6c615e55793fe475cb53 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Tue, 21 Nov 2023 22:56:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.0.4=E3=80=91=E3=80=90fav=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E3=80=91=E6=9B=B4=E6=96=B0=E7=BB=91=E5=AE=9A=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=92=8C=E5=8F=96=E6=B6=88=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FavoriteController.java | 53 +++++++ .../pojo/request/SysUserFavoriteRequest.java | 14 +- .../service/SysUserFavoriteService.java | 64 +-------- .../impl/SysUserFavoriteServiceImpl.java | 131 +++++++----------- 4 files changed, 108 insertions(+), 154 deletions(-) create mode 100644 kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/controller/FavoriteController.java diff --git a/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/controller/FavoriteController.java b/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/controller/FavoriteController.java new file mode 100644 index 000000000..da50661b7 --- /dev/null +++ b/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/controller/FavoriteController.java @@ -0,0 +1,53 @@ +package cn.stylefeng.roses.kernel.favorite.modular.controller; + +import cn.stylefeng.roses.kernel.favorite.modular.pojo.request.SysUserFavoriteRequest; +import cn.stylefeng.roses.kernel.favorite.modular.service.SysUserFavoriteService; +import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest; +import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData; +import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData; +import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource; +import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * 通用收藏控制器 + * + * @author fengshuonan + * @since 2023/11/21 22:36 + */ +@RestController +@ApiResource(name = "通用收藏控制器") +public class FavoriteController { + + @Resource + private SysUserFavoriteService sysUserFavoriteService; + + /** + * 绑定用户收藏 + * + * @author fengshuonan + * @since 2023/11/21 22:46 + */ + @PostResource(name = "绑定用户收藏", path = "/favorite/bind") + public ResponseData bind(@RequestBody @Validated(BaseRequest.add.class) SysUserFavoriteRequest sysUserFavoriteRequest) { + sysUserFavoriteService.bind(sysUserFavoriteRequest); + return new SuccessResponseData<>(); + } + + /** + * 取消用户绑定的收藏 + * + * @author fengshuonan + * @since 2023/11/21 22:46 + */ + @PostResource(name = "取消用户绑定的收藏", path = "/favorite/unBind") + public ResponseData unBind(@RequestBody @Validated(BaseRequest.delete.class) SysUserFavoriteRequest sysUserFavoriteRequest) { + sysUserFavoriteService.unBind(sysUserFavoriteRequest); + return new SuccessResponseData<>(); + } + +} diff --git a/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/pojo/request/SysUserFavoriteRequest.java b/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/pojo/request/SysUserFavoriteRequest.java index 45900d8c1..3f2e0197f 100644 --- a/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/pojo/request/SysUserFavoriteRequest.java +++ b/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/pojo/request/SysUserFavoriteRequest.java @@ -22,42 +22,32 @@ public class SysUserFavoriteRequest extends BaseRequest { /** * 主键id */ - @NotNull(message = "主键id不能为空", groups = {edit.class, delete.class}) @ChineseDescription("主键id") private Long favoriteId; /** * 收藏业务的类型,存业务编码 */ - @NotBlank(message = "收藏业务的类型,存业务编码不能为空", groups = {add.class, edit.class}) @ChineseDescription("收藏业务的类型,存业务编码") + @NotBlank(message = "收藏业务类型不能为空", groups = {add.class, delete.class}) private String favType; /** * 用户id */ - @NotNull(message = "用户id不能为空", groups = {add.class, edit.class}) @ChineseDescription("用户id") private Long userId; /** * 具体业务id */ - @NotNull(message = "具体业务id不能为空", groups = {add.class, edit.class}) @ChineseDescription("具体业务id") + @NotNull(message = "具体业务id不能为空", groups = {add.class, delete.class}) private Long businessId; - /** - * 收藏时间 - */ - @ChineseDescription("收藏时间") - private String favTime; - - /** * 批量删除用的id集合 */ - @NotNull(message = "批量删除id集合不能为空", groups = batchDelete.class) @ChineseDescription("批量删除用的id集合") private List batchDeleteIdList; diff --git a/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/service/SysUserFavoriteService.java b/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/service/SysUserFavoriteService.java index d97bf7439..7606a3219 100644 --- a/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/service/SysUserFavoriteService.java +++ b/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/service/SysUserFavoriteService.java @@ -1,13 +1,10 @@ package cn.stylefeng.roses.kernel.favorite.modular.service; -import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult; import cn.stylefeng.roses.kernel.favorite.modular.api.UserFavoriteApi; import cn.stylefeng.roses.kernel.favorite.modular.entity.SysUserFavorite; import cn.stylefeng.roses.kernel.favorite.modular.pojo.request.SysUserFavoriteRequest; import com.baomidou.mybatisplus.extension.service.IService; -import java.util.List; - /** * 用户收藏信息服务类 * @@ -17,68 +14,19 @@ import java.util.List; public interface SysUserFavoriteService extends IService, UserFavoriteApi { /** - * 新增用户收藏信息 + * 绑定用户收藏信息 * - * @param sysUserFavoriteRequest 请求参数 * @author fengshuonan - * @since 2023/11/21 22:09 + * @since 2023/11/21 22:48 */ - void add(SysUserFavoriteRequest sysUserFavoriteRequest); + void bind(SysUserFavoriteRequest sysUserFavoriteRequest); /** - * 删除用户收藏信息 + * 取消用户绑定的收藏 * - * @param sysUserFavoriteRequest 请求参数 * @author fengshuonan - * @since 2023/11/21 22:09 + * @since 2023/11/21 22:54 */ - void del(SysUserFavoriteRequest sysUserFavoriteRequest); - - /** - * 批量删除用户收藏信息 - * - * @param sysUserFavoriteRequest 请求参数 - * @author fengshuonan - * @since 2023/11/21 22:09 - */ - void batchDelete(SysUserFavoriteRequest sysUserFavoriteRequest); - - /** - * 编辑用户收藏信息 - * - * @param sysUserFavoriteRequest 请求参数 - * @author fengshuonan - * @since 2023/11/21 22:09 - */ - void edit(SysUserFavoriteRequest sysUserFavoriteRequest); - - /** - * 查询详情用户收藏信息 - * - * @param sysUserFavoriteRequest 请求参数 - * @author fengshuonan - * @since 2023/11/21 22:09 - */ - SysUserFavorite detail(SysUserFavoriteRequest sysUserFavoriteRequest); - - /** - * 获取用户收藏信息列表 - * - * @param sysUserFavoriteRequest 请求参数 - * @return List 返回结果 - * @author fengshuonan - * @since 2023/11/21 22:09 - */ - List findList(SysUserFavoriteRequest sysUserFavoriteRequest); - - /** - * 获取用户收藏信息分页列表 - * - * @param sysUserFavoriteRequest 请求参数 - * @return PageResult 返回结果 - * @author fengshuonan - * @since 2023/11/21 22:09 - */ - PageResult findPage(SysUserFavoriteRequest sysUserFavoriteRequest); + void unBind(SysUserFavoriteRequest sysUserFavoriteRequest); } diff --git a/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/service/impl/SysUserFavoriteServiceImpl.java b/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/service/impl/SysUserFavoriteServiceImpl.java index b1e80be42..e5cbae43f 100644 --- a/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/service/impl/SysUserFavoriteServiceImpl.java +++ b/kernel-s-user-favorite/user-favorite-business/src/main/java/cn/stylefeng/roses/kernel/favorite/modular/service/impl/SysUserFavoriteServiceImpl.java @@ -1,25 +1,18 @@ package cn.stylefeng.roses.kernel.favorite.modular.service.impl; -import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; import cn.stylefeng.roses.kernel.auth.api.context.LoginContext; import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser; -import cn.stylefeng.roses.kernel.db.api.factory.PageFactory; -import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory; -import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult; import cn.stylefeng.roses.kernel.favorite.modular.entity.SysUserFavorite; -import cn.stylefeng.roses.kernel.favorite.modular.enums.SysUserFavoriteExceptionEnum; import cn.stylefeng.roses.kernel.favorite.modular.mapper.SysUserFavoriteMapper; import cn.stylefeng.roses.kernel.favorite.modular.pojo.request.SysUserFavoriteRequest; import cn.stylefeng.roses.kernel.favorite.modular.service.SysUserFavoriteService; -import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -32,64 +25,6 @@ import java.util.stream.Collectors; @Service public class SysUserFavoriteServiceImpl extends ServiceImpl implements SysUserFavoriteService { - @Override - public void add(SysUserFavoriteRequest sysUserFavoriteRequest) { - SysUserFavorite sysUserFavorite = new SysUserFavorite(); - BeanUtil.copyProperties(sysUserFavoriteRequest, sysUserFavorite); - this.save(sysUserFavorite); - } - - @Override - public void del(SysUserFavoriteRequest sysUserFavoriteRequest) { - SysUserFavorite sysUserFavorite = this.querySysUserFavorite(sysUserFavoriteRequest); - this.removeById(sysUserFavorite.getFavoriteId()); - } - - @Override - @Transactional(rollbackFor = Exception.class) - public void batchDelete(SysUserFavoriteRequest sysUserFavoriteRequest) { - this.removeByIds(sysUserFavoriteRequest.getBatchDeleteIdList()); - } - - @Override - public void edit(SysUserFavoriteRequest sysUserFavoriteRequest) { - SysUserFavorite sysUserFavorite = this.querySysUserFavorite(sysUserFavoriteRequest); - BeanUtil.copyProperties(sysUserFavoriteRequest, sysUserFavorite); - this.updateById(sysUserFavorite); - } - - @Override - public SysUserFavorite detail(SysUserFavoriteRequest sysUserFavoriteRequest) { - return this.querySysUserFavorite(sysUserFavoriteRequest); - } - - @Override - public PageResult findPage(SysUserFavoriteRequest sysUserFavoriteRequest) { - LambdaQueryWrapper wrapper = createWrapper(sysUserFavoriteRequest); - Page pageList = this.page(PageFactory.defaultPage(), wrapper); - return PageResultFactory.createPageResult(pageList); - } - - @Override - public List findList(SysUserFavoriteRequest sysUserFavoriteRequest) { - LambdaQueryWrapper wrapper = this.createWrapper(sysUserFavoriteRequest); - return this.list(wrapper); - } - - /** - * 获取信息 - * - * @author fengshuonan - * @since 2023/11/21 22:09 - */ - private SysUserFavorite querySysUserFavorite(SysUserFavoriteRequest sysUserFavoriteRequest) { - SysUserFavorite sysUserFavorite = this.getById(sysUserFavoriteRequest.getFavoriteId()); - if (ObjectUtil.isEmpty(sysUserFavorite)) { - throw new ServiceException(SysUserFavoriteExceptionEnum.SYS_USER_FAVORITE_NOT_EXISTED); - } - return sysUserFavorite; - } - @Override public List getFavoriteBusinessId(Long userId, String favType) { @@ -126,27 +61,55 @@ public class SysUserFavoriteServiceImpl extends ServiceImpl sysUserFavoriteLambdaQueryWrapper = createCommonUserBindWrapper( + userId, businessId, favType); + long count = this.count(sysUserFavoriteLambdaQueryWrapper); + if (count > 0) { + return; + } + + // 新绑定用户收藏 + SysUserFavorite sysUserFavorite = new SysUserFavorite(); + sysUserFavorite.setFavType(favType); + sysUserFavorite.setUserId(userId); + sysUserFavorite.setBusinessId(businessId); + sysUserFavorite.setFavTime(new Date()); + this.save(sysUserFavorite); + } + + @Override + public void unBind(SysUserFavoriteRequest sysUserFavoriteRequest) { + + String favType = sysUserFavoriteRequest.getFavType(); + Long businessId = sysUserFavoriteRequest.getBusinessId(); + Long userId = LoginContext.me().getLoginUser().getUserId(); + + // 直接取消绑定 + LambdaQueryWrapper sysUserFavoriteLambdaQueryWrapper = createCommonUserBindWrapper( + userId, businessId, favType); + this.remove(sysUserFavoriteLambdaQueryWrapper); + } + /** - * 创建查询wrapper + * 创建通用wrapper * * @author fengshuonan - * @since 2023/11/21 22:09 + * @since 2023/11/21 22:55 */ - private LambdaQueryWrapper createWrapper(SysUserFavoriteRequest sysUserFavoriteRequest) { - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - - Long favoriteId = sysUserFavoriteRequest.getFavoriteId(); - String favType = sysUserFavoriteRequest.getFavType(); - Long userId = sysUserFavoriteRequest.getUserId(); - Long businessId = sysUserFavoriteRequest.getBusinessId(); - String favTime = sysUserFavoriteRequest.getFavTime(); - - queryWrapper.eq(ObjectUtil.isNotNull(favoriteId), SysUserFavorite::getFavoriteId, favoriteId); - queryWrapper.like(ObjectUtil.isNotEmpty(favType), SysUserFavorite::getFavType, favType); - queryWrapper.eq(ObjectUtil.isNotNull(userId), SysUserFavorite::getUserId, userId); - queryWrapper.eq(ObjectUtil.isNotNull(businessId), SysUserFavorite::getBusinessId, businessId); - queryWrapper.eq(ObjectUtil.isNotNull(favTime), SysUserFavorite::getFavTime, favTime); - - return queryWrapper; + private static LambdaQueryWrapper createCommonUserBindWrapper(Long userId, Long businessId, String favType) { + LambdaQueryWrapper sysUserFavoriteLambdaQueryWrapper = new LambdaQueryWrapper<>(); + sysUserFavoriteLambdaQueryWrapper.eq(SysUserFavorite::getUserId, userId); + sysUserFavoriteLambdaQueryWrapper.eq(SysUserFavorite::getBusinessId, businessId); + sysUserFavoriteLambdaQueryWrapper.eq(SysUserFavorite::getFavType, favType); + return sysUserFavoriteLambdaQueryWrapper; } + }