using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
namespace BLL
{
    /// 
    /// 检验批管理
    /// 
    public static class InspectionManagementService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 记录数
        /// 
        private static int count
        {
            get;
            set;
        }
        /// 
        /// 获取分页列表
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable GetListData(string projectId, string UnitWorkId, string cNProfessionalId, string startDate, string endDate, int startRowIndex, int maximumRows)
        {
            IQueryable q = from x in db.ProcessControl_InspectionManagement
                                                                      where x.ProjectId == projectId && x.FileType == null
                                                                      orderby x.InspectionDate descending
                                                                      select x;
            if (UnitWorkId != "0")
            {
                q = q.Where(e => e.UnitWorkId == UnitWorkId);
            }
            if (cNProfessionalId != "0")
            {
                q = q.Where(e => e.CNProfessionalId == cNProfessionalId);
            }
            if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                q = q.Where(e => e.InspectionDate >= Funs.GetNewDateTime(startDate) && e.InspectionDate <= Funs.GetNewDateTime(endDate));
            }
            count = q.Count();
            if (count == 0)
            {
                return new object[] { "" };
            }
            return from x in q.Skip(startRowIndex).Take(maximumRows)
                   select new
                   {
                       x.InspectionId,
                       x.ProjectId,
                       x.NoticeCode,
                       x.AcceptanceCheckMan,
                       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.InspectionCode,
                       InstallationName = (from y in db.WBS_UnitWork where y.UnitWorkId == x.UnitWorkId select y.UnitWorkName).FirstOrDefault(),
                       Branch = (from y in db.WBS_DivisionProject where y.DivisionProjectId == x.Branch select y.DivisionName).FirstOrDefault(),//分部
                       ControlPointType = (from y in db.WBS_BreakdownProject where y.BreakdownProjectId == x.ControlPointType select y.BreakdownName).FirstOrDefault(),//质量控制点
                       Class = (from y in db.WBS_BreakdownProject where y.BreakdownProjectId == x.ControlPointType select y.Class).FirstOrDefault(),//控制等级
                       x.AcceptanceSite,
                       IsOnceQualified = (x.IsOnceQualified == true ? "是" : "否"),
                       x.InspectionDate,
                       x.AttachUrl,
                       x.AttachUrl2,
                   };
        }
        public static IEnumerable GetListDataForDataType(string controlPointType, string projectId, string UnitWorkId, string cNProfessionalId, string startDate, string endDate, int startRowIndex, int maximumRows)
        {
            IQueryable q = from x in db.ProcessControl_InspectionManagement
                                                                      where x.ProjectId == projectId && x.FileType == null
                                                                      orderby x.InspectionDate descending
                                                                      select x;
            if (!string.IsNullOrEmpty(controlPointType))
            {
                string[] types = controlPointType.Split(',');
                q = q.Where(e=> types.Contains(e.ControlPointType));
            }
            if (UnitWorkId != "0")
            {
                q = q.Where(e => e.UnitWorkId == UnitWorkId);
            }
            if (cNProfessionalId != "0")
            {
                q = q.Where(e => e.CNProfessionalId == cNProfessionalId);
            }
            if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                q = q.Where(e => e.InspectionDate >= Funs.GetNewDateTime(startDate) && e.InspectionDate <= Funs.GetNewDateTime(endDate));
            }
            count = q.Count();
            if (count == 0)
            {
                return new object[] { "" };
            }
            return from x in q.Skip(startRowIndex).Take(maximumRows)
                   select new
                   {
                       x.InspectionId,
                       x.ProjectId,
                       x.NoticeCode,
                       x.AcceptanceCheckMan,
                       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.InspectionCode,
                       InstallationName = (from y in db.WBS_UnitWork where y.UnitWorkId == x.UnitWorkId select y.UnitWorkName).FirstOrDefault(),
                       Branch = (from y in db.WBS_DivisionProject where y.DivisionProjectId == x.Branch select y.DivisionName).FirstOrDefault(),//分部
                       ControlPointType = (from y in db.WBS_BreakdownProject where y.BreakdownProjectId == x.ControlPointType select y.BreakdownName).FirstOrDefault(),//质量控制点
                       Class = (from y in db.WBS_BreakdownProject where y.BreakdownProjectId == x.ControlPointType select y.Class).FirstOrDefault(),//控制等级
                       x.AcceptanceSite,
                       IsOnceQualified = (x.IsOnceQualified == true ? "是" : "否"),
                       x.InspectionDate,
                       x.AttachUrl,
                       x.AttachUrl2,
                   };
        }
        /// 
        /// 获取分页列表数
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int GetListCount(string projectId, string UnitWorkId, string cNProfessionalId, string startDate, string endDate)
        {
            return count;
        }
        public static int GetListCountForDataType(string controlPointType, string projectId, string UnitWorkId, string cNProfessionalId, string startDate, string endDate)
        {
            return count;
        }
        /// 
        /// 获取分页列表
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int GetListCount(string projectId, string name,   string startDate, string endDate)
        { 
            return count;
        }
        public static DataSet GetListData(string projectId, string name, string startDate, string endDate, int startRowIndex, int maximumRows)
        {
            string sql = @"SELECT AcceptanceCheckMan,COUNT(*) SunNumber,COUNT(IsOnceQualified) OneStatisticsSunNumber,COUNT(IsOnceQualified)*100/COUNT(*) as OneStatistics
                          FROM[dbo].[ProcessControl_InspectionManagement]
                          where InspectionDate is not null
                          ";
            if (!string.IsNullOrEmpty(projectId))
            {
                sql += " and ProjectId = '" + projectId + "'";
            }
            if (!string.IsNullOrEmpty(startDate))
            {
                sql += " and InspectionDate >= '" + startDate + "'";
            }
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and AcceptanceCheckMan  like  '%" + name + "%'";
            }
            if (!string.IsNullOrEmpty(endDate))
            {
                sql += " and InspectionDate <= '" + endDate + "'";
            }
            sql += " group by AcceptanceCheckMan";
            DataSet dataSet = SQLHelper.RunSqlString(sql, "dt");
            if (dataSet != null && dataSet.Tables.Count > 0)
            {
                count = dataSet.Tables[0].Rows.Count;
                DataSet data = new DataSet();
                DataTable dataTable = new DataTable();
                foreach (var row in dataSet.Tables[0].Columns)
                {
                    dataTable.Columns.Add(row.ToString());
                }
                data.Tables.Add(dataTable);
                for (int i = startRowIndex; i <= (startRowIndex + maximumRows)&&i
        /// 根据逐渐获取检验批管理
        /// 
        /// 
        /// 
        public static Model.ProcessControl_InspectionManagement GetInspectionManagementById(string inspectionId)
        {
            return Funs.DB.ProcessControl_InspectionManagement.FirstOrDefault(e => e.InspectionId == inspectionId);
        }
        /// 
        /// 添加检验批管理
        /// 
        /// 
        public static void AddInspectionManagement(Model.ProcessControl_InspectionManagement inspectionManagement)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.ProcessControl_InspectionManagement newInspectionManagement = new Model.ProcessControl_InspectionManagement();
                newInspectionManagement.InspectionId = inspectionManagement.InspectionId;
                newInspectionManagement.ProjectId = inspectionManagement.ProjectId;
                newInspectionManagement.UnitId = inspectionManagement.UnitId;
                newInspectionManagement.CNProfessionalId = inspectionManagement.CNProfessionalId;
                newInspectionManagement.InspectionCode = inspectionManagement.InspectionCode;
                newInspectionManagement.UnitWorkId = inspectionManagement.UnitWorkId;
                newInspectionManagement.Branch = inspectionManagement.Branch;
                newInspectionManagement.ControlPointType = inspectionManagement.ControlPointType;
                newInspectionManagement.AcceptanceSite = inspectionManagement.AcceptanceSite;
                newInspectionManagement.IsOnceQualified = inspectionManagement.IsOnceQualified;
                newInspectionManagement.InspectionDate = inspectionManagement.InspectionDate;
                newInspectionManagement.AttachUrl = inspectionManagement.AttachUrl;
                newInspectionManagement.AttachUrl2 = inspectionManagement.AttachUrl2;
                newInspectionManagement.NoticeCode = inspectionManagement.NoticeCode;
                newInspectionManagement.ParentDivisionProjectId = inspectionManagement.ParentDivisionProjectId;
                newInspectionManagement.CheckDate = inspectionManagement.CheckDate;
                newInspectionManagement.CheckMan = inspectionManagement.CheckMan;
                newInspectionManagement.CompileMan = inspectionManagement.CompileMan;
                newInspectionManagement.CompileDate = inspectionManagement.CompileDate;
                newInspectionManagement.UnqualifiedReason = inspectionManagement.UnqualifiedReason;
                newInspectionManagement.AcceptanceCheckMan = inspectionManagement.AcceptanceCheckMan;
                newInspectionManagement.FileType = inspectionManagement.FileType;
                db.ProcessControl_InspectionManagement.InsertOnSubmit(newInspectionManagement);
                db.SubmitChanges();
            }
        }
        /// 
        /// 修改检验批管理
        /// 
        /// 
        public static void UpdateInspectionManagement(Model.ProcessControl_InspectionManagement inspectionManagement)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.ProcessControl_InspectionManagement newInspectionManagement = db.ProcessControl_InspectionManagement.FirstOrDefault(e => e.InspectionId == inspectionManagement.InspectionId);
                if (newInspectionManagement != null)
                {
                    newInspectionManagement.ProjectId = inspectionManagement.ProjectId;
                    newInspectionManagement.UnitId = inspectionManagement.UnitId;
                    newInspectionManagement.CNProfessionalId = inspectionManagement.CNProfessionalId;
                    newInspectionManagement.InspectionCode = inspectionManagement.InspectionCode;
                    newInspectionManagement.UnitWorkId = inspectionManagement.UnitWorkId;
                    newInspectionManagement.Branch = inspectionManagement.Branch;
                    newInspectionManagement.ControlPointType = inspectionManagement.ControlPointType;
                    newInspectionManagement.AcceptanceSite = inspectionManagement.AcceptanceSite;
                    newInspectionManagement.IsOnceQualified = inspectionManagement.IsOnceQualified;
                    newInspectionManagement.InspectionDate = inspectionManagement.InspectionDate;
                    newInspectionManagement.AttachUrl = inspectionManagement.AttachUrl;
                    newInspectionManagement.AttachUrl2 = inspectionManagement.AttachUrl2;
                    newInspectionManagement.NoticeCode = inspectionManagement.NoticeCode;
                    newInspectionManagement.ParentDivisionProjectId = inspectionManagement.ParentDivisionProjectId;
                    newInspectionManagement.UnqualifiedReason = inspectionManagement.UnqualifiedReason;
                    newInspectionManagement.AcceptanceCheckMan = inspectionManagement.AcceptanceCheckMan;
                    db.SubmitChanges();
                }
            }
        }
        /// 
        /// 根据主键删除检验批管理
        /// 
        /// 
        public static void DeleteInspectionManagement(string inspectionId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.ProcessControl_InspectionManagement inspectionManagement = db.ProcessControl_InspectionManagement.FirstOrDefault(e => e.InspectionId == inspectionId);
            if (inspectionManagement != null)
            {
                if (!string.IsNullOrEmpty(inspectionManagement.AttachUrl))
                {
                    BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, inspectionManagement.AttachUrl);//删除附件
                }
                if (!string.IsNullOrEmpty(inspectionManagement.AttachUrl2))
                {
                    BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, inspectionManagement.AttachUrl2);//删除附件
                }
                db.ProcessControl_InspectionManagement.DeleteOnSubmit(inspectionManagement);
                db.SubmitChanges();
            }
        }
        public static List getInspectionManagementListByCNProfessionalIdAndDate(string projectId, string cNProfessionalId, DateTime startDate, DateTime SoptDate, bool isOnceQualified)
        {
            List InspectionMangementList = (from x in Funs.DB.ProcessControl_InspectionManagement where x.ProjectId == projectId select x).ToList();
            if (!string.IsNullOrEmpty(cNProfessionalId) && cNProfessionalId != "0")
            {
                InspectionMangementList = (from x in InspectionMangementList where x.CNProfessionalId == cNProfessionalId select x).ToList();
            }
            if (isOnceQualified == true)
            {
                InspectionMangementList = (from x in InspectionMangementList where x.IsOnceQualified == isOnceQualified select x).ToList();
            }
            if (startDate != null && SoptDate != null)
            {
                InspectionMangementList = (from x in InspectionMangementList where x.InspectionDate >= startDate && x.InspectionDate <= SoptDate select x).ToList();
            }
            return InspectionMangementList;
        }
        public static List getInspectionManagementDetailListByCNProfessionalIdAndDate(string projectId, string cNProfessionalId, DateTime startDate, DateTime SoptDate, bool isOnceQualified)
        {
            List InspectionMangementList = (from x in Funs.DB.View_CQMS_InspectionManagementDetail where x.ProjectId == projectId select x).ToList();
            if (!string.IsNullOrEmpty(cNProfessionalId) && cNProfessionalId != "0")
            {
                InspectionMangementList = (from x in InspectionMangementList where x.CNProfessionalId == cNProfessionalId select x).ToList();
            }
            if (isOnceQualified == true)
            {
                InspectionMangementList = (from x in InspectionMangementList where x.IsOnceQualified == isOnceQualified select x).ToList();
            }
            if (startDate != null && SoptDate != null)
            {
                InspectionMangementList = (from x in InspectionMangementList where x.InspectionDate >= startDate && x.InspectionDate <= SoptDate select x).ToList();
            }
            return InspectionMangementList;
        }
        public static List getInspectionManagementDetailListByUnitIdAndDate(string projectId, string unitId, DateTime startDate, DateTime SoptDate, bool isOnceQualified)
        {
            List InspectionMangementList = (from x in Funs.DB.View_CQMS_InspectionManagementDetail where x.ProjectId == projectId select x).ToList();
            if (!string.IsNullOrEmpty(unitId) && unitId != "0")
            {
                InspectionMangementList = (from x in InspectionMangementList where x.UnitId == unitId select x).ToList();
            }
            if (isOnceQualified == true)
            {
                InspectionMangementList = (from x in InspectionMangementList where x.IsOnceQualified == isOnceQualified select x).ToList();
            }
            if (startDate != null && SoptDate != null)
            {
                InspectionMangementList = (from x in InspectionMangementList where x.InspectionDate >= startDate && x.InspectionDate <= SoptDate select x).ToList();
            }
            return InspectionMangementList;
        }
        /// 
        /// 根据主键获取视图信息
        /// 
        /// 
        /// 
        public static Model.View_InspectionManagement GetViewInspectionManagementById(string inspectionManagementId)
        {
            return Funs.DB.View_InspectionManagement.FirstOrDefault(e => e.InspectionId == inspectionManagementId);
        }
    }
}