/// <summary> /// 获得一条数据 /// </summary> /// <returns></returns> public async Task <ApiResult <ErpPackLog> > GetByGuidAsync(string parm) { var model = ErpPackLogDb.GetById(parm); var res = new ApiResult <ErpPackLog> { statusCode = 200, data = model ?? new ErpPackLog() { } }; return(await Task.Run(() => res)); }
/// <summary> /// 批量添加数据 /// </summary> /// <returns></returns> public async Task <ApiResult <string> > AddBatchAsync(string guid, string json, string adminGuid) { var res = new ApiResult <string>() { data = "1", statusCode = (int)ApiEnum.Error }; try { if (string.IsNullOrEmpty(json)) { res.message = "商品不能为空~"; return(await Task.Run(() => res)); } var packModel = ErpPackLogDb.GetById(guid); //解析字符串转换成List对象 var goodsList = JsonConvert.DeserializeObject <List <ErpInOutLog> >(json); //根据条形码查询平台库存的数据 var codeList = goodsList.Select(m => m.GoodsGuid).ToList(); var skuList = ErpGoodsSkuDb.GetList(m => codeList.Contains(m.Guid)); //定义一个值,判断条形码里面没有的数据数量 var noSkuCount = 0; //根据条形码,查询商家库存信息 var shopSkuList = new List <ErpShopSku>(); //一个新的商家库存信息 var newShopSkuList = new List <ErpShopSku>(); //只有在出库的时候查询,入库不处理 if (packModel.Types == 2) { shopSkuList = ErpShopSkuDb.GetList(m => codeList.Contains(m.SkuGuid) && m.ShopGuid == packModel.ShopGuid); } //循环平台数据,减少相应的库存 foreach (var item in skuList) { var goodsModel = goodsList.Find(m => m.GoodsGuid == item.Guid); //出库 if (goodsModel != null && packModel.Types == 2) { item.StockSum = item.StockSum - goodsModel.GoodsSum; //根据条形码判断商家库存是否存在,如果存在,修改库存信息,不存在增加一条新的库存信息 if (shopSkuList.Count > 0) { var shopSkuModel = shopSkuList.Find(m => m.SkuGuid == item.Guid); if (shopSkuModel != null) { shopSkuList.Find(m => m.SkuGuid == item.Guid).Stock = shopSkuModel.Stock + goodsModel.GoodsSum; } else { newShopSkuList.Add(new ErpShopSku() { Guid = Guid.NewGuid().ToString(), SkuGuid = item.Guid, SkuCode = item.Code, ShopGuid = packModel.ShopGuid, Stock = goodsModel.GoodsSum }); } } else { newShopSkuList.Add(new ErpShopSku() { Guid = Guid.NewGuid().ToString(), SkuGuid = item.Guid, SkuCode = item.Code, ShopGuid = packModel.ShopGuid, Stock = goodsModel.GoodsSum }); } } //入库 else if (goodsModel != null && packModel.Types == 1) { item.StockSum = item.StockSum + goodsModel.GoodsSum; } else { noSkuCount++; } } if (noSkuCount != 0) { res.message = "添加的商品在平台没有查询到~"; return(await Task.Run(() => res)); } //循环构造出入库详情数据 foreach (var item in goodsList) { item.Guid = Guid.NewGuid().ToString(); item.Types = packModel.Types; item.InTypes = 1; item.PackGuid = packModel.Guid; item.ShopGuid = packModel.ShopGuid; item.AdminGuid = adminGuid; item.AddDate = DateTime.Now; } packModel.GoodsSum += goodsList.Sum(m => m.GoodsSum); var result = Db.Ado.UseTran(() => { //修改打包的数量 Db.Updateable(packModel).ExecuteCommand(); //添加该打包日志下面的商品 Db.Insertable(goodsList).ExecuteCommand(); //修改平台库存数量 Db.Updateable(skuList).ExecuteCommand(); if (packModel.Types == 2) { if (shopSkuList.Count > 0) { Db.Updateable(shopSkuList).ExecuteCommand(); } if (newShopSkuList.Count > 0) { Db.Insertable(newShopSkuList).ExecuteCommand(); } } }); res.statusCode = (int)ApiEnum.Status; if (!result.IsSuccess) { res.statusCode = (int)ApiEnum.Error; res.message = result.ErrorMessage; } } catch (Exception ex) { res.message = ApiEnum.Error.GetEnumText() + ex.Message; } return(await Task.Run(() => res)); }