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.
 
 

432 lines
22 KiB

using JDXX.Model.Db;
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
{
private FreeSqlMultiDBManager _freeSqlMultiDBManager;
public TrusteeshipBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, FreeSqlMultiDBManager _freeSqlMultiDBManager) : base(fsql, nLogManager, idGenerator)
{
this._freeSqlMultiDBManager = _freeSqlMultiDBManager;
}
/// <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 && s.IsEnabled == true)
.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>
/// <exception cref="BusinessException"></exception>
public ListResponse<TrusteeshipTaskResponse> QueryTrusteeship(QueryTrusteeshipRequest request)
{
if (request.ShopId == null || request.ShopId == 0)
throw new BusinessException("缺少ShopId");
if (request.PageSize > 20)
request.PageSize = 20;
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 &&
t.BusinessType == request.BusinessType &&
t.IsEnabled == true)
.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(request.IsEnd == 1, (t, p, ps) => t.IsEnd == true)
.WhereIf(request.IsEnd == 0, (t, p, ps) => t.IsEnd == false)
.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,
AnchorBudget = t.AnchorBudget,
ActualBudget = t.ActualBudget,
BusinessType = t.BusinessType,
CampaginName = t.CampaginName,
CampaignId = t.CampaignId,
CostInTrusteeship = t.CostInTrusteeship,
CreateTime = t.CreateTime,
UpdateTime = t.UpdateTime,
EndTime = t.EndTime,
IsEnd = t.IsEnd,
StartTrusteeshipDate = t.StartTrusteeshipDate,
PolicyType = t.PolicyType,
IsEnabled = t.IsEnabled,
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
});
#endregion
#region SKU商品营业额
var actualAmountList = fsql.Select<AggregationJDPopularizeSkuDaily>()
.Where(x => x.ShopId == request.ShopId &&
x.Date >= startDate && x.Date <= endDate &&
skuIdList.Contains(x.SkuId))
.ToList(x => new
{
x.Date,
x.SkuId,
//x.Cost,
x.ActualAmount
});
#endregion
#region 免费访客量
var shopIdStr = request.ShopId.ToString();
var uvList = _freeSqlMultiDBManager.XXfsql.Select<Newskus>().Where(s => s.ShopId == shopIdStr &&
s.CreateTime >= startDate &&
s.CreateTime <= endDate &&
skuIdList.Contains(s.Sku))
.ToList(s => new { s.Uv, s.Sku, Date = s.CreateTime });
#endregion
foreach (var task in list)
{
task.CostByDateList = costList.Where(x => x.SkuId == task.SkuId &&
x.CampaignId == task.CampaignId)
.OrderBy(x => x.Date)
.Select(x => new NumberByDate()
{
Date = x.Date,
Value = x.Cost
}).ToList();
task.ActualAmountByDateList = actualAmountList.Where(x => x.SkuId == task.SkuId)
.OrderBy(x => x.Date)
.Select(x => new NumberByDate()
{
Date = x.Date,
Value = x.ActualAmount
}).ToList();
task.UVByDateList = uvList.Where(x => x.Sku == task.SkuId)
.OrderBy(x => x.Date)
.Select(x => new NumberByDate()
{
Date = x.Date,
Value = x.Uv
}).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,
AnchorBudget = 0M,
ActualBudget = 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,
PolicyType = Model.Enums.PolicyType.,
IsEnabled = true
}).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();
}
public void StopTrusteeship(long id)
{
var task = fsql.Select<Sbf_TrusteeshipTask>(id).ToOne();
if (task == null)
throw new BusinessException("托管任务不存在");
if (task.IsEnd == true)
return;
fsql.Update<Sbf_TrusteeshipTask>(id).Set(t => t.IsEnd, true)
.Set(t => t.EndTime, DateTime.Now)
.Set(t => t.UpdateTime, DateTime.Now)
.ExecuteAffrows();
}
public void DeleteTrusteehip(long id)
{
var task = fsql.Select<Sbf_TrusteeshipTask>(id).ToOne();
if (task == null)
throw new BusinessException("托管任务不存在");
if (task.IsEnabled == false)
return;
fsql.Update<Sbf_TrusteeshipTask>(id).Set(t => t.IsEnabled, false)
.Set(t => t.IsEnd, true)
.Set(t => t.EndTime, DateTime.Now)
.Set(t => t.UpdateTime, DateTime.Now)
.ExecuteAffrows();
}
public TrusteeshipTaskResponse GetTrusteeshipTaskCurveResponse(long Id)
{
var task = 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.Id == Id && t.IsEnabled == true)
.OrderByDescending((t, p, ps) => t.CreateTime)
.ToOne((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,
AnchorBudget = t.AnchorBudget,
ActualBudget = t.ActualBudget,
BusinessType = t.BusinessType,
CampaginName = t.CampaginName,
CampaignId = t.CampaignId,
CostInTrusteeship = t.CostInTrusteeship,
CreateTime = t.CreateTime,
UpdateTime = t.UpdateTime,
EndTime = t.EndTime,
IsEnd = t.IsEnd,
StartTrusteeshipDate = t.StartTrusteeshipDate,
PolicyType = t.PolicyType,
IsEnabled = t.IsEnabled,
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<TrusteeshipTaskResponse>();
var startDate = DateTime.Now.Date.AddDays(-30);
var endDate = DateTime.Now.Date.AddDays(-1);
//var spuIdList = list.Select(x => x.SpuId).Distinct().ToList();
#region 推广花费/推广盈利
var costList = fsql.Select<AggregationJDPopularizeAdSkuDaily>()
.Where(x => x.ShopId == task.ShopId &&
x.Date >= startDate && x.Date <= endDate &&
x.BusinessType == task.BusinessType &&
x.CampaignId == task.CampaignId &&
x.SkuId == task.SkuId)
.ToList(x => new
{
x.Date,
x.SkuId,
x.CampaignId,
x.BusinessType,
x.Cost,
x.PopularizeLevelProfit
});
#endregion
#region SKU商品营业额
var actualAmountList = fsql.Select<AggregationJDPopularizeSkuDaily>()
.Where(x => x.ShopId == task.ShopId &&
x.Date >= startDate && x.Date <= endDate &&
x.SkuId == task.SkuId)
.ToList(x => new
{
x.Date,
x.SkuId,
x.Cost,
x.ActualAmount
});
#endregion
#region 免费访客量
var shopIdStr = task.ShopId.ToString();
var uvList = _freeSqlMultiDBManager.XXfsql.Select<Newskus>().Where(s => s.ShopId == shopIdStr &&
s.CreateTime >= startDate &&
s.CreateTime <= endDate &&
s.Sku == task.SkuId)
.ToList(s => new { s.Uv, s.Sku, Date = s.CreateTime });
#endregion
task.CostByDateList = costList.OrderBy(x => x.Date)
.Select(x => new NumberByDate()
{
Date = x.Date,
Value = x.Cost
}).ToList();
task.PopluarizeNetProfitByDateList = costList.OrderBy(x => x.Date)
.Select(x => new NumberByDate()
{
Date = x.Date,
Value = x.PopularizeLevelProfit - x.Cost
}).ToList();
task.ActualAmountByDateList = actualAmountList.OrderBy(x => x.Date)
.Select(x => new NumberByDate()
{
Date = x.Date,
Value = x.ActualAmount
}).ToList();
task.UVByDateList = uvList.OrderBy(x => x.Date)
.Select(x => new NumberByDate()
{
Date = x.Date,
Value = x.Uv
}).ToList();
return task;
}
}
}