Ejemplo n.º 1
0
        /// <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));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <returns></returns>
        public async Task <ApiResult <string> > AddAsync(ErpTransferGoods parm, List <TransferGoods> list)
        {
            var res = new ApiResult <string>()
            {
                statusCode = (int)ApiEnum.Error
            };

            try
            {
                //根据调拨单,查询入库信息
                var transferModel = ErpTransferDb.GetById(parm.TransferGuid);
                if (transferModel == null)
                {
                    res.message = "调拨单不存在~";
                    return(await Task.Run(() => res));
                }
                //增加调拨商品日志
                var listModel = new List <ErpTransferGoods>();
                //增加调拨的入库
                var inOutLogList = new List <ErpInOutLog>();
                //查询调拨的商品唯一编号
                var skuArray = list.Select(m => m.guid).ToList();
                //根据条形码编号,查询条形码信息
                var goodsSkuList = ErpGoodsSkuDb.GetList(m => skuArray.Contains(m.Guid));
                //查询调拨出库的商家条形码列表
                var outStockSkuList = ErpShopSkuDb.GetList(m => m.ShopGuid == transferModel.OutShopGuid && skuArray.Contains(m.SkuGuid));
                //查询调拨入库的商家条形码列表
                var inStockSkuList = ErpShopSkuDb.GetList(m => m.ShopGuid == transferModel.InShopGuid && skuArray.Contains(m.SkuGuid));

                var newShopSkuList = new List <ErpShopSku>();

                //创建一个出库到加盟商的打包日志
                var packModel = new ErpPackLog()
                {
                    Guid     = Guid.NewGuid().ToString(),
                    Types    = 2,
                    Mode     = 1,
                    Number   = "DB" + Utils.GetOrderNumber(),
                    PackName = "调拨",
                    GoodsSum = list.Sum(m => m.goodsSum),
                    ShopGuid = transferModel.InShopGuid
                };
                //循环操作
                foreach (var item in list)
                {
                    //出库减少库存
                    var sourceStockOutModel = outStockSkuList.Find(m => m.SkuGuid == item.guid);
                    outStockSkuList.Find(m => m.SkuGuid == item.guid).Stock = sourceStockOutModel.Stock - item.goodsSum;

                    //根据条形码,查询商家条形码表是否存在,如果存在,则修改-增加库存,不存在,增加一条数据
                    var sourceStockSkuInModel = inStockSkuList.Find(m => m.SkuGuid == item.guid);
                    if (sourceStockSkuInModel != null)
                    {
                        inStockSkuList.Find(m => m.SkuGuid == item.guid).Stock = sourceStockSkuInModel.Stock + item.goodsSum;
                    }
                    else
                    {
                        newShopSkuList.Add(new ErpShopSku()
                        {
                            SkuGuid  = item.guid,
                            SkuCode  = goodsSkuList.Find(m => m.Guid == item.guid).Code,
                            ShopGuid = transferModel.InShopGuid,
                            Stock    = item.goodsSum
                        });
                    }

                    //入库
                    inOutLogList.Add(new ErpInOutLog()
                    {
                        Guid      = Guid.NewGuid().ToString(),
                        Types     = 2,
                        ShopGuid  = transferModel.InShopGuid,
                        PackGuid  = packModel.Guid,
                        GoodsGuid = item.guid,
                        GoodsSum  = item.goodsSum,
                        AddDate   = DateTime.Now,
                        AdminGuid = parm.GoodsGuid,
                        InTypes   = 2
                    });

                    //调拨单商品
                    listModel.Add(new ErpTransferGoods()
                    {
                        Guid         = Guid.NewGuid().ToString(),
                        TransferGuid = parm.TransferGuid,
                        GoodsGuid    = item.guid,
                        GoodsSum     = item.goodsSum
                    });
                }
                //判断商品总数是否大于调拨单商品数量
                var transgerModel = ErpTransferDb.GetById(parm.TransferGuid);
                if (transgerModel.GoodsSum < listModel.Sum(m => m.GoodsSum))
                {
                    res.statusCode = (int)ApiEnum.Error;
                    res.message    = "调拨数量不能大于调拨单总数~";
                    return(await Task.Run(() => res));
                }

                var result = Db.Ado.UseTran(() =>
                {
                    //增加一条打包日志
                    Db.Insertable(packModel).ExecuteCommand();
                    //增加打包里面的商品列表
                    Db.Insertable(inOutLogList).ExecuteCommand();
                    //增加调拨单里面的商品记录
                    Db.Insertable(listModel).ExecuteCommand();

                    //更新调拨  选择出库的商家条形码库存数
                    Db.Updateable(outStockSkuList).ExecuteCommand();
                    //更新调拨  选择入库的商家条形码的库存,有的增加,没有的增加一条数据
                    Db.Updateable(inStockSkuList).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));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task <ApiResult <string> > AddAsync(ErpSaleOrder parm, string goodsJson)
        {
            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();
                //判断销售订单字符串是否为空
                if (string.IsNullOrEmpty(goodsJson))
                {
                    res.message = "销售订单里面的商品不能为空~";
                    return(await Task.Run(() => res));
                }
                //判断用户信息存在,如果存在需要根据用户查询编号
                ErpShopUser userModel = null;
                if (!string.IsNullOrEmpty(parm.UserGuid))
                {
                    //判断是否存在
                    userModel = ErpShopUserDb.GetSingle(m => m.Mobile == parm.UserGuid);
                    if (userModel == null)
                    {
                        res.message = "会员不存在~";
                        return(await Task.Run(() => res));
                    }
                    parm.UserGuid = userModel.Guid;
                }

                var isStockSuccess = true;
                //解析字符串转换成List对象
                var roGoodsList = JsonConvert.DeserializeObject <List <ErpSaleOrderGoods> >(goodsJson);
                //验证销售商品的数量是否大于库存数量
                foreach (var item in roGoodsList.GroupBy(m => m.GoodsGuid).Select(m => new ErpSaleOrderGoods {
                    GoodsGuid = m.Key, Counts = m.Sum(g => g.Counts)
                }).ToList())
                {
                    var shopStockSum = Db.Queryable <ErpShopSku>()
                                       .Where(m => m.ShopGuid == parm.ShopGuid && m.SkuGuid == item.GoodsGuid).First();
                    if (shopStockSum.Stock < item.Counts)
                    {
                        isStockSuccess = false;
                    }
                }
                if (!isStockSuccess)
                {
                    res.message = "商品库存数量";
                    return(await Task.Run(() => res));
                }

                //获得商品的所有id
                var goodIds = roGoodsList.Select(m => m.GoodsGuid).ToList();
                //根据商品获得列表
                var goodList = ErpGoodsSkuDb.GetList(m => goodIds.Contains(m.Guid));
                //根据店铺获得加盟商的库存信息
                var shopGoodsSkuList = ErpShopSkuDb.GetList(m => m.ShopGuid == parm.ShopGuid && goodIds.Contains(m.SkuGuid));
                //满减变量
                var fullJson = new List <ShopActivity>();
                //根据活动编号,查询活动详情,处理对应金额
                ErpShopActivity activityModel = null;
                if (!string.IsNullOrEmpty(parm.ActivityName))
                {
                    //查询活动
                    activityModel = ErpShopActivityDb.GetSingle(m => m.Guid == parm.ActivityName && m.Enable && !m.IsDel);
                    if (activityModel != null)
                    {
                        parm.ActivityGuid  = parm.ActivityName;
                        parm.ActivityName  = Utils.GetActivityMethod(activityModel.Method);
                        parm.ActivityTypes = Utils.GetActivityTypes(activityModel.Method);
                        if (!string.IsNullOrEmpty(activityModel.FullBack))
                        {
                            //满减======序列号满减对象
                            fullJson = JsonConvert.DeserializeObject <List <ShopActivity> >(activityModel.FullBack);
                        }
                    }
                }
                //根据活动算好订单金额,如果有活动,满减或者打折,最终金额会根据活动而变
                var roIndex = 0; //定义一个循环的标记
                foreach (var item in roGoodsList)
                {
                    roIndex++;
                    var skuItem = goodList.Find(m => m.Guid == item.GoodsGuid);
                    //增加到GoodsSku的销售数量
                    goodList.Find(m => m.Guid == item.GoodsGuid).SaleSum += item.Counts;
                    //修改加盟商条形码表中的库存减少,  销售数量增加
                    var shopSkuModel = shopGoodsSkuList.Find(m => m.SkuGuid == skuItem.Guid);
                    shopGoodsSkuList.Find(m => m.SkuGuid == item.GoodsGuid).Stock = shopSkuModel.Stock - item.Counts;
                    //修改加盟商条形码表中的销售数量   增加
                    shopGoodsSkuList.Find(m => m.SkuGuid == item.GoodsGuid).Sale = shopSkuModel.Sale + item.Counts;
                    //获得商品原价*购买商品的数量
                    parm.Money += Convert.ToDecimal(skuItem.SalePrice) * item.Counts;
                    //如果没有活动直接结算金额
                    if (activityModel == null)
                    {
                        if (parm.SaleType == 1)
                        {
                            parm.RealMoney += Convert.ToInt32(skuItem.SalePrice) * item.Counts;
                            item.Money      = Convert.ToInt32(skuItem.SalePrice);
                        }
                        else
                        {
                            item.Money = parm.RealMoney;
                        }
                    }
                    else
                    {
                        //统计非品牌活动,到下面去处理这个值
                        if (activityModel.Types != 2 && parm.SaleType == 1)
                        {
                            parm.RealMoney += Convert.ToInt32(skuItem.SalePrice) * item.Counts;
                        }
                    }
                    //整除销售计算价格,残次品价格是前端传过来的
                    if (activityModel != null && parm.SaleType == 1)
                    {
                        //按品牌——这里面只处理打折的,并且是按品牌的
                        if (activityModel.Method == 1 && activityModel.Types == 2)
                        {
                            if (skuItem.BrankGuid == activityModel.BrandGuid)
                            {
                                //品牌打折
                                var tempMoney = Convert.ToDecimal(skuItem.SalePrice) * item.Counts;
                                tempMoney       = tempMoney * (Convert.ToDecimal(activityModel.CountNum) / 100);
                                parm.RealMoney += tempMoney;
                                item.Money      = parm.RealMoney;
                            }
                            else
                            {
                                //不是该品牌部打折
                                parm.RealMoney += Convert.ToDecimal(skuItem.SalePrice) * item.Counts;
                                item.Money      = parm.RealMoney;
                            }
                        }
                        //按品牌——满减
                        else if (activityModel.Method == 2 && activityModel.Types == 2)
                        {
                            //查询该品牌价格是否满足满减需要
                            var brankMoney = goodList.Where(m => m.BrankGuid == activityModel.BrandGuid).Sum(m => Convert.ToInt32(m.SalePrice) * item.Counts);
                            var brankCount = goodList.Count(m => m.BrankGuid == activityModel.BrandGuid);
                            //是否满足品牌打折的要求
                            var isBrankOk = false;
                            //如果满足打折要求,满减值
                            var fullMoney = 0;
                            //循环判断符合满减对象
                            foreach (var fullItem in fullJson.OrderByDescending(m => m.fullbegin).ToList())
                            {
                                if (brankMoney >= fullItem.fullbegin)
                                {
                                    isBrankOk = true;
                                    fullMoney = fullItem.fullend;
                                    break;
                                }
                            }
                            //满足打折需求——根据品牌计算单个商品的价格  品牌平均分配价格
                            if (skuItem.BrankGuid == activityModel.BrandGuid && isBrankOk)
                            {
                                //单个商品的金额减满减值/品牌个数=订单详情商品的金额
                                item.Money      = Convert.ToDecimal(skuItem.SalePrice) * item.Counts - Convert.ToDecimal(fullMoney / brankCount);
                                parm.RealMoney += item.Money;
                            }
                            else
                            {
                                item.Money      = Convert.ToDecimal(skuItem.SalePrice) * item.Counts;
                                parm.RealMoney += item.Money;
                            }
                        }
                        //按品牌——买一增一
                        else if (activityModel.Method == 3 && activityModel.Types == 2)
                        {
                            //按品牌买一增一
                            if (skuItem.BrankGuid == activityModel.BrandGuid)
                            {
                                if (roIndex == 1)
                                {
                                    item.Money      = Convert.ToDecimal(skuItem.SalePrice) * item.Counts;
                                    parm.RealMoney += item.Money;
                                }
                                else
                                {
                                    item.Money = 0;
                                }
                            }
                            else
                            {
                                //不是该品牌,不参与活动   既然后是品牌买一赠一活动,实际上不应该出现非品牌的商品出现
                                item.Money      = Convert.ToDecimal(skuItem.SalePrice) * item.Counts;
                                parm.RealMoney += item.Money;
                            }
                        }
                        //按全部店铺——打折
                        else if (activityModel.Method == 1 && activityModel.Types != 2)
                        {
                            //全部商铺,也就是所有金额
                            var zhVal = Convert.ToDecimal(activityModel.CountNum) / 100;
                            item.Money = (Convert.ToDecimal(skuItem.SalePrice) * item.Counts) * zhVal;
                        }
                        //按全部店铺——满减
                        else if (activityModel.Method == 2 && activityModel.Types != 2)
                        {
                            var allMoney = goodList.Sum(m => Convert.ToInt32(m.SalePrice) * item.Counts);
                            var allCount = goodList.Count();
                            //是否满足品牌打折的要求
                            var isBrankOk = false;
                            //如果满足打折要求,满减值
                            var fullMoney = 0;
                            //循环判断符合满减对象
                            foreach (var fullItem in fullJson.OrderByDescending(m => m.fullbegin).ToList())
                            {
                                if (allMoney >= fullItem.fullbegin)
                                {
                                    isBrankOk = true;
                                    fullMoney = fullItem.fullend;
                                    break;
                                }
                            }
                            if (isBrankOk)
                            {
                                item.Money = Convert.ToDecimal(skuItem.SalePrice) * item.Counts - Convert.ToDecimal(fullMoney / allCount);
                            }
                        }
                        //按全部店铺——买一赠一
                        else if (activityModel.Method == 3 && activityModel.Types != 2)
                        {
                            item.Money = roIndex == 1 ? Convert.ToDecimal(skuItem.SalePrice) * item.Counts : 0;
                        }
                    }
                }
                //有活动,并且是正常销售的情况下   非按品牌
                if (activityModel != null && parm.SaleType == 1 && activityModel.Types != 2)
                {
                    //====打折/满减
                    if (activityModel.Method == 1)
                    {
                        //全部商铺,也就是所有金额
                        var zhVal = Convert.ToDecimal(activityModel.CountNum) / 100;
                        parm.RealMoney = Convert.ToDecimal(parm.RealMoney * zhVal);
                    }
                    else if (activityModel.Method == 2)
                    {
                        //循环判断符合满减对象
                        foreach (var item in fullJson.OrderByDescending(m => m.fullbegin).ToList())
                        {
                            if (parm.RealMoney >= item.fullbegin)
                            {
                                parm.RealMoney = parm.RealMoney - item.fullend;
                                break;
                            }
                        }
                    }
                    else if (activityModel.Method == 3)
                    {
                        //买一增一  循环购买商品,只读取第一件,第二个是赠品
                        for (int i = 0; i < roGoodsList.Count; i++)
                        {
                            if (i == 0)
                            {
                                parm.RealMoney = Convert.ToDecimal(goodList.Find(m => m.Guid == roGoodsList[i].GoodsGuid).SalePrice);
                                //给商品明细,第一条商品价格,第二价格为0元,不需要处理
                                roGoodsList[i].Money = parm.RealMoney;
                                break;
                            }
                        }
                    }
                }



                //查询今天销售数量
                var dayCount = ErpSaleOrderDb.Count(m => SqlFunc.DateIsSame(m.AddDate, dayTime));
                parm.Number = "SO-" + DateTime.Now.ToString("yyyyMMdd") + "-" + (1001 + dayCount);
                res.data    = parm.Number;
                for (int i = 0; i < roGoodsList.Count; i++)
                {
                    var item = roGoodsList[i];
                    item.OrderNumber = parm.Number;
                    item.Guid        = Guid.NewGuid().ToString();
                    item.ShopGuid    = parm.ShopGuid;
                }

                var result = Db.Ado.UseTran(() =>
                {
                    //添加订单
                    Db.Insertable(parm).ExecuteCommand();
                    //添加订单商品
                    Db.Insertable(roGoodsList).ExecuteCommand();
                    //根据商品修改商品的销售数量
                    Db.Updateable(goodList).ExecuteCommand();
                    //修改加盟商商品条形码的销售数量
                    Db.Updateable(shopGoodsSkuList).ExecuteCommand();
                    if (userModel != null)
                    {
                        //根据实付金额,计算积分值
                        userModel.Points = Convert.ToInt32(parm.RealMoney / 10);
                        //构建积分变动记录
                        var pointLogModel = new ErpUserPointLog()
                        {
                            Guid        = Guid.NewGuid().ToString(),
                            UserGuid    = userModel.Guid,
                            OperateGuid = parm.Guid,
                            Types       = 0,
                            Point       = userModel.Points,
                            Summary     = "[增加]-销售商品"
                        };
                        Db.Insertable(pointLogModel).ExecuteCommand();
                        //修改用户积分
                        Db.Updateable <ErpShopUser>().UpdateColumns(m => m.Points == m.Points + userModel.Points).Where(m => m.Guid == userModel.Guid).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));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <returns></returns>
        public async Task <ApiResult <string> > AddAsync(ErpReturnOrder parm, string goodsJson)
        {
            var res = new ApiResult <string>()
            {
                data = "1", statusCode = (int)ApiEnum.Error
            };

            try
            {
                parm.Guid = Guid.NewGuid().ToString();
                //判断返货订单字符串是否为空
                if (string.IsNullOrEmpty(goodsJson))
                {
                    res.message = "返货订单里面的商品不能为空~";
                    return(await Task.Run(() => res));
                }
                var isStockSuccess = true;
                //解析字符串转换成List对象
                var roGoodsList = JsonConvert.DeserializeObject <List <ErpReturnGoods> >(goodsJson);
                var guidArray   = roGoodsList.Select(m => m.GoodsGuid).ToList();
                //根据返货的商品,查询平台的条形码
                var goodsSkuList = ErpGoodsSkuDb.GetList(m => guidArray.Contains(m.Guid));
                //根据返货的商品,查询加盟商的条形码
                var shopsSkuList = ErpShopSkuDb.GetList(m => guidArray.Contains(m.SkuGuid) && m.ShopGuid == parm.ShopGuid);
                //验证返货商品的数量是否大于库存数量
                foreach (var item in roGoodsList.GroupBy(m => m.GoodsGuid).Select(m => new ErpReturnGoods {
                    GoodsGuid = m.Key, ReturnCount = m.Sum(g => g.ReturnCount)
                }).ToList())
                {
                    var shopStockSum = shopsSkuList.Find(m => m.SkuGuid == item.GoodsGuid);
                    if (shopStockSum.Stock < item.ReturnCount)
                    {
                        isStockSuccess = false;
                    }
                    //加盟商条形码表,减少返货的库存
                    shopsSkuList.Find(m => m.SkuGuid == item.GoodsGuid).Stock = shopStockSum.Stock - item.ReturnCount;
                    //平台条形码表,增加返货的库存
                    var goodsStock = goodsSkuList.Find(m => m.Guid == item.GoodsGuid);
                    goodsSkuList.Find(m => m.Guid == item.GoodsGuid).StockSum = goodsStock.StockSum + item.ReturnCount;
                }
                if (!isStockSuccess)
                {
                    res.message = "返货的商品数量大于库存数量";
                    return(await Task.Run(() => res));
                }
                foreach (var item in roGoodsList)
                {
                    item.OrderGuid = parm.Guid;
                    item.Guid      = Guid.NewGuid().ToString();
                    item.ShopGuid  = parm.ShopGuid;
                }
                parm.GoodsSum = roGoodsList.Sum(m => m.ReturnCount);
                //查询今天返货数量
                DateTime dayTime  = Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString() + " 00:00:00");
                var      dayCount = ErpReturnOrderDb.Count(m => SqlFunc.DateIsSame(m.AddDate, dayTime));
                parm.Number = "RO-" + DateTime.Now.ToString("yyyyMMdd") + "-" + (1001 + dayCount);
                var result = Db.Ado.UseTran(() =>
                {
                    //添加订单
                    Db.Insertable(parm).ExecuteCommand();
                    //添加订单商品
                    Db.Insertable(roGoodsList).ExecuteCommand();
                    //修改平台库存
                    Db.Updateable(goodsSkuList).ExecuteCommand();
                    //修改加盟商库存
                    Db.Updateable(shopsSkuList).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));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task <ApiResult <string> > AddAsync(ErpSaleOrder parm, string goodsJson)
        {
            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();
                //判断销售订单字符串是否为空
                if (string.IsNullOrEmpty(goodsJson))
                {
                    res.message = "销售订单里面的商品不能为空~";
                    return(await Task.Run(() => res));
                }
                //判断用户信息存在,如果存在需要根据用户查询编号
                ErpShopUser userModel = null;
                if (!string.IsNullOrEmpty(parm.UserGuid))
                {
                    //判断是否存在
                    userModel = ErpShopUserDb.GetSingle(m => m.Mobile == parm.UserGuid);
                    if (userModel == null)
                    {
                        res.message = "会员不存在~";
                        return(await Task.Run(() => res));
                    }
                    parm.UserGuid = userModel.Guid;
                }

                var isStockSuccess = true;
                //解析字符串转换成List对象
                var roGoodsList = JsonConvert.DeserializeObject <List <ErpSaleOrderGoods> >(goodsJson);
                //验证销售商品的数量是否大于库存数量
                foreach (var item in roGoodsList.GroupBy(m => m.GoodsGuid).Select(m => new ErpSaleOrderGoods {
                    GoodsGuid = m.Key, Counts = m.Sum(g => g.Counts)
                }).ToList())
                {
                    var shopStockSum = Db.Queryable <ErpShopSku>()
                                       .Where(m => m.ShopGuid == parm.ShopGuid && m.SkuGuid == item.GoodsGuid).First();
                    if (shopStockSum.Stock < item.Counts)
                    {
                        isStockSuccess = false;
                    }
                }
                if (!isStockSuccess)
                {
                    res.message = "商品库存数量";
                    return(await Task.Run(() => res));
                }

                //根据活动编号,查询活动详情,处理对应金额
                ErpShopActivity activityModel = null;
                if (!string.IsNullOrEmpty(parm.ActivityName))
                {
                    //查询活动
                    activityModel = ErpShopActivityDb.GetById(parm.ActivityName);
                    if (activityModel != null)
                    {
                        parm.ActivityGuid  = parm.ActivityName;
                        parm.ActivityName  = activityModel.Method == 1 ? "打折" : "满减";
                        parm.ActivityTypes = activityModel.Method == 1 ? Convert.ToByte(2) : Convert.ToByte(3);
                    }
                }
                //根据活动算好订单金额,如果有活动,满减或者打折,最终金额会根据活动而变

                //获得商品的所有id
                var goodIds = roGoodsList.Select(m => m.GoodsGuid).ToList();
                //根据商品获得列表
                var goodList = ErpGoodsSkuDb.GetList(m => goodIds.Contains(m.Guid));
                //根据店铺获得加盟商的库存信息
                var shopGoodsSkuList = ErpShopSkuDb.GetList(m => m.ShopGuid == parm.ShopGuid && goodIds.Contains(m.SkuGuid));
                foreach (var item in goodList)
                {
                    foreach (var roitem in roGoodsList)
                    {
                        if (roitem.GoodsGuid == item.Guid)
                        {
                            //修改商品的销售数量
                            item.SaleSum += roitem.Counts;
                            //修改加盟商条形码表中的库存减少,  销售数量增加
                            var shopSkuModel = shopGoodsSkuList.Find(m => m.SkuGuid == roitem.GoodsGuid);
                            shopGoodsSkuList.Find(m => m.SkuGuid == roitem.GoodsGuid).Stock = shopSkuModel.Stock - roitem.Counts;
                            //修改加盟商条形码表中的销售数量   增加
                            shopGoodsSkuList.Find(m => m.SkuGuid == roitem.GoodsGuid).Sale = shopSkuModel.Sale + roitem.Counts;
                            //获得商品原价*购买商品的数量
                            parm.Money += Convert.ToDecimal(item.SalePrice) * roitem.Counts;
                            //整除销售计算价格,残次品价格是前端传过来的
                            if (parm.SaleType == 1)
                            {
                                //这里面只处理打折的,并且是按品牌的
                                if (activityModel != null && activityModel.Method == 1 && activityModel.Types == 2)
                                {
                                    if (item.BrankGuid == activityModel.BrandGuid)
                                    {
                                        //品牌打折
                                        var tempMoney = Convert.ToDecimal(item.SalePrice) * roitem.Counts;
                                        tempMoney       = tempMoney * (Convert.ToDecimal(activityModel.CountNum) / 100);
                                        parm.RealMoney += tempMoney;
                                    }
                                    else
                                    {
                                        //不是该品牌部打折
                                        parm.RealMoney += Convert.ToDecimal(item.SalePrice) * roitem.Counts;
                                    }
                                }
                                //else if (activityModel != null && activityModel.Method == 2 && activityModel.Types == 2) {
                                //    //品牌满减
                                //    if (item.BrankGuid == activityModel.BrandGuid)
                                //    {

                                //    }
                                //}
                                else
                                {
                                    parm.RealMoney += Convert.ToDecimal(item.SalePrice) * roitem.Counts;
                                }
                            }
                        }
                    }
                }
                if (activityModel != null && parm.SaleType == 1)
                {
                    //====打折/满减
                    if (activityModel.Method == 1)
                    {
                        //打折   实收金额=实收金额*(折扣值/100)
                        if (activityModel.Types == 0)
                        {
                            //全部商铺,也就是所有金额
                            var zhVal = Convert.ToDecimal(activityModel.CountNum) / 100;
                            parm.RealMoney = Convert.ToDecimal(parm.RealMoney * zhVal);
                        }
                    }
                    else
                    {
                        //满减======序列号满减对象
                        var fullJson = JsonConvert.DeserializeObject <List <ShopActivity> >(activityModel.FullBack);
                        //循环判断符合满减对象
                        foreach (var item in fullJson.OrderByDescending(m => m.fullbegin).ToList())
                        {
                            if (parm.RealMoney > item.fullbegin)
                            {
                                parm.RealMoney = parm.RealMoney - item.fullend;
                                break;
                            }
                        }
                    }
                }


                //根据实付金额,计算积分值
                if (userModel != null)
                {
                    userModel.Points = Convert.ToInt32(parm.RealMoney / 10);
                }

                //查询今天销售数量
                var dayCount = ErpSaleOrderDb.Count(m => SqlFunc.DateIsSame(m.AddDate, dayTime));
                parm.Number = "SO-" + DateTime.Now.ToString("yyyyMMdd") + "-" + (1001 + dayCount);
                res.data    = parm.Number;
                foreach (var item in roGoodsList)
                {
                    item.OrderNumber = parm.Number;
                    item.Guid        = Guid.NewGuid().ToString();
                    item.ShopGuid    = parm.ShopGuid;
                }

                var result = Db.Ado.UseTran(() =>
                {
                    //添加订单
                    Db.Insertable(parm).ExecuteCommand();
                    //添加订单商品
                    Db.Insertable(roGoodsList).ExecuteCommand();
                    //根据商品修改商品的销售数量
                    Db.Updateable(goodList).ExecuteCommand();
                    //修改加盟商商品条形码的销售数量
                    Db.Updateable(shopGoodsSkuList).ExecuteCommand();
                    if (userModel != null)
                    {
                        //修改用户积分
                        Db.Updateable <ErpShopUser>().UpdateColumns(m => m.Points == m.Points + userModel.Points).Where(m => m.Guid == userModel.Guid).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));
        }