/// <summary> /// 添加商品信息 /// </summary> /// <param name="goodInfo">基本信息</param> /// <param name="goodInfoExtensions">扩展信息</param> public async Task <DataResult> AddGoodInfo(GoodInfo goodInfo, GoodInfoExtensions goodInfoExtensions, List <GoodInfoSKU> skuList) { ShopInfoExtensions shopInfoExtensions = await shopInfoExtensionsDbSet.FirstOrDefaultAsync(sh => sh.ShopId == goodInfo.ShopId); int goodCount = await CurrentDbSet.CountAsync(gr => gr.ShopId == goodInfo.ShopId); if (goodCount >= shopInfoExtensions.LimitGoodNum) { return new DataResult { Code = DataResultCode.Fail, Message = $"最多只能拥有{shopInfoExtensions.LimitGoodNum}个商品!" } } ; using (IDbContextTransaction transaction = Database.BeginTransaction(IsolationLevel.ReadCommitted)) { try { await CurrentDbSet.AddAsync(goodInfo); await goodInfoExtensionsDbSet.AddAsync(goodInfoExtensions); await goodInfoSKUDbSet.AddRangeAsync(skuList); string sql = $"update ShopInfoExtensions set GoodDataVersion = @newVersion where shopId = @shopId and GoodDataVersion = @oldVersion"; int result = this.Database.ExecuteSqlCommand(sql, new[] { new MySqlParameter("newVersion", Guid.NewGuid().ToString("N")), new MySqlParameter("oldVersion", shopInfoExtensions.GoodDataVersion), new MySqlParameter("shopId", shopInfoExtensions.ShopId), }); if (result <= 0) { transaction.Rollback(); return(new DataResult { Code = DataResultCode.Fail, Message = "添加失败,请重试!" }); } SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); return(new DataResult { Code = DataResultCode.Fail, Message = "添加失败,请重试!" }); } } return(new DataResult { Code = DataResultCode.Success, Message = "添加成功!" }); }
public async Task <DataResult> AddFileData(FileData fileData) { ShopInfoExtensions shopInfoExtensions = await shopInfoExtensionsDbSet.FirstOrDefaultAsync(sh => sh.ShopId == fileData.ShopId); int fileCount = await CurrentDbSet.CountAsync(gr => gr.ShopId == fileData.ShopId); if (fileCount >= shopInfoExtensions.LimitFileNum) { return new DataResult { Code = DataResultCode.Fail, Message = $"该商店最多只能拥有{shopInfoExtensions.LimitFileNum}张图片!" } } ; using (IDbContextTransaction transaction = Database.BeginTransaction(IsolationLevel.ReadCommitted)) { CurrentDbSet.Add(fileData); string sql = $"update ShopInfoExtensions set FileDataVersion = @newVersion where shopId = @shopId and FileDataVersion = @oldVersion"; int result = this.Database.ExecuteSqlCommand(sql, new[] { new MySqlParameter("newVersion", Guid.NewGuid().ToString("N")), new MySqlParameter("oldVersion", shopInfoExtensions.FileDataVersion), new MySqlParameter("shopId", shopInfoExtensions.ShopId), }); if (result <= 0) { transaction.Rollback(); return(new DataResult { Code = DataResultCode.Fail, Message = "添加失败,请重试!" }); } SaveChanges(); transaction.Commit(); } return(new DataResult { Code = DataResultCode.Success, Message = "添加成功!" }); } }
public void Test() { string shopId = "1"; ShopInfoExtensions shopInfoExtensions = new ShopInfoExtensions { ShopId = shopId, LimitFileNum = 100, LimitGroupNum = 100 }; shopInfoExtensions.GroupDataVersion = Guid.NewGuid().ToString("N"); shopInfoExtensions.FileDataVersion = Guid.NewGuid().ToString("N"); IShopInfoExtensionsBusiness shopInfoExtensionsBusiness = BusinessFactory.GetBusiness <IShopInfoExtensionsBusiness>(); shopInfoExtensionsBusiness.Delete(shopId); shopInfoExtensionsBusiness.Add(shopInfoExtensions); IFileGroupBusiness fileGroupBusiness = BusinessFactory.GetBusiness <IFileGroupBusiness>(); var f = fileGroupBusiness.Get(shopId, "123"); Assert.Null(f); FileGroup fg = new FileGroup(); fg.Name = "test"; fg.ShopId = shopId; var res = fileGroupBusiness.Add(fg).Result; Assert.False(res.Code == 0); fg.Name = "test2"; fg = fileGroupBusiness.Update(fg); Assert.False(fg.Name != "test2"); var res2 = fileGroupBusiness.GetByShopId(shopId).Result; Assert.False(res2.Count == 0); fileGroupBusiness.Delete(shopId, fg.Id); var res3 = fileGroupBusiness.GetByShopId(shopId).Result; Assert.True(res3.Count == 0); }
public ShopInfoExtensions Update(ShopInfoExtensions shopInfoExtensions) { shopInfoExtensionsContext.Update(shopInfoExtensions); return(shopInfoExtensionsContext.SingleOrDefault(f => f.ShopId == shopInfoExtensions.ShopId)); }