public void CreatePallet([FromUri] string grandNumber, [FromUri] int pltQuantity, [FromUri] string pltSize, [FromUri] bool doesAppliedLabel, [FromUri] bool hasSortingMarking, [FromUri] bool isOverSizeOrOverwidth, [FromUri] string packType, [FromUri] bool isSelectedByCheckBox, [FromBody] IEnumerable <PalletInfoDto> objArray)
        {
            //进入库存的carton对象都按GW/ctn,CBM/ctn记录
            var cartonLocationList = new List <FBACartonLocation>();
            var orderDetailsInDb   = _context.FBAOrderDetails
                                     .Where(x => x.GrandNumber == grandNumber);
            var masterOrderInDb = _context.FBAMasterOrders.First(x => x.GrandNumber == grandNumber);

            if (masterOrderInDb.Container == "NULL" || masterOrderInDb.Container == "")
            {
                throw new Exception("Must assign container number befroe operation.");
            }

            if (objArray.Count() == 0)
            {
                throw new Exception("No items or cartons were selected.");
            }

            if (packType == FBAPackType.DetailPack)
            {
                if (isSelectedByCheckBox == true)
                {
                    throw new Exception("Detail pack does not support selecting items by check-boxes.");
                }

                foreach (var obj in objArray)
                {
                    var orderDetailInDb = orderDetailsInDb.SingleOrDefault(x => x.Id == obj.Id);

                    orderDetailInDb.ComsumedQuantity += obj.Quantity * pltQuantity;

                    if (orderDetailInDb.ComsumedQuantity > orderDetailInDb.ActualQuantity)
                    {
                        throw new Exception("Not enough quantity for comsuming. Check Id:" + obj.Id);
                    }

                    var cartonLocation    = new FBACartonLocation();
                    var ctnsPerPlt        = obj.Quantity;
                    var grossWeightPerCtn = (float)Math.Round((orderDetailInDb.ActualGrossWeight / orderDetailInDb.ActualQuantity), 2);
                    var cbmPerCtn         = (float)Math.Round((orderDetailInDb.ActualCBM / orderDetailInDb.ActualQuantity), 2);

                    cartonLocation.AssembleFirstStringPart(orderDetailInDb.ShipmentId, orderDetailInDb.AmzRefId, orderDetailInDb.WarehouseCode);
                    cartonLocation.AssemblePltInfo(grossWeightPerCtn, cbmPerCtn, ctnsPerPlt);

                    cartonLocation.Container = orderDetailInDb.Container;
                    //cartonLocation.AvaliableCtns = cartonLocation.ActualQuantity;
                    cartonLocation.Location       = "Pallet";
                    cartonLocation.HowToDeliver   = orderDetailInDb.HowToDeliver;
                    cartonLocation.GrandNumber    = grandNumber;
                    cartonLocation.FBAOrderDetail = orderDetailInDb;
                    cartonLocation.ActualQuantity = ctnsPerPlt * pltQuantity;
                    cartonLocation.AvailableCtns  = ctnsPerPlt * pltQuantity;
                    cartonLocation.Status         = FBAStatus.InPallet;

                    cartonLocationList.Add(cartonLocation);
                }

                //建立FBAPallet对象
                var pallet           = new FBAPallet();
                var firstId          = objArray.First().Id;
                var firstOrderDetail = orderDetailsInDb
                                       .Include(x => x.FBAMasterOrder)
                                       .SingleOrDefault(x => x.Id == firstId);

                pallet.AssembleFirstStringPart(DistinctStringList(cartonLocationList.Select(x => x.ShipmentId)), DistinctStringList(cartonLocationList.Select(x => x.AmzRefId)), DistinctStringList(cartonLocationList.Select(x => x.WarehouseCode)));
                pallet.AssembleActualDetails(cartonLocationList.Sum(x => x.GrossWeightPerCtn * x.CtnsPerPlt * pltQuantity), cartonLocationList.Sum(x => x.CBMPerCtn * x.CtnsPerPlt * pltQuantity), cartonLocationList.Sum(x => x.CtnsPerPlt * pltQuantity));
                pallet.AssembleBoolValue(doesAppliedLabel, hasSortingMarking, isOverSizeOrOverwidth);

                pallet.Container      = firstOrderDetail.Container;
                pallet.HowToDeliver   = firstOrderDetail.HowToDeliver;
                pallet.PalletSize     = pltSize;
                pallet.GrandNumber    = grandNumber;
                pallet.FBAMasterOrder = firstOrderDetail.FBAMasterOrder;
                pallet.ActualPallets  = pltQuantity;

                _context.FBAPallets.Add(pallet);

                foreach (var cartonLocation in cartonLocationList)
                {
                    cartonLocation.FBAPallet = pallet;
                }

                _context.FBACartonLocations.AddRange(cartonLocationList);
            }
            else
            {
                foreach (var obj in objArray)
                {
                    var orderDetailInDb = orderDetailsInDb.SingleOrDefault(x => x.Id == obj.Id);
                    var packedCtn       = 0;

                    //如果是处理复选框对象,则默认分配所有剩下的箱数,否则按照数组中的数量分配
                    if (isSelectedByCheckBox == true)
                    {
                        packedCtn = orderDetailInDb.ActualQuantity - orderDetailInDb.ComsumedQuantity;
                        orderDetailInDb.ComsumedQuantity = orderDetailInDb.ActualQuantity;
                    }
                    else
                    {
                        packedCtn = obj.Quantity;
                        orderDetailInDb.ComsumedQuantity += obj.Quantity;
                    }

                    if (orderDetailInDb.ComsumedQuantity > orderDetailInDb.ActualQuantity)
                    {
                        throw new Exception("Not enough quantity for comsuming. Check Id:" + obj.Id);
                    }

                    var cartonLocation = new FBACartonLocation();
                    cartonLocation.Status = FBAStatus.InPallet;
                    var grossWeightPerCtn = (float)Math.Round((orderDetailInDb.ActualGrossWeight / orderDetailInDb.ActualQuantity), 2);
                    var cbmPerCtn         = (float)Math.Round((orderDetailInDb.ActualCBM / orderDetailInDb.ActualQuantity), 2);

                    cartonLocation.AssembleFirstStringPart(orderDetailInDb.ShipmentId, orderDetailInDb.AmzRefId, orderDetailInDb.WarehouseCode);
                    cartonLocation.AssemblePltInfo(grossWeightPerCtn, cbmPerCtn, 0);

                    cartonLocation.Container      = orderDetailInDb.Container;
                    cartonLocation.Location       = "Pallet";
                    cartonLocation.HowToDeliver   = orderDetailInDb.HowToDeliver;
                    cartonLocation.GrandNumber    = grandNumber;
                    cartonLocation.FBAOrderDetail = orderDetailInDb;
                    cartonLocation.ActualQuantity = packedCtn;
                    cartonLocation.AvailableCtns  = packedCtn;

                    cartonLocationList.Add(cartonLocation);
                }

                //建立FBAPallet对象
                var pallet = new FBAPallet();

                var firstId          = objArray.First().Id;
                var firstOrderDetail = orderDetailsInDb
                                       .Include(x => x.FBAMasterOrder)
                                       .SingleOrDefault(x => x.Id == firstId);

                pallet.AssembleFirstStringPart(DistinctStringList(cartonLocationList.Select(x => x.ShipmentId)), DistinctStringList(cartonLocationList.Select(x => x.AmzRefId)), DistinctStringList(cartonLocationList.Select(x => x.WarehouseCode)));
                pallet.ActualQuantity = cartonLocationList.Sum(x => x.ActualQuantity);
                pallet.AssembleBoolValue(doesAppliedLabel, hasSortingMarking, isOverSizeOrOverwidth);

                pallet.Container      = firstOrderDetail.Container;
                pallet.HowToDeliver   = firstOrderDetail.HowToDeliver;
                pallet.PalletSize     = pltSize;
                pallet.FBAMasterOrder = firstOrderDetail.FBAMasterOrder;
                pallet.GrandNumber    = grandNumber;
                pallet.ActualPallets  = pltQuantity;

                _context.FBAPallets.Add(pallet);

                foreach (var cartonLocation in cartonLocationList)
                {
                    cartonLocation.FBAPallet = pallet;
                }

                _context.FBACartonLocations.AddRange(cartonLocationList);
            }

            _context.SaveChanges();
        }
        public void CreateLocationObjects([FromUri] string grandNumber, [FromUri] string inventoryType, [FromBody] IEnumerable <FBALocationDto> objArray)
        {
            var masterOrderInDb = _context.FBAMasterOrders.SingleOrDefault(x => x.GrandNumber == grandNumber);

            if (inventoryType == FBAInventoryType.Pallet)
            {
                var palletLocationList = new List <FBAPalletLocation>();
                var palletsInDb        = _context.FBAPallets
                                         .Include(x => x.FBACartonLocations)
                                         .Where(x => x.GrandNumber == grandNumber &&
                                                x.ActualPallets - x.ComsumedPallets > 0);

                if (palletsInDb.Count() == 0)
                {
                    throw new Exception("No quantity for allocating.");
                }

                foreach (var obj in objArray)
                {
                    var palletInDb = palletsInDb
                                     .Include(x => x.FBACartonLocations)
                                     .SingleOrDefault(x => x.Id == obj.Id);

                    //如果这是一个rough packed pallets,那么默认分配所有货物
                    //if (palletInDb.FBACartonLocations.Sum(x => x.CtnsPerPlt) == 0)
                    //{
                    //    obj.Quantity = palletInDb.ActualPallets;
                    //}

                    //所有类型的pallets现在不允许分开入库
                    obj.Quantity = palletInDb.ActualPallets - palletInDb.ComsumedPallets;

                    palletInDb.ComsumedPallets += obj.Quantity;

                    if (palletInDb.ComsumedPallets > palletInDb.ActualPallets)
                    {
                        throw new Exception("Not enough quantity for comsuming. Check Id:" + obj.Id);
                    }

                    var palletLocation = new FBAPalletLocation();

                    palletLocation.Status       = FBAStatus.InStock;
                    palletLocation.HowToDeliver = palletInDb.HowToDeliver;
                    //palletLocation.GrossWeightPerPlt = palletInDb.ActualGrossWeight / palletInDb.ActualPallets;
                    palletLocation.GrossWeightPerPlt = palletInDb.FBACartonLocations.Sum(x => x.GrossWeightPerCtn * x.ActualQuantity) / obj.Quantity;
                    //palletLocation.CBMPerPlt = palletInDb.ActualCBM / palletInDb.ActualPallets;
                    palletLocation.CBMPerPlt     = palletInDb.FBACartonLocations.Sum(x => x.CBMPerCtn * x.ActualQuantity) / obj.Quantity;
                    palletLocation.CtnsPerPlt    = palletInDb.FBACartonLocations.Sum(x => x.CtnsPerPlt) == 0 ? 0 : palletInDb.ActualQuantity / palletInDb.ActualPallets;
                    palletLocation.AvailablePlts = obj.Quantity;
                    palletLocation.Location      = obj.Location;
                    palletLocation.PalletSize    = palletInDb.PalletSize;

                    palletLocation.AssembleFirstStringPart(palletInDb.ShipmentId, palletInDb.AmzRefId, palletInDb.WarehouseCode);
                    //PalletLocation的Actualquantity指内含cartons的总数量
                    palletLocation.AssembleActualDetails(palletLocation.GrossWeightPerPlt * obj.Quantity, palletLocation.CBMPerPlt * obj.Quantity, palletInDb.FBACartonLocations.Sum(x => x.ActualQuantity));
                    palletLocation.ActualPlts = obj.Quantity;
                    palletLocation.AssembleUniqueIndex(palletInDb.Container, palletInDb.GrandNumber);

                    palletLocation.FBAMasterOrder = masterOrderInDb;
                    palletLocation.FBAPallet      = palletInDb;

                    palletLocationList.Add(palletLocation);
                }
                _context.FBAPalletLocations.AddRange(palletLocationList);
            }
            else
            {
                var cartonLocationList = new List <FBACartonLocation>();
                var orderDetailsInDb   = _context.FBAOrderDetails
                                         .Where(x => x.GrandNumber == grandNumber &&
                                                x.ActualQuantity - x.ComsumedQuantity > 0);

                if (orderDetailsInDb.Count() == 0)
                {
                    throw new Exception("No quantity for allocating.");
                }

                foreach (var obj in objArray)
                {
                    var orderDetailInDb = orderDetailsInDb.SingleOrDefault(x => x.Id == obj.Id);
                    orderDetailInDb.ComsumedQuantity += obj.Quantity;

                    if (orderDetailInDb.ComsumedQuantity > orderDetailInDb.ActualQuantity)
                    {
                        throw new Exception("Not enough quantity for comsuming. Check Id:" + obj.Id);
                    }

                    if (orderDetailInDb.Container == "NULL" || orderDetailInDb.Container == "")
                    {
                        throw new Exception("Please assign container number first.");
                    }

                    var cartonLocation = new FBACartonLocation();

                    cartonLocation.Status            = FBAStatus.InStock;
                    cartonLocation.HowToDeliver      = orderDetailInDb.HowToDeliver;
                    cartonLocation.GrossWeightPerCtn = (float)Math.Round((orderDetailInDb.ActualGrossWeight / orderDetailInDb.ActualQuantity), 2);
                    cartonLocation.CBMPerCtn         = (float)Math.Round((orderDetailInDb.ActualCBM / orderDetailInDb.ActualQuantity), 2);
                    cartonLocation.AvailableCtns     = obj.Quantity;
                    cartonLocation.Location          = obj.Location;

                    cartonLocation.AssembleFirstStringPart(orderDetailInDb.ShipmentId, orderDetailInDb.AmzRefId, orderDetailInDb.WarehouseCode);
                    cartonLocation.AssembleActualDetails(cartonLocation.GrossWeightPerCtn * obj.Quantity, cartonLocation.CBMPerCtn * obj.Quantity, obj.Quantity);
                    cartonLocation.AssembleUniqueIndex(orderDetailInDb.Container, orderDetailInDb.GrandNumber);

                    cartonLocation.FBAOrderDetail = orderDetailInDb;

                    cartonLocationList.Add(cartonLocation);
                }

                _context.FBACartonLocations.AddRange(cartonLocationList);
            }
            _context.SaveChanges();
        }