mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2026-06-10 04:47:51 +08:00
添加项目文件。
This commit is contained in:
351
CoreCms.Net.Services/Promotion/CoreCmsCouponServices.cs
Normal file
351
CoreCms.Net.Services/Promotion/CoreCmsCouponServices.cs
Normal file
@@ -0,0 +1,351 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms
|
||||
* ProjectName: 核心内容管理系统
|
||||
* Web: https://www.corecms.net
|
||||
* Author: 大灰灰
|
||||
* Email: jianweie@163.com
|
||||
* CreateTime: 2021/1/31 21:45:10
|
||||
* Description: 暂无
|
||||
***********************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.IRepository;
|
||||
using CoreCms.Net.IRepository.UnitOfWork;
|
||||
using CoreCms.Net.IServices;
|
||||
using CoreCms.Net.Loging;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
using CoreCms.Net.Model.ViewModels.Basics;
|
||||
using CoreCms.Net.Model.ViewModels.UI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SqlSugar;
|
||||
|
||||
|
||||
namespace CoreCms.Net.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 优惠券表 接口实现
|
||||
/// </summary>
|
||||
public class CoreCmsCouponServices : BaseServices<CoreCmsCoupon>, ICoreCmsCouponServices
|
||||
{
|
||||
private readonly ICoreCmsCouponRepository _dal;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public CoreCmsCouponServices(IUnitOfWork unitOfWork, ICoreCmsCouponRepository dal, IServiceProvider serviceProvider)
|
||||
{
|
||||
this._dal = dal;
|
||||
_serviceProvider = serviceProvider;
|
||||
base.BaseDal = dal;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据优惠券编码取优惠券的信息,并判断是否可用
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="check"></param>
|
||||
public async Task<WebApiCallBack> CodeToInfo(string[] code, bool check = false)
|
||||
{
|
||||
return await _dal.ToInfo(code, check);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除核销多个优惠券
|
||||
/// </summary>
|
||||
/// <param name="couponCode">优惠券码</param>
|
||||
/// <param name="orderId">使用序列</param>
|
||||
/// <returns></returns>
|
||||
public async Task<WebApiCallBack> UsedMultipleCoupon(string[] couponCode, string orderId)
|
||||
{
|
||||
var res = new WebApiCallBack() { methodDescription = "删除核销多个优惠券" };
|
||||
//判断优惠券码能否有效
|
||||
var resCodeToInfo = await CodeToInfo(couponCode, true);
|
||||
if (resCodeToInfo.status == false)
|
||||
{
|
||||
return resCodeToInfo;
|
||||
}
|
||||
var dt = DateTime.Now;
|
||||
var doHasChange = await _dal.UpdateAsync(p => new CoreCmsCoupon() { isUsed = true, usedId = orderId, updateTime = dt },
|
||||
p => p.isUsed == false && couponCode.Contains(p.couponCode));
|
||||
if (doHasChange)
|
||||
{
|
||||
res.status = true;
|
||||
res.msg = "核销使用优惠券成功";
|
||||
res.data = couponCode;
|
||||
}
|
||||
else
|
||||
{
|
||||
res.status = false;
|
||||
res.msg = "核销使用优惠券失败";
|
||||
res.data = couponCode;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 我的优惠券
|
||||
/// </summary>
|
||||
/// <param name="userId">用户序列</param>
|
||||
/// <param name="promotionId">促销序列</param>
|
||||
/// <param name="display">优惠券状态编码</param>
|
||||
/// <param name="page">页码</param>
|
||||
/// <param name="limit">数量</param>
|
||||
public async Task<WebApiCallBack> GetMyCoupon(int userId, int promotionId = 0, string display = "all", int page = 1, int limit = 10)
|
||||
{
|
||||
|
||||
return await _dal.GetMyCoupon(userId, promotionId, display, page, limit);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户领取优惠券 插入数据
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="promotionId"></param>
|
||||
/// <param name="promotion"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<WebApiCallBack> AddData(int userId, int promotionId, CoreCmsPromotion promotion)
|
||||
{
|
||||
var jm = new WebApiCallBack();
|
||||
|
||||
var dtTime = DateTime.Now;
|
||||
var eTime = DateTime.Now;
|
||||
|
||||
if (promotion.effectiveDays > 0)
|
||||
{
|
||||
eTime = eTime.AddDays(promotion.effectiveDays);
|
||||
}
|
||||
if (promotion.effectiveHours > 0)
|
||||
{
|
||||
eTime = eTime.AddHours(promotion.effectiveHours);
|
||||
}
|
||||
var coupon = new CoreCmsCoupon
|
||||
{
|
||||
couponCode = GeneratePromotionCode()[0],
|
||||
promotionId = promotion.id,
|
||||
isUsed = false,
|
||||
userId = userId,
|
||||
createTime = dtTime,
|
||||
startTime = dtTime,
|
||||
endTime = eTime,
|
||||
remark = "接口领取"
|
||||
};
|
||||
|
||||
var bl = await _dal.InsertAsync(coupon) > 0;
|
||||
jm.status = bl;
|
||||
jm.msg = bl ? "领取成功" : "领取失败";
|
||||
|
||||
return jm;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 通过优惠券号领取优惠券
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="couponCode"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<WebApiCallBack> ReceiveCoupon(int userId, string couponCode)
|
||||
{
|
||||
using var container = _serviceProvider.CreateScope();
|
||||
var promotionServices = container.ServiceProvider.GetService<ICoreCmsPromotionServices>();
|
||||
|
||||
var jm = new WebApiCallBack();
|
||||
|
||||
var coupon = await _dal.QueryByClauseAsync(p => p.couponCode == couponCode);
|
||||
if (coupon == null)
|
||||
{
|
||||
jm.msg = "该优惠券不存在";
|
||||
return jm;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(coupon.usedId))
|
||||
{
|
||||
jm.msg = "该优惠券已被使用";
|
||||
return jm;
|
||||
}
|
||||
if (coupon.userId > 0)
|
||||
{
|
||||
jm.msg = "该优惠券已被其他人领取";
|
||||
return jm;
|
||||
}
|
||||
|
||||
coupon.userId = userId;
|
||||
var bl = await _dal.UpdateAsync(p => new CoreCmsCoupon() { userId = userId }, p => p.id == coupon.id);
|
||||
if (bl)
|
||||
{
|
||||
var promotion = await promotionServices.QueryByIdAsync(coupon.promotionId);
|
||||
if (promotion != null)
|
||||
{
|
||||
coupon.couponName = promotion.name;
|
||||
}
|
||||
}
|
||||
jm.status = bl;
|
||||
jm.msg = bl ? "领取成功" : "领取失败";
|
||||
jm.data = coupon;
|
||||
|
||||
return jm;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 生成优惠券code 方法
|
||||
/// </summary>
|
||||
/// <param name="noOfCodes">定义一个int类型的参数 用来确定生成多少个优惠码</param>
|
||||
/// <param name="excludeCodesArray">定义一个exclude_codes_array类型的数组</param>
|
||||
/// <param name="codeLength">定义一个code_length的参数来确定优惠码的长度</param>
|
||||
/// <returns></returns>
|
||||
public List<string> GeneratePromotionCode(int noOfCodes = 1, List<string> excludeCodesArray = null, int codeLength = 10)
|
||||
{
|
||||
char[] constant =
|
||||
{
|
||||
'0','1','2','3','4','5','6','7','8','9',
|
||||
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
|
||||
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
|
||||
};
|
||||
var promotionCodes = new List<string>(); //这个数组用来接收生成的优惠码
|
||||
Random rd = new Random();
|
||||
for (int i = 0; i < noOfCodes; i++)
|
||||
{
|
||||
var code = "";
|
||||
for (int j = 0; j < codeLength; j++)
|
||||
{
|
||||
code += constant[rd.Next(62)];
|
||||
}
|
||||
//如果生成的6位随机数不再我们定义的$promotion_codes函数里面
|
||||
if (!promotionCodes.Contains(code))
|
||||
{
|
||||
if (excludeCodesArray != null && excludeCodesArray.Any())
|
||||
{
|
||||
if (!excludeCodesArray.Contains(code))
|
||||
{
|
||||
promotionCodes.Add(code);//将优惠码赋值给数组
|
||||
}
|
||||
else
|
||||
{
|
||||
i--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
promotionCodes.Add(code);//将优惠码赋值给数组
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return promotionCodes;
|
||||
}
|
||||
|
||||
|
||||
#region 根据条件查询分页数据及导航数据
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件查询分页数据及导航数据
|
||||
/// </summary>
|
||||
/// <param name="predicate">判断集合</param>
|
||||
/// <param name="orderByType">排序方式</param>
|
||||
/// <param name="isToPage">是否分页</param>
|
||||
/// <param name="pageIndex">当前页面索引</param>
|
||||
/// <param name="pageSize">分布大小</param>
|
||||
/// <param name="orderByExpression"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IPageList<CoreCmsCoupon>> QueryPageMapperAsync(Expression<Func<CoreCmsCoupon, bool>> predicate,
|
||||
Expression<Func<CoreCmsCoupon, object>> orderByExpression, OrderByType orderByType, bool isToPage = false, int pageIndex = 1,
|
||||
int pageSize = 20)
|
||||
{
|
||||
|
||||
return await _dal.QueryPageMapperAsync(predicate, orderByExpression, orderByType, isToPage, pageIndex, pageSize);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 重写数据并获取相关
|
||||
/// </summary>
|
||||
/// <param name="predicate">判断集合</param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<CoreCmsCoupon>> QueryWithAboutAsync(Expression<Func<CoreCmsCoupon, bool>> predicate)
|
||||
{
|
||||
return await _dal.QueryWithAboutAsync(predicate);
|
||||
}
|
||||
|
||||
|
||||
#region 重写根据条件查询分页数据
|
||||
/// <summary>
|
||||
/// 重写根据条件查询分页数据
|
||||
/// </summary>
|
||||
/// <param name="predicate">判断集合</param>
|
||||
/// <param name="orderByType">排序方式</param>
|
||||
/// <param name="pageIndex">当前页面索引</param>
|
||||
/// <param name="pageSize">分布大小</param>
|
||||
/// <param name="orderByExpression"></param>
|
||||
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
|
||||
/// <returns></returns>
|
||||
public new async Task<IPageList<CoreCmsCoupon>> QueryPageAsync(Expression<Func<CoreCmsCoupon, bool>> predicate,
|
||||
Expression<Func<CoreCmsCoupon, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
|
||||
int pageSize = 20, bool blUseNoLock = false)
|
||||
{
|
||||
return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock);
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 获取 我的优惠券可用数量
|
||||
/// </summary>
|
||||
/// <param name="userId">用户序列</param>
|
||||
public async Task<int> GetMyCouponCount(int userId)
|
||||
{
|
||||
return await _dal.GetMyCouponCount(userId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region 优惠券返还
|
||||
/// <summary>
|
||||
/// 优惠券返还
|
||||
/// </summary>
|
||||
/// <param name="couponCodes">优惠券数组</param>
|
||||
public async Task<WebApiCallBack> CancelReturnCoupon(string couponCodes)
|
||||
{
|
||||
var jm = new WebApiCallBack();
|
||||
jm.code = 0;
|
||||
var bl = false;
|
||||
var ids = couponCodes.Split(",");
|
||||
var list = await _dal.QueryListByClauseAsync(p => ids.Contains(p.couponCode));
|
||||
if (list != null && list.Any())
|
||||
{
|
||||
var newList = new List<CoreCmsCoupon>();
|
||||
list.ForEach(p =>
|
||||
{
|
||||
var eTime = p.endTime.AddMinutes(1);
|
||||
newList.Add(new CoreCmsCoupon()
|
||||
{
|
||||
couponCode = GeneratePromotionCode()[0],
|
||||
promotionId = p.promotionId,
|
||||
isUsed = false,
|
||||
userId = p.userId,
|
||||
createTime = DateTime.Now,
|
||||
remark = "优惠券返还",
|
||||
startTime = p.startTime,
|
||||
endTime = eTime
|
||||
});
|
||||
});
|
||||
bl = await _dal.InsertAsync(newList) > 0;
|
||||
jm.status = bl;
|
||||
jm.msg = bl ? "返还成功" : "返还失败";
|
||||
}
|
||||
return jm;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms
|
||||
* ProjectName: 核心内容管理系统
|
||||
* Web: https://www.corecms.net
|
||||
* Author: 大灰灰
|
||||
* Email: jianweie@163.com
|
||||
* CreateTime: 2021/1/31 21:45:10
|
||||
* Description: 暂无
|
||||
***********************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.IRepository;
|
||||
using CoreCms.Net.IRepository.UnitOfWork;
|
||||
using CoreCms.Net.IServices;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
using CoreCms.Net.Model.ViewModels.UI;
|
||||
using CoreCms.Net.Model.ViewModels.DTO;
|
||||
using CoreCms.Net.Utility.Extensions;
|
||||
using CoreCms.Net.Utility.Helper;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
|
||||
namespace CoreCms.Net.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 促销条件表 接口实现
|
||||
/// </summary>
|
||||
public class CoreCmsPromotionConditionServices : BaseServices<CoreCmsPromotionCondition>, ICoreCmsPromotionConditionServices
|
||||
{
|
||||
private readonly ICoreCmsPromotionConditionRepository _dal;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ICoreCmsGoodsCategoryServices _goodsCategoryServices;
|
||||
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
|
||||
public CoreCmsPromotionConditionServices(IUnitOfWork unitOfWork
|
||||
, ICoreCmsPromotionConditionRepository dal
|
||||
, ICoreCmsGoodsCategoryServices goodsCategoryServices
|
||||
, IServiceProvider serviceProvider
|
||||
)
|
||||
{
|
||||
this._dal = dal;
|
||||
base.BaseDal = dal;
|
||||
_unitOfWork = unitOfWork;
|
||||
_goodsCategoryServices = goodsCategoryServices;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否满足条件
|
||||
/// </summary>
|
||||
/// <param name="conditionInfo"></param>
|
||||
/// <param name="cart"></param>
|
||||
/// <param name="promotionInfo"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> check(CoreCmsPromotionCondition conditionInfo, CartDto cart,
|
||||
CoreCmsPromotion promotionInfo)
|
||||
{
|
||||
if (string.IsNullOrEmpty(conditionInfo.parameters)) return false;
|
||||
|
||||
var getPromotionConditionType = SystemSettingDictionary.GetPromotionConditionType();
|
||||
var codeModel = getPromotionConditionType.Find(p => p.sKey == conditionInfo.code);
|
||||
if (codeModel != null)
|
||||
{
|
||||
//如果是订单促销就直接去判断促销条件,如果是商品促销,就循环订单明细
|
||||
JObject parameters = (JObject)JsonConvert.DeserializeObject(conditionInfo.parameters);
|
||||
|
||||
if (codeModel.sValue == "goods")
|
||||
{
|
||||
var key = false;
|
||||
foreach (var item in cart.list)
|
||||
{
|
||||
var type = 0;
|
||||
//判断是哪个规则,并且确认是否符合
|
||||
switch (conditionInfo.code)
|
||||
{
|
||||
case "GOODS_ALL":
|
||||
type = condition_GOODS_ALL(parameters,
|
||||
(int)item.products.goodsId, item.nums);
|
||||
break;
|
||||
case "GOODS_IDS":
|
||||
type = condition_GoodsIdS(parameters,
|
||||
(int)item.products.goodsId, item.nums);
|
||||
break;
|
||||
case "GOODS_CATS":
|
||||
type = await condition_GOODS_CATS(parameters, (int)item.products.goodsId, item.nums);
|
||||
break;
|
||||
case "GOODS_BRANDS":
|
||||
type = await condition_GOODS_BRANDS(parameters,
|
||||
(int)item.products.goodsId, item.nums);
|
||||
break;
|
||||
default:
|
||||
type = 0;
|
||||
break;
|
||||
}
|
||||
if (type > 0)
|
||||
{
|
||||
if (item.products.promotionList.ContainsKey(promotionInfo.id))
|
||||
{
|
||||
item.products.promotionList[promotionInfo.id].name = promotionInfo.name;
|
||||
item.products.promotionList[promotionInfo.id].type = type;
|
||||
}
|
||||
else
|
||||
{
|
||||
item.products.promotionList.Add(promotionInfo.id, new WxNameTypeDto()
|
||||
{
|
||||
name = promotionInfo.name,
|
||||
type = type
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
//只有选中的商品才算促销
|
||||
if (item.isSelect)
|
||||
{
|
||||
if (!key)
|
||||
{
|
||||
if (type == 2)
|
||||
{
|
||||
key = true;//针对某一条商品促销条件,循环购物车的所有商品,只要有一条满足要求就,算,就返回true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return key;
|
||||
}
|
||||
else if (codeModel.sValue == "order")
|
||||
{
|
||||
var type = condition_ORDER_FULL(parameters, cart);
|
||||
if (type > 0)
|
||||
{
|
||||
if (cart.promotionList.ContainsKey(promotionInfo.id))
|
||||
{
|
||||
cart.promotionList[promotionInfo.id].name = promotionInfo.name;
|
||||
cart.promotionList[promotionInfo.id].type = type;
|
||||
}
|
||||
//else
|
||||
//{
|
||||
// cart.promotionList.Add(type, new WxNameTypeDto()
|
||||
// {
|
||||
// name = promotionInfo.name,
|
||||
// type = type
|
||||
// });
|
||||
//}
|
||||
}
|
||||
if (type == 2)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (codeModel.sValue == "user")
|
||||
{
|
||||
var type = await condition_USER_GRADE(parameters, cart.userId);
|
||||
if (type == 2)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 在促销结果中,如果是商品促销结果,调用此方法,判断商品是否符合需求
|
||||
/// </summary>
|
||||
/// <param name="promotionId"></param>
|
||||
/// <param name="goodsId"></param>
|
||||
/// <param name="nums"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> goods_check(int promotionId, int goodsId, int nums = 1)
|
||||
{
|
||||
var conditionInfos = await _dal.QueryListByClauseAsync(p => p.promotionId == promotionId);
|
||||
var getPromotionConditionType = SystemSettingDictionary.GetPromotionConditionType();
|
||||
|
||||
foreach (var item in conditionInfos)
|
||||
{
|
||||
var codeModel = getPromotionConditionType.Find(p => p.sKey == item.code);
|
||||
if (codeModel != null && codeModel.sValue == "goods")
|
||||
{
|
||||
JObject parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
|
||||
var type = 0;
|
||||
//判断是哪个规则,并且确认是否符合
|
||||
switch (item.code)
|
||||
{
|
||||
case "GOODS_ALL":
|
||||
type = condition_GOODS_ALL(parameters, goodsId, nums);
|
||||
break;
|
||||
case "GOODS_IDS":
|
||||
type = condition_GoodsIdS(parameters, goodsId, nums);
|
||||
break;
|
||||
case "GOODS_CATS":
|
||||
type = await condition_GOODS_CATS(parameters, goodsId, nums);
|
||||
break;
|
||||
case "GOODS_BRANDS":
|
||||
type = await condition_GOODS_BRANDS(parameters, goodsId, nums);
|
||||
break;
|
||||
default:
|
||||
type = 0;
|
||||
break;
|
||||
}
|
||||
if (type != 2)
|
||||
{
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 因为计算过促销条件后啊,前面有些是满足条件的,所以,他们的type是2,后面有不满足条件的时候呢,要把前面满足条件的回滚成不满足条件的
|
||||
/// </summary>
|
||||
/// <param name="cart"></param>
|
||||
/// <param name="promotionInfo"></param>
|
||||
/// <returns></returns>
|
||||
public CartDto PromotionFalse(CartDto cart, CoreCmsPromotion promotionInfo)
|
||||
{
|
||||
switch (promotionInfo.type)
|
||||
{
|
||||
case (int)GlobalEnumVars.PromotionType.Promotion:
|
||||
//订单促销回滚
|
||||
if (cart.promotionList.ContainsKey(promotionInfo.id))
|
||||
{
|
||||
cart.promotionList[promotionInfo.id].name = promotionInfo.name;
|
||||
cart.promotionList[promotionInfo.id].type = 1;
|
||||
}
|
||||
//商品回滚
|
||||
foreach (var item in cart.list.Where(item => item.products.promotionList.ContainsKey(promotionInfo.id)))
|
||||
{
|
||||
item.products.promotionList[promotionInfo.id].name = promotionInfo.name;
|
||||
item.products.promotionList[promotionInfo.id].type = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return cart;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订单满XX金额时满足条件
|
||||
/// </summary>
|
||||
/// <param name="parameters">参数对象</param>
|
||||
/// <param name="cart"></param>
|
||||
/// <returns></returns>
|
||||
public int condition_ORDER_FULL(JObject parameters, CartDto cart)
|
||||
{
|
||||
if (!parameters.ContainsKey("money")) return 1;
|
||||
var objMoney = Convert.ToDecimal(parameters["money"]);
|
||||
return cart.amount >= objMoney ? 2 : 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 所有商品满足条件
|
||||
/// </summary>
|
||||
/// <param name="parameters">参数对象</param>
|
||||
/// <param name="goodsId">商品序列</param>
|
||||
/// <param name="nums">数量</param>
|
||||
/// <returns></returns>
|
||||
public int condition_GOODS_ALL(JObject parameters, int goodsId, int nums)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定某些商品满足条件
|
||||
/// </summary>
|
||||
/// <param name="parameters">参数对象</param>
|
||||
/// <param name="goodsId">商品序列</param>
|
||||
/// <param name="nums">数量</param>
|
||||
/// <returns></returns>
|
||||
public int condition_GoodsIdS(JObject parameters, int goodsId, int nums)
|
||||
{
|
||||
if (!parameters.ContainsKey("goodsId") || !parameters.ContainsKey("nums")) return 0;
|
||||
|
||||
var objNums = Convert.ToInt32(parameters["nums"]);
|
||||
|
||||
var goodsIds = CommonHelper.StringToIntArray(parameters["goodsId"].ObjectToString());
|
||||
|
||||
return goodsIds.Any() && goodsIds.Contains(goodsId) ? nums >= objNums ? 2 : 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 指定商品分类满足条件
|
||||
/// </summary>
|
||||
/// <param name="parameters">参数对象</param>
|
||||
/// <param name="goodsId">商品序列</param>
|
||||
/// <param name="nums">数量</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> condition_GOODS_CATS(JObject parameters, int goodsId, int nums)
|
||||
{
|
||||
|
||||
using (var container = _serviceProvider.CreateScope())
|
||||
{
|
||||
var goodsServices = container.ServiceProvider.GetService<ICoreCmsGoodsServices>();
|
||||
|
||||
if (!parameters.ContainsKey("catId") || !parameters.ContainsKey("nums")) return 0;
|
||||
var objCatId = parameters["catId"].ObjectToInt();
|
||||
var objNums = parameters["nums"].ObjectToInt();
|
||||
var goodsModel = await goodsServices.QueryByIdAsync(goodsId);
|
||||
|
||||
if (goodsModel == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return await _goodsCategoryServices.IsChild(objCatId, goodsModel.goodsCategoryId)
|
||||
? nums >= objNums ? 2 : 1
|
||||
: 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定商品品牌满足条件
|
||||
/// </summary>
|
||||
/// <param name="parameters">参数对象</param>
|
||||
/// <param name="goodsId">商品序列</param>
|
||||
/// <param name="nums">数量</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> condition_GOODS_BRANDS(JObject parameters, int goodsId, int nums)
|
||||
{
|
||||
using (var container = _serviceProvider.CreateScope())
|
||||
{
|
||||
var goodsServices = container.ServiceProvider.GetService<ICoreCmsGoodsServices>();
|
||||
|
||||
if (!parameters.ContainsKey("brandId") || !parameters.ContainsKey("nums")) return 0;
|
||||
var objBrandId = parameters["brandId"].ObjectToInt(0);
|
||||
var objNums = parameters["nums"].ObjectToInt(0);
|
||||
|
||||
var goodsModel = await goodsServices.QueryByIdAsync(goodsId);
|
||||
if (goodsModel == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return goodsModel.brandId == objBrandId ? nums >= objNums ? 2 : 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 指定用户等级满足条件
|
||||
/// </summary>
|
||||
/// <param name="parameters">参数对象</param>
|
||||
/// <param name="userId">用户序列</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> condition_USER_GRADE(JObject parameters, int userId)
|
||||
{
|
||||
using (var container = _serviceProvider.CreateScope())
|
||||
{
|
||||
var userServices = container.ServiceProvider.GetService<ICoreCmsUserServices>();
|
||||
|
||||
if (!parameters.ContainsKey("grades")) return 0;
|
||||
var userInfo = await userServices.QueryByIdAsync(userId);
|
||||
if (userInfo == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
var arr = CommonHelper.StringToIntArray(parameters["grades"].ObjectToString());
|
||||
if (arr.Contains(userInfo.grade))
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
126
CoreCms.Net.Services/Promotion/CoreCmsPromotionRecordServices.cs
Normal file
126
CoreCms.Net.Services/Promotion/CoreCmsPromotionRecordServices.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms
|
||||
* ProjectName: 核心内容管理系统
|
||||
* Web: https://www.corecms.net
|
||||
* Author: 大灰灰
|
||||
* Email: jianweie@163.com
|
||||
* CreateTime: 2021/1/31 21:45:10
|
||||
* Description: 暂无
|
||||
***********************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.IRepository;
|
||||
using CoreCms.Net.IRepository.UnitOfWork;
|
||||
using CoreCms.Net.IServices;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
using CoreCms.Net.Model.ViewModels.Basics;
|
||||
using CoreCms.Net.Model.ViewModels.UI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SqlSugar;
|
||||
|
||||
|
||||
namespace CoreCms.Net.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 促销活动记录表 接口实现
|
||||
/// </summary>
|
||||
public class CoreCmsPromotionRecordServices : BaseServices<CoreCmsPromotionRecord>, ICoreCmsPromotionRecordServices
|
||||
{
|
||||
private readonly ICoreCmsPromotionRecordRepository _dal;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
|
||||
|
||||
public CoreCmsPromotionRecordServices(IUnitOfWork unitOfWork, ICoreCmsPromotionRecordRepository dal, IServiceProvider serviceProvider)
|
||||
{
|
||||
this._dal = dal;
|
||||
_serviceProvider = serviceProvider;
|
||||
base.BaseDal = dal;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
|
||||
#region 生成订单的时候,增加信息
|
||||
|
||||
/// <summary>
|
||||
/// 生成订单的时候,增加信息
|
||||
/// </summary>
|
||||
/// <param name="order">订单数据</param>
|
||||
/// <param name="items">货品列表</param>
|
||||
/// <param name="groupId">秒杀团购序列</param>
|
||||
/// <param name="orderType">购物车类型</param>
|
||||
/// <returns></returns>
|
||||
public async Task<WebApiCallBack> OrderAdd(CoreCmsOrder order, List<CoreCmsOrderItem> items, int groupId,
|
||||
int orderType)
|
||||
{
|
||||
var jm = new WebApiCallBack();
|
||||
|
||||
using var container = _serviceProvider.CreateScope();
|
||||
|
||||
var orderServices = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
|
||||
var goodsServices = container.ServiceProvider.GetService<ICoreCmsGoodsServices>();
|
||||
|
||||
|
||||
var orderItem = items.FirstOrDefault();
|
||||
|
||||
//判断商品是否做团购秒杀
|
||||
if (goodsServices.IsInGroup((int)orderItem.goodsId, out var promotionsModel, groupId) == true)
|
||||
{
|
||||
jm.msg = "团购秒杀机制验证失败";
|
||||
|
||||
var checkOrder = orderServices.FindLimitOrder(orderItem.productId, order.userId, promotionsModel.startTime, promotionsModel.endTime, orderType);
|
||||
if (promotionsModel.maxGoodsNums > 0)
|
||||
{
|
||||
if (checkOrder.TotalOrders + 1 > promotionsModel.maxGoodsNums)
|
||||
{
|
||||
jm.data = 15610;
|
||||
jm.msg = GlobalErrorCodeVars.Code15610;
|
||||
return jm;
|
||||
}
|
||||
}
|
||||
if (promotionsModel.maxNums > 0)
|
||||
{
|
||||
if (checkOrder.TotalUserOrders > promotionsModel.maxNums)
|
||||
{
|
||||
jm.data = 15611;
|
||||
jm.msg = GlobalErrorCodeVars.Code15611;
|
||||
return jm;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var model = new CoreCmsPromotionRecord();
|
||||
model.promotionId = groupId;
|
||||
model.userId = order.userId;
|
||||
model.goodsId = orderItem.goodsId;
|
||||
model.productId = orderItem.productId;
|
||||
model.orderId = order.orderId;
|
||||
model.type = orderType;
|
||||
|
||||
var res = await _dal.InsertAsync(model) > 0;
|
||||
|
||||
if (res == false)
|
||||
{
|
||||
jm.data = 10004;
|
||||
jm.msg = GlobalErrorCodeVars.Code10004;
|
||||
return jm;
|
||||
}
|
||||
|
||||
jm.status = true;
|
||||
jm.msg = "团购秒杀机制验证通过";
|
||||
|
||||
|
||||
return jm;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
306
CoreCms.Net.Services/Promotion/CoreCmsPromotionResultServices.cs
Normal file
306
CoreCms.Net.Services/Promotion/CoreCmsPromotionResultServices.cs
Normal file
@@ -0,0 +1,306 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms
|
||||
* ProjectName: 核心内容管理系统
|
||||
* Web: https://www.corecms.net
|
||||
* Author: 大灰灰
|
||||
* Email: jianweie@163.com
|
||||
* CreateTime: 2021/1/31 21:45:10
|
||||
* Description: 暂无
|
||||
***********************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.IRepository;
|
||||
using CoreCms.Net.IRepository.UnitOfWork;
|
||||
using CoreCms.Net.IServices;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
using CoreCms.Net.Model.ViewModels.UI;
|
||||
using CoreCms.Net.Model.ViewModels.DTO;
|
||||
using CoreCms.Net.Utility.Extensions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
|
||||
namespace CoreCms.Net.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 促销结果表 接口实现
|
||||
/// </summary>
|
||||
public class CoreCmsPromotionResultServices : BaseServices<CoreCmsPromotionResult>, ICoreCmsPromotionResultServices
|
||||
{
|
||||
private readonly ICoreCmsPromotionResultRepository _dal;
|
||||
|
||||
private readonly ICoreCmsPromotionConditionServices _promotionConditionServices;
|
||||
|
||||
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
public CoreCmsPromotionResultServices(IUnitOfWork unitOfWork, ICoreCmsPromotionResultRepository dal, ICoreCmsPromotionConditionServices promotionConditionServices)
|
||||
{
|
||||
this._dal = dal;
|
||||
base.BaseDal = dal;
|
||||
_unitOfWork = unitOfWork;
|
||||
_promotionConditionServices = promotionConditionServices;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//去计算结果
|
||||
public async Task<bool> toResult(CoreCmsPromotionResult resultInfo, CartDto cart,
|
||||
CoreCmsPromotion promotionInfo)
|
||||
{
|
||||
if (string.IsNullOrEmpty(resultInfo.parameters)) return false;
|
||||
|
||||
var resultType = SystemSettingDictionary.GetPromotionResultType();
|
||||
var resultModel = resultType.Find(p => p.sKey == resultInfo.code);
|
||||
if (resultModel != null)
|
||||
{
|
||||
JObject parameters = (JObject)JsonConvert.DeserializeObject(resultInfo.parameters);
|
||||
//如果是订单促销就直接去判断促销条件,如果是商品促销,就循环订单明细
|
||||
if (resultModel.sValue == "goods")
|
||||
{
|
||||
foreach (var item in cart.list)
|
||||
{
|
||||
var type = await _promotionConditionServices.goods_check(promotionInfo.id, (int)item.products.goodsId, item.nums);
|
||||
if (type == 2)
|
||||
{
|
||||
//到这里就说明此商品信息满足促销商品促销信息的条件,去计算结果
|
||||
//注意,在明细上面,就不细分促销的种类了,都放到一个上面,在订单上面才细分
|
||||
decimal promotionModel = 0;
|
||||
switch (resultInfo.code)
|
||||
{
|
||||
case "GOODS_REDUCE":
|
||||
promotionModel = result_GOODS_REDUCE(parameters, item, promotionInfo);
|
||||
break;
|
||||
case "GOODS_DISCOUNT":
|
||||
promotionModel = result_GOODS_DISCOUNT(parameters, item, promotionInfo);
|
||||
break;
|
||||
case "GOODS_ONE_PRICE":
|
||||
promotionModel = result_GOODS_ONE_PRICE(parameters, item, promotionInfo);
|
||||
break;
|
||||
default:
|
||||
promotionModel = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (item.isSelect)
|
||||
{
|
||||
switch (promotionInfo.type)
|
||||
{
|
||||
case (int)GlobalEnumVars.PromotionType.Promotion:
|
||||
//设置总的商品促销金额
|
||||
cart.goodsPromotionMoney = Math.Round(cart.goodsPromotionMoney + promotionModel, 2);
|
||||
//设置总的价格
|
||||
cart.amount = Math.Round(cart.amount - promotionModel, 2);
|
||||
break;
|
||||
case (int)GlobalEnumVars.PromotionType.Coupon:
|
||||
//优惠券促销金额
|
||||
cart.couponPromotionMoney = Math.Round(cart.couponPromotionMoney + promotionModel, 2);
|
||||
//设置总的价格
|
||||
cart.amount = Math.Round(cart.amount - promotionModel, 2);
|
||||
break;
|
||||
case (int)GlobalEnumVars.PromotionType.Group:
|
||||
//团购
|
||||
cart.goodsPromotionMoney = Math.Round(cart.goodsPromotionMoney + promotionModel, 2);
|
||||
//设置总的价格
|
||||
cart.amount = Math.Round(cart.amount - promotionModel, 2);
|
||||
break;
|
||||
case (int)GlobalEnumVars.PromotionType.Seckill:
|
||||
//秒杀
|
||||
cart.goodsPromotionMoney = Math.Round(cart.goodsPromotionMoney + promotionModel, 2);
|
||||
//设置总的价格
|
||||
cart.amount = Math.Round(cart.amount - promotionModel, 2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//商品促销可能做的比较狠,导致订单价格为负数了,这里判断一下,如果订单价格小于0了,就是0了
|
||||
cart.amount = cart.amount > 0 ? cart.amount : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (resultInfo.code == "ORDER_DISCOUNT")
|
||||
{
|
||||
result_ORDER_DISCOUNT(parameters, cart, promotionInfo);
|
||||
}
|
||||
else if (resultInfo.code == "ORDER_REDUCE")
|
||||
{
|
||||
result_ORDER_REDUCE(parameters, cart, promotionInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订单减固定金额
|
||||
/// </summary>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="cart"></param>
|
||||
/// <param name="promotionInfo"></param>
|
||||
/// <returns></returns>
|
||||
public bool result_ORDER_REDUCE(JObject parameters, CartDto cart, CoreCmsPromotion promotionInfo)
|
||||
{
|
||||
if (!parameters.ContainsKey("money")) return true;
|
||||
|
||||
//判断极端情况,减的太多,超过购物车的总金额了,那么就最多减到0
|
||||
if (cart.amount < (decimal)parameters["money"])
|
||||
{
|
||||
parameters["money"] = cart.amount;
|
||||
}
|
||||
//总价格修改
|
||||
cart.amount -= (decimal)parameters["money"];
|
||||
switch (promotionInfo.type)
|
||||
{
|
||||
case (int)GlobalEnumVars.PromotionType.Promotion:
|
||||
//总促销修改
|
||||
cart.orderPromotionMoney += (decimal)parameters["money"];
|
||||
//设置促销列表
|
||||
if (cart.promotionList.ContainsKey(promotionInfo.id))
|
||||
{
|
||||
cart.promotionList[promotionInfo.id].name = promotionInfo.name;
|
||||
cart.promotionList[promotionInfo.id].type = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
cart.promotionList.Add(promotionInfo.id, new WxNameTypeDto() { name = promotionInfo.name, type = 2 });
|
||||
}
|
||||
break;
|
||||
|
||||
case (int)GlobalEnumVars.PromotionType.Coupon:
|
||||
//优惠券促销金额
|
||||
cart.couponPromotionMoney += (decimal)parameters["money"];
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订单打X折
|
||||
/// </summary>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="cart"></param>
|
||||
/// <param name="promotionInfo"></param>
|
||||
/// <returns></returns>
|
||||
public bool result_ORDER_DISCOUNT(JObject parameters, CartDto cart, CoreCmsPromotion promotionInfo)
|
||||
{
|
||||
//if (parameters.Property("discount") == null) return true;
|
||||
//var objDiscount = Convert.ToInt32(parameters["discount"]);
|
||||
|
||||
if (!parameters.ContainsKey("discount")) return true;
|
||||
var objDiscount = parameters["discount"].ObjectToDecimal(0);
|
||||
|
||||
//判断参数是否设置的正确
|
||||
if (objDiscount >= 10 || objDiscount <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
var orderAmount = cart.amount;
|
||||
//总价格修改
|
||||
cart.amount = Math.Round(Math.Round(Math.Round(cart.amount * objDiscount, 3) * 10, 2) / 100, 2);
|
||||
switch (promotionInfo.type)
|
||||
{
|
||||
case (int)GlobalEnumVars.PromotionType.Promotion:
|
||||
//总促销修改
|
||||
cart.orderPromotionMoney = Math.Round(cart.orderPromotionMoney + Math.Round(orderAmount - cart.amount, 2), 2);
|
||||
//设置促销列表
|
||||
if (cart.promotionList.ContainsKey(promotionInfo.id))
|
||||
{
|
||||
cart.promotionList[promotionInfo.id].name = promotionInfo.name;
|
||||
cart.promotionList[promotionInfo.id].type = 2;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
cart.promotionList.Add(promotionInfo.id, new WxNameTypeDto() { name = promotionInfo.name, type = 2 });
|
||||
}
|
||||
break;
|
||||
|
||||
case (int)GlobalEnumVars.PromotionType.Coupon:
|
||||
//优惠券促销金额
|
||||
cart.couponPromotionMoney = Math.Round(cart.couponPromotionMoney + Math.Round(orderAmount - cart.amount, 2), 2);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定商品减固定金额
|
||||
/// </summary>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="cartProducts"></param>
|
||||
/// <param name="promotionInfo"></param>
|
||||
/// <returns></returns>
|
||||
public decimal result_GOODS_REDUCE(JObject parameters, CartProducts cartProducts, CoreCmsPromotion promotionInfo)
|
||||
{
|
||||
if (!parameters.ContainsKey("money")) return 0;
|
||||
var objMoney = parameters["money"].ObjectToDecimal(0);
|
||||
|
||||
decimal promotionMoney = 0;
|
||||
//判断极端情况,减的太多,超过商品单价了,那么就最多减到0
|
||||
if (cartProducts.products.price < objMoney)
|
||||
{
|
||||
objMoney = cartProducts.products.price;
|
||||
}
|
||||
cartProducts.products.price = Math.Round(cartProducts.products.price - objMoney, 2);
|
||||
//此次商品促销一共优惠了多少钱
|
||||
promotionMoney = Math.Round(cartProducts.nums * objMoney, 2);
|
||||
//设置商品优惠总金额
|
||||
cartProducts.products.promotionAmount = Math.Round(cartProducts.products.promotionAmount + objMoney, 2);
|
||||
//设置商品的实际销售金额(单品)
|
||||
cartProducts.products.amount = Math.Round(cartProducts.products.amount - promotionMoney, 2);
|
||||
return promotionMoney;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定商品打X折
|
||||
/// </summary>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="cartProducts"></param>
|
||||
/// <param name="promotionInfo"></param>
|
||||
/// <returns></returns>
|
||||
public decimal result_GOODS_DISCOUNT(JObject parameters, CartProducts cartProducts, CoreCmsPromotion promotionInfo)
|
||||
{
|
||||
if (!parameters.ContainsKey("discount")) return 0;
|
||||
var objDiscount = parameters["discount"].ObjectToDecimal(0);
|
||||
|
||||
decimal promotionMoney = 0;
|
||||
decimal goodsPrice = cartProducts.products.price;
|
||||
cartProducts.products.price = Math.Round(Math.Round(Math.Round(cartProducts.products.price * objDiscount, 3) * 10, 2) / 100, 2);
|
||||
var pmoney = Math.Round(goodsPrice - cartProducts.products.price, 2); //单品优惠的金额
|
||||
promotionMoney = Math.Round(cartProducts.nums * pmoney, 2);
|
||||
//设置商品优惠总金额
|
||||
cartProducts.products.promotionAmount = Math.Round(cartProducts.products.promotionAmount + promotionMoney, 2);
|
||||
//设置商品的实际销售总金额
|
||||
cartProducts.products.amount = Math.Round(cartProducts.products.amount - promotionMoney, 2);
|
||||
|
||||
return promotionMoney;
|
||||
}
|
||||
|
||||
//商品一口价
|
||||
public decimal result_GOODS_ONE_PRICE(JObject parameters, CartProducts cartProducts, CoreCmsPromotion promotionInfo)
|
||||
{
|
||||
if (!parameters.ContainsKey("money")) return 0;
|
||||
var objMoney = parameters["money"].ObjectToDecimal(0);
|
||||
|
||||
//如果一口价比商品价格高,那么就不执行了
|
||||
decimal promotionMoney = 0;
|
||||
if (cartProducts.products.price <= objMoney)
|
||||
{
|
||||
return promotionMoney;
|
||||
}
|
||||
var goodsPrice = (decimal)cartProducts.products.price;
|
||||
cartProducts.products.price = Math.Round(objMoney, 2);
|
||||
var pmoney = Math.Round(goodsPrice - cartProducts.products.price, 2); //单品优惠的金额
|
||||
promotionMoney = Math.Round(cartProducts.nums * pmoney, 2);
|
||||
//设置商品优惠总金额
|
||||
cartProducts.products.promotionAmount = Math.Round(cartProducts.products.promotionAmount + promotionMoney, 2);
|
||||
//设置商品的实际销售总金额
|
||||
cartProducts.products.amount = Math.Round(cartProducts.products.amount - promotionMoney, 2);
|
||||
return promotionMoney;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
501
CoreCms.Net.Services/Promotion/CoreCmsPromotionServices.cs
Normal file
501
CoreCms.Net.Services/Promotion/CoreCmsPromotionServices.cs
Normal file
@@ -0,0 +1,501 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms
|
||||
* ProjectName: 核心内容管理系统
|
||||
* Web: https://www.corecms.net
|
||||
* Author: 大灰灰
|
||||
* Email: jianweie@163.com
|
||||
* CreateTime: 2021/1/31 21:45:10
|
||||
* Description: 暂无
|
||||
***********************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.IRepository;
|
||||
using CoreCms.Net.IRepository.UnitOfWork;
|
||||
using CoreCms.Net.IServices;
|
||||
using CoreCms.Net.Loging;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
using CoreCms.Net.Model.Entities.Expression;
|
||||
using CoreCms.Net.Model.ViewModels.Basics;
|
||||
using CoreCms.Net.Model.ViewModels.UI;
|
||||
using CoreCms.Net.Model.ViewModels.DTO;
|
||||
using CoreCms.Net.Utility.Extensions;
|
||||
using CoreCms.Net.Utility.Helper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar;
|
||||
|
||||
|
||||
namespace CoreCms.Net.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 促销表 接口实现
|
||||
/// </summary>
|
||||
public class CoreCmsPromotionServices : BaseServices<CoreCmsPromotion>, ICoreCmsPromotionServices
|
||||
{
|
||||
private readonly ICoreCmsPromotionRepository _dal;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
private readonly ICoreCmsPromotionConditionServices _promotionConditionServices;
|
||||
private readonly ICoreCmsPromotionResultServices _promotionResultServices;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
private readonly ICoreCmsCouponServices _couponServices;
|
||||
|
||||
|
||||
public CoreCmsPromotionServices(IUnitOfWork unitOfWork
|
||||
, ICoreCmsPromotionRepository dal
|
||||
, ICoreCmsPromotionConditionServices promotionConditionServices
|
||||
, ICoreCmsPromotionResultServices promotionResultServices
|
||||
, IServiceProvider serviceProvider, ICoreCmsCouponServices couponServices)
|
||||
{
|
||||
this._dal = dal;
|
||||
base.BaseDal = dal;
|
||||
_unitOfWork = unitOfWork;
|
||||
|
||||
_promotionConditionServices = promotionConditionServices;
|
||||
_promotionResultServices = promotionResultServices;
|
||||
_serviceProvider = serviceProvider;
|
||||
_couponServices = couponServices;
|
||||
}
|
||||
|
||||
|
||||
#region 购物车的数据传过来,然后去算促销================
|
||||
|
||||
/// <summary>
|
||||
/// 购物车的数据传过来,然后去算促销
|
||||
/// </summary>
|
||||
/// <param name="cart"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<CartDto> ToPromotion(CartDto cart, int type = (int)GlobalEnumVars.PromotionType.Promotion)
|
||||
{
|
||||
//团购秒杀不会走到这里,团购秒杀直接调用setPromotion方法
|
||||
if (type == (int)GlobalEnumVars.PromotionType.Group || type == (int)GlobalEnumVars.PromotionType.Seckill)
|
||||
{
|
||||
return cart;
|
||||
}
|
||||
|
||||
//按照权重取所有已生效的促销列表
|
||||
var dt = DateTime.Now;
|
||||
var promotions = await _dal.QueryListByClauseAsync(p => p.isEnable == true && p.startTime < dt && p.endTime > dt && p.type == (int)GlobalEnumVars.PromotionType.Promotion && p.isDel == false, p => p.sort, OrderByType.Asc);
|
||||
|
||||
foreach (var item in promotions)
|
||||
{
|
||||
await SetPromotion(item, cart);
|
||||
if (item.isExclusive == true) break;
|
||||
}
|
||||
|
||||
return cart;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 购物车的数据传过来,然后去算优惠券
|
||||
|
||||
/// <summary>
|
||||
/// 购物车的数据传过来,然后去算优惠券
|
||||
/// </summary>
|
||||
/// <param name="cart"></param>
|
||||
/// <param name="promotions"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<WebApiCallBack> ToCoupon(CartDto cart, List<CoreCmsPromotion> promotions)
|
||||
{
|
||||
var jm = new WebApiCallBack();
|
||||
foreach (var item in promotions)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
jm.data = 15014;
|
||||
jm.msg = GlobalErrorCodeVars.Code15014;
|
||||
return jm;
|
||||
}
|
||||
var bl = await SetPromotion(item, cart);
|
||||
if (bl)
|
||||
{
|
||||
cart.coupon.Add(item.name);
|
||||
}
|
||||
else
|
||||
{
|
||||
jm.data = 15014;
|
||||
jm.msg = GlobalErrorCodeVars.Code15014;
|
||||
return jm;
|
||||
}
|
||||
}
|
||||
jm.status = true;
|
||||
jm.data = cart;
|
||||
return jm;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 根据促销信息,去计算购物车的促销情况
|
||||
|
||||
/// <summary>
|
||||
/// 根据促销信息,去计算购物车的促销情况
|
||||
/// </summary>
|
||||
/// <param name="promotion"></param>
|
||||
/// <param name="cartModel"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> SetPromotion(CoreCmsPromotion promotion, CartDto cartModel)
|
||||
{
|
||||
var promotionConditions = await _promotionConditionServices.QueryListByClauseAsync(p => p.promotionId == promotion.id);
|
||||
//循环取出所有的促销条件,有一条不满足,就不行,就返回false
|
||||
var key = true;
|
||||
foreach (var item in promotionConditions)
|
||||
{
|
||||
var res = await _promotionConditionServices.check(item, cartModel, promotion);
|
||||
if (!key) continue;
|
||||
if (!res)
|
||||
{
|
||||
//多个促销条件中,如果有一个不满足,整体就不满足,但是为了显示完整的促销标签,还是要运算完所有的促销条件
|
||||
key = false;
|
||||
}
|
||||
}
|
||||
if (key)
|
||||
{
|
||||
//走到这一步就说明所有的促销条件都符合,那么就去计算结果
|
||||
var promotionResults = await _promotionResultServices.QueryListByClauseAsync(p => p.promotionId == promotion.id);
|
||||
foreach (var item in promotionResults)
|
||||
{
|
||||
await _promotionResultServices.toResult(item, cartModel, promotion);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//如果不满足需求,就要统一标准,把有些满足条件的(2),变成1
|
||||
_promotionConditionServices.PromotionFalse(cartModel, promotion);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取团购列表数据
|
||||
/// <summary>
|
||||
/// 获取团购列表数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<WebApiCallBack> GetGroupList(int type, int userId, int status, int pageIndex, int pageSize)
|
||||
{
|
||||
|
||||
var jm = new WebApiCallBack { status = true };
|
||||
|
||||
var where = PredicateBuilder.True<CoreCmsPromotion>();
|
||||
where = where.And(p => p.isEnable == true && p.isDel == false);
|
||||
if (type == (int)GlobalEnumVars.PromotionType.Group)
|
||||
{
|
||||
where = where.And(p => p.type == (int)GlobalEnumVars.PromotionType.Group);
|
||||
}
|
||||
else if (type == (int)GlobalEnumVars.PromotionType.Seckill)
|
||||
{
|
||||
where = where.And(p => p.type == (int)GlobalEnumVars.PromotionType.Seckill);
|
||||
}
|
||||
|
||||
var dt = DateTime.Now;
|
||||
|
||||
if (status == (int)GlobalEnumVars.GroupSeckillStatus.Upcoming)
|
||||
{
|
||||
where = where.And(p => p.startTime > dt);
|
||||
|
||||
}
|
||||
else if (status == (int)GlobalEnumVars.GroupSeckillStatus.InProgress)
|
||||
{
|
||||
where = where.And(p => p.startTime < dt && dt < p.endTime);
|
||||
}
|
||||
else if (status == (int)GlobalEnumVars.GroupSeckillStatus.Finished)
|
||||
{
|
||||
where = where.And(p => p.endTime < dt);
|
||||
}
|
||||
|
||||
var goods = new List<CoreCmsGoods>();
|
||||
var list = await _dal.QueryPageAsync(where, p => p.endTime, OrderByType.Desc, pageIndex, pageSize);
|
||||
if (list != null && list.Any())
|
||||
{
|
||||
foreach (var item in list)
|
||||
{
|
||||
var promotionId = item.id;
|
||||
var condition = await _promotionConditionServices.QueryByClauseAsync(p => p.promotionId == promotionId);
|
||||
if (condition != null && condition.parameters.Contains("goodsId"))
|
||||
{
|
||||
JObject parameters = (JObject)JsonConvert.DeserializeObject(condition.parameters);
|
||||
|
||||
var res = await GetGroupDetail(parameters["goodsId"].ObjectToInt(0), userId, "group", item.id);
|
||||
if (res.status)
|
||||
{
|
||||
var good = res.data as CoreCmsGoods;
|
||||
|
||||
good.groupId = item.id;
|
||||
good.groupType = item.type;
|
||||
good.groupStatus = item.isEnable;
|
||||
good.groupTime = DateTime.Now;
|
||||
good.groupStartTime = item.startTime;
|
||||
good.groupEndTime = item.endTime;
|
||||
|
||||
TimeSpan ts = item.endTime.Subtract(dt);
|
||||
good.groupTimestamp = (int)ts.TotalSeconds;
|
||||
|
||||
goods.Add(good);
|
||||
}
|
||||
else
|
||||
{
|
||||
item.expression1 = res.msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
jm.data = new
|
||||
{
|
||||
goods,
|
||||
list.TotalCount,
|
||||
list.TotalPages,
|
||||
list,
|
||||
pageIndex,
|
||||
pageSize
|
||||
};
|
||||
return jm;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取团购/秒杀商品详情
|
||||
/// <summary>
|
||||
/// 获取团购/秒杀商品详情
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task<WebApiCallBack> GetGroupDetail(int goodId = 0, int userId = 0, string type = "group", int groupId = 0)
|
||||
{
|
||||
using var container = _serviceProvider.CreateScope();
|
||||
|
||||
var goodsServices = container.ServiceProvider.GetService<ICoreCmsGoodsServices>();
|
||||
var orderServices = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
|
||||
|
||||
var jm = new WebApiCallBack() { msg = "关键参数丢失" };
|
||||
|
||||
if (goodId == 0)
|
||||
{
|
||||
return jm;
|
||||
}
|
||||
//判断商品是否参加团购
|
||||
var isInGroup = _dal.IsInGroup(goodId, out var promotionId);
|
||||
if (!isInGroup)
|
||||
{
|
||||
jm.msg = "商品未参加团购";
|
||||
return jm;
|
||||
}
|
||||
|
||||
var promotion = await _dal.QueryByClauseAsync(p => p.isDel == false && p.isEnable == true && p.id == promotionId);
|
||||
if (promotion == null)
|
||||
{
|
||||
jm.msg = "无此活动";
|
||||
jm.otherData = promotionId;
|
||||
return jm;
|
||||
}
|
||||
|
||||
var goods = new CoreCmsGoods();
|
||||
goods = await goodsServices.GetGoodsDetial(goodId, userId, true, type, groupId);
|
||||
if (goods == null)
|
||||
{
|
||||
jm.msg = "商品不存在";
|
||||
return jm;
|
||||
}
|
||||
|
||||
if (goods.isMarketable == false)
|
||||
{
|
||||
jm.msg = "商品已下架";
|
||||
return jm;
|
||||
}
|
||||
|
||||
//调整前台显示数量
|
||||
if (!string.IsNullOrEmpty(promotion.parameters))
|
||||
{
|
||||
var extendParams = (JObject)JsonConvert.DeserializeObject(promotion.parameters);
|
||||
var checkOrder = orderServices.FindLimitOrder(goods.product.id, userId, promotion.startTime, promotion.endTime);
|
||||
|
||||
if (extendParams != null && extendParams.ContainsKey("max_goods_nums") && extendParams["max_goods_nums"].ObjectToInt(0) != 0)
|
||||
{
|
||||
var maxGoodsNums = extendParams["max_goods_nums"].ObjectToInt(0);
|
||||
goods.stock = maxGoodsNums;
|
||||
//活动销售件数
|
||||
goods.product.stock = maxGoodsNums - checkOrder.TotalOrders;
|
||||
goods.buyPromotionCount = checkOrder.TotalOrders;
|
||||
}
|
||||
else
|
||||
{
|
||||
goods.buyPromotionCount = checkOrder.TotalOrders;
|
||||
}
|
||||
}
|
||||
|
||||
var dt = DateTime.Now;
|
||||
|
||||
goods.groupId = promotion.id;
|
||||
goods.groupType = promotion.type;
|
||||
goods.groupStatus = promotion.isEnable;
|
||||
goods.groupTime = dt;
|
||||
goods.groupStartTime = promotion.startTime;
|
||||
goods.groupEndTime = promotion.endTime;
|
||||
|
||||
TimeSpan ts = promotion.endTime.Subtract(dt);
|
||||
goods.groupTimestamp = (int)ts.TotalSeconds;
|
||||
|
||||
|
||||
//进行促销后要更换原销售价替换原市场价
|
||||
var originPrice = Math.Round(goods.product.price + goods.product.promotionAmount, 2);
|
||||
goods.product.mktprice = originPrice;
|
||||
jm.status = true;
|
||||
jm.msg = "数据获取成功";
|
||||
jm.data = goods;
|
||||
|
||||
return jm;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取可领取的优惠券(不分页)
|
||||
/// <summary>
|
||||
/// 获取可领取的优惠券(不分页)
|
||||
/// </summary>
|
||||
/// <param name="limit">数量</param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<CoreCmsPromotion>> ReceiveCouponList(int limit = 3)
|
||||
{
|
||||
var where = PredicateBuilder.True<CoreCmsPromotion>();
|
||||
where = where.And(p => p.endTime > DateTime.Now); //判断优惠券失效时间 是否可领取
|
||||
where = where.And(p => p.isEnable == true); //启用状态
|
||||
where = where.And(p => p.type == (int)GlobalEnumVars.PromotionType.Coupon); //促销 类型
|
||||
where = where.And(p => p.isAutoReceive == true); //自动领取状态
|
||||
where = where.And(p => p.isDel == false); //是否被删除
|
||||
|
||||
var data = await _dal.QueryPageAndChildsAsync(where, p => p.id, OrderByType.Desc, false, 0, limit);
|
||||
|
||||
if (data != null && data.Any())
|
||||
{
|
||||
foreach (var item in data)
|
||||
{
|
||||
|
||||
var expression1 = "";
|
||||
var expression2 = "";
|
||||
|
||||
foreach (var condition in item.promotionCondition)
|
||||
{
|
||||
var str = PromotionHelper.GetConditionMsg(condition.code, condition.parameters);
|
||||
expression1 += str;
|
||||
item.conditions.Add(str);
|
||||
}
|
||||
foreach (var result in item.promotionResult)
|
||||
{
|
||||
var str = PromotionHelper.GetResultMsg(result.code, result.parameters);
|
||||
expression2 += str;
|
||||
item.results.Add(str);
|
||||
}
|
||||
item.expression1 = expression1;
|
||||
item.expression2 = expression2;
|
||||
}
|
||||
}
|
||||
return data.ToList();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取可领取的优惠券(分页)
|
||||
|
||||
/// <summary>
|
||||
/// 获取可领取的优惠券(分页)
|
||||
/// </summary>
|
||||
/// <param name="page">页码</param>
|
||||
/// <param name="limit">数量</param>
|
||||
/// <returns></returns>
|
||||
public async Task<IPageList<CoreCmsPromotion>> GetReceiveCouponList(int page = 1, int limit = 10)
|
||||
{
|
||||
var where = PredicateBuilder.True<CoreCmsPromotion>();
|
||||
where = where.And(p => p.endTime > DateTime.Now); //判断优惠券失效时间 是否可领取
|
||||
where = where.And(p => p.isEnable == true); //启用状态
|
||||
where = where.And(p => p.type == (int)GlobalEnumVars.PromotionType.Coupon); //促销 类型
|
||||
where = where.And(p => p.isAutoReceive == true); //自动领取状态
|
||||
where = where.And(p => p.isDel == false); //是否被删除
|
||||
|
||||
var data = await _dal.QueryPageAndChildsAsync(where, p => p.id, OrderByType.Desc, true, page, limit);
|
||||
|
||||
if (data != null && data.Any())
|
||||
{
|
||||
foreach (var item in data)
|
||||
{
|
||||
var expression1 = "";
|
||||
var expression2 = "";
|
||||
foreach (var condition in item.promotionCondition)
|
||||
{
|
||||
var str = PromotionHelper.GetConditionMsg(condition.code, condition.parameters);
|
||||
expression1 += str;
|
||||
item.conditions.Add(str);
|
||||
}
|
||||
foreach (var result in item.promotionResult)
|
||||
{
|
||||
var str = PromotionHelper.GetResultMsg(result.code, result.parameters);
|
||||
expression2 += str;
|
||||
item.results.Add(str);
|
||||
}
|
||||
item.expression1 = expression1;
|
||||
item.expression2 = expression2;
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取指定id 的优惠券是否可以领取
|
||||
/// <summary>
|
||||
/// 获取指定id 的优惠券是否可以领取
|
||||
/// </summary>
|
||||
/// <param name="promotionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<WebApiCallBack> ReceiveCoupon(int promotionId)
|
||||
{
|
||||
var jm = new WebApiCallBack();
|
||||
|
||||
var where = PredicateBuilder.True<CoreCmsPromotion>();
|
||||
where = where.And(p => p.endTime > DateTime.Now); //判断优惠券失效时间 是否可领取
|
||||
where = where.And(p => p.isEnable == true); //启用状态
|
||||
where = where.And(p => p.type == (int)GlobalEnumVars.PromotionType.Coupon); //促销 类型
|
||||
where = where.And(p => p.isAutoReceive == true); //自动领取状态
|
||||
where = where.And(p => p.id == promotionId);
|
||||
where = where.And(p => p.isDel == false); //是否被删除
|
||||
|
||||
|
||||
var info = await _dal.QueryByClauseAsync(where);
|
||||
if (info != null)
|
||||
{
|
||||
jm.data = info;
|
||||
//判断最大领取数量
|
||||
if (info.maxRecevieNums == 0)
|
||||
{
|
||||
jm.status = true;
|
||||
return jm;
|
||||
}
|
||||
var receiveCount = await _couponServices.GetCountAsync(p => p.promotionId == promotionId);
|
||||
if (receiveCount >= info.maxRecevieNums)
|
||||
{
|
||||
jm.status = false;
|
||||
jm.msg = "该优惠券已被领完,请下次再来!";
|
||||
return jm;
|
||||
}
|
||||
else
|
||||
{
|
||||
jm.status = true;
|
||||
jm.code = receiveCount;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
jm.msg = GlobalErrorCodeVars.Code15007;
|
||||
}
|
||||
return jm;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user