using FineUIPro;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLL
{
    public static class EnergyreportItemService
    {
        public static Model.SGGLDB db = Funs.DB;
        #region 获取列表
        /// 
        /// 记录数
        /// 
        public static int count
        {
            get;
            set;
        }
        public static List GetEnvironmental_EnergyReportItemByModle(Model.Environmental_EnergyReportItem table)
        {
            var q = from x in db.Environmental_EnergyReportItem
                    where
                              (string.IsNullOrEmpty(table.EnergyReportItemId) || x.EnergyReportItemId.Contains(table.EnergyReportItemId)) &&
                              (string.IsNullOrEmpty(table.EnergyReportId) || x.EnergyReportId.Contains(table.EnergyReportId)) &&
                              (string.IsNullOrEmpty(table.SortIndex) || x.SortIndex.Contains(table.SortIndex)) &&
                              (string.IsNullOrEmpty(table.BusinessCategory) || x.BusinessCategory.Contains(table.BusinessCategory)) &&
                              (string.IsNullOrEmpty(table.Throughput_UnitOfMeasurement) || x.Throughput_UnitOfMeasurement.Contains(table.Throughput_UnitOfMeasurement)) &&
                              (string.IsNullOrEmpty(table.Yield_UnitOfMeasurement) || x.Yield_UnitOfMeasurement.Contains(table.Yield_UnitOfMeasurement)) &&
                              (string.IsNullOrEmpty(table.OperationScale_UnitOfMeasurement) || x.OperationScale_UnitOfMeasurement.Contains(table.OperationScale_UnitOfMeasurement)) 
                    select x
                  ;
            return q.ToList();
        }
        /// 获取分页列表
        /// 
        /// 页码
        /// 每页数量
        /// 
        public static IEnumerable getListData(Model.Environmental_EnergyReportItem table, Grid Grid1)
        {
            var q = GetEnvironmental_EnergyReportItemByModle(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.EnergyReportItemId,
                       x.EnergyReportId,
                       x.SortIndex,
                       x.BusinessCategory,
                       x.Throughput_BasePeriod,
                       x.Throughput_LastPeriod,
                       x.Throughput_ThisPeriod,
                       x.Throughput_UnitOfMeasurement,
                       x.Yield_BasePeriod,
                       x.Yield_LastPeriod,
                       x.Yield_ThisPeriod,
                       x.Yield_UnitOfMeasurement,
                       x.OutputValue_BasePeriod,
                       x.OutputValue_LastYear,
                       x.OutputValue_ThisYear,
                       x.OperationScale_BasePeriod,
                       x.OperationScale_LastYear,
                       x.OperationScale_ThisYear,
                       x.OperationScale_UnitOfMeasurement,
                       x.ServiceOperatingIncome_BasePeriod,
                       x.ServiceOperatingIncome_LastYear,
                       x.ServiceOperatingIncome_ThisYear,
                   };
        }
        #endregion
        public static Model.Environmental_EnergyReportItem GetEnvironmental_EnergyReportItemById(string EnergyReportItemId)
        {
            return db.Environmental_EnergyReportItem.FirstOrDefault(x => x.EnergyReportItemId == EnergyReportItemId);
        }
        /// 
        /// 根据主表Id获取明细记录集合
        /// 
        /// 
        /// 明细记录集合
        public static List GetItems(string EnergyReportId)
        {
            return (from x in Funs.DB.Environmental_EnergyReportItem
                    where x.EnergyReportId == EnergyReportId
                    orderby x.SortIndex,x.SortUnit
                    select x).ToList();
        }
        /// 
        /// 根据主表Id获取明细记录集合
        /// 
        /// 化工行业能源节约与生态环境保护汇总明细表Id
        /// 明细记录集合
        public static List GetYearSumItems(string unitId, int? year, int? Quarters)
        {
            return (from x in Funs.DB.Environmental_EnergyReportItem
                    join y in Funs.DB.Environmental_EnergyReport
                    on x.EnergyReportId equals y.EnergyReportId
                    where y.UnitId == unitId && y.Year == year && y.Quarters == Quarters
                    orderby x.SortIndex
                    select x).Distinct().ToList();
        }
        /// 
        /// 根据主表Id获取年度明细记录集合
        /// 
        /// 化工行业能源节约与生态环境保护汇总明细表Id
        /// 年度明细记录集合
        public static List GetLastYearItems(int year)
        {
            return (from x in Funs.DB.Environmental_EnergyReportItem
                    join y in Funs.DB.Environmental_EnergyReport
                    on x.EnergyReportId equals y.EnergyReportId
                    where y.Year == year
                    select x).Distinct().ToList();
        }
        /// 
        /// 根据主表Id获取明细记录集合(不包含本月合计行)
        /// 
        /// 化工行业能源节约与生态环境保护汇总明细表Id
        /// 明细记录集合
        public static List GetShowItems(string EnergyReportId)
        {
            var q = (from x in Funs.DB.Environmental_EnergyReportItem
                     where x.EnergyReportId == EnergyReportId
                     orderby x.SortIndex,x.SortUnit
                     select x).ToList();
            //List newItems = new List();
            //foreach (var item in q)
            //{
            //    Model.Environmental_EnergyReportItem newItem = new Model.Environmental_EnergyReportItem();
            //    newItem.EnergyReportItemId = item.EnergyReportItemId;
            //    newItem.EnergyReportId = item.EnergyReportId;
            //    newItem.SortIndex = item.SortIndex;
            //    newItem.BusinessCategory = item.BusinessCategory;
            //    newItem.Throughput_BasePeriod = item.Throughput_BasePeriod;
            //    newItem.Throughput_LastPeriod = item.Throughput_LastPeriod;
            //    newItem.Throughput_ThisPeriod = item.Throughput_ThisPeriod;
            //    newItem.Throughput_UnitOfMeasurement = item.Throughput_UnitOfMeasurement;
            //    newItem.Yield_BasePeriod = item.Yield_BasePeriod;
            //    newItem.Yield_LastPeriod = item.Yield_LastPeriod;
            //    newItem.Yield_ThisPeriod = item.Yield_ThisPeriod;
            //    newItem.Yield_UnitOfMeasurement = item.Yield_UnitOfMeasurement;
            //    newItem.OutputValue_BasePeriod = item.OutputValue_BasePeriod;
            //    newItem.OutputValue_LastYear = item.OutputValue_LastYear;
            //    newItem.OutputValue_ThisYear = item.OutputValue_ThisYear;
            //    newItem.OperationScale_BasePeriod = item.OperationScale_BasePeriod;
            //    newItem.OperationScale_LastYear = item.OperationScale_LastYear;
            //    newItem.OperationScale_ThisYear = item.OperationScale_ThisYear;
            //    newItem.OperationScale_UnitOfMeasurement = item.OperationScale_UnitOfMeasurement;
            //    newItem.ServiceOperatingIncome_BasePeriod = item.ServiceOperatingIncome_BasePeriod;
            //    newItem.ServiceOperatingIncome_LastYear = item.ServiceOperatingIncome_LastYear;
            //    newItem.ServiceOperatingIncome_ThisYear = item.ServiceOperatingIncome_ThisYear;
                
            //    newItems.Add(newItem);
            //}
            return q;
        }
        public static void AddEnvironmental_EnergyReportItem(Model.Environmental_EnergyReportItem newtable)
        {
            Model.Environmental_EnergyReportItem table = new Model.Environmental_EnergyReportItem
            {
                EnergyReportItemId = newtable.EnergyReportItemId,
                EnergyReportId = newtable.EnergyReportId,
                SortIndex = newtable.SortIndex,
                BusinessCategory = newtable.BusinessCategory,
                Throughput_BasePeriod = newtable.Throughput_BasePeriod,
                Throughput_LastPeriod = newtable.Throughput_LastPeriod,
                Throughput_ThisPeriod = newtable.Throughput_ThisPeriod,
                Throughput_UnitOfMeasurement = newtable.Throughput_UnitOfMeasurement,
                Yield_BasePeriod = newtable.Yield_BasePeriod,
                Yield_LastPeriod = newtable.Yield_LastPeriod,
                Yield_ThisPeriod = newtable.Yield_ThisPeriod,
                Yield_UnitOfMeasurement = newtable.Yield_UnitOfMeasurement,
                OutputValue_BasePeriod = newtable.OutputValue_BasePeriod,
                OutputValue_LastYear = newtable.OutputValue_LastYear,
                OutputValue_ThisYear = newtable.OutputValue_ThisYear,
                OperationScale_BasePeriod = newtable.OperationScale_BasePeriod,
                OperationScale_LastYear = newtable.OperationScale_LastYear,
                OperationScale_ThisYear = newtable.OperationScale_ThisYear,
                OperationScale_UnitOfMeasurement = newtable.OperationScale_UnitOfMeasurement,
                ServiceOperatingIncome_BasePeriod = newtable.ServiceOperatingIncome_BasePeriod,
                ServiceOperatingIncome_LastYear = newtable.ServiceOperatingIncome_LastYear,
                ServiceOperatingIncome_ThisYear = newtable.ServiceOperatingIncome_ThisYear,
            };
            db.Environmental_EnergyReportItem.InsertOnSubmit(table);
            db.SubmitChanges();
        }
        public static void AddBulkEnvironmental_EnergyReportItem(List newtables)
        {
            db.Environmental_EnergyReportItem.InsertAllOnSubmit(newtables);
            db.SubmitChanges();
        }
 
        public static void UpdateEnvironmental_EnergyReportItem(Model.Environmental_EnergyReportItem newtable)
        {
            Model.Environmental_EnergyReportItem table = db.Environmental_EnergyReportItem.FirstOrDefault(x => x.EnergyReportItemId == newtable.EnergyReportItemId);
            if (table != null)
            {
                table.EnergyReportItemId = newtable.EnergyReportItemId;
                table.EnergyReportId = newtable.EnergyReportId;
                table.SortIndex = newtable.SortIndex;
                table.BusinessCategory = newtable.BusinessCategory;
                table.Throughput_BasePeriod = newtable.Throughput_BasePeriod;
                table.Throughput_LastPeriod = newtable.Throughput_LastPeriod;
                table.Throughput_ThisPeriod = newtable.Throughput_ThisPeriod;
                table.Throughput_UnitOfMeasurement = newtable.Throughput_UnitOfMeasurement;
                table.Yield_BasePeriod = newtable.Yield_BasePeriod;
                table.Yield_LastPeriod = newtable.Yield_LastPeriod;
                table.Yield_ThisPeriod = newtable.Yield_ThisPeriod;
                table.Yield_UnitOfMeasurement = newtable.Yield_UnitOfMeasurement;
                table.OutputValue_BasePeriod = newtable.OutputValue_BasePeriod;
                table.OutputValue_LastYear = newtable.OutputValue_LastYear;
                table.OutputValue_ThisYear = newtable.OutputValue_ThisYear;
                table.OperationScale_BasePeriod = newtable.OperationScale_BasePeriod;
                table.OperationScale_LastYear = newtable.OperationScale_LastYear;
                table.OperationScale_ThisYear = newtable.OperationScale_ThisYear;
                table.OperationScale_UnitOfMeasurement = newtable.OperationScale_UnitOfMeasurement;
                table.ServiceOperatingIncome_BasePeriod = newtable.ServiceOperatingIncome_BasePeriod;
                table.ServiceOperatingIncome_LastYear = newtable.ServiceOperatingIncome_LastYear;
                table.ServiceOperatingIncome_ThisYear = newtable.ServiceOperatingIncome_ThisYear;
                db.SubmitChanges();
            }
        }
        public static void DeleteEnvironmental_EnergyReportItemById(string EnergyReportItemId)
        {
            Model.Environmental_EnergyReportItem table = db.Environmental_EnergyReportItem.FirstOrDefault(x => x.EnergyReportItemId == EnergyReportItemId);
            if (table != null)
            {
                db.Environmental_EnergyReportItem.DeleteOnSubmit(table);
                db.SubmitChanges();
            }
        }
        public static void DeleteALLEnvironmental_EnergyReportItem()
        {
            if (db.Environmental_EnergyReportItem != null)
            {
                db.Environmental_EnergyReportItem.DeleteAllOnSubmit(db.Environmental_EnergyReportItem);
                db.SubmitChanges();
            }
        }
        public static void DeleteEnergyReportItemByChemicalReportId(string EnergyReportId)
        {
            var q = from x in Funs.DB.Environmental_EnergyReportItem where x.EnergyReportId == EnergyReportId select x;
            if (q != null)
            {
                Funs.DB.Environmental_EnergyReportItem.DeleteAllOnSubmit(q);
                Funs.DB.SubmitChanges();
            }
        }
      
    }
}