⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deliverysignaturebase.cs

📁 微软的行业应用解决方案示例
💻 CS
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using HardwareDistributor.Properties;
using System.Windows.Forms;
using HardwareDistributor.Synchronization;
using HardwareDistributor.Business;
using System.Drawing.Imaging;

namespace HardwareDistributor.UI
{
    //allow design view in Visual studio by making it a regular class
    public abstract partial class DeliverySignatureBase : Form
    {
        #region Fields

        protected Guid _orderId;
        protected int _customerId;

        #endregion

        #region Constants

        protected const string SIGNATURE_IMAGE_FILE = "signature.gif";

        #endregion

        #region Constructors

        protected DeliverySignatureBase()
        {
            InitializeComponent();

        }

        protected DeliverySignatureBase(Guid orderId, int customerId)
        {
            InitializeComponent();

            this._orderId = orderId;
            this._customerId = customerId;
        }

        #endregion

        #region Abstract Methods

        protected abstract bool SignatureCaptured();

        protected abstract void CreateSignatureImageFile(string filePath, ImageFormat imageFormat);

        #endregion

        #region Event Handlers

        /// <summary>
        /// Go back to the last state in the delivery wizard
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemBack_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Completes order delivery
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemNext_Click(object sender, EventArgs e)
        {
            if (!SignatureCaptured())
            {
                MessageBox.Show("Must input a signature");
                return;
            }

            bool localDBUpdated = false;

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                Application.DoEvents();

                //Save image to DB
                DisplayStatus(Resources.SavingSignatureImage, 0);
                Application.DoEvents();
                SaveSignatureImage();
                Application.DoEvents();

                //update order to change the state of the order
                DisplayStatus(Resources.UpdateOrderMsg, 0);
                Application.DoEvents();
                UpdateOrder();
                Application.DoEvents();

                localDBUpdated = true;

                //check if we have network connectivity
                DisplayStatus(Resources.CheckNetworkMessage, 0);
                Application.DoEvents();
                bool networkAvailable = Business.Services.GetConnectionStatus();
                Application.DoEvents();

                //try to sync only if network is available
                if (networkAvailable)
                {
                    //Test to see if Replication URL is reachable
                    DisplayStatus(Resources.checkIisMessage, 0); ;
                    bool replicationAvailable = Business.Services.CheckSynchronizationUrl();
                    Application.DoEvents();

                    if (replicationAvailable)
                    {
                        //send changes down to server DB
                        DisplayStatus(Resources.SyncDbMessage, 0);
                        Application.DoEvents();
                        Synchronize();
                        Application.DoEvents();

                        //send notification to next role in workflow
                        DisplayStatus(Resources.SendingNotificationMessage, 0);
                        Application.DoEvents();
                        SendNotification();
                        Application.DoEvents();
                    }
                    else
                    {
                        //Replication URL isn't reachable
                        DisplayStatus(Resources.IisUnavailableMessage, 1000);
                    }
                }
                else
                {
                    DisplayStatus(Resources.NetworkUnavailableMessage, 1000);
                }

                //success !
                this.DialogResult = DialogResult.OK;

            }
            catch (Utilities.WrapperException wex)
            {
                Cursor.Current = Cursors.Default;

                //log and display error
                Business.Services.LogErrors(wex.InnerException);
                MessageBox.Show(wex.UserDisplayMessage, Resources.DeliveryError);
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;

                //log and display error
                Business.Services.LogErrors(ex);
                MessageBox.Show(ex.Message, Resources.SystemError);
            }
            finally
            {
                //reset the cursro
                Cursor.Current = Cursors.Default;
                ClearStatus();

                if (localDBUpdated)
                    this.Close();
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Display status in form. Wait for the specified timeout so that the user can see the message
        /// before returning
        /// </summary>
        /// <param name="statusText"></param>
        /// <param name="timeout"></param>
        private void DisplayStatus(string statusText, int timeout)
        {
            //Network isn't available
            StatusBarLabel.Visible = true;
            StatusBarLabel.Text = statusText;

            Application.DoEvents();
            System.Threading.Thread.Sleep(timeout);
            Application.DoEvents();

        }

        /// <summary>
        /// clear the status of the form.
        /// </summary>
        private void ClearStatus()
        {
            StatusBarLabel.Text = string.Empty;
            StatusBarLabel.Visible = false;
        }

        /// <summary>
        /// Synchronizes local db with server DB
        /// </summary>
        private void Synchronize()
        {
            try
            {
                Business.Services.SyncWithDataSource();
            }
            catch (Exception ex)
            {
                throw new Utilities.WrapperException(Resources.SyncDbError, ex);
            }

        }

        /// <summary>
        /// sends notification to the warehouse role that the order has been placed
        /// </summary>
        private void SendNotification()
        {
            try
            {
                Business.Services.SendNotification("Customer Service");
            }
            catch (Exception ex)
            {
                throw new Utilities.WrapperException(Resources.NotificationError, ex);
            }
        }

        /// <summary>
        /// Save signature image to DB
        /// </summary>
        private void SaveSignatureImage()
        {
            try
            {
                //create image file
                CreateSignatureImageFile(GlobalCache.Instance.AppPath + SIGNATURE_IMAGE_FILE, ImageFormat.Gif);

                //persist to database
				Business.Services.SaveGifToDatabase(GlobalCache.Instance.AppPath + SIGNATURE_IMAGE_FILE, _orderId);

                //delete image file from folder
                System.IO.File.Delete(GlobalCache.Instance.AppPath + SIGNATURE_IMAGE_FILE);
            }
            catch (Exception ex)
            {
                throw new Utilities.WrapperException(Resources.SavingImageError, ex);
            }
        }

        /// <summary>
        /// Update the state of the order to Delivered
        /// </summary>
        private void UpdateOrder()
        {
            try
            {
                //Update the Orders table to reflect final Order State of Delivered (5)
                Business.Order myOrder = Business.Services.GetOrder(_orderId);
                myOrder.OrderState = Business.OrderState.Delivered;
                Business.Services.SaveOrder(myOrder);
            }
            catch(Exception ex)
            {
                throw new Utilities.WrapperException(Resources.UpdateOrderError, ex);
            }
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -