xuyi
9/25/2019 - 12:27 AM

Page.Import

<commonData:SecurityPage
           xmlns:commonData="clr-namespace:HD.SL.Common.Data.CommonData;assembly=HD.SL.Common.Data"
           xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
           x:Class="HD.SL.RSM.Config.MetaData.CustomerImport" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="CustomerImport Page">
    <telerik:RadBusyIndicator x:Name="busyIndicator">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <telerik:RadGridView Grid.Row="0"  
                                 x:Name="gridViewCustomer" 
                                 Height="Auto"
                                 AutoGenerateColumns="False" 
                                 EnableLostFocusSelectedState="False" 
                                 ShowGroupPanel="False"
                                 IsSynchronizedWithCurrentItem="False"
                                 CanUserSelect="True" 
                                 SelectionMode="Single" >
                <telerik:RadGridView.Columns>
                    <telerik:GridViewToggleRowDetailsColumn Header="" ExpandMode="Multiple" />
                </telerik:RadGridView.Columns>
                <telerik:RadGridView.RowDetailsTemplate>
                    <DataTemplate>
                        <telerik:RadGridView Height="Auto" 
                                             AutoGenerateColumns="False" 
                                             EnableLostFocusSelectedState="False" 
                                             ShowGroupPanel="False"
                                             IsSynchronizedWithCurrentItem="False" 
                                             CanUserSelect="True"
                                             SelectionMode="Single">
                            <telerik:RadGridView.Columns>
                                <telerik:GridViewDataColumn Header="联系人编号" DataMemberBinding="{Binding CONTACT_ID}" Width="100" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="客户编号" DataMemberBinding="{Binding CUSTOMER_ID}" Width="100" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="性别" DataMemberBinding="{Binding CONTACT_GENDER}" Width="40" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="名称" DataMemberBinding="{Binding CONTACT_PERSON}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="手机" DataMemberBinding="{Binding CONTACT_MOBILE}" Width="100" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="角色" DataMemberBinding="{Binding CONTACT_ROLE}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="职务" DataMemberBinding="{Binding CONTACT_DUTY}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="个人喜好" DataMemberBinding="{Binding EF_CHAR1}" Width="120" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="联系人生日" DataMemberBinding="{Binding CONTACT_BIRTHDAY}" Width="100" IsReadOnly="True" ></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="阴阳历" DataMemberBinding="{Binding EF_CHAR2}" Width="60" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="邮箱" DataMemberBinding="{Binding CONTACT_EMAIL}" Width="120" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="电话" DataMemberBinding="{Binding CONTACT_TEL}" Width="100" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="传真" DataMemberBinding="{Binding CONTACT_FAX}" Width="100" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="微信号" DataMemberBinding="{Binding WEXIN_CODE}" Width="100" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="QQ号" DataMemberBinding="{Binding QQ_CODE}" Width="100" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="省份编号" DataMemberBinding="{Binding PROVINCE_ID}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="省份名称" DataMemberBinding="{Binding PROVINCE_NAME}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="城市编号" DataMemberBinding="{Binding CITY_ID}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="城市名称" DataMemberBinding="{Binding CITY_NAME}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="区县编码" DataMemberBinding="{Binding DISTRICT_ID}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="区县名称" DataMemberBinding="{Binding DISTRICT_NAME}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="详细地址" DataMemberBinding="{Binding DETAIL_ADDRESS}" Width="120" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="默认标记" DataMemberBinding="{Binding DEFAULT_FLAG}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                                <telerik:GridViewDataColumn Header="备注" DataMemberBinding="{Binding REMARK}" Width="120" IsReadOnly="True"></telerik:GridViewDataColumn>

                            </telerik:RadGridView.Columns>
                        </telerik:RadGridView>
                    </DataTemplate>
                </telerik:RadGridView.RowDetailsTemplate>
            </telerik:RadGridView>

            <StackPanel Grid.Row="1"  Orientation="Horizontal" HorizontalAlignment="Left">
                <telerik:RadButton x:Name="btnDownload" Content="下载模板" Style="{StaticResource commonButton}"  Click="btnDownLoad_Click" ToolTipService.ToolTip="下载模板"></telerik:RadButton>
                <telerik:RadButton x:Name="btnLoad" Content="加载文档"  Style="{StaticResource commonButton}"  Click="btnLoad_Click" ToolTipService.ToolTip="加载Excel文档"></telerik:RadButton>
                <telerik:RadButton x:Name="btnImport" Content="导入数据"  Style="{StaticResource commonButton}" Click="btnImport_Click" ToolTipService.ToolTip="导入工艺数据" ></telerik:RadButton>
            </StackPanel>
            
        </Grid>
    </telerik:RadBusyIndicator>
