|
|
|
using BBWYB.Common.Log;
|
|
|
|
using BBWYB.Common.Models;
|
|
|
|
using BBWYB.Server.Model;
|
|
|
|
using BBWYB.Server.Model.Db;
|
|
|
|
using BBWYB.Server.Model.Dto;
|
|
|
|
using FreeSql;
|
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
|
|
|
namespace BBWYB.Server.Business
|
|
|
|
{
|
|
|
|
public class PurchaserBusiness : BaseBusiness, IDenpendency
|
|
|
|
{
|
|
|
|
private FreeSqlMultiDBManager _freeSqlMultiDBManager;
|
|
|
|
|
|
|
|
public PurchaserBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, FreeSqlMultiDBManager freeSqlMultiDBManager) : base(fsql, nLogManager, idGenerator)
|
|
|
|
{
|
|
|
|
this._freeSqlMultiDBManager = freeSqlMultiDBManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListResponse<string> QueryPurchaserNameList(string keywords)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(keywords))
|
|
|
|
throw new BusinessException("关键字不能为空");
|
|
|
|
var list = fsql.Select<Purchaser>().Where(p => p.Name.Contains(keywords)).Distinct().ToList(x => x.Name);
|
|
|
|
return new ListResponse<string> { Items = list, TotalCount = list.Count() };
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListResponse<string> QueryPurchaserLocationList(string keywords)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(keywords))
|
|
|
|
throw new BusinessException("关键字不能为空");
|
|
|
|
var list = fsql.Select<Purchaser>().Where(p => p.Location.Contains(keywords)).Distinct().ToList(x => x.Location);
|
|
|
|
return new ListResponse<string> { Items = list, TotalCount = list.Count() };
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListResponse<PurchaserResponse> QueryPurchaserList(QueryPurchaserRequest request)
|
|
|
|
{
|
|
|
|
//默认ShopId为空
|
|
|
|
request.ShopId = null;
|
|
|
|
|
|
|
|
#region 数据验证
|
|
|
|
if (request.RecentDayCondition != null) // && request.RecentDayCondition.RecentDay > 0
|
|
|
|
{
|
|
|
|
if (request.RecentDayCondition.PurchasedCountComparisonOperator != null)
|
|
|
|
{
|
|
|
|
if (request.RecentDayCondition.PurchasedCount == null || (request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.介于 &&
|
|
|
|
request.RecentDayCondition.PurchasedCount2 == null))
|
|
|
|
throw new BusinessException("采购量条件不完整");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.RecentDayCondition.PurchasedAmountComparisonOperator != null)
|
|
|
|
{
|
|
|
|
if (request.RecentDayCondition.PurchasedAmount == null || (request.RecentDayCondition.PurchasedAmountComparisonOperator == Enums.ComparisonOperator.介于 &&
|
|
|
|
request.RecentDayCondition.PurchasedAmount2 == null))
|
|
|
|
throw new BusinessException("采购金额条件不完整");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
if (request.PageSize > 20)
|
|
|
|
request.PageSize = 20;
|
|
|
|
|
|
|
|
var select = fsql.Select<Purchaser>()
|
|
|
|
.WhereIf(!string.IsNullOrEmpty(request.Spu), p => fsql.Select<PurchaseSchemeProduct>()
|
|
|
|
.Where(psp1 => psp1.PurchaserId == p.Id &&
|
|
|
|
psp1.ProductId == request.Spu)
|
|
|
|
.Any())
|
|
|
|
.WhereIf(!string.IsNullOrEmpty(request.Sku), p => fsql.Select<PurchaseSchemeProduct>()
|
|
|
|
.Where(psp2 => psp2.PurchaserId == p.Id &&
|
|
|
|
psp2.SkuId == request.Sku)
|
|
|
|
.Any())
|
|
|
|
.WhereIf(request.PurchaserNameList != null &&
|
|
|
|
request.PurchaserNameList.Count() > 0, p => request.PurchaserNameList.Contains(p.Name))
|
|
|
|
.WhereIf(request.CategoryIdList != null &&
|
|
|
|
request.CategoryIdList.Count() > 0, p => fsql.Select<Purchaser_ExtendedInfo_Relation>()
|
|
|
|
.Where(per => per.PurchaserId == p.Id &&
|
|
|
|
request.CategoryIdList.Contains(per.ExtendedInfoId.Value)).Any())
|
|
|
|
.WhereIf(request.LocationList != null && request.LocationList.Count() > 0, p => request.LocationList.Contains(p.Location))
|
|
|
|
.WhereIf(request.ManagmentMode != null, p => p.ManagmentMode == request.ManagmentMode);
|
|
|
|
|
|
|
|
if (request.RecentDayCondition != null) // && request.RecentDayCondition.RecentDay > 0
|
|
|
|
{
|
|
|
|
//var recentStartDay = DateTime.Now.Date.AddDays(request.RecentDayCondition.RecentDay.Value * -1);
|
|
|
|
if (request.RecentDayCondition.PurchasedCountComparisonOperator != null &&
|
|
|
|
request.RecentDayCondition.PurchasedCount != null &&
|
|
|
|
request.RecentDayCondition.PurchasedCount != 0)
|
|
|
|
{
|
|
|
|
select = select.WhereIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.等于,
|
|
|
|
p => p.Recent90dPurchasedCount != null && p.Recent90dPurchasedCount == request.RecentDayCondition.PurchasedCount)
|
|
|
|
.WhereIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.小于,
|
|
|
|
p => p.Recent90dPurchasedCount != null && p.Recent90dPurchasedCount <= request.RecentDayCondition.PurchasedCount)
|
|
|
|
.WhereIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.大于,
|
|
|
|
p => p.Recent90dPurchasedCount != null && p.Recent90dPurchasedCount >= request.RecentDayCondition.PurchasedCount)
|
|
|
|
.WhereIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.介于,
|
|
|
|
p => p.Recent90dPurchasedCount != null && p.Recent90dPurchasedCount >= request.RecentDayCondition.PurchasedCount &&
|
|
|
|
p.Recent90dPurchasedCount <= request.RecentDayCondition.PurchasedCount2);
|
|
|
|
}
|
|
|
|
if (request.RecentDayCondition.PurchasedAmountComparisonOperator != null &&
|
|
|
|
request.RecentDayCondition.PurchasedAmount != null &&
|
|
|
|
request.RecentDayCondition.PurchasedAmount != 0)
|
|
|
|
{
|
|
|
|
select = select.WhereIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.等于,
|
|
|
|
p => p.Recent90dPurchasedAmount != null && p.Recent90dPurchasedAmount == request.RecentDayCondition.PurchasedAmount)
|
|
|
|
.WhereIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.小于,
|
|
|
|
p => p.Recent90dPurchasedAmount != null && p.Recent90dPurchasedAmount <= request.RecentDayCondition.PurchasedAmount)
|
|
|
|
.WhereIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.大于,
|
|
|
|
p => p.Recent90dPurchasedAmount != null && p.Recent90dPurchasedAmount >= request.RecentDayCondition.PurchasedAmount)
|
|
|
|
.WhereIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.介于,
|
|
|
|
p => p.Recent90dPurchasedAmount != null && p.Recent90dPurchasedAmount >= request.RecentDayCondition.PurchasedAmount &&
|
|
|
|
p.Recent90dPurchasedAmount <= request.RecentDayCondition.PurchasedAmount2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select = select.Page(request.PageIndex, request.PageSize)
|
|
|
|
.OrderByPropertyNameIf(!string.IsNullOrEmpty(request.SortColumn), request.SortColumn, request.SortType == 0);
|
|
|
|
var sql = select.ToSql();
|
|
|
|
var purchaserList = select.Count(out var count).ToList<PurchaserResponse>();
|
|
|
|
if (purchaserList.Count() > 0)
|
|
|
|
{
|
|
|
|
var purchaserIdList = purchaserList.Select(p => p.Id).ToList();
|
|
|
|
|
|
|
|
#region 查询标签/主营类目
|
|
|
|
var purchaserExtendInfoList = fsql.Select<PurchaserExtendedInfo, Purchaser_ExtendedInfo_Relation>()
|
|
|
|
.InnerJoin((pei, per) => pei.Id == per.ExtendedInfoId)
|
|
|
|
.Where((pei, per) => purchaserIdList.Contains(per.PurchaserId))
|
|
|
|
.ToList((pei, per) => new
|
|
|
|
{
|
|
|
|
pei.Id,
|
|
|
|
pei.Name,
|
|
|
|
pei.Type,
|
|
|
|
per.PurchaserId,
|
|
|
|
pei.ExtendInfo
|
|
|
|
});
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 最近采购的店铺商品
|
|
|
|
var recent30d = DateTime.Now.AddDays(-90);
|
|
|
|
var bePurchasedProductSkuList = fsql.Select<OrderPurchaseInfo, OrderPurchaseRelationInfo, Order, ProductSku>()
|
|
|
|
.InnerJoin((opi, opri, o, ps) => opi.OrderId == o.Id)
|
|
|
|
.InnerJoin((opi, opri, o, ps) => opri.PurchaseOrderId == opi.PurchaseOrderId)
|
|
|
|
.InnerJoin((opi, opri, o, ps) => opri.BelongSkuId == ps.Id)
|
|
|
|
.WhereIf(request.ShopId != null && request.ShopId > 0, (opi, opri, o, ps) => o.ShopId == request.ShopId)
|
|
|
|
.Where((opi, opri, o, ps) => o.OrderState != Enums.OrderState.已取消 &&
|
|
|
|
opi.IsEnabled == true &&
|
|
|
|
opi.CreateTime >= recent30d &&
|
|
|
|
purchaserIdList.Contains(opi.PurchaserId))
|
|
|
|
.GroupBy((opi, opri, o, ps) => new
|
|
|
|
{
|
|
|
|
ps.Id,
|
|
|
|
opi.PurchaserId,
|
|
|
|
ps.Logo,
|
|
|
|
ps.SkuName,
|
|
|
|
opri.SourceSkuId
|
|
|
|
})
|
|
|
|
.ToList(g => new
|
|
|
|
{
|
|
|
|
g.Key.Id,
|
|
|
|
g.Key.PurchaserId,
|
|
|
|
g.Key.Logo,
|
|
|
|
g.Key.SkuName,
|
|
|
|
g.Key.SourceSkuId,
|
|
|
|
MaxPurchasedTime = g.Max(g.Value.Item1.CreateTime)
|
|
|
|
});
|
|
|
|
var belongSkuIdList = bePurchasedProductSkuList.Select(x => x.SourceSkuId).Distinct().ToList();
|
|
|
|
var belongSkuList = _freeSqlMultiDBManager.BBWYCfsql.Select<BBWYB.Server.Model.Db.BBWY.ProductSku>(belongSkuIdList).ToList(ps => new { ps.Id, ps.Logo });
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
foreach (var purchaser in purchaserList)
|
|
|
|
{
|
|
|
|
#region 主营类目/标签
|
|
|
|
var currentExtendInfoList = purchaserExtendInfoList.Where(x => x.PurchaserId == purchaser.Id).ToList();
|
|
|
|
purchaser.CategoryList = currentExtendInfoList.Where(x => x.Type == Enums.PurchaserBasicInfoType.主营类目)
|
|
|
|
.Select(x => new PurchaserExtendedInfoResponse()
|
|
|
|
{
|
|
|
|
Id = x.Id,
|
|
|
|
Name = x.Name,
|
|
|
|
Type = x.Type,
|
|
|
|
ExtendInfo = x.ExtendInfo
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
purchaser.TagList = currentExtendInfoList.Where(x => x.Type == Enums.PurchaserBasicInfoType.标签)
|
|
|
|
.Select(x => new PurchaserExtendedInfoResponse()
|
|
|
|
{
|
|
|
|
Id = x.Id,
|
|
|
|
Name = x.Name,
|
|
|
|
Type = x.Type,
|
|
|
|
ExtendInfo = x.ExtendInfo
|
|
|
|
}).ToList();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 最近采购店铺商品
|
|
|
|
purchaser.Recent30dProductSku = bePurchasedProductSkuList.Where(x => x.PurchaserId == purchaser.Id)
|
|
|
|
.OrderByDescending(x => x.MaxPurchasedTime)
|
|
|
|
.Take(10)
|
|
|
|
.Select(x =>
|
|
|
|
{
|
|
|
|
var belongSku = belongSkuList.FirstOrDefault(b => b.Id == x.SourceSkuId);
|
|
|
|
return new RecentPurchasedSkuResponse()
|
|
|
|
{
|
|
|
|
Id = x.Id,
|
|
|
|
Logo = belongSku?.Logo ?? x.Logo,
|
|
|
|
SkuName = x.SkuName,
|
|
|
|
BelongSku = x.SourceSkuId,
|
|
|
|
MaxPurchasedTime = x.MaxPurchasedTime
|
|
|
|
};
|
|
|
|
}).ToList();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ListResponse<PurchaserResponse>()
|
|
|
|
{
|
|
|
|
Items = purchaserList,
|
|
|
|
TotalCount = count
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListResponse<PurchaserExtendedInfoResponse> QueryPurchaserCategoryList(QueryPurchaserExtendedRequest request)
|
|
|
|
{
|
|
|
|
var list = fsql.Select<PurchaserExtendedInfo>()
|
|
|
|
.WhereIf(request.Type != null, x => x.Type == request.Type)
|
|
|
|
.WhereIf(!string.IsNullOrEmpty(request.Keywords), x => x.Name.Contains(request.Keywords))
|
|
|
|
.Count(out var count)
|
|
|
|
.ToList<PurchaserExtendedInfoResponse>();
|
|
|
|
return new ListResponse<PurchaserExtendedInfoResponse> { Items = list, TotalCount = count };
|
|
|
|
}
|
|
|
|
|
|
|
|
public void EditPurchaserExtendedInfo(EditPurchaserExtendedInfoRequest request)
|
|
|
|
{
|
|
|
|
var insertRelationList = new List<Purchaser_ExtendedInfo_Relation>();
|
|
|
|
insertRelationList.AddRange(request.CategoryIdList.Select(x => new Purchaser_ExtendedInfo_Relation()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
PurchaserId = request.PurchaserId,
|
|
|
|
ExtendedType = Enums.PurchaserBasicInfoType.主营类目,
|
|
|
|
ExtendedInfoId = x
|
|
|
|
}));
|
|
|
|
|
|
|
|
insertRelationList.AddRange(request.TagIdList.Select(x => new Purchaser_ExtendedInfo_Relation()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
PurchaserId = request.PurchaserId,
|
|
|
|
ExtendedType = Enums.PurchaserBasicInfoType.标签,
|
|
|
|
ExtendedInfoId = x
|
|
|
|
}));
|
|
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
fsql.Delete<Purchaser_ExtendedInfo_Relation>().Where(r => r.PurchaserId == request.PurchaserId).ExecuteAffrows();
|
|
|
|
fsql.Update<Purchaser>(request.PurchaserId).Set(p => p.ManagmentMode, request.ManagmentMode).ExecuteAffrows();
|
|
|
|
if (insertRelationList.Count() > 0)
|
|
|
|
fsql.Insert(insertRelationList).ExecuteAffrows();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|