public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            if ((request.Direction != 1 && request.Direction != 0) || request.CollectionId == 0 || string.IsNullOrEmpty(request.TraceCode) || request.Quantity <= 0)
            {
                throw new BusinessException("AddWrongInformation");
            }

            var employee = await WebHelper.HttpGet <Employee>(GlobalConfiguration.APIGateWayURI, AppUrl.GetEmployeeByUser, request.LoginSession.AccessToken);

            if (employee == null)
            {
                throw new NotPermissionException();
            }
            var collection = (await collectionQueries.GetsByEmployeeId(employee.Id)).FirstOrDefault(c => c.Id == request.CollectionId);

            if (collection == null)
            {
                throw new NotPermissionException();
            }

            var rs        = -1;
            var inventory = await collectionInventoryQueries.GetByTraceCode(request.TraceCode);

            if (inventory == null)
            {
                if (request.Direction == 0)
                {
                    throw new BusinessException("Collection.Inventory.TraceCode.NotExisted");
                }
                using (var conn = DALHelper.GetConnection())
                {
                    conn.Open();
                    using (var trans = conn.BeginTransaction())
                    {
                        try
                        {
                            collectionInventoryRepository.JoinTransaction(conn, trans);
                            collectionInventoryHistoryRepository.JoinTransaction(conn, trans);

                            await collectionInventoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventory()
                            {
                                CollectionId = request.CollectionId,
                                ProductId    = request.ProductId,
                                UoMId        = request.UoMId,
                                TraceCode    = request.TraceCode,
                                Quantity     = request.Quantity,
                                ModifiedBy   = request.LoginSession.Id,
                                ModifiedDate = DateTime.Now
                            });

                            await collectionInventoryHistoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventoryHistory()
                            {
                                TraceCode    = request.TraceCode,
                                CollectionId = request.CollectionId,
                                ProductId    = request.ProductId,
                                UoMId        = request.UoMId,
                                Direction    = request.Direction,
                                Quantity     = request.Quantity,
                                LastQuantity = request.Quantity,
                                CreatedDate  = DateTime.Now,
                                CreatedBy    = request.LoginSession.Id,
                                Reason       = request.Reason
                            });

                            return(rs = 0);
                        }
                        finally
                        {
                            if (rs == 0)
                            {
                                trans.Commit();
                            }
                        }
                    }
                }
            }
            else
            {
                if (request.CollectionId != inventory.CollectionId)
                {
                    throw new BusinessException("Collection.Inventory.TraceCode.NotSameCollection");
                }

                using (var conn = DALHelper.GetConnection())
                {
                    conn.Open();
                    using (var trans = conn.BeginTransaction())
                    {
                        try
                        {
                            collectionInventoryRepository.JoinTransaction(conn, trans);
                            collectionInventoryHistoryRepository.JoinTransaction(conn, trans);

                            var quantity = inventory.Quantity + (request.Direction == 1 ? request.Quantity : -request.Quantity);
                            if (request.Direction == 0 && quantity < 0)
                            {
                                throw new BusinessException("Collection.Inventory.TraceCode.NotEnoughQuantity");
                            }

                            inventory.Quantity     = quantity;
                            inventory.ModifiedDate = DateTime.Now;
                            inventory.ModifiedBy   = request.LoginSession.Id;
                            await collectionInventoryRepository.Update(inventory);

                            await collectionInventoryHistoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventoryHistory()
                            {
                                TraceCode    = request.TraceCode,
                                CollectionId = request.CollectionId,
                                ProductId    = inventory.ProductId,
                                UoMId        = inventory.UoMId,
                                Direction    = request.Direction,
                                Quantity     = request.Quantity,
                                LastQuantity = quantity,
                                CreatedDate  = DateTime.Now,
                                CreatedBy    = request.LoginSession.Id,
                                Reason       = request.Reason
                            });

                            return(rs = 0);
                        }
                        finally
                        {
                            if (rs == 0)
                            {
                                trans.Commit();
                            }
                        }
                    }
                }
            }
        }
        public override async Task <int> HandleCommand(UpdateStatusCommand request, CancellationToken cancellationToken)
        {
            if (request.ShippingId == 0 || request.StatusId == 0)
            {
                throw new BusinessException("AddWrongInformation");
            }

            if (!Enum.IsDefined(typeof(TripStatuses), (int)request.StatusId))
            {
                throw new BusinessException("Trip.NotExistedStatus");
            }
            var status = (TripStatuses)request.StatusId;

            var shipping = await cFShippingQueries.Get(request.ShippingId);

            if (shipping == null)
            {
                throw new BusinessException("Trip.NotExisted");
            }

            if (shipping.StatusId == (int)TripStatuses.Finished || shipping.StatusId == (int)TripStatuses.Canceled)
            {
                throw new BusinessException("Trip.WrongStep");
            }

            if (status != TripStatuses.Canceled)
            {
                if (request.StatusId != shipping.StatusId + 1)
                {
                    throw new BusinessException("Trip.WrongStep");
                }
            }

            if (status == TripStatuses.Confirmed || (status == TripStatuses.Canceled && shipping.StatusId != (int)TripStatuses.Created))
            {
                var employee = await WebHelper.HttpGet <Employee>(GlobalConfiguration.APIGateWayURI, UI.AppUrl.GetEmployeeByUser, request.LoginSession.AccessToken);

                if (employee == null)
                {
                    throw new NotPermissionException();
                }
                var collection = (await collectionQueries.GetsByEmployeeId(employee.Id)).FirstOrDefault(c => c.Id == shipping.CollectionId);
                if (collection == null)
                {
                    throw new NotPermissionException();
                }

                if (status == TripStatuses.Confirmed && shipping.ShipperId == null)
                {
                    throw new BusinessException("Distribution.Trip.RequiredDeliveryMan");
                }

                using (var conn = DALHelper.GetConnection())
                {
                    conn.Open();
                    using (var trans = conn.BeginTransaction())
                    {
                        var rs = -1;
                        try
                        {
                            cFShippingQueries.JoinTransaction(conn, trans);
                            cFShippingRepository.JoinTransaction(conn, trans);
                            collectionInventoryQueries.JoinTransaction(conn, trans);
                            collectionInventoryHistoryRepository.JoinTransaction(conn, trans);

                            var items = await cFShippingQueries.GetItems(shipping.Id);

                            if (items.Count() == 0)
                            {
                                throw new BusinessException("Collection.Shipping.NotExsitedItem");
                            }
                            foreach (var item in items)
                            {
                                var inventory = await collectionInventoryQueries.GetByTraceCode(item.TraceCode);

                                if (status == TripStatuses.Confirmed)
                                {
                                    if (inventory == null || inventory.CollectionId != shipping.CollectionId)
                                    {
                                        throw new BusinessException("Collection.Inventory.TraceCode.NotExisted");
                                    }

                                    if (inventory.Quantity < item.ShippedQuantity)
                                    {
                                        throw new BusinessException("Collection.Inventory.TraceCode.NotEnoughQuantity");
                                    }

                                    inventory.Quantity -= item.ShippedQuantity;
                                    if (inventory.Quantity == 0)
                                    {
                                        await collectionInventoryRepository.Delete(inventory.Id);
                                    }
                                    else
                                    {
                                        inventory.ModifiedDate = DateTime.Now;
                                        inventory.ModifiedBy   = request.LoginSession.Id;
                                        await collectionInventoryRepository.Update(inventory);
                                    }
                                    await collectionInventoryHistoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventoryHistory()
                                    {
                                        TraceCode    = inventory.TraceCode,
                                        CollectionId = inventory.CollectionId,
                                        ProductId    = inventory.ProductId,
                                        UoMId        = inventory.UoMId,
                                        Direction    = 0,
                                        Quantity     = item.ShippedQuantity,
                                        LastQuantity = inventory.Quantity,
                                        CreatedDate  = DateTime.Now,
                                        CreatedBy    = request.LoginSession.Id
                                    });
                                }
                                else
                                {
                                    if (inventory == null)
                                    {
                                        await collectionInventoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventory()
                                        {
                                            CollectionId = shipping.CollectionId,
                                            ProductId    = item.ProductId,
                                            UoMId        = item.UoMId,
                                            TraceCode    = item.TraceCode,
                                            Quantity     = item.ShippedQuantity,
                                            ModifiedBy   = request.LoginSession.Id,
                                            ModifiedDate = DateTime.Now
                                        });
                                    }
                                    else
                                    {
                                        inventory.Quantity    += item.ShippedQuantity;
                                        inventory.ModifiedDate = DateTime.Now;
                                        inventory.ModifiedBy   = request.LoginSession.Id;
                                        await collectionInventoryRepository.Update(inventory);
                                    }

                                    await collectionInventoryHistoryRepository.Add(new MDM.UI.Collections.Models.CollectionInventoryHistory()
                                    {
                                        TraceCode    = item.TraceCode,
                                        CollectionId = shipping.CollectionId,
                                        ProductId    = item.ProductId,
                                        UoMId        = item.UoMId,
                                        Direction    = 1,
                                        Quantity     = item.ShippedQuantity,
                                        LastQuantity = inventory == null ? item.ShippedQuantity : item.ShippedQuantity + inventory.Quantity,
                                        CreatedDate  = DateTime.Now,
                                        CreatedBy    = request.LoginSession.Id
                                    });
                                }
                            }

                            shipping          = UpdateBuild(shipping, request.LoginSession);
                            shipping.StatusId = request.StatusId;
                            return(rs = await cFShippingRepository.Update(shipping));
                        }
                        finally
                        {
                            if (rs == 0)
                            {
                                trans.Commit();
                            }
                            else
                            {
                                try
                                {
                                    trans.Rollback();
                                }
                                catch { }
                            }
                        }
                    }
                }
            }
            else
            {
                shipping          = UpdateBuild(shipping, request.LoginSession);
                shipping.StatusId = request.StatusId;
                return(await cFShippingRepository.Update(shipping));
            }
        }