/// <summary> /// 取消收藏 /// </summary> public async Task <bool> UnCollectAsync(long id, long userId) { using (var dapper = DapperFactory.CreateWithTrans()) { var @event = await dapper.QueryOneAsync <ArticleUserEvent>(new { ArticleId = id, UserId = userId, Event = EmArticleUserEvent.Collect }); if (@event == null) { return(true); } else { if (@event.IsCancel) { throw new KuException("你已取消收藏!"); } await dapper.UpdateAsync <ArticleUserEvent>(new { IsCancel = true, IsDeleted = false, EventTime = DateTime.Now, CancelTime = DateTime.Now, }, new { @event.Id }); } var sql = $"update {dapper.Dialect.FormatTableName<Article>()} set {nameof(Article.Collects)}={nameof(Article.Collects)}-1 where Id=@Id"; if (await dapper.ExecuteAsync(sql, new { Id = id }) == 1) { dapper.Commit(); } } return(true); }
/// <summary> /// 回复 /// </summary> public async Task AdminReplyAsync(long dialogueId, string content, long adminUserId) { using (var dapper = DapperFactory.CreateWithTrans()) { //取得对话记录 var dialogue = await dapper.QueryOneAsync <UserDialogue>(new { Id = dialogueId, IsDeleted = false }); if (dialogue == null) { throw new KuException("无法取得对话信息!"); } //新增对话消息 var model = new UserDialogueMessage { Id = ID.NewID(), DialogueId = dialogue.Id, UserId = adminUserId, CreateTime = DateTime.Now, IsDeleted = false, Message = content, IsAdmin = true, }; await dapper.InsertAsync <UserDialogueMessage>(model); dapper.Commit(); } }
/// <summary> /// 保存数据 /// </summary> public async Task AddAsync(long userId, string message) { using (var dapper = DapperFactory.CreateWithTrans()) { //取得对话记录 var dialogue = await dapper.QueryOneAsync <UserDialogue>(new { UserId = userId, IsDeleted = false }); if (dialogue != null && dialogue.IsForbidden) { //已禁言 throw new KuException("您暂时无法留言!"); } if (dialogue == null) { //新增对话 dialogue = new UserDialogue { Id = ID.NewID(), UserId = userId, CreateTime = DateTime.Now, IsDeleted = false, LastMessageSummary = message.Substr(0, 120, "..."), LastMessageTime = DateTime.Now, IsForbidden = false, IsSolved = false }; await dapper.InsertAsync <UserDialogue>(dialogue); } else { //更新对话信息 var item = new { LastMessageSummary = message.Substring(0, 128), LastMessageTime = DateTime.Now, IsSolved = false, SolveTime = null as DateTime?, SolveUserId = null as long? }; await dapper.UpdateAsync <UserDialogue>(item, new { dialogue.Id }); } //新增对话消息 var model = new UserDialogueMessage { Id = ID.NewID(), DialogueId = dialogue.Id, UserId = userId, CreateTime = DateTime.Now, IsDeleted = false, Message = message, IsAdmin = false, }; await dapper.InsertAsync <UserDialogueMessage>(model); dapper.Commit(); } }
/// <summary> /// 保存数据 /// </summary> public async Task SaveAsync(MenuDto dto) { Menu model = Mapper.Map <Menu>(dto); if (model.Id == 0) { //新增 using (var dapper = DapperFactory.CreateWithTrans()) { //取得父功能 if (model.ParentId.HasValue) { var pModel = await dapper.QueryOneAsync <Menu>(new { Id = model.ParentId.Value }); if (pModel == null) { throw new VinoDataNotFoundException("无法取得父级菜单数据!"); } if (!pModel.HasSubMenu) { await dapper.UpdateAsync <Menu>(new { HasSubMenu = true }, new { pModel.Id }); } } else { model.ParentId = null; } model.Id = ID.NewID(); model.CreateTime = DateTime.Now; await dapper.InsertAsync(model); dapper.Commit(); } } else { //更新 using (var dapper = DapperFactory.Create()) { var item = new { //这里进行赋值 model.Name, model.AuthCode, model.IsShow, model.OrderIndex, model.Icon, model.Url, model.Tag }; await dapper.UpdateAsync <Menu>(item, new { model.Id }); } } }
/// <summary> /// 保存数据 /// </summary> public async Task SaveAsync(FunctionDto dto) { Function model = Mapper.Map <Function>(dto); if (model.Id == 0) { //新增 using (var dapper = DapperFactory.CreateWithTrans()) { //取得父功能 if (model.ParentId.HasValue) { var pModel = await dapper.QueryOneAsync <Function>(new { Id = model.ParentId.Value }); if (pModel == null) { throw new VinoDataNotFoundException("无法取得父模块数据!"); } if (!pModel.HasSub) { pModel.HasSub = true; await dapper.UpdateAsync <Function>(new { HasSub = true }, new { pModel.Id }); } model.Level = pModel.Level + 1; } else { model.ParentId = null; model.Level = 1; } model.Id = ID.NewID(); model.CreateTime = DateTime.Now; await dapper.InsertAsync(model); dapper.Commit(); } } else { //更新 using (var dapper = DapperFactory.Create()) { var item = new { //这里进行赋值 model.Name, model.AuthCode, model.IsEnable, }; await dapper.UpdateAsync <Function>(item, new { model.Id }); } } }
/// <summary> /// 保存数据 /// </summary> public async Task SaveAsync(AdvertisementDto dto, long[] boards) { Advertisement model = Mapper.Map <Advertisement>(dto); if (model.Id == 0) { //新增 model.Id = ID.NewID(); model.CreateTime = DateTime.Now; model.IsDeleted = false; using (var dapper = DapperFactory.CreateWithTrans()) { await dapper.InsertAsync(model); //广告位处理 var refs = boards?.Select(x => new AdvertisementBoardRef { AdvertisementId = model.Id, AdvertisementBoardId = x }); await dapper.InsertAsync <AdvertisementBoardRef>(refs.ToArray()); dapper.Commit(); } } else { //更新 using (var dapper = DapperFactory.CreateWithTrans()) { var item = new { //这里进行赋值 model.Name, model.Title, model.Provenance, model.ImageData, model.FlashUrl, model.IsPublished, model.Link, model.OrderIndex }; await dapper.UpdateAsync <Advertisement>(item, new { model.Id }); //广告位处理 await dapper.DeleteAsync <AdvertisementBoardRef>(new { AdvertisementId = model.Id }); var refs = boards?.Select(x => new AdvertisementBoardRef { AdvertisementId = model.Id, AdvertisementBoardId = x }); await dapper.InsertAsync <AdvertisementBoardRef>(refs.ToArray()); dapper.Commit(); } } }
/// <summary> /// 删除数据 /// </summary> /// <param name="id">主键</param> /// <returns></returns> public override async Task DeleteAsync(params long[] id) { using (var _dapper = DapperFactory.CreateWithTrans()) { //删除分组素材关联 await _dapper.DeleteAsync <UserMaterialGroupRef>(new DapperSql("GroupId IN @Ids", new { Ids = id })); await _dapper.DeleteAsync <UserMaterialGroup>(new DapperSql("Id IN @Ids", new { Ids = id })); _dapper.Commit(); } }
/// <summary> /// 保存数据 /// </summary> public async Task SaveAsync(BrandDto dto, long[] CategoryIds) { Brand model = Mapper.Map <Brand>(dto); if (model.Id == 0) { //新增 model.Id = ID.NewID(); model.CreateTime = DateTime.Now; model.IsDeleted = false; var refs = CategoryIds.Select(x => new BrandCategoryRef { BrandId = model.Id, ProductCategoryId = x }); using (var dapper = DapperFactory.CreateWithTrans()) { await dapper.InsertAsync(model); await dapper.InsertAsync(refs); dapper.Commit(); } } else { var refs = CategoryIds.Select(x => new BrandCategoryRef { BrandId = model.Id, ProductCategoryId = x }); //更新 using (var dapper = DapperFactory.CreateWithTrans()) { var item = new { //这里进行赋值 model.Name, model.Logo, model.Intro, model.IsEnable }; await dapper.UpdateAsync <Brand>(item, new { model.Id }); //类目关联 await dapper.DeleteAsync <BrandCategoryRef>(new { BrandId = model.Id }); await dapper.InsertAsync(refs); dapper.Commit(); } } }
/// <summary> /// 收藏 /// </summary> public async Task <bool> CollectAsync(long id, long userId) { using (var dapper = DapperFactory.CreateWithTrans()) { var @event = await dapper.QueryOneAsync <ArticleUserEvent>(new { ArticleId = id, UserId = userId, Event = EmArticleUserEvent.Collect }); if (@event == null) { @event = new ArticleUserEvent { Id = ID.NewID(), ArticleId = id, UserId = userId, Event = Domain.Enum.Content.EmArticleUserEvent.Collect, IsCancel = false, EventTime = DateTime.Now, CreateTime = DateTime.Now, CancelTime = null, IsDeleted = false }; await dapper.InsertAsync <ArticleUserEvent>(@event); } else { if ([email protected] && [email protected]) { throw new KuException("你已收藏成功!"); } await dapper.UpdateAsync <ArticleUserEvent>(new { IsCancel = false, IsDeleted = false, EventTime = DateTime.Now, CancelTime = (DateTime?)null, }, new { @event.Id }); } var sql = $"update {dapper.Dialect.FormatTableName<Article>()} set {nameof(Article.Collects)}={nameof(Article.Collects)}+1 where Id=@Id"; if (await dapper.ExecuteAsync(sql, new { Id = id }) == 1) { dapper.Commit(); } } return(true); }
/// <summary> /// 获得积分 /// </summary> /// <remarks>请在Transaction中使用</remarks> /// <returns></returns> public async Task GainAsync(long MemberId, EmMemberPointType MemberPointType, int Points, EmMemberPointBusType BusType, long BusId, string BusDesc, long?OperatorId) { using (var dapper = DapperFactory.CreateWithTrans()) { try { await _GainAsync(dapper, MemberId, MemberPointType, Points, BusType, BusId, BusDesc, OperatorId); dapper.Commit(); } catch (Exception) { dapper.Rollback(); throw; } } }
/// <summary> /// 调整积分 /// </summary> public async Task AdjustAsync(long[] MemberId, EmMemberPointType MemberPointType, int Points, EmMemberPointBusType BusType, long BusId, string BusDesc, long?OperatorId) { if (MemberId == null || MemberId.Length == 0) { throw new VinoArgNullException("请选择会员!"); } if (Points == 0) { throw new VinoArgNullException("调整积分不能为0!"); } if (Points < -9999 || Points > 9999) { throw new VinoArgNullException("调整积分超出范围!"); } using (var dapper = DapperFactory.CreateWithTrans()) { try { if (Points > 0) { foreach (var id in MemberId) { await _GainAsync(dapper, id, MemberPointType, Points, BusType, BusId, BusDesc, OperatorId); } } else { foreach (var id in MemberId) { await _ConsumeAsync(dapper, id, MemberPointType, Math.Abs(Points), BusType, BusId, BusDesc, OperatorId); } } dapper.Commit(); } catch (Exception) { dapper.Rollback(); throw; } } }
/// <summary> /// 添加评论 /// </summary> public async Task AddAsync(CommentDto dto) { Comment model = Mapper.Map <Comment>(dto); //新增 model.Id = ID.NewID(); model.CreateTime = DateTime.Now; model.IsDeleted = false; model.Praises = 0; using (var dapper = DapperFactory.CreateWithTrans()) { await dapper.InsertAsync(model); var sql = $"update {dapper.Dialect.FormatTableName<Article>()} set {nameof(Article.Comments)}={nameof(Article.Comments)}+1 where Id=@Id"; if (await dapper.ExecuteAsync(sql, new { Id = model.ArticleId }) == 1) { dapper.Commit(); } } }
/// <summary> /// 新增数据 /// </summary> public async Task AddAsync(PictureDto dto, long[] groups) { Picture model = Mapper.Map <Picture>(dto); //新增 model.CreateTime = DateTime.Now; model.IsDeleted = false; var grouprefs = groups?.Select(x => new UserMaterialGroupRef { MaterialId = model.Id, GroupId = x }); using (var dapper = DapperFactory.CreateWithTrans()) { await dapper.InsertAsync(model); //素材分组 await dapper.InsertAsync <UserMaterialGroupRef>(grouprefs); dapper.Commit(); } }
/// <summary> /// 保存数据 /// </summary> public async Task SaveAsync(ProductDto dto, List <ProductSkuDto> skus) { Product model = Mapper.Map <Product>(dto); List <ProductSku> skuList = Mapper.Map <List <ProductSku> >(skus); if (skuList == null || skuList.Count == 0) { throw new VinoDataNotFoundException("至少添加一项商品SKU!"); } if (model.Id == 0) { //新增 model.Id = ID.NewID(); model.CreateTime = DateTime.Now; model.Sales = 0; model.Visits = 0; model.IsDeleted = false; model.IsSnapshot = false; model.SnapshotCount = 0; model.OriginId = null; model.EffectiveTime = DateTime.Now; foreach (var item in skuList) { item.ProductId = model.Id; item.CreateTime = DateTime.Now; item.IsDeleted = false; item.Sales = 0; if (item.PointsGainRule != EmPointsGainRule.ProductSku) { item.GainPoints = 0; } } model.Stock = skuList.Sum(x => x.Stock); var maxPrice = skuList.Max(x => x.Price); var minPrice = skuList.Min(x => x.Price); if (maxPrice == minPrice) { model.PriceRange = maxPrice + ""; } else { model.PriceRange = minPrice + "~" + maxPrice; } using (var dapper = DapperFactory.CreateWithTrans()) { await dapper.InsertAsync(model); await dapper.InsertAsync <ProductSku>(skuList); dapper.Commit(); } } else { ////更新 //var item = await _repository.GetByIdAsync(model.Id); //if (item == null || item.IsDeleted) //{ // throw new VinoDataNotFoundException("无法取得商品数据!"); //} //if (item.IsSnapshot) //{ // throw new VinoDataNotFoundException("快照数据无法修改!"); //} //using (var trans = await _repository.BeginTransactionAsync()) //{ // //生成快照 // Product snapshot = item.Copy(); // snapshot.Id = ID.NewID(); // snapshot.IsDeleted = false; // snapshot.CreateTime = DateTime.Now; // snapshot.ExpireTime = DateTime.Now; // snapshot.IsSnapshot = true; // snapshot.OriginId = item.OriginId; // await _repository.InsertAsync(snapshot); // //var search = new ProductSkuSearch { ProductId = item.Id }; // //包含已删除数据 // var skuItems = await _skuRepository.QueryAsync((x=>x.ProductId == item.Id), null, null); // var snapshotSkus = new List<ProductSku>(); // foreach (var sku in skuItems) // { // ProductSku snapshotSku = sku.Copy(); // snapshotSku.Id = ID.NewID(); // snapshotSku.ProductId = snapshot.Id; // snapshotSkus.Add(snapshotSku); // } // await _skuRepository.InsertRangeAsync(snapshotSkus); // //end // model.Stock = skuList.Sum(x => x.Stock); // var maxPrice = skuList.Max(x => x.Price); // var minPrice = skuList.Min(x => x.Price); // if (maxPrice == minPrice) // { // model.PriceRange = maxPrice + ""; // } // else // { // model.PriceRange = minPrice + "~" + maxPrice; // } // item.Status = model.Status; // item.Name = model.Name; // item.Title = model.Title; // item.Intro = model.Intro; // item.ImageData = model.ImageData; // item.ContentType = model.ContentType; // item.Content = model.Content; // item.PriceRange = model.PriceRange; // item.Stock = model.Stock; // item.OrderIndex = model.OrderIndex; // item.Properties = model.Properties; // item.CategoryId = model.CategoryId; // item.SnapshotCount = item.SnapshotCount + 1; // item.EffectiveTime = DateTime.Now; // //var search = new ProductSkuSearch { ProductId = item.Id }; // //var skuItems = await _skuRepository.QueryAsync(search.GetExpression(), null); // //新增 // var newSkus = skuList.Where(x => x.ModifyStatus == Domain.Enum.EmEntityModifyStatus.Insert); // foreach (var sku in newSkus) // { // sku.ProductId = item.Id; // sku.CreateTime = DateTime.Now; // sku.IsDeleted = false; // sku.Sales = 0; // if (sku.PointsGainRule != EmPointsGainRule.ProductSku) // { // sku.GainPoints = 0; // } // } // await _skuRepository.InsertRangeAsync(newSkus.ToList()); // //删除 // foreach (var sku in skuList.Where(x => x.ModifyStatus == Domain.Enum.EmEntityModifyStatus.Delete)) // { // await _skuRepository.DeleteAsync(sku.Id); // } // //更新 // foreach (var sku in skuList.Where(x => x.ModifyStatus == Domain.Enum.EmEntityModifyStatus.Update)) // { // var skuItem = _skuRepository.GetById(sku.Id); // if (skuItem == null || skuItem.IsDeleted) // { // trans.Rollback(); // throw new VinoDataNotFoundException("SKU数据无法取得!"); // } // skuItem.Title = sku.Title; // skuItem.CoverImage = sku.CoverImage; // skuItem.Price = sku.Price; // skuItem.MarketPrice = sku.MarketPrice; // skuItem.Stock = sku.Stock; // skuItem.OrderIndex = sku.OrderIndex; // skuItem.PointsGainRule = sku.PointsGainRule; // skuItem.GainPoints = sku.GainPoints; // if (skuItem.PointsGainRule != EmPointsGainRule.ProductSku) // { // skuItem.GainPoints = 0; // } // _skuRepository.Update(skuItem); // } // _repository.Update(item); // await _repository.SaveAsync(); // trans.Commit(); //} } }
/// <summary> /// 同步数据 /// </summary> public async Task SyncAsync(long accountId) { //取得微信AccessToken var token = await _wxAccountService.GetAccessToken(accountId); //开始同步用户标签 var tagsRsp = await wcUserTool.GetUserTagListAsync(token); if (tagsRsp.ErrCode != 0) { throw new KuDataNotFoundException(tagsRsp.ToString()); } var tags = tagsRsp.Data.Tags; using (var dapper = DapperFactory.CreateWithTrans()) { //取得当前所有标签 var localTags = await dapper.QueryListAsync <WxUserTag>(new { AccountId = accountId }); var deltags = new List <long>(); foreach (var item in localTags) { //判断是否还有该数据 var newTag = tags.FirstOrDefault(x => x.Id == item.TagId); if (newTag == null) { //远端已删除 deltags.Add(item.Id); //await wxUserTagRepository.DeleteAsync(item.Id); } else { //更新本地数据 if (!item.Name.Equals(newTag.Name) || item.Count != newTag.Count) { item.Name = newTag.Name; item.Count = newTag.Count; await dapper.UpdateAsync <WxUserTag>(new { newTag.Name, newTag.Count }, new { item.Id }); } tags.Remove(newTag); } } if (deltags.Any()) { await dapper.DeleteAsync <WxUserTag>(new DapperSql("Id IN @Ids", new { Ids = deltags.ToArray() })); } //保存新的标签 var newTags = tags.Select(item => new WxUserTag { Id = ID.NewID(), AccountId = accountId, TagId = item.Id, Name = item.Name, Count = item.Count }); await dapper.InsertAsync(newTags); //保存 dapper.Commit(); } using (var dapper = DapperFactory.Create()) { //开始同步用户 string nextOpenId = null; do { var rsp = await wcUserTool.GetUserListAsync(token, nextOpenId); if (rsp.ErrCode != 0) { throw new KuArgNullException(rsp.ToString()); } if (rsp.Data.Data.Openid == null || rsp.Data.Data.Openid.Length == 0) { break; } //处理数据 foreach (var openid in rsp.Data.Data.Openid) { await SyncOneOpenidAsync(dapper, token, accountId, openid); } nextOpenId = rsp.Data.Data.NextOpenid; if (nextOpenId.IsNullOrEmpty()) { break; } } while (true); } //结束同步 }
/// <summary> /// 保存数据 /// </summary> public async Task SaveAsync(ArticleDto dto) { Article model = Mapper.Map <Article>(dto); if (model.Id == 0) { //新增 model.Id = ID.NewID(); model.Keyword = model.Keyword.R(",", ","); model.CreateTime = DateTime.Now; model.Visits = 0; model.Praises = 0; model.Collects = 0; model.Comments = 0; model.Tags = "|" + string.Join("|", model.Tags.SplitRemoveEmpty(',')) + "|"; if (model.IsPublished && !model.PublishedTime.HasValue) { model.PublishedTime = DateTime.Now; } model.IsDeleted = false; using (var dapper = DapperFactory.CreateWithTrans()) { await dapper.InsertAsync(model); if (model.AlbumId.HasValue) { var cnt = await dapper.QueryCountAsync <Article>(new { AlbumId = model.AlbumId.Value, IsPublished = true }); await dapper.UpdateAsync <Album>(new { Videos = cnt }, new { id = model.AlbumId.Value }); } dapper.Commit(); } } else { //更新 using (var dapper = DapperFactory.CreateWithTrans()) { var entity = await dapper.QueryOneAsync <Article>(new { model.Id }); if (entity == null) { throw new KuDataNotFoundException("无法取得相关数据!"); } dynamic item = new ExpandoObject(); item.Title = model.Title; item.Author = model.Author; item.Provenance = model.Provenance; item.CoverData = model.CoverData; item.OrderIndex = model.OrderIndex; item.Keyword = model.Keyword.R(",", ","); item.SubTitle = model.SubTitle; item.IsPublished = model.IsPublished; item.Content = model.Content; item.ContentType = model.ContentType; item.Intro = model.Intro; item.Duration = model.Duration; item.Tags = "|" + string.Join("|", model.Tags.SplitRemoveEmpty(',')) + "|"; if (model.IsPublished && !model.PublishedTime.HasValue) { model.PublishedTime = DateTime.Now; } item.PublishedTime = model.PublishedTime; await dapper.UpdateAsync <Article>(item, new { model.Id }); if (entity.AlbumId.HasValue) { var cnt = await dapper.QueryCountAsync <Article>(new { AlbumId = entity.AlbumId.Value, IsPublished = true }); await dapper.UpdateAsync <Album>(new { Videos = cnt }, new { id = entity.AlbumId.Value }); } dapper.Commit(); } } //清除缓存 }