using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace BLL
{
    /// 
    /// 一般方案过程描述
    /// 
    public static class GeneralPlanApprovalItemService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 记录数
        /// 
        public static int count
        {
            get;
            set;
        }
        /// 
        /// 获取分页列表
        /// 
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable GetListData(string generalPlanApprovalId, int startRowIndex, int maximumRows)
        {
            IQueryable q = from x in db.Comprehensive_GeneralPlanApprovalItem
                                                                        where x.GeneralPlanApprovalId == generalPlanApprovalId
                                                                        orderby x.SubmitDate descending
                                                                        select x;
            count = q.Count();
            if (count == 0)
            {
                return new object[] { "" };
            }
            return from x in q.Skip(startRowIndex).Take(maximumRows)
                   select new
                   {
                       x.GeneralPlanApprovalItemId,
                       x.GeneralPlanApprovalId,
                       x.UnitId,
                       UnitName = (from y in db.Base_Unit where y.UnitId == x.UnitId select y.UnitName).FirstOrDefault(),
                       x.Counts,
                       x.SubmitDate,
                       x.ReturnDate,
                       x.ModifyContents,
                       x.AttachUrl
                   };
        }
        /// 
        /// 获取分页列表数
        /// 
        /// 
        /// 
        public static int GetListCount(string generalPlanApprovalId)
        {
            return count;
        }
        /// 
        /// 根据主键获取一般方案过程描述
        /// 
        /// 
        /// 
        public static Model.Comprehensive_GeneralPlanApprovalItem GetGeneralPlanApprovalItemById(string generalPlanApprovalItemId)
        {
            return Funs.DB.Comprehensive_GeneralPlanApprovalItem.FirstOrDefault(e => e.GeneralPlanApprovalItemId == generalPlanApprovalItemId);
        }
        /// 
        /// 添加一般方案过程描述
        /// 
        /// 
        public static void AddGeneralPlanApprovalItem(Model.Comprehensive_GeneralPlanApprovalItem generalPlanApprovalItem)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Comprehensive_GeneralPlanApprovalItem newGeneralPlanApprovalItem = new Model.Comprehensive_GeneralPlanApprovalItem();
            newGeneralPlanApprovalItem.GeneralPlanApprovalItemId = generalPlanApprovalItem.GeneralPlanApprovalItemId;
            newGeneralPlanApprovalItem.GeneralPlanApprovalId = generalPlanApprovalItem.GeneralPlanApprovalId;
            newGeneralPlanApprovalItem.UnitId = generalPlanApprovalItem.UnitId;
            newGeneralPlanApprovalItem.Counts = generalPlanApprovalItem.Counts;
            newGeneralPlanApprovalItem.SubmitDate = generalPlanApprovalItem.SubmitDate;
            newGeneralPlanApprovalItem.ReturnDate = generalPlanApprovalItem.ReturnDate;
            newGeneralPlanApprovalItem.ModifyContents = generalPlanApprovalItem.ModifyContents;
            newGeneralPlanApprovalItem.AttachUrl = generalPlanApprovalItem.AttachUrl;
            db.Comprehensive_GeneralPlanApprovalItem.InsertOnSubmit(newGeneralPlanApprovalItem);
            db.SubmitChanges();
        }
        /// 
        /// 修改一般方案过程描述
        /// 
        /// 
        public static void UpdateGeneralPlanApprovalItem(Model.Comprehensive_GeneralPlanApprovalItem generalPlanApprovalItem)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Comprehensive_GeneralPlanApprovalItem newGeneralPlanApprovalItem = db.Comprehensive_GeneralPlanApprovalItem.FirstOrDefault(e => e.GeneralPlanApprovalItemId == generalPlanApprovalItem.GeneralPlanApprovalItemId);
            if (newGeneralPlanApprovalItem != null)
            {
                newGeneralPlanApprovalItem.UnitId = generalPlanApprovalItem.UnitId;
                newGeneralPlanApprovalItem.Counts = generalPlanApprovalItem.Counts;
                newGeneralPlanApprovalItem.SubmitDate = generalPlanApprovalItem.SubmitDate;
                newGeneralPlanApprovalItem.ReturnDate = generalPlanApprovalItem.ReturnDate;
                newGeneralPlanApprovalItem.ModifyContents = generalPlanApprovalItem.ModifyContents;
                newGeneralPlanApprovalItem.AttachUrl = generalPlanApprovalItem.AttachUrl;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除一般方案过程描述
        /// 
        /// 
        public static void DeleteGeneralPlanApprovalItemById(string generalPlanApprovalItemId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Comprehensive_GeneralPlanApprovalItem newGeneralPlanApprovalItem = db.Comprehensive_GeneralPlanApprovalItem.FirstOrDefault(e => e.GeneralPlanApprovalItemId == generalPlanApprovalItemId);
            if (newGeneralPlanApprovalItem != null)
            {
                if (!string.IsNullOrEmpty(newGeneralPlanApprovalItem.AttachUrl))
                {
                    BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, newGeneralPlanApprovalItem.AttachUrl);//删除附件
                }
                db.Comprehensive_GeneralPlanApprovalItem.DeleteOnSubmit(newGeneralPlanApprovalItem);
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据一般方案主键删除所有相关过程描述
        /// 
        /// 
        public static void DeleteGeneralPlanApprovalItemByGeneralPlanApprovalId(string generalPlanApprovalId)
        {
            Model.SGGLDB db = Funs.DB;
            var q = (from x in db.Comprehensive_GeneralPlanApprovalItem where x.GeneralPlanApprovalId == generalPlanApprovalId select x).ToList();
            if (q != null)
            {
                foreach (var att in q)
                {
                    if (!string.IsNullOrEmpty(att.AttachUrl))
                    {
                        BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, att.AttachUrl);//删除附件
                    }
                }
                db.Comprehensive_GeneralPlanApprovalItem.DeleteAllOnSubmit(q);
                db.SubmitChanges();
            }
        }
    }
}