|
|
@@ -5,25 +5,35 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.exception.CustomException;
|
|
|
+import com.fs.common.exception.ServiceException;
|
|
|
import com.fs.his.domain.FsIntegralCart;
|
|
|
import com.fs.his.domain.FsIntegralGoods;
|
|
|
import com.fs.his.domain.FsUser;
|
|
|
+import com.fs.his.domain.FsUserAddress;
|
|
|
import com.fs.his.mapper.FsIntegralCartMapper;
|
|
|
import com.fs.his.mapper.FsIntegralGoodsMapper;
|
|
|
+import com.fs.his.mapper.FsUserAddressMapper;
|
|
|
import com.fs.his.mapper.FsUserMapper;
|
|
|
+import com.fs.his.param.AddGoodsIntoCartParam;
|
|
|
+import com.fs.his.param.GetFsIntegralCartDetailsParm;
|
|
|
+import com.fs.his.param.GetFsIntegralCartListParam;
|
|
|
import com.fs.his.service.IFsIntegralCartService;
|
|
|
import com.fs.his.vo.FsIntegralCartVO;
|
|
|
+import com.fs.his.vo.GetCartGoodsDetailsVo;
|
|
|
+import com.fs.his.vo.GetFsIntegralCartDetailsVo;
|
|
|
+import com.fs.his.vo.GetFsIntegralCartListVo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.validation.constraints.NotNull;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -34,6 +44,12 @@ public class FsIntegralCartServiceImpl extends ServiceImpl<FsIntegralCartMapper,
|
|
|
@Resource
|
|
|
private FsUserMapper userMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private FsUserAddressMapper fsUserAddressMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FsIntegralGoodsMapper fsIntegralGoodsMapper;
|
|
|
+
|
|
|
/**
|
|
|
* 添加或修改购物车
|
|
|
*
|
|
|
@@ -128,4 +144,90 @@ public class FsIntegralCartServiceImpl extends ServiceImpl<FsIntegralCartMapper,
|
|
|
params.put("ids", ids);
|
|
|
return baseMapper.getCartsByMap(params);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<GetFsIntegralCartListVo> getFsIntegralCartList(GetFsIntegralCartListParam param, Long aLong) {
|
|
|
+ return baseMapper.getFsIntegralCartList(param, aLong, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户购物车详情页
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @param userId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public GetFsIntegralCartDetailsVo getFsIntegralCartDetails(GetFsIntegralCartDetailsParm param, Long userId) {
|
|
|
+ List<GetFsIntegralCartListVo> cartListVoList = baseMapper.getFsIntegralCartList(new GetFsIntegralCartListParam(), userId, param.getCartId());
|
|
|
+ GetFsIntegralCartDetailsVo vos = new GetFsIntegralCartDetailsVo();
|
|
|
+ ArrayList<GetCartGoodsDetailsVo> goodsDetailsVos = new ArrayList<>();
|
|
|
+ for (GetFsIntegralCartListVo listVo : cartListVoList) {
|
|
|
+ GetCartGoodsDetailsVo cartListVo = new GetCartGoodsDetailsVo();
|
|
|
+ BeanUtils.copyProperties(listVo, cartListVo);
|
|
|
+ cartListVo.setGoodsIntegralTotal(listVo.getGoodsIntegral() * listVo.getQuantity());
|
|
|
+ goodsDetailsVos.add(cartListVo);
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(param.getAddressId())) {
|
|
|
+ FsUserAddress address = fsUserAddressMapper.selectFsUserAddressByAddressId(param.getAddressId());
|
|
|
+ vos.setUserName(address.getRealName());
|
|
|
+ vos.setUserAddress(address.getProvince() + address.getCity() + address.getDistrict() + address.getDetail());
|
|
|
+ vos.setUserPhone(address.getPhone());
|
|
|
+ vos.setAddressId(address.getAddressId());
|
|
|
+ }
|
|
|
+ vos.setUserId(cartListVoList.get(0).getUserId());
|
|
|
+ vos.setUserIntegral(cartListVoList.get(0).getUserIntegral());
|
|
|
+ vos.setGoodsIntegralTotal(goodsDetailsVos.stream().filter(n -> ObjectUtils.isNotEmpty(n.getGoodsIntegralTotal())).mapToLong(GetCartGoodsDetailsVo::getGoodsIntegralTotal).sum());
|
|
|
+ vos.setGoodsDetailsVoList(goodsDetailsVos);
|
|
|
+ return vos;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean addGoodsIntoCart(AddGoodsIntoCartParam param, Long userId) {
|
|
|
+ // 查询现有购物车项
|
|
|
+ FsIntegralCart existingCart = baseMapper.selectOne(Wrappers.<FsIntegralCart>lambdaQuery().eq(FsIntegralCart::getUserId, userId).eq(FsIntegralCart::getGoodsId, param.getGoodsId()));
|
|
|
+ int finalQuantity;
|
|
|
+ // 查询商品最新状态
|
|
|
+ FsIntegralGoods freshGoods = fsIntegralGoodsMapper.selectFsIntegralGoodsByGoodsId(param.getGoodsId());
|
|
|
+ if (freshGoods == null || freshGoods.getStatus() != 1) {
|
|
|
+ try {
|
|
|
+ int deleteCount = baseMapper.delete(Wrappers.<FsIntegralCart>lambdaQuery().eq(FsIntegralCart::getUserId, userId).eq(FsIntegralCart::getGoodsId, param.getGoodsId()));
|
|
|
+ log.info("清除下架商品,userId:{}, goodsId:{},清除数量:{}", userId, param.getGoodsId(), deleteCount);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("清除下架商品失败,userId:{}, goodsId:{}", userId, param.getGoodsId(), e);
|
|
|
+ }
|
|
|
+ throw new ServiceException(String.format("商品[名称:%d]不存在或已下架", freshGoods.getGoodsName()));
|
|
|
+ }
|
|
|
+ int maxAllowed = Math.min((int) freshGoods.getStock().longValue(), 99);
|
|
|
+ if (existingCart != null) {
|
|
|
+ // 先做校验
|
|
|
+ finalQuantity = existingCart.getCartNum() + param.getQuantity();
|
|
|
+ if (finalQuantity > maxAllowed) {
|
|
|
+ if (existingCart.getCartNum() >= maxAllowed) {
|
|
|
+ throw new ServiceException(String.format("商品已达最大购买数量%d件", maxAllowed));
|
|
|
+ }
|
|
|
+ int canAdd = maxAllowed - existingCart.getCartNum();
|
|
|
+ throw new ServiceException(String.format("最多还能购买%d件", canAdd));
|
|
|
+ }
|
|
|
+ int affectedRows = baseMapper.updateQuantityAtomically(userId, param.getGoodsId(), param.getQuantity(), maxAllowed);
|
|
|
+ if (affectedRows == 0) {
|
|
|
+ throw new ServiceException("添加失败,请重试");
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ // 新增购物车
|
|
|
+ if (param.getQuantity() > maxAllowed && param.getQuantity() > freshGoods.getStock()) {
|
|
|
+ throw new ServiceException(String.format("最多可购买%d件", freshGoods.getStock()));
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ FsIntegralCart newCart = FsIntegralCart.builder().userId(userId).goodsId(param.getGoodsId()).cartNum(param.getQuantity()).isSelected(param.getIsSelected()).build();
|
|
|
+ return this.save(newCart);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ log.info("购物车并发插入,转为更新,userId:{}, goodsId:{}", userId, param.getGoodsId());
|
|
|
+ int affectedRows = baseMapper.updateQuantityAtomically(userId, param.getGoodsId(), param.getQuantity(), maxAllowed);
|
|
|
+ return affectedRows > 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|