using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    public class SpotCheckDetailService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 获取实体验收记录明细
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable getListData(string SpotCheckCode)
        {
            return from x in db.Check_SpotCheckDetail
                   where x.SpotCheckCode == SpotCheckCode
                   select new
                   {
                       x.SpotCheckDetailId,
                       x.SpotCheckCode,
                       x.ControlItemAndCycleId,
                       x.IsOnesOK,
                       x.IsOK,
                       x.ConfirmDate,
                   };
        }
        /// 
        /// 添加实体验收记录明细
        /// 
        /// 
        public static void AddSpotCheckDetail(Model.Check_SpotCheckDetail SpotCheckDetail)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Check_SpotCheckDetail newSpotCheckDetail = new Model.Check_SpotCheckDetail();
            newSpotCheckDetail.SpotCheckDetailId = SpotCheckDetail.SpotCheckDetailId;
            newSpotCheckDetail.SpotCheckCode = SpotCheckDetail.SpotCheckCode;
            newSpotCheckDetail.ControlItemAndCycleId = SpotCheckDetail.ControlItemAndCycleId;
            newSpotCheckDetail.IsOnesOK = SpotCheckDetail.IsOnesOK;
            newSpotCheckDetail.IsOK = SpotCheckDetail.IsOK;
            newSpotCheckDetail.ConfirmDate = SpotCheckDetail.ConfirmDate;
            newSpotCheckDetail.RectifyDescription = SpotCheckDetail.RectifyDescription;
            newSpotCheckDetail.CreateDate = SpotCheckDetail.CreateDate;
            db.Check_SpotCheckDetail.InsertOnSubmit(newSpotCheckDetail);
            db.SubmitChanges();
        }
        public static void AddSpotCheckDetailForApi(Model.Check_SpotCheckDetail SpotCheckDetail)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.Check_SpotCheckDetail newSpotCheckDetail = new Model.Check_SpotCheckDetail();
                newSpotCheckDetail.SpotCheckDetailId = SpotCheckDetail.SpotCheckDetailId;
                newSpotCheckDetail.SpotCheckCode = SpotCheckDetail.SpotCheckCode;
                newSpotCheckDetail.ControlItemAndCycleId = SpotCheckDetail.ControlItemAndCycleId;
                newSpotCheckDetail.IsOnesOK = SpotCheckDetail.IsOnesOK;
                newSpotCheckDetail.IsOK = SpotCheckDetail.IsOK;
                newSpotCheckDetail.ConfirmDate = SpotCheckDetail.ConfirmDate;
                newSpotCheckDetail.RectifyDescription = SpotCheckDetail.RectifyDescription;
                newSpotCheckDetail.CreateDate = SpotCheckDetail.CreateDate;
                db.Check_SpotCheckDetail.InsertOnSubmit(newSpotCheckDetail);
                db.SubmitChanges();
            }
        }
        /// 
        /// 修改实体验收记录明细
        /// 
        /// 
        public static void UpdateSpotCheckDetail(Model.Check_SpotCheckDetail SpotCheckDetail)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Check_SpotCheckDetail newSpotCheckDetail = db.Check_SpotCheckDetail.First(e => e.SpotCheckDetailId == SpotCheckDetail.SpotCheckDetailId);
            newSpotCheckDetail.SpotCheckCode = SpotCheckDetail.SpotCheckCode;
            newSpotCheckDetail.ControlItemAndCycleId = SpotCheckDetail.ControlItemAndCycleId;
            newSpotCheckDetail.IsOnesOK = SpotCheckDetail.IsOnesOK;
            newSpotCheckDetail.IsOK = SpotCheckDetail.IsOK;
            newSpotCheckDetail.ConfirmDate = SpotCheckDetail.ConfirmDate;
            newSpotCheckDetail.RectifyDescription = SpotCheckDetail.RectifyDescription;
            newSpotCheckDetail.IsDataOK = SpotCheckDetail.IsDataOK;
            newSpotCheckDetail.DataConfirmDate = SpotCheckDetail.DataConfirmDate;
            newSpotCheckDetail.State = SpotCheckDetail.State;
            newSpotCheckDetail.HandleMan = SpotCheckDetail.HandleMan;
            newSpotCheckDetail.IsShow = SpotCheckDetail.IsShow;
            db.SubmitChanges();
        }
        /// 
        /// 根据实体验收记录明细Id删除一个实体验收记录信息明细
        /// 
        /// 
        public static void DeleteSpotCheckDetail(string SpotCheckDetailId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Check_SpotCheckDetail SpotCheckDetail = db.Check_SpotCheckDetail.First(e => e.SpotCheckDetailId == SpotCheckDetailId);
            db.Check_SpotCheckDetail.DeleteOnSubmit(SpotCheckDetail);
            db.SubmitChanges();
        }
        public static void DeleteSpotCheckDetailForApi(string SpotCheckDetailId)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.Check_SpotCheckDetail SpotCheckDetail = db.Check_SpotCheckDetail.First(e => e.SpotCheckDetailId == SpotCheckDetailId);
                db.Check_SpotCheckDetail.DeleteOnSubmit(SpotCheckDetail);
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据实体验收记录主表删除所有实体验收记录信息明细
        /// 
        /// 
        public static void DeleteAllSpotCheckDetail(string SpotCheckCode)
        {
            Model.SGGLDB db = Funs.DB;
            var q = (from x in db.Check_SpotCheckDetail where x.SpotCheckCode == SpotCheckCode select x).ToList();
            db.Check_SpotCheckDetail.DeleteAllOnSubmit(q);
            db.SubmitChanges();
        }
        /// 
        /// 根据实体验收记录明细Id获取一个实体验收记录信息明细
        /// 
        /// 
        public static Model.Check_SpotCheckDetail GetSpotCheckDetail(string SpotCheckDetailId)
        {
            return Funs.DB.Check_SpotCheckDetail.FirstOrDefault(e => e.SpotCheckDetailId == SpotCheckDetailId);
        }
        /// 
        /// 根据实体验收记录Id获取一个实体验收记录信息明细
        /// 
        /// 
        public static Model.Check_SpotCheckDetail GetSpotCheckDetailBySoptCheckCode(string SpotCheckCode)
        {
            return Funs.DB.Check_SpotCheckDetail.FirstOrDefault(e => e.SpotCheckCode == SpotCheckCode);
        }
        /// 
        /// 根据实体验收记录Id获取一个不合格实体验收记录信息明细
        /// 
        /// 
        public static Model.Check_SpotCheckDetail GetNotOKSpotCheckDetailBySoptCheckCode(string SpotCheckCode)
        {
            return Funs.DB.Check_SpotCheckDetail.FirstOrDefault(e => e.SpotCheckCode == SpotCheckCode && (e.IsOK == null || e.IsOK == false));
        }
        /// 
        /// 根据实体验收记录主键获取所有实体验收记录信息明细
        /// 
        /// 
        public static List GetSpotCheckDetails(string SpotCheckCode)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                return (from x in db.Check_SpotCheckDetail
                        join y in db.WBS_ControlItemAndCycle
                        on x.ControlItemAndCycleId equals y.ControlItemAndCycleId
                        where x.SpotCheckCode == SpotCheckCode
                        orderby y.WorkPackageId
                        select x).ToList();
            }
        }
        public static List GetSpotCheckDetailsForApi(string SpotCheckCode)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                return (from x in db.Check_SpotCheckDetail
                        join y in db.WBS_ControlItemAndCycle
                        on x.ControlItemAndCycleId equals y.ControlItemAndCycleId
                        where x.SpotCheckCode == SpotCheckCode
                        orderby y.WorkPackageId
                        select x).ToList();
            }
        }
        /// 
        /// 根据实体验收记录主键获取所有实体合格实体验收记录信息明细
        /// 
        /// 
        public static List GetOKSpotCheckDetails(string SpotCheckCode)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                return (from x in db.Check_SpotCheckDetail
                        join y in db.WBS_ControlItemAndCycle
                        on x.ControlItemAndCycleId equals y.ControlItemAndCycleId
                        where x.SpotCheckCode == SpotCheckCode && x.IsOK == true
                        orderby y.WorkPackageId
                        select x).ToList();
            }
        }
        public static List GetOKSpotCheckDetailsForApi(string SpotCheckCode)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                return (from x in db.Check_SpotCheckDetail
                        join y in db.WBS_ControlItemAndCycle
                        on x.ControlItemAndCycleId equals y.ControlItemAndCycleId
                        where x.SpotCheckCode == SpotCheckCode && x.IsOK == true
                        orderby y.WorkPackageId
                        select x).ToList();
            }
        }
        /// 
        /// 根据实体验收记录主键获取所有实体合格并且有资料表格需要上传的实体验收记录信息明细
        /// 
        /// 
        public static List GetShowSpotCheckDetails(string SpotCheckCode)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                return (from x in db.Check_SpotCheckDetail
                        join y in db.WBS_ControlItemAndCycle
                        on x.ControlItemAndCycleId equals y.ControlItemAndCycleId
                        where x.SpotCheckCode == SpotCheckCode && x.IsOK == true && x.IsShow == true
                        orderby y.WorkPackageId
                        select x).ToList();
            }
        }
        /// 
        /// 根据单位工程Id获取所有工序验收记录信息明细
        /// 
        /// 
        public static List GetViewSpotCheckDetailsByUnitWorkIdAndDate(string unitWorkId, DateTime endDate, string isDataOK)
        {
            if (string.IsNullOrEmpty(isDataOK))   //不按资料合格统计
            {
                return (from x in Funs.DB.View_Check_SoptCheckDetail
                        where x.UnitWorkId == unitWorkId && x.IsOK == true && x.SpotCheckDate <= endDate
                        select x).ToList();
            }
            else    //按资料合格统计
            {
                return (from x in Funs.DB.View_Check_SoptCheckDetail
                        where x.UnitWorkId == unitWorkId && x.IsOK == true && (x.IsDataOK == "1" || x.IsDataOK == "2") && x.SpotCheckDate <= endDate
                        select x).ToList();
            }
        }
        /// 
        /// 根据项目Id获取所有工序验收记录信息明细
        /// 
        /// 
        public static List GetViewSpotCheckDetailsByProjectIdAndDate(string projectId, DateTime endDate, string isDataOK)
        {
            if (string.IsNullOrEmpty(isDataOK))   //不按资料合格统计
            {
                return (from x in Funs.DB.View_Check_SoptCheckDetail
                        where x.ProjectId == projectId && x.IsOK == true && x.SpotCheckDate <= endDate
                        select x).ToList();
            }
            else    //按资料合格统计
            {
                return (from x in Funs.DB.View_Check_SoptCheckDetail
                        where x.ProjectId == projectId && x.IsOK == true && (x.IsDataOK == "1" || x.IsDataOK == "2") && x.SpotCheckDate <= endDate
                        select x).ToList();
            }
        }
        /// 
        /// 根据工作包主键获取所有实体验收记录信息明细
        /// 
        /// 
        public static List GetSpotCheckDetailsByControlItemAndCycleId(string controlItemAndCycleId)
        {
            return (from x in Funs.DB.Check_SpotCheckDetail
                    where x.ControlItemAndCycleId == controlItemAndCycleId && x.IsOK == true
                    select x).ToList();
        }
        /// 
        /// 根据工作包主键获取所有工序验收记录信息明细
        /// 
        /// 
        public static List GetSpotCheckDetailsByControlItemAndCycleIds(List controlItemAndCycleId)
        {
            return (from x in Funs.DB.Check_SpotCheckDetail
                    where controlItemAndCycleId.Contains(x.ControlItemAndCycleId) && x.IsOK == true
                    select x).ToList();
        }
        /// 根据时间段获取实体验收记录明细集合
        /// 
        /// 开始时间
        /// 结束时间
        /// 工程类型
        public static List GetOKSpotCheckDetailListByTime(string projectId, DateTime startTime, DateTime endTime)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                return (from x in db.Check_SpotCheckDetail
                        join y in db.Check_SpotCheck
                        on x.SpotCheckCode equals y.SpotCheckCode
                        where y.ProjectId == projectId && y.SpotCheckDate >= startTime && y.SpotCheckDate < endTime && x.IsOK == true
                        select x).ToList();
            }
        }
        /// 
        /// 根据时间段获取实体验收记录明细集合
        /// 
        /// 开始时间
        /// 结束时间
        /// 工程类型
        public static List GetTotalOKSpotCheckDetailListByTime(string projectId, DateTime endTime)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                return (from x in db.Check_SpotCheckDetail
                        join y in db.Check_SpotCheck
                        on x.SpotCheckCode equals y.SpotCheckCode
                        where y.ProjectId == projectId && y.SpotCheckDate < endTime && x.IsOK == true
                        select x).ToList();
            }
        }
        public static void UpdateSpotCheckDetailForApi(Model.Check_SpotCheckDetail SpotCheckDetail)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.Check_SpotCheckDetail newSpotCheckDetail = db.Check_SpotCheckDetail.First(e => e.SpotCheckDetailId == SpotCheckDetail.SpotCheckDetailId);
                if (!string.IsNullOrEmpty(SpotCheckDetail.SpotCheckCode))
                    newSpotCheckDetail.SpotCheckCode = SpotCheckDetail.SpotCheckCode;
                if (!string.IsNullOrEmpty(SpotCheckDetail.ControlItemAndCycleId))
                    newSpotCheckDetail.ControlItemAndCycleId = SpotCheckDetail.ControlItemAndCycleId;
                if (SpotCheckDetail.IsOnesOK.HasValue)
                    newSpotCheckDetail.IsOnesOK = SpotCheckDetail.IsOnesOK;
                if (SpotCheckDetail.IsOK.HasValue)
                    newSpotCheckDetail.IsOK = SpotCheckDetail.IsOK;
                if (SpotCheckDetail.ConfirmDate.HasValue)
                    newSpotCheckDetail.ConfirmDate = SpotCheckDetail.ConfirmDate;
                if (!string.IsNullOrEmpty(SpotCheckDetail.RectifyDescription))
                    newSpotCheckDetail.RectifyDescription = SpotCheckDetail.RectifyDescription;
                if (SpotCheckDetail.IsShow.HasValue)
                    newSpotCheckDetail.IsShow = SpotCheckDetail.IsShow;
                if (!string.IsNullOrEmpty(SpotCheckDetail.State))
                    newSpotCheckDetail.State = SpotCheckDetail.State;
                newSpotCheckDetail.HandleMan = SpotCheckDetail.HandleMan;
                db.SubmitChanges();
            }
        }
        /// 质量验收(本月合格)
        /// 
        /// 开始时间
        /// 结束时间
        public static List GetOKSpotCheckDetailListByTime1(string projectId, DateTime startTime, DateTime endTime)
        {
            return (from x in Funs.DB.View_Check_SoptCheckDetail
                    where x.ProjectId == projectId && !x.ControlPoint.Contains("C") && x.SpotCheckDate >= startTime && x.SpotCheckDate < endTime && x.IsOK == true
                    select x).ToList();
        }
        /// 质量验收(本月全部)
        /// 
        /// 开始时间
        /// 结束时间
        public static List GetAllSpotCheckDetailListByTime(string projectId, DateTime startTime, DateTime endTime)
        {
            return (from x in Funs.DB.View_Check_SoptCheckDetail
                    where x.ProjectId == projectId && !x.ControlPoint.Contains("C") && x.SpotCheckDate >= startTime && x.SpotCheckDate < endTime && x.IsOK != null
                    select x).ToList();
        }
        /// 质量验收累计合格(累计合格)
        /// 
        /// 开始时间
        /// 结束时间
        public static List GetTotalOKSpotCheckDetailListByTime1(string projectId, DateTime endTime)
        {
            return (from x in Funs.DB.View_Check_SoptCheckDetail
                    where x.ProjectId == projectId && !x.ControlPoint.Contains("C") && x.SpotCheckDate < endTime && x.IsOK == true
                    select x).ToList();
        }
        /// 质量验收累计合格(累计全部)
        /// 
        /// 开始时间
        /// 结束时间
        public static List GetTotalAllSpotCheckDetailListByTime(string projectId, DateTime endTime)
        {
            return (from x in Funs.DB.View_Check_SoptCheckDetail
                    where x.ProjectId == projectId && !x.ControlPoint.Contains("C") && x.SpotCheckDate < endTime && x.IsOK != null
                    select x).ToList();
        }
        /// 
        /// 质量记录本月同步率(资料本月合格)
        /// 
        /// 开始时间
        /// 结束时间
        public static List GetMonthDataOkSpotCheckDetailListByTime(string projectId, DateTime startTime, DateTime endTime)
        {
            return (from x in Funs.DB.View_Check_SoptCheckDetail
                    where x.ProjectId == projectId && !x.ControlPoint.Contains("C") && x.SpotCheckDate >= startTime && x.SpotCheckDate < endTime && x.IsDataOK == "1"
                    select x).ToList();
        }
        /// 
        /// 质量记录本月同步率(资料累计合格)
        /// 
        /// 开始时间
        /// 结束时间
        /// 工程类型
        public static List GetAllDataOkSpotCheckDetailListByTime(string projectId, DateTime endTime)
        {
            return (from x in Funs.DB.View_Check_SoptCheckDetail
                    where x.ProjectId == projectId && !x.ControlPoint.Contains("C") && x.SpotCheckDate < endTime && x.IsDataOK == "1"
                    select x).ToList();
        }
        /// 
        /// 根据单位工程Id获取所有工序验收记录信息明细
        /// 
        /// 
        public static List GetViewSpotCheckDetailsByUnitWorkIdsAndDate(string[] unitWorkIds, DateTime endDate, string isDataOK)
        {
            if (string.IsNullOrEmpty(isDataOK))   //不按资料合格统计
            {
                return (from x in Funs.DB.View_Check_SoptCheckDetail
                        where unitWorkIds.Contains(x.UnitWorkId) && x.IsOK == true && x.SpotCheckDate <= endDate
                        select x).ToList();
            }
            else    //按资料合格统计
            {
                return (from x in Funs.DB.View_Check_SoptCheckDetail
                        where unitWorkIds.Contains(x.UnitWorkId) && x.IsOK == true && (x.IsDataOK == "1" || x.IsDataOK == "2") && x.SpotCheckDate <= endDate
                        select x).ToList();
            }
        }
    }
}