添加用户商品展示和删除接口
This commit is contained in:
parent
ef622fbf95
commit
1fa68d070b
@ -30,11 +30,10 @@ public class UsersProductSpuCollectionController {
|
||||
|
||||
@PostMapping("/collection/{spuId}/{hasCollectionType}")
|
||||
@ApiOperation("商品收藏")
|
||||
// @RequiresLogin
|
||||
@RequiresLogin
|
||||
public CommonResult<Boolean> productSpuCollection(@PathVariable("spuId") Integer spuId,
|
||||
@PathVariable("hasCollectionType") Integer hasCollectionType) {
|
||||
// final Integer userId = UserSecurityContextHolder.getContext().getUserId();
|
||||
|
||||
return success(productSpuCollectionService.productSpuCollection(spuId, hasCollectionType,140));
|
||||
final Integer userId = UserSecurityContextHolder.getContext().getUserId();
|
||||
return success(productSpuCollectionService.productSpuCollection(spuId, hasCollectionType,userId));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
package cn.iocoder.mall.user.application.controller.users;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.user.api.UserProductSpuCollectionsService;
|
||||
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsPageBO;
|
||||
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsPageDTO;
|
||||
import cn.iocoder.mall.user.sdk.annotation.RequiresLogin;
|
||||
import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 用户收藏
|
||||
* @author xiaofeng
|
||||
* @date 2019/07/07 11:06
|
||||
* @version 1.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("users/favorite")
|
||||
@Api("用户收藏")
|
||||
public class UserFavoriteController {
|
||||
|
||||
@Reference(validation = "true", version = "${dubbo.provider.UserProductSpuCollectionsService.version}")
|
||||
private UserProductSpuCollectionsService userProductSpuCollectionsService;
|
||||
|
||||
@GetMapping("page")
|
||||
@RequiresLogin
|
||||
@ApiOperation("用户商品收藏列表")
|
||||
public CommonResult<UserProductSpuCollectionsPageBO> getUserProductSpuCollectionsPage(
|
||||
@Validated UserProductSpuCollectionsPageDTO userProductSpuCollectionsPageDTO) {
|
||||
final Integer userId = UserSecurityContextHolder.getContext().getUserId();
|
||||
userProductSpuCollectionsPageDTO.setUserId(userId);
|
||||
return userProductSpuCollectionsService.getUserProductSpuCollectionsPage(userProductSpuCollectionsPageDTO);
|
||||
}
|
||||
|
||||
@DeleteMapping("remove")
|
||||
@RequiresLogin
|
||||
@ApiOperation(value = "用户商品收藏-删除")
|
||||
public CommonResult<Boolean> removeFavorite(@RequestParam("spuId") final Integer spuId) {
|
||||
final Integer userId = UserSecurityContextHolder.getContext().getUserId();
|
||||
return userProductSpuCollectionsService.deleteUserProductSpuCollections(userId, spuId);
|
||||
}
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
package cn.iocoder.mall.user.api;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO;
|
||||
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsPageBO;
|
||||
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO;
|
||||
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsPageDTO;
|
||||
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO;
|
||||
|
||||
/**
|
||||
@ -33,4 +36,19 @@ public interface UserProductSpuCollectionsService {
|
||||
*/
|
||||
int updateUserProductSpuCollections(UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO);
|
||||
|
||||
/**
|
||||
* 获取用户收藏列表数据
|
||||
* @param userProductSpuCollectionsPageDTO
|
||||
* @return
|
||||
*/
|
||||
CommonResult<UserProductSpuCollectionsPageBO> getUserProductSpuCollectionsPage(
|
||||
UserProductSpuCollectionsPageDTO userProductSpuCollectionsPageDTO);
|
||||
|
||||
/**
|
||||
* 删除收藏数据
|
||||
* @param userId
|
||||
* @param spuId
|
||||
* @return
|
||||
*/
|
||||
CommonResult<Boolean> deleteUserProductSpuCollections(Integer userId, Integer spuId);
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
package cn.iocoder.mall.user.api.bo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品收藏分页
|
||||
* @author xiaofeng
|
||||
* @date 2019/07/06 18:37
|
||||
* @version 1.0
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class UserProductSpuCollectionsPageBO implements Serializable {
|
||||
|
||||
/**
|
||||
* 返回的数据列表
|
||||
*/
|
||||
private List<UserProductSpuCollectionsBO> list;
|
||||
|
||||
/**
|
||||
* 总量
|
||||
*/
|
||||
private Integer total;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package cn.iocoder.mall.user.api.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 商品收藏分页参数
|
||||
* @author xiaofeng
|
||||
* @date 2019/07/06 18:40
|
||||
* @version 1.0
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class UserProductSpuCollectionsPageDTO implements Serializable {
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
/**
|
||||
* 当前页
|
||||
*/
|
||||
@NotNull(message = "页码不能为空")
|
||||
private Integer pageNo;
|
||||
|
||||
/**
|
||||
* 每页显示的条数
|
||||
*/
|
||||
@NotNull(message = "每页条数不能为空")
|
||||
private Integer pageSize;
|
||||
}
|
@ -3,6 +3,9 @@ package cn.iocoder.mall.user.biz.dao;
|
||||
import cn.iocoder.mall.user.biz.dataobject.UserProductSpuCollectionsDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户_商品_收藏记录表
|
||||
@ -25,4 +28,22 @@ public interface UserProductSpuCollectionsMapper extends BaseMapper<UserProductS
|
||||
return selectOne(query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询用户收藏列表
|
||||
* @param userId
|
||||
* @param offset
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
List<UserProductSpuCollectionsDO> selectListByUser(@Param("userId") Integer userId, @Param("offset") Integer offset,
|
||||
@Param("limit") Integer limit);
|
||||
|
||||
/**
|
||||
* 根据用户ID 查找总数
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
Integer selectCountByUser(Integer userId);
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,23 @@
|
||||
package cn.iocoder.mall.user.biz.service;
|
||||
|
||||
import cn.iocoder.common.framework.constant.DeletedStatusEnum;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.user.api.UserProductSpuCollectionsService;
|
||||
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO;
|
||||
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsPageBO;
|
||||
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO;
|
||||
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsPageDTO;
|
||||
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO;
|
||||
import cn.iocoder.mall.user.biz.convert.UserProductSpuCollectionsConvert;
|
||||
import cn.iocoder.mall.user.biz.dao.UserProductSpuCollectionsMapper;
|
||||
import cn.iocoder.mall.user.biz.dataobject.UserProductSpuCollectionsDO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* UserSkuCollectionsServiceImpl
|
||||
@ -44,5 +53,54 @@ public class UserProductSpuCollectionsServiceImpl implements UserProductSpuColle
|
||||
.updateById(UserProductSpuCollectionsConvert.INSTANCE.convert(userProductSpuCollectionsUpdateDTO));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CommonResult<UserProductSpuCollectionsPageBO> getUserProductSpuCollectionsPage(
|
||||
UserProductSpuCollectionsPageDTO userProductSpuCollectionsPageDTO) {
|
||||
final int offset =
|
||||
(userProductSpuCollectionsPageDTO.getPageNo() - 1) * userProductSpuCollectionsPageDTO.getPageSize();
|
||||
final int totalCount = this.userProductSpuCollectionsMapper
|
||||
.selectCountByUser(userProductSpuCollectionsPageDTO.getUserId());
|
||||
if (totalCount == 0) {
|
||||
return CommonResult
|
||||
.success(new UserProductSpuCollectionsPageBO().setList(Collections.emptyList()).setTotal(0));
|
||||
}
|
||||
|
||||
List<UserProductSpuCollectionsDO> list = userProductSpuCollectionsMapper
|
||||
.selectListByUser(userProductSpuCollectionsPageDTO.getUserId(), offset,
|
||||
userProductSpuCollectionsPageDTO.getPageSize());
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return CommonResult.success(
|
||||
new UserProductSpuCollectionsPageBO().setList(Collections.emptyList()).setTotal(totalCount));
|
||||
}
|
||||
|
||||
UserProductSpuCollectionsPageBO userProductSpuCollectionsPageBO = new UserProductSpuCollectionsPageBO();
|
||||
userProductSpuCollectionsPageBO.setList(UserProductSpuCollectionsConvert.INSTANCE.convert(list));
|
||||
// 查询分页总数
|
||||
userProductSpuCollectionsPageBO.setTotal(totalCount);
|
||||
// 返回结果
|
||||
return CommonResult.success(userProductSpuCollectionsPageBO);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 取消收藏
|
||||
* @param userId
|
||||
* @param spuId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public CommonResult<Boolean> deleteUserProductSpuCollections(final Integer userId, final Integer spuId) {
|
||||
UserProductSpuCollectionsBO userProductSpuCollectionsBO = this
|
||||
.getUserSpuCollectionsByUserIdAndSpuId(userId, spuId);
|
||||
int result = 0;
|
||||
if (userProductSpuCollectionsBO != null) {
|
||||
// 未取消收藏的数据
|
||||
if (userProductSpuCollectionsBO.getDeleted().equals(DeletedStatusEnum.DELETED_NO.getValue())) {
|
||||
UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO = new UserProductSpuCollectionsUpdateDTO().setId(userProductSpuCollectionsBO.getId())
|
||||
.setUpdateTime(new Date()).setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
|
||||
result = this.updateUserProductSpuCollections(userProductSpuCollectionsUpdateDTO);
|
||||
}
|
||||
}
|
||||
return CommonResult.success(result > 0 ? Boolean.TRUE : Boolean.FALSE);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.user.biz.dao.UserProductSpuCollectionsMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, user_id, nickname, spu_id, spu_name,
|
||||
spu_image, create_time, update_time,
|
||||
deleted
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="cn.iocoder.mall.user.biz.dataobject.UserProductSpuCollectionsDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM user_spu_collections
|
||||
WHERE id = #{id}
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
|
||||
<select id="selectListByUser" resultType="cn.iocoder.mall.user.biz.dataobject.UserProductSpuCollectionsDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM user_spu_collections
|
||||
<where>
|
||||
user_id = #{userId} AND deleted = 0
|
||||
</where>
|
||||
ORDER BY sort ASC
|
||||
<if test="offset != null and limit != null">
|
||||
LIMIT #{offset}, #{limit}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="selectCountByUser" resultType="Integer">
|
||||
SELECT
|
||||
COUNT(1)
|
||||
FROM user_spu_collections
|
||||
<where>
|
||||
user_id = #{userId} AND deleted = 0
|
||||
</where>
|
||||
</select>
|
||||
|
||||
</mapper>
|
Loading…
Reference in New Issue
Block a user