//按最大采购数量填充到货数量
        public void FillAllPurchaseTotal(IList <PurchaseViewModel> models)
        {
            foreach (var item in models)
            {
                var purchase = _purchaseRepository.FindBy(x => x.PurchaseId == item.PurchaseId).Include(x => x.PurchaseApplication.PurchaseApplicationHeader.RequestHeader).FirstOrDefault();
                var inStocks = _inStockRepository.FindBy(x => x.PurchaseId == item.PurchaseId).ToArray();
                if (purchase == null)
                {
                    continue;
                }
                {
                    var totalInStock  = inStocks.Any() ? inStocks.Sum(x => x.Total) : 0;
                    var purchaseTotal = purchase.PurchaseTotal;
                    purchase.ReadyForInStock  = purchaseTotal - totalInStock;
                    purchase.LastDeliveryDate = DateTime.Now;
                    _purchaseRepository.Edit(purchase);
                    _purchaseRepository.Save();

                    //if (purchase.ReadyForInStock == purchase.PurchaseTotal)
                    //{
                    if (purchase.PurchaseApplication.PurchaseApplicationHeader.RequestHeader.RequestCategory == RequestCategoriesEnum.采购退货)
                    {
                        UpdatePurchaseProcessStatus(item.PurchaseId, ProcessStatusEnum.退货申请完成);
                    }
                    else
                    {
                        UpdatePurchaseProcessStatus(item.PurchaseId, ProcessStatusEnum.采购完成);
                        purchase.LastDeliveryDate = DateTime.Now;
                    }

                    // }
                }
            }
        }
        //从需求单入库
        public void CreateInStock(IList <RequestViewModel> models, string temp_instockNumber)
        {
            if (models == null || models.Count <= 0)
            {
                return;
            }
            RequestHeader requestHeader    = _requestHeaderRepository.GetRequestHeader(models.FirstOrDefault()?.RequestNumber);
            InStockHeader newInStockHeader = _mapper.Map <InStockHeader>(requestHeader);
            int           lastSerialNumber = _inStockHeaderRepository.GetLatestSerialNumber(DateTime.Now);

            newInStockHeader.SerialNo      = ++lastSerialNumber;
            newInStockHeader.InStockNumber = string.IsNullOrEmpty(temp_instockNumber) ? ServiceHelper.GenerateCodeNumber("RK", newInStockHeader.SerialNo) : temp_instockNumber;

            var listOfOutStocksExisting = _outStockRepository.FindBy(x => x.Type == requestHeader.RequestCategory).ToArray();
            var listOfInStocksExisting  = _inStockRepository.FindBy(x => x.Type == requestHeader.RequestCategory).ToArray();

            try
            {
                _inStockHeaderRepository.Add(newInStockHeader);
                _inStockHeaderRepository.Save();
                foreach (RequestViewModel model in models)
                {
                    var newInStock = _mapper.Map <InStock>(model);
                    newInStock.Type          = model.RequestCategory;
                    newInStock.InStockNumber = newInStockHeader.InStockNumber;
                    switch (model.RequestCategory)
                    {
                    case RequestCategoriesEnum.工程车维修 when model.Status == ProcessStatusEnum.需求建立:
                        newInStock.Status = ProcessStatusEnum.工程车维修入库;
                        _requestService.UpdateRequestProcessStatus(model.RequestId, ProcessStatusEnum.工程车维修入库);
                        break;

                    case RequestCategoriesEnum.工程车维修:
                    {
                        if (model.Status == ProcessStatusEnum.维修中)
                        {
                            var alreadyOutStocks = listOfOutStocksExisting.Where(x => x.RequestId == model.RequestId && x.Status == ProcessStatusEnum.维修中).Sum(s => s.Total);
                            var alreadyInStocks  = listOfInStocksExisting.Where(x => x.RequestId == model.RequestId && x.Status == ProcessStatusEnum.维修完成).Sum(s => s.Total);
                            newInStock.Status = ProcessStatusEnum.维修完成;
                            if (alreadyInStocks + newInStock.Total == alreadyOutStocks)
                            {
                                _requestService.UpdateRequestProcessStatus(model.RequestId, ProcessStatusEnum.维修完成);
                            }
                        }

                        break;
                    }

                    case RequestCategoriesEnum.工具维修:
                    {
                        var alreadyOutStocks = listOfOutStocksExisting.Where(x => x.RequestId == model.RequestId && x.Status == ProcessStatusEnum.维修中).Sum(s => s.Total);
                        var alreadyInStocks  = listOfInStocksExisting.Where(x => x.RequestId == model.RequestId && x.Status == ProcessStatusEnum.维修完成).Sum(s => s.Total);
                        newInStock.Status = ProcessStatusEnum.维修完成;
                        if (alreadyInStocks + newInStock.Total == alreadyOutStocks)
                        {
                            _requestService.UpdateRequestProcessStatus(model.RequestId, ProcessStatusEnum.维修完成);
                        }

                        break;
                    }

                    case RequestCategoriesEnum.工具借出:
                    {
                        var alreadyOutStocks = listOfOutStocksExisting.Where(x => x.RequestId == model.RequestId && x.Status == ProcessStatusEnum.借出出库).Sum(s => s.Total);
                        var alreadyInStocks  = listOfInStocksExisting.Where(x => x.RequestId == model.RequestId && x.Status == ProcessStatusEnum.归还入库).Sum(s => s.Total);
                        newInStock.Status = ProcessStatusEnum.归还入库;
                        if (alreadyInStocks + newInStock.Total == alreadyOutStocks)
                        {
                            _requestService.UpdateRequestProcessStatus(model.RequestId, ProcessStatusEnum.归还入库);
                        }

                        break;
                    }

                    case RequestCategoriesEnum.物品退回:
                        newInStock.Status = ProcessStatusEnum.退回入库;
                        _requestService.UpdateRequestProcessStatus(model.RequestId, ProcessStatusEnum.退回入库);
                        break;
                    }

                    _inStockRepository.Add(newInStock);
                    _inStockRepository.Save();
                    _managementService.UpdateStorage(model.Code, "Stage", newInStock.Total, newInStock.InStockNumber);
                }
            }
            catch (DbUpdateException)
            {
            }
        }