添加商品收藏

This commit is contained in:
xiaofeng 2019-07-02 23:57:52 +08:00
parent 41cd57df2d
commit 5c2c4da6fa
11 changed files with 555 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package cn.iocoder.mall.user.api;
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO;
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO;
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO;
/**
* UserProductSpuCollectionsService
* @author xiaofeng
* @date 2019/07/01 20:27
* @version 1.0
*/
public interface UserProductSpuCollectionsService {
/**
* 添加商品收藏
* @return
*/
int addUserSkuCollections(UserProductSpuCollectionsAddDTO userProductSpuCollectionsAddDTO);
/**
* 获取用户商品收藏
* @param userId 用户ID
* @param spuId 商品ID
* @return
*/
UserProductSpuCollectionsBO getUserSpuCollectionsByUserIdAndSpuId(Integer userId, Integer spuId);
/**
* 取消商品收藏
* @param userProductSpuCollectionsUpdateDTO
* @return
*/
int updateUserProductSpuCollections(UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO);
}

View File

@ -0,0 +1,64 @@
package cn.iocoder.mall.user.api.bo;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* 用户_商品_收藏记录表
* @author xiaofeng
* @date 2019-07-01 20:23:30
*/
@Data
@Accessors(chain = true)
public class UserProductSpuCollectionsBO implements Serializable {
/**
* id自增长
*/
private Integer id;
/**
* 用户id
*/
private Integer userId;
/**
* 用户名称
*/
private String nickname;
/**
* 商品id
*/
private Integer spuId;
/**
* 商品名字
*/
private String spuName;
/**
* 图片名字
*/
private String spuImage;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 删除状态
*/
private Integer deleted;
}

View File

@ -0,0 +1,65 @@
package cn.iocoder.mall.user.api.dto;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* 添加商品收藏参数
* @author xiaofeng
* @date 2019/07/01 20:38
* @version 1.0
*/
@Data
@Accessors(chain = true)
public class UserProductSpuCollectionsAddDTO implements Serializable {
/**
* id自增长
*/
private Integer id;
/**
* 用户id
*/
private Integer userId;
/**
* 用户名称
*/
private String nickname;
/**
* 商品id
*/
private Integer spuId;
/**
* 商品名字
*/
private String spuName;
/**
* 图片名字
*/
private String spuImage;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 删除状态
*/
private Integer deleted;
}

View File

@ -0,0 +1,35 @@
package cn.iocoder.mall.user.api.dto;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* 更新商品收藏参数
* @author xiaofeng
* @date 2019/07/01 20:38
* @version 1.0
*/
@Data
@Accessors(chain = true)
public class UserProductSpuCollectionsUpdateDTO implements Serializable {
/**
* id自增长
*/
private Integer id;
/**
* 更新时间
*/
private Date updateTime;
/**
* 删除状态
*/
private Integer deleted;
}

View File

@ -17,6 +17,12 @@
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>product-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- DB 相关 -->
<dependency>
<groupId>mysql</groupId>
@ -67,6 +73,12 @@
<artifactId>curator-framework</artifactId>
</dependency>
<!-- MQ 相关 -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,66 @@
package cn.iocoder.mall.user.biz.convert;
import cn.iocoder.mall.product.api.message.ProductSpuCollectionMessage;
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO;
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO;
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO;
import cn.iocoder.mall.user.biz.dataobject.UserProductSpuCollectionsDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 用户_商品_收藏记录表
*
* @author xiaofeng
* @date 2019-07-01 20:23:30
*/
@Mapper
public interface UserProductSpuCollectionsConvert {
UserProductSpuCollectionsConvert INSTANCE = Mappers.getMapper(UserProductSpuCollectionsConvert.class);
/**
* DTO convert DO
* @param userSkuCollectionsAddDTO
* @return
*/
@Mappings({})
UserProductSpuCollectionsDO convert(UserProductSpuCollectionsAddDTO userSkuCollectionsAddDTO);
/**
* update DTO convert DO
* @param userProductSpuCollectionsUpdateDTO
* @return
*/
@Mappings({})
UserProductSpuCollectionsDO convert(UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO);
/**
* DO Convert BO
* @param userSkuCollectionsDO
* @return
*/
@Mappings({})
UserProductSpuCollectionsBO convert(UserProductSpuCollectionsDO userSkuCollectionsDO);
/**
* DO List convert BO LIST
* @param userSkuCollectionsDOS
* @return
*/
@Mappings({})
List<UserProductSpuCollectionsBO> convert(List<UserProductSpuCollectionsDO> userSkuCollectionsDOS);
/**
* 消处数据转换
* @param productSpuCollectionMessage
* @return
*/
@Mappings({})
UserProductSpuCollectionsAddDTO convert(ProductSpuCollectionMessage productSpuCollectionMessage);
}

