package com.ruoyi.order.service.impl;
|
|
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.account.api.feignClient.AppUserClient;
|
import com.ruoyi.account.api.feignClient.UserAddressClient;
|
import com.ruoyi.account.api.feignClient.UserCouponClient;
|
import com.ruoyi.account.api.model.AppUser;
|
import com.ruoyi.account.api.model.UserAddress;
|
import com.ruoyi.account.api.vo.CouponInfoVo;
|
import com.ruoyi.account.api.vo.PaymentUserCoupon;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.web.domain.AjaxResult;
|
import com.ruoyi.common.security.service.TokenService;
|
import com.ruoyi.order.mapper.ShoppingCartMapper;
|
import com.ruoyi.order.service.OrderGoodService;
|
import com.ruoyi.order.service.OrderService;
|
import com.ruoyi.order.service.ShoppingCartService;
|
import com.ruoyi.order.vo.*;
|
import com.ruoyi.other.api.domain.*;
|
import com.ruoyi.other.api.feignClient.*;
|
import com.ruoyi.other.api.vo.GetGoodsBargainPrice;
|
import com.ruoyi.other.api.vo.GetSeckillActivityInfo;
|
import io.swagger.annotations.ApiModelProperty;
|
import lombok.Data;
|
import model.Order;
|
import model.OrderGood;
|
import model.ShoppingCart;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
@Service
|
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
|
|
@Resource
|
private TokenService tokenService;
|
|
@Resource
|
private GoodsClient goodsClient;
|
|
@Resource
|
private GoodsShopClient goodsShopClient;
|
|
@Resource
|
private AppUserClient appUserClient;
|
|
@Resource
|
private GoodsAreaClient goodsAreaClient;
|
|
@Resource
|
private GoodsVipClient goodsVipClient;
|
|
@Resource
|
private SeckillActivityInfoClient seckillActivityInfoClient;
|
|
@Resource
|
private GoodsBargainPriceClient goodsBargainPriceClient;
|
|
@Resource
|
private OrderService orderService;
|
|
@Resource
|
private OrderGoodService orderGoodService;
|
|
@Resource
|
private ShopClient shopClient;
|
|
@Resource
|
private OrderActivityInfoClient orderActivityInfoClient;
|
|
@Resource
|
private BaseSettingClient baseSettingClient;
|
|
@Resource
|
private UserAddressClient userAddressClient;
|
|
@Resource
|
private UserCouponClient userCouponClient;
|
|
@Resource
|
private SystemConfigClient systemConfigClient;
|
|
|
|
|
|
|
/**
|
* 获取购物车列表
|
* @param type
|
* @param shopId
|
* @return
|
*/
|
@Override
|
public List<MyShoppingCartVo> getMyShoppingCart(Integer type, Integer shopId) {
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
AppUser appUser = appUserClient.getAppUserById(userid);
|
//获取对应类型的商品数据
|
List<Goods> data = goodsClient.getGoodsByType(type).getData();
|
if(null == data){
|
throw new RuntimeException("根据类型(1=服务商品,2=单品商品)获取商品数据失败");
|
}
|
List<Long> goodsIds = data.stream().map(Goods::getId).collect(Collectors.toList());
|
//查询符合商品类型的商品数据
|
List<ShoppingCart> list = this.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getAppUserId, userid).in(ShoppingCart::getGoodsId, goodsIds));
|
//构建返回数据
|
List<MyShoppingCartVo> page = buildDetail(appUser, shopId, list, null);
|
return page;
|
}
|
|
|
/**
|
* 获取支付价格
|
* @param appUser
|
* @param goodsId
|
* @param shopId
|
* @return
|
*/
|
public Price getPrice(AppUser appUser, Integer goodsId, Integer shopId){
|
//获取支付价格
|
//秒杀活动>门店特价>地区价格>会员价格
|
//判断是否有秒杀活动
|
Price price = new Price();
|
GetSeckillActivityInfo info = new GetSeckillActivityInfo();
|
info.setGoodsId(goodsId);
|
info.setVip(appUser.getVipId());
|
GoodsSeckill goodsSeckill = seckillActivityInfoClient.getSeckillActivityInfo(info).getData();
|
if(null == goodsSeckill){
|
//没有秒杀价,则判断门店特价
|
GetGoodsBargainPrice goodsBargainPrice = new GetGoodsBargainPrice();
|
goodsBargainPrice.setGoodsId(goodsId);
|
goodsBargainPrice.setVip(appUser.getVipId());
|
goodsBargainPrice.setShopId(shopId);
|
GoodsBargainPriceDetail bargainPriceDetail = goodsBargainPriceClient.getGoodsBargainPrice(goodsBargainPrice).getData();
|
if(null == bargainPriceDetail){
|
//没有门店特价,判断地区价格配置
|
GoodsArea area = new GoodsArea();
|
area.setDistrictsCode(appUser.getDistrictCode());
|
area.setCityCode(appUser.getCityCode());
|
area.setProvinceCode(appUser.getProvinceCode());
|
area.setVip(appUser.getVipId());
|
GoodsArea goodsArea = goodsAreaClient.getGoodsArea(area).getData();
|
if(null == goodsArea){
|
//没有地区价格,则使用会员价格
|
GoodsVip goodsVip = goodsVipClient.getGoodsVip(appUser.getVipId()).getData();
|
if(null == goodsVip){
|
//没有配置价格,直接使用原始基础价格
|
return null;
|
}else{
|
price.setCash(goodsVip.getSellingPrice());
|
price.setPoint(goodsVip.getIntegral());
|
price.setCashPayment(goodsVip.getCashPayment() == 1 ? true : false);
|
price.setPointPayment(goodsVip.getPointPayment() == 1 ? true : false);
|
price.setEarnSpendingPoints(goodsVip.getEarnSpendingPoints());
|
}
|
}else{
|
price.setCash(goodsArea.getSellingPrice());
|
price.setPoint(goodsArea.getIntegral());
|
price.setCashPayment(goodsArea.getCashPayment() == 1 ? true : false);
|
price.setPointPayment(goodsArea.getPointPayment() == 1 ? true : false);
|
price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
|
}
|
}else{
|
price.setCash(bargainPriceDetail.getSellingPrice());
|
price.setPoint(bargainPriceDetail.getIntegral());
|
price.setCashPayment(bargainPriceDetail.getSellingPrice() != null ? true : false);
|
price.setPointPayment(bargainPriceDetail.getIntegral() != null ? true : false);
|
//门店特价,消费积分使用会员等级的消费积分
|
GoodsArea area = new GoodsArea();
|
area.setDistrictsCode(appUser.getDistrictCode());
|
area.setCityCode(appUser.getCityCode());
|
area.setProvinceCode(appUser.getProvinceCode());
|
area.setVip(appUser.getVipId());
|
GoodsArea goodsArea = goodsAreaClient.getGoodsArea(area).getData();
|
price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
|
}
|
}else{
|
//构建价格数据
|
if(goodsSeckill.getCashPayment() == 1 && goodsSeckill.getPointPayment() == 1){
|
price.setCash(goodsSeckill.getSellingPrice());
|
price.setPoint(goodsSeckill.getIntegral());
|
}
|
if(goodsSeckill.getCashPayment() == 1 && goodsSeckill.getPointPayment() == 0){
|
price.setCash(goodsSeckill.getSellingPrice());
|
}
|
if(goodsSeckill.getCashPayment() == 0 && goodsSeckill.getPointPayment() == 1){
|
price.setPoint(goodsSeckill.getIntegral());
|
}
|
price.setCashPayment(goodsSeckill.getCashPayment() == 1 ? true : false);
|
price.setPointPayment(goodsSeckill.getPointPayment() == 1 ? true : false);
|
price.setEndTime(goodsSeckill.getEndTime());
|
price.setEarnSpendingPoints(goodsSeckill.getEarnSpendingPoints());
|
}
|
return price;
|
}
|
|
|
@Data
|
class Price {
|
/**
|
* 现金
|
*/
|
private BigDecimal cash;
|
/**
|
* 积分
|
*/
|
private Integer point;
|
/**
|
* 获取结束时间
|
*/
|
private Long endTime;
|
/**
|
* 现金支付
|
*/
|
private Boolean cashPayment;
|
/**
|
* 积分支付
|
*/
|
private Boolean pointPayment;
|
/**
|
* 可获得消费积分
|
*/
|
private Integer earnSpendingPoints;
|
}
|
|
|
@Override
|
public void addGoods(ShoppingCart shoppingCart) {
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
shoppingCart.setAppUserId(userid);
|
this.save(shoppingCart);
|
}
|
|
|
/**
|
* 修改购物车数量
|
* @param setGoodsNumber
|
* @return
|
*/
|
@Override
|
public R setGoodsNumber(SetGoodsNumber setGoodsNumber) {
|
if(0 >= setGoodsNumber.getNumber()){
|
return R.fail("修改数量不能小于等于0");
|
}
|
ShoppingCart shoppingCart = this.getById(setGoodsNumber.getId());
|
Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
|
if(null != goods.getPurchaseLimit() && -1 != goods.getPurchaseLimit() && goods.getPurchaseLimit() < setGoodsNumber.getNumber()){
|
return R.fail("修改数量不能大于限购数量");
|
}
|
if(null != shoppingCart){
|
shoppingCart.setNumber(setGoodsNumber.getNumber());
|
this.updateById(shoppingCart);
|
}
|
return R.ok();
|
}
|
|
|
/**
|
* 确认购物车订单
|
* @param confirmOrder
|
* @return
|
*/
|
@Override
|
public ConfirmOrderVo confirmOrder(ConfirmOrder confirmOrder) {
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
AppUser appUser = appUserClient.getAppUserById(userid);
|
Integer shopId = confirmOrder.getShopId();
|
Shop shop = shopClient.getShopById(shopId).getData();
|
String goodsJson = confirmOrder.getGoodsJson();
|
List<Long> ids = new ArrayList<>();
|
JSONArray objects = JSON.parseArray(goodsJson);
|
for (int i = 0; i < objects.size(); i++) {
|
Long id = objects.getJSONObject(i).getLong("id");
|
ids.add(id);
|
}
|
List<ShoppingCart> list = this.listByIds(ids);
|
ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
|
//构建商品明细列表
|
List<MyShoppingCartVo> goodsList = buildDetail(appUser, shopId, list, objects);
|
confirmOrderVo.setGoodsList(goodsList);
|
confirmOrderVo.setShopId(confirmOrder.getShopId());
|
confirmOrderVo.setShopName(shop.getName());
|
//现金支付
|
if(confirmOrder.getPaymentType() == 1){
|
BigDecimal bigDecimal = goodsList.stream().map(MyShoppingCartVo::getCash).reduce(BigDecimal::add).get();
|
confirmOrderVo.setOrderMoney(bigDecimal);
|
}else{
|
int sum = goodsList.stream().mapToInt(MyShoppingCartVo::getPoint).sum();
|
confirmOrderVo.setOrderPoint(sum);
|
}
|
//查询当前是否有订单活动
|
OrderActivityInfo orderActivityInfo = orderActivityInfoClient.getNowOrderActivityInfo(appUser.getVipId()).getData();
|
BigDecimal orderMoney = confirmOrderVo.getOrderMoney();
|
BigDecimal paymentMoney = orderMoney;
|
//满XX才打折,只有现金才能优惠
|
if(null != orderActivityInfo && confirmOrder.getPaymentType() == 1 && orderActivityInfo.getConditionAmount().compareTo(orderMoney) <= 0){
|
confirmOrderVo.setActivityName(orderActivityInfo.getActivityName());
|
paymentMoney = orderActivityInfo.getDiscount().divide(new BigDecimal(10)).multiply(orderMoney);
|
confirmOrderVo.setDiscountAmount(orderMoney.subtract(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
|
}
|
BaseSetting baseSetting = baseSettingClient.getBaseSetting(4).getData();
|
confirmOrderVo.setUseSimultaneously(baseSetting.getContent().equals("1") ? true : false);
|
int earnPoint = goodsList.stream().mapToInt(MyShoppingCartVo::getEarnSpendingPoints).sum();
|
confirmOrderVo.setEarnPoint(earnPoint);
|
//支付金额,订单金额-订单优惠
|
confirmOrderVo.setPayMoney(paymentMoney);
|
confirmOrderVo.setResidualPoint(appUser.getLavePoint().intValue());
|
//获取默认收货地址
|
UserAddress userAddress = userAddressClient.getDefaultUserAddress(userid).getData();
|
confirmOrderVo.setUserAddress(userAddress);
|
confirmOrderVo.setPaymentType(confirmOrder.getPaymentType());
|
//获取用户优惠券,用户全部优惠券,不能使用的需要标识出来置灰展示
|
PaymentUserCoupon paymentUserCoupon = new PaymentUserCoupon();
|
paymentUserCoupon.setUserId(userid);
|
paymentUserCoupon.setOrderMoney(orderMoney);
|
paymentUserCoupon.setType(confirmOrder.getType());
|
List<CouponInfoVo> data = userCouponClient.getPaymentUserCoupon(paymentUserCoupon).getData();
|
confirmOrderVo.setCoupon(data);
|
//获取快递策略
|
SystemConfig systemConfig = systemConfigClient.getSystemConfig(3).getData();
|
JSONObject jsonObject = JSON.parseObject(systemConfig.getContent());
|
confirmOrderVo.setExpressFee(jsonObject.getBigDecimal("expressFee"));
|
List<Integer> vip = jsonObject.getJSONArray("vip").toList(Integer.class);
|
//包邮条件(所有会员或者满足条件的会员)
|
if(vip.get(0) == -1 || vip.contains(appUser.getVipId())){
|
if(confirmOrder.getPaymentType() == 1){
|
//现金支付,支付金额满足包邮条件
|
BigDecimal cash = jsonObject.getBigDecimal("cash");
|
if(confirmOrderVo.getPayMoney().compareTo(cash) >= 0){
|
confirmOrderVo.setExpressFee(BigDecimal.ZERO);
|
}
|
}else{
|
//积分支付,支付积分是否满足包邮条件
|
Integer point = jsonObject.getInteger("point");
|
if(confirmOrderVo.getOrderPoint().compareTo(point) >= 0){
|
confirmOrderVo.setExpressFee(BigDecimal.ZERO);
|
}
|
}
|
}
|
return confirmOrderVo;
|
}
|
|
|
/**
|
* 构建购物车商品列表
|
* @param appUser
|
* @param shopId
|
* @param list
|
* @param objects
|
* @return
|
*/
|
private List<MyShoppingCartVo> buildDetail(AppUser appUser, Integer shopId, List<ShoppingCart> list, JSONArray objects){
|
List<MyShoppingCartVo> page = new ArrayList<>();
|
for (ShoppingCart shoppingCart : list) {
|
Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
|
MyShoppingCartVo vo = new MyShoppingCartVo();
|
vo.setId(shoppingCart.getId());
|
vo.setHomePicture(goods.getHomePagePicture());
|
vo.setName(goods.getName());
|
int num = shoppingCart.getNumber();
|
if(null != objects){
|
for (int i = 0; i < objects.size(); i++) {
|
Long id = objects.getJSONObject(i).getLong("id");
|
if(id.equals(shoppingCart.getId())){
|
num = objects.getJSONObject(i).getInteger("num");
|
break;
|
}
|
}
|
}
|
//获取支付价格
|
Price price = getPrice(appUser, shoppingCart.getGoodsId(), shopId);
|
if(null == price){
|
//使用商品的基础价格
|
price.setCash(1 == goods.getCashPayment() ? goods.getSellingPrice() : null);
|
price.setPoint(1 == goods.getPointPayment() ? goods.getIntegral() : null);
|
price.setCashPayment(goods.getCashPayment() == 1 ? true : false);
|
price.setPointPayment(goods.getPointPayment() == 1 ? true : false);
|
}
|
vo.setCash(price.getCash());
|
vo.setPoint(price.getPoint());
|
vo.setCashPayment(price.getCashPayment());
|
vo.setPointPayment(price.getPointPayment());
|
vo.setEndTime(price.getEndTime());
|
vo.setOriginalPrice(goods.getOriginalPrice().toString());
|
vo.setNumber(num);
|
GoodsShop goodsShop = new GoodsShop();
|
goodsShop.setGoodsId(shoppingCart.getGoodsId());
|
goodsShop.setShopId(shopId);
|
GoodsShop goodsShop1 = goodsShopClient.getGoodsShop(goodsShop).getData();
|
vo.setVerifiable(null == goodsShop1 ? false : true);
|
//判断当前数量是否已经超出限购数量(需要计算已经购买的数量)
|
if(null == goods.getPurchaseLimit() || -1 == goods.getPurchaseLimit()){
|
vo.setPurchaseLimit(false);
|
}else{
|
List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getAppUserId, appUser.getId()).eq(Order::getDelFlag, 0).in(Order::getOrderStatus, Arrays.asList(4, 8)));
|
List<Long> orderIds = orders.stream().map(Order::getId).collect(Collectors.toList());
|
int sum = 0;
|
if(orderIds.size() > 0){
|
List<OrderGood> orderGoodList = orderGoodService.list(new LambdaQueryWrapper<OrderGood>().in(OrderGood::getOrderId, orderIds)
|
.eq(OrderGood::getGoodsId, shoppingCart.getGoodsId()).eq(OrderGood::getDelFlag, 0));
|
sum = orderGoodList.stream().mapToInt(OrderGood::getNum).sum();
|
}
|
vo.setPurchaseLimit((num + sum) > goods.getPurchaseLimit() ? true : false);
|
}
|
vo.setEarnSpendingPoints(price.getEarnSpendingPoints());
|
vo.setDistributionMode(goods.getDistributionMode());
|
page.add(vo);
|
}
|
return page;
|
}
|
|
|
/**
|
* 购物车支付操作
|
* @param shoppingCartPayment
|
* @return
|
*/
|
@Override
|
public R shoppingCartPayment(ShoppingCartPayment shoppingCartPayment) {
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
AppUser appUser = appUserClient.getAppUserById(userid);
|
Integer shopId = shoppingCartPayment.getShopId();
|
Shop shop = shopClient.getShopById(shopId).getData();
|
String goodsJson = shoppingCartPayment.getGoodsJson();
|
List<Long> ids = new ArrayList<>();
|
JSONArray objects = JSON.parseArray(goodsJson);
|
for (int i = 0; i < objects.size(); i++) {
|
Long id = objects.getJSONObject(i).getLong("id");
|
ids.add(id);
|
}
|
List<ShoppingCart> list = this.listByIds(ids);
|
ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
|
//构建商品明细列表
|
List<MyShoppingCartVo> goodsList = buildDetail(appUser, shopId, list, objects);
|
|
|
|
|
return null;
|
}
|
}
|