package com.panzhihua.service_community.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.panzhihua.common.model.dtos.shop.ComShopCartDTO;
|
import com.panzhihua.common.model.dtos.shop.ComShopEditNubCartDTO;
|
import com.panzhihua.common.model.vos.R;
|
import com.panzhihua.common.model.vos.shop.ComShopCartListVO;
|
import com.panzhihua.common.model.vos.shop.ComShopCartStoreVO;
|
import com.panzhihua.common.model.vos.shop.ComShopCartVO;
|
import com.panzhihua.service_community.dao.ComShopCartDAO;
|
import com.panzhihua.service_community.dao.ComShopGoodsAttrDAO;
|
import com.panzhihua.service_community.dao.ComShopGoodsDAO;
|
import com.panzhihua.service_community.dao.ComShopStoreDAO;
|
import com.panzhihua.service_community.model.dos.ComShopCartDO;
|
import com.panzhihua.service_community.model.dos.ComShopGoodsAttrDO;
|
import com.panzhihua.service_community.model.dos.ComShopGoodsDO;
|
import com.panzhihua.service_community.model.dos.ComShopStoreDO;
|
import com.panzhihua.service_community.service.ComShopCartService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @auther lyq
|
* @create 2021-04-14 15:02:14
|
* @describe 购物车表服务实现类
|
*/
|
@Slf4j
|
@Service
|
public class ComShopCartServiceImpl extends ServiceImpl<ComShopCartDAO, ComShopCartDO> implements ComShopCartService {
|
|
@Resource
|
private ComShopGoodsDAO shopGoodsDAO;
|
@Resource
|
private ComShopStoreDAO shopStoreDAO;
|
@Resource
|
private ComShopGoodsAttrDAO shopGoodsAttrDAO;
|
|
|
/**
|
* 根据用户id查询购物车列表
|
* @param userId 用户id
|
* @return 购物车列表
|
*/
|
@Override
|
public R shopCartList(Long userId){
|
//返回参数
|
ComShopCartListVO comShopCart = new ComShopCartListVO();
|
//失效宝贝集合
|
List<ComShopCartVO> invalidCartList = new ArrayList<>();
|
//有效宝贝集合
|
List<ComShopCartStoreVO> cartList = new ArrayList<>();
|
|
List<ComShopCartDO> shopCartDOList = this.baseMapper.selectList(new QueryWrapper<ComShopCartDO>().eq("user_id",userId));
|
if(!shopCartDOList.isEmpty()){
|
shopCartDOList.forEach(shopCart->{
|
//查询当前商品是否是有效商品
|
ComShopGoodsDO goodsDO = shopGoodsDAO.selectOne(new QueryWrapper<ComShopGoodsDO>()
|
.eq("id",shopCart.getGoodsId()).eq("delete_status",ComShopGoodsDO.deleteStatus.no)
|
.eq("status",ComShopGoodsDO.status.sell));
|
if(goodsDO == null){
|
ComShopCartVO invalidCart = new ComShopCartVO();
|
BeanUtils.copyProperties(shopCart,invalidCart);
|
invalidCartList.add(invalidCart);
|
}
|
|
//有效商品区分店铺
|
if(cartList.isEmpty()){
|
ComShopCartStoreVO cartStoreVO = new ComShopCartStoreVO();
|
//查询商品所属店铺
|
ComShopStoreDO shopStoreDO = shopStoreDAO.selectById(goodsDO.getStoreId());
|
if(shopStoreDO != null){
|
cartStoreVO.setStoreId(shopStoreDO.getId());
|
cartStoreVO.setStoreName(shopStoreDO.getName());
|
cartStoreVO.setStoreLogo(shopStoreDO.getLogo());
|
}
|
List<ComShopCartVO> cartsList = new ArrayList<>();
|
ComShopCartVO cartVO = new ComShopCartVO();
|
BeanUtils.copyProperties(shopCart,cartVO);
|
cartsList.add(cartVO);
|
cartStoreVO.setCartList(cartsList);
|
cartList.add(cartStoreVO);
|
}else{
|
boolean isOk = true;
|
for (ComShopCartStoreVO vo:cartList) {
|
if(vo.getStoreId().equals(goodsDO.getStoreId())){
|
isOk = false;
|
ComShopCartVO cartVO = new ComShopCartVO();
|
BeanUtils.copyProperties(shopCart,cartVO);
|
vo.getCartList().add(cartVO);
|
}
|
}
|
|
if(isOk){
|
ComShopCartStoreVO cartStoreVO = new ComShopCartStoreVO();
|
//查询商品所属店铺
|
ComShopStoreDO shopStoreDO = shopStoreDAO.selectById(goodsDO.getStoreId());
|
if(shopStoreDO != null){
|
cartStoreVO.setStoreId(shopStoreDO.getId());
|
cartStoreVO.setStoreName(shopStoreDO.getName());
|
cartStoreVO.setStoreLogo(shopStoreDO.getLogo());
|
}
|
List<ComShopCartVO> cartsList = new ArrayList<>();
|
ComShopCartVO cartVO = new ComShopCartVO();
|
BeanUtils.copyProperties(shopCart,cartVO);
|
cartsList.add(cartVO);
|
cartStoreVO.setCartList(cartsList);
|
cartList.add(cartStoreVO);
|
}
|
|
}
|
|
|
});
|
//设置返回参数
|
comShopCart.setInvalidCartList(invalidCartList);
|
comShopCart.setCartList(cartList);
|
}
|
return R.ok(comShopCart);
|
}
|
|
/**
|
* 购物车添加
|
* @param comShopCartDTO 请求参数
|
* @return 添加结果
|
*/
|
@Override
|
public R shopAddCart(ComShopCartDTO comShopCartDTO){
|
//查询当前商品是否已经在购物车中
|
ComShopCartDO shopCartDO = this.baseMapper.selectOne(new QueryWrapper<ComShopCartDO>().eq("user_id",comShopCartDTO.getUserId())
|
.eq("goods_id",comShopCartDTO.getGoodsId()).eq("goods_attr_id",comShopCartDTO.getGoodsAttrId()));
|
if(shopCartDO == null){//购物车表里未有该商品的记录
|
shopCartDO = new ComShopCartDO();
|
shopCartDO.setUserId(comShopCartDTO.getUserId());
|
shopCartDO.setAmount(comShopCartDTO.getNumber());
|
//查询商品
|
ComShopGoodsDO shopGoodsDO = shopGoodsDAO.selectById(comShopCartDTO.getGoodsId());
|
if(shopGoodsDO == null){
|
return R.fail("未查询到商品信息");
|
}
|
shopCartDO.setGoodsName(shopGoodsDO.getName());
|
shopCartDO.setGoodsId(shopGoodsDO.getId());
|
shopCartDO.setStoreId(shopGoodsDO.getStoreId());
|
//查询商品规格
|
ComShopGoodsAttrDO shopGoodsAttrDO = shopGoodsAttrDAO.selectById(comShopCartDTO.getGoodsAttrId());
|
if(shopGoodsAttrDO == null){
|
return R.fail("未查询到商品规格");
|
}
|
shopCartDO.setGoodsAttrId(shopGoodsAttrDO.getId());
|
shopCartDO.setGoodsAttr(shopGoodsAttrDO.getGoodsAttr());
|
shopCartDO.setGoodsAttrPic(shopGoodsAttrDO.getAttrPic());
|
shopCartDO.setPrice(shopGoodsAttrDO.getPrice());
|
//执行添加操作
|
if(this.baseMapper.insert(shopCartDO) > 0){
|
return R.ok();
|
}else{
|
return R.fail("添加购物车失败");
|
}
|
}else{//购物车已有该商品
|
shopCartDO.setAmount(shopCartDO.getAmount() + comShopCartDTO.getNumber());
|
//执行修改操作
|
if(this.baseMapper.updateById(shopCartDO) > 0){
|
return R.ok();
|
}else{
|
return R.fail("添加购物车失败");
|
}
|
}
|
}
|
|
/**
|
* 购物车内商品数量修改
|
* @param shopEditNubCartDTO 请求参数
|
* @return 修改结果
|
*/
|
@Override
|
public R shopEditNubCart(ComShopEditNubCartDTO shopEditNubCartDTO){
|
//查询购物车记录
|
ComShopCartDO shopCartDO = this.baseMapper.selectById(shopEditNubCartDTO.getCartId());
|
if(shopCartDO == null){
|
return R.fail("未查询到购物车记录");
|
}
|
shopCartDO.setAmount(shopEditNubCartDTO.getNumber());
|
//执行修改操作
|
if(this.baseMapper.updateById(shopCartDO) > 0){
|
return R.ok();
|
}else{
|
return R.fail("修改失败");
|
}
|
}
|
|
/**
|
* 删除购物车记录
|
* @param Ids 购物车id集合
|
* @return 删除结果
|
*/
|
public R shopDelCart(List<Long> Ids){
|
if(this.baseMapper.deleteBatchIds(Ids) > 0){
|
return R.ok();
|
}else{
|
return R.fail("修改失败");
|
}
|
}
|
}
|