/// <summary> /// 添加一条记录 /// </summary> /// <param name="parm"></param> /// <returns></returns> public async Task <ApiResult <string> > AddAsync(List <ErpSaleOrderGoods> parm) { var res = new ApiResult <string>() { data = "1", statusCode = 200 }; try { foreach (var item in parm) { item.Guid = Guid.NewGuid().ToString(); } var dbres = ErpSaleOrderGoodsDb.InsertRange(parm.ToArray()); if (!dbres) { res.statusCode = (int)ApiEnum.Error; res.message = "插入数据失败~"; } } catch (Exception ex) { res.statusCode = (int)ApiEnum.Error; res.message = ApiEnum.Error.GetEnumText() + ex.Message; } return(await Task.Run(() => res)); }
/// <summary> /// 查询多条记录 /// </summary> /// <returns></returns> public Task <ApiResult <Page <SaleOrderDto> > > GetPagesAsync(PageParm parm, AppSearchParm searchParm) { var res = new ApiResult <Page <SaleOrderDto> >(); try { var query = Db.Queryable <ErpSaleOrder, ErpShops>((eso, es) => new object[] { JoinType.Left, eso.ShopGuid == es.Guid }) .WhereIF(!string.IsNullOrEmpty(parm.guid), (eso, es) => eso.ShopGuid == parm.guid) .WhereIF(parm.types != 0, (eso, es) => eso.ActivityTypes == parm.types) .WhereIF(searchParm.saleType != 0, (eso, es) => eso.SaleType == searchParm.saleType) .WhereIF(searchParm.activityTypes != 0, (eso, es) => eso.ActivityTypes == searchParm.activityTypes) .WhereIF(!string.IsNullOrEmpty(searchParm.btime) && !string.IsNullOrEmpty(searchParm.etime), (eso, es) => eso.AddDate >= Convert.ToDateTime(searchParm.btime) && eso.AddDate <= Convert.ToDateTime(searchParm.etime)) .OrderBy((eso, es) => eso.AddDate, OrderByType.Desc) .Select((eso, es) => new SaleOrderDto() { Number = eso.Number, ShopName = es.ShopName, ActivityTypes = SqlFunc.ToString(eso.ActivityTypes), SaleType = SqlFunc.ToString(eso.SaleType), Counts = eso.Counts, ActivityName = eso.ActivityName, Money = eso.Money, RealMoney = eso.RealMoney, AddDate = eso.AddDate }) .ToPage(parm.page, parm.limit); //循环商品订单,查询订单下面的商品 var orderNumbers = query.Items?.Select(m => m.Number).ToList(); var orderGood = ErpSaleOrderGoodsDb.GetList(m => orderNumbers.Contains(m.OrderNumber)); foreach (var item in query.Items) { var list = new List <SaleOrderGoodsDto>(); foreach (var row in orderGood) { if (item.Number == row.OrderNumber) { var goodSku = ErpGoodsSkuDb.GetById(row.GoodsGuid); list.Add(new SaleOrderGoodsDto() { Counts = row.Counts, Code = goodSku.Code, GoodsName = SysCodeDb.GetById(goodSku.BrankGuid).Name + SysCodeDb.GetById(goodSku.StyleGuid).Name }); } } item.Goods = list; } res.success = true; res.message = "获取成功!"; res.data = query; } catch (Exception ex) { res.message = ApiEnum.Error.GetEnumText() + ex.Message; res.statusCode = (int)ApiEnum.Error; } return(Task.Run(() => res)); }
/// <summary> /// 查询库存剩余数量和销售数量 /// 可根据店铺查询,日期,品牌 /// </summary> /// <returns></returns> public Task <ApiResult <Page <StockSaleNum> > > GetStockNumByShopAsync(PageParm parm, AppSearchParm searchParm) { var res = new ApiResult <Page <StockSaleNum> >(); try { var query = Db.Queryable <ErpShopSku, ErpGoodsSku>((t1, t2) => new object[] { JoinType.Left, t1.SkuGuid == t2.Guid }) .Where((t1, t2) => t1.ShopGuid == parm.guid) .WhereIF(!string.IsNullOrEmpty(searchParm.brand), (t1, t2) => t2.BrankGuid == searchParm.brand) .OrderByIF(parm.orderType == 1, (t1, t2) => t1.Sale, OrderByType.Desc) .OrderByIF(parm.orderType == 2, (t1, t2) => t1.Stock, OrderByType.Desc) .Select((t1, t2) => new StockSaleNum() { Guid = t2.Guid, Code = t2.Code, Brand = SqlFunc.Subqueryable <SysCode>().Where(g => g.Guid == t2.BrankGuid).Select(g => g.Name), Style = SqlFunc.Subqueryable <SysCode>().Where(g => g.Guid == t2.StyleGuid).Select(g => g.Name), Stock = t1.Stock, returnSum = SqlFunc.Subqueryable <ErpReturnGoods>().Where(g => g.GoodsGuid == t1.SkuGuid && g.ShopGuid == parm.guid).Sum(g => g.ReturnCount) }).ToPage(parm.page, parm.limit); //根据日期查询 var guidList = query.Items.Select(m => m.Guid).ToList(); if (parm.types == 0) { //所有 var dayList = ErpSaleOrderGoodsDb.GetList(m => guidList.Contains(m.GoodsGuid) && m.ShopGuid == parm.guid); foreach (var item in query.Items) { item.Sale = dayList.Where(m => m.GoodsGuid == item.Guid).Sum(m => m.Counts) - dayList.Where(m => m.GoodsGuid == item.Guid).Sum(m => m.BackCounts); } } if (parm.types == 1) { DateTime dayTime = Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString() + " 00:00:00"); //本日 var dayList = ErpSaleOrderGoodsDb.GetList(m => guidList.Contains(m.GoodsGuid) && m.ShopGuid == parm.guid && SqlFunc.DateIsSame(SqlFunc.Subqueryable <ErpSaleOrder>().Where(g => g.Number == m.OrderNumber).Select(g => g.AddDate), dayTime)); foreach (var item in query.Items) { item.Sale = dayList.Where(m => m.GoodsGuid == item.Guid).Sum(m => m.Counts) - dayList.Where(m => m.GoodsGuid == item.Guid).Sum(m => m.BackCounts); } } if (parm.types == 2) { //本月 DateTime now = DateTime.Now; DateTime d1 = new DateTime(now.Year, now.Month, 1); DateTime d2 = d1.AddMonths(1); var dayList = ErpSaleOrderGoodsDb.GetList(m => guidList.Contains(m.GoodsGuid) && m.ShopGuid == parm.guid && SqlFunc.Between(SqlFunc.Subqueryable <ErpSaleOrder>().Where(g => g.Number == m.OrderNumber).Select(g => g.AddDate), d1, d2)); foreach (var item in query.Items) { item.Sale = dayList.Where(m => m.GoodsGuid == item.Guid).Sum(m => m.Counts) - dayList.Where(m => m.GoodsGuid == item.Guid).Sum(m => m.BackCounts); } } if (parm.types == 3) { //自定义时间 var dayList = ErpSaleOrderGoodsDb.GetList(m => guidList.Contains(m.GoodsGuid) && m.ShopGuid == parm.guid && SqlFunc.Between(SqlFunc.Subqueryable <ErpSaleOrder>().Where(g => g.Number == m.OrderNumber).Select(g => g.AddDate), Convert.ToDateTime(searchParm.btime), Convert.ToDateTime(searchParm.etime))); foreach (var item in query.Items) { item.Sale = dayList.Where(m => m.GoodsGuid == item.Guid).Sum(m => m.Counts) - dayList.Where(m => m.GoodsGuid == item.Guid).Sum(m => m.BackCounts); } } res.data = query; } catch (Exception ex) { res.message = ApiEnum.Error.GetEnumText() + ex.Message; res.statusCode = (int)ApiEnum.Error; } return(Task.Run(() => res)); }
/// <summary> /// 添加一条数据 /// </summary> /// <returns></returns> public async Task <ApiResult <string> > AddAsync(ErpBackGoods parm) { var res = new ApiResult <string>() { data = "1", statusCode = (int)ApiEnum.Error }; try { DateTime dayTime = Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString() + " 00:00:00"); parm.Guid = Guid.NewGuid().ToString(); //查询今天退货数量 var dayCount = ErpBackGoodsDb.Count(m => SqlFunc.DateIsSame(m.AddDate, dayTime)); parm.Number = "BO-" + DateTime.Now.ToString("yyyyMMdd") + "-" + (1001 + dayCount); //根据条形码查询唯一编号 var goodSku = ErpGoodsSkuDb.GetSingle(m => m.Code == parm.GoodsGuid); if (goodSku != null) { parm.GoodsGuid = goodSku.Guid; } else { res.message = "商品不存在~"; return(await Task.Run(() => res)); } //判断退货商品,金额是否大于订单金额, 以及商品数量,是否大于订单出售数量 var orderModel = ErpSaleOrderDb.GetSingle(m => m.Number == parm.OrderNumber); if (orderModel == null) { res.message = "订单不存在~"; return(await Task.Run(() => res)); } //跨月不允许退货 if (orderModel.AddDate.Month != DateTime.Now.Month) { res.message = "跨月不允许退货~"; return(await Task.Run(() => res)); } //查询活动,是否买一赠一活动,如果是,需要单独处理下 var activityModel = new ErpShopActivity(); if (!string.IsNullOrEmpty(orderModel.ActivityGuid)) { activityModel = ErpShopActivityDb.GetSingle(m => m.Guid == orderModel.ActivityGuid); } //if (parm.BackMoney>orderModel.RealMoney) //{ // res.message = "退货金额不能大于订单金额~"; // return await Task.Run(() => res); //} //判断是否存在 var egbCount = ErpBackGoodsDb.Count(m => m.ShopGuid == parm.ShopGuid && m.GoodsGuid == parm.GoodsGuid && m.OrderNumber == parm.OrderNumber); if (egbCount > 0) { //买一增一,最多可以退货2次 if (activityModel != null && activityModel.Method == 3) { if (egbCount > 1) { res.message = "该退货信息已存在~"; return(await Task.Run(() => res)); } } else { res.message = "该退货信息已存在~"; return(await Task.Run(() => res)); } } //根据订单查询商品数量是否满足 var orderGoodsModel = new ErpSaleOrderGoods(); if (activityModel != null && activityModel.Method == 3) { //如果是买一赠一,第一次差=0的,第二次差!=0的 if (egbCount == 0) { orderGoodsModel = ErpSaleOrderGoodsDb.GetSingle(m => m.OrderNumber == parm.OrderNumber && m.GoodsGuid == parm.GoodsGuid && m.ShopGuid == parm.ShopGuid && m.Money == 0); } else { orderGoodsModel = ErpSaleOrderGoodsDb.GetSingle(m => m.OrderNumber == parm.OrderNumber && m.GoodsGuid == parm.GoodsGuid && m.ShopGuid == parm.ShopGuid && m.Money != 0); } } else { orderGoodsModel = ErpSaleOrderGoodsDb.GetSingle(m => m.OrderNumber == parm.OrderNumber && m.GoodsGuid == parm.GoodsGuid && m.ShopGuid == parm.ShopGuid); } if (orderGoodsModel == null) { res.message = "该商品在该订单号中不存在~"; return(await Task.Run(() => res)); } if (orderGoodsModel.Counts < parm.BackCount) { res.message = "退货商品数量不能大于订单购买数量~"; return(await Task.Run(() => res)); } //获取订单销售的金额 客户端不需要手动输入, 注意需要判断下是否买一赠一活动 parm.BackMoney = orderGoodsModel.Money; //构建积分变动记录 var result = Db.Ado.UseTran(() => { //修改加盟商条形码里面的库存 退货=加盟商库存增加 Db.Updateable <ErpShopSku>() .UpdateColumns(m => new ErpShopSku() { Stock = m.Stock + parm.BackCount, Sale = m.Sale - parm.BackCount }) .Where(m => m.ShopGuid == parm.ShopGuid && m.SkuGuid == goodSku.Guid) .ExecuteCommand(); //根据商品编号,修改平台的销售数量 Db.Updateable <ErpGoodsSku>().UpdateColumns(m => new ErpGoodsSku() { SaleSum = m.SaleSum - parm.BackCount }) .Where(m => m.Guid == goodSku.Guid).ExecuteCommand(); //修改订单-商品详情表中的退货数量 增加 orderGoodsModel.BackCounts = orderGoodsModel.BackCounts + parm.BackCount; Db.Updateable(orderGoodsModel).ExecuteCommand(); //增加一条退货信息 Db.Insertable(parm).ExecuteCommand(); //判断是否存在用户信息 if (!string.IsNullOrEmpty(orderModel.UserGuid)) { var pointLogModel = new ErpUserPointLog() { Guid = Guid.NewGuid().ToString(), UserGuid = orderModel.UserGuid, OperateGuid = parm.Guid, Types = 1, Point = orderGoodsModel.Money != 0?Convert.ToInt32((orderGoodsModel.Money / orderGoodsModel.Counts) / 10):0, Summary = "[减少]-退货积分变更" }; Db.Insertable(pointLogModel).ExecuteCommand(); //用户积分减少 Db.Updateable <ErpShopUser>().UpdateColumns(m => m.Points == m.Points - pointLogModel.Point) .Where(m => m.Guid == orderModel.UserGuid).ExecuteCommand(); } }); res.statusCode = (int)ApiEnum.Status; if (!result.IsSuccess) { res.statusCode = (int)ApiEnum.Error; res.message = result.ErrorMessage; } } catch (Exception ex) { res.statusCode = (int)ApiEnum.Error; res.message = ApiEnum.Error.GetEnumText() + ex.Message; } return(await Task.Run(() => res)); }
/// <summary> /// 添加一条数据 /// </summary> /// <returns></returns> public async Task <ApiResult <string> > AddAsync(ErpBackGoods parm) { var res = new ApiResult <string>() { data = "1", statusCode = (int)ApiEnum.Error }; try { DateTime dayTime = Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString() + " 00:00:00"); parm.Guid = Guid.NewGuid().ToString(); //查询今天退货数量 var dayCount = ErpBackGoodsDb.Count(m => SqlFunc.DateIsSame(m.AddDate, dayTime)); parm.Number = "BO-" + DateTime.Now.ToString("yyyyMMdd") + "-" + (1001 + dayCount); //根据条形码查询唯一编号 var goodSku = ErpGoodsSkuDb.GetSingle(m => m.Code == parm.GoodsGuid); if (goodSku != null) { parm.GoodsGuid = goodSku.Guid; } else { res.message = "商品不存在~"; return(await Task.Run(() => res)); } //判断退货商品,金额是否大于订单金额, 以及商品数量,是否大于订单出售数量 var orderModel = ErpSaleOrderDb.GetSingle(m => m.Number == parm.OrderNumber); if (orderModel == null) { res.message = "订单不存在~"; return(await Task.Run(() => res)); } if (parm.BackMoney > orderModel.RealMoney) { res.message = "退货金额不能大于订单金额~"; return(await Task.Run(() => res)); } //判断是否存在 var isExt = ErpBackGoodsDb.IsAny(m => m.ShopGuid == parm.ShopGuid && m.GoodsGuid == parm.GoodsGuid && m.OrderNumber == parm.OrderNumber); if (isExt) { res.message = "该退货信息已存在~"; return(await Task.Run(() => res)); } //根据订单查询商品数量是否满足 var orderGoodsModel = ErpSaleOrderGoodsDb.GetSingle(m => m.OrderNumber == parm.OrderNumber && m.GoodsGuid == parm.GoodsGuid && m.ShopGuid == parm.ShopGuid); if (orderGoodsModel == null) { res.message = "该商品在该订单号中不存在~"; return(await Task.Run(() => res)); } if (orderGoodsModel.Counts < parm.BackCount) { res.message = "退货商品数量不能大于订单购买数量~"; return(await Task.Run(() => res)); } var result = Db.Ado.UseTran(() => { //修改加盟商条形码里面的库存 退货=加盟商库存增加 Db.Updateable <ErpShopSku>() .UpdateColumns(m => new ErpShopSku() { Stock = m.Stock + parm.BackCount, Sale = m.Sale - parm.BackCount }) .Where(m => m.ShopGuid == parm.ShopGuid && m.SkuGuid == goodSku.Guid) .ExecuteCommand(); //根据商品编号,修改平台的销售数量 Db.Updateable <ErpGoodsSku>().UpdateColumns(m => new ErpGoodsSku() { SaleSum = m.SaleSum - parm.BackCount }) .Where(m => m.Guid == goodSku.Guid).ExecuteCommand(); //修改订单-商品详情表中的退货数量 增加 orderGoodsModel.BackCounts = orderGoodsModel.BackCounts + parm.BackCount; Db.Updateable(orderGoodsModel).ExecuteCommand(); //增加一条退货信息 Db.Insertable(parm).ExecuteCommand(); }); res.statusCode = (int)ApiEnum.Status; if (!result.IsSuccess) { res.statusCode = (int)ApiEnum.Error; res.message = result.ErrorMessage; } } catch (Exception ex) { res.statusCode = (int)ApiEnum.Error; res.message = ApiEnum.Error.GetEnumText() + ex.Message; } return(await Task.Run(() => res)); }