public async Task SaveData(F_PublishFood data) { if (data.Id.IsNullOrEmpty()) { InitEntity(data); await _f_PublishFoodBus.AddDataAsync(data); } else { InitUpdateEntity(data); await _f_PublishFoodBus.UpdateDataAsync(data); } }
public async Task PublishFoodDataAsync(List <string> ids, DateTime?dt) { if (!dt.HasValue) { dt = DateTime.Now; } var tempQuery = Service.GetIQueryable <F_PublishFood>().Where(a => ids.Contains(a.FoodInfoId) && a.PublishDate >= dt.Value.Date && a.PublishDate < dt.Value.Date.AddDays(1)) .ToList(); if (tempQuery.Count > 0) { throw new BusException("[" + string.Join(",", tempQuery.Select(a => a.FoodName)) + "]菜品" + dt.Value.ToString("yyyy-MM-dd") + "已经发布过,请重新选择!"); } var query = GetIQueryable().Where(a => ids.Contains(a.Id)).ToList(); if (query.Count == 0) { throw new BusException("数据异常!"); } if (query.GroupBy(a => a.ShopInfoId)?.Count() > 1) { throw new BusException("不能同时发布两个门店菜品!"); } string shopInfoId = query?.FirstOrDefault()?.ShopInfoId; var shopInfo = Service.GetIQueryable <F_ShopInfoSet>().FirstOrDefault(a => a.ShopInfoId == shopInfoId); var toDayFoodsCount = Service.GetIQueryable <F_PublishFood>().Count(a => a.PublishDate >= dt.Value.Date && a.PublishDate < dt.Value.Date.AddDays(1) && a.ShopInfoId == shopInfoId); List <F_PublishFood> publishFoodList = new List <F_PublishFood>(); query.ForEach(a => { F_PublishFood publishFood = new F_PublishFood() { Id = IdHelper.GetId(), CreateTime = DateTime.Now, CreatorId = oOperator.UserId, CreatorName = oOperator.Property.RealName, ShopInfoId = a.ShopInfoId, SupplierName = a.SupplierName, Price = a.Price, FoodQty = 999, FoodDesc = a.FoodDesc, FoodName = a.FoodName, ImgUrl = a.ImgUrl, PublishDate = dt.Value, FoodInfoId = a.Id, Limit = a.Limit.HasValue ? a.Limit:1 }; publishFoodList.Add(publishFood); }); await Service.InsertAsync(publishFoodList); //如果今天有发布的菜品则不再发送消息 if (shopInfo != null && !string.IsNullOrEmpty(shopInfo.OrderBeginRemind) && shopInfo.OrderBeginDate.HasValue && toDayFoodsCount <= 0) { //发送开始点餐信息 var beginTimeSpan = GetNowDay(shopInfo.OrderBeginDate.Value, dt.Value) - DateTime.Now; //如果当前时间已经过了点餐时间不在发送 if (shopInfo.OrderBeginEnd.HasValue && DateTime.Now < GetNowDay(shopInfo.OrderBeginEnd.Value, dt.Value)) { //添加发送消息 BackgroundJob.Schedule(() => PublishFoodSendToWeChat(shopInfoId, shopInfo.OrderBeginRemind, 0), beginTimeSpan); } } //如果今天有发布的菜品则不再发送消息 if (shopInfo != null && !string.IsNullOrEmpty(shopInfo.OrderEndRemind) && shopInfo.OrderBeginEnd.HasValue && toDayFoodsCount <= 0) { //发送结束点餐信息,提前5分钟 var endTimeSpan = GetNowDay(shopInfo.OrderBeginEnd.Value.AddMinutes(-5), dt.Value) - DateTime.Now; //如果当前时间已经过了点餐时间不发送 if (DateTime.Now < GetNowDay(shopInfo.OrderBeginEnd.Value, dt.Value)) { //添加发送消息 BackgroundJob.Schedule(() => PublishFoodSendToWeChat(shopInfoId, shopInfo.OrderEndRemind, 0), endTimeSpan); } } //如果今天有发布的菜品则不再发送消息 if (shopInfo != null && !string.IsNullOrEmpty(shopInfo.OrderReceiveRemind) && shopInfo.OrderReceiveDate.HasValue && toDayFoodsCount <= 0) { //发送领取餐品信息 var endTimeSpan = GetNowDay(shopInfo.OrderReceiveDate.Value, dt.Value) - DateTime.Now; //如果当前时间已经过了领取时间不发送 if (DateTime.Now < GetNowDay(shopInfo.OrderReceiveDate.Value, dt.Value)) { //添加发送消息 BackgroundJob.Schedule(() => PublishFoodSendToWeChat(shopInfoId, shopInfo.OrderReceiveRemind, shopInfo.IsRandomSendReceiveMsg?2:1), endTimeSpan); } } await Task.CompletedTask; }