using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BLL
{
    /// <summary>
    /// 无损检测管理
    /// </summary>
    public static class NondestructiveTestService
    {
        public static Model.SGGLDB db = Funs.DB;

        /// <summary>
        /// 记录数
        /// </summary>
        private static int count
        {
            get;
            set;
        }

        /// <summary>
        /// 获取分页列表数
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="UnitWorkId"></param>
        /// <param name="unitId"></param>
        /// <param name="welderCode"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="startRowIndex"></param>
        /// <param name="maximumRows"></param>
        /// <returns></returns>
        public static IEnumerable GetListData(string projectId, string UnitWorkId, string unitId, string welderCode, string startDate, string endDate, int startRowIndex, int maximumRows)
        {
            Model.SGGLDB db = Funs.DB;
            IQueryable<Model.ProcessControl_NondestructiveTest> q = from x in db.ProcessControl_NondestructiveTest
                                                                    where x.ProjectId == projectId
                                                                    orderby x.NondestructiveTestDate descending
                                                                    select x;
            if (UnitWorkId != "0")
            {
                q = q.Where(e => e.UnitWorkId == UnitWorkId);
            }
            if (unitId != "0")
            {
                q = q.Where(e => e.UnitId == unitId);
            }
            if (!string.IsNullOrEmpty(welderCode))
            {
                q = q.Where(e => e.WelderCode.Contains(welderCode));
            }
            if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                q = q.Where(e => e.NondestructiveTestDate >= Funs.GetNewDateTime(startDate) && e.NondestructiveTestDate <= Funs.GetNewDateTime(endDate));
            }
            count = q.Count();
            if (count == 0)
            {
                return new object[] { "" };
            }
            return from x in q.Skip(startRowIndex).Take(maximumRows)
                   select new
                   {
                       x.NondestructiveTestId,
                       x.ProjectId,
                       InstallationName = (from y in db.WBS_UnitWork where y.UnitWorkId == x.UnitWorkId select y.UnitWorkName).FirstOrDefault(),
                       UnitName = (from y in db.Base_Unit where y.UnitId == x.UnitId select y.UnitName).FirstOrDefault(),
                       //ProfessionalName = (from y in db.WBS_CNProfessional where y.CNProfessionalId == x.CNProfessionalId select y.ProfessionalName).FirstOrDefault(),
                       x.WelderCode,
                       x.NondestructiveTestCode,
                       x.PieceCount,
                       x.OnceQualifiedCount,
                       x.NondestructiveTestDate,
                       InspectionAreaName = (from y in db.Base_InspectionArea where y.InspectionAreaId == x.InspectionAreaId select y.InspectionAreaName).FirstOrDefault(),
                   };
        }

        /// <summary>
        /// 获取分页列表数
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="UnitWorkId"></param>
        /// <param name="unitId"></param>
        /// <param name="welderCode"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static int GetListCount(string projectId, string UnitWorkId, string unitId, string welderCode, string startDate, string endDate)
        {
            return count;
        }

        /// <summary>
        /// 根据主键获取无损检测管理
        /// </summary>
        /// <param name="nondestructiveTestId"></param>
        /// <returns></returns>
        public static Model.ProcessControl_NondestructiveTest GetNondestructiveTestById(string nondestructiveTestId)
        {
            return Funs.DB.ProcessControl_NondestructiveTest.FirstOrDefault(e => e.NondestructiveTestId == nondestructiveTestId);
        }

        /// <summary>
        /// 添加无损检测管理
        /// </summary>
        /// <param name="nondestructiveTest"></param>
        public static void AddNondestructiveTest(Model.ProcessControl_NondestructiveTest nondestructiveTest)
        {
            Model.SGGLDB db = Funs.DB;
            Model.ProcessControl_NondestructiveTest newNondestructiveTest = new Model.ProcessControl_NondestructiveTest();
            newNondestructiveTest.NondestructiveTestId = nondestructiveTest.NondestructiveTestId;
            newNondestructiveTest.ProjectId = nondestructiveTest.ProjectId;
            newNondestructiveTest.UnitWorkId = nondestructiveTest.UnitWorkId;
            newNondestructiveTest.UnitId = nondestructiveTest.UnitId;
            //newNondestructiveTest.CNProfessionalId = nondestructiveTest.CNProfessionalId;
            newNondestructiveTest.WelderCode = nondestructiveTest.WelderCode;
            newNondestructiveTest.NondestructiveTestCode = nondestructiveTest.NondestructiveTestCode;
            newNondestructiveTest.PieceCount = nondestructiveTest.PieceCount;
            newNondestructiveTest.OnceQualifiedCount = nondestructiveTest.OnceQualifiedCount;
            newNondestructiveTest.NondestructiveTestDate = nondestructiveTest.NondestructiveTestDate;
            newNondestructiveTest.WED_ID = nondestructiveTest.WED_ID;
            newNondestructiveTest.STE_ID = nondestructiveTest.STE_ID;
            newNondestructiveTest.CompileMan = nondestructiveTest.CompileMan;
            newNondestructiveTest.InspectionAreaId = nondestructiveTest.InspectionAreaId;

            db.ProcessControl_NondestructiveTest.InsertOnSubmit(newNondestructiveTest);
            db.SubmitChanges();
        }

        /// <summary>
        /// 修改无损检测管理
        /// </summary>
        /// <param name="nondestructiveTest"></param>
        public static void UpdateNondestructiveTest(Model.ProcessControl_NondestructiveTest nondestructiveTest)
        {
            Model.SGGLDB db = Funs.DB;
            Model.ProcessControl_NondestructiveTest newNondestructiveTest = db.ProcessControl_NondestructiveTest.FirstOrDefault(e => e.NondestructiveTestId == nondestructiveTest.NondestructiveTestId);
            if (newNondestructiveTest != null)
            {
                newNondestructiveTest.ProjectId = nondestructiveTest.ProjectId;
                newNondestructiveTest.UnitWorkId = nondestructiveTest.UnitWorkId;
                newNondestructiveTest.UnitId = nondestructiveTest.UnitId;
                //newNondestructiveTest.CNProfessionalId = nondestructiveTest.CNProfessionalId;
                newNondestructiveTest.WelderCode = nondestructiveTest.WelderCode;
                newNondestructiveTest.NondestructiveTestCode = nondestructiveTest.NondestructiveTestCode;
                newNondestructiveTest.PieceCount = nondestructiveTest.PieceCount;
                newNondestructiveTest.OnceQualifiedCount = nondestructiveTest.OnceQualifiedCount;
                newNondestructiveTest.NondestructiveTestDate = nondestructiveTest.NondestructiveTestDate;
                newNondestructiveTest.WED_ID = nondestructiveTest.WED_ID;
                newNondestructiveTest.STE_ID = nondestructiveTest.STE_ID;
                newNondestructiveTest.InspectionAreaId = nondestructiveTest.InspectionAreaId;

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 根据主键删除无损检测管理
        /// </summary>
        /// <param name="nondestructiveTestId"></param>
        public static void DeleteNondestructiveTest(string nondestructiveTestId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.ProcessControl_NondestructiveTest nondestructiveTest = db.ProcessControl_NondestructiveTest.FirstOrDefault(e => e.NondestructiveTestId == nondestructiveTestId);
            if (nondestructiveTest != null)
            {
                db.ProcessControl_NondestructiveTest.DeleteOnSubmit(nondestructiveTest);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 根据单位获取对应时间内的集合
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="unitId"></param>
        /// <param name="startDate"></param>
        /// <param name="SoptDate"></param>
        /// <returns></returns>
        public static List<Model.ProcessControl_NondestructiveTest> GetNondestructiveTestListByUnitIdAndDate(string projectId, string unitId, DateTime startDate, DateTime SoptDate)
        {
            List<Model.ProcessControl_NondestructiveTest> NondestructiveTestList = (from x in Funs.DB.ProcessControl_NondestructiveTest where x.ProjectId == projectId select x).ToList();
            NondestructiveTestList = (from x in NondestructiveTestList where x.UnitId == unitId select x).ToList();
            if (startDate != null && SoptDate != null)
            {
                NondestructiveTestList = (from x in NondestructiveTestList where x.NondestructiveTestDate >= startDate && x.NondestructiveTestDate <= SoptDate select x).ToList();
            }

            return NondestructiveTestList;
        }
    }
}