public MultiFileUpdateEntity()
 {
     documentManagerContext = new Web.DocumentManagerDomainContext();
     TaxPayerDocumentEntity = new TaxPayerDocumentEntity();
     TaxPayerDocumentEntity.TaxPayerDocument = new Web.Model.taxpayerdocument();
     UserFile = new UserFile();
     Status   = "就绪";
     MultiFileUpdateStatus   = Entities.MultiFileUpdateStatus.PREPARE;
     UserFile.FinishUpdates += UserFile_FinishUpdate;
     OnMove = new DelegateCommand(onMove);
 }
 public MultiFileUpdateEntity()
 {
     documentManagerContext = new Web.DocumentManagerDomainContext();
     TaxPayerDocumentEntity = new TaxPayerDocumentEntity();
     TaxPayerDocumentEntity.TaxPayerDocument = new Web.Model.taxpayerdocument();
     UserFile = new UserFile();
     Status = "就绪";
     MultiFileUpdateStatus = Entities.MultiFileUpdateStatus.PREPARE;
     UserFile.FinishUpdates += UserFile_FinishUpdate;
     OnMove = new DelegateCommand(onMove);
 }
        private void UserFile_FinishUpdate(object sender, EventArgs e)
        {
            Status = "完成上传,正在存储";
            App app = Application.Current as App;

            TaxPayerDocumentEntity.TaxPayerUpdateUserId = app.MainPageViewModel.User.UserID;
            TaxPayerDocumentEntity.TaxPayerUpdateTime   = DateTime.Now;
            TaxPayerDocumentEntity.DUpdate();
            documentManagerContext.taxpayerdocuments.Add(TaxPayerDocumentEntity.TaxPayerDocument);
            Log.AddLog(documentManagerContext, TaxPayerDocumentEntity.ToString());
            SubmitOperation lSubmitOperation = documentManagerContext.SubmitChanges();

            lSubmitOperation.Completed += SubOperation_Completed;
        }
        private void LoadTaxPayerDocument_Complete(LoadOperation<Web.Model.taxpayerdocument> sender)
        {
            taxPayerDocumentSource.Source = sender.Entities;
            foreach (DocumentManager.Web.Model.taxpayerdocument taxpayerdocument in sender.Entities)
            {
                TaxPayerDocumentEntity taxPayerDocumentEntity = new TaxPayerDocumentEntity();
                taxPayerDocumentEntity.TaxPayerDocument = taxpayerdocument;
                taxPayerDocumentEntity.Update();

                if (taxPayerDocumentEntity.TaxPayerDocumentTypeId == 2) // 拨款报告
                {
                    continue;
                }

                if (taxPayerDocumentEntity.TaxPayerDocumentTypeId == 7) // 税票及发票
                {
                    continue;
                }

                if (taxPayerDocumentEntity.TaxPayerDocumentTypeId == 5) // 合同
                {
                    continue;
                }

                if (taxPayerDocumentEntity.TaxPayerDocumentTypeId == 6) // 代开申请
                {
                    continue;
                }

                if (taxPayerDocumentEntity.TaxPayerDocumentTypeId == 3) // 申报表
                {
                    continue;
                }

                App lApp = Application.Current as App;

                TaxPayerDocumentEntity lTaxPayerDocumentEntity = new TaxPayerDocumentEntity();
                lTaxPayerDocumentEntity.TaxPayerDocument = new Web.Model.taxpayerdocument();

                lTaxPayerDocumentEntity.TaxPayerId = addTaxPayerEntity.TaxPayerId;
                lTaxPayerDocumentEntity.TaxPayerDocumentName = taxPayerDocumentEntity.TaxPayerDocumentName;
                lTaxPayerDocumentEntity.TaxPayerDocumentTypeId = taxPayerDocumentEntity.TaxPayerDocumentTypeId;
                lTaxPayerDocumentEntity.TaxPayerDocumentDescript = taxPayerDocumentEntity.TaxPayerDocumentDescript;
                lTaxPayerDocumentEntity.TaxPayerUpdateUserId = lApp.MainPageViewModel.User.UserID;
                lTaxPayerDocumentEntity.TaxPayerUpdateTime = taxPayerDocumentEntity.TaxPayerUpdateTime;
                lTaxPayerDocumentEntity.TaxPayerDocumentBytes = taxPayerDocumentEntity.TaxPayerDocumentBytes;
                lTaxPayerDocumentEntity.TaxPayerIsLink = true;
                if (taxPayerDocumentEntity.TaxPayerIsLink.HasValue && taxPayerDocumentEntity.TaxPayerIsLink.Value)
                {
                    lTaxPayerDocumentEntity.TaxPayerLinkId = taxPayerDocumentEntity.TaxPayerLinkId;
                }
                else
                {
                    lTaxPayerDocumentEntity.TaxPayerLinkId = SelectTaxPayerEntity.TaxPayerId;
                }

                lTaxPayerDocumentEntity.TaxPayerDocument = new Web.Model.taxpayerdocument();
                lTaxPayerDocumentEntity.DUpdate();
                documentManagerContext.taxpayerdocuments.Add(lTaxPayerDocumentEntity.TaxPayerDocument);
            }
            SubmitOperation lSubmitOperation = documentManagerContext.SubmitChanges();
            lSubmitOperation.Completed += SubOperation_Completed;
        }
        private void onOpenStandBookFile()
        {
            OpenFileDialog oFile = new OpenFileDialog();
            // .xls filter specified to select only .xls file.
            oFile.Filter = "Excel (*.xls)|*.xls";

            if (oFile.ShowDialog() == true)
            {
                HasShowTemp = Visibility.Collapsed;
                try
                {
                    FileStream fs = oFile.File.OpenRead();
                    Workbook book = Workbook.Open(fs);
                    bool lIsSuccessRead = ReadSheet(book.Worksheets[0]);
                    if (lIsSuccessRead)
                    {
                        fs.Close();
                        HasShowTemp = Visibility.Visible;
                        CanInput = CheckVaild();
                        if (CanInput)
                        {
                            String fileName = oFile.File.Name;
                            UserFile = new UserFile();
                            DateTime lDateTime = DateTime.Now;
                            UserFile.FileName = lDateTime.ToString("yyyyMMdd_HHmmss_") + fileName;
                            UserFile.FileFolder = currentTaxPayerEntity.TaxPayerId.ToString();
                            UserFile.FileStream = oFile.File.OpenRead();

                            taxPayerDocumentEntity = new TaxPayerDocumentEntity();
                            taxPayerDocumentEntity.TaxPayerDocumentBytes = UserFile.FileStream.Length;
                        }

                        UpdateChanged("StandBookEntity");
                    }
                }
                catch (Exception ex)
                {
                    NotifyWindow lNotifyWindow = new NotifyWindow("打开文件出错", "请检查文件是否被其他程序打开或者文件中是否有自定义数据类型,请与模板比对!");
                    lNotifyWindow.Show();
                }
                UpdateChanged("HasShowTemp");
            }
        }
        private void LoadOperationTaxPayerDocumentCompleted(LoadOperation<DocumentManager.Web.Model.taxpayerdocument> aLoadOperation)
        {
            TaxPayerDocumentEntityList.Clear();
            foreach (DocumentManager.Web.Model.taxpayerdocument taxpayerdocument in aLoadOperation.Entities)
            {
                TaxPayerDocumentEntity taxPayerDocumentEntity = new TaxPayerDocumentEntity();
                taxPayerDocumentEntity.TaxPayerDocument = taxpayerdocument;
                taxPayerDocumentEntity.Update();

                FileTypeEntity lFileTypeEntity;
                if (FileTypeEntityDictionary != null
                    && taxPayerDocumentEntity.TaxPayerDocumentTypeId.HasValue
                    && FileTypeEntityDictionary.TryGetValue(taxPayerDocumentEntity.TaxPayerDocumentTypeId.Value, out lFileTypeEntity))
                {
                    taxPayerDocumentEntity.FileTypeName = lFileTypeEntity.FileTypeName;
                }

                TaxPayerDocumentEntityList.Add(taxPayerDocumentEntity);
            }
            UpdateChanged("TaxPayerDocumentEntityList");
            IsBusy = false;
        }
        private void LoadTaxPayerDocument_Complete(LoadOperation<Web.Model.taxpayerdocument> sender)
        {
            TaxPayerDocumentList.Clear();
            taxPayerDocumentSource.Source = sender.Entities;
            foreach (DocumentManager.Web.Model.taxpayerdocument taxpayerdocument in sender.Entities)
            {
                TaxPayerDocumentEntity taxPayerDocumentEntity = new TaxPayerDocumentEntity();
                taxPayerDocumentEntity.TaxPayerDocument = taxpayerdocument;
                taxPayerDocumentEntity.Update();

                taxPayerDocumentEntity.TaxPayerName = taxPayerEntity.TaxPayerName;
                FileTypeEntity lFileTypeEntity;
                if(FileTypeEntityDictionary != null
                    && taxPayerDocumentEntity.TaxPayerDocumentTypeId.HasValue
                    && FileTypeEntityDictionary.TryGetValue(taxPayerDocumentEntity.TaxPayerDocumentTypeId.Value, out lFileTypeEntity))
                {
                    taxPayerDocumentEntity.FileTypeName = lFileTypeEntity.FileTypeName;
                }

                UserEntity lUserEntity;
                if (UserEntityDictionary != null
                    && taxPayerDocumentEntity.TaxPayerUpdateUserId.HasValue
                    && UserEntityDictionary.TryGetValue(taxPayerDocumentEntity.TaxPayerUpdateUserId.Value, out lUserEntity))
                {
                    taxPayerDocumentEntity.UpdateUserName = lUserEntity.UserName;
                }

                TaxPayerDocumentList.Add(taxPayerDocumentEntity);
            }
            UpdateChanged("TaxPayerDocumentList");
            FinishLoadings(null, null);
        }
        private void linkFileWindow_Closed(object sender, EventArgs e)
        {
            LinkFileWindow linkFileWindow = sender as LinkFileWindow;
            if(linkFileWindow.DialogResult == true)
            {
                LinkFileViewModel lLinkFileViewModel = linkFileWindow.DataContext as LinkFileViewModel;
                int rTaxPayerId = lLinkFileViewModel.SelectTaxPayerEntity.TaxPayerId;
                foreach (TaxPayerEntity taxPayerEntity in lLinkFileViewModel.TaxPayerEntityLinkList)
                {
                    if (rTaxPayerId == taxPayerEntity.TaxPayerId)
                    {
                        continue;
                    }

                    foreach (TaxPayerDocumentEntity taxPayerDocumentEntity in linkFileWindow.FileListDataGrid.SelectedItems)
                    {
                        TaxPayerDocumentEntity lTaxPayerDocumentEntity = new TaxPayerDocumentEntity();

                        App lApp = Application.Current as App;

                        lTaxPayerDocumentEntity.TaxPayerId = taxPayerEntity.TaxPayerId;
                        lTaxPayerDocumentEntity.TaxPayerDocumentName = taxPayerDocumentEntity.TaxPayerDocumentName;
                        lTaxPayerDocumentEntity.TaxPayerDocumentTypeId = taxPayerDocumentEntity.TaxPayerDocumentTypeId;
                        lTaxPayerDocumentEntity.TaxPayerDocumentDescript = taxPayerDocumentEntity.TaxPayerDocumentDescript;
                        lTaxPayerDocumentEntity.TaxPayerUpdateUserId = lApp.MainPageViewModel.User.UserID;
                        lTaxPayerDocumentEntity.TaxPayerUpdateTime = taxPayerDocumentEntity.TaxPayerUpdateTime;
                        lTaxPayerDocumentEntity.TaxPayerDocumentBytes = taxPayerDocumentEntity.TaxPayerDocumentBytes;
                        lTaxPayerDocumentEntity.TaxPayerIsLink = true;
                        lTaxPayerDocumentEntity.TaxPayerLinkId = rTaxPayerId;
                        lTaxPayerDocumentEntity.TaxPayerDocument = new Web.Model.taxpayerdocument();
                        lTaxPayerDocumentEntity.DUpdate();
                        documentManagerContext.taxpayerdocuments.Add(lTaxPayerDocumentEntity.TaxPayerDocument);
                        Log.AddLog(documentManagerContext, lTaxPayerDocumentEntity.ToString());
                    }
                }

                SubmitOperation lSubmitOperation = documentManagerContext.SubmitChanges();
                lSubmitOperation.Completed += SubOperation_Completed;

            }
        }
        private void onOpenFile()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == true)
            {
                String fileName = ofd.File.Name;

                if (fileName.Contains("\"")
                    || fileName.Contains("#")
                    || fileName.Contains("%")
                    || fileName.Contains("&")
                    || fileName.Contains("\'")
                    || fileName.Contains("~")
                    || fileName.Contains("|")
                    || fileName.Contains(">")
                    || fileName.Contains("<")
                    || fileName.Contains("[")
                    || fileName.Contains("]")
                    || fileName.Contains("^")
                    || fileName.Contains("{")
                    || fileName.Contains("}"))
                {
                    NotifyWindow notificationWindow = new NotifyWindow("错误", "文件名包含 \"#%&\'~|><[]^{} 等非法字符!");
                    notificationWindow.Show();
                    return;
                }

                FileName = fileName;
                UserFile = new UserFile();
                UserFile.FileName = fileName;
                UserFile.FileFolder = taxPayerEntity.TaxPayerId.ToString();
                UserFile.FileStream = ofd.File.OpenRead();
                taxPayerDocumentEntity = new TaxPayerDocumentEntity();
                taxPayerDocumentEntity.TaxPayerDocumentBytes = UserFile.FileStream.Length;
                //ProjectFilesEntity.FileBytes = UserFile.FileStream.Length;
                (OnUpdate as DelegateCommand).RaiseCanExecuteChanged();
                UpdateChanged("UserFile");
            }
        }
        private void onCreatePDFFile()
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                NotifyWindow notifyWindow = new NotifyWindow("错误", "请先输入生成PDF文件名称!");
                notifyWindow.Show();
                return;
            }

            if (ImagesList.Count == 0)
            {
                NotifyWindow notifyWindow = new NotifyWindow("错误", "请先添加文件!");
                notifyWindow.Show();
                return;
            }

            pdfDocument doc = new pdfDocument(fileName, "", false);
            bool getIamge = false;
            foreach (AddImageEntity imageEntity in ImagesList)
            {
                try
                {
                    WriteableBitmap bitmap;
                    using (FileStream fs = new FileStream(imageEntity.FilePath, FileMode.Open))
                    {
                        BitmapImage bi = new BitmapImage();
                        bi.SetSource(fs);
                        bitmap = new WriteableBitmap(bi);
                    }

                    if (null != bitmap)
                    {
                        pdfPage page = doc.addPage((int)bitmap.PixelHeight, (int)bitmap.PixelWidth);
                        page.addImage(bitmap, 0, 0);
                        getIamge = true;
                        createPDFFileSuccess = true;
                    }
                    else
                    {
                        createPDFFileSuccess = false;
                    }
                }
                catch
                {
                    createPDFFileSuccess = false;

                    NotifyWindow notifyWindow = new NotifyWindow("错误", string.Format("打开图片文件{0}失败,请检查文件!!!", imageEntity.FilePath));
                    notifyWindow.Show();
                }

            }

            if (getIamge)
            {
                tempPDFFile = System.IO.Path.GetTempFileName();

                using (FileStream fs = new FileStream(tempPDFFile, FileMode.Create))
                {
                    doc.createPDF(fs);
                }

                UserFile = new UserFile();
                UserFile.FileName = fileName;
                UserFile.FileFolder = taxPayerEntity.TaxPayerId.ToString();
                UserFile.FileStream = (new FileInfo(tempPDFFile)).OpenRead();

                taxPayerDocumentEntity = new TaxPayerDocumentEntity();
                taxPayerDocumentEntity.TaxPayerDocumentBytes = UserFile.FileStream.Length;

                (OnUpdate as DelegateCommand).RaiseCanExecuteChanged();

                UpdateChanged("UserFile");
            }
        }