352 lines
15 KiB
C#
352 lines
15 KiB
C#
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接口代码
|
||
|
||
#region 查询
|
||
|
||
/// <summary>
|
||
/// 查询购物车记录列表(Api)
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
public async Task<CartListVoApi> GetCartListApi(CartQueryDtoApi parm)
|
||
{
|
||
// 找到当前客户的所有购物车列表
|
||
var customerCartList = await _CartRepository.Queryable()
|
||
.Where(s => s.CustomerGuid == parm.CustomerGuid)
|
||
.OrderBy(s => s.Create_time, OrderByType.Desc)
|
||
.ToListAsync();
|
||
|
||
// 找到店铺的Guids
|
||
var sameShopGuids = customerCartList
|
||
.OrderByDescending(s => s.Create_time)
|
||
.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)
|
||
// {
|
||
// // 从优惠券中获取指定的商品ID列表
|
||
// if (string.IsNullOrEmpty(coupon.CouponGoodsIds))
|
||
// {
|
||
// throw new CustomException("该优惠券还未指定商品");
|
||
// }
|
||
|
||
// var goodsGuids = coupon.CouponGoodsIds.Split(',')
|
||
// .Select(id => Convert.ToInt32(id))
|
||
// .Select(goodsId => _GoodsRepository.GetFirstAsync(s => s.GoodsId == goodsId))
|
||
// .Select(task => task.Result.GoodsGuid)
|
||
// .ToList();
|
||
|
||
// var hasCouponGoodsList = new List<Cart>();
|
||
// var notHasCouponGoodsList = new List<Cart>();
|
||
|
||
// // 根据优惠券指定的商品分组购物车中的商品
|
||
// foreach (var cart in customerCartList)
|
||
// {
|
||
// foreach (var goodsGuid in goodsGuids)
|
||
// {
|
||
// if (cart.GoodsGuid == goodsGuid)
|
||
// {
|
||
// hasCouponGoodsList.Add(cart);
|
||
// }
|
||
// }
|
||
// }
|
||
// promotionGoods.GoodsPromotionList = await GetGoodsPromotionList(hasCouponGoodsList, shop.ShopGuid);
|
||
// if (promotionGoods.GoodsPromotionList.Count() > 0)
|
||
// {
|
||
// promotionGoodsList.Add(promotionGoods);
|
||
// }
|
||
// notHasCouponGoodsList = customerCartList.Except(hasCouponGoodsList).ToList();
|
||
|
||
// // 处理未包含在优惠券指定商品列表中的商品
|
||
// var NotCouponPromotionGoods = new PromotionGoods();
|
||
// NotCouponPromotionGoods.GoodsPromotionList = await GetGoodsPromotionList(notHasCouponGoodsList, shop.ShopGuid);
|
||
// if (NotCouponPromotionGoods.GoodsPromotionList.Count() > 0)
|
||
// {
|
||
// 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();
|
||
|
||
decimal totalAmount = res
|
||
.SelectMany(item => item.PromotionGoodsList)
|
||
.SelectMany(item1 => item1.GoodsPromotionList)
|
||
.Where(item2 => item2.IsSelected)
|
||
.Sum(item2 => item2.Price * item2.Quantity);
|
||
|
||
int selectedGoodsCount = res
|
||
.SelectMany(item => item.PromotionGoodsList)
|
||
.SelectMany(item1 => item1.GoodsPromotionList)
|
||
.Count(item2 => item2.IsSelected);
|
||
|
||
cartList.TotalAmount = totalAmount;
|
||
cartList.SelectedGoodsCount = selectedGoodsCount;
|
||
|
||
if (res.Count() > 0)
|
||
{
|
||
// 判断是否全选购物车
|
||
cartList.IsAllSelected = customerCartList.All(cart => cart.IsSelected);
|
||
cartList.IsNotEmpty = true;
|
||
cartList.StoreGoods = res;
|
||
}
|
||
|
||
return cartList;
|
||
|
||
}
|
||
|
||
|
||
/// <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 == null) throw new CustomException("所选商品不存在,请重新挑选");
|
||
|
||
|
||
|
||
if (goods.GoodsId != 0)
|
||
{
|
||
goodsPromotion.CartId = _item.CartId;
|
||
goodsPromotion.IsSelected = _item.IsSelected;
|
||
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;
|
||
|
||
// 查找当前规格的价格
|
||
if (_item.GoodsSkuId != 0)
|
||
{
|
||
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;
|
||
}
|
||
else
|
||
{
|
||
goodsPromotion.Price = goods.GoodsPriceHighest;
|
||
goodsPromotion.OriginPrice = goods.GoodsDashedPriceHighest;
|
||
}
|
||
|
||
goodsPromotion.JoinCartTime = _item.Create_time;
|
||
goodsPromotionList.Add(goodsPromotion);
|
||
}
|
||
|
||
}
|
||
}
|
||
return goodsPromotionList;
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
/// <summary>
|
||
/// 修改购物车商品数量
|
||
/// </summary>
|
||
public async Task<string> UpdateCartGoodsNum(CartGoodsNumDto parm)
|
||
{
|
||
var response = await _CartRepository.UpdateAsync(f => new Cart
|
||
{
|
||
CartGoodsNum = parm.CartGoodsNum
|
||
}, s => s.CartId == parm.CartId);
|
||
return "修改成功!";
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 修改购物车商品选中状态
|
||
/// </summary>
|
||
public async Task<string> UpdateCartGoodsSelect(CartGoodsSelectDto parm)
|
||
{
|
||
var response = await _CartRepository.UpdateAsync(f => new Cart
|
||
{
|
||
IsSelected = parm.IsSelected
|
||
}, s => s.CartId == parm.CartId);
|
||
return "修改成功!";
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 全选购物车商品
|
||
/// </summary>
|
||
public async Task<string> SelectAllCartGoods(CartGoodsSelectDto parm)
|
||
{
|
||
var response = await _CartRepository.UpdateAsync(f => new Cart
|
||
{
|
||
IsSelected = parm.IsSelected
|
||
}, s => s.CustomerGuid == parm.CustomerGuid && s.IsDelete == false);
|
||
return "修改成功!";
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
}
|
||
}
|