View File

@ -0,0 +1,28 @@
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;
/**
* 用户_商品_收藏记录表
*
* @author xiaofeng
* @date 2019-07-01 20:23:30
*/
public interface UserProductSpuCollectionsMapper extends BaseMapper<UserProductSpuCollectionsDO> {
/**
* 根据用户id spuId 查找用户商品收藏
* @param userId
* @param spuId
* @return
*/
default UserProductSpuCollectionsDO getUserSpuCollectionsByUserIdAndSpuId(final Integer userId,
final Integer spuId) {
QueryWrapper<UserProductSpuCollectionsDO> query = new QueryWrapper<UserProductSpuCollectionsDO>()
.eq("user_id", userId).eq("spu_id", spuId);
return selectOne(query);
}
}

View File

@ -0,0 +1,69 @@
package cn.iocoder.mall.user.biz.dataobject;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* 用户_商品_收藏记录表
*
* @author xiaofeng
* @date 2019-07-01 20:23:30
*/
@TableName("user_spu_collections")
@Data
@Accessors(chain = true)
public class UserProductSpuCollectionsDO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* id自增长
*/
private Integer id;
/**
* 用户id
*/
private Integer userId;
/**
* 用户名称
*/
private String nickname;
/**
* 商品id
*/
private Integer spuId;
/**
* 商品名字
*/
private String spuName;
/**
* 图片名字
*/
private String spuImage;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 删除状态
*/
private Integer deleted;
}

View File

@ -0,0 +1,124 @@
package cn.iocoder.mall.user.biz.mq;
import cn.iocoder.common.framework.constant.DeletedStatusEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.mall.product.api.message.ProductSpuCollectionMessage;
import cn.iocoder.mall.user.api.UserProductSpuCollectionsService;
import cn.iocoder.mall.user.api.UserService;
import cn.iocoder.mall.user.api.bo.UserBO;
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO;
import cn.iocoder.mall.user.api.constant.UserErrorCodeEnum;
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO;
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO;
import cn.iocoder.mall.user.biz.convert.UserProductSpuCollectionsConvert;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* 商品收藏 消费者
* @author xiaofeng
* @date 2019/07/02 19:57
* @version 1.0
*/
@Service
@RocketMQMessageListener(topic = ProductSpuCollectionMessage.TOPIC, consumerGroup = "product-spu-consumer-group-"
+ ProductSpuCollectionMessage.TOPIC)
public class UserProductSpuCollectionsConsumer implements RocketMQListener<ProductSpuCollectionMessage> {
@Autowired
private UserProductSpuCollectionsService userProductSpuCollectionsService;
@Autowired
private UserService userService;
@Override
public void onMessage(ProductSpuCollectionMessage productSpuCollectionMessage) {
UserBO userBO = userService.getUser(productSpuCollectionMessage.getUserId());
if (userBO == null) {
throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_NOT_EXISTS.getCode());
}
// 收藏
if (productSpuCollectionMessage.getHasCollectionType().equals(1)) {
this.saveUserProductSpuCollections(productSpuCollectionMessage, userBO.getNickname());
} else if (productSpuCollectionMessage.getHasCollectionType().equals(2)) {
// 取消收藏
this.deleteUserProductSpuCollections(productSpuCollectionMessage.getUserId(),
productSpuCollectionMessage.getSpuId());
}
}
/**
* 保存商品收藏
* @param productSpuCollectionMessage
* @param nickname
* @return
*/
private int saveUserProductSpuCollections(final ProductSpuCollectionMessage productSpuCollectionMessage,
final String nickname) {
int result = 0;
UserProductSpuCollectionsBO userProductSpuCollectionsBO = this.userProductSpuCollectionsService
.getUserSpuCollectionsByUserIdAndSpuId(productSpuCollectionMessage.getUserId(),
productSpuCollectionMessage.getSpuId());
if (userProductSpuCollectionsBO == null) {
UserProductSpuCollectionsAddDTO userProductSpuCollectionsAddDTO = UserProductSpuCollectionsConvert.INSTANCE
.convert(productSpuCollectionMessage);
userProductSpuCollectionsAddDTO.setNickname(StringUtils.isEmpty(nickname) ? "" : nickname);
userProductSpuCollectionsAddDTO.setCreateTime(new Date());
userProductSpuCollectionsAddDTO.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
result = userProductSpuCollectionsService.addUserSkuCollections(userProductSpuCollectionsAddDTO);
} else {
// 存在重新收藏
if (userProductSpuCollectionsBO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) {
UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO = this
.setUserProductSpuCollectionsUpdateDTO(userProductSpuCollectionsBO.getId(),
DeletedStatusEnum.DELETED_NO);
result = this.userProductSpuCollectionsService
.updateUserProductSpuCollections(userProductSpuCollectionsUpdateDTO);
}
}
return result;
}
/**
* 取消收藏
* @param userId
* @param spuId
* @return
*/
private int deleteUserProductSpuCollections(final Integer userId, final Integer spuId) {
UserProductSpuCollectionsBO userProductSpuCollectionsBO = this.userProductSpuCollectionsService
.getUserSpuCollectionsByUserIdAndSpuId(userId, spuId);
int result = 0;
if (userProductSpuCollectionsBO != null) {
// 未取消收藏的数据
if (userProductSpuCollectionsBO.getDeleted().equals(DeletedStatusEnum.DELETED_NO.getValue())) {
UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO = this
.setUserProductSpuCollectionsUpdateDTO(userProductSpuCollectionsBO.getId(),
DeletedStatusEnum.DELETED_YES);
result = this.userProductSpuCollectionsService
.updateUserProductSpuCollections(userProductSpuCollectionsUpdateDTO);
}
}
return result;
}
/**
* 设置更新值
* @param id
* @param deletedStatusEnum
* @return
*/
private UserProductSpuCollectionsUpdateDTO setUserProductSpuCollectionsUpdateDTO(final Integer id,
final DeletedStatusEnum deletedStatusEnum) {
return new UserProductSpuCollectionsUpdateDTO().setId(id).setUpdateTime(new Date())
.setDeleted(deletedStatusEnum.getValue());
}
}

