|
|
|
using BBWY.Common.Extensions;
|
|
|
|
using BBWY.Common.Http;
|
|
|
|
using BBWY.Common.Models;
|
|
|
|
using BBWY.Server.Model;
|
|
|
|
using BBWY.Server.Model.Db;
|
|
|
|
using BBWY.Server.Model.Dto;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
|
|
|
namespace BBWY.Server.Business
|
|
|
|
{
|
|
|
|
public class EvaluationAssistantBusiness : BasePlatformRelayBusiness, IDenpendency
|
|
|
|
{
|
|
|
|
private IFreeSql fsql;
|
|
|
|
|
|
|
|
private IIdGenerator idGenerator;
|
|
|
|
|
|
|
|
public EvaluationAssistantBusiness(RestApiService restApiService, IOptions<GlobalConfig> options, YunDingBusiness yunDingBusiness, IFreeSql fsql, IIdGenerator idGenerator) : base(restApiService, options, yunDingBusiness)
|
|
|
|
{
|
|
|
|
this.fsql = fsql;
|
|
|
|
this.idGenerator = idGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region 赠品模板
|
|
|
|
public void AddOrEditGiftTemplate(AddOrEditGiftTemplateRequest request)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(request.TemplateName))
|
|
|
|
throw new BusinessException("缺少模板名称");
|
|
|
|
if (string.IsNullOrEmpty(request.GiftSkus))
|
|
|
|
throw new BusinessException("缺少赠品SKU");
|
|
|
|
|
|
|
|
var giftCount = request.GiftSkus.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Count();
|
|
|
|
|
|
|
|
if (giftCount == 0)
|
|
|
|
throw new BusinessException("缺少赠品SKU");
|
|
|
|
|
|
|
|
if (request.Id == 0)
|
|
|
|
{
|
|
|
|
var giftTemplate = new GiftTemplate()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
TemplateName = request.TemplateName,
|
|
|
|
Platform = Enums.Platform.京东,
|
|
|
|
ShopId = request.ShopId,
|
|
|
|
TemplateSpu = request.TemplateSpu,
|
|
|
|
GiftCount = giftCount,
|
|
|
|
GiftSkus = request.GiftSkus
|
|
|
|
};
|
|
|
|
fsql.Insert(giftTemplate).ExecuteAffrows();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fsql.Update<GiftTemplate>(request.Id).Set(g => g.TemplateName, request.TemplateName)
|
|
|
|
.Set(g => g.TemplateSpu, request.TemplateSpu)
|
|
|
|
.Set(g => g.GiftSkus, request.GiftSkus)
|
|
|
|
.Set(g => g.GiftCount, giftCount)
|
|
|
|
.ExecuteAffrows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IList<GiftTemplateResponse> GetGiftTemplateList(long shopId)
|
|
|
|
{
|
|
|
|
return fsql.Select<GiftTemplate>().Where(g => g.ShopId == shopId).ToList<GiftTemplateResponse>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DeleteGiftTemplate(long giftTemplateId)
|
|
|
|
{
|
|
|
|
fsql.Delete<GiftTemplate>(giftTemplateId).ExecuteAffrows();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 评价助手任务
|
|
|
|
public void AddOrEditPromotionTask(AddOrEditPromotionTaskRequest request)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(request.MainProductSpu))
|
|
|
|
throw new BusinessException("缺少主商品SPU");
|
|
|
|
|
|
|
|
if (request.MotherTemplateId == 0 && string.IsNullOrEmpty(request.MainProductSku))
|
|
|
|
throw new BusinessException("奶妈模板和主商品SKU至少具备一个");
|
|
|
|
|
|
|
|
if (request.GiftTemplateId == 0 && string.IsNullOrEmpty(request.MainProductGiftSku))
|
|
|
|
throw new BusinessException("赠品模板和主商品赠品SKU至少具备一个");
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(request.SimpleTitle) || string.IsNullOrEmpty(request.FullTitle))
|
|
|
|
throw new BusinessException("缺少精简标题或完整标题");
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(request.ActivityName))
|
|
|
|
throw new BusinessException("缺少活动名称");
|
|
|
|
|
|
|
|
if (request.ActivityName.Length > 10)
|
|
|
|
throw new BusinessException("活动名称不能超过10个字");
|
|
|
|
|
|
|
|
if (request.Id == 0)
|
|
|
|
{
|
|
|
|
var relayAPIHost = GetPlatformRelayAPIHost(Enums.Platform.京东);
|
|
|
|
var restApiResult_spu = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetProductList", new SearchProductRequest()
|
|
|
|
{
|
|
|
|
AppKey = "E1AA9247D5583A6D87449CE6AB290185",
|
|
|
|
AppSecret = "e479558ddd9e40f8929cfc00c6cbbc9c",
|
|
|
|
AppToken = "01dc6f6e7fc34dcd99090d690312556cmdfk",
|
|
|
|
PageIndex = 1,
|
|
|
|
PageSize = 10,
|
|
|
|
Platform = Enums.Platform.京东,
|
|
|
|
Spu = request.MainProductSpu
|
|
|
|
}, GetYunDingRequestHeader(), HttpMethod.Post);
|
|
|
|
if (restApiResult_spu.StatusCode != System.Net.HttpStatusCode.OK)
|
|
|
|
throw new BusinessException(restApiResult_spu.Content) { Code = (int)restApiResult_spu.StatusCode };
|
|
|
|
var spuResponse = JsonConvert.DeserializeObject<ApiResponse<ProductListResponse>>(restApiResult_spu.Content);
|
|
|
|
if (!spuResponse.Success)
|
|
|
|
throw new BusinessException(spuResponse.Msg) { Code = spuResponse.Code };
|
|
|
|
|
|
|
|
var sort = fsql.Select<PromotionTask>().ToAggregate(p => p.Max(p.Key.Sort));
|
|
|
|
var promotionTask = new PromotionTask()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
ActivityName = request.ActivityName,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
FullTitle = request.FullTitle,
|
|
|
|
ShopId = request.ShopId,
|
|
|
|
SimpleTitle = request.SimpleTitle,
|
|
|
|
GiftTemplateId = request.GiftTemplateId,
|
|
|
|
MainProductGiftSku = request.MainProductGiftSku,
|
|
|
|
MainProductSku = request.MainProductSku,
|
|
|
|
MainProductSpu = request.MainProductSpu,
|
|
|
|
MotherTemplateId = request.MotherTemplateId,
|
|
|
|
IsEnabled = true,
|
|
|
|
Status = Enums.PromitionTaskStatus.等待,
|
|
|
|
PromotionId = 0,
|
|
|
|
Sort = sort + 1,
|
|
|
|
UpdateSortTime = DateTime.Now,
|
|
|
|
SpuLogo = spuResponse.Data.Items[0].Logo,
|
|
|
|
SpuPublishTime = spuResponse.Data.Items[0].CreateTime
|
|
|
|
};
|
|
|
|
fsql.Insert(promotionTask).ExecuteAffrows();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var dbPromotionTask = fsql.Select<PromotionTask>(request.Id).ToOne();
|
|
|
|
if (dbPromotionTask == null)
|
|
|
|
throw new BusinessException("活动任务不存在");
|
|
|
|
if (dbPromotionTask.Status != Enums.PromitionTaskStatus.等待)
|
|
|
|
throw new BusinessException("只能在活动处于等待状态时才能修改");
|
|
|
|
|
|
|
|
request.Map(dbPromotionTask);
|
|
|
|
fsql.Update<PromotionTask>().SetSource(dbPromotionTask)
|
|
|
|
.IgnoreColumns(new string[] { "UpdateSortTime", "Sort", "Status", "CreateTime", "SpuLogo", "SpuPublishTime", "StartTime", "EndTime" })
|
|
|
|
.ExecuteAffrows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取活动列表
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public PromotionTaskResponse GetPromotionTaskList(QueryPromotionTaskRequest request)
|
|
|
|
{
|
|
|
|
var list = fsql.Select<PromotionTask>().Where(pt => pt.ShopId == request.ShopId)
|
|
|
|
.OrderByDescending(pt => pt.Sort)
|
|
|
|
.OrderByDescending(pt => pt.UpdateSortTime)
|
|
|
|
.Page(request.PageIndex, request.PageSize)
|
|
|
|
.Count(out long count)
|
|
|
|
.ToList<PromotionTaskItemResponse>();
|
|
|
|
return new PromotionTaskResponse()
|
|
|
|
{
|
|
|
|
Count = count,
|
|
|
|
ItemList = list
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 修改活动排序
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request"></param>
|
|
|
|
public void EditPromotionTaskSort(EditPromotionTaskSortRequest request)
|
|
|
|
{
|
|
|
|
var dbPromotionTask = fsql.Select<PromotionTask>(request.Id).ToOne();
|
|
|
|
PromotionTask oldDbPromotionTask = null;
|
|
|
|
if (request.MoveType == 1)
|
|
|
|
oldDbPromotionTask = fsql.Select<PromotionTask>().Where(pt => pt.Sort > dbPromotionTask.Sort).OrderBy(pt => pt.Sort).ToOne();
|
|
|
|
else
|
|
|
|
oldDbPromotionTask = fsql.Select<PromotionTask>().Where(pt => pt.Sort < dbPromotionTask.Sort).OrderByDescending(pt => pt.Sort).ToOne();
|
|
|
|
|
|
|
|
|
|
|
|
if (oldDbPromotionTask != null)
|
|
|
|
{
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
fsql.Update<PromotionTask>(dbPromotionTask.Id).Set(pt => pt.Sort, oldDbPromotionTask.Sort)
|
|
|
|
.Set(pt => pt.UpdateSortTime, DateTime.Now)
|
|
|
|
.ExecuteAffrows();
|
|
|
|
fsql.Update<PromotionTask>(oldDbPromotionTask.Id).Set(pt => pt.Sort, dbPromotionTask.Sort)
|
|
|
|
.Set(pt => pt.UpdateSortTime, DateTime.Now)
|
|
|
|
.ExecuteAffrows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 开始活动任务
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request"></param>
|
|
|
|
public void StartPromotionTask(StartPromotionTaskRequest request)
|
|
|
|
{
|
|
|
|
var dbPromotionTask = fsql.Select<PromotionTask>(request.Id).ToOne();
|
|
|
|
|
|
|
|
if (dbPromotionTask == null)
|
|
|
|
throw new BusinessException("活动任务不存在");
|
|
|
|
if (dbPromotionTask.Status != Enums.PromitionTaskStatus.等待)
|
|
|
|
throw new BusinessException("只能在活动处于等待状态时才能开始");
|
|
|
|
|
|
|
|
|
|
|
|
var giftTemplateSku = string.Empty; //赠品模板sku
|
|
|
|
//var mainProductGiftSku = dbPromotionTask.MainProductGiftSku; //主商品的赠品sku
|
|
|
|
var motherTemplateSku = string.Empty; //奶妈模板的sku
|
|
|
|
|
|
|
|
if (dbPromotionTask.GiftTemplateId != null && dbPromotionTask.GiftTemplateId != 0)
|
|
|
|
{
|
|
|
|
var giftTemplate = fsql.Select<GiftTemplate>(dbPromotionTask.GiftTemplateId).ToOne();
|
|
|
|
if (giftTemplate == null)
|
|
|
|
throw new BusinessException("赠品模板不存在");
|
|
|
|
giftTemplateSku = giftTemplate.GiftSkus;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbPromotionTask.MotherTemplateId != null && dbPromotionTask.MotherTemplateId != 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|