You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
254 lines
12 KiB
254 lines
12 KiB
2 years ago
|
using SBF.Common.Extensions;
|
||
|
using SBF.Common.Log;
|
||
|
using SBF.Common.Models;
|
||
|
using SBF.Model.Db;
|
||
|
using SBF.Model.Dto;
|
||
|
using SiNan.Business;
|
||
|
using Yitter.IdGenerator;
|
||
|
|
||
|
namespace SBF.Business
|
||
|
{
|
||
|
public class TrusteeshipBusiness : BaseBusiness, IDenpendency
|
||
|
{
|
||
|
public TrusteeshipBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator) : base(fsql, nLogManager, idGenerator)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 搜索SKU参与的推广渠道
|
||
|
/// </summary>
|
||
|
/// <param name="request"></param>
|
||
|
/// <returns></returns>
|
||
|
public IList<SkuJoinPopularizeChannelResponse> SearchSkuJoinPopularizeChannel(SearchSkuJoinPopularizeChannelRequest request)
|
||
|
{
|
||
|
if (request.ShopId == null || request.ShopId == 0)
|
||
|
throw new BusinessException("缺少ShopId");
|
||
|
|
||
|
|
||
|
var skuList = new List<string>();
|
||
|
if (request.SkuList != null && request.SkuList.Count() > 0)
|
||
|
{
|
||
|
request.Sku = string.Empty;
|
||
|
request.Spu = string.Empty;
|
||
|
skuList.AddRange(request.SkuList);
|
||
|
}
|
||
|
|
||
|
if (!string.IsNullOrEmpty(request.Sku))
|
||
|
{
|
||
|
request.Spu = string.Empty;
|
||
|
skuList.Add(request.Sku);
|
||
|
}
|
||
|
|
||
|
if (!string.IsNullOrEmpty(request.Spu))
|
||
|
{
|
||
|
skuList.AddRange(fsql.Select<ProductSku, Product>()
|
||
|
.InnerJoin((ps, p) => ps.ProductId == p.Id)
|
||
|
.Where((ps, p) => ps.State == 1 && p.State == 8)
|
||
|
.WhereIf(!string.IsNullOrEmpty(request.Spu), (ps, p) => ps.ProductId == request.Spu)
|
||
|
.ToList((ps, p) => ps.Id));
|
||
|
}
|
||
|
|
||
|
if (skuList.Count == 0)
|
||
|
throw new BusinessException("缺少sku信息");
|
||
|
|
||
|
var yesterDay = DateTime.Now.Date.AddDays(-1);
|
||
|
var list = fsql.Select<JDPopularizeAdSku, JDPopularizeCampaign, JDPopularizeAdGroup>()
|
||
|
.LeftJoin((ads, c, ad) => ads.CampaignId == c.CampaignId && ads.Date == c.Date)
|
||
|
.LeftJoin((ads, c, ad) => ads.AdGroupId == ad.AdGroupId && ads.Date == ad.Date)
|
||
|
.Where((ads, c, ad) => ads.ShopId == request.ShopId && ads.Date == yesterDay)
|
||
|
.WhereIf(skuList.Count() > 1, (ads, c, ad) => skuList.Contains(ads.Sku))
|
||
|
.WhereIf(skuList.Count() == 1, (ads, c, ad) => ads.Sku == skuList[0])
|
||
|
.Where((ads, c, ad) => !fsql.Select<Sbf_TrusteeshipTask>()
|
||
|
.Where(s => s.ShopId == request.ShopId && s.CampaignId == ads.CampaignId && s.SkuId == ads.Sku)
|
||
|
.Any())
|
||
|
.GroupBy((ads, c, ad) => new { ads.BusinessType, ads.CampaignId, c.CampaignName, ads.AdGroupId, ad.AdGroupName, ads.AdId, ads.AdName, ads.Sku })
|
||
|
.ToList(g => new SkuJoinPopularizeChannelResponse
|
||
|
{
|
||
|
BusinessType = g.Key.BusinessType.Value,
|
||
|
CampaignId = g.Key.CampaignId,
|
||
|
CampaignName = g.Key.CampaignName,
|
||
|
AdGroupId = g.Key.AdGroupId,
|
||
|
AdGroupName = g.Key.AdGroupName,
|
||
|
AdId = g.Key.AdId,
|
||
|
AdName = g.Key.AdName,
|
||
|
Sku = g.Key.Sku
|
||
|
});
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 查询托管任务
|
||
|
/// </summary>
|
||
|
/// <param name="request"></param>
|
||
|
/// <returns></returns>
|
||
|
public ListResponse<TrusteeshipTaskResponse> QueryTrusteeship(QueryTrusteeshipRequest request)
|
||
|
{
|
||
|
if (request.ShopId == null || request.ShopId == 0)
|
||
|
throw new BusinessException("缺少ShopId");
|
||
|
|
||
|
var list = fsql.Select<Sbf_TrusteeshipTask, Product, ProductSku>()
|
||
|
.InnerJoin((t, p, ps) => t.SpuId == p.Id)
|
||
|
.InnerJoin((t, p, ps) => t.SkuId == ps.Id)
|
||
|
.Where((t, p, ps) => t.ShopId == request.ShopId)
|
||
|
.WhereIf(request.Stage != null, (t, p, ps) => p.Stage == request.Stage)
|
||
|
.WhereIf(!string.IsNullOrEmpty(request.Spu), (t, p, ps) => t.SpuId == request.Spu)
|
||
|
.WhereIf(!string.IsNullOrEmpty(request.Sku), (t, p, ps) => t.SkuId == request.Sku)
|
||
|
.WhereIf(!string.IsNullOrEmpty(request.Title), (t, p, ps) => p.Title.StartsWith(request.Title))
|
||
|
.OrderByDescending((t, p, ps) => t.CreateTime)
|
||
|
.Page(request.PageIndex, request.PageSize)
|
||
|
.Count(out var count)
|
||
|
.ToList((t, p, ps) => new Sbf_TrusteeshipTask()
|
||
|
{
|
||
|
Id = t.Id,
|
||
|
ShopId = t.ShopId,
|
||
|
SpuId = t.SpuId,
|
||
|
SkuId = t.SkuId,
|
||
|
ActualAmountInTrusteeship = t.ActualAmountInTrusteeship,
|
||
|
AdGroupId = t.AdGroupId,
|
||
|
AdGroupName = t.AdGroupName,
|
||
|
AdId = t.AdId,
|
||
|
AdName = t.AdName,
|
||
|
BidPrice = t.BidPrice,
|
||
|
Budget = t.Budget,
|
||
|
BusinessType = t.BusinessType,
|
||
|
CampaginName = t.CampaginName,
|
||
|
CampaignId = t.CampaignId,
|
||
|
CostInTrusteeship = t.CostInTrusteeship,
|
||
|
CreateTime = t.CreateTime,
|
||
|
EndTime = t.EndTime,
|
||
|
IsEnd = t.IsEnd,
|
||
|
StartTrusteeshipDate = t.StartTrusteeshipDate,
|
||
|
|
||
|
Logo = ps.Logo,
|
||
|
Price = ps.Price,
|
||
|
SkuTitle = ps.Title,
|
||
|
SkuState = ps.State,
|
||
|
SkuCreateTime = ps.CreateTime,
|
||
|
CategoryId = ps.CategoryId,
|
||
|
CategoryName = ps.CategoryName,
|
||
|
|
||
|
MainSkuId = p.MainSkuId,
|
||
|
ProductCreateTime = p.CreateTime,
|
||
|
ProductItemNum = p.ProductItemNum,
|
||
|
ProductState = p.State,
|
||
|
ProductTitle = p.Title,
|
||
|
Stage = p.Stage,
|
||
|
Platform = p.Platform
|
||
|
})
|
||
|
.Map<List<TrusteeshipTaskResponse>>();
|
||
|
|
||
|
var startDate = DateTime.Now.Date.AddDays(-7);
|
||
|
var endDate = DateTime.Now.Date.AddDays(-1);
|
||
|
|
||
|
var skuIdList = list.Select(x => x.SkuId).Distinct().ToList();
|
||
|
var spuIdList = list.Select(x => x.SpuId).Distinct().ToList();
|
||
|
|
||
|
|
||
|
#region 推广花费
|
||
|
var costList = fsql.Select<AggregationJDPopularizeAdSkuDaily>()
|
||
|
.Where(x => x.ShopId == request.ShopId &&
|
||
|
x.Date >= startDate && x.Date <= endDate &&
|
||
|
skuIdList.Contains(x.SkuId))
|
||
|
.ToList(x => new
|
||
|
{
|
||
|
x.Date,
|
||
|
x.SkuId,
|
||
|
x.CampaignId,
|
||
|
x.BusinessType,
|
||
|
x.Cost
|
||
|
});
|
||
|
//.GroupBy(x => new { x.Date, x.SkuId, x.CampaignId, x.BusinessType })
|
||
|
//.ToList(g => new
|
||
|
//{
|
||
|
// Date = g.Key.Date,
|
||
|
// SkuId = g.Key.SkuId,
|
||
|
// CampaignId = g.Key.CampaignId,
|
||
|
// BusinessType = g.Key.BusinessType,
|
||
|
// Cost = g.Sum(g.Value.Cost)
|
||
|
//});
|
||
|
#endregion
|
||
|
|
||
|
#region 商品营业额
|
||
|
var actualAmountList = fsql.Select<AggregationJDPopularizeSpuDaily>()
|
||
|
.Where(x => x.ShopId == request.ShopId &&
|
||
|
x.Date >= startDate && x.Date <= endDate &&
|
||
|
spuIdList.Contains(x.ProductId))
|
||
|
.ToList(x => new
|
||
|
{
|
||
|
x.Date,
|
||
|
x.ProductId,
|
||
|
x.Cost,
|
||
|
x.ActualAmount
|
||
|
});
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 免费访客量
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
foreach (var task in list)
|
||
|
{
|
||
|
task.CostByDateList = costList.Where(x => x.SkuId == task.SkuId && x.CampaignId == task.CampaignId && x.BusinessType == task.BusinessType)
|
||
|
.OrderBy(x => x.Date)
|
||
|
.Select(x => new NumberByDate()
|
||
|
{
|
||
|
Date = x.Date,
|
||
|
Value = x.Cost
|
||
|
}).ToList();
|
||
|
|
||
|
task.ActualAmountByDateList = actualAmountList.Where(x => x.ProductId == task.SpuId)
|
||
|
.OrderBy(x => x.Date)
|
||
|
.Select(x => new NumberByDate()
|
||
|
{
|
||
|
Date = x.Date,
|
||
|
Value = x.ActualAmount
|
||
|
}).ToList();
|
||
|
}
|
||
|
|
||
|
return new ListResponse<TrusteeshipTaskResponse>() { ItemList = list, Count = count };
|
||
|
}
|
||
|
|
||
|
public void CreateTrusteeship(CreateTrusteeshipRequest request)
|
||
|
{
|
||
|
if (request.SkuList == null || request.SkuList.Count() == 0)
|
||
|
throw new BusinessException("缺少SkuList");
|
||
|
var joinList = SearchSkuJoinPopularizeChannel(new SearchSkuJoinPopularizeChannelRequest()
|
||
|
{
|
||
|
ShopId = request.ShopId,
|
||
|
SkuList = request.SkuList
|
||
|
});
|
||
|
|
||
|
var insertList = joinList.Select(x => new Sbf_TrusteeshipTask()
|
||
|
{
|
||
|
Id = idGenerator.NewLong(),
|
||
|
ActualAmountInTrusteeship = 0M,
|
||
|
AdGroupId = x.AdGroupId,
|
||
|
AdGroupName = x.AdGroupName,
|
||
|
AdId = x.AdId,
|
||
|
AdName = x.AdName,
|
||
|
BidPrice = 0M,
|
||
|
Budget = 0M,
|
||
|
BusinessType = x.BusinessType,
|
||
|
CampaginName = x.CampaignName,
|
||
|
CampaignId = x.CampaignId,
|
||
|
CostInTrusteeship = 0M,
|
||
|
CreateTime = DateTime.Now,
|
||
|
EndTime = null,
|
||
|
IsEnd = false,
|
||
|
StartTrusteeshipDate = DateTime.Now.Date.AddDays(1),
|
||
|
ShopId = request.ShopId,
|
||
|
SkuId = x.Sku
|
||
|
}).ToList();
|
||
|
|
||
|
var skuIdList = insertList.Select(x => x.SkuId).Distinct().ToList();
|
||
|
var psList = fsql.Select<ProductSku>(skuIdList).ToList();
|
||
|
foreach (var insertTask in insertList)
|
||
|
insertTask.SpuId = psList.FirstOrDefault(ps => ps.Id == insertTask.SkuId)?.ProductId;
|
||
|
|
||
|
fsql.Insert(insertList).ExecuteAffrows();
|
||
|
}
|
||
|
}
|
||
|
}
|