using FineUIPro;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace BLL
{

    public static class Base_FactoryService
    {
        public static Model.CNPCDB db = Funs.DB;


        #region 获取列表
        /// <summary>
        /// 记录数
        /// </summary>
        public static int count
        {
            get;
            set;
        }
        public static List<Model.Base_Factory> GetBase_FactoryByModle(Model.Base_Factory table)
        {
            var q = from x in db.Base_Factory
                    where
                              (string.IsNullOrEmpty(table.FactoryId) || x.FactoryId.Contains(table.FactoryId)) &&
                              (string.IsNullOrEmpty(table.UnitId) || x.UnitId.Contains(table.UnitId)) &&
                              (string.IsNullOrEmpty(table.FactoryCode) || x.FactoryCode.Contains(table.FactoryCode)) &&
                              (string.IsNullOrEmpty(table.FactoryName) || x.FactoryName.Contains(table.FactoryName)) &&
                              (string.IsNullOrEmpty(table.Address) || x.Address.Contains(table.Address)) 
                    select x
                  ;

            return q.ToList();
        }
        public static List<Model.Base_Factory> GetBase_FactoryList()
        {
            var q = (from x in db.Base_Factory orderby x.FactoryCode select x).ToList();    
                   

            return q;
        }
        /// 获取分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">每页数量</param>
        /// <returns></returns>
        public static IEnumerable getListData(Model.Base_Factory table, Grid Grid1)
        {
            var q = GetBase_FactoryByModle(table);
            count = q.Count();
            if (count == 0)
            {
                return null;
            }
            //  q=  q.Take(Grid1.PageSize * Grid1.PageIndex).Skip(Grid1.PageSize * (Grid1.PageIndex)).ToList();
            // q = SortConditionHelper.SortingAndPaging(q, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize);
            return from x in q
                   select new
                   {
                       x.FactoryId,
                       x.UnitId,
                       x.FactoryCode,
                       x.FactoryName,
                       x.Address,

                   };
        }
        #endregion

        public static Model.Base_Factory GetBase_FactoryById(string FactoryId)
        {
            return db.Base_Factory.FirstOrDefault(x => x.FactoryId == FactoryId);
        }
        public static string  GetBase_FactoryNameById(object FactoryId)
        {
            string name = string.Empty;
            if (FactoryId!=null)
            {
                var model = db.Base_Factory.FirstOrDefault(x => x.FactoryId == FactoryId.ToString());
                if (model != null)
                {
                    name = model.FactoryName;
                }

            }
            return name; 
        }
        public static Model.Base_Factory GetBase_FactoryByCode(string  FactoryCode)
        {
            return db.Base_Factory.FirstOrDefault(x => x.FactoryCode == FactoryCode);
        }
        public static void AddBase_Factory(Model.Base_Factory newtable)
        {

            Model.Base_Factory table = new Model.Base_Factory
            {
                FactoryId = newtable.FactoryId,
                UnitId = newtable.UnitId,
                FactoryCode = newtable.FactoryCode,
                FactoryName = newtable.FactoryName,
                Address = newtable.Address,
                MapCoordinates= newtable.MapCoordinates,
            };
            db.Base_Factory.InsertOnSubmit(table);
            db.SubmitChanges();
        }

        public static void AddBulkBase_Factory(List<Model.Base_Factory> newtables)
        {

            db.Base_Factory.InsertAllOnSubmit(newtables);
            db.SubmitChanges();
        }


        public static void UpdateBase_Factory(Model.Base_Factory newtable)
        {

            Model.Base_Factory table = db.Base_Factory.FirstOrDefault(x => x.FactoryId == newtable.FactoryId);
            if (table != null)
            {
                table.FactoryId = newtable.FactoryId;
                table.UnitId = newtable.UnitId;
                table.FactoryCode = newtable.FactoryCode;
                table.FactoryName = newtable.FactoryName;
                table.Address = newtable.Address;
                table.MapCoordinates = newtable.MapCoordinates;
                db.SubmitChanges();
            }

        }
        public static void DeleteBase_FactoryById(string FactoryId)
        {

            Model.Base_Factory table = db.Base_Factory.FirstOrDefault(x => x.FactoryId == FactoryId);
            if (table != null)
            {
                db.Base_Factory.DeleteOnSubmit(table);
                db.SubmitChanges();
            }

        }

        public static void DeleteALLBase_Factory()
        {
            if (db.Base_Factory != null)
            {
                db.Base_Factory.DeleteAllOnSubmit(db.Base_Factory);
                db.SubmitChanges();
            }
        }
        public static void InitBase_FactoryDownList(FineUIPro.DropDownList dropName, bool isShowPlease)
        {
            dropName.DataValueField = "FactoryId";
            dropName.DataTextField = "FactoryName";
            dropName.DataSource = GetBase_FactoryList();
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
    }
}