using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Collections;
using System.Web.UI.WebControls;
namespace BLL
{
    public class CostStatisticService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 记录数
        /// 
        private static int count
        {
            get;
            set;
        }
        /// 
        /// 定义变量
        /// 
        private static IQueryable cost = from x in db.TC_CostStatistic orderby x.CostStatisticCode descending select x;
        /// 
        /// 获取分页列表
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable getListCost(string projectId, int startRowIndex, int maximumRows)
        {
            IQueryable q = cost;
            if (!string.IsNullOrEmpty(projectId))
            {
                q = q.Where(e => e.ProjectId == projectId);
            }
            count = q.Count();
            if (count == 0)
            {
                return new object[] { "" };
            }
            return from x in q.Skip(startRowIndex).Take(maximumRows)
                   select new
                   {
                       x.CostStatisticCode,
                       x.ProjectId,
                       Months = Convert.ToDateTime(x.Months).Year + "-" + (Convert.ToInt32(Convert.ToDateTime(x.Months).Month) < 10 ? ("0" + Convert.ToDateTime(x.Months).Month).ToString() : Convert.ToDateTime(x.Months).Month.ToString()),
                   };
        }
        /// 
        /// 获取列表数
        /// 
        /// 
        public static int getListCount(string projectId)
        {
            return count;
        }
        /// 
        /// 根据费用汇总编号查询费用汇总信息
        /// 
        /// 费用汇总编号
        /// 费用汇总信息
        public static Model.TC_CostStatistic GetCostStatisticByCostStatisticCode(string costStatisticCode)
        {
            return Funs.DB.TC_CostStatistic.FirstOrDefault(x => x.CostStatisticCode == costStatisticCode);
        }
        /// 
        /// 根据月份和项目号查询费用汇总信息
        /// 
        /// 月份
        /// 项目号
        /// 费用汇总信息
        public static Model.TC_CostStatistic GetCostStatisticByMonthsAndProjectId(DateTime months, string projectId)
        {
            return Funs.DB.TC_CostStatistic.FirstOrDefault(x => x.Months == months && x.ProjectId == projectId);
        }
        /// 
        /// 根据月份和项目号查询最近的一条费用汇总信息
        /// 
        /// 月份
        /// 项目号
        /// 费用汇总信息
        public static Model.TC_CostStatistic GetLastCostStatisticByMonthsAndProjectId(DateTime months, string projectId)
        {
            return (from x in Funs.DB.TC_CostStatistic where x.Months < months && x.ProjectId == projectId orderby x.Months descending select x).FirstOrDefault();
        }
        /// 
        /// 增加费用汇总信息
        /// 
        /// 费用汇总实体
        public static void AddCostStatistic(Model.TC_CostStatistic tc_CostStatistic)
        {
            Model.SGGLDB db = Funs.DB;
            Model.TC_CostStatistic newtc_CostStatistic = new Model.TC_CostStatistic
            {
                CostStatisticCode = tc_CostStatistic.CostStatisticCode,
                ProjectId = tc_CostStatistic.ProjectId,
                Months = tc_CostStatistic.Months,
                CompileMan = tc_CostStatistic.CompileMan,
                CompileDate = tc_CostStatistic.CompileDate
            };
            db.TC_CostStatistic.InsertOnSubmit(newtc_CostStatistic);
            db.SubmitChanges();
        }
        /// 
        /// 根据费用汇总主键删除一个费用汇总信息
        /// 
        /// 费用汇总主键
        public static void DeleteCostStatisticByCostStatisticCode(string costStatisticCode)
        {
            Model.SGGLDB db = Funs.DB;
            Model.TC_CostStatistic tc_CostStatistic = db.TC_CostStatistic.FirstOrDefault(e => e.CostStatisticCode == costStatisticCode);
            if (tc_CostStatistic != null)
            {
                db.TC_CostStatistic.DeleteOnSubmit(tc_CostStatistic);
                db.SubmitChanges();
            }
        }
    }
}