using CK.SCP.Models;
using CK.SCP.Models.ExchangeCenterTables;
using CK.SCP.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Migrations;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CK.SCP.Controller
{
    public class UN_EC_CONTROLLER_SUPPLIER
    {
        public static void Get_TES_SUPPLIER_List(TES_SUPPLIER p_entity, Action<ResultObject<IQueryable<TES_SUPPLIER>>> p_action)
        {
                ResultObject<IQueryable<TES_SUPPLIER>> _ret = new ResultObject<IQueryable<TES_SUPPLIER>>();
                try
            {
                using (var db = EntitiesFactory.CreateExchangeCenterInstance())
                {
                    IQueryable<TES_SUPPLIER> q = db.TES_SUPPLIER;
                    if (!string.IsNullOrEmpty(p_entity.SupplierCode))
                    {
                        q = q.Where(p => p.SupplierCode.Contains(p_entity.SupplierCode));
                    }
                    if (!string.IsNullOrEmpty(p_entity.SupplierName))
                    {
                        q = q.Where(p => p.SupplierName.Contains(p_entity.SupplierName));
                    }
                    if (!string.IsNullOrEmpty(p_entity.SearchName))
                    {
                        q = q.Where(p => p.SearchName.Contains(p_entity.SearchName));
                    }
                    if (!string.IsNullOrEmpty(p_entity.SecondName))
                    {
                        q = q.Where(p => p.SecondName.Contains(p_entity.SecondName));
                    }
                    if (!string.IsNullOrEmpty(p_entity.ThirdName))
                    {
                        q = q.Where(p => p.ThirdName.Contains(p_entity.ThirdName));
                    }
                    if (!string.IsNullOrEmpty(p_entity.GroupName))
                    {
                        q = q.Where(p => p.GroupName.Contains(p_entity.GroupName));
                    }
                    if (p_entity.Active != false)
                    {
                        q = q.Where(p => p.Active == p_entity.Active);
                    }
                    if (!string.IsNullOrEmpty(p_entity.Address1))
                    {
                        q = q.Where(p => p.Address1.Contains(p_entity.Address1));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Address2))
                    {
                        q = q.Where(p => p.Address2.Contains(p_entity.Address2));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Address3))
                    {
                        q = q.Where(p => p.Address3.Contains(p_entity.Address3));
                    }
                    if (p_entity.ZipCode != 0)
                    {
                        q = q.Where(p => p.ZipCode == p_entity.ZipCode);
                    }
                    if (!string.IsNullOrEmpty(p_entity.City))
                    {
                        q = q.Where(p => p.City.Contains(p_entity.City));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Country))
                    {
                        q = q.Where(p => p.Country.Contains(p_entity.Country));
                    }
                    if (p_entity.Telephone != 0)
                    {
                        q = q.Where(p => p.Telephone == p_entity.Telephone);
                    }
                    if (!string.IsNullOrEmpty(p_entity.province))
                    {
                        q = q.Where(p => p.province.Contains(p_entity.province));
                    }
                    if (!string.IsNullOrEmpty(p_entity.County))
                    {
                        q = q.Where(p => p.County.Contains(p_entity.County));
                    }
                    if (p_entity.Fax != 0)
                    {
                        q = q.Where(p => p.Fax == p_entity.Fax);
                    }
                    if (!string.IsNullOrEmpty(p_entity.LanguageCode))
                    {
                        q = q.Where(p => p.LanguageCode.Contains(p_entity.LanguageCode));
                    }
                    if (p_entity.Email != 0)
                    {
                        q = q.Where(p => p.Email == p_entity.Email);
                    }
                    if (p_entity.Internet != 0)
                    {
                        q = q.Where(p => p.Internet == p_entity.Internet);
                    }
                    if (p_entity.AddressIsTaxable != false)
                    {
                        q = q.Where(p => p.AddressIsTaxable == p_entity.AddressIsTaxable);
                    }
                    if (p_entity.AddressIsTaxIncluded != false)
                    {
                        q = q.Where(p => p.AddressIsTaxIncluded == p_entity.AddressIsTaxIncluded);
                    }
                    if (!string.IsNullOrEmpty(p_entity.FederalTax))
                    {
                        q = q.Where(p => p.FederalTax.Contains(p_entity.FederalTax));
                    }
                    if (!string.IsNullOrEmpty(p_entity.StateTax))
                    {
                        q = q.Where(p => p.StateTax.Contains(p_entity.StateTax));
                    }
                    if (!string.IsNullOrEmpty(p_entity.MiscTax1))
                    {
                        q = q.Where(p => p.MiscTax1.Contains(p_entity.MiscTax1));
                    }
                    if (!string.IsNullOrEmpty(p_entity.MiscTax2))
                    {
                        q = q.Where(p => p.MiscTax2.Contains(p_entity.MiscTax2));
                    }
                    if (!string.IsNullOrEmpty(p_entity.MiscTax3))
                    {
                        q = q.Where(p => p.MiscTax3.Contains(p_entity.MiscTax3));
                    }
                    if (p_entity.TaxInCity != false)
                    {
                        q = q.Where(p => p.TaxInCity == p_entity.TaxInCity);
                    }
                    if (!string.IsNullOrEmpty(p_entity.TaxZone))
                    {
                        q = q.Where(p => p.TaxZone.Contains(p_entity.TaxZone));
                    }
                    if (p_entity.TaxClass != "0")
                    {
                        q = q.Where(p => p.TaxClass == p_entity.TaxClass);
                    }
                    if (!string.IsNullOrEmpty(p_entity.TaxUsage))
                    {
                        q = q.Where(p => p.TaxUsage.Contains(p_entity.TaxUsage));
                    }
                    if (p_entity.Compens != false)
                    {
                        q = q.Where(p => p.Compens == p_entity.Compens);
                    }
                    if (string.IsNullOrEmpty(p_entity.GlProfileInvoice))
                    {
                        q = q.Where(p => p.GlProfileInvoice == p_entity.GlProfileInvoice);
                    }
                    if (string.IsNullOrEmpty(p_entity.GlProfileCreditNote))
                    {
                        q = q.Where(p => p.GlProfileCreditNote == p_entity.GlProfileCreditNote);
                    }
                    if (string.IsNullOrEmpty(p_entity.GlProfilePrepayment))
                    {
                        q = q.Where(p => p.GlProfilePrepayment == p_entity.GlProfilePrepayment);
                    }
                    if (string.IsNullOrEmpty(p_entity.AccountGLProfile))
                    {
                        q = q.Where(p => p.AccountGLProfile == p_entity.AccountGLProfile);
                    }
                    if (!string.IsNullOrEmpty(p_entity.SubAccountProfile))
                    {
                        q = q.Where(p => p.SubAccountProfile.Contains(p_entity.SubAccountProfile));
                    }
                    if (!string.IsNullOrEmpty(p_entity.CreditAgencyReference))
                    {
                        q = q.Where(p => p.CreditAgencyReference.Contains(p_entity.CreditAgencyReference));
                    }
                    if (!string.IsNullOrEmpty(p_entity.CommerceNumber))
                    {
                        q = q.Where(p => p.CommerceNumber.Contains(p_entity.CommerceNumber));
                    }
                    if (!string.IsNullOrEmpty(p_entity.TidNotice))
                    {
                        q = q.Where(p => p.TidNotice.Contains(p_entity.TidNotice));
                    }
                    if (!string.IsNullOrEmpty(p_entity.ExternalCustomerNumber))
                    {
                        q = q.Where(p => p.ExternalCustomerNumber.Contains(p_entity.ExternalCustomerNumber));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Currency))
                    {
                        q = q.Where(p => p.Currency.Contains(p_entity.Currency));
                    }
                    if (!string.IsNullOrEmpty(p_entity.SupplierType))
                    {
                        q = q.Where(p => p.SupplierType.Contains(p_entity.SupplierType));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Purchasetype))
                    {
                        q = q.Where(p => p.Purchasetype.Contains(p_entity.Purchasetype));
                    }
                    if (!string.IsNullOrEmpty(p_entity.CreditTerms))
                    {
                        q = q.Where(p => p.CreditTerms.Contains(p_entity.CreditTerms));
                    }
                    if (!string.IsNullOrEmpty(p_entity.InvoiceStatusCode))
                    {
                        q = q.Where(p => p.InvoiceStatusCode.Contains(p_entity.InvoiceStatusCode));
                    }
                    if (!string.IsNullOrEmpty(p_entity.PaymentGroup))
                    {
                        q = q.Where(p => p.PaymentGroup.Contains(p_entity.PaymentGroup));
                    }
                    if (p_entity.SendRemittance != false)
                    {
                        q = q.Where(p => p.SendRemittance == p_entity.SendRemittance);
                    }
                    if (p_entity.SplitAccount != false)
                    {
                        q = q.Where(p => p.SplitAccount == p_entity.SplitAccount);
                    }
                    if (!string.IsNullOrEmpty(p_entity.ShipVia))
                    {
                        q = q.Where(p => p.ShipVia.Contains(p_entity.ShipVia));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Remarks))
                    {
                        q = q.Where(p => p.Remarks.Contains(p_entity.Remarks));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Carrier))
                    {
                        q = q.Where(p => p.Carrier.Contains(p_entity.Carrier));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Purchasecontact))
                    {
                        q = q.Where(p => p.Purchasecontact.Contains(p_entity.Purchasecontact));
                    }
                    if (!string.IsNullOrEmpty(p_entity.DayBookSet))
                    {
                        q = q.Where(p => p.DayBookSet.Contains(p_entity.DayBookSet));
                    }
                    if (p_entity.KanBanSupplier != false)
                    {
                        q = q.Where(p => p.KanBanSupplier == p_entity.KanBanSupplier);
                    }
                    if (!string.IsNullOrEmpty(p_entity.PromotionGroup))
                    {
                        q = q.Where(p => p.PromotionGroup.Contains(p_entity.PromotionGroup));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Buyer))
                    {
                        q = q.Where(p => p.Buyer.Contains(p_entity.Buyer));
                    }
                    if (!string.IsNullOrEmpty(p_entity.PriceTable))
                    {
                        q = q.Where(p => p.PriceTable.Contains(p_entity.PriceTable));
                    }
                    if (!string.IsNullOrEmpty(p_entity.DiscountTable))
                    {
                        q = q.Where(p => p.DiscountTable.Contains(p_entity.DiscountTable));
                    }
                    if (p_entity.FixedPrice != false)
                    {
                        q = q.Where(p => p.FixedPrice == p_entity.FixedPrice);
                    }
                    if (!string.IsNullOrEmpty(p_entity.NonSoPrice))
                    {
                        q = q.Where(p => p.NonSoPrice.Contains(p_entity.NonSoPrice));
                    }
                    if (p_entity.UID != 0)
                    {
                        q = q.Where(p => p.UID == p_entity.UID);
                    }
                    if (!string.IsNullOrEmpty(p_entity.CreateUser))
                    {
                        q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Remark))
                    {
                        q = q.Where(p => p.Remark.Contains(p_entity.Remark));
                    }
                    if (!string.IsNullOrEmpty(p_entity.CommandType))
                    {
                        q = q.Where(p => p.CommandType.Contains(p_entity.CommandType));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Domain))
                    {
                        q = q.Where(p => p.Domain.Contains(p_entity.Domain));
                    }
                    if (!string.IsNullOrEmpty(p_entity.Site))
                    {
                        q = q.Where(p => p.Site.Contains(p_entity.Site));
                    }

                    _ret.Result = q;
                    _ret.State = ReturnStatus.Succeed;
                    p_action(_ret);
                }
            }
            catch (Exception e)
            {
                _ret.State = ReturnStatus.Failed;
                _ret.ErrorList.Add(e);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(TES_SUPPLIER), "Get_TES_SUPPLIER_List", e.Message);
                throw e;
            }
            
        }
        public static DataTable Get_TES_SUPPLIER_List(string p_tableName, string p_TaskID)
        {
            DataTable dt = new DataTable(); ;
            var dbSetting = GlobalConfig.ExchangeCenterDB;
            try
            {

                var strConn = EntitiesFactory.GetEfConnectionString(dbSetting);
                SqlConnection conn = new System.Data.SqlClient.SqlConnection();

                conn.ConnectionString = strConn;

                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                SqlCommand cmd = new SqlCommand();

                cmd.Connection = conn;
                string _sql = string.Format("select * from {0} where  TaskID='{1}'", p_tableName, p_TaskID);

                cmd.CommandText = _sql;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);

                adapter.Fill(dt);

            }
            catch (SqlException ex)
            {
                throw new Exception($"系统无法连接到数据库:{dbSetting},请检查配置的服务器,数据库,用户名和密码等信息是否正确。{Environment.NewLine}{ex}");
            }
            return dt;
        }
        public static ResultObject<bool> Save_TES_SUPPLIER(List<long> p_listUid, EnumTaskState p_state)
        {

            ResultObject<bool> _ret = new ResultObject<bool>();
            try
            {
                using (var db = EntitiesFactory.CreateExchangeCenterInstance())
                {
                    var _lst = db.TES_SUPPLIER.Where(p => p_listUid.Contains(p.UID)).ToList();
                    foreach (var itm in _lst)
                    {
                        db.TES_SUPPLIER.AddOrUpdate(itm);
                    }
                    if (db.SaveChanges() != -1)
                    {
                        _ret.State = ReturnStatus.Succeed;
                        _ret.Result = true;
                    }
                    else
                    {
                        _ret.State = ReturnStatus.Failed;
                        _ret.Result = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ret.State = ReturnStatus.Failed;
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(TES_SUPPLIER), "Save_TES_SUPPLIER", e.Message);
                _ret.Result = false;
                _ret.ErrorList.Add(e);
                throw e;
            }
            return _ret;

        }



        public ResultObject<bool> Del_TES_SUPPLIER(List<TES_SUPPLIER> p_entitys)
        {
            ResultObject<bool> _ret = new ResultObject<bool>();
            try
            {
                using (var db = EntitiesFactory.CreateExchangeCenterInstance())
                {
                    foreach (var itm in p_entitys)
                    {
                        db.TES_SUPPLIER.Remove(itm);
                    }
                    if (db.SaveChanges() != -1)
                    {
                        _ret.State = ReturnStatus.Succeed;
                        _ret.Result = true;
                    }
                    else
                    {
                        _ret.State = ReturnStatus.Failed;
                        _ret.Result = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(e);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(TES_SUPPLIER), "Del_TES_SUPPLIER", e.Message); throw e;
            }
            return _ret;
        }

    }
}