public static Guid DepotActUse(this DepotEntities db, Guid depotId, DateTime useTime, Guid operatorId, Guid userId, List<InMemoryUse> list)
 {
     if (list == null || list.Count == 0)
     {
         return Guid.Empty;
     }
     var use = new DepotUse
     {
         Id = db.GlobalId(),
         DepotId = depotId,
         UserId = userId,
         Time = useTime,
         OperatorId = operatorId,
         OperationTime = DateTime.Now,
         Age = string.Empty,
         Place = string.Empty,
         Money = 0
     };
     db.DepotUse.Add(use);
     foreach (var item in list)
     {
         if (!item.ObjectId.HasValue)
             continue;
         var objId = item.ObjectId.Value;
         var obj = db.DepotObject.Single(o => o.Id == objId);
         if (obj.Single)
         {
             if (item.Ordinals.Count == 0 || !item.Type.HasValue)
             {
                 continue;
             }
             foreach (var index in item.Ordinals)
             {
                 var @in = db.DepotInX.Single(o => o.ObjectId == objId && o.Ordinal == index);
                 var x = new DepotUseX
                 {
                     Id = db.GlobalId(),
                     ObjectId = objId,
                     UseId = use.Id,
                     InXId = @in.Id,
                     Type = item.Type.Value,
                     Age = item.Age,
                     Place = item.Place,
                     Amount = 1,
                     Money = @in.Price,
                     ReturnedAmount = 0,
                     Note = item.Note
                 };
                 db.DepotUseX.Add(x);
                 @in.AvailableAmount = 0;
                 @in.DepotIn.AvailableAmount -= 1;
                 obj.Amount -= 1;
                 var flowx = new DepotFlowX
                 {
                     Id = db.GlobalId(),
                     ObjectId = @in.ObjectId,
                     ObjectOrdinal = index,
                     UserId = operatorId,
                     Type = FlowType.借用出库,
                     TypeName = FlowType.借用出库.ToString(),
                     Time = useTime,
                     Amount = -1,
                     Money = @in.Price,
                     Note = item.Note
                 };
                 use.Money += @in.Price;
                 db.DepotFlowX.Add(flowx);
                 db.DepotActStatistics(@in.ObjectId, useTime, 0, 0, 1, @in.Price, 0, 0, 0, 0, 0, 0);
             }
         }
         else
         {
             if (!item.Amount.HasValue || item.Amount.Value == 0)
             {
                 continue;
             }
             if (!item.Type.HasValue)
             {
                 continue;
             }
             var todo = obj.Amount < item.Amount.Value ? obj.Amount : item.Amount.Value;
             var totalAmount = 0M;
             var totalMoney = 0M;
             foreach (var @in in obj.DepotIn.Where(o => o.ObjectId == objId && o.AvailableAmount > 0).OrderBy(o => o.Time).ToList())
             {
                 var xObj = db.DepotInX.Single(o => o.ObjectId == objId && o.InId == @in.Id);
                 if (@in.AvailableAmount < todo)
                 {
                     var x = new DepotUseX
                     {
                         Id = db.GlobalId(),
                         ObjectId = objId,
                         UseId = use.Id,
                         InXId = xObj.Id,
                         Type = item.Type.Value,
                         Age = item.Age,
                         Place = item.Place,
                         Amount = @in.AvailableAmount,
                         Money = @in.Price * @in.AvailableAmount,
                         ReturnedAmount = 0,
                         Note = item.Note
                     };
                     db.DepotUseX.Add(x);
                     todo -= @in.AvailableAmount;
                     totalAmount += @in.AvailableAmount;
                     totalMoney += @in.AvailableAmount * @in.Price;
                     obj.Amount -= @in.AvailableAmount;
                     obj.Money -= @in.AvailableAmount * @in.Price;
                     @in.Total -= @in.AvailableAmount * @in.Price;
                     @in.AvailableAmount = 0;
                     var inx = @in.DepotInX.Single();
                     inx.AvailableAmount = @in.AvailableAmount;
                     inx.Total = @in.Total;
                 }
                 else
                 {
                     var x = new DepotUseX
                     {
                         Id = db.GlobalId(),
                         ObjectId = objId,
                         UseId = use.Id,
                         InXId = xObj.Id,
                         Type = item.Type.Value,
                         Age = item.Age,
                         Place = item.Place,
                         Amount = todo,
                         Money = @in.Price * todo,
                         ReturnedAmount = 0,
                         Note = item.Note
                     };
                     db.DepotUseX.Add(x);
                     totalAmount += todo;
                     totalMoney += todo * @in.Price;
                     obj.Amount -= todo;
                     obj.Money -= todo * @in.Price;
                     @in.Total -= todo * @in.Price;
                     @in.AvailableAmount -= todo;
                     var inx = @in.DepotInX.Single();
                     inx.AvailableAmount = @in.AvailableAmount;
                     inx.Total = @in.Total;
                     todo = 0;
                     break;
                 }
             }
             use.Money += totalMoney;
             var flow = new DepotFlow
             {
                 Id = db.GlobalId(),
                 ObjectId = objId,
                 UserId = userId,
                 Type = item.Type.Value == UseType.借用 ? FlowType.借用出库 : FlowType.领用出库,
                 TypeName = (item.Type.Value == UseType.借用 ? FlowType.借用出库 : FlowType.领用出库).ToString(),
                 Time = useTime,
                 Amount = -totalAmount,
                 Money = -totalMoney,
                 Note = "出库"
             };
             db.DepotFlow.Add(flow);
             if (item.Type.Value == UseType.借用)
                 db.DepotActStatistics(objId, useTime, 0, 0, totalAmount, totalMoney, 0, 0, 0, 0, 0, 0);
             else
                 db.DepotActStatistics(objId, useTime, 0, 0, 0, 0, totalAmount, totalMoney, 0, 0, 0, 0);
         }
     }
     db.SaveChanges();
     if (use.DepotUseX.Count == 0)
     {
         db.DepotUse.Remove(use);
         db.SaveChanges();
         return Guid.Empty;
     }
     return use.Id;
 }
 public static void DepotActReturn(this DepotEntities db, Guid depotId, DateTime returnTime, Guid operatorId, List<InMemoryReturn> list)
 {
     if (list == null || list.Count == 0)
     {
         return;
     }
     var @out = new List<InMemoryOut>();
     foreach (var item in list)
     {
         if (!item.Amount.HasValue || item.Amount.Value == 0)
             continue;
         var usexId = item.UseX;
         var usex = db.DepotUseX.SingleOrDefault(o => o.Id == usexId && o.ReturnedAmount < o.Amount);
         if (usex == null)
             continue;
         var inx = usex.DepotInX;
         var obj = inx.DepotObject;
         var @in = inx.DepotIn;
         var @return = new DepotReturn
         {
             Id = db.GlobalId(),
             UserId = usex.DepotUse.Id,
             UseXId = item.UseX,
             Amount = item.Amount.Value,
             Price = inx.Price,
             Total = item.Amount.Value * inx.Price,
             Time = returnTime,
             Note = item.Note
         };
         var catalog = db.DepotObjectCatalog.Where(o => o.ObjectId == obj.Id && o.IsLeaf == true).ToList().Join(db.DepotCatalog.Where(o => o.DepotId == depotId), o => o.CatalogId, o => o.Id, (a, b) => a.CatalogId).FirstOrDefault();
         usex.ReturnedAmount += @return.Amount;
         db.DepotReturn.Add(@return);
         obj.Amount += @return.Amount;
         obj.Money += @return.Total;
         @in.AvailableAmount += @return.Amount;
         @in.Total += @in.Price * @return.Amount;
         inx.AvailableAmount += @return.Amount;
         @in.Total += @in.Price * @return.Amount;
         if (obj.Single)
         {
             var flowx = new DepotFlowX
             {
                 Id = db.GlobalId(),
                 ObjectId = @in.ObjectId,
                 ObjectOrdinal = inx.Ordinal,
                 UserId = operatorId,
                 Type = FlowType.归还,
                 TypeName = FlowType.归还.ToString(),
                 Time = returnTime,
                 Amount = @return.Amount,
                 Money = @in.Price,
                 Note = item.Note
             };
             db.DepotFlowX.Add(flowx);
             if (item.OutAmount.HasValue && item.OutAmount.Value > 0)
             {
                 var nl = new List<int>();
                 nl.Add(inx.Ordinal);
                 @out.Add(new InMemoryOut { Amount = item.OutAmount.Value, ObjectId = @in.ObjectId, Ordinals = nl, Reason = "报废", CatalogId = catalog });
             }
         }
         else
         {
             var flow = new DepotFlow
             {
                 Id = db.GlobalId(),
                 ObjectId = @in.ObjectId,
                 UserId = usex.DepotUse.Id,
                 Type = FlowType.归还,
                 TypeName = FlowType.归还.ToString(),
                 Time = returnTime,
                 Amount = @return.Amount,
                 Money = @in.Price,
                 Note = item.Note
             };
             db.DepotFlow.Add(flow);
             if (item.OutAmount > 0)
             {
                 var nl = new List<int>();
                 @out.Add(new InMemoryOut { Amount = item.OutAmount, ObjectId = @in.ObjectId, Ordinals = nl, Reason = "报废", CatalogId = catalog });
             }
         }
         db.DepotActStatistics(@in.ObjectId, returnTime, 0, 0, [email protected], [email protected], 0, 0, 0, 0, 0, 0);
     }
     db.SaveChanges();
     try
     {
         db.DepotActOut(depotId, returnTime, operatorId, operatorId, @out);
     }
     catch
     {
     }
 }
 public static void DepotActInRedo(this DepotEntities db, Guid depotId, DepotIn @in, decimal backed, Guid operatorId)
 {
     var obj = db.DepotObject.Single(o => o.Id == @in.ObjectId);
     var order = db.DepotOrder.Single(o => o.Id == @in.OrderId);
     decimal amount = @in.Amount - backed;
     decimal money = (@in.Amount - backed) * @in.Price;
     decimal plusAmount = -backed;
     decimal plusMoney = -backed * @in.Price;
     db.DepotActStatistics(obj.Id, @in.Time, plusAmount, plusMoney, 0, 0, 0, 0, 0, 0, 0, 0);
     if (obj.Single)
     {
         return;
     }
     else
     {
         var x = @in.DepotInX.First();
         if (x.DepotUseX.Count > 0)
             return;
         if (amount > 0 && money > 0)
         {
             @in.Amount = amount;
             @in.AvailableAmount = amount;
             @in.Total = money;
             @in.Price = decimal.Divide(money, amount);
             obj.Amount += plusAmount;
             obj.Money += plusMoney;
             order.Paid += plusMoney;
             var flow = new DepotFlow
             {
                 Id = db.GlobalId(),
                 ObjectId = @in.ObjectId,
                 UserId = operatorId,
                 Type = FlowType.退货,
                 TypeName = FlowType.退货.ToString(),
                 Time = @in.Time,
                 Amount = plusAmount,
                 Money = plusMoney,
                 Note = (new InMemoryRedo { Amount = backed, Name = @in.DepotObject.Name, OrderId = @in.OrderId }).ToJson()
             };
             db.DepotFlow.Add(flow);
             x.Amount = @in.Amount;
             x.AvailableAmount = @in.Amount;
             x.Total = @in.Total;
         }
     }
     db.SaveChanges();
 }
 public static void DepotActInEdit(this DepotEntities db, Guid depotId, DepotIn @in, DateTime day, decimal amount, decimal priceSet, decimal money, string age, string place, string note, Guid operatorId)
 {
     var obj = db.DepotObject.Single(o => o.Id == @in.ObjectId);
     var order = db.DepotOrder.Single(o => o.Id == @in.OrderId);
     decimal plusAmount = amount - @in.Amount;
     decimal plusMoney = money - @in.Total;
     if (day.Year == @in.Time.Year && day.Month == @in.Time.Month)
     {
         db.DepotActStatistics(obj.Id, day, plusAmount, plusMoney, 0, 0, 0, 0, 0, 0, 0, 0);
     }
     else
     {
         db.DepotActStatistics(obj.Id, @in.Time, [email protected], [email protected], 0, 0, 0, 0, 0, 0, 0, 0);
         db.DepotActStatistics(obj.Id, day, amount, money, 0, 0, 0, 0, 0, 0, 0, 0);
     }
     if (obj.Single)
     {
         return;
     }
     else
     {
         @in.Age = age;
         @in.Place = place;
         @in.Note = note;
         @in.Time = day;
         var x = @in.DepotInX.First();
         x.Age = @in.Age;
         x.Place = @in.Place;
         if (amount != @in.Amount || money != @in.Total)
         {
             if (amount > 0 && money > 0)
             {
                 @in.Amount = amount;
                 @in.AvailableAmount = amount;
                 @in.Total = money;
                 @in.PriceSet = priceSet;
                 @in.Price = decimal.Divide(money, amount);
                 obj.Amount += plusAmount;
                 obj.Money += plusMoney;
                 order.Paid += plusMoney;
                 var flow = new DepotFlow
                 {
                     Id = db.GlobalId(),
                     ObjectId = @in.ObjectId,
                     UserId = operatorId,
                     Type = FlowType.入库修改,
                     TypeName = FlowType.入库修改.ToString(),
                     Time = day,
                     Amount = plusAmount,
                     Money = plusMoney,
                     Note = @in.Note
                 };
                 db.DepotFlow.Add(flow);
                 x.Amount = @in.Amount;
                 x.AvailableAmount = @in.Amount;
                 x.Total = @in.Total;
                 x.PriceSet = @in.PriceSet;
                 x.Price = @in.Price;
             }
             else
             {
                 db.DepotInX.Remove(x);
                 db.DepotIn.Remove(@in);
                 obj.Amount += plusAmount;
                 obj.Money += plusMoney;
                 order.Paid += plusMoney;
                 var flow = new DepotFlow
                 {
                     Id = db.GlobalId(),
                     ObjectId = @in.ObjectId,
                     UserId = operatorId,
                     Type = FlowType.入库修改,
                     TypeName = FlowType.入库修改.ToString(),
                     Time = day,
                     Amount = plusAmount,
                     Money = plusMoney,
                     Note = @in.Note
                 };
                 db.DepotFlow.Add(flow);
             }
         }
     }
     db.SaveChanges();
     db.DepotDictionaryAdd(depotId, DictionaryType.年龄段, age);
     db.DepotDictionaryAdd(depotId, DictionaryType.存放地, place);
 }
 public static void DepotActIn(this DepotEntities db, Guid depotId, Guid orderId, DateTime inTime, Guid operatorId, List<InMemoryIn> list)
 {
     foreach (var item in list)
     {
         if (!item.ObjectId.HasValue || !item.Amount.HasValue || item.Amount.Value == 0)
             continue;
         var objId = item.ObjectId.Value;
         var obj = db.DepotObject.Single(o => o.Id == objId);
         var @in = new DepotIn
         {
             Id = db.GlobalId(),
             OrderId = orderId,
             ObjectId = item.ObjectId.Value,
             Age = item.Age,
             Place = item.Place,
             ResponsibleId = null,
             Note = item.Note,
             Time = inTime,
             OperatorId = operatorId,
             OperationTime = DateTime.Now,
             Amount = item.Amount.Value,
             AvailableAmount = item.Amount.Value,
             Price = decimal.Divide(item.Money.Value, item.Amount.Value),
             PriceSet = item.PriceSet.HasValue ? item.PriceSet.Value : 0M,
             Total = item.Money.Value,
         };
         db.DepotIn.Add(@in);
         obj.Amount += @in.Amount;
         obj.Money += @in.Total;
         if (obj.Single)
         {
             var current = db.DepotInX.Count(o => o.ObjectId == obj.Id) == 0 ? 0 : db.DepotInX.Where(o => o.ObjectId == obj.Id).Max(o => o.Ordinal);
             for (var j = 0; j < @in.Amount; j++)
             {
                 current++;
                 var inx = new DepotInX
                 {
                     Id = db.GlobalId(),
                     InId = @in.Id,
                     OrderId = @in.OrderId,
                     ObjectId = @in.ObjectId,
                     Age = @in.Age,
                     Place = @in.Place,
                     Ordinal = current,
                     Amount = 1,
                     PriceSet = @in.PriceSet,
                     Price = @in.Price,
                     Total = @in.Price,
                     AvailableAmount = 1,
                     Code = obj.Code
                 };
                 db.DepotInX.Add(inx);
                 db.SaveChanges();
                 inx.Code = db.ToQR(CodeType.Single, inx.AutoId);
                 var flowx = new DepotFlowX
                 {
                     Id = db.GlobalId(),
                     ObjectId = @in.ObjectId,
                     ObjectOrdinal = inx.Ordinal,
                     UserId = operatorId,
                     Type = FlowType.入库,
                     TypeName = FlowType.入库.ToString(),
                     Time = inTime,
                     Amount = 1,
                     Money = @in.Price,
                     Note = @in.Note
                 };
                 db.DepotFlowX.Add(flowx);
             }
         }
         else
         {
             var inx = new DepotInX
             {
                 Id = db.GlobalId(),
                 InId = @in.Id,
                 OrderId = @in.OrderId,
                 ObjectId = @in.ObjectId,
                 Age = @in.Age,
                 Place = @in.Place,
                 Ordinal = -1,
                 Amount = @in.Amount,
                 PriceSet = @in.PriceSet,
                 Price = @in.Price,
                 Total = @in.Total,
                 AvailableAmount = @in.AvailableAmount,
                 Code = obj.Code
             };
             db.DepotInX.Add(inx);
             var flow = new DepotFlow
             {
                 Id = db.GlobalId(),
                 ObjectId = @in.ObjectId,
                 UserId = operatorId,
                 Type = FlowType.入库,
                 TypeName = FlowType.入库.ToString(),
                 Time = inTime,
                 Amount = @in.Amount,
                 Money = @in.Total,
                 Note = @in.Note
             };
             db.DepotFlow.Add(flow);
         }
         db.DepotActStatistics(@in.ObjectId, inTime, @in.Amount, @in.Total, 0, 0, 0, 0, 0, 0, 0, 0);
         db.SaveChanges();
         db.DepotDictionaryAdd(depotId, DictionaryType.年龄段, item.Age);
         db.DepotDictionaryAdd(depotId, DictionaryType.存放地, item.Place);
     }
 }
 public static void DepotActInRedo(this DepotEntities db, Guid depotId, DepotIn @in, decimal backed, Guid operatorId)
 {
     if (backed == 0)
         return;
     var obj = db.DepotObject.Single(o => o.Id == @in.ObjectId);
     var order = db.DepotOrder.Single(o => o.Id == @in.OrderId);
     decimal amount = @in.Amount - backed;
     decimal money = (@in.Amount - backed) * @in.Price;
     decimal plusAmount = -backed;
     decimal plusMoney = -backed * @in.Price;
     db.DepotActStatistics(obj.Id, @in.Time, plusAmount, plusMoney, 0, 0, 0, 0, 0, 0, 0, 0);
     if (obj.Single)
     {
         if (amount >= 0 && money >= 0)
         {
             foreach (var inx in @in.DepotInX)
             {
                 if (inx.DepotUseX.Count > 0)
                     return;
             }
             @in.Amount = amount;
             @in.AvailableAmount = amount;
             @in.Total = money;
             @in.Price = decimal.Divide(money, amount);
             obj.Amount += plusAmount;
             obj.Money += plusMoney;
             order.Paid += plusMoney;
             var dr = new DepotRedo
             {
                 Id = db.GlobalId(),
                 DepotId = depotId,
                 UserId = operatorId,
                 ObjectId = obj.Id,
                 InId = @in.Id,
                 Amount = backed,
                 Money = backed * @in.Price,
                 Time = DateTime.Now,
                 Note = ""
             };
             db.DepotRedo.Add(dr);
             var left = backed;
             foreach (var inx in @in.DepotInX.OrderByDescending(o => o.Ordinal).ToList())
             {
                 inx.Amount = 0;
                 inx.AvailableAmount = 0;
                 inx.Total = 0;
                 var flowx = new DepotFlowX
                 {
                     Id = db.GlobalId(),
                     ObjectId = @in.ObjectId,
                     ObjectOrdinal = inx.Ordinal,
                     UserId = operatorId,
                     Type = FlowType.退货,
                     TypeName = FlowType.退货.ToString(),
                     Time = @in.Time,
                     Amount = plusAmount,
                     Money = plusMoney,
                     Note = ""
                 };
                 db.DepotFlowX.Add(flowx);
                 left--;
                 if (left == 0)
                     break;
             }
         }
     }
     else
     {
         var x = @in.DepotInX.First();
         if (x.DepotUseX.Count(o => o.InXId == x.Id) > 0)
             return;
         if (amount >= 0 && money >= 0)
         {
             @in.Amount = amount;
             @in.AvailableAmount = amount;
             @in.Total = money;
             @in.Price = amount == 0 ? 0 : decimal.Divide(money, amount);
             obj.Amount += plusAmount;
             obj.Money += plusMoney;
             order.Paid += plusMoney;
             var flow = new DepotFlow
             {
                 Id = db.GlobalId(),
                 ObjectId = @in.ObjectId,
                 UserId = operatorId,
                 Type = FlowType.退货,
                 TypeName = FlowType.退货.ToString(),
                 Time = @in.Time,
                 Amount = plusAmount,
                 Money = plusMoney,
                 Note = ""
             };
             db.DepotFlow.Add(flow);
             x.Amount = @in.Amount;
             x.AvailableAmount = @in.Amount;
             x.Total = @in.Total;
             var dr = new DepotRedo
             {
                 Id = db.GlobalId(),
                 DepotId = depotId,
                 UserId = operatorId,
                 ObjectId = obj.Id,
                 InId = @in.Id,
                 Amount = backed,
                 Money = backed * @in.Price,
                 Time = DateTime.Now,
                 Note = ""
             };
             db.DepotRedo.Add(dr);
         }
     }
     db.SaveChanges();
 }