using System;
using System.ComponentModel;
using System.Linq;
using CK.SCP.Models;
using CK.SCP.Models.Enums;
using CK.SCP.Models.UniApiEntity;
using CK.SCP.GrupUniApi.Controller;
using System.Transactions;
using System.Data.Entity.Core;
using CK.SCP.UniApi.Controller;

namespace CK.SCP.GrupUniApi
{
    public class QadOdbcApi : IApi
    {
        private readonly BackgroundWorker _bgwGet = new BackgroundWorker();
        private readonly BackgroundWorker _bgwPut = new BackgroundWorker();
        private readonly BackgroundWorker _bgwMail = new BackgroundWorker();
        public QadOdbcApi()
        {
            InitBgw();
        }

        private void InitBgw()
        {
            _bgwGet.DoWork += BgwGetDoWork;
            _bgwGet.RunWorkerCompleted += BgwGetRunWorkerCompleted;
            //回传给QAD    
            _bgwPut.DoWork += BgwPutDoWork;
            _bgwPut.RunWorkerCompleted += BgwPutRunWorkerCompleted;
            //发邮件
            _bgwMail.DoWork += BgwMailDoWork;
            _bgwMail.RunWorkerCompleted += BgwMailRunWorkerCompleted;
        }

        private void BgwPutRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Console.WriteLine(@"Odbc接口数据发送完成<<<<<<<<<<");
        }
        private void BgwMailRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Console.WriteLine(@"邮件发送完成<<<<<<<<<<");
        }
        private void BgwPutDoWork(object sender, DoWorkEventArgs e)
        {
            var idb = EntitiesFactory.CreateUniApiInstance();
            var wdb = EntitiesFactory.CreateScpInstance();
          
            try
            {
                var apiList = UniApiController.GetNewInterfaceList(wdb);
                OdbcApiScpController.PutAll(wdb, idb, apiList);
                apiList = apiList.Where(p => p.State == (int)FormState.关闭).ToList();
                var apiHisList = apiList.Select(uniApi => uniApi.ToHis()).ToList();
                using (var scope =
                    new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
                    {
                        IsolationLevel = System.Transactions.IsolationLevel.Serializable,
                        Timeout = new TimeSpan(0, 20, 0)
                    }))
                {

                    var list=idb.xxscm_inv_det.ToList();

                    UniApiController.AddHisList(wdb, apiHisList);
                    UniApiController.RemoveList(wdb, apiList);
                    EntitiesFactory.SaveDb(idb);
                    EntitiesFactory.SaveDb(wdb);

                    scope.Complete();
                }
            }


            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
            {

                Console.WriteLine(dbEx);

            }
            catch (OptimisticConcurrencyException ex)//并发冲突异常
            {


                Console.WriteLine(ex);

            }


            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        /// <summary>
        /// 邮件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BgwMailDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                MailController.GetNewCtrlList();
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("错误:{0}", ex.InnerException.Message));
            }
        }

        private void BgwGetRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Console.WriteLine(@"Odbc接口数据接收完成<<<<<<<<<<");
        }

        private void BgwGetDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                OdbcApiQadController.GetNewCtrlList(); //取QAD数据
                OdbcApiWmsController.GetNewCtrlList();  //取WMS数据
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("错误:{0}", ex.Message));
                //                MessageHelper.ShowError(ex);
            }
        }

        public void Get()
        {
            try
            {
                if (_bgwGet.IsBusy) return;
                Console.WriteLine(@"Odbc接口数据接收开始>>>>>>>>>>");
                _bgwGet.RunWorkerAsync();
            }
            catch (ScpException ex)
            {
                Console.WriteLine(ex);
            }
        }

        public void Put()
        {
            try
            {
                if (_bgwPut.IsBusy) return;
                Console.WriteLine(@"Odbc接口数据发送开始>>>>>>>>>>");
                _bgwPut.RunWorkerAsync();
            }
            catch (ScpException ex)
            {
                Console.WriteLine(ex);
            }
        }
        public void Mail()
        {
            try
            {
                if (_bgwMail.IsBusy) return;
                Console.WriteLine(@"发送邮件>>>>>>>>>>");
                _bgwMail.RunWorkerAsync();
            }
            catch (ScpException ex)
            {
                Console.WriteLine(ex);
            }
        }
    }

}