public void CreateApplication(IList <RequestViewModel> requestViewModels, string poNumber, string temp_ApplicationNumber)
        {
            foreach (var requestViewModel in requestViewModels.Where(x => x.ToApplyTotal > 0))
            {
                var currentRequestHeaderNumber = requestViewModel.RequestNumber;
                var purchaseApplicationHeader  = _purchaseApplicationHeaderRepository.FindBy(x => x.RequestNumber == currentRequestHeaderNumber).FirstOrDefault();

                if (purchaseApplicationHeader == null) //创建一个新的采购申请Header
                {
                    var requestHeader = _requestHeaderRepository.FindBy(x => x.RequestHeaderNumber == currentRequestHeaderNumber).FirstOrDefault();
                    //TO DO mapping
                    var newPurchaseApplicationHeader = _mapper.Map <PurchaseApplicationHeader>(requestHeader);
                    //Create a new purchase application header.
                    var prefix           = newPurchaseApplicationHeader.RequestCategory == RequestCategoriesEnum.采购退货 ? "THSQ" : "SQ";
                    var lastSerialNumber = _purchaseApplicationHeaderRepository.GetLatestSerialNumber(DateTime.Now);
                    newPurchaseApplicationHeader.SerialNo = ++lastSerialNumber;
                    newPurchaseApplicationHeader.PurchaseApplicationNumber = string.IsNullOrEmpty(temp_ApplicationNumber) ? ServiceHelper.GenerateCodeNumber(prefix, newPurchaseApplicationHeader.SerialNo) : temp_ApplicationNumber;
                    _purchaseApplicationHeaderRepository.Add(newPurchaseApplicationHeader);
                    try
                    {
                        _purchaseApplicationHeaderRepository.Save();
                        purchaseApplicationHeader = newPurchaseApplicationHeader;

                        //Update PO Number of contract in RequestHeader.
                        if (!string.IsNullOrEmpty(poNumber))
                        {
                            var poNumberFromDb = _managementService.GetPoNumberIfNotExisting(poNumber);
                            if (requestHeader != null)
                            {
                                requestHeader.Contract.PoId = poNumberFromDb;
                                requestHeader.UpdateDate    = DateTime.Now;
                            }

                            _requestHeaderRepository.Save();
                        }
                    }
                    catch (Exception e)
                    {
                        // ignored
                        return;
                    }
                }
                var newPurchaseApplication = _mapper.Map <PurchaseApplication>(requestViewModel);
                newPurchaseApplication.CurrentPurchasePrice = _managementService.GetItemById(requestViewModel.ItemId).Price;
                if (purchaseApplicationHeader != null)
                {
                    newPurchaseApplication.PurchaseApplicationNumber =
                        purchaseApplicationHeader.PurchaseApplicationNumber;
                    newPurchaseApplication.ProcessStatus = ProcessStatusEnum.申请审核中;

                    if (purchaseApplicationHeader.RequestCategory == RequestCategoriesEnum.采购退货)
                    {
                        var latestpurchase = _purchaseRepository
                                             .FindBy(x => x.PurchaseApplication.ItemId == requestViewModel.ItemId)
                                             .OrderByDescending(x => x.CreateDate).FirstOrDefault();
                        if (latestpurchase != null)
                        {
                            newPurchaseApplication.SupplierId           = latestpurchase.PurchaseApplication.SupplierId;
                            newPurchaseApplication.CurrentPurchasePrice = latestpurchase.CurrentPurchasePrice;
                        }
                    }
                }

                _purchaseApplicationRepository.Add(newPurchaseApplication);
                _requestService.UpdateRequestProcessStatus(requestViewModel.RequestId, ProcessStatusEnum.申请审核中);
            }
            _purchaseApplicationRepository.Save();
        }
        private void GenerateInStockWorkSheet(InStockHeaderViewModel inStockHeaderViewModel, ExcelWorksheet template)
        {
            if (inStockHeaderViewModel == null)
            {
                return;
            }
            decimal subTotal = 0;
            var     row      = 8;

            template.Name = inStockHeaderViewModel.InStockNumber;
            template.InsertRow(row, inStockHeaderViewModel.InStockViewModels.Count);

            //Header
            var requestHeader = _requestHeaderRepository.FindBy(x => x.RequestHeaderNumber == inStockHeaderViewModel.RequestNumber).Include(c => c.Contract).FirstOrDefault();

            template.Cells["B3"].Value = DateTime.Now.ToString("yyyy-MM-dd");    //出单日期
            template.Cells["D3"].Value = inStockHeaderViewModel.ApplicationDept; //制表科室
            template.Cells["F3"].Value = inStockHeaderViewModel.CreatePerson;    //制表人
            template.Cells["H3"].Value = inStockHeaderViewModel.AuditDepart;     //审核部门
            template.Cells["J3"].Value = inStockHeaderViewModel.AuditDepart;     //审核人
            template.Cells["B4"].Value = inStockHeaderViewModel.InStockNumber;   //入库单号
            template.Cells["D4"].Value = inStockHeaderViewModel.CreatePerson;    //仓库名称
            template.Cells["H4"].Value = inStockHeaderViewModel.PurchaseNumber;  //采购单号
            if (requestHeader != null)
            {
                template.Cells["F4"].Value = requestHeader.Contract.Address;           //合同地址
                template.Cells["J4"].Value = requestHeader.Contract.ContractNumber;    //合同编号
                template.Cells["B5"].Value = requestHeader.RequestCategory.ToString(); //入库类型
            }

            //Body
            foreach (var m in inStockHeaderViewModel.InStockViewModels)
            {
                var serialNo      = "A" + row;
                var name          = "B" + row;
                var code          = "C" + row;
                var model         = "D" + row;
                var specification = "E" + row;
                var dimension     = "F" + row;
                var position      = "G" + row; //库位
                var total         = "H" + row;
                var unit          = "I" + row;
                var currentPrice  = "J" + row;
                var totalPrice    = "K" + row;
                var note          = "L" + row;

                template.Cells[serialNo].Value      = serialNo;
                template.Cells[name].Value          = m.Name;
                template.Cells[code].Value          = m.Code;
                template.Cells[model].Value         = m.Model;
                template.Cells[specification].Value = m.Specification;
                template.Cells[dimension].Value     = m.Dimension;
                template.Cells[position].Value      = m.PositionName;
                template.Cells[total].Value         = m.Total;
                template.Cells[unit].Value          = m.Unit;
                template.Cells[currentPrice].Value  = $"{m.Price:C}";
                template.Cells[totalPrice].Value    = $"{m.Price * m.Total:C}";
                template.Cells[note].Value          = m.Note;

                template.Row(row).Height = 35;
                template.Cells[serialNo + ":" + note].Style.Font.Size           = 12;
                template.Cells[serialNo + ":" + note].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                template.Cells[serialNo + ":" + note].Style.VerticalAlignment   = ExcelVerticalAlignment.Center;
                template.Cells[serialNo + ":" + note].Style.Border.Top.Style    = ExcelBorderStyle.Thin;
                template.Cells[serialNo + ":" + note].Style.Border.Left.Style   = ExcelBorderStyle.Thin;
                template.Cells[serialNo + ":" + note].Style.Border.Right.Style  = ExcelBorderStyle.Thin;
                template.Cells[serialNo + ":" + note].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;


                subTotal = subTotal + (decimal)m.Price * m.Total;
                row++;
            }

            template.Cells["K" + row++].Value = $"{subTotal:C}";                 //总计

            template.Cells["K" + row++].Value = $"{subTotal * (decimal)0.13:C}"; //HST
            template.Cells["K" + row].Value   = $"{subTotal * (decimal)1.13:C}"; //总合计
        }