using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace BLL
{
    /// 
    /// 一般方案审批
    /// 
    public static class GeneralPlanApprovalService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 记录数
        /// 
        private static int count
        {
            get;
            set;
        }
        /// 
        /// 获取分页列表
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable GetListData(string projectId, string unitId, string cNProfessionalId,string UnitWorkId, int startRowIndex, int maximumRows)
        {
            IQueryable q = from x in db.Comprehensive_GeneralPlanApproval
                                                                    where x.ProjectId == projectId
                                                                    orderby x.ApprovalDate descending
                                                                    select x;
            if (unitId != "0")
            {
                q = q.Where(e => e.UnitId == unitId);
            }
            if (cNProfessionalId != "0")
            {
                q = q.Where(e => e.CNProfessionalId == cNProfessionalId);
            }
            if (UnitWorkId != "0")
            {
                q = q.Where(e => e.UnitWorkId.Contains(UnitWorkId));
            }
            count = q.Count();
            if (count == 0)
            {
                return new object[] { "" };
            }
            return from x in q.Skip(startRowIndex).Take(maximumRows)
                   select new
                   {
                       x.GeneralPlanApprovalId,
                       x.ProjectId,
                       UnitName = (from y in db.Base_Unit where y.UnitId == x.UnitId select y.UnitName).FirstOrDefault(),
                       ProfessionalName = (from y in db.Base_CNProfessional where y.CNProfessionalId == x.CNProfessionalId select y.ProfessionalName).FirstOrDefault(),
                       x.PlanCode,
                       x.PlanName,
                       x.ApprovalDate,
                       x.AuditMan,
                       x.ApprovalMan,
                       UnitWorkName = x.UnitWorkId != null ? BLL.UnitWorkService.GetUnitWorkName(x.UnitWorkId) : null,
                       x.UnitWorkId,
                       x.ImplementationDeviation,
                       x.AttachUrl
                   };
        }
        /// 
        /// 获取分页列表数
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int GetListCount(string projectId, string unitId, string cNProfessionalId, string UnitWorkId)
        {
            return count;
        }
        /// 
        /// 根据主键获取一般方案审批
        /// 
        /// 
        /// 
        public static Model.Comprehensive_GeneralPlanApproval GetGeneralPlanApprovalById(string generalPlanApprovalId)
        {
            return Funs.DB.Comprehensive_GeneralPlanApproval.FirstOrDefault(e => e.GeneralPlanApprovalId == generalPlanApprovalId);
        }
        /// 
        /// 添加一般方案审批
        /// 
        /// 
        public static void AddGeneralPlanApproval(Model.Comprehensive_GeneralPlanApproval generalPlanApproval)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Comprehensive_GeneralPlanApproval newGeneralPlanApproval = new Model.Comprehensive_GeneralPlanApproval();
            newGeneralPlanApproval.GeneralPlanApprovalId = generalPlanApproval.GeneralPlanApprovalId;
            newGeneralPlanApproval.ProjectId = generalPlanApproval.ProjectId;
            newGeneralPlanApproval.UnitId = generalPlanApproval.UnitId;
            newGeneralPlanApproval.CNProfessionalId = generalPlanApproval.CNProfessionalId;
            newGeneralPlanApproval.PlanCode = generalPlanApproval.PlanCode;
            newGeneralPlanApproval.PlanName = generalPlanApproval.PlanName;
            newGeneralPlanApproval.ApprovalDate = generalPlanApproval.ApprovalDate;
            newGeneralPlanApproval.ApprovalMan = generalPlanApproval.ApprovalMan;
            newGeneralPlanApproval.AuditMan = generalPlanApproval.AuditMan;
            newGeneralPlanApproval.ImplementationDeviation = generalPlanApproval.ImplementationDeviation;
            newGeneralPlanApproval.AttachUrl = generalPlanApproval.AttachUrl;
            newGeneralPlanApproval.CompileMan = generalPlanApproval.CompileMan;
            newGeneralPlanApproval.CompileDate = generalPlanApproval.CompileDate;
            newGeneralPlanApproval.UnitWorkId = generalPlanApproval.UnitWorkId;
            db.Comprehensive_GeneralPlanApproval.InsertOnSubmit(newGeneralPlanApproval);
            db.SubmitChanges();
        }
        /// 
        /// 修改一般方案审批
        /// 
        /// 
        public static void UpdateGeneralPlanApproval(Model.Comprehensive_GeneralPlanApproval generalPlanApproval)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Comprehensive_GeneralPlanApproval newGeneralPlanApproval = db.Comprehensive_GeneralPlanApproval.FirstOrDefault(e => e.GeneralPlanApprovalId == generalPlanApproval.GeneralPlanApprovalId);
            if (newGeneralPlanApproval != null)
            {
                newGeneralPlanApproval.ProjectId = generalPlanApproval.ProjectId;
                newGeneralPlanApproval.UnitId = generalPlanApproval.UnitId;
                newGeneralPlanApproval.CNProfessionalId = generalPlanApproval.CNProfessionalId;
                newGeneralPlanApproval.PlanCode = generalPlanApproval.PlanCode;
                newGeneralPlanApproval.PlanName = generalPlanApproval.PlanName;
                newGeneralPlanApproval.ApprovalDate = generalPlanApproval.ApprovalDate;
                newGeneralPlanApproval.ApprovalMan = generalPlanApproval.ApprovalMan;
                newGeneralPlanApproval.AuditMan = generalPlanApproval.AuditMan;
                newGeneralPlanApproval.ImplementationDeviation = generalPlanApproval.ImplementationDeviation;
                newGeneralPlanApproval.AttachUrl = generalPlanApproval.AttachUrl;
                newGeneralPlanApproval.UnitWorkId = generalPlanApproval.UnitWorkId;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除一般方案审批
        /// 
        /// 
        public static void DeleteGeneralPlanApproval(string generalPlanApprovalId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Comprehensive_GeneralPlanApproval generalPlanApproval = db.Comprehensive_GeneralPlanApproval.FirstOrDefault(e => e.GeneralPlanApprovalId == generalPlanApprovalId);
            if (generalPlanApproval != null)
            {
                if (!string.IsNullOrEmpty(generalPlanApproval.AttachUrl))
                {
                    BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, generalPlanApproval.AttachUrl);//删除附件
                }
                db.Comprehensive_GeneralPlanApproval.DeleteOnSubmit(generalPlanApproval);
                db.SubmitChanges();
            }
        }
    }
}