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 System; using System.Collections.Generic; using System.Linq; using Yitter.IdGenerator; namespace BBWY.Server.Business { public class EvaluationAssistantBusiness : BasePlatformRelayBusiness, IDenpendency { private IFreeSql fsql; private IIdGenerator idGenerator; public EvaluationAssistantBusiness(RestApiService restApiService, IOptions options, YunDingBusiness yunDingBusiness, IFreeSql fsql, IIdGenerator idGenerator) : base(restApiService, options, yunDingBusiness) { this.fsql = fsql; this.idGenerator = idGenerator; } #region 赠品模板 public void AddOrEditGiftTemplate(AddOrEditGiftTemplateRequest request) { var giftCount = request.GiftSkus.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Count(); 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(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 GetGiftTemplateList(long shopId) { return fsql.Select().Where(g => g.ShopId == shopId).ToList(); } public void DeleteGiftTemplate(long giftTemplateId) { fsql.Delete(giftTemplateId).ExecuteAffrows(); } #endregion #region 评价助手任务 public void AddOrEditPromotionTask(AddOrEditPromotionTaskRequest request) { if (request.Id == 0) { var sort = fsql.Select().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 }; fsql.Insert(promotionTask).ExecuteAffrows(); } else { var dbPromotionTask = fsql.Select(request.Id).ToOne(); if (dbPromotionTask == null) throw new BusinessException("活动任务不存在"); if (dbPromotionTask.Status != Enums.PromitionTaskStatus.等待) throw new BusinessException("只能在活动处于等待状态时才能修改"); request.Map(dbPromotionTask); fsql.Update().SetSource(dbPromotionTask) .IgnoreColumns(new string[] { "UpdateSortTime", "Sort", "Status", "CreateTime" }) .ExecuteAffrows(); } } /// /// 获取活动列表 /// /// /// public PromotionTaskResponse GetPromotionTaskList(QueryPromotionTaskRequest request) { var list = fsql.Select().Where(pt => pt.ShopId == request.ShopId) .Page(request.PageIndex, request.PageSize) .Count(out long count) .OrderByDescending(pt => new { pt.Sort, pt.UpdateSortTime }) .ToList(); return new PromotionTaskResponse() { Count = count, ItemList = list }; } /// /// 修改活动排序 /// /// public void EditPromotionTaskSort(EditPromotionTaskSortRequest request) { fsql.Update(request.Id).Set(pt => pt.Sort + request.MoveType) .Set(pt => pt.UpdateSortTime, DateTime.Now) .ExecuteAffrows(); } #endregion } }