public override async Task HandleRevert(FlowSheet flowSheet)
        {
            //数据处理
            var formObj     = Newtonsoft.Json.JsonConvert.DeserializeObject <JObject>(flowSheet.FlowInstance.FormData);
            var sheetData   = formObj["sheetData"];
            var sheetHeader = sheetData["header"];

            var storeId  = sheetHeader["storeId"].ToObject <int>(); //出货仓库id;
            var unitId   = sheetHeader["unitId"].ToObject <int>();  //代理商id
            var totalFee = sheetHeader["totalFee"].ToObjectWithDefault <decimal>();
            //更改往来单位金额
            await UnitManager.ChangeFee(unitId, null, totalFee, flowSheet);

            var materialSellIds = new List <int>();
            var toOutMaterials  = new Dictionary <int, int>();

            foreach (var sheetItem in sheetData["body"])
            {
                var materialId     = sheetItem["materialId"].ToObjectWithDefault <int>(); //商品Id
                var sellMaterialId = Convert.ToInt32(sheetItem["id"]);                    //对应的销售物料Id
                var number         = sheetItem["number"].ToObjectWithDefault <int>();     //出货数量
                if (number == 0)
                {
                    continue;
                }
                //取消销售出库记录
                var materialSellOut = await MaterialSellOutManager.GetAll()
                                      .Where(o => o.UnitId == unitId && o.FlowSheetId == sheetItem["flowSheetId"].ToObjectWithDefault <int>() && o.OutNumber == number && o.MaterialId == materialId)
                                      .FirstOrDefaultAsync();

                if (materialSellOut != null)
                {
                    await MaterialSellOutManager.DeleteAsync(materialSellOut);
                }

                materialSellIds.Add(sellMaterialId);
                //加入待出库集合
                if (!toOutMaterials.ContainsKey(materialId))
                {
                    toOutMaterials.Add(materialId, number);
                }
                else
                {
                    toOutMaterials[materialId] += number;
                }

                await MaterialSellManager.Out(unitId, sellMaterialId, storeId, -number, flowSheet);
            }
            //出库
            foreach (var item in toOutMaterials)
            {
                await StoreMaterialManager.CountMaterial(storeId, item.Key, item.Value, flowSheet);
            }
            await MaterialSellManager.CheckSellSheetStatus(materialSellIds);
        }
        public override async Task Handle(FlowSheet flowSheet)
        {
            await base.Handle(flowSheet);

            //数据处理
            var formObj     = Newtonsoft.Json.JsonConvert.DeserializeObject <JObject>(flowSheet.FlowInstance.FormData);
            var sheetData   = formObj["sheetData"];
            var sheetHeader = sheetData["header"];

            flowSheet.SheetDate = sheetHeader["sheetDate"].ToObjectWithDefault <DateTime>();
            flowSheet.Remarks   = sheetHeader["remarks"].ToObjectWithDefault <string>();
            var storeId  = sheetHeader["storeId"].ToObject <int>(); //出货仓库id;
            var unitId   = sheetHeader["unitId"].ToObject <int>();  //代理商id
            var totalFee = sheetHeader["totalFee"].ToObjectWithDefault <decimal>();

            flowSheet.UnitId = unitId;
            flowSheet.SetPropertyValue("StoreName", sheetHeader["storeName"].ToObjectWithDefault <string>());
            flowSheet.SetPropertyValue("Fee", totalFee);

            //更改往来单位金额
            await UnitManager.ChangeFee(unitId, null, -totalFee, flowSheet);

            var materialSellIds = new List <int>();
            var toOutMaterials  = new Dictionary <int, int>();

            foreach (var sheetItem in sheetData["body"])
            {
                var materialId     = sheetItem["materialId"].ToObjectWithDefault <int>(); //商品Id
                var sellMaterialId = Convert.ToInt32(sheetItem["id"]);                    //对应的销售物料Id
                var number         = sheetItem["number"].ToObjectWithDefault <int>();     //出货数量
                if (number == 0)
                {
                    continue;
                }
                //建立销售出库记录
                var materialSellOut = new MaterialSellOut()
                {
                    UnitId      = unitId,
                    FlowSheetId = sheetItem["flowSheetId"].ToObjectWithDefault <int>(),
                    MaterialId  = materialId,
                    OutNumber   = number,
                    Price       = sheetItem["price"].ToObjectWithDefault <decimal>(),
                    Discount    = sheetItem["discount"].ToObjectWithDefault <decimal>()
                };
                await MaterialSellOutManager.InsertAsync(materialSellOut);

                materialSellIds.Add(sellMaterialId);
                //加入待出库集合
                if (!toOutMaterials.ContainsKey(materialId))
                {
                    toOutMaterials.Add(materialId, number);
                }
                else
                {
                    toOutMaterials[materialId] += number;
                }

                await MaterialSellManager.Out(unitId, sellMaterialId, storeId, number, flowSheet);
            }
            //出库
            foreach (var item in toOutMaterials)
            {
                //检测库存是否足够
                if (!StoreMaterialManager.IsSatisfied(item.Key, storeId, unitId, item.Value, out var message))
                {
                    throw new UserFriendlyException(message);
                }
                await StoreMaterialManager.CountMaterial(storeId, item.Key, -item.Value, flowSheet);
            }
            await MaterialSellManager.CheckSellSheetStatus(materialSellIds);
        }
        public override async Task Action(FlowSheet flowSheet, string action, DateTime?lastModifyTime)
        {
            //数据处理
            var formObj     = Newtonsoft.Json.JsonConvert.DeserializeObject <JObject>(flowSheet.FlowInstance.FormData);
            var sheetData   = formObj["sheetData"];
            var sheetHeader = sheetData["header"];

            flowSheet.Fee = sheetHeader["totalFee"].ToObjectWithDefault <decimal>();
            var unitId = sheetHeader["unitId"].ToObject <int>();//代理商id

            if (action == "backToCart")
            {
                foreach (var sheetItem in sheetData["body"])
                {
                    var materialId = Convert.ToInt32(sheetItem["id"]);                //对应的物料Id
                    var number     = sheetItem["number"].ToObjectWithDefault <int>(); //订购数量

                    var materialCart = await CartRepository.GetAll().Where(o => o.UnitId == unitId && o.CreatorUserId == AbpSession.UserId && o.MaterialId == materialId).FirstOrDefaultAsync();

                    if (materialCart != null)
                    {
                        materialCart.Number += number;
                        await CartRepository.UpdateAsync(materialCart);
                    }
                    else
                    {
                        materialCart = new MaterialSellCart()
                        {
                            MaterialId = materialId,
                            Number     = number,
                            UnitId     = unitId
                        };
                        await CartRepository.InsertAsync(materialCart);
                    }
                }
                //删除订单
                await FlowSheetManager.DeleteAsync(flowSheet);
            }
            else if (action == "modify")
            {
                if (flowSheet.OrderStatus != "待审核")
                {
                    throw new UserFriendlyException("本单据已被审核,当前操作无效,请重新查看");
                }
            }
            else if (action == "verify")
            {
                if (flowSheet.FlowInstance.LastModificationTime != null && (!lastModifyTime.HasValue || (flowSheet.FlowInstance.LastModificationTime.Value - lastModifyTime.Value).TotalSeconds > 1))
                {
                    throw new UserFriendlyException($"本单据已在{flowSheet.FlowInstance.LastModificationTime?.ToString("yyyy-MM-dd HH:mm:ss")}被{flowSheet.FlowInstance.LastModifierUser?.Name}修改,当前操作无效,请重新查看后再提交");
                }
                flowSheet.OrderStatus = "待发货";
            }
            else if (action == "cancel")
            {
                flowSheet.OrderStatus = "已取消";
            }
            else if (action == "send")
            {
                flowSheet.OrderStatus = "已发货";
                //‘收货时间’‘收货人姓名’‘联系方式’‘收货地址’
                flowSheet.SetPropertyValue("Receiver", sheetHeader["receiver"].ToObjectWithDefault <string>());
                flowSheet.SetPropertyValue("DeliverDate", sheetHeader["deliverDate"].ToObjectWithDefault <DateTime>());
                flowSheet.SetPropertyValue("ReceiverMobile", sheetHeader["receiverMobile"].ToObjectWithDefault <string>());
                flowSheet.SetPropertyValue("ReceiverAddress", sheetHeader["receiverAddress"].ToObjectWithDefault <string>());
                var totalFee = sheetHeader["totalFee"].ToObjectWithDefault <decimal>();
                //更改往来单位金额
                await UnitManager.ChangeFee(unitId, null, -totalFee, flowSheet);

                var materialSellIds = new List <int>();
                var toOutMaterials  = new Dictionary <int, int>();
                var outStoreId      = sheetHeader["storeId"].ToObjectWithDefault <int?>();
                if (outStoreId == null)
                {
                    throw new UserFriendlyException("请选择发货仓库");
                }

                var logs = new List <string>();

                foreach (var sheetItem in sheetData["body"])
                {
                    var materialId   = sheetItem["materialId"].ToObjectWithDefault <int>(); //商品Id
                    var number       = sheetItem["number"].ToObjectWithDefault <int>();     //出货数量
                    var materialSell = await MaterialSellManager.GetAll().Include(o => o.Material)
                                       .Where(o => o.FlowSheetId == flowSheet.Id && o.MaterialId == materialId).FirstOrDefaultAsync();

                    if (materialSell != null)
                    {
                        if (materialSell.SellNumber != number)
                        {
                            //发生订购数量更改需要进行记录
                            logs.Add($"商品\"{materialSell.Material.Name}\"订购数量从{materialSell.SellNumber}调整为{number}");
                        }
                        materialSell.SellNumber = number;
                        materialSell.OutNumber  = materialSell.SellNumber;//设置销售记录的出货数量等于订货数量
                    }
                    //建立销售出库记录
                    var materialSellOut = new MaterialSellOut()
                    {
                        UnitId      = unitId,
                        FlowSheetId = flowSheet.Id,
                        MaterialId  = materialId,
                        OutNumber   = number,
                        Price       = sheetItem["price"].ToObjectWithDefault <decimal>(),
                        Discount    = sheetItem["discount"].ToObjectWithDefault <decimal>()
                    };
                    await MaterialSellOutManager.InsertAsync(materialSellOut);

                    //检测库存是否足够
                    if (!StoreMaterialManager.IsSatisfied(materialId, outStoreId.Value, unitId, number, out var message))
                    {
                        throw new UserFriendlyException(message);
                    }
                    //库存变化
                    await StoreMaterialManager.CountMaterial(outStoreId.Value, materialId, -number, flowSheet);
                }
                if (logs.Count > 0)
                {
                    flowSheet.SetPropertyValue("Log", $"{AbpSession.Name()}于{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}将{string.Join(';', logs)}");
                }
            }
            else if (action == "back")
            {
                var totalFee = sheetHeader["totalFee"].ToObjectWithDefault <decimal>();
                flowSheet.OrderStatus = "已退货";
                //更改往来单位金额
                await UnitManager.ChangeFee(unitId, null, totalFee, flowSheet);

                var materialSellIds = new List <int>();
                var toOutMaterials  = new Dictionary <int, int>();
                var backStoreId     = sheetHeader["backStoreId"].ToObjectWithDefault <int?>();
                if (backStoreId == null)
                {
                    throw new UserFriendlyException("请选择退入仓库");
                }

                foreach (var sheetItem in sheetData["body"])
                {
                    var materialId = sheetItem["materialId"].ToObjectWithDefault <int>(); //商品Id
                    var number     = sheetItem["number"].ToObjectWithDefault <int>();     //出货数量
                    //库存变化
                    await StoreMaterialManager.CountMaterial(backStoreId.Value, materialId, number, flowSheet);

                    //产生退货数据
                    var materialSellBack = new MaterialSellBack()
                    {
                        UnitId      = unitId,
                        MaterialId  = materialId,
                        BackNumber  = number,
                        FlowSheetId = flowSheet.Id,
                        Discount    = sheetItem["discount"].ToObjectWithDefault <decimal>(),
                        Price       = sheetItem["price"].ToObjectWithDefault <decimal>(),
                    };
                    await MaterialSellBackManager.InsertAsync(materialSellBack);
                }
            }
        }