</commonData:SecurityPage>
    
    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows;
using HD.SL.Common.Data.CommonData;
using Telerik.Windows.Controls;
using HD.SL.Common.Controls;
using HD.SL.Common.Data.Operation;
using HD.SL.Common.Data.UI.Validation;
using HD.SL.Common.Asset.ResouceLists;
using System.Windows.Controls;
using System.ComponentModel;
using Infragistics.Documents.Excel;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using HD.SL.RSM.DataAccess.DataModels;
using HD.SVR.RSM.DataAccess.DataModels;
using System.Windows.Browser;

namespace HD.SL.RSM.Config.MetaData
{
    public partial class CustomerImport : SecurityPage
    {
        protected static CustomerImport currentObject;
        public static CustomerImport GetInstance()
        {
            if (currentObject == null)
            {
                currentObject = new CustomerImport();
                currentObject.OnDispose += (sender, e) =>
                {
                    currentObject = null;
                };
            }
            currentObject.InitialPage();
            return currentObject;
        }

        private HD.SVR.RSM.DataAccess.DataServices.DBContext ctxRSM;
        private CustomerImport()
        {
            ctxRSM = new SVR.RSM.DataAccess.DataServices.DBContext();
            InitializeComponent();

            #region gridViewCustomer 初始化
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.CUSTOMER_ID, "", 120, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.CUSTOMER_NAME, "", 150, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.CUSTOMER_TYPE, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.CUSTOMER_LEVEL, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.AGENT_FLAG, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.SUPPLIER_FLAG, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.ORG_TYPE, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.SOURCE_TYPE, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.OWNER_ID, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.OWNER_NAME, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.PROVINCE_ID, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.PROVINCE_NAME, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.CITY_ID, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.CITY_NAME, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.DISTRICT_ID, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.DISTRICT_NAME, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.DETAIL_ADDRESS, "", 160, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.PARENT_CUSTOMER, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.USE_FLAG, "", 80, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.EF_CHAR1, "社会信用代码", 120, "", true, true);
            gridViewCustomer.CreateDataColumnWithBind<RsCustomer>(p => p.REMARK, "", 120, "", true, true);
            gridViewCustomer.RegisterCellCopy();
            #endregion

            this.gridViewCustomer.LoadingRowDetails += gridViewCustomer_LoadingRowDetails;
        }

        private void gridViewCustomer_LoadingRowDetails(object sender, Telerik.Windows.Controls.GridView.GridViewRowDetailsEventArgs e)
        {
            var customerItem = e.Row.DataContext as RsCustomer;
            var rowGridView = e.DetailsElement as RadGridView;
            rowGridView.ItemsSource = customerItem.ContactItems;
        }

        private ObservableCollection<RsCustomer> customerItems;
        private ObservableCollection<RsContact> contactItems;
        private UploaderWrapper uploaderWrapper;
        private Stream currentStream;
        private void InitialPage()
        {
            this.ctxRSM.EntityContainer.Clear();

            this.uploaderWrapper = new UploaderWrapper();
            customerItems = new ObservableCollection<RsCustomer>();
            contactItems = new ObservableCollection<RsContact>();

        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            customerItems.Clear();
            contactItems.Clear();

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            dialog.Filter = "Excel Files (*.xls, *.xlsx)| *.xls;*.xlsx";
            if (dialog.ShowDialog() != true) return;

            currentStream = dialog.File.OpenRead();

            BackgroundWorker bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.DoWork += new DoWorkEventHandler((obj, args) => ImportExcel(obj, args));
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            this.busyIndicator.IsBusy = true;
            if (!bw.IsBusy) bw.RunWorkerAsync();

        }

        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.busyIndicator.BusyContent = e.UserState.ToString();
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.busyIndicator.IsBusy = false;

            FinishCustomerData();
            FinishContactItemData();

            foreach (var customerItem in customerItems)
            {
                foreach (var contactItem in contactItems.Where(p => p.CUSTOMER_ID == customerItem.CUSTOMER_ID).OrderBy(p => p.CONTACT_PERSON))
                {
                    customerItem.ContactItems.Add(contactItem);
                }
            }