View File

@ -0,0 +1,48 @@
package cn.iocoder.mall.user.biz.service;
import cn.iocoder.mall.user.api.UserProductSpuCollectionsService;
import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO;
import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO;
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;
/**
* UserSkuCollectionsServiceImpl
* @author xiaofeng
* @date 2019/07/01 21:02
* @version 1.0
*/
@Service
@org.apache.dubbo.config.annotation.Service(validation = "true", version = "${dubbo.provider.UserProductSpuCollectionsService.version}")
public class UserProductSpuCollectionsServiceImpl implements UserProductSpuCollectionsService {
@Autowired
private UserProductSpuCollectionsMapper userProductSpuCollectionsMapper;
@Override
public int addUserSkuCollections(UserProductSpuCollectionsAddDTO userProductSpuCollectionsAddDTO) {
return userProductSpuCollectionsMapper
.insert(UserProductSpuCollectionsConvert.INSTANCE.convert(userProductSpuCollectionsAddDTO));
}
@Override
public UserProductSpuCollectionsBO getUserSpuCollectionsByUserIdAndSpuId(Integer userId, Integer spuId) {
UserProductSpuCollectionsDO userProductSpuCollectionsDO = userProductSpuCollectionsMapper
.getUserSpuCollectionsByUserIdAndSpuId(userId, spuId);
return UserProductSpuCollectionsConvert.INSTANCE.convert(userProductSpuCollectionsDO);
}
@Override
public int updateUserProductSpuCollections(UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO) {
return userProductSpuCollectionsMapper
.updateById(UserProductSpuCollectionsConvert.INSTANCE.convert(userProductSpuCollectionsUpdateDTO));
}
}

View File

@ -39,6 +39,14 @@ dubbo:
version: 1.0.0
UserService:
version: 1.0.0
UserProductSpuCollectionsService:
version: 1.0.0
consumer:
OAuth2Service:
version: 1.0.0
# rocketmq
rocketmq:
name-server: 127.0.0.1:9876
producer:
group: user-producer-spu-collection-group