using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CK.SCP.Models;
using CK.SCP.Models.AppBoxEntity;
using CK.SCP.Models.ScpEntity;
using CK.SCP.Utils;
using System.Data.Entity.Core;
using CK.SCP.Models.Enums;
using CK.SCP.Models.ScpEntity.ExcelExportEnttity;

namespace CK.SCP.Controller
{
    public class SCP_TA_LANGUAGE_CONTROLLER
    {
        public static ResultObject<List<TA_LANGUAGE>> Get_TA_LANGUAGE_List(TA_LANGUAGE p_entity)
        {
            ResultObject<List<TA_LANGUAGE>> _ret = new ResultObject<List<TA_LANGUAGE>>();
            try
            {
                using (ScpEntities db = EntitiesFactory.CreateScpInstance())
                {
                    IQueryable<TA_LANGUAGE> q = db.TA_LANGUAGE;
                    if (p_entity.UID != 0)
                    {
                        q = q.Where(p => p.UID == p_entity.UID);
                    }
                    if (!string.IsNullOrEmpty(p_entity.GUID))
                    {
                        q = q.Where(p => p.GUID.Contains(p_entity.GUID));
                    }
                    if (!string.IsNullOrEmpty(p_entity.CH))
                    {
                        q = q.Where(p => p.CH==p_entity.CH);
                    }
                    if (!string.IsNullOrEmpty(p_entity.EN))
                    {
                        q = q.Where(p => p.EN.Contains(p_entity.EN));
                    }
                    if (!string.IsNullOrEmpty(p_entity.RU))
                    {
                        q = q.Where(p => p.RU.Contains(p_entity.RU));
                    }
                    if (!string.IsNullOrEmpty(p_entity.DE))
                    {
                        q = q.Where(p => p.DE.Contains(p_entity.DE));
                    }
                    if (!string.IsNullOrEmpty(p_entity.FR))
                    {
                        q = q.Where(p => p.FR.Contains(p_entity.FR));
                    }
                    if (!string.IsNullOrEmpty(p_entity.EL))
                    {
                        q = q.Where(p => p.EL.Contains(p_entity.EL));
                    }
                    if (!string.IsNullOrEmpty(p_entity.AR))
                    {
                        q = q.Where(p => p.AR.Contains(p_entity.AR));
                    }
                    if (!string.IsNullOrEmpty(p_entity.JP))
                    {
                        q = q.Where(p => p.JP.Contains(p_entity.JP));
                    }
                   
                    _ret.State = ReturnStatus.Succeed;
                    _ret.Result = q.ToList();
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
            {
                var sb = new StringBuilder();

                foreach (var error in dbEx.EntityValidationErrors.ToList())
                {

                    error.ValidationErrors.ToList().ForEach(i =>
                    {
                        sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
                    });
                }
                _ret.State = ReturnStatus.Failed;
                _ret.ErrorList.Add(dbEx);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Get_TA_LANGUAGE_List", sb.ToString());
                throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
            }
            catch (OptimisticConcurrencyException ex)//并发冲突异常
            {
                
                _ret.State = ReturnStatus.Failed;
                _ret.ErrorList.Add(ex);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Get_TA_LANGUAGE_List", ex.ToString());
                throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
            }
            catch (ScpException ex)
            {
                

                _ret.State = ReturnStatus.Failed;
                _ret.ErrorList.Add(ex);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Get_TA_LANGUAGE_List", ex.ToString());

                if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
                {
                    var inner = (UpdateException)ex.InnerException;

                    
                    throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
                }
                else
                {
                    if (ex.InnerException != null) throw ex.InnerException;
                }
            }
            catch (Exception e)
            {
                _ret.State = ReturnStatus.Failed;
                _ret.ErrorList.Add(e);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Get_TA_LANGUAGE_List", e.Message);
                throw e;
            }
            return _ret;
        }

        public static ResultObject<bool> EXCEL_ADRESS_LIST_CQ(List<SCP_VENDER_EXPORT> p_order_list, string site, string p_creator)
        {
            ResultObject<bool> _ret = new ResultObject<bool>();
            try
            {
                using (ScpEntities db = EntitiesFactory.CreateScpInstance())
                {
                    int number = 1;
                    List<string> lineError = new List<string>();
                    List<string> ErrorList = new List<string>();
                    var _lst = p_order_list;

                  
                    if (lineError.Count > 0)
                    {
                        _ret.State = ReturnStatus.Failed;
                        _ret.MessageList.AddRange(ErrorList);
                        _ret.Result = false;
                    }
                    else
                    {
                        _lst.ForEach(p =>
                        {

                            var _Sitelist = db.TB_ADDRESS.Count(t => t.Address == p.收货地址);
                            if (_Sitelist == 0)
                            {
                                TB_ADDRESS Price = new TB_ADDRESS();
                                Price.Address = p.收货地址;
                                Price.NamePone = p.收货人信息;
                              
                                db.TB_ADDRESS.AddOrUpdate(Price);
                            }
                            else if (_Sitelist == 1)
                            {
                                var _PAlist = db.TB_ADDRESS.SingleOrDefault(t => t.Address == p.收货地址);                              
                                _PAlist.NamePone = p.收货人信息;
                                db.TB_ADDRESS.AddOrUpdate(_PAlist);
                            }

                        });
                        if (_ret.MessageList.Count == 0)
                        {
                            int state = db.SaveChanges();
                            if (state != -1)
                            {
                                _ret.State = ReturnStatus.Succeed;
                                _ret.Result = true;
                            }
                            else
                            {
                                _ret.State = ReturnStatus.Failed;
                                _ret.Result = false;
                            }
                        }
                        else
                        {
                            _ret.State = ReturnStatus.Failed;
                            _ret.Result = false;
                        }
                    }
                }

            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
            {
                var sb = new StringBuilder();

                foreach (var error in dbEx.EntityValidationErrors.ToList())
                {

                    error.ValidationErrors.ToList().ForEach(i =>
                    {
                        sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
                    });
                }
                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(dbEx);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "EXCEL_VENDER_PART_MOD", sb.ToString());
                throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
            }
            catch (OptimisticConcurrencyException ex)//并发冲突异常
            {

                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(ex);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "EXCEL_VENDER_PART_MOD", ex.ToString());
                throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
            }
            catch (ScpException ex)
            {


                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(ex);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "EXCEL_VENDER_PART_MOD", ex.ToString());

                if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
                {
                    var inner = (UpdateException)ex.InnerException;


                    throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
                }
                else
                {
                    if (ex.InnerException != null) throw ex.InnerException;
                }
            }
            catch (Exception e)
            {
                _ret.State = ReturnStatus.Failed;
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "EXCEL_VENDER_PART_MOD", e.Message);
                _ret.Result = false;
                _ret.ErrorList.Add(e);
                throw e;
            }
            return _ret;
        }
        public static ResultObject<bool> Save_TA_LANGUAGE(List<TA_LANGUAGE> p_entitys)
        {
            ResultObject<bool> _ret = new ResultObject<bool>();
            try
            {
                using (ScpEntities db = EntitiesFactory.CreateScpInstance())
                {
                    foreach (var itm in p_entitys)
                    {
                        db.TA_LANGUAGE.AddOrUpdate(p => p.GUID, itm);
                    }
                    if (db.SaveChanges() != -1)
                    {
                        _ret.State = ReturnStatus.Succeed;
                        _ret.Result = true;
                    }
                    else
                    {
                        _ret.State = ReturnStatus.Failed;
                        _ret.Result = false;
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
            {
                var sb = new StringBuilder();

                foreach (var error in dbEx.EntityValidationErrors.ToList())
                {

                    error.ValidationErrors.ToList().ForEach(i =>
                    {
                        sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
                    });
                }
                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(dbEx);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Save_TA_LANGUAGE", sb.ToString());
                throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
            }
            catch (OptimisticConcurrencyException ex)//并发冲突异常
            {
                
                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(ex);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Save_TA_LANGUAGE", ex.ToString());
                throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
            }
            catch (ScpException ex)
            {
                

                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(ex);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Save_TA_LANGUAGE", ex.ToString());

                if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
                {
                    var inner = (UpdateException)ex.InnerException;

                    
                    throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
                }
                else
                {
                    if (ex.InnerException != null) throw ex.InnerException;
                }
            }
            catch (Exception e)
            {
                _ret.State = ReturnStatus.Failed;
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Save_TA_LANGUAGE", e.Message);
                _ret.Result = false;
                _ret.ErrorList.Add(e);
                throw e;
            }
            return _ret;
        }



        public static ResultObject<bool> Del_TA_LANGUAGE(TA_LANGUAGE p_entity)
        {
            ResultObject<bool> _ret = new ResultObject<bool>();
            try
            {
                using (ScpEntities db = EntitiesFactory.CreateScpInstance())
                {
                    var _entity = db.TA_LANGUAGE.Where(itm => itm.GUID == p_entity.GUID).FirstOrDefault();

                    db.TA_LANGUAGE.Remove(_entity);
                    if (db.SaveChanges() != -1)
                    {
                        _ret.State = ReturnStatus.Succeed;
                        _ret.Result = true;
                    }
                    else
                    {
                        _ret.State = ReturnStatus.Failed;
                        _ret.Result = false;
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
            {
                var sb = new StringBuilder();

                foreach (var error in dbEx.EntityValidationErrors.ToList())
                {

                    error.ValidationErrors.ToList().ForEach(i =>
                    {
                        sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
                    });
                }
                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(dbEx);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Del_TA_LANGUAGE", sb.ToString());
                throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
            }
            catch (OptimisticConcurrencyException ex)//并发冲突异常
            {
                
                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(ex);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Del_TA_LANGUAGE", ex.ToString());
                throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
            }
            catch (ScpException ex)
            {
                

                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(ex);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Del_TA_LANGUAGE", ex.ToString());

                if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
                {
                    var inner = (UpdateException)ex.InnerException;

                    
                    throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
                }
                else
                {
                    if (ex.InnerException != null) throw ex.InnerException;
                }
            }
            catch (Exception e)
            {
                _ret.State = ReturnStatus.Failed;
                _ret.Result = false;
                _ret.ErrorList.Add(e);
                LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TA_LANGUAGE_CONTROLLER), "Del_TA_LANGUAGE", e.Message); throw e;
            }
            return _ret;
        }



    }




}