            this.gridViewCustomer.ItemsSource = customerItems;
        }

        private async void FinishCustomerData()
        {
            List<string> provinceNames = new List<string>();
            List<string> cityNames = new List<string>();
            List<string> districtNames = new List<string>();

            foreach (var item in customerItems)
            {
                if (!string.IsNullOrEmpty(item.PROVINCE_NAME) && !provinceNames.Contains(item.PROVINCE_NAME))
                {
                    provinceNames.Add(item.PROVINCE_NAME);
                }
                if (!string.IsNullOrEmpty(item.CITY_NAME) && !cityNames.Contains(item.CITY_NAME))
                {
                    cityNames.Add(item.CITY_NAME);
                }
                if (!string.IsNullOrEmpty(item.DISTRICT_NAME) && !districtNames.Contains(item.DISTRICT_NAME))
                {
                    districtNames.Add(item.DISTRICT_NAME);
                }
            }

            this.busyIndicator.BusyContent = "正在加载数据库中省市县编号";
            this.busyIndicator.IsBusy = true;
            var getDicProvince = await ctxRSM.GetRsProvinceIdsByNames(provinceNames).AsTask();
            var getDicCity = await ctxRSM.GetRsCityIdsByNames(cityNames).AsTask();
            var getDicDistrict = await ctxRSM.GetRsDistrictIdsByNames(districtNames).AsTask();
            this.busyIndicator.IsBusy = false;

            Dictionary<string, string> dicProvince = new Dictionary<string, string>();
            Dictionary<string, string> dicCity = new Dictionary<string, string>();
            Dictionary<string, string> dicDistrict = new Dictionary<string, string>();

            dicProvince = getDicProvince.Value as Dictionary<string, string>;
            dicCity = getDicCity.Value as Dictionary<string, string>;
            dicDistrict = getDicDistrict.Value as Dictionary<string, string>;

            foreach (var customerItem in customerItems)
            {
                if (!string.IsNullOrEmpty(customerItem.PROVINCE_NAME) && dicProvince.ContainsKey(customerItem.PROVINCE_NAME))
                {
                    customerItem.PROVINCE_ID = dicProvince[customerItem.PROVINCE_NAME];
                }
                if (!string.IsNullOrEmpty(customerItem.CITY_NAME) && dicCity.ContainsKey(customerItem.CITY_NAME))
                {
                    customerItem.CITY_ID = dicCity[customerItem.CITY_NAME];
                }
                if (!string.IsNullOrEmpty(customerItem.DISTRICT_NAME) && dicDistrict.ContainsKey(customerItem.DISTRICT_NAME))
                {
                    customerItem.DISTRICT_ID = dicDistrict[customerItem.DISTRICT_NAME];
                }
            }

        }

        private async void FinishContactItemData()
        {
            List<string> provinceNames = new List<string>();
            List<string> cityNames = new List<string>();
            List<string> districtNames = new List<string>();

            foreach (var item in contactItems)
            {
                if (!string.IsNullOrEmpty(item.PROVINCE_NAME) && !provinceNames.Contains(item.PROVINCE_NAME))
                {
                    provinceNames.Add(item.PROVINCE_NAME);
                }
                if (!string.IsNullOrEmpty(item.CITY_NAME) && !cityNames.Contains(item.CITY_NAME))
                {
                    cityNames.Add(item.CITY_NAME);
                }
                if (!string.IsNullOrEmpty(item.DISTRICT_NAME) && !districtNames.Contains(item.DISTRICT_NAME))
                {
                    districtNames.Add(item.DISTRICT_NAME);
                }
            }

            this.busyIndicator.BusyContent = "正在加载数据库中省市县编号";
            this.busyIndicator.IsBusy = true;
            var getDicProvince = await ctxRSM.GetRsProvinceIdsByNames(provinceNames).AsTask();
            var getDicCity = await ctxRSM.GetRsCityIdsByNames(cityNames).AsTask();
            var getDicDistrict = await ctxRSM.GetRsDistrictIdsByNames(districtNames).AsTask();
            this.busyIndicator.IsBusy = false;

            Dictionary<string, string> dicProvince = new Dictionary<string, string>();
            Dictionary<string, string> dicCity = new Dictionary<string, string>();
            Dictionary<string, string> dicDistrict = new Dictionary<string, string>();

            dicProvince = getDicProvince.Value as Dictionary<string, string>;
            dicCity = getDicCity.Value as Dictionary<string, string>;
            dicDistrict = getDicDistrict.Value as Dictionary<string, string>;

            foreach (var contactItem in contactItems)
            {
                if (!string.IsNullOrEmpty(contactItem.PROVINCE_NAME) && dicProvince.ContainsKey(contactItem.PROVINCE_NAME))
                {
                    contactItem.PROVINCE_ID = dicProvince[contactItem.PROVINCE_NAME];
                }
                if (!string.IsNullOrEmpty(contactItem.CITY_NAME) && dicCity.ContainsKey(contactItem.CITY_NAME))
                {
                    contactItem.CITY_ID = dicCity[contactItem.CITY_NAME];
                }
                if (!string.IsNullOrEmpty(contactItem.DISTRICT_NAME) && dicDistrict.ContainsKey(contactItem.DISTRICT_NAME))
                {
                    contactItem.DISTRICT_ID = dicDistrict[contactItem.DISTRICT_NAME];
                }
            }

        }

        public void ImportExcel(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = (sender as BackgroundWorker);
            Workbook book = Workbook.Load(currentStream);

            //客户信息
            int stopFlag = 0;
            int rowIndex = 1;
            Dictionary<string, string> dicCustomerIds = new System.Collections.Generic.Dictionary<string, string>();
            dicCustomerIds.Clear();

            if (book.Worksheets.Count < 2) return;

            bw.ReportProgress(1, string.Format("开始导入客户信息"));

            while (stopFlag < 2)
            {
                rowIndex = rowIndex + 1;
                if (book.Worksheets[0].GetCell("A" + rowIndex.ToString()).Value == null)
                {
                    stopFlag = stopFlag + 1;
                    continue;
                }
                else
                {
                    stopFlag = 0;

                    string customerId = (book.Worksheets[0].GetCell("A" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[0].GetCell("A" + rowIndex.ToString()).Value.ToString().Trim();

                    string customerName = (book.Worksheets[0].GetCell("B" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[0].GetCell("B" + rowIndex.ToString()).Value.ToString().Trim();

                    string customerType = (book.Worksheets[0].GetCell("C" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[0].GetCell("C" + rowIndex.ToString()).Value.ToString().Trim();

                    string customerLevel = (book.Worksheets[0].GetCell("D" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[0].GetCell("D" + rowIndex.ToString()).Value.ToString().Trim();

                    string agentFlag = (book.Worksheets[0].GetCell("E" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[0].GetCell("E" + rowIndex.ToString()).Value.ToString().Trim();

                    string supplierFlag = (book.Worksheets[0].GetCell("F" + rowIndex.ToString()).Value == null) ? null :
                       book.Worksheets[0].GetCell("F" + rowIndex.ToString()).Value.ToString().Trim();

                    string orgType = (book.Worksheets[0].GetCell("G" + rowIndex.ToString()).Value == null) ? null :
                      book.Worksheets[0].GetCell("G" + rowIndex.ToString()).Value.ToString().Trim();

                    string sourceType = (book.Worksheets[0].GetCell("H" + rowIndex.ToString()).Value == null) ? null :
                      book.Worksheets[0].GetCell("H" + rowIndex.ToString()).Value.ToString().Trim();

                    string ownerId = (book.Worksheets[0].GetCell("I" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("I" + rowIndex.ToString()).Value.ToString().Trim();

                    string provinceName = (book.Worksheets[0].GetCell("J" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("J" + rowIndex.ToString()).Value.ToString().Trim();

                    string cityName = (book.Worksheets[0].GetCell("K" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("K" + rowIndex.ToString()).Value.ToString().Trim();

                    string districtName = (book.Worksheets[0].GetCell("L" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("L" + rowIndex.ToString()).Value.ToString().Trim();

                    string detailAddress = (book.Worksheets[0].GetCell("M" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("M" + rowIndex.ToString()).Value.ToString().Trim();

                    string parentCustomer = (book.Worksheets[0].GetCell("N" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("N" + rowIndex.ToString()).Value.ToString().Trim();

                    string useFlag = (book.Worksheets[0].GetCell("O" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("O" + rowIndex.ToString()).Value.ToString().Trim();

                    string remark = (book.Worksheets[0].GetCell("P" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("P" + rowIndex.ToString()).Value.ToString().Trim();

                    //社会信用代码
                    string efChar1 = (book.Worksheets[0].GetCell("Q" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[0].GetCell("Q" + rowIndex.ToString()).Value.ToString().Trim();


                    if (string.IsNullOrEmpty(customerId)) continue;

                    RsCustomer customerItem = new RsCustomer();

                    customerItem.CUSTOMER_ID = GuidGenerator.GetShortGuid();
                    dicCustomerIds.Add(customerId, customerItem.CUSTOMER_ID);

                    customerItem.CUSTOMER_NAME = customerName;
                    customerItem.CUSTOMER_TYPE = customerType;
                    customerItem.CUSTOMER_LEVEL = customerLevel;
                    customerItem.AGENT_FLAG = string.IsNullOrWhiteSpace(agentFlag) ? "N" : agentFlag;
                    customerItem.SUPPLIER_FLAG = string.IsNullOrWhiteSpace(supplierFlag) ? "N" : supplierFlag;
                    customerItem.ORG_TYPE = orgType;
                    customerItem.SOURCE_TYPE = sourceType;
                    customerItem.OWNER_ID = ownerId;
                    customerItem.PROVINCE_NAME = provinceName;
                    customerItem.CITY_NAME = cityName;
                    customerItem.DISTRICT_NAME = districtName;
                    customerItem.DETAIL_ADDRESS = detailAddress;
                    customerItem.PARENT_CUSTOMER = parentCustomer;
                    customerItem.USE_FLAG = string.IsNullOrWhiteSpace(useFlag) ? "Y" : useFlag;
                    customerItem.EF_CHAR1 = efChar1;
                    customerItem.REMARK = remark;

                    this.customerItems.Add(customerItem);

                    bw.ReportProgress(1, string.Format("已导入{0}行客户信息", this.customerItems.Count.ToString()));

                }
            }

            //联系人信息
            stopFlag = 0;
            rowIndex = 1;

            bw.ReportProgress(1, string.Format("开始导入联系人信息"));

            while (stopFlag < 2)
            {
                rowIndex = rowIndex + 1;
                if (book.Worksheets[1].GetCell("A" + rowIndex.ToString()).Value == null)
                {
                    stopFlag = stopFlag + 1;
                    continue;
                }
                else
                {
                    stopFlag = 0;

                    RsContact contactItem = new RsContact();
                    contactItem.CONTACT_ID = GuidGenerator.GetShortGuid();

                    string customerId = (book.Worksheets[1].GetCell("A" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[1].GetCell("A" + rowIndex.ToString()).Value.ToString().Trim();
                    if (!dicCustomerIds.ContainsKey(customerId)) continue;
                    contactItem.CUSTOMER_ID = dicCustomerIds[customerId];

                    string contactGender = (book.Worksheets[1].GetCell("B" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[1].GetCell("B" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CONTACT_GENDER = contactGender;

                    string contactPerson = (book.Worksheets[1].GetCell("C" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[1].GetCell("C" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CONTACT_PERSON = contactPerson;

                    string contactMobile = (book.Worksheets[1].GetCell("D" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[1].GetCell("D" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CONTACT_MOBILE = contactMobile;

                    string contactRole = (book.Worksheets[1].GetCell("E" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[1].GetCell("E" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CONTACT_ROLE = contactRole;

                    string contactDuty = (book.Worksheets[1].GetCell("F" + rowIndex.ToString()).Value == null) ? null :
                       book.Worksheets[1].GetCell("F" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CONTACT_DUTY = contactDuty;

                    //个人喜好
                    string efChar1 = (book.Worksheets[1].GetCell("G" + rowIndex.ToString()).Value == null) ? null :
                      book.Worksheets[1].GetCell("G" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.EF_CHAR1 = efChar1;

                    string contactBirthday = (book.Worksheets[1].GetCell("H" + rowIndex.ToString()).Value == null) ? null :
                      book.Worksheets[1].GetCell("H" + rowIndex.ToString()).Value.ToString().Trim();

                    contactItem.CONTACT_BIRTHDAY = null;
                    if (!string.IsNullOrEmpty(contactBirthday))
                    {
                        contactBirthday = DateTime.FromOADate(Convert.ToInt32(contactBirthday)).ToString("d");
                        DateTime contactBirthdayValue;
                        DateTime.TryParse(contactBirthday, out contactBirthdayValue);
                        contactItem.CONTACT_BIRTHDAY = contactBirthdayValue;
                    }

                    //阴阳历
                    string efChar2 = (book.Worksheets[1].GetCell("I" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("I" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.EF_CHAR2 = efChar2;

                    string contactEmail = (book.Worksheets[1].GetCell("J" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("J" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CONTACT_EMAIL = contactEmail;

                    string contactTel = (book.Worksheets[1].GetCell("K" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("K" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CONTACT_TEL = contactTel;

                    string contactFax = (book.Worksheets[1].GetCell("L" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("L" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CONTACT_FAX = contactFax;

                    string wexinCode = (book.Worksheets[1].GetCell("M" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("M" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.WEXIN_CODE = wexinCode;

                    string qqCode = (book.Worksheets[1].GetCell("N" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("N" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.QQ_CODE = qqCode;

                    string provinceName = (book.Worksheets[1].GetCell("O" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("O" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.PROVINCE_NAME = provinceName;

                    string cityName = (book.Worksheets[1].GetCell("P" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("P" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.CITY_NAME = cityName;

                    string districtName = (book.Worksheets[1].GetCell("Q" + rowIndex.ToString()).Value == null) ? null :
                     book.Worksheets[1].GetCell("Q" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.DISTRICT_NAME = districtName;

                    string detailAddress = (book.Worksheets[1].GetCell("R" + rowIndex.ToString()).Value == null) ? null :
                        book.Worksheets[1].GetCell("R" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.DETAIL_ADDRESS = detailAddress;

                    string defaultFlag = (book.Worksheets[1].GetCell("S" + rowIndex.ToString()).Value == null) ? null :
                       book.Worksheets[1].GetCell("S" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.DEFAULT_FLAG = defaultFlag;

                    string remark = (book.Worksheets[1].GetCell("T" + rowIndex.ToString()).Value == null) ? null :
                      book.Worksheets[1].GetCell("T" + rowIndex.ToString()).Value.ToString().Trim();
                    contactItem.REMARK = remark;

                    contactItems.Add(contactItem);
                    bw.ReportProgress(1, string.Format("已导入{0}行联系人信息", this.contactItems.Count.ToString()));
                }
            }

            this.currentStream.Close();
        }

        private async void btnImport_Click(object sender, RoutedEventArgs e)
        {
            this.busyIndicator.BusyContent = "正在保存中...";
            this.busyIndicator.IsBusy = true;

            foreach (var customerItem in customerItems)
            {
                var m_RS_CUSTOMER = new RS_CUSTOMER();
                DataExchange.CopyDataItem(customerItem, m_RS_CUSTOMER);
                DataExchange.SetCreationLog(m_RS_CUSTOMER);
                this.ctxRSM.RS_CUSTOMERs.Add(m_RS_CUSTOMER);
            }

            foreach (var contactItem in contactItems)
            {
                var m_RS_CONTACT = new RS_CONTACT();
                DataExchange.CopyDataItem(contactItem, m_RS_CONTACT);
                DataExchange.SetCreationLog(m_RS_CONTACT);
                this.ctxRSM.RS_CONTACTs.Add(m_RS_CONTACT);
            }

            var operationSave = await ctxRSM.SubmitChanges().AsTask();
            this.busyIndicator.IsBusy = false;
            if (!this.CheckLoadDataArgs(ExceptionManager.CheckDataContextResult(operationSave)))
            {
                MessageBox.Show(operationSave.Error.Message.ToString());
            }

            RadWindowExtension.CloseRadWindow(this, true);

        }

        private async void btnDownLoad_Click(object sender, RoutedEventArgs e)
        {
            UploadFileItem m_UploadFileItem = new UploadFileItem();

            this.busyIndicator.IsBusy = true;
            var getOper = await HD.SL.SDM.DataAccess.Utilities.CacheEngine.GetCacheData<HD.SVR.SDM.DataAccess.DataModels.SYS_DICTIONARY>(
                CacheItem.SysDictionary.ToString(), p => p.ITEM_TYPE == "EXCEL_TEMPLATE" && p.ITEM_ID == "CUSTOMER_IMPORT");
            this.busyIndicator.IsBusy = false;

            m_UploadFileItem.FILE_ID = getOper.FirstOrDefault().ITEM_NAME.ToString();
            m_UploadFileItem.FILE_NAME = getOper.FirstOrDefault().ITEM_NAME.ToString();
            this.uploaderWrapper.DownLoadFile(m_UploadFileItem, "./Common/TemplateDownload.aspx");
        }

    }
}