Exemplo n.º 1
0
        /// <summary>
        /// Add Boxes to the stock.
        /// if the box exist will only update the Count
        /// if the box doesn't exist will create a new record
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="count"></param>
        /// <param name="timeLastPurchase"></param>
        void AddToStock(double x, double y, int count, DateTime?timeLastPurchase = null, bool saveToDB = true, int?id = null)
        {
            bool newBox = false;
            //Get the mainTreeData for the search term from the main tree
            var mainTreeSearchTerm = new BottomSizeTreeDataModel(x);

            _mainTree.FindAndUpdate(mainTreeSearchTerm, out BottomSizeTreeDataModel mainTreeData);

            //Get the innerTreeData for the searchTerm in the inner tree
            var innerTreeSearchTerm = new HeightSizeTreeDataModel(y);

            if (mainTreeData.InnerTree is null)
            {
                mainTreeData.InnerTree = new BinarySearchTree <HeightSizeTreeDataModel>();
            }

            //if the inner TreeData is new
            if (mainTreeData.InnerTree.FindAndUpdate(innerTreeSearchTerm, out HeightSizeTreeDataModel innerTreeData) == false)
            {
                //if there isn't time data, than will get the current date and time
                if (timeLastPurchase.HasValue == false)
                {
                    innerTreeData.DateNode = _dataByTime.Add(new TimeListDataModel()
                    {
                        X = x, Y = y, TimeLastPurchase = DateTime.Now
                    });
                }
                //if there is a time last purchase it means this should be added to a diffrent position.
                //will never be null here
                else
                {
                    innerTreeData.DateNode = _dataByTime.InsertByValue(new TimeListDataModel {
                        X = x, Y = y, TimeLastPurchase = timeLastPurchase.Value
                    });
                }

                newBox = true;
            }
            //if the inner tree data is old
            else
            {
                //update the data
                innerTreeData.DateNode.Data.TimeLastPurchase = DateTime.Now;
                //Shift the node to the end of the list
                _dataByTime.ShiftNodeToEnd(innerTreeData.DateNode);
            }

            //Set the Properties
            innerTreeData.Count += count;
            //if there is no value than id will be 0 by default(int)
            if (id.HasValue)
            {
                innerTreeData.Id = id.Value;
            }

            //the box of this size
            var box = new Box
            {
                Id               = innerTreeData.Id,
                X                = mainTreeData.X,
                Y                = innerTreeData.Y,
                Count            = innerTreeData.Count,
                TimeLastPurchase = innerTreeData.DateNode.Data.TimeLastPurchase
            };

            //Add Data To DB
            if (saveToDB)
            {
                _logger.Log(new LogData("Updated Stock", true));
                //save the id
                innerTreeData.Id = SaveToDB(box, newBox).Id;
            }
        }