【8.0.4】【fav模块】更新绑定接口和取消绑定

pull/60/head
fengshuonan 2023-11-21 22:56:07 +08:00
parent 99dfbe2468
commit 5e57f76383
4 changed files with 108 additions and 154 deletions

View File

@ -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<>();
}
}

View File

@ -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<Long> batchDeleteIdList;

View File

@ -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<SysUserFavorite>, 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<SysUserFavorite>
* @author fengshuonan
* @since 2023/11/21 22:09
*/
List<SysUserFavorite> findList(SysUserFavoriteRequest sysUserFavoriteRequest);
/**
*
*
* @param sysUserFavoriteRequest
* @return PageResult<SysUserFavorite>
* @author fengshuonan
* @since 2023/11/21 22:09
*/
PageResult<SysUserFavorite> findPage(SysUserFavoriteRequest sysUserFavoriteRequest);
void unBind(SysUserFavoriteRequest sysUserFavoriteRequest);
}

View File

@ -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<SysUserFavoriteMapper, SysUserFavorite> 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<SysUserFavorite> findPage(SysUserFavoriteRequest sysUserFavoriteRequest) {
LambdaQueryWrapper<SysUserFavorite> wrapper = createWrapper(sysUserFavoriteRequest);
Page<SysUserFavorite> pageList = this.page(PageFactory.defaultPage(), wrapper);
return PageResultFactory.createPageResult(pageList);
}
@Override
public List<SysUserFavorite> findList(SysUserFavoriteRequest sysUserFavoriteRequest) {
LambdaQueryWrapper<SysUserFavorite> 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<Long> getFavoriteBusinessId(Long userId, String favType) {
@ -126,27 +61,55 @@ public class SysUserFavoriteServiceImpl extends ServiceImpl<SysUserFavoriteMappe
return this.getFavoriteBusinessId(loginUserNullable.getUserId(), favType);
}
@Override
public void bind(SysUserFavoriteRequest sysUserFavoriteRequest) {
String favType = sysUserFavoriteRequest.getFavType();
Long businessId = sysUserFavoriteRequest.getBusinessId();
Long userId = LoginContext.me().getLoginUser().getUserId();
// 查询是否用户已经绑定了收藏
LambdaQueryWrapper<SysUserFavorite> 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<SysUserFavorite> 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<SysUserFavorite> createWrapper(SysUserFavoriteRequest sysUserFavoriteRequest) {
LambdaQueryWrapper<SysUserFavorite> 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<SysUserFavorite> createCommonUserBindWrapper(Long userId, Long businessId, String favType) {
LambdaQueryWrapper<SysUserFavorite> sysUserFavoriteLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysUserFavoriteLambdaQueryWrapper.eq(SysUserFavorite::getUserId, userId);
sysUserFavoriteLambdaQueryWrapper.eq(SysUserFavorite::getBusinessId, businessId);
sysUserFavoriteLambdaQueryWrapper.eq(SysUserFavorite::getFavType, favType);
return sysUserFavoriteLambdaQueryWrapper;
}
}