/***********************************************************************
* Project: baifenBinfa
* ProjectName: 百分兵法管理系统
* Web: http://chuanyin.com
* Author:
* Email:
* CreateTime: 202403/02
* 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.Entities.Expression;
using CoreCms.Net.Utility.Helper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SqlSugar;
namespace CoreCms.Net.Services
{
///
/// 拼团商品表 接口实现
///
public class CoreCmsPinTuanGoodsServices : BaseServices, ICoreCmsPinTuanGoodsServices
{
private readonly ICoreCmsPinTuanGoodsRepository _dal;
private readonly IUnitOfWork _unitOfWork;
private readonly IServiceProvider _serviceProvider;
public CoreCmsPinTuanGoodsServices(IUnitOfWork unitOfWork, ICoreCmsPinTuanGoodsRepository dal,
IServiceProvider serviceProvider
)
{
this._dal = dal;
base.BaseDal = dal;
_unitOfWork = unitOfWork;
_serviceProvider = serviceProvider;
}
///
/// 取拼团的商品信息,增加拼团的一些属性,会显示优惠价
///
/// 拼团规则
/// 商品id
/// 用户序列
/// 获取拼团记录
/// 校验库存
/// 获取商品sku
///
public async Task GetGoodsInfo(CoreCmsPinTuanRule pinTuanRuleInfo, int goodsId, int userId, bool needRecord, bool needCheckStock, bool needGoodSku)
{
if (pinTuanRuleInfo == null) return null;
if (goodsId == 0) return null;
using var container = _serviceProvider.CreateScope();
var goodsServices = container.ServiceProvider.GetService();
var pinTuanRecordServices = container.ServiceProvider.GetService();
var orderServices = container.ServiceProvider.GetService();
var goodsInfo = await goodsServices.GetGoodsDetail(goodsId, userId, false, "goods", 0, needGoodSku);
if (goodsInfo == null) return null;
goodsInfo.pinTuanRule = pinTuanRuleInfo;
goodsInfo.pinTuanPrice = goodsInfo.price - pinTuanRuleInfo.discountAmount;
if (goodsInfo.pinTuanPrice < 0) goodsInfo.pinTuanPrice = 0;
//取拼团记录
goodsInfo.pinTuanRecordNums = await pinTuanRecordServices.GetCountAsync(p => p.ruleId == pinTuanRuleInfo.id && p.goodsId == goodsInfo.id, true);
//判断拼团状态
var dt = DateTime.Now;
if (goodsInfo.pinTuanRule.startTime > dt)
{
goodsInfo.pinTuanRule.pinTuanStartStatus = (int)GlobalEnumVars.PinTuanRuleStatus.notBegun;
var ts = goodsInfo.pinTuanRule.startTime.Subtract(dt);
goodsInfo.pinTuanRule.lastTime = (int)ts.TotalSeconds;
}
else if (goodsInfo.pinTuanRule.startTime <= dt && goodsInfo.pinTuanRule.endTime > dt)
{
goodsInfo.pinTuanRule.pinTuanStartStatus = (int)GlobalEnumVars.PinTuanRuleStatus.begin;
var ts = goodsInfo.pinTuanRule.endTime.Subtract(dt);
goodsInfo.pinTuanRule.lastTime = (int)ts.TotalSeconds;
}
else
{
goodsInfo.pinTuanRule.pinTuanStartStatus = (int)GlobalEnumVars.PinTuanRuleStatus.haveExpired;
}
//拼团记录
if (needRecord)
{
var re = await pinTuanRecordServices.GetRecord(pinTuanRuleInfo.id, goodsInfo.id, (int)GlobalEnumVars.PinTuanRecordStatus.InProgress);
if (re.status)
{
goodsInfo.pinTuanRecord = re.data as List;
}
}
//判断是否需要检测库存
if (needCheckStock)
{
var checkOrder = orderServices.FindLimitOrder(goodsInfo.product.id, userId, pinTuanRuleInfo.startTime, pinTuanRuleInfo.endTime, 0);
if (pinTuanRuleInfo.maxGoodsNums > 0)
{
goodsInfo.stock = pinTuanRuleInfo.maxGoodsNums;
//活动销售件数
goodsInfo.product.stock = pinTuanRuleInfo.maxGoodsNums - checkOrder.TotalOrders;
//goodsInfo.buyCount = info.maxGoodsNums - checkOrder.TotalOrders;
goodsInfo.buyPinTuanCount = checkOrder.TotalOrders;
}
else
{
goodsInfo.buyPinTuanCount = checkOrder.TotalOrders;
}
}
return goodsInfo;
}
}
}