/// <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 = "添加成功!" }); } }