//客戶叫修主畫面-叫修案件讀取 20150818-刪除子單不讀-Hsiuyang        
        public IQueryable<WBSEventViewModel> Read()
        {
            List<WBSEventViewModel> ret = new List<WBSEventViewModel>();

            List<AlertDetailSettingViewModel> alertMList = new List<AlertDetailSettingViewModel>();
            var Aqry = db.AlertDetailSetting;
            foreach (AlertDetailSetting o in Aqry)
            {
                AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();
                a.EventClass = o.EventClass;
                a.AlertEvent = o.AlertEvent;
                a.FirstTime = o.FirstTime;
                alertMList.Add(a);
            }

            var qry = db.WBSEventM.Where(x => x.CaseClass != "M");
            foreach (WBSEventM m in qry)
            {
                var qry2 = db.WBSEventC.Where(x => x.MomCaseID == m.CaseID && x.EventStatus != 0 && x.EventStatus < 30);
                var qry3 = db.Customer.Where(x => x.CustomerRef == m.CustomerRef);
                //var qry4 = db.ProductClass.Where(x => x.)
                String Addr = "", SName = "", FName = "";
                foreach (Customer o in qry3)
                {
                    Addr = o.Addr;
                    SName = o.ShortName;
                    FName = o.FullName;
                }

                foreach (WBSEventC c in qry2)
                {
                                        
                    String ProductNote = "", ItemNote = "" ,QuestionNote = "";
                    var qry4 = db.ProductClass.Where(x => x.ProductClassID == c.ProductClass);
                    foreach (ProductClass o in qry4)
                    {                        
                        ProductNote = o.Note;                        
                    }

                    var qry5 = db.ItemClass.Where(x => x.ItemClassID == c.ItemClass);
                    foreach (ItemClass o in qry5)
                    {
                        ItemNote = o.Note;
                    }

                    var qry6 = db.QuestionClass.Where(x => x.QuestionClassID == c.QuestionClass);
                    foreach (QuestionClass o in qry6)
                    {
                        QuestionNote = o.Note;
                    }


                    WBSEventViewModel v = new WBSEventViewModel();
                    v.M_ID = m.CaseID;
                    v.C_ID = c.Num;
                    //v.WBSCaseNumberM = m.CaseClass + m.CaseDate + m.CaseCode;
                    v.WBSCaseNumberM = m.CaseClass + m.CaseDate + "-";
                    if (m.CaseCode < 10)
                        v.WBSCaseNumberM += "0000" + m.CaseCode.ToString();
                    else if (m.CaseCode < 100 && m.CaseCode >=10)
                        v.WBSCaseNumberM += "000" + m.CaseCode.ToString();
                    else if (m.CaseCode < 1000 && m.CaseCode >=100)
                        v.WBSCaseNumberM += "00" + m.CaseCode.ToString();
                    else if (m.CaseCode < 10000 && m.CaseCode >=1000)
                        v.WBSCaseNumberM += "0" + m.CaseCode.ToString();
                    else
                        v.WBSCaseNumberM += m.CaseCode.ToString();
                    v.WBSCaseNumberC = (c.ChildCaseID < 10) ? ("0"+c.ChildCaseID.ToString()) : c.ChildCaseID.ToString();
                    v.WBSCaseNumber = v.WBSCaseNumberM + "-" + v.WBSCaseNumberC;
                    v.EventClass = c.EventClass;
                    v.EventClassStr = c.EventClass + "-" + db.EventClass.Where(x => x.Class == c.EventClass).Select(x => x.Name).First(); ;
                    v.CustomerRef = m.CustomerRef;
                    v.CustomerAddr = Addr;
                    v.CustomerSName = SName;
                    v.CustomerUserID = m.CustomerUserID;
                    v.CustomerUserName = m.CustomerUserName;
                    v.CustomerUserPhone1 = m.CustomerUserPhone1;
                    v.CustomerUserPhone2 = m.CustomerUserPhone2;
                    v.CustomerUserPhone = m.CustomerUserPhone1;
                    if (m.CustomerUserPhone2 != null && m.CustomerUserPhone2 != "")
                        v.CustomerUserPhone += " /" + m.CustomerUserPhone2;
                    v.CustomerCaseNum = m.CustomerCaseNum;
                    v.ProductClass = c.ProductClass.ToString();
                    v.ProductNote = ProductNote;
                    v.ItemClass = c.ItemClass.ToString();
                    v.ItemNote = ItemNote;
                    v.QuestionClass = c.QuestionClass.ToString();
                    v.QuestionNote = QuestionNote;
                    v.QuestionNoteAll = ProductNote + "-" + ItemNote + "-" + QuestionNote;
                    v.QuestionGrade = c.QuestionGrade.ToString();
                    v.QuestionServiceHours = c.QuestionServiceHours.ToString();
                    v.QuestionDescription = c.QuestionDescription;

                    v.EventStatus = c.EventStatus;
                    v.EventStatusStr = (v.EventStatus == 0) ? "0" + v.EventStatus.ToString() : v.EventStatus.ToString();                    
                    v.EventStatusStr += "." + GetEventStatusStr(v.EventStatus);

                    v.HasActtachment = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 1).Count() > 0)
                        v.HasActtachment = "@";

                    v.HasAssignActtachment = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 2).Count() > 0)
                        v.HasAssignActtachment = "@";

                    v.HasKnowledge = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 3).Count() > 0)
                        v.HasKnowledge = "@";

                    v.HasHyperlink = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 9).Count() > 0)
                        v.HasHyperlink = "@";

                    v.QuestionCreateUserID = c.QuestionCreateUserID;
                    v.QuestionCreateDT = c.QuestionCreateDateTime;
                    v.ExpectArriveDT = c.ExpectArriveDateTime;
                    v.ExpectFinishDT = c.ExpectFinishDateTime;
                    v.ExpectArriveSW = c.ExpectArriveSW;

                    v.ExpectArriveDTStr = (c.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.ExpectFinishDTStr = (c.ExpectFinishDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    
                    if (c.ExpectArriveSW == "1")
                        v.ExpectArriveSWStr = "合約規定";
                    else if (c.ExpectArriveSW == "2")
                        v.ExpectArriveSWStr = "建議期望";
                    else if (c.ExpectArriveSW == "3")
                        v.ExpectArriveSWStr = "客戶指定";
                    else if (c.ExpectArriveSW == "4")
                        v.ExpectArriveSWStr = "自派新單";
                    else
                    {
                        v.ExpectArriveSWStr = "自行約定";
                        v.ExpectArriveDTStr = "請自行與客戶約定";
                        v.ExpectFinishDTStr = "請自行與客戶約定";
                    }  

                    v.DeleteMarkSW = c.DeleteMarkSW;
                    v.DeleteReason = c.DeleteReason;
                    v.DeleteMarkUserID = c.DeleteMarkUserID;
                    v.DeleteMarkDT = c.DeleteMarkDateTime;
                    v.CreateDT = c.CreateDateTime;
                    v.CreateD = c.CreateDateTime.Value.Date;
                    v.CreateDTStr = (c.CreateDateTime ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                    v.CreateYYYYStr = (c.CreateDateTime ?? DateTime.Now).ToString("yyyy");
                    v.CreateMMDDStr = (c.CreateDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                    v.CreateHHmmStr = (c.CreateDateTime ?? DateTime.Now).ToString("HH:mm");
                    v.CreateUserID = c.CreateUserID;
                    v.CreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.CreateUserID).Select(x => x.UserName).First();
                    v.ModifyDT = c.ModifyDateTime;
                    v.ModifyUserID = c.ModifyUserID;

                    v.ResponsibleUserName = "";
                    if (c.EventStatus >= 21)
                    {
                        if (c.ResponsibleID != null)
                        {
                            var RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == c.ResponsibleID).Select(x2 => x2.UserID).First();
                            v.ResponsibleUserName = c.ResponsibleClass.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                        }
                        //if (c.ResponsibleClass != null)
                        //    v.ResponsibleUserName = c.ResponsibleClass.UserData2.UserName;
                        else //線上結案
                            v.ResponsibleUserName = "";
                    }

                    v.MaintainNum = "";
                    int maintainNum = db.EventRelation.Where(x => x.CID == c.Num).Count();
                    if (maintainNum != 0)
                        v.MaintainNum = maintainNum.ToString();

                    //已開立時間
                    System.TimeSpan diff1 = DateTime.Now.Subtract(c.CreateDateTime ?? DateTime.Now);
                    //v.ServiceDT = Convert.ToDateTime(diff1.ToString());
                    int day = Int32.Parse(diff1.ToString("%d"));
                    int hour = Int32.Parse(diff1.ToString("%h"));
                    hour += day * 24;
                    v.CreatedTime = hour.ToString() + ":" + diff1.ToString(@"mm\:ss");

                    int temp = 0;                    
                    if (alertMList.Exists(x => x.EventClass == c.EventClass))
                        temp = alertMList.Find(x => x.EventClass == c.EventClass && x.AlertEvent == 1).FirstTime;
                    else
                        temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 1).FirstTime;
                    if (c.CreateDateTime.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                    {
                        //派單後超過"開單時間"N分未接受
                        if (c.EventStatus == 11)
                        {
                            v.AbnormalReason = "逾時未派工";
                        }                            
                        else if (c.EventStatus == 20)
                        {
                            v.AbnormalReason = "逾時未重派";
                        }
                        else if (c.EventStatus == 21)
                        {
                            v.AbnormalReason = "逾時未接受";
                        }
                    }
                    
             

                    List<AlertDetailSettingViewModel> AlertDetailList = alertSettingRepository.ReadAlertDetail(v.EventClass);
                    //v.OverTime = "";
                    //v.OverTimeDisplay = "none";
                    //TimeSpan duration = new TimeSpan(0, 0, AlertDetailList[0].FirstTime, 0);
                    
                    //if (diff1.CompareTo(duration) > 0)
                    //{
                    //    if (v.EventStatus < 30)
                    //    {
                    //        if (v.EventStatus == 11)
                    //            v.OverTime = "派工逾時";
                    //        else if (v.EventStatus == 21)
                    //            v.OverTime = "確認逾時";
                    //        else if (v.EventStatus == 20)
                    //            v.OverTime = "派工被拒";
                    //        v.OverTimeColor = "Red";
                    //        v.OverTimeDisplay = "";
                    //    }
                    //}
                    //if (v.EventStatus == 20)
                    //{
                    //    v.OverTime = "派工被拒";
                    //    v.OverTimeColor = "Red";
                    //    v.OverTimeDisplay = "";
                    //}

                    //v.DefaultUserName = m.Customer1.SubArea.ResponsibleClass.UserData2.UserName;
                    v.DefaultUserName = m.Customer1.SubArea.ResponsibleClass.Name + "-" + m.Customer1.SubArea.ResponsibleClass.UserData2.UserName;
                    v.ResponsibleArea = m.Customer1.SubArea.SubAreaID;
                    if (m.Customer1.SubArea.ResponsibleID2 != null)
                    {
                        v.DefaultUserName2 = m.Customer1.SubArea.ResponsibleClass1.Name + "-" + m.Customer1.SubArea.ResponsibleClass1.UserData2.UserName;
                        v.ResponsibleArea2 = m.Customer1.SubArea.SubAreaID;
                    }
                    
                    //v.DefaultUserName = db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                    //v.ResponsibleArea = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID1).Select(x2 => x2.SubAreaID).First();


                    ret.Add(v);
                }                
            }
            return ret.AsQueryable();
        }
        //案件檢視ByCase
        public IQueryable<WBSEventViewModel> EventSearchByCase(String CaseNum, String MNum, String CNum)
        {
            List<AlertDetailSettingViewModel> alertMList = new List<AlertDetailSettingViewModel>();
            var Aqry = db.AlertDetailSetting;
            foreach (AlertDetailSetting o in Aqry)
            {
                AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();
                a.EventClass = o.EventClass;
                a.AlertEvent = o.AlertEvent;
                a.FirstTime = o.FirstTime;
                alertMList.Add(a);
            }

            List<WBSEventViewModel> ret = new List<WBSEventViewModel>();

            string caseClass = MNum.Substring(0, 1);
            string caseDate = MNum.Substring(1, 8);
            string caseCodeStr = MNum.Substring(9, 5);
            int caseCode = int.Parse(caseCodeStr);

            var qry = db.WBSEventM.Where(x => x.CaseClass == caseClass && x.CaseDate == caseDate && x.CaseCode == caseCode);
            foreach (WBSEventM m in qry)
            {                
                var qry2 = db.WBSEventC.Where(x => x.MomCaseID == m.CaseID);
                if (CNum != "")
                {
                    int tmp = int.Parse(CNum);
                    qry2 = qry2.Where(x => x.ChildCaseID == tmp);
                }
                foreach (WBSEventC c in qry2)
                {
                    WBSEventViewModel v = new WBSEventViewModel();
                    v.M_ID = m.CaseID;
                    v.C_ID = c.Num;
                    //v.WBSCaseNumberM = m.CaseClass + m.CaseDate + m.CaseCode;
                    v.WBSCaseNumberM = m.CaseClass + m.CaseDate + "-";
                    if (m.CaseCode < 10)
                        v.WBSCaseNumberM += "0000" + m.CaseCode.ToString();
                    else if (m.CaseCode < 100 && m.CaseCode >= 10)
                        v.WBSCaseNumberM += "000" + m.CaseCode.ToString();
                    else if (m.CaseCode < 1000 && m.CaseCode >= 100)
                        v.WBSCaseNumberM += "00" + m.CaseCode.ToString();
                    else if (m.CaseCode < 10000 && m.CaseCode >= 1000)
                        v.WBSCaseNumberM += "0" + m.CaseCode.ToString();
                    else
                        v.WBSCaseNumberM += m.CaseCode.ToString();
                    v.WBSCaseNumberC = (c.ChildCaseID < 10) ? ("0" + c.ChildCaseID.ToString()) : c.ChildCaseID.ToString();
                    v.WBSCaseNumber = v.WBSCaseNumberM + "-" + v.WBSCaseNumberC;
                    v.EventClass = c.EventClass;
                    v.EventClassStr = v.EventClass + " : " + db.EventClass.AsEnumerable().Where(x => x.Class == c.EventClass).Select(x => x.Name).First();
                    v.EventStatus = c.EventStatus;

                    v.EventStatusStr = (v.EventStatus == 0) ? "0" + v.EventStatus.ToString() : v.EventStatus.ToString();
                    v.EventStatusStr += "." + GetEventStatusStr(v.EventStatus);

                    v.CustomerRef = m.CustomerRef;
                    v.CustomerAddr = m.Customer1.Addr;
                    if (m.CustomerAddr != "" && m.CustomerAddr != null)
                        v.CustomerAddr = m.CustomerAddr;
                    v.CustomerSName = m.Customer1.ShortName;
                    v.CustomerUserID = m.CustomerUserID;
                    v.CustomerUserName = m.CustomerUserName;
                    v.CustomerUserPhone1 = m.CustomerUserPhone1;
                    v.CustomerUserPhone = m.CustomerUserPhone1;
                    if (m.CustomerUserPhone2 != null && m.CustomerUserPhone2 != "")
                        v.CustomerUserPhone += " /" + m.CustomerUserPhone2;
                    v.CustomerUserPhone2 = m.CustomerUserPhone2;
                    v.ProductClass = c.ProductClass.ToString();
                    v.ProductNote = c.ProductClass1.Note;
                    v.ItemClass = c.ItemClass.ToString();
                    v.ItemNote = c.ItemClass1.Note;
                    v.QuestionClass = c.QuestionClass.ToString();
                    v.QuestionNote = c.QuestionClass1.Note;
                    v.QuestionNoteAll = c.ProductClass1.Note + "-" + c.ItemClass1.Note + "-" + c.QuestionClass1.Note;
                    v.QuestionGrade = c.QuestionGrade.ToString();
                    v.QuestionServiceHours = c.QuestionServiceHours.ToString();
                    v.QuestionDescription = c.QuestionDescription;

                    v.HasActtachment = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 1).Count() > 0)
                        v.HasActtachment = "@";
                    v.HasAssignActtachment = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 2).Count() > 0)
                        v.HasAssignActtachment = "@";

                    v.HasKnowledge = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 3).Count() > 0)
                        v.HasKnowledge = "@";

                    v.HasHyperlink = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 9).Count() > 0)
                        v.HasHyperlink = "@";

                    v.ExpectArriveDTStr = (c.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.ExpectFinishDTStr = (c.ExpectFinishDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    if (c.ExpectArriveSW == "1")
                        v.ExpectArriveSWStr = "合約規定";
                    else if (c.ExpectArriveSW == "2")
                        v.ExpectArriveSWStr = "建議期望";
                    else if (c.ExpectArriveSW == "3")
                        v.ExpectArriveSWStr = "客戶指定";
                    else if (c.ExpectArriveSW == "4")
                        v.ExpectArriveSWStr = "自派新單";
                    else
                    {
                        v.ExpectArriveSWStr = "自行約定";
                        v.ExpectArriveDTStr = "請自行與客戶約定";
                        v.ExpectFinishDTStr = "請自行與客戶約定";
                    }

                    v.QuestionCreateUserID = c.QuestionCreateUserID;
                    v.QuestionCreateDT = c.QuestionCreateDateTime;
                    v.ExpectArriveDT = c.ExpectArriveDateTime;
                    v.ExpectFinishDT = c.ExpectFinishDateTime;
                    v.ExpectArriveSW = c.ExpectArriveSW;
                    v.DeleteMarkSW = c.DeleteMarkSW;
                    v.DeleteReason = c.DeleteReason;
                    v.DeleteMarkUserID = c.DeleteMarkUserID;
                    v.DeleteMarkDT = c.DeleteMarkDateTime;
                    v.CreateDT = c.CreateDateTime;
                    v.CreateDTStr = (c.CreateDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                    v.CreateYYYYStr = c.CreateDateTime.Value.Year.ToString();
                    v.CreateUserID = c.CreateUserID;
                    v.CreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.CreateUserID).Select(x => x.UserName).First();
                    v.ModifyDT = c.ModifyDateTime;
                    if (c.ModifyDateTime != null)
                        v.ChgDTStr = c.ModifyDateTime.Value.ToString("MM/dd HH:mm");
                    v.ModifyUserID = c.ModifyUserID;
                    if (c.AssignDT != null)
                        v.AssignDTStr = c.AssignDT.Value.ToString("MM/dd HH:mm");
                    if (c.AssignUserID != null)
                        v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.AssignUserID).Select(x => x.UserName).First();
                    if (c.ResponsibleID != null)
                        v.ResponsibleUserName = c.ResponsibleClass.Name + "-" + c.ResponsibleClass.UserData2.UserName;
                    if (c.RespondSW == "Y")
                        v.ConfirmDTStr = c.RespondDT.Value.ToString("MM/dd HH:mm");
                    else
                        v.ConfirmDTStr = "";

                    if (c.EventStatus >= 41)
                    {
                        if (c.ServiceModifyUserID != null)
                            v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.ServiceModifyUserID).Select(x => x.UserName).First();
                        else if (c.ServiceCreateUserID != null)
                            v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.ServiceCreateUserID).Select(x => x.UserName).First();
                        else
                            v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.CreateUserID).Select(x => x.UserName).First();
                    }

                    if (c.ServiceArriveSysDT != null)
                    {
                        v.ArriveDTStr = c.ServiceArriveDT.Value.ToString("MM/dd HH:mm");
                        v.ServiceArriveDT = c.ServiceArriveDT;
                        v.ArriveSysDTStr = c.ServiceArriveSysDT.Value.ToString("MM/dd HH:mm");
                    }
                    if (c.QuestionFinishSysDT != null)
                    {
                        v.SolveDTStr = c.QuestionFinishDT.Value.ToString("MM/dd HH:mm");
                        v.SolveSysDTStr = c.QuestionFinishSysDT.Value.ToString("MM/dd HH:mm");
                        v.QuestionFinishDT = c.QuestionFinishDT;
                    }

                    if (c.ExpectArriveDateTime != null)
                        v.ExArriveDTStr = c.ExpectArriveDateTime.Value.ToString("MM/dd HH:mm");
                    if (c.ExpectFinishDateTime != null)
                        v.ExFinishDTStr = c.ExpectFinishDateTime.Value.ToString("MM/dd HH:mm");
                    if (c.ScheduleStartDT != null)
                    {
                        v.SArriveDTStr = c.ScheduleStartDT.Value.ToString("MM/dd HH:mm");
                        v.ScheduleStartDT = c.ScheduleStartDT;
                    }
                    v.ServiceLeaveDT = c.ServiceLeaveDT;
                    if (c.WBSEventM.CloseDT != null)
                    {
                        v.CloseDTStr = c.WBSEventM.CloseDT.Value.ToString("MM/dd HH:mm");
                        v.EventStatusStr = "99.已結案";
                    }
                    v.MaintainNum = "";
                    int maintainNum = db.EventRelation.Where(x => x.CID == c.Num).Count();
                    if (maintainNum != 0)
                        v.MaintainNum = maintainNum.ToString();

                    v.RespondReason = c.RespondReason;
                    if (m.Customer1.SubArea.ResponsibleID2 != null)
                    {
                        v.DefaultUserName2 = m.Customer1.SubArea.ResponsibleClass1.Name + "-" + m.Customer1.SubArea.ResponsibleClass1.UserData2.UserName;                        
                    }
                    v.CGroupSName = c.WBSEventM.Customer1.CustomerGroup.CGroupID + " : " + c.WBSEventM.Customer1.CustomerGroup.CGroupShortName;

                    //已開立時間
                    System.TimeSpan diff1 = DateTime.Now.Subtract(c.CreateDateTime ?? DateTime.Now);                    
                    int day = Int32.Parse(diff1.ToString("%d"));
                    int hour = Int32.Parse(diff1.ToString("%h"));
                    hour += day * 24;
                    v.CreatedTime = hour.ToString() + ":" + diff1.ToString(@"mm\:ss");

                    TimeSpan duration = new TimeSpan(0, 0, settingRepository.GetSetting().FirstOverTimeM, 0);
                    v.OverTime = "";
                    if (diff1.CompareTo(duration) > 0)
                        v.OverTime = "逾時";

                    v.DefaultUserName = m.Customer1.SubArea.ResponsibleClass.Name + "-" + m.Customer1.SubArea.ResponsibleClass.UserData2.UserName;
                    v.ResponsibleArea = m.Customer1.SubArea.SubAreaID;                    
                    v.AreaID = m.Customer1.SubArea.AreaID;
                    v.AreaStr = m.Customer1.SubArea.Area.Name;

                    v.AbnormalReason = isAbnormal(c, alertMList);

                    ret.Add(v);
                }
            }

            return ret.AsQueryable();
        }
        //我的工作(type: 1-未解決案件 2-異常案件 3-與我相關的案件 4-最近異動的案件 5-實際執行人已解決未結案案件 6-所屬區域大類案件)
        public IQueryable<WBSEventViewModel> MyJobSearch(String type, String dateClass, DateTime startDate, DateTime endDate, String uid, String isClosed)
        {
            string roleid = db.UserData.Find(uid).RoleID;
            List<WBSEventViewModel> ret = new List<WBSEventViewModel>();
            List<String> areaIDList = new List<String>();
            List<AlertDetailSettingViewModel> alertMList = new List<AlertDetailSettingViewModel>();
            if (type.Equals("2")) {
                var Aqry = db.AlertDetailSetting;
                foreach (AlertDetailSetting o in Aqry) {
                    AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();
                    a.EventClass = o.EventClass;
                    a.AlertEvent = o.AlertEvent;
                    a.FirstTime = o.FirstTime;
                    alertMList.Add(a);
                }
            }
            if(type.Equals("6")){
                var qry3 = db.SubArea.Where(x=>x.ResponsibleClass.UserID == uid);
                foreach (SubArea o in qry3) {                    
                    areaIDList.Add(o.AreaID);
                }
            }


            var qry = db.WBSEventM.Where(x => x.CaseID != 0);

            foreach (WBSEventM m in qry) {
                //var qry2 = db.WBSEventC.Where(x => x.MomCaseID == m.CaseID && x.EventStatus != 0 && x.EventStatus < 30);
                var qry2 = db.WBSEventC.Where(x => x.MomCaseID == m.CaseID);

                //qry2 = qry2.Where(x => x.EventStatus >= 11 && x.EventStatus < 50);
                if (type.Equals("1"))   //未解決
                {
                    qry2 = qry2.Where(x => x.WBSEventM.CaseMStatus != "9" && x.WBSEventM.CaseMStatus != "0" && x.EventStatus >= 11 && x.EventStatus < 50);
                    if (roleid != "99" && roleid != "10")
                        qry2 = qry2.Where(x => x.ResponsibleClass.UserID == uid);
                }
                else if (type.Equals("2"))  //異常
                {
                    qry2 = qry2.Where(x => x.WBSEventM.CaseMStatus != "9" && x.WBSEventM.CaseMStatus != "0" && x.EventStatus >= 11 && x.EventStatus <= 50);
                    if (roleid != "99" && roleid != "10")
                        qry2 = qry2.Where(x => x.ResponsibleClass.UserID == uid);
                }
                else if (type.Equals("3"))  //與我相關
                {
                    qry2 = qry2.Where(x => x.WBSEventM.CaseMStatus != "9" && x.WBSEventM.CaseMStatus != "0" && x.EventStatus >= 11 && x.EventStatus <= 50);
                    if (roleid != "99" && roleid != "10")
                        qry2 = qry2.Where(x => x.CreateUserID == uid || x.AssignUserID == uid || x.ServiceCreateUserID == uid || x.ResponsibleClass.UserID == uid || x.WBSEventM.Customer1.SubArea.ResponsibleClass.UserID == uid);
                }
                else if (type.Equals("4"))  //最近異動
                {
                    //qry2 = qry2.Where(x => x.WBSEventM.CaseMStatus != "9" && x.WBSEventM.CaseMStatus != "0");
                    if (roleid != "99" && roleid != "10")
                        qry2 = qry2.Where(x => x.ModifyUserID == uid);                    
                }
                else if (type.Equals("5")) //實際執行人已解決未結案
                {
                    qry2 = qry2.Where(x => x.WBSEventM.CaseMStatus != "9" && x.WBSEventM.CaseMStatus != "0" && x.EventStatus >= 46 && x.EventStatus <= 50);
                    if (roleid != "99" && roleid != "10")
                        qry2 = qry2.Where(x => x.ServiceCreateUserID == uid || x.ServiceModifyUserID == uid);
                }
                else if (type.Equals("6"))  //所屬區域
                {
                    qry2 = qry2.Where(x => x.WBSEventM.CaseMStatus != "9" && x.WBSEventM.CaseMStatus != "0");
                    if(isClosed == "true") //已解決未結案
                        qry2 = qry2.Where(x => x.EventStatus >= 46 && x.EventStatus <= 50);
                    else
                        qry2 = qry2.Where(x => x.EventStatus >= 11 && x.EventStatus < 50);
                    //String AreaID = GetCustomerArea()
                    //if (roleid != "99" && roleid != "10")
                       
                }

                


                String Addr = "", SName = "", FName = "";
                Addr = m.Customer1.Addr;
                SName = m.Customer1.ShortName;
                FName = m.Customer1.FullName;

                if (dateClass != null && dateClass != "") {
                    switch (dateClass) {
                        case "1":   //開單日期
                            qry2 = qry2.Where(x => x.CreateDateTime.Value.CompareTo(startDate) >= 0 && x.CreateDateTime.Value.CompareTo(endDate) <= 0);
                            break;
                        case "2":   //派工日期
                            qry2 = qry2.Where(x => x.AssignDT.Value.CompareTo(startDate) >= 0 && x.AssignDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "3":   //接受日期
                            qry2 = qry2.Where(x => x.RespondDT.Value.CompareTo(startDate) >= 0 && x.RespondDT.Value.CompareTo(endDate) <= 0 && x.RespondSW == "Y");
                            break;
                        case "4":   //到達日期
                            qry2 = qry2.Where(x => x.ServiceArriveDT.Value.CompareTo(startDate) >= 0 && x.ServiceArriveDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "5":   //解決日期
                            qry2 = qry2.Where(x => x.QuestionFinishDT.Value.CompareTo(startDate) >= 0 && x.QuestionFinishDT.Value.CompareTo(endDate) <= 0 && x.WBSEventM.CloseDT == null);
                            //qry2 = qry2.Where(x => x.ServiceLeaveDT.Value.CompareTo(startDate) >= 0 && x.ServiceLeaveDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "6":   //結案日期
                            qry2 = qry2.Where(x => x.WBSEventM.CloseDT.Value.CompareTo(startDate) >= 0 && x.WBSEventM.CloseDT.Value.CompareTo(endDate) <= 0);
                            //qry2 = qry2.Where(x => x.QuestionFinishDT.Value.CompareTo(startDate) >= 0 && x.QuestionFinishDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "7":   //異動日期
                            qry2 = qry2.Where(x => x.ModifyDateTime.Value.CompareTo(startDate) >= 0 && x.ModifyDateTime.Value.CompareTo(endDate) <= 0);
                            break;
                        case "8":   //系統到達日期
                            qry2 = qry2.Where(x => x.ServiceArriveSysDT.Value.CompareTo(startDate) >= 0 && x.ServiceArriveSysDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "9":   //系統解決日期
                            qry2 = qry2.Where(x => x.QuestionFinishSysDT.Value.CompareTo(startDate) >= 0 && x.QuestionFinishSysDT.Value.CompareTo(endDate) <= 0 && x.WBSEventM.CloseDT == null);
                            break;
                    }                    
                }

                foreach (WBSEventC c in qry2) {
                    //先判斷如果為區域大類且不是主管 此筆還要不要做
                    Boolean notSameArea = true;
                    if (type.Equals("6") && roleid != "99" && roleid != "10"){
                        foreach(string o in areaIDList){
                            if(c.WBSEventM.Customer1.SubArea.AreaID == o)
                                notSameArea = false;
                        }
                        if(notSameArea)
                            continue ;
                    }

                    WBSEventViewModel v = new WBSEventViewModel();
                    v.M_ID = m.CaseID;
                    v.C_ID = c.Num;
                    //v.WBSCaseNumberM = m.CaseClass + m.CaseDate + m.CaseCode;
                    v.WBSCaseNumberM = m.CaseClass + m.CaseDate + "-";
                    if (m.CaseCode < 10)
                        v.WBSCaseNumberM += "0000" + m.CaseCode.ToString();
                    else if (m.CaseCode < 100 && m.CaseCode >= 10)
                        v.WBSCaseNumberM += "000" + m.CaseCode.ToString();
                    else if (m.CaseCode < 1000 && m.CaseCode >= 100)
                        v.WBSCaseNumberM += "00" + m.CaseCode.ToString();
                    else if (m.CaseCode < 10000 && m.CaseCode >= 1000)
                        v.WBSCaseNumberM += "0" + m.CaseCode.ToString();
                    else
                        v.WBSCaseNumberM += m.CaseCode.ToString();
                    v.WBSCaseNumberC = (c.ChildCaseID < 10) ? ("0" + c.ChildCaseID.ToString()) : c.ChildCaseID.ToString();
                    v.WBSCaseNumber = v.WBSCaseNumberM + "-" + v.WBSCaseNumberC;
                    v.EventClass = c.EventClass;
                    v.EventClassStr = v.EventClass + " : " + db.EventClass.AsEnumerable().Where(x => x.Class == c.EventClass).Select(x => x.Name).First();
                    v.EventStatus = c.EventStatus;

                    v.EventStatusStr = (v.EventStatus == 0) ? "0" + v.EventStatus.ToString() : v.EventStatus.ToString();
                    v.EventStatusStr += "." + GetEventStatusStr(v.EventStatus);

                    v.CustomerSName = m.Customer1.ShortName;                    
                    v.CustomerUserName = m.CustomerUserName;
                    v.CustomerUserPhone1 = m.CustomerUserPhone1;
                    v.CustomerUserPhone = m.CustomerUserPhone1;
                    if (m.CustomerUserPhone2 != null && m.CustomerUserPhone2 != "")
                        v.CustomerUserPhone += " /" + m.CustomerUserPhone2;
                    v.CustomerUserPhone2 = m.CustomerUserPhone2;
                    
                    v.ProductNote = c.ProductClass1.Note;
                    
                    v.ItemNote = c.ItemClass1.Note;
                    
                    v.QuestionNote = c.QuestionClass1.Note;
                    
                    
                    v.QuestionDescription = c.QuestionDescription;

                    v.HasActtachment = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 1).Count() > 0)
                        v.HasActtachment = "@";
                    v.HasAssignActtachment = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 2).Count() > 0)
                        v.HasAssignActtachment = "@";

                    v.HasKnowledge = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 3).Count() > 0)
                        v.HasKnowledge = "@";

                    v.HasHyperlink = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 9).Count() > 0)
                        v.HasHyperlink = "@";

                    v.ExpectArriveDTStr = (c.ExpectArriveDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                    v.ExpectFinishDTStr = (c.ExpectFinishDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                    if (c.ExpectArriveSW == "1")
                        v.ExpectArriveSWStr = "合約規定";
                    else if (c.ExpectArriveSW == "2")
                        v.ExpectArriveSWStr = "建議期望";
                    else if (c.ExpectArriveSW == "3")
                        v.ExpectArriveSWStr = "客戶指定";
                    else if (c.ExpectArriveSW == "4")
                        v.ExpectArriveSWStr = "自派新單";
                    else
                    {
                        v.ExpectArriveSWStr = "自行約定";
                        v.ExpectArriveDTStr = "請自行與客戶約定";
                        v.ExpectFinishDTStr = "請自行與客戶約定";
                    }
                    
                    v.ExpectArriveDT = c.ExpectArriveDateTime;
                    v.ExpectFinishDT = c.ExpectFinishDateTime;
                    v.ExpectArriveSW = c.ExpectArriveSW;
                    //v.DeleteMarkSW = c.DeleteMarkSW;
                    //v.DeleteReason = c.DeleteReason;
                    //v.DeleteMarkUserID = c.DeleteMarkUserID;
                    //v.DeleteMarkDT = c.DeleteMarkDateTime;
                    v.CreateDT = c.CreateDateTime;
                    
                    
                    v.CreateUserID = c.CreateUserID;
                    v.CreateUserName = c.UserData.UserName;
                    v.ModifyDT = c.ModifyDateTime;                    
                    v.ModifyUserName = c.UserData2.UserName;
                    if (c.AssignDT != null)
                        v.AssignDTStr = c.AssignDT.Value.ToString("MM/dd HH:mm");
                    if (c.AssignUserID != null)
                        v.AssignUserName = c.UserData4.UserName;//db.UserData.AsEnumerable().Where(x => x.UserID == c.AssignUserID).Select(x => x.UserName).First();
                    if (c.ResponsibleID != null)
                        v.ResponsibleUserName = c.ResponsibleClass.Name + "-" + c.ResponsibleClass.UserData2.UserName;
                    

                    if (c.EventStatus >= 41)
                    {
                        if (c.ServiceModifyUserID != null)
                            v.ServiceUserName = c.UserData7.UserName;//db.UserData.AsEnumerable().Where(x => x.UserID == c.ServiceModifyUserID).Select(x => x.UserName).First();
                        else if (c.ServiceCreateUserID != null)
                            v.ServiceUserName = c.UserData8.UserName;//db.UserData.AsEnumerable().Where(x => x.UserID == c.ServiceCreateUserID).Select(x => x.UserName).First();
                        else
                            v.ServiceUserName = c.UserData.UserName;//db.UserData.AsEnumerable().Where(x => x.UserID == c.CreateUserID).Select(x => x.UserName).First();
                    }

                    if (c.ServiceArriveSysDT != null)
                        v.ServiceArriveDT = c.ServiceArriveDT;                    

                    if (c.ScheduleStartDT != null)
                        v.ScheduleStartDT = c.ScheduleStartDT;

                    if (c.ServiceLeaveSysDT != null)
                        v.ServiceLeaveDT = c.ServiceLeaveDT;

                    //v.CloseDTStr = c.QuestionFinishDT.Value.ToString("yyyy/MM/dd HH:mm");
                    //if (c.ServiceLeaveDT != null)
                    //    v.SolveDTStr = c.ServiceLeaveDT.Value.ToString("yyyy/MM/dd HH:mm");
                    if (c.WBSEventM.CloseDT != null)
                    {
                        v.CloseDTStr = c.WBSEventM.CloseDT.Value.ToString("MM/dd HH:mm");
                        v.EventStatusStr = "99.已結案";
                    }
                    v.MaintainNum = "";
                    int maintainNum = db.EventRelation.Where(x => x.CID == c.Num).Count();
                    if (maintainNum != 0)
                        v.MaintainNum = maintainNum.ToString();

                    v.RespondReason = c.RespondReason;
                    if (m.Customer1.SubArea.ResponsibleID2 != null)
                    {
                        v.DefaultUserName2 = m.Customer1.SubArea.ResponsibleClass1.Name + "-" + m.Customer1.SubArea.ResponsibleClass1.UserData2.UserName;
                        //v.ResponsibleArea2 = m.Customer1.SubArea.SubAreaID;
                    }



                    v.DefaultUserName = m.Customer1.SubArea.ResponsibleClass.Name + "-" + m.Customer1.SubArea.ResponsibleClass.UserData2.UserName;
                    //v.ResponsibleArea = m.Customer1.SubArea.SubAreaID;
                    //v.DefaultUserName = db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                    //v.ResponsibleArea = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID1).Select(x2 => x2.SubAreaID).First();
                    //v.AreaID = m.Customer1.SubArea.AreaID;
                    //v.AreaStr = m.Customer1.SubArea.Area.Name;

                    if (type.Equals("2")) 
                        v.AbnormalReason = isAbnormal(c, alertMList);
                    if (type.Equals("3"))
                    {
                        v.RelatedCauses = " ";
                        if(c.CreateUserID == uid)
                            v.RelatedCauses += " 開單";
                        if(c.AssignUserID == uid )
                            v.RelatedCauses += " 派工";
                        if(c.ServiceCreateUserID == uid )
                            v.RelatedCauses += " 實際執行人";
                        if(c.EventStatus >= 30)
                            if(c.ResponsibleClass.UserID == uid )
                                v.RelatedCauses += " 執行人";
                        if(m.Customer1.SubArea.ResponsibleClass.UserID == uid)
                            v.RelatedCauses += " 預設責任區人員";

                        
                    }
                    //AbnormalReason //異常狀態
                    if (type != "2")
                        ret.Add(v);
                    else {
                        if (v.AbnormalReason != "")
                            ret.Add(v);
                    }
                }
            }
            return ret.AsQueryable();
        }
        //手機-異常案件列表
        public List<WBSAssignViewModel> AbnormalMobileRead(String uid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            List<AlertDetailSettingViewModel> alertMList = new List<AlertDetailSettingViewModel>();            
            var Aqry = db.AlertDetailSetting;
            foreach (AlertDetailSetting o in Aqry) {
                AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();
                a.EventClass = o.EventClass;
                a.AlertEvent = o.AlertEvent;
                a.FirstTime = o.FirstTime;
                alertMList.Add(a);
            }
            var qryC = db.WBSEventC.Where(x => x.ResponsibleClass.UserID == uid);
            
            foreach (WBSEventC wbsC in qryC) {
                WBSAssignViewModel v = new WBSAssignViewModel();
                v.AbnormalReason = isAbnormal(wbsC, alertMList);
                if (v.AbnormalReason == "")
                    continue ;

                v.MCaseID = wbsC.WBSEventM.CaseID;
                v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName; 
                v.CCaseNum = wbsC.Num;
                v.QuestionDesc = wbsC.QuestionDescription;
                int temp2 = wbsC.EventStatus;
                                                
                v.EventStatusStr = GetEventStatusStr(wbsC.EventStatus);

                if(wbsC.WBSEventM.CaseMStatus == "9")
                    v.EventStatusStr = "已結案";

                ret.Add(v);
            }              

            return ret;
        }
        //案件檢視
        public IQueryable<WBSEventViewModel> EventSearch(String NotClose, String eventStatus, String eventClass, String userClass, String user, String areaID, String childCaseID, String CGroupSW, String CGroupID, String CSName, String dateClass, DateTime startDate, DateTime endDate, String Etype)
        {
            List<AlertDetailSettingViewModel> alertMList = new List<AlertDetailSettingViewModel>();            
            var Aqry = db.AlertDetailSetting;
            foreach (AlertDetailSetting o in Aqry)
            {
                AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();
                a.EventClass = o.EventClass;
                a.AlertEvent = o.AlertEvent;
                a.FirstTime = o.FirstTime;
                alertMList.Add(a);
            }            

            List<WBSEventViewModel> ret = new List<WBSEventViewModel>();
            endDate = endDate.AddDays(1);

            var qry = db.WBSEventM.Where(x=>x.CaseID!=0);

            //if (eventClass != null && eventClass != "")
            //{
            //    String temp = eventClass.First().ToString();
            //    qry = qry.Where(x => x.CaseClass == temp);
            //}

            if (areaID != null && areaID != "" && areaID != "12")   //12為不分區
            {
                qry = qry.Where(x => x.Customer1.SubArea.AreaID == areaID);
            }

            if (CGroupSW != null && CGroupSW != "")
            {
                qry = qry.Where(x => x.Customer1.GroupSW == CGroupSW);
            }

            if (CGroupID != null && CGroupID != "")
            {
                qry = qry.Where(x => x.Customer1.GroupID == CGroupID);
            }

            if (CSName != null && CSName != "")
            {
                int t = Int32.Parse(CSName);
                qry = qry.Where(x => x.CustomerRef == t);
            }

            //var qry = db.WBSEventM.Where(x => x.CaseClass == "C");
            foreach (WBSEventM m in qry)
            {
                
                var qry2 = db.WBSEventC.Where(x => x.MomCaseID == m.CaseID);
                
                if (eventClass != null && eventClass.Length > 1)
                {
                    qry2 = qry2.Where(x => x.EventClass == eventClass);
                }

                if (NotClose == "true")
                    qry2 = qry2.Where(x => x.EventStatus >= 11 && x.EventStatus < 50);

                if (Etype == "C02" || Etype == "C03")
                    qry2 = qry2.Where(x => x.EventStatus >= 30 && x.EventStatus < 50);

                if (eventStatus != null && eventStatus != "" && eventStatus != "99")
                {
                    int teventStatus = Int32.Parse(eventStatus);
                    //20160520改
                    qry2 = qry2.Where(x => x.EventStatus == teventStatus && x.WBSEventM.CaseMStatus != "9");
                }
                else if (eventStatus == "99")
                {
                    qry2 = qry2.Where(x => x.WBSEventM.CaseMStatus == "9");
                }

                if (userClass != null && userClass != "")
                {
                    if (user != null && user != "")
                    {
                        if (userClass == "1")       //開單人員
                            qry2 = qry2.Where(x => x.CreateUserID == user);
                        else if (userClass == "2")  //派工人員
                            qry2 = qry2.Where(x => x.AssignUserID == user);
                        else if (userClass == "3")  //責任區人員
                        {
                            int tempU = Int32.Parse(user);
                            qry2 = qry2.Where(x => x.WBSEventM.Customer1.SubArea.ResponsibleClass.ID == tempU); 
                        }
                        else if (userClass == "4")  //執行人
                        {
                            int tempU = Int32.Parse(user);
                            qry2 = qry2.Where(x => x.ResponsibleID == tempU);
                        }
                        else if (userClass == "5")  //實際執行人
                            qry2 = qry2.Where(x => x.ServiceCreateUserID == user);
                        else if (userClass == "6") //執行人或實際執行人
                        {
                            int tempU = Int32.Parse(user);
                            string uid = db.ResponsibleClass.Find(tempU).UserID;
                            qry2 = qry2.Where(x => (x.ServiceCreateUserID == uid && x.EventStatus >= 41) || (x.ResponsibleID == tempU && x.EventStatus <41));
                        }
                    }
                }
                
                if (dateClass != null && dateClass != "")
                {
                    switch(dateClass){
                        case "1":   //開單日期
                            qry2 = qry2.Where(x => x.CreateDateTime.Value.CompareTo(startDate) >= 0 && x.CreateDateTime.Value.CompareTo(endDate) <= 0);
                            break;
                        case "2":   //派工日期
                            qry2 = qry2.Where(x => x.AssignDT.Value.CompareTo(startDate) >= 0 && x.AssignDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "3":   //接受日期
                            qry2 = qry2.Where(x => x.RespondDT.Value.CompareTo(startDate) >= 0 && x.RespondDT.Value.CompareTo(endDate) <= 0 && x.RespondSW == "Y");
                            break;
                        case "4":   //到達日期
                            qry2 = qry2.Where(x => x.ServiceArriveDT.Value.CompareTo(startDate) >= 0 && x.ServiceArriveDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "5":   //解決日期
                            //qry2 = qry2.Where(x => x.ServiceLeaveDT.Value.CompareTo(startDate) >= 0 && x.ServiceLeaveDT.Value.CompareTo(endDate) <= 0);
                            qry2 = qry2.Where(x => x.QuestionFinishDT.Value.CompareTo(startDate) >= 0 && x.QuestionFinishDT.Value.CompareTo(endDate) <= 0 && x.WBSEventM.CloseDT == null);
                            break;
                        case "6":   //結案日期
                            qry2 = qry2.Where(x => x.WBSEventM.CloseDT.Value.CompareTo(startDate) >= 0 && x.WBSEventM.CloseDT.Value.CompareTo(endDate) <= 0);
                            //qry2 = qry2.Where(x => x.QuestionFinishDT.Value.CompareTo(startDate) >= 0 && x.QuestionFinishDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "7":   //異動日期
                            qry2 = qry2.Where(x => x.ModifyDateTime.Value.CompareTo(startDate) >= 0 && x.ModifyDateTime.Value.CompareTo(endDate) <= 0);
                            break;
                        case "8":   //應到達日期(期望)
                            qry2 = qry2.Where(x => x.ExpectArriveDateTime.Value.CompareTo(startDate) >= 0 && x.ExpectArriveDateTime.Value.CompareTo(endDate) <= 0);
                            break;
                        case "9":   //應完修日期(期望)
                            qry2 = qry2.Where(x => x.ExpectFinishDateTime.Value.CompareTo(startDate) >= 0 && x.ExpectFinishDateTime.Value.CompareTo(endDate) <= 0);
                            break;
                        case "10":   //預計到達日期
                            qry2 = qry2.Where(x => x.ScheduleStartDT.Value.CompareTo(startDate) >= 0 && x.ScheduleStartDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "11":   //系統到達日期
                            qry2 = qry2.Where(x => x.ServiceArriveSysDT.Value.CompareTo(startDate) >= 0 && x.ServiceArriveSysDT.Value.CompareTo(endDate) <= 0);
                            break;
                        case "12":   //系統解決日期
                            qry2 = qry2.Where(x => x.QuestionFinishSysDT.Value.CompareTo(startDate) >= 0 && x.QuestionFinishSysDT.Value.CompareTo(endDate) <= 0 && x.WBSEventM.CloseDT == null);
                            break;
                    }
                }

                if (childCaseID != null && childCaseID != "")
                {
                    if (childCaseID == "M")
                        qry2 = qry2.Where(x => x.ChildCaseID == 1);
                    else
                        qry2 = qry2.Where(x => x.ChildCaseID != 1);
                }
                foreach (WBSEventC c in qry2)
                {
                    WBSEventViewModel v = new WBSEventViewModel();
                    v.M_ID = m.CaseID;
                    v.C_ID = c.Num;
                    //v.WBSCaseNumberM = m.CaseClass + m.CaseDate + m.CaseCode;
                    v.WBSCaseNumberM = m.CaseClass + m.CaseDate + "-";
                    if (m.CaseCode < 10)
                        v.WBSCaseNumberM += "0000" + m.CaseCode.ToString();
                    else if (m.CaseCode < 100 && m.CaseCode >= 10)
                        v.WBSCaseNumberM += "000" + m.CaseCode.ToString();
                    else if (m.CaseCode < 1000 && m.CaseCode >= 100)
                        v.WBSCaseNumberM += "00" + m.CaseCode.ToString();
                    else if (m.CaseCode < 10000 && m.CaseCode >= 1000)
                        v.WBSCaseNumberM += "0" + m.CaseCode.ToString();
                    else
                        v.WBSCaseNumberM += m.CaseCode.ToString();
                    v.WBSCaseNumberC = (c.ChildCaseID < 10) ? ("0" + c.ChildCaseID.ToString()) : c.ChildCaseID.ToString();
                    v.WBSCaseNumber = v.WBSCaseNumberM + "-" + v.WBSCaseNumberC;
                    v.EventClass = c.EventClass;
                    v.EventClassStr = v.EventClass + " : " + db.EventClass.AsEnumerable().Where(x => x.Class == c.EventClass).Select(x => x.Name).First();
                    v.EventStatus = c.EventStatus;

                    v.EventStatusStr = (v.EventStatus == 0) ? "0" + v.EventStatus.ToString() : v.EventStatus.ToString();
                    v.EventStatusStr += "." + GetEventStatusStr(v.EventStatus);

                    v.CustomerRef = m.CustomerRef;
                    v.CustomerAddr = m.Customer1.Addr;
                    if (m.CustomerAddr != "" && m.CustomerAddr != null)
                        v.CustomerAddr = m.CustomerAddr;
                    v.CustomerSName = m.Customer1.ShortName;
                    v.CustomerUserID = m.CustomerUserID;
                    v.CustomerUserName = m.CustomerUserName;
                    v.CustomerUserPhone1 = m.CustomerUserPhone1;
                    v.CustomerUserPhone = m.CustomerUserPhone1;
                    if (m.CustomerUserPhone2 != null && m.CustomerUserPhone2 != "")
                        v.CustomerUserPhone += " /" + m.CustomerUserPhone2;
                    v.CustomerUserPhone2 = m.CustomerUserPhone2;
                    v.ProductClass = c.ProductClass.ToString();
                    v.ProductNote = c.ProductClass1.Note;
                    v.ItemClass = c.ItemClass.ToString();
                    v.ItemNote = c.ItemClass1.Note;
                    v.QuestionClass = c.QuestionClass.ToString();
                    v.QuestionNote = c.QuestionClass1.Note;
                    v.QuestionNoteAll = c.ProductClass1.Note + "-" + c.ItemClass1.Note + "-" + c.QuestionClass1.Note;
                    v.QuestionGrade = c.QuestionGrade.ToString();
                    v.QuestionServiceHours = c.QuestionServiceHours.ToString();
                    v.QuestionDescription = c.QuestionDescription;

                    v.HasActtachment = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 1).Count() > 0)
                        v.HasActtachment = "@";
                    v.HasAssignActtachment = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 2).Count() > 0)
                        v.HasAssignActtachment = "@";

                    v.HasKnowledge = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 3).Count() > 0)
                        v.HasKnowledge = "@";

                    v.HasHyperlink = "";
                    if (db.Files.Where(x => x.CID == c.Num && x.Class == 9).Count() > 0)
                        v.HasHyperlink = "@";

                    v.ExpectArriveDTStr = (c.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.ExpectFinishDTStr = (c.ExpectFinishDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    if (c.ExpectArriveSW == "1")
                        v.ExpectArriveSWStr = "合約規定";
                    else if (c.ExpectArriveSW == "2")
                        v.ExpectArriveSWStr = "建議期望";
                    else if (c.ExpectArriveSW == "3")
                        v.ExpectArriveSWStr = "客戶指定";
                    else if (c.ExpectArriveSW == "4")
                        v.ExpectArriveSWStr = "自派新單";
                    else
                    {
                        v.ExpectArriveSWStr = "自行約定";
                        v.ExpectArriveDTStr = "請自行與客戶約定";
                        v.ExpectFinishDTStr = "請自行與客戶約定";
                    } 

                    v.QuestionCreateUserID = c.QuestionCreateUserID;
                    v.QuestionCreateDT = c.QuestionCreateDateTime;
                    v.ExpectArriveDT = c.ExpectArriveDateTime;
                    v.ExpectFinishDT = c.ExpectFinishDateTime;
                    v.ExpectArriveSW = c.ExpectArriveSW;
                    v.DeleteMarkSW = c.DeleteMarkSW;
                    v.DeleteReason = c.DeleteReason;
                    v.DeleteMarkUserID = c.DeleteMarkUserID;
                    v.DeleteMarkDT = c.DeleteMarkDateTime;
                    v.CreateDT = c.CreateDateTime;
                    v.CreateDTStr = (c.CreateDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                    v.CreateYYYYStr = c.CreateDateTime.Value.Year.ToString();
                    v.CreateUserID = c.CreateUserID;
                    v.CreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.CreateUserID).Select(x => x.UserName).First();
                    v.ModifyDT = c.ModifyDateTime;
                    if (c.ModifyDateTime != null)
                        v.ChgDTStr = c.ModifyDateTime.Value.ToString("MM/dd HH:mm");
                    v.ModifyUserID = c.ModifyUserID;
                    if (c.AssignDT != null)
                        v.AssignDTStr = c.AssignDT.Value.ToString("MM/dd HH:mm");
                    if (c.AssignUserID != null)
                        v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.AssignUserID).Select(x => x.UserName).First();
                    if(c.ResponsibleID != null)
                        v.ResponsibleUserName = c.ResponsibleClass.Name + "-" + c.ResponsibleClass.UserData2.UserName;
                    if (c.RespondSW == "Y")
                        v.ConfirmDTStr = c.RespondDT.Value.ToString("MM/dd HH:mm");
                    else
                        v.ConfirmDTStr = "";

                    if (c.EventStatus >= 41)
                    {
                        if (c.ServiceModifyUserID != null)
                            v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.ServiceModifyUserID).Select(x => x.UserName).First();
                        else if (c.ServiceCreateUserID != null)
                            v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.ServiceCreateUserID).Select(x => x.UserName).First();
                        else
                            v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.CreateUserID).Select(x => x.UserName).First();
                    }

                    if (c.ServiceArriveSysDT != null)
                    {                        
                        v.ArriveDTStr = c.ServiceArriveDT.Value.ToString("MM/dd HH:mm");
                        v.ServiceArriveDT = c.ServiceArriveDT;
                        v.ArriveSysDTStr = c.ServiceArriveSysDT.Value.ToString("MM/dd HH:mm");
                    }
                    if (c.QuestionFinishSysDT != null)
                    {
                        v.SolveDTStr = c.QuestionFinishDT.Value.ToString("MM/dd HH:mm");
                        v.SolveSysDTStr = c.QuestionFinishSysDT.Value.ToString("MM/dd HH:mm");
                        v.QuestionFinishDT = c.QuestionFinishDT;
                    }

                    if (c.ExpectArriveDateTime != null)
                        v.ExArriveDTStr = c.ExpectArriveDateTime.Value.ToString("MM/dd HH:mm");
                    if (c.ExpectFinishDateTime != null)
                        v.ExFinishDTStr = c.ExpectFinishDateTime.Value.ToString("MM/dd HH:mm");
                    if (c.ScheduleStartDT != null)
                    {
                        v.SArriveDTStr = c.ScheduleStartDT.Value.ToString("MM/dd HH:mm");
                        v.ScheduleStartDT = c.ScheduleStartDT;
                    }

                    v.ServiceLeaveDT = c.ServiceLeaveDT;

                        //v.CloseDTStr = c.QuestionFinishDT.Value.ToString("yyyy/MM/dd HH:mm");
                    //if (c.ServiceLeaveDT != null)
                    //    v.SolveDTStr = c.ServiceLeaveDT.Value.ToString("yyyy/MM/dd HH:mm");
                    if (c.WBSEventM.CloseDT != null)
                    {
                        v.CloseDTStr = c.WBSEventM.CloseDT.Value.ToString("MM/dd HH:mm");
                        v.EventStatusStr = "99.已結案";
                    }
                    v.MaintainNum = "";
                    int maintainNum = db.EventRelation.Where(x => x.CID == c.Num).Count();
                    if (maintainNum != 0)
                        v.MaintainNum = maintainNum.ToString();

                    v.RespondReason = c.RespondReason;
                    if (m.Customer1.SubArea.ResponsibleID2 != null)
                    {
                        v.DefaultUserName2 = m.Customer1.SubArea.ResponsibleClass1.Name + "-" + m.Customer1.SubArea.ResponsibleClass1.UserData2.UserName;
                        //v.ResponsibleArea2 = m.Customer1.SubArea.SubAreaID;
                    }


                    v.CGroupSName = c.WBSEventM.Customer1.CustomerGroup.CGroupID + " : " + c.WBSEventM.Customer1.CustomerGroup.CGroupShortName;

                    //已開立時間
                    System.TimeSpan diff1 = DateTime.Now.Subtract(c.CreateDateTime ?? DateTime.Now);
                    //v.ServiceDT = Convert.ToDateTime(diff1.ToString());
                    int day = Int32.Parse(diff1.ToString("%d"));
                    int hour = Int32.Parse(diff1.ToString("%h"));
                    hour += day * 24;
                    v.CreatedTime = hour.ToString() + ":" + diff1.ToString(@"mm\:ss");

                    TimeSpan duration = new TimeSpan(0, 0, settingRepository.GetSetting().FirstOverTimeM, 0);
                    v.OverTime = "";
                    if (diff1.CompareTo(duration) > 0)
                        v.OverTime = "逾時";


                    v.DefaultUserName = m.Customer1.SubArea.ResponsibleClass.Name + "-" + m.Customer1.SubArea.ResponsibleClass.UserData2.UserName;
                    v.ResponsibleArea = m.Customer1.SubArea.SubAreaID;
                    //v.DefaultUserName = db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                    //v.ResponsibleArea = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID1).Select(x2 => x2.SubAreaID).First();
                    v.AreaID = m.Customer1.SubArea.AreaID;
                    v.AreaStr = m.Customer1.SubArea.Area.Name;

                    v.AbnormalReason = isAbnormal(c, alertMList);

                    if (Etype == "C02" || Etype == "C03")
                    {
                        int addday = db.SystemSetting.Where(x => x.isSysDefault == true).Select(x => x.Alert43Day).First();

                        if (db.SystemSetting.Where(x => x.EventClass == Etype).Count() > 0)
                            addday = db.SystemSetting.Where(x => x.EventClass == eventClass).Select(x => x.Alert43Day).First();

                        //if (c.ServiceLeaveDT != null)
                        if (c.EventStatus == 43)    //20161012 改只有43看的到提醒截止時間
                        {
                            v.FinishDTStr = c.ServiceLeaveDT.Value.ToString("MM/dd HH:mm");

                            if (c.ServiceLeaveSysDT != null)
                                v.FinishSysDTStr = c.ServiceLeaveSysDT.Value.ToString("MM/dd HH:mm");
                            if (c.ServiceLeaveDT.Value.AddDays(addday).CompareTo(DateTime.Now) < 0)
                            {
                                DateTime temp = c.ServiceLeaveDT.Value.AddDays(addday);
                                while (temp.CompareTo(DateTime.Now) < 0)
                                {
                                    temp = temp.AddDays(addday);
                                }
                                v.NextAlertDTStr = temp.ToString("MM/dd HH:mm");
                            }
                            else
                            {
                                v.NextAlertDTStr = c.ServiceLeaveDT.Value.AddDays(addday).ToString("MM/dd HH:mm");
                            }
                        }
                        //else
                        //{
                        //    if (c.CreateDateTime.Value.AddDays(3).CompareTo(DateTime.Now) < 0)
                        //    {
                        //        DateTime temp = c.CreateDateTime.Value.AddDays(3);
                        //        while(temp.CompareTo(DateTime.Now) < 0){
                        //            temp = temp.AddDays(3);
                        //        }
                        //            v.NextAlertDTStr = temp.ToString("MM/dd HH:mm");
                        //    }
                        //    else
                        //    {
                        //        v.NextAlertDTStr = c.CreateDateTime.Value.AddDays(3).ToString("MM/dd HH:mm");
                        //    }
                        //}
                    }

                    ret.Add(v);
                }
            }

            return ret.AsQueryable();
        }
        //派單主畫面-該執行人異常案件
        public IQueryable<AssignPersonCaseViewModel> ReadPersonAbnormalCase(String uid) 
        {
            List<AssignPersonCaseViewModel> ret = new List<AssignPersonCaseViewModel>();

            List<AlertDetailSettingViewModel> alertMList = new List<AlertDetailSettingViewModel>();
            var Aqry = db.AlertDetailSetting;
            foreach (AlertDetailSetting o in Aqry)
            {
                AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();
                a.EventClass = o.EventClass;
                a.AlertEvent = o.AlertEvent;
                a.FirstTime = o.FirstTime;
                alertMList.Add(a);
            }

            var qryR = db.ResponsibleClass.Where(x => x.UserID == uid);
            foreach (ResponsibleClass r in qryR) {
                //for (int i = 0; i < 2; i++) {
                var qryC = db.WBSEventC.Where(x => x.ResponsibleID == r.ID && x.EventStatus < 50);

                    //if (i == 0)         //已到達逾時未離開
                    //    qryC = qryC.Where(x => x.EventStatus == 41 && x.ScheduleEndDT.Value.CompareTo(DateTime.Now) < 0);
                    //else if (i == 1)    //已排程逾時未到達
                    //    qryC = qryC.Where(x => x.EventStatus == 31 && x.ScheduleStartDT.Value.CompareTo(DateTime.Now) < 0);

                    foreach (WBSEventC c in qryC) {
                        AssignPersonCaseViewModel v = new AssignPersonCaseViewModel();

                        v.AbnormalReason = "";
                        int temp = 0;
                        if (c.EventStatus == 31)
                        {
                            if (alertMList.Exists(x => x.EventClass == c.EventClass))
                                temp = alertMList.Find(x => x.EventClass == c.EventClass && x.AlertEvent == 3).FirstTime;
                            else
                                temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 3).FirstTime;

                            //if (c.CreateDateTime.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                            if (c.ScheduleStartDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                            { //排程後超過"預計到達時間"N分未到達                            
                                v.AbnormalReason = "逾時未到達";
                                v.OverTimeColor = "Red";
                                v.OverTimeDisplay = "";
                            }
                        }
                        else if (c.EventStatus == 41)
                        {
                            if (alertMList.Exists(x => x.EventClass == c.EventClass))
                                temp = alertMList.Find(x => x.EventClass == c.EventClass && x.AlertEvent == 4).FirstTime;
                            else
                                temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 4).FirstTime;

                            if (c.ServiceArriveDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                            { //排程後超過"實際到達時間"N分未離開                            
                                v.AbnormalReason = "逾時未離開";
                                v.OverTimeColor = "Red";
                                v.OverTimeDisplay = "";
                            }
                        }
                        if (v.AbnormalReason == "")
                            continue;


                        //v.OverTime = "";
                        //v.OverTimeDisplay = "none";
                        ////if (i == 0) {
                        ////    v.AbnormalReason = "逾時未離開";
                        ////    v.OverTimeColor = "Red";
                        ////    v.OverTimeDisplay = "";
                        ////}
                        ////else if (i == 1) {
                        ////    v.AbnormalReason = "逾時未到達";
                        ////    v.OverTimeColor = "Red";
                        ////    v.OverTimeDisplay = "";
                        ////}
                        v.MCaseID = c.MomCaseID;
                        var qryM = db.WBSEventM.Where(x => x.CaseID == c.MomCaseID);
                        foreach (WBSEventM m in qryM) {
                            v.CaseNumM = m.CaseClass + m.CaseDate + "-";
                            if (m.CaseCode < 10)
                                v.CaseNumM += "0000" + m.CaseCode.ToString();
                            else if (m.CaseCode < 100 && m.CaseCode >= 10)
                                v.CaseNumM += "000" + m.CaseCode.ToString();
                            else if (m.CaseCode < 1000 && m.CaseCode >= 100)
                                v.CaseNumM += "00" + m.CaseCode.ToString();
                            else if (m.CaseCode < 10000 && m.CaseCode >= 1000)
                                v.CaseNumM += "0" + m.CaseCode.ToString();
                            else
                                v.CaseNumM += m.CaseCode.ToString();

                            v.CustomerPhone1 = m.CustomerUserPhone1;
                            v.CustomerPhone = m.CustomerUserPhone1;
                            if (m.CustomerUserPhone2 != null && m.CustomerUserPhone2 != "")
                                v.CustomerPhone += " /" + m.CustomerUserPhone2;
                            v.CustomerRef = m.CustomerRef;
                            v.CustomerSName = m.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                            v.CustomerUserName = m.CustomerUserName;
                            v.CustomerCaseNum = m.CustomerCaseNum;
                        }
                        v.CCaseNum = c.Num;
                        v.CaseNumC = (c.ChildCaseID < 10) ? ("0" + c.ChildCaseID.ToString()) : c.ChildCaseID.ToString();
                        v.ResponsibleUserName = c.ResponsibleClass.Name + "-" + c.ResponsibleClass.UserData2.UserName;
                        v.DefaultUserName = r.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == r.UserID).Select(x => x.UserName).First();
                        v.AssignDT = c.AssignDT;
                        v.AssignDTStr = (c.AssignDT ?? DateTime.Now).ToString("MM/dd HH:mm");
                        v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.AssignCreateUserID).Select(x => x.UserName).First();
                        v.CreateDT = c.CreateDateTime;
                        v.CreateDTStr = (c.CreateDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                        v.CreateUserID = c.CreateUserID;
                        v.CreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == c.CreateUserID).Select(x => x.UserName).First();

                        v.ExpectArriveDT = c.ExpectArriveDateTime;
                        v.ExpectArriveDTStr = (c.ExpectArriveDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                        if (c.ExpectArriveSW == "0") {
                            v.ExpectArriveSW = "自行約定";
                            v.ExpectArriveDTStr = "自行約定";
                        }
                        else if (c.ExpectArriveSW == "1")
                            v.ExpectArriveSW = "合約規定";
                        else if (c.ExpectArriveSW == "2")
                            v.ExpectArriveSW = "建議期望";
                        else if (c.ExpectArriveSW == "4")
                            v.ExpectArriveSW = "自派新單";
                        else
                            v.ExpectArriveSW = "客戶指定";

                        //尚未處理
                        v.ProductNote = c.ProductClass1.Note; //db.ProductClass.AsEnumerable().Where(x => x.ProductClassID == c.ProductClass).Select(x => x.Note).First();
                        v.ProductClass = c.ProductClass;
                        v.ItemClass = c.ItemClass;
                        v.ItemNote = c.ItemClass1.Note; //db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == c.ItemClass).Select(x => x.Note).First();
                        v.QuestionClass = c.QuestionClass;
                        v.QuestionNote = c.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == c.QuestionClass).Select(x => x.Note).First();
                        v.QuestionDesc = c.QuestionDescription;
                        v.RespondDT = c.RespondDT;
                        v.RespondReason = c.RespondReason;
                        v.RespondSW = c.RespondSW;
                        v.ScheduleEndDT = c.ScheduleEndDT;
                        v.ScheduleEndDTStr = (c.ScheduleEndDT ?? DateTime.Now).ToString("MM/dd HH:mm");
                        v.ScheduleStartDT = c.ScheduleStartDT;
                        v.ScheduleStartDTStr = (c.ScheduleStartDT ?? DateTime.Now).ToString("MM/dd HH:mm");
                        v.ServiceHours = c.ScheduleServiceHours ?? 0;

                        //問題附件
                        v.HasActtachment = "";
                        if (db.Files.Where(x => x.CID == c.Num && x.Class == 1).Count() > 0)
                            v.HasActtachment = "@";
                        //預先保養
                        v.MaintainNum = "";
                        int maintainNum = db.EventRelation.Where(x => x.CID == c.Num).Count();
                        if (maintainNum != 0)
                            v.MaintainNum = maintainNum.ToString();
                        //解決附件
                        v.HasAssignActtachment = "";
                        if (db.Files.Where(x => x.CID == c.Num && x.Class == 2).Count() > 0)
                            v.HasAssignActtachment = "@";
                        //知識庫附件
                        v.HasKnowledge = "";
                        if (db.Files.Where(x => x.CID == c.Num && x.Class == 3).Count() > 0)
                            v.HasKnowledge = "@";
                        //解決路徑
                        v.HasHyperlink = "";
                        if (db.Files.Where(x => x.CID == c.Num && x.Class == 9).Count() > 0)
                            v.HasHyperlink = "@";

                        v.EventStatusStr = (c.EventStatus == 0) ? "0" + c.EventStatus.ToString() : c.EventStatus.ToString();
                        v.EventStatusStr += "." + GetEventStatusStr(c.EventStatus);

                        v.ServiceArriveDT = c.ServiceArriveDT;
                        v.ServiceFinishDT = c.ServiceLeaveDT;
                        v.EventClassStr = c.EventClass + "-" + db.EventClass.Where(x => x.Class == c.EventClass).Select(x => x.Name).First();

                        String RegionID = c.WBSEventM.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();
                        String RegionID2 = "";
                        int RID2 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID2).First() ?? 0;
                        if (RID2 != 0)
                        {
                            RegionID2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.UserID).First();
                            v.DefaultUserName2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.Name).First() + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == RegionID2).Select(x => x.UserName).First();
                        }

                        ret.Add(v);
                    }
                //}
            }
            return ret.AsQueryable().OrderByDescending(x => x.CreateDT);
        }
        //客戶叫修主畫面-案件詳情讀取
        public WBSEventViewModel ReadOneCase(int mID, int cID)
        {
            List<AlertDetailSettingViewModel> alertMList = new List<AlertDetailSettingViewModel>();
            var Aqry = db.AlertDetailSetting;
            foreach (AlertDetailSetting o in Aqry)
            {
                AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();
                a.EventClass = o.EventClass;
                a.AlertEvent = o.AlertEvent;
                a.FirstTime = o.FirstTime;
                alertMList.Add(a);
            }

            WBSEventViewModel v = new WBSEventViewModel();
            WBSEventM M = db.WBSEventM.Find(mID);
            WBSEventC C = db.WBSEventC.Find(cID);

            v.C_ID = C.Num;
            v.CreateDT = C.CreateDateTime;
            v.CreateUserID = C.CreateUserID;
            v.CustomerAddr = M.Customer1.Addr; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == M.CustomerRef).Select(x => x.Addr).First();
            v.CustomerNote = M.Customer1.Notes; 
            v.CustomerID = M.Customer1.CustomerID; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == M.CustomerRef).Select(x => x.CustomerID).First();
            v.CustomerRef = M.CustomerRef;
            v.CustomerSName = M.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == M.CustomerRef).Select(x => x.ShortName).First();
            v.CustomerUserName = M.CustomerUserName;
            v.CustomerUserPhone1 = M.CustomerUserPhone1;
            v.CustomerUserPhone2 = M.CustomerUserPhone2;
            v.CustomerUserPhone = M.CustomerUserPhone1;
            if (M.CustomerUserPhone2 != null && M.CustomerUserPhone2 != "")
                v.CustomerUserPhone += " /" + M.CustomerUserPhone2;
            v.CustomerCaseNum = M.CustomerCaseNum;

            if (M.ProductID != null)
            {
                v.ProductID = M.ProductID;
                v.ProductIDNote = db.ProductData.Where(x => x.ProductID == M.ProductID).Select(x => x.Note2).First();
            }
            else
            {
                v.ProductID = "";
                v.ProductIDNote = "";
            }
            v.EventClass = C.EventClass;
            v.EventClassStr = C.EventClass + "-" + db.EventClass.Where(x => x.Class == C.EventClass).Select(x => x.Name).First(); ;
            v.ExpectArriveDT = C.ExpectArriveDateTime;
            v.ExpectArriveDD = C.ExpectArriveDateTime.Value.Date;
            v.ExpectArriveHH = C.ExpectArriveDateTime.Value.ToString("HH");
            v.ExpectArriveMM = C.ExpectArriveDateTime.Value.ToString("mm");
            v.ExpectArriveSW = C.ExpectArriveSW;
            v.ExpectFinishDT = C.ExpectFinishDateTime;
            v.ExpectFinishDD = C.ExpectFinishDateTime.Value.Date;
            v.ExpectFinishHH = C.ExpectFinishDateTime.Value.ToString("HH");
            v.ExpectFinishMM = C.ExpectFinishDateTime.Value.ToString("mm");
            //20150811新增-Hsiuyang
            v.ChangeAddr = M.CustomerAddr;
            v.SpareMach = M.SpareMach;
            v.UserMach = M.UserMach;
            v.CaseMStatus = M.CaseMStatus;

            //v.DefaultUserName = M.Customer1.SubArea.ResponsibleClass.UserData2.UserName;
            v.DefaultUserName = M.Customer1.SubArea.ResponsibleClass.Name + "-" + M.Customer1.SubArea.ResponsibleClass.UserData2.UserName;
            v.ResponsibleArea = M.Customer1.SubArea.SubAreaID;

            v.ExpectArriveDTStr = (C.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
            v.ExpectFinishDTStr = (C.ExpectFinishDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
            v.ExpectArriveSW = C.ExpectArriveSW;
            if (C.ExpectArriveSW == "1")            
                v.ExpectArriveSWStr = "合約規定";            
            else if (C.ExpectArriveSW == "2")
                v.ExpectArriveSWStr = "建議期望";
            else if (C.ExpectArriveSW == "3")
                v.ExpectArriveSWStr = "客戶指定";
            else if (C.ExpectArriveSW == "4")
                v.ExpectArriveSWStr = "自派新單";                
            else
            {
                v.ExpectArriveSWStr = "自行約定";
                v.ExpectArriveDTStr = "請自行與客戶約定";
                v.ExpectFinishDTStr = "請自行與客戶約定";
            }                

            v.ItemClass = C.ItemClass.ToString();

            v.ProductNote = C.ProductClass1.Note; //db.ProductClass.AsEnumerable().Where(x => x.ProductClassID == C.ProductClass).Select(x => x.Note).First();
            v.ItemNote = C.ItemClass1.Note; //db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == C.ItemClass).Select(x => x.Note).First();
            v.QuestionNote = C.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == C.QuestionClass).Select(x => x.Note).First();

            v.M_ID = M.CaseID;
            v.ProductClass = C.ProductClass.ToString();
            v.QuestionClass = C.QuestionClass.ToString();
            v.QuestionDescription = C.QuestionDescription;
            v.WBSCaseNumberC = (C.ChildCaseID < 10) ? ("0" + C.ChildCaseID.ToString()) : C.ChildCaseID.ToString();
            v.WBSCaseNumberM = M.CaseClass + M.CaseDate + "-";
            if (M.CaseCode < 10)
                v.WBSCaseNumberM += "0000" + M.CaseCode.ToString();
            else if (M.CaseCode < 100 && M.CaseCode >= 10)
                v.WBSCaseNumberM += "000" + M.CaseCode.ToString();
            else if (M.CaseCode < 1000 && M.CaseCode >= 100)
                v.WBSCaseNumberM += "00" + M.CaseCode.ToString();
            else if (M.CaseCode < 10000 && M.CaseCode >= 1000)
                v.WBSCaseNumberM += "0" + M.CaseCode.ToString();
            else
                v.WBSCaseNumberM += M.CaseCode.ToString();
            v.WBSCaseNumber = v.WBSCaseNumberM + "-" + v.WBSCaseNumberC;

            //20150811新增-Hsiuyang
            v.SugProvideSpareMach = C.SugProvideSpareMach;
            v.SugRetrieveSpareMach = C.SugRetrieveSpareMach;
            v.SugRetrieveUserMach = C.SugRetrieveUserMach;
            v.SugReturnUserMach = C.SugReturnUserMach;
            v.SugMachMemo = C.SugMachMemo;
            v.EventStatus = C.EventStatus;
            v.AssignDescription = C.AssignDescription;
            v.ServiceReport = C.ServiceReport;
            if (C.ServiceTakeHours != null)
                v.WorkHours = C.ServiceTakeHours.Value;
            else
                v.WorkHours = 0;
            //20150814新增-Hsiuyang
            if (C.EventStatus >= 50)
                v.Closed = "T";
            else
                v.Closed = "F";
            //建議零件
            var qryS = db.SugProduct.Where(x => x.CID == cID && (x.Class == 1 || x.Class == 3));
            JArray SugProduct = new JArray();
            foreach (SugProduct s in qryS) {
                JObject item = new JObject();
                item["Class"] = s.Class.ToString();
                if (s.ProductNum == null) {
                    item["ProductNum"] = "";
                    item["ProductName"] = s.ProductName;
                }
                else {
                    item["ProductNum"] = s.ProductNum.ToString();
                    item["ProductName"] = s.ProductData.Name; //db.ProductData.AsEnumerable().Where(x => x.ProductNum == s.ProductNum).Select(x => x.Name).First();
                    //item["ProductName"] = db.ProductData.AsEnumerable().Where(x => x.ProductNum == s.ProductNum).Select(x => x.Name).First();
                }
                SugProduct.Add(item);
            }
            if (SugProduct.Count > 0)
                v.MaterialJson = SugProduct.ToString();

            //實際零件
            var qryS2 = db.SugProduct.Where(x => x.CID == cID && (x.Class == 2 || x.Class == 4));
            JArray SugProduct2 = new JArray();
            foreach (SugProduct s in qryS2)
            {
                JObject item = new JObject();
                item["Class"] = s.Class.ToString();
                if (s.ProductNum == null)
                {
                    item["ProductNum"] = "";
                    item["ProductName"] = s.ProductName;
                }
                else
                {
                    item["ProductNum"] = s.ProductNum.ToString();
                    item["ProductName"] = s.ProductData.Name; //db.ProductData.AsEnumerable().Where(x => x.ProductNum == s.ProductNum).Select(x => x.Name).First();
                    //item["ProductName"] = db.ProductData.AsEnumerable().Where(x => x.ProductNum == s.ProductNum).Select(x => x.Name).First();
                }
                SugProduct2.Add(item);
            }
            if (SugProduct2.Count > 0)
                v.MaterialJson2 = SugProduct2.ToString();

            
            //解決方式知識庫附件
            var qryK = db.Files.Where(x => x.CID == cID && x.Class == 3);
            JArray KnowledgeData = new JArray();
            foreach (Files k in qryK) {
                JObject item = new JObject();
                item["KnowledgeID"] = k.KnowledgeID.ToString();
                item["Name"] = k.KnowledgeBase.FileName; //db.KnowledgeBase.AsEnumerable().Where(x => x.KnowledgeID == k.KnowledgeID).Select(x => x.FileName).First();

                KnowledgeData.Add(item);
            }
            if (KnowledgeData.Count > 0)
                v.KnowledgeJson = KnowledgeData.ToString();
            //執行預先保養單
            var qryE = db.EventRelation.Where(x => x.CID == cID);
            JArray MaintainCase = new JArray();
            foreach (EventRelation e in qryE) {
                JObject item = new JObject();
                item["mID"] = db.WBSEventC.AsEnumerable().Where(x => x.Num == e.RelationCID).Select(x => x.MomCaseID).First();                
                item["cID"] = e.RelationCID;
                MaintainCase.Add(item);
            }
            if (MaintainCase.Count > 0)
                v.MaintainCaseJson = MaintainCase.ToString();

            //20151113新增-Hsiuyang-Hyperlink建議參考網頁(CLASS=9)
            var qryH = db.Files.Where(x => x.CID == cID && x.Class == 9);
            JArray HyperlinkData = new JArray();
            foreach (Files k in qryH) {
                JObject item = new JObject();
                item["Href"] = k.FileAddr;
                item["Name"] = k.FileName;
                HyperlinkData.Add(item);
            }
            if (HyperlinkData.Count > 0)
                v.HyperlinkJson = HyperlinkData.ToString();
            if (C.EventStatus == 0) {
                v.DeleteMarkDT = C.DeleteMarkDateTime;
                v.DeleteMarkDTStr = (C.DeleteMarkDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                v.DeleteMarkUserID = C.DeleteMarkUserID;
                v.DeleteMarkUserName = db.UserData.AsEnumerable().Where(x => x.UserID == C.DeleteMarkUserID).Select(x => x.UserName).First();
                v.DeleteReason = C.DeleteReason;
            }
            //20150824新增-Hsiuyang-已排程所需(預計到達時間,回覆說明)
            v.ResponsibleUserName = "";
            if (C.ScheduleServiceHours != null)
                v.WorkHours = C.ScheduleServiceHours.Value;
            if (C.EventStatus >= 21)
            {
                if (C.ResponsibleClass != null)
                    v.ResponsibleUserName = C.ResponsibleClass.UserData2.UserName;
                else //線上結案
                    v.ResponsibleUserName = "";
            }
            if (C.EventStatus >= 31) {
                //v.ResponsibleUserName = C.ResponsibleClass.UserData2.UserName;
                v.ScheduleStartDT = C.ScheduleStartDT;
                v.ScheduleStartDTStr = (C.ScheduleStartDT ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                v.RespondReason = C.RespondReason;
                if(C.RespondSW == "Y")
                    v.RespondSWStr = "任務已接受";
                else if (C.RespondSW == "N")
                    v.RespondSWStr = "任務已拒絕";
                else
                    v.RespondSWStr = "尚未回覆";
            }
            if (C.EventStatus >= 41) {
                if(C.ServiceModifyUserID != null)
                    v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == C.ServiceModifyUserID).Select(x => x.UserName).First();
                else if(C.ServiceCreateUserID!=null)
                    v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == C.ServiceCreateUserID).Select(x => x.UserName).First();
                else
                    v.ServiceUserName = db.UserData.AsEnumerable().Where(x => x.UserID == C.CreateUserID).Select(x => x.UserName).First();

                v.RespondSW = C.RespondSW;
                if (C.ServiceArriveDT == null)//非線上結案
                {
                    v.ServiceArriveDT = C.ServiceArriveDT;
                    v.ServiceArriveDTStr = "已線上結案或強制結案";
                    v.ServiceArriveSysDT = C.ServiceArriveSysDT;
                    v.ServiceArriveSysDTStr = "已線上結案或強制結案";
                    v.EditArriveDT = C.ServiceArriveDT;
                }
                else {
                    v.ServiceArriveDT = C.ServiceArriveDT;
                    v.ServiceArriveDTStr = (C.ServiceArriveDT ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.ServiceArriveSysDT = C.ServiceArriveSysDT;
                    v.ServiceArriveSysDTStr = (C.ServiceArriveSysDT ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.EditArriveDT = C.ServiceArriveDT;
                    v.EditArriveDD = C.ServiceArriveDT.Value.Date;
                    v.EditArriveHH = C.ServiceArriveDT.Value.ToString("HH");
                    v.EditArriveMM = C.ServiceArriveDT.Value.ToString("mm");
                }
            }
            if (C.EventStatus >= 42) {                
                if (C.ServiceLeaveDT == null)//非線上結案
                {
                    v.ServiceLeaveDT = C.ServiceLeaveDT;
                    v.ServiceLeaveDTStr = "已線上結案或強制結案";
                    v.ServiceLeaveSysDT = C.ServiceLeaveSysDT;
                    v.ServiceLeaveSysDTStr = "已線上結案或強制結案";
                    v.EditLeaveDT = C.ServiceLeaveDT;
                }
                else {
                    v.ServiceLeaveDT = C.ServiceLeaveDT;
                    v.ServiceLeaveDTStr = (C.ServiceLeaveDT ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.ServiceLeaveSysDT = C.ServiceLeaveSysDT;
                    v.ServiceLeaveSysDTStr = (C.ServiceLeaveSysDT ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.EditLeaveDT = C.ServiceLeaveDT;
                    v.EditLeaveDD = C.ServiceLeaveDT.Value.Date;
                    v.EditLeaveHH = C.ServiceLeaveDT.Value.ToString("HH");
                    v.EditLeaveMM = C.ServiceLeaveDT.Value.ToString("mm");
                }
                v.ProvideSpareMach = C.ProvideSpareMach;
                v.RetrieveSpareMach = C.RetrieveSpareMach;
                v.RetrieveUserMach = C.RetrieveUserMach;
                v.ReturnUserMach = C.ReturnUserMach;
                v.MachMemo = C.MachMemo;                
            }
            //20150922新增-Hsiuyang
            if (C.EventStatus >= 46) {
                if (C.QuestionFinishDT != null) {
                    v.SolveDTStr = C.QuestionFinishDT.Value.ToString("yyyy/MM/dd HH:mm");
                    v.SolveSysDTStr = C.QuestionFinishSysDT.Value.ToString("yyyy/MM/dd HH:mm");
                }
                if (C.WBSEventM.CloseDT != null)                    
                    v.CloseDTStr = C.WBSEventM.CloseDT.Value.ToString("yyyy/MM/dd HH:mm");
            }
            v.haveContract = "false";
            Boolean havaContract = false;
            if(db.CustomerContract.Where(x=>x.CustomerRef == M.CustomerRef && x.ProductID == M.ProductID).Count() > 0)
                havaContract = true;
            if(havaContract) {
                v.haveContract = "true";
                v.CCProduct = C.ProductClass;
                String temp = db.CustomerContract.Where(x=>x.CustomerRef == M.CustomerRef && x.ProductID == M.ProductID).Select(x=>x.ContractStatus).First();
                //合約狀況代碼
                switch(temp){//01.洽談中 (可能用計算方式:合約到期日 +N天) A1.總行合約中 B1.分行合約中 C1.叫修 L1.租賃中 W1.保固中 ZZ.未分類 XX.停用
                    case "01": v.ContractStatusStr = "洽談中"; break;
                    case "A1": v.ContractStatusStr = "總行合約中"; break;
                    case "B1": v.ContractStatusStr = "分行合約中"; break;
                    case "C1": v.ContractStatusStr = "叫修"; break;
                    case "L1": v.ContractStatusStr = "租賃中"; break;
                    case "W1": v.ContractStatusStr = "保固中"; break;
                    case "ZZ": v.ContractStatusStr = "未分類"; break;
                    case "XX": v.ContractStatusStr = "停用"; break;
                    default: v.ContractStatusStr = ""; break;
                }
                //v.ContractStatusStr = db.CustomerContract.Where(x=>x.CustomerRef == M.CustomerRef && x.ProductClassID == C.ProductClass).Select(x=>x.ContractStatus).First();
                //v.MaintainNum = db.CustomerContract.Where(x => x.CustomerRef == M.CustomerRef && x.ProductClassID == C.ProductClass).Select(x => x.MaintainNum).First();
                v.MaintainNum = db.CustomerContract.Where(x => x.CustomerRef == M.CustomerRef && x.ProductID == M.ProductID).Select(x => x.MaintainNum).First();
                v.MaintainMonthMemo = db.CustomerContract.Where(x => x.CustomerRef == M.CustomerRef && x.ProductID == M.ProductID).Select(x => x.MaintainMonthMemo).First();
            }

            if (v.Closed == "T" && v.RespondSW != "Y") {//已結案且沒有回覆者判定為線上結案
                v.ExpectArriveDTStr = "已線上結案";
                v.ExpectFinishDTStr = "已線上結案";
            }

            v.AbnormalReason = isAbnormal(C, alertMList);
            v.EventStatusStr = GetEventStatusStr(C.EventStatus);
            

            return v;
        }
        public List<AlertDetailSettingViewModel> ReadAlertDetail(String EventClass)
        {
            List<AlertDetailSettingViewModel> ret = new List<AlertDetailSettingViewModel>();
            var qry = db.AlertDetailSetting.Where(x => x.Seq != 0);
            if (db.AlertDetailSetting.Where(x => x.EventClass == EventClass).Count() == 0)
                qry = qry.Where(x => x.isSysDefault == true);
            else
                qry = qry.Where(x => x.EventClass == EventClass);

            foreach (AlertDetailSetting o in qry) {
                AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();

                a.FirstTime = o.FirstTime;
                a.AlertEvent = o.AlertEvent;
                a.SecondTime = o.SecondTime;
                a.Times = o.Times;
                a.Memo = o.Memo;

                ret.Add(a);
            }
            return ret;
        }
        public IQueryable<AlertSettingViewModel> GetAlertSetting(String EventClass)
        {
            List<AlertSettingViewModel> ret = new List<AlertSettingViewModel>();

            var qry = db.SystemSetting.Where(x => x.Seq != 0);
            if (EventClass != "" && db.SystemSetting.Where(x => x.EventClass == EventClass).Count() != 0)
                qry = db.SystemSetting.Where(x => x.EventClass == EventClass);
            else
                qry = db.SystemSetting.Where(x => x.EventClass == null);
            foreach (SystemSetting o in qry) {
                AlertSettingViewModel s = new AlertSettingViewModel();

                s.AlertTimes = o.AlertTimes;
                s.EventClass = o.EventClass;
                s.isSysDefault = o.isSysDefault;
                s.Seq = o.Seq;
                s.TimeSetting = new List<AlertTimeSettingViewModel>();
                s.HolidaySetting = new List<AlertHolidaySettingViewModel>();
                s.DetailSetting = new List<AlertDetailSettingViewModel>();

                var qry2 = db.AlertTimeSetting.Where(x => x.Seq != 0);
                if (EventClass != "" && db.AlertTimeSetting.Where(x => x.EventClass == EventClass).Count() != 0)
                    qry2 = db.AlertTimeSetting.Where(x => x.EventClass == EventClass);
                else
                    qry2 = db.AlertTimeSetting.Where(x => x.EventClass == null);
                foreach (AlertTimeSetting o2 in qry2) {
                    AlertTimeSettingViewModel t = new AlertTimeSettingViewModel();

                    t.EventClass = o2.EventClass;
                    t.isSysDefault = o2.isSysDefault;
                    t.Seq = o2.Seq;
                    t.Week = o2.Week;
                    t.isAlert = o2.isAlert;
                    t.StartTime = o2.StartTime;
                    t.EndTime = o2.EndTime;
                    t.StartTime2 = o2.StartTime2;
                    t.EndTime2 = o2.EndTime2;
                    t.StartTime3 = o2.StartTime3;
                    t.EndTime3 = o2.EndTime3;

                    s.TimeSetting.Add(t);
                }

                var qry3 = db.AlertHolidaySetting.Where(x => x.Seq != 0);
                if (EventClass != "" && db.AlertHolidaySetting.Where(x => x.EventClass == EventClass).Count() != 0)
                    qry3 = db.AlertHolidaySetting.Where(x => x.EventClass == EventClass);
                else
                    qry3 = db.AlertHolidaySetting.Where(x => x.EventClass == null);
                foreach (AlertHolidaySetting o3 in qry3) {
                    AlertHolidaySettingViewModel h = new AlertHolidaySettingViewModel();

                    h.EventClass = o3.EventClass;
                    h.isSysDefault = o3.isSysDefault;
                    h.Seq = o3.Seq;
                    h.EndDate = o3.EndDate;
                    h.StartDate = o3.StartDate;
                    h.isAlert = o3.isAlert;

                    s.HolidaySetting.Add(h);
                }

                var qry4 = db.AlertDetailSetting.Where(x => x.Seq != 0);
                if (EventClass != "" && db.AlertDetailSetting.Where(x => x.EventClass == EventClass).Count() != 0)
                    qry4 = db.AlertDetailSetting.Where(x => x.EventClass == EventClass);
                else
                    qry4 = db.AlertDetailSetting.Where(x => x.EventClass == null);
                foreach (AlertDetailSetting o4 in qry4) {
                    AlertDetailSettingViewModel d = new AlertDetailSettingViewModel();

                    d.EventClass = o4.EventClass;
                    d.isSysDefault = o4.isSysDefault;
                    d.Seq = o4.Seq;
                    d.AlertEvent = o4.AlertEvent;
                    d.FirstTime = o4.FirstTime;
                    d.Memo = o4.Memo;
                    d.SecondTime = o4.SecondTime;
                    d.Times = o4.Times;

                    s.DetailSetting.Add(d);
                }
                ret.Add(s);
            }
            return ret.AsQueryable();
        }
        //派單畫面GridView2
        public IQueryable<WBSAssignPeopleViewModel> ReadPeople(String area)
        {
            List<WBSAssignPeopleViewModel> ret = new List<WBSAssignPeopleViewModel>();

            var qryS = db.AreaReResponsibleClass.Where(x => x.AreaID != "ALL");
            if (area != "ALL")
                qryS = qryS.Where(x => x.AreaID == area);

            List<AlertDetailSettingViewModel> alertMList = new List<AlertDetailSettingViewModel>();
            var Aqry = db.AlertDetailSetting;
            foreach (AlertDetailSetting o in Aqry)
            {
                AlertDetailSettingViewModel a = new AlertDetailSettingViewModel();
                a.EventClass = o.EventClass;
                a.AlertEvent = o.AlertEvent;
                a.FirstTime = o.FirstTime;
                alertMList.Add(a);
            }

            foreach (AreaReResponsibleClass s in qryS)
            {
                //var qryR = db.ResponsibleClass.Where(x => x.SubAreaID == s.SubAreaID);
                var qryR = db.ResponsibleClass.Where(x => x.ID == s.ResponsibleClassID);
                foreach (ResponsibleClass R in qryR)
                {
                    var qry = db.UserData.Where(x => x.UserID == R.UserID);
                    foreach (UserData u in qry)
                    {
                        int Anum = 0;   //派於今日且接受(預計到達時間為今日)
                        int Anum2 = 0;  //派於今日未接受(期望到達時間為今日) //20160803改全部未接受
                        int Anum3 = 0;  //派於今日且拒絕(期望到達時間為今日) //20160803改全部已拒絕
                        int Allnum = 0; //總數
                        var qryA = db.ResponsibleClass.Where(x => x.UserID == u.UserID);
                        foreach (ResponsibleClass a in qryA)
                        {
                            //var qryN = db.WBSEventC.Where(x => x.ResponsibleID == a.ID && x.ScheduleStartDT != null);
                            var qryN = db.WBSEventC.Where(x => x.ResponsibleID == a.ID && x.RespondSW == "Y");
                            //var qryN = db.WBSEventC.Where(x => x.AssignDT != null && (x.RespondSW == null || x.RespondSW == "Y"));                            
                            foreach (WBSEventC n in qryN)
                            {
                                //DateTime ADT = n.ScheduleStartDT ?? DateTime.Now;
                                
                                if (n.ScheduleStartDT != null) {
                                    DateTime ADT = n.ScheduleStartDT.Value;
                                    if (ADT.Year == DateTime.Now.Year && ADT.Month == DateTime.Now.Month && ADT.Day == DateTime.Now.Day)
                                        Anum++;
                                }
                                
                            }

                            var qryN2 = db.WBSEventC.Where(x => x.ResponsibleID == a.ID && x.EventStatus == 21);
                            //var qryN = db.WBSEventC.Where(x => x.AssignDT != null && (x.RespondSW == null || x.RespondSW == "Y"));                            
                            foreach (WBSEventC n in qryN2)
                            {
                                if (n.ExpectArriveDateTime != null)
                                {
                                    //DateTime ADT = n.ExpectArriveDateTime.Value;
                                    //if (ADT.Year == DateTime.Now.Year && ADT.Month == DateTime.Now.Month && ADT.Day == DateTime.Now.Day)
                                        Anum2++;
                                }
                            }

                            var qryN3 = db.WBSEventC.Where(x => x.ResponsibleID == a.ID && x.EventStatus == 20);                                                  
                            foreach (WBSEventC n in qryN3)
                            {
                                if (n.ExpectArriveDateTime != null)
                                {
                                    //DateTime ADT = n.ExpectArriveDateTime.Value;
                                    //if (ADT.Year == DateTime.Now.Year && ADT.Month == DateTime.Now.Month && ADT.Day == DateTime.Now.Day)
                                        Anum3++;
                                }
                            }

                            //var qryN4 = db.WBSEventC.Where(x => x.ResponsibleID == a.ID);
                            //foreach (WBSEventC n in qryN4)
                            //{

                            //    if (n.ScheduleStartDT != null) {
                            //        DateTime ADT = n.ScheduleStartDT.Value;
                            //        if (ADT.Year == DateTime.Now.Year && ADT.Month == DateTime.Now.Month && ADT.Day == DateTime.Now.Day)
                            //            Allnum++;
                            //    }
                            //    else
                            //    {
                            //        if (n.ExpectArriveDateTime != null)
                            //        {
                            //            DateTime ADT = n.ExpectArriveDateTime.Value;
                            //            if (ADT.Year == DateTime.Now.Year && ADT.Month == DateTime.Now.Month && ADT.Day == DateTime.Now.Day)
                            //                Allnum++;
                            //        }
                            //    }
                            //}
                        }                        
                        WBSAssignPeopleViewModel v = new WBSAssignPeopleViewModel();
                        v.HasActtachment = "";
                        v.AssignNum = Anum;     //派於今日且接受
                        v.AssignNum2 = Anum2;   //ALL未接受
                        Allnum = Anum + Anum2 + Anum3;
                        v.Num1DNum2 = Anum.ToString() + "/" + Anum2.ToString() + "/" + Anum3.ToString() + "/" + Allnum.ToString();

                        v.Now = "待命中";
                        v.UserID = u.UserID;
                        v.UserName = R.Name + "-" + u.UserName;
                        if (R.UserID2 != null)
                            v.UserName2 = R.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == R.UserID2).Select(x => x.UserName).First();
                        v.RoleID = u.RoleID;
                        v.Phone1 = u.Phone1;
                        v.Phone2 = u.Phone2;
                        v.Phone3 = u.Phone3;
                        v.LoginID = u.LoginID;
                        v.StartDate = u.StartDate;
                        v.EndDate = u.EndDate;
                        v.Email = u.Email;
                        v.ResponsibleID = u.ResponsibleID;
                        foreach (ResponsibleClass a in qryA)
                        {
                            //var qryC = db.WBSEventC.Where(x => x.ResponsibleID == a.ID && x.ServiceLeaveDT == null && x.ServiceArriveDT != null);
                            //var qryC = db.WBSEventC.Where(x => x.ServiceLeaveDT == null && x.ServiceArriveDT != null);
                            if (db.WBSEventC.Where(x => x.ResponsibleID == a.ID && x.EventStatus == 41).Count() > 0) { 
                                var c = db.WBSEventC.Where(x => x.ResponsibleID == a.ID && x.EventStatus == 41).OrderByDescending(x => x.ServiceArriveDT).First();
                            //foreach (WBSEventC c in qryC)
                            //{
                                var qryM = db.WBSEventM.Where(x => x.CaseID == c.MomCaseID);
                                foreach (WBSEventM m in qryM)
                                {
                                    v.CustomerSName = db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                                    v.CaseNumM = m.CaseClass + m.CaseDate + "-";
                                    if (m.CaseCode < 10)
                                        v.CaseNumM += "0000" + m.CaseCode.ToString();
                                    else if (m.CaseCode < 100 && m.CaseCode >= 10)
                                        v.CaseNumM += "000" + m.CaseCode.ToString();
                                    else if (m.CaseCode < 1000 && m.CaseCode >= 100)
                                        v.CaseNumM += "00" + m.CaseCode.ToString();
                                    else if (m.CaseCode < 10000 && m.CaseCode >= 1000)
                                        v.CaseNumM += "0" + m.CaseCode.ToString();
                                    else
                                        v.CaseNumM += m.CaseCode.ToString();

                                    v.MCaseID = m.CaseID;
                                }
                                
                                v.CCaseNum = c.Num;
                                v.CaseNumC = (c.ChildCaseID < 10) ? ("0" + c.ChildCaseID.ToString()) : c.ChildCaseID.ToString();
                                v.Now = "服務中";
                                v.ProductClass = c.ProductClass;
                                v.ProductNote = db.ProductClass.AsEnumerable().Where(x => x.ProductClassID == c.ProductClass).Select(x => x.Note).First();
                                v.ItemClass = c.ItemClass;
                                v.ItemNote = db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == c.ItemClass).Select(x => x.Note).First();
                                v.QuestionClass = c.QuestionClass;
                                v.QuestionNote = db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == c.QuestionClass).Select(x => x.Note).First();
                                v.QuestionDesc = c.QuestionDescription;

                                
                                if (db.Files.Where(x => x.CID == c.Num && x.Class == 1).Count() > 0)
                                    v.HasActtachment = "@";

                                v.StartDT = c.ServiceArriveDT;
                                v.StartDTStr = (c.ServiceArriveDT ?? DateTime.Now).ToString("MM/dd HH:mm");
                                v.AbnormalReason = "";
                                int temp = 0;
                                if (c.EventStatus == 31)
                                {
                                    if (alertMList.Exists(x => x.EventClass == c.EventClass))
                                        temp = alertMList.Find(x => x.EventClass == c.EventClass && x.AlertEvent == 3).FirstTime;
                                    else
                                        temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 3).FirstTime;

                                    //if (c.CreateDateTime.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                                    if (c.ServiceArriveDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                                    { //排程後超過"預計到達時間"N分未到達                            
                                        v.AbnormalReason = "逾時未到達";
                                        //isAbnormal = true;
                                    }
                                }
                                else if (c.EventStatus == 41)
                                {
                                    if (alertMList.Exists(x => x.EventClass == c.EventClass))
                                        temp = alertMList.Find(x => x.EventClass == c.EventClass && x.AlertEvent == 4).FirstTime;
                                    else
                                        temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 4).FirstTime;

                                    //if (c.CreateDateTime.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                                    if (c.ServiceArriveDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                                    { //排程後超過"實際到達時間"N分未離開                            
                                        v.AbnormalReason = "逾時未離開";
                                        //isAbnormal = true;
                                    }
                                }



                                System.TimeSpan diff1 = DateTime.Now.Subtract(c.ServiceArriveDT ?? DateTime.Now);
                                //v.ServiceDT = Convert.ToDateTime(diff1.ToString());
                                //int day = Int32.Parse(diff1.ToString("%d"));
                                //int hour = Int32.Parse(diff1.ToString("%h"));

                                //hour += day * 24;
                                v.ServiceDTStr = diff1.Days.ToString() + "日" + diff1.Hours.ToString() + "時" + diff1.Minutes.ToString() + "分";
                                ////v.ServiceDT = DateTime.Now.Subtract(c.ServiceArriveDT;

                                //TimeSpan duration = new TimeSpan(0, c.ScheduleServiceHours ?? 0, 0, 0);
                                //v.OverTime = "";
                                //if (diff1.CompareTo(duration) > 0)
                                //    v.OverTime = "逾時";
                            //}
                            }
                        }
                        //v.UserName2 = db.UserData.AsEnumerable().Where(x => x.UserID == R.UserID2).Select(x => x.UserName).First();
                        v.Name = R.Name;
                        v.ID = R.ID;

                        if (v.CaseNumC == null)
                            v.CaseNumC = "";
                        if (v.CaseNumM == null)
                            v.CaseNumM = "";
                        if (v.CustomerSName == null)
                            v.CustomerSName = "";

                        ret.Add(v);
                    }
                }
            }


            return ret.AsQueryable().GroupBy(x => x.ID).Select(x => x.First()); ; 
        }