emoticon_api/ARW.Service/Api/BusinessService/Carts/CartServiceApi.cs

268 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Infrastructure.Attribute;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ARW.Model;
using ARW.Repository;
using ARW.Repository.Business.Carts;
using ARW.Service.Api.IBusinessService.Carts;
using ARW.Model.Dto.Api.Carts;
using ARW.Model.Models.Business.Carts;
using ARW.Model.Vo.Api.Carts;
using ARW.Model.Models.Business.Custom.Customers;
using ARW.Model.Models.Business.GoodsManager.Goodss;
using ARW.Model.Models.Business.ShopManager.Shops;
using Org.BouncyCastle.Crypto.Prng;
using ARW.Repository.Business.ShopManager.Shops;
using ARW.Repository.Business.Marketing.CouponManage.Coupons;
using ARW.Repository.Business.Marketing.CouponManage.CustomerCoupons;
using ARW.Repository.Business.GoodsManager.Goodss;
using ARW.Repository.Business.GoodsManager.GoodsSpecs.GoodsSkus;
using ARW.Repository.Business.GoodsManager.GoodsSpecs.Specs;
using ARW.Repository.Business.GoodsManager.GoodsSpecs.SpecValues;
using ARW.Repository.Business.GoodsManager.GoodsSpecs.GoodsSpecRels;
using Senparc.Weixin.MP.AdvancedAPIs.MerChant;
using Infrastructure;
using ARW.Model.Models.Business.Marketing.CouponManage.CustomerCoupons;
using ARW.Model.Models.Business.Marketing.CouponManage.Coupons;
namespace ARW.Service.Api.BusinessService.Carts
{
/// <summary>
/// 购物车记录接口实现类Api
///
/// @author lwh
/// @date 2023-07-20
/// </summary>
[AppService(ServiceType = typeof(ICartServiceApi), ServiceLifetime = LifeTime.Transient)]
public class CartServiceImplApi : BaseService<Cart>, ICartServiceApi
{
private readonly CartRepository _CartRepository;
private readonly ShopRepository _ShopRepository;
private readonly CouponRepository _CouponRepository;
private readonly CustomerCouponRepository _CustomerCouponRepository;
private readonly GoodsRepository _GoodsRepository;
private readonly GoodsSkuRepository _GoodsSkuRepository;
private readonly SpecRepository _SpecRepository;
private readonly SpecValueRepository _SpecValueRepository;
public CartServiceImplApi(CartRepository CartRepository, ShopRepository shopRepository, CustomerCouponRepository customerCouponRepository, CouponRepository couponRepository, GoodsRepository goodsRepository, GoodsSkuRepository goodsSkuRepository, SpecRepository specRepository, SpecValueRepository specValueRepository)
{
this._CartRepository = CartRepository;
_ShopRepository = shopRepository;
_CustomerCouponRepository = customerCouponRepository;
_CouponRepository = couponRepository;
_GoodsRepository = goodsRepository;
_GoodsSkuRepository = goodsSkuRepository;
_SpecRepository = specRepository;
_SpecValueRepository = specValueRepository;
}
#region Api接口代码
/// <summary>
/// 查询购物车记录列表(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<CartListVoApi> GetCartListApi(CartQueryDtoApi parm)
{
// 找到当前客户的所有购物车列表
var customerCartList = await _CartRepository.GetListAsync(s => s.CustomerGuid == parm.CustomerGuid);
// 找到店铺的Guids
var sameShopGuids = customerCartList.Select(item => item.ShopGuid).Distinct().ToList();
var res = new List<CartVoApi>();
// 循环每个店铺中的购物车记录
foreach (var item in sameShopGuids)
{
var shop = await _ShopRepository.GetFirstAsync(s => s.ShopGuid == item);
var shopCart = new CartVoApi();
shopCart.StoreId = shop.ShopGuid;
shopCart.StoreName = shop.ShopName;
var promotionGoodsList = new List<PromotionGoods>();
// 查找当前客户可使用的优惠券
var couponList = await _CustomerCouponRepository.GetListAsync(s => s.CustomerGuid == parm.CustomerGuid && s.CustomerCouponIsUsed == 1 && s.CustomerCouponIsExpired == 1);
// 如果有优惠券
if (couponList.Count() > 0)
{
foreach (var customerCoupon in couponList)
{
var promotionGoods = new PromotionGoods();
var coupon = await _CouponRepository.GetFirstAsync(s => s.CouponGuid == customerCoupon.CouponGuid);
promotionGoods.PromotionId = coupon.CouponId;
promotionGoods.Title = coupon.CouponName;
promotionGoods.PromotionCode = "MERCHANT";
if (coupon.CouponType == 1)
promotionGoods.Tag = "满减";
else
promotionGoods.Tag = "折扣";
promotionGoods.Description = coupon.CouponDesc;
// 情况1全部商品的通用券
if (coupon.CouponApplicableScope == 1)
{
var goodsPromotionList = await GetGoodsPromotionList(customerCartList, shop.ShopGuid);
promotionGoods.GoodsPromotionList = goodsPromotionList;
promotionGoodsList.Add(promotionGoods);
}
// // 情况2指定商品的券
if (coupon.CouponApplicableScope == 2)
{
// 根据优惠券指定的商品查找购物车中是否有对应的商品
if (string.IsNullOrEmpty(coupon.CouponGoodsIds)) throw new CustomException("该优惠券还未指定商品");
var goodsIds = coupon.CouponGoodsIds.Split(',');
var hasCouponGoodsList = new List<Cart>();
var notHasCouponGoodsList = new List<Cart>();
foreach (var id in goodsIds)
{
var couponGoods = await _GoodsRepository.GetFirstAsync(s => s.GoodsId == Convert.ToInt32(id));
foreach (var cart in customerCartList)
{
if (cart.GoodsGuid == couponGoods.GoodsGuid)
{
hasCouponGoodsList.Add(cart);
}
else
{
notHasCouponGoodsList.Add(cart);
}
}
}
var goodsPromotionList = await GetGoodsPromotionList(hasCouponGoodsList, shop.ShopGuid);
promotionGoods.GoodsPromotionList = goodsPromotionList;
if(promotionGoods.GoodsPromotionList.Count() > 0)
{
promotionGoodsList.Add(promotionGoods);
}
var NotCouponPromotionGoods = new PromotionGoods();
NotCouponPromotionGoods.GoodsPromotionList = await GetGoodsPromotionList(notHasCouponGoodsList, shop.ShopGuid); ;
promotionGoodsList.Add(NotCouponPromotionGoods);
}
}
}
else // 如果没有优惠券
{
var promotionGoods = new PromotionGoods();
var goodsPromotionList = await GetGoodsPromotionList(customerCartList, shop.ShopGuid);
promotionGoods.GoodsPromotionList = goodsPromotionList;
promotionGoodsList.Add(promotionGoods);
}
shopCart.PromotionGoodsList = promotionGoodsList;
res.Add(shopCart);
}
var cartList = new CartListVoApi();
if(res.Count() > 0)
{
cartList.IsNotEmpty = true;
cartList.StoreGoods = res;
}
return cartList;
}
private async Task<SpecInfo> GetSpecInfo(int value)
{
var specInfo = new SpecInfo();
var sepcValue = await _SpecValueRepository.GetFirstAsync(s => s.SpecValueId == value);
var sepc = await _SpecRepository.GetFirstAsync(s => s.SpecId == sepcValue.SpecId);
specInfo.SpecTitle = sepc.SpecName;
specInfo.SpecValue = sepcValue.SpecValueName;
return specInfo;
}
/// <summary>
/// 获取购物车商品信息
/// </summary>
/// <param name="customerCartList">当前客户的购物车列表</param>
/// <param name="shopGuid">店铺guid</param>
/// <param name="type">购物车类型</param>
/// <returns></returns>
/// <exception cref="CustomException"></exception>
private async Task<List<GoodsPromotion>> GetGoodsPromotionList(List<Cart> customerCartList, long shopGuid)
{
var goodsPromotionList = new List<GoodsPromotion>();
foreach (var _item in customerCartList)
{
var goodsPromotion = new GoodsPromotion();
if (_item.ShopGuid == shopGuid)
{
var goods = new Goods();
goods = await _GoodsRepository.GetFirstAsync(s => s.GoodsGuid == _item.GoodsGuid);
if (goods.GoodsId != 0)
{
goodsPromotion.StoreId = goods.ShopGuid;
goodsPromotion.SpuId = goods.GoodsGuid;
goodsPromotion.SkuId = _item.GoodsSkuId;
goodsPromotion.Thumb = goods.GoodsPicture.Split(',').First();
goodsPromotion.Title = goods.GoodsName;
goodsPromotion.Quantity = _item.CartGoodsNum;
goodsPromotion.StockStatus = goods.GoodsTotalInventory > _item.CartGoodsNum;
goodsPromotion.StockQuantity = goods.GoodsTotalInventory;
// 查找当前规格的价格
var sku = await _GoodsSkuRepository.GetFirstAsync(s => s.GoodsSkuId == _item.GoodsSkuId);
if (sku == null) throw new CustomException("所选规格不存在,请重新挑选");
goodsPromotion.Price = sku.GoodsSkuPrice;
goodsPromotion.OriginPrice = sku.GoodsSkuLinePrice;
// 查找当前规格的详细信息
var specInfoList = new List<SpecInfo>();
if (sku.SpecValueId != 0)
{
var specInfo = await GetSpecInfo(sku.SpecValueId);
specInfoList.Add(specInfo);
if (sku.SpecSecondValueId != 0)
{
var specInfo2 = await GetSpecInfo(sku.SpecSecondValueId);
specInfoList.Add(specInfo2);
}
if (sku.SpecThirdValueId != 0)
{
var specInfo3 = await GetSpecInfo(sku.SpecThirdValueId);
specInfoList.Add(specInfo3);
}
}
goodsPromotion.SpecInfo = specInfoList;
goodsPromotion.JoinCartTime = _item.Create_time;
goodsPromotionList.Add(goodsPromotion);
}
}
}
return goodsPromotionList;
}
#endregion
}
}