//案件追蹤
        public List<WBSAssignViewModel> EventTraceRead(int cid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            int mid = db.WBSEventC.Find(cid).MomCaseID;
            //WBSEventM M = db.WBSEventM.Find(mid);

            var qryC = db.WBSEventC.Where(x => x.MomCaseID == mid);
            foreach (WBSEventC wbsC in qryC)
            {
                WBSAssignViewModel v = new WBSAssignViewModel();

                v.MCaseID = wbsC.WBSEventM.CaseID;
                v.CCaseNum = wbsC.Num;

                v.CreateDT = wbsC.CreateDateTime;
                v.CreateUserID = wbsC.CreateUserID;
                v.ScheduleStartDT = wbsC.ExpectArriveDateTime;
                v.ScheduleEndDT = wbsC.ExpectFinishDateTime;
                v.ServiceHours = wbsC.QuestionClass1.QuestionServiceHours; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.QuestionServiceHours).First();

                v.CaseNumM = wbsC.WBSEventM.CaseClass + wbsC.WBSEventM.CaseDate + "-";
                if (wbsC.WBSEventM.CaseCode < 10)
                    v.CaseNumM += "0000" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 100 && wbsC.WBSEventM.CaseCode >= 10)
                    v.CaseNumM += "000" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 1000 && wbsC.WBSEventM.CaseCode >= 100)
                    v.CaseNumM += "00" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 10000 && wbsC.WBSEventM.CaseCode >= 1000)
                    v.CaseNumM += "0" + wbsC.WBSEventM.CaseCode.ToString();
                else
                    v.CaseNumM += wbsC.WBSEventM.CaseCode.ToString();
                v.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                v.HasActtachment = "";
                if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 1).Count() > 0)
                    v.HasActtachment = "@";

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

                //var qryF = db.Files.Where(x => x.CID == wbsC.Num && x.Class == 1); //問題附件
                //foreach (Files f in qryF)
                //{
                //    v.HasActtachment = "@";
                //}                            
                v.EventStatusStr = (wbsC.EventStatus == 0) ? "0" + wbsC.EventStatus.ToString() : wbsC.EventStatus.ToString();
                v.EventStatusStr += "." + GetEventStatusStr(wbsC.EventStatus);

                v.EventClassStr = wbsC.EventClass + "-" + db.EventClass.Where(x => x.Class == wbsC.EventClass).Select(x => x.Name).First();
                v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName;
                v.CustomerPhone1 = wbsC.WBSEventM.CustomerUserPhone1;
                v.CustomerPhone = wbsC.WBSEventM.CustomerUserPhone1;
                if (wbsC.WBSEventM.CustomerUserPhone2 != null && wbsC.WBSEventM.CustomerUserPhone2 != "")
                    v.CustomerPhone += " /" + wbsC.WBSEventM.CustomerUserPhone2;
                v.CustomerCaseNum = wbsC.WBSEventM.CustomerCaseNum;
                v.CustomerUserName = wbsC.WBSEventM.CustomerUserName;
                v.ProductNote = wbsC.ProductClass1.Note; //db.ProductClass.AsEnumerable().Where(x => x.ProductClassID == wbsC.ProductClass).Select(x => x.Note).First();
                v.ProductClass = wbsC.ProductClass;
                v.ItemClass = wbsC.ItemClass;
                v.ItemNote = wbsC.ItemClass1.Note; //db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == wbsC.ItemClass).Select(x => x.Note).First();
                v.QuestionClass = wbsC.QuestionClass;
                v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                v.QuestionDesc = wbsC.QuestionDescription;
                v.CaseCreateDT = wbsC.CreateDateTime;
                System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                //v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                v.CaseCreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.CreateUserID).Select(x => x.UserName).First();

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


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

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

                v.HasServiceActtachment = "";
                if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 4).Count() > 0)
                    v.HasServiceActtachment = "@";

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

                v.AssignUserID = wbsC.AssignUserID;
                v.AssignDT = wbsC.AssignDT;

                if (wbsC.AssignUserID != null)
                {
                    v.AssignDTStr = (wbsC.AssignDT ?? DateTime.Now).ToString("MM/dd HH:mm");
                    v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.AssignUserID).Select(x => x.UserName).First();
                }
                else
                {
                    v.AssignDTStr = "";
                    v.AssignUserName = "";
                }
                v.ResponsibleID = wbsC.ResponsibleID.GetValueOrDefault();
                v.RespondSW = wbsC.RespondSW;
                v.RespondDT = wbsC.RespondDT;
                v.RespondReason = wbsC.RespondReason;
                v.ScheduleStartDT = wbsC.ScheduleStartDT;
                v.ScheduleEndDT = wbsC.ScheduleEndDT;
                v.ServiceHours = wbsC.ScheduleServiceHours ?? wbsC.QuestionServiceHours;
                v.ModifyDT = wbsC.AssignModifyDT;
                v.ModifyUserID = wbsC.ModifyUserID;

                v.ServiceArriveDT = wbsC.ServiceArriveDT;
                v.ServiceArriveSysDT = wbsC.ServiceArriveSysDT;
                v.ServiceFinishDT = wbsC.ServiceLeaveDT;
                v.ServiceFinishSysDT = wbsC.ServiceLeaveSysDT;

                if (wbsC.ServiceReport != null)
                    v.ServiceReport = wbsC.ServiceReport;
                else
                    v.ServiceReport = "";

                String RegionID = wbsC.WBSEventM.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();
                String RegionID2 = "";
                int RID1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID).First();
                int RID2 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID2).First() ?? 0;
                RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID1).Select(x2 => x2.UserID).First();

                if (RID2 != 0)
                {
                    RegionID2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.UserID).First();
                    v.ResponsibleArea2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.Name).First();
                    v.DefaultUserName2 = v.ResponsibleArea2 + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == RegionID2).Select(x => x.UserName).First();
                    v.DefaultResponsibleID2 = RID2;
                }
                v.DefaultResponsibleID = RID1;
                v.ResponsibleArea = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID1).Select(x2 => x2.Name).First();
                v.DefaultUserName = wbsC.WBSEventM.Customer1.SubArea.ResponsibleClass.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();

                if (wbsC.ResponsibleID != null)
                {
                    //RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                    //v.ResponsibleUserName = wbsC.ResponsibleClass.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                    v.ResponsibleUserName = wbsC.ResponsibleClass.Name + "-" + wbsC.ResponsibleClass.UserData2.UserName;
                }

                if (wbsC.ProvideSpareMach == "T")
                    v.ProvideSpareMachStr = "◎";
                else
                    v.ProvideSpareMachStr = "";
                if (wbsC.RetrieveSpareMach == "T")
                    v.RetrieveSpareMachStr = "◎";
                else
                    v.RetrieveSpareMachStr = "";
                if (wbsC.RetrieveUserMach == "T")
                    v.RetrieveUserMachStr = "◎";
                else
                    v.RetrieveUserMachStr = "";
                if (wbsC.ReturnUserMach == "T")
                    v.ReturnUserMachStr = "◎";
                else
                    v.ReturnUserMachStr = "";
                ret.Add(v);

                //v.MCaseID = wbsC.WBSEventM.CaseID;
                //v.CCaseNum = wbsC.Num;

                //v.CreateDT = wbsC.CreateDateTime;
                //v.CreateUserID = wbsC.CreateUserID;
                //v.ScheduleStartDT = wbsC.ExpectArriveDateTime;
                //v.ScheduleEndDT = wbsC.ExpectFinishDateTime;
                //v.ServiceHours = wbsC.QuestionClass1.QuestionServiceHours; 
                //v.EventStatus = wbsC.EventStatus.ToString();
                ////if (wbsC.EventStatus >= 50 )
                ////    v.TraceStatus = "完成";
                ////else if(wbsC.EventStatus == 0)
                ////    v.TraceStatus = "刪除";
                ////else
                ////    v.TraceStatus = "追蹤";
                //v.EventStatusStr = (wbsC.EventStatus == 0) ? "0" + wbsC.EventStatus.ToString() : wbsC.EventStatus.ToString();
                //v.EventStatusStr += "." + GetEventStatusStr(wbsC.EventStatus);

                //if (wbsC.ServiceReport != null)
                //    v.ServiceReport = wbsC.ServiceReport;
                //else
                //    v.ServiceReport = "";

                //v.EventClass = wbsC.EventClass;
                //v.EventClassStr = v.EventClass + " : " + db.EventClass.AsEnumerable().Where(x => x.Class == wbsC.EventClass).Select(x => x.Name).First();

                //v.CaseNumM = wbsC.WBSEventM.CaseClass + wbsC.WBSEventM.CaseDate + "-";
                //if (wbsC.WBSEventM.CaseCode < 10)
                //    v.CaseNumM += "0000" + wbsC.WBSEventM.CaseCode.ToString();
                //else if (wbsC.WBSEventM.CaseCode < 100 && wbsC.WBSEventM.CaseCode >= 10)
                //    v.CaseNumM += "000" + wbsC.WBSEventM.CaseCode.ToString();
                //else if (wbsC.WBSEventM.CaseCode < 1000 && wbsC.WBSEventM.CaseCode >= 100)
                //    v.CaseNumM += "00" + wbsC.WBSEventM.CaseCode.ToString();
                //else if (wbsC.WBSEventM.CaseCode < 10000 && wbsC.WBSEventM.CaseCode >= 1000)
                //    v.CaseNumM += "0" + wbsC.WBSEventM.CaseCode.ToString();
                //else
                //    v.CaseNumM += wbsC.WBSEventM.CaseCode.ToString();
                //v.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                //v.HasActtachment = "";
                //if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 1).Count() > 0)
                //    v.HasActtachment = "@";

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

                //v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName;
                //v.CustomerPhone1 = wbsC.WBSEventM.CustomerUserPhone1;
                //v.CustomerPhone = wbsC.WBSEventM.CustomerUserPhone1;
                //if (wbsC.WBSEventM.CustomerUserPhone2 != null && wbsC.WBSEventM.CustomerUserPhone2 != "")
                //    v.CustomerPhone += " /" + wbsC.WBSEventM.CustomerUserPhone2;
                //v.CustomerUserName = wbsC.WBSEventM.CustomerUserName;
                //v.ProductNote = wbsC.ProductClass1.Note;
                //v.ProductClass = wbsC.ProductClass;
                //v.ItemClass = wbsC.ItemClass;
                //v.ItemNote = wbsC.ItemClass1.Note;
                //v.QuestionClass = wbsC.QuestionClass;
                //v.QuestionNote = wbsC.QuestionClass1.Note;
                //v.QuestionDesc = wbsC.QuestionDescription;
                //v.CaseCreateDT = wbsC.CreateDateTime;
                //System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                //v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("MM/dd HH:mm");
                //v.CaseCreateDT = wbsC.CreateDateTime.Value;
                //v.CaseCreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.CreateUserID).Select(x => x.UserName).First();

                //v.HasAssignActtachment = "";
                //if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 2).Count() > 0)
                //    v.HasAssignActtachment = "@";
                
                //v.HasKnowledge = "";
                //if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 3).Count() > 0)
                //    v.HasKnowledge = "@";
                
                //v.HasServiceActtachment = "";
                //if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 4).Count() > 0)
                //    v.HasServiceActtachment = "@";

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

                //v.AssignUserID = wbsC.AssignUserID;
                //v.AssignDT = wbsC.AssignDT;

                //if (wbsC.AssignUserID != null)
                //{
                //    v.AssignDTStr = (wbsC.AssignDT ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                //    v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.AssignUserID).Select(x => x.UserName).First();
                //}
                //else
                //{
                //    v.AssignDTStr = "";
                //    v.AssignUserName = "";
                //}
                //v.ResponsibleID = wbsC.ResponsibleID.GetValueOrDefault();
                //v.RespondSW = wbsC.RespondSW;
                //v.RespondDT = wbsC.RespondDT;
                //v.RespondReason = wbsC.RespondReason;
                //v.ScheduleStartDT = wbsC.ScheduleStartDT;
                //v.ScheduleEndDT = wbsC.ScheduleEndDT;
                //v.ServiceHours = wbsC.ScheduleServiceHours ?? wbsC.QuestionServiceHours;
                //v.ModifyDT = wbsC.AssignModifyDT;
                //v.ModifyUserID = wbsC.ModifyUserID;

                //if (wbsC.ServiceLeaveDT != null)
                //{
                //    v.ServiceFinishDTStr = wbsC.ServiceLeaveDT.Value.ToString("yyyy/MM/dd HH:mm");
                //    v.ServiceFinishDT = wbsC.ServiceLeaveDT;
                //    v.ServiceFinishSysDTStr = wbsC.ServiceLeaveSysDT.Value.ToString("yyyy/MM/dd HH:mm");
                //    v.ServiceFinishSysDT = wbsC.ServiceLeaveSysDT;
                //}
                //else
                //{
                //    v.ServiceFinishDTStr = "";
                //}

                //String RegionID = wbsC.WBSEventM.Customer1.RegionID; 
                //String RegionID2 = "";
                //int RID1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID).First();
                //int RID2 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID2).First() ?? 0;
                //RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID1).Select(x2 => x2.UserID).First();

                //if (RID2 != 0)
                //{
                //    RegionID2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.UserID).First();
                //    v.ResponsibleArea2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.SubAreaID).First();
                //    v.DefaultUserName2 = db.UserData.AsEnumerable().Where(x => x.UserID == RegionID2).Select(x => x.UserName).First();
                //}

                //v.DefaultUserName = wbsC.WBSEventM.Customer1.SubArea.ResponsibleClass.Name + "-" + 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();
                //if (wbsC.ResponsibleID != null)
                //{
                //    RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                //    v.ResponsibleUserName =wbsC.ResponsibleClass.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                //}

                //if (wbsC.ProvideSpareMach == "T")
                //    v.ProvideSpareMachStr = "◎";
                //else
                //    v.ProvideSpareMachStr = "";
                //if (wbsC.RetrieveSpareMach == "T")
                //    v.RetrieveSpareMachStr = "◎";
                //else
                //    v.RetrieveSpareMachStr = "";
                //if (wbsC.RetrieveUserMach == "T")
                //    v.RetrieveUserMachStr = "◎";
                //else
                //    v.RetrieveUserMachStr = "";
                //if (wbsC.ReturnUserMach == "T")
                //    v.ReturnUserMachStr = "◎";
                //else
                //    v.ReturnUserMachStr = "";
                //if (wbsC.ServiceArriveDT != null)
                //{
                //    v.ArriveDTStr = wbsC.ServiceArriveDT.Value.ToString("yyyy/MM/dd HH:mm");
                //    v.ArriveSysDTStr = wbsC.ServiceArriveSysDT.Value.ToString("yyyy/MM/dd HH:mm");
                //}
                //v.ArriveDTStr
                //ret.Add(v);
            }

            return ret;
        }
        //手機-所屬區域大類案件列表(20160222取消讀取沒用到得資訊)
        public List<WBSAssignViewModel> AreaEventMobileRead(String uid, String customerStr, String isClosed) //isClosed:true為50<=x<99 false為11<=x<50
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();
           
            //String areaID = "";
            //if(db.SubArea.Where(x => x.ResponsibleClass.UserID == uid).Count()!=0)
            //    areaID = db.SubArea.Where(x => x.ResponsibleClass.UserID == uid).Select(x=>x.AreaID).First();

            var qryA = db.SubArea.Where(x => x.ResponsibleClass.UserID == uid).Select(x => x.AreaID).Distinct();
            foreach (String areaID in qryA)
            {
                var qryW = db.WBSEventC.Where(x => x.WBSEventM.Customer1.SubArea.AreaID == areaID);

                if (isClosed == null || isClosed == "" || isClosed == "false")
                    qryW = qryW.Where(x => x.EventStatus >= 11 && x.EventStatus < 50).OrderByDescending(p => p.CreateDateTime);
                else
                    qryW = qryW.Where(x => x.EventStatus >= 50 && x.WBSEventM.CaseMStatus != "9").OrderByDescending(p => p.CreateDateTime);

                if (customerStr != null && customerStr != "")
                    qryW = qryW.Where(x => x.WBSEventM.Customer1.ShortName.Contains(customerStr)).OrderByDescending(p => p.CreateDateTime);
                //else
                //    qryW = qryW.OrderBy(p => p.CreateDateTime);

                foreach (WBSEventC wbsC in qryW)
                {
                    WBSAssignViewModel v = new WBSAssignViewModel();

                    v.MCaseID = wbsC.WBSEventM.CaseID;
                    v.CaseNumM = wbsC.WBSEventM.CaseClass + wbsC.WBSEventM.CaseDate + "-";
                    if (wbsC.WBSEventM.CaseCode < 10)
                        v.CaseNumM += "0000" + wbsC.WBSEventM.CaseCode.ToString();
                    else if (wbsC.WBSEventM.CaseCode < 100 && wbsC.WBSEventM.CaseCode >= 10)
                        v.CaseNumM += "000" + wbsC.WBSEventM.CaseCode.ToString();
                    else if (wbsC.WBSEventM.CaseCode < 1000 && wbsC.WBSEventM.CaseCode >= 100)
                        v.CaseNumM += "00" + wbsC.WBSEventM.CaseCode.ToString();
                    else if (wbsC.WBSEventM.CaseCode < 10000 && wbsC.WBSEventM.CaseCode >= 1000)
                        v.CaseNumM += "0" + wbsC.WBSEventM.CaseCode.ToString();
                    else
                        v.CaseNumM += wbsC.WBSEventM.CaseCode.ToString();
                    v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName;
                    v.CCaseNum = wbsC.Num;
                    v.CreateDT = wbsC.CreateDateTime;
                    v.ScheduleStartDT = wbsC.ScheduleStartDT;

                    v.QuestionClass = wbsC.QuestionClass;
                    v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                    v.QuestionDesc = wbsC.QuestionDescription;
                    v.CaseCreateDT = wbsC.CreateDateTime;
                    //System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                    if (wbsC.ServiceArriveDT != null)
                    {
                        v.ServiceArriveDT = wbsC.ServiceArriveDT;
                        v.ServiceArriveSysDT = wbsC.ServiceArriveSysDT;
                    }
                    ret.Add(v);
                }
            }
            
            return ret;
        }
        public List<WBSAssignViewModel> EventTraceMobileRead(String mid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();
            int t = int.Parse(mid);
            var qryC = db.WBSEventC.Where(x => x.WBSEventM.CaseID == t ).OrderByDescending(p => p.CreateDateTime);
            //qryC = qryC.Where(x => x.CreateDateTime.Value.CompareTo(S) >= 0 && x.CreateDateTime.Value.CompareTo(E) <= 0)
            foreach (WBSEventC wbsC in qryC)
            {
                WBSAssignViewModel v = new WBSAssignViewModel();

                v.MCaseID = wbsC.WBSEventM.CaseID;
                v.EventStatusStr = GetEventStatusStr(wbsC.EventStatus);                
                v.ResponsibleUserName = wbsC.ResponsibleClass.Name + "-" + wbsC.ResponsibleClass.UserData2.UserName;
                v.CCaseNum = wbsC.Num;
                v.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                v.CaseCreateDT = wbsC.CreateDateTime;
                v.ServiceFinishDT = wbsC.QuestionFinishDT;
                ret.Add(v);
            }

            return ret;
        }
        //手機-已接受未排程案件列表
        public List<WBSAssignViewModel> AcceptMobileRead(String uid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            //var qryR = db.ResponsibleClass.Where(x => x.UserID == uid);

            //foreach (ResponsibleClass R in qryR)
            //{
                //var qryC = db.WBSEventC.Where(x => x.ResponsibleID == R.ID && x.EventStatus == 30).OrderBy(p => p.ExpectArriveDateTime);
                var qryC = db.WBSEventC.Where(x => x.ResponsibleClass.UserID == uid && x.EventStatus == 30).OrderBy(p => p.CreateDateTime);
                foreach (WBSEventC wbsC in qryC)
                {
                    WBSAssignViewModel v = new WBSAssignViewModel();

                    //var qryM = db.WBSEventM.Where(x => x.CaseID == wbsC.MomCaseID);
                    //foreach (WBSEventM m in qryM)
                    //{
                    //    v.MCaseID = m.CaseID;
                    //    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.CustomerSName = m.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                    //    v.CustomerPhone1 = m.CustomerUserPhone1;
                    //    v.CustomerUserName = m.CustomerUserName;


                    //    String temp = m.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();

                    //    int temp1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == temp).Select(x2 => x2.ResponsibleID).First();
                    //    temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == temp1).Select(x2 => x2.UserID).First();
                    //    v.DefaultUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                    //    if (wbsC.ResponsibleID != null)
                    //    {
                    //        temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                    //        v.ResponsibleUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                    //    }
                    //}

                    v.MCaseID = wbsC.WBSEventM.CaseID;
                    v.CaseNumM = wbsC.WBSEventM.CaseClass + wbsC.WBSEventM.CaseDate + "-";
                    //if (wbsC.WBSEventM.CaseCode < 10)
                    //    v.CaseNumM += "0000" + wbsC.WBSEventM.CaseCode.ToString();
                    //else if (wbsC.WBSEventM.CaseCode < 100 && wbsC.WBSEventM.CaseCode >= 10)
                    //    v.CaseNumM += "000" + wbsC.WBSEventM.CaseCode.ToString();
                    //else if (wbsC.WBSEventM.CaseCode < 1000 && wbsC.WBSEventM.CaseCode >= 100)
                    //    v.CaseNumM += "00" + wbsC.WBSEventM.CaseCode.ToString();
                    //else if (wbsC.WBSEventM.CaseCode < 10000 && wbsC.WBSEventM.CaseCode >= 1000)
                    //    v.CaseNumM += "0" + wbsC.WBSEventM.CaseCode.ToString();
                    //else
                    //    v.CaseNumM += wbsC.WBSEventM.CaseCode.ToString();
                    v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                    //v.CustomerPhone1 = wbsC.WBSEventM.CustomerUserPhone1;
                    //v.CustomerUserName = wbsC.WBSEventM.CustomerUserName;


                    //String temp = wbsC.WBSEventM.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();

                    //int temp1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == temp).Select(x2 => x2.ResponsibleID).First();
                    //temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == temp1).Select(x2 => x2.UserID).First();
                    //v.DefaultUserName = wbsC.WBSEventM.Customer1.SubArea.ResponsibleClass.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                    //if (wbsC.ResponsibleID != null)
                    //{
                    //    temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                    //    v.ResponsibleUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                    //}


                    v.CCaseNum = wbsC.Num;
                    v.CreateDT = wbsC.CreateDateTime;
                    v.CreateUserID = wbsC.CreateUserID;
                    //v.ScheduleStartDT = wbsC.ExpectArriveDateTime;
                    //v.ScheduleEndDT = wbsC.ExpectFinishDateTime;
                    //v.ServiceHours = wbsC.QuestionClass1.QuestionServiceHours; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.QuestionServiceHours).First();

                    //v.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                    //v.HasActtachment = "";
                    //var qryF = db.Files.Where(x => x.CID == wbsC.Num);
                    //foreach (Files f in qryF)
                    //{
                    //    v.HasActtachment = "@";
                    //}

                    //v.ProductNote = wbsC.ProductClass1.Note; //db.ProductClass.AsEnumerable().Where(x => x.ProductClassID == wbsC.ProductClass).Select(x => x.Note).First();
                    //v.ProductClass = wbsC.ProductClass;
                    //v.ItemClass = wbsC.ItemClass;
                    //v.ItemNote = wbsC.ItemClass1.Note; //db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == wbsC.ItemClass).Select(x => x.Note).First();
                    v.QuestionClass = wbsC.QuestionClass;
                    v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                    v.QuestionDesc = wbsC.QuestionDescription;
                    v.CaseCreateDT = wbsC.CreateDateTime;
                    //System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                    //v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    //v.CaseCreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.CreateUserID).Select(x => x.UserName).First();

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

                    //v.AssignUserID = wbsC.AssignUserID;
                    //v.AssignDT = wbsC.AssignDT;

                    //if (wbsC.AssignUserID != null)
                    //{
                    //    v.AssignDTStr = (wbsC.AssignDT ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    //    v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.AssignUserID).Select(x => x.UserName).First();
                    //}
                    //else
                    //{
                    //    v.AssignDTStr = "";
                    //    v.AssignUserName = "";
                    //}
                    //v.ResponsibleID = wbsC.ResponsibleID.GetValueOrDefault();
                    //v.RespondSW = wbsC.RespondSW;
                    //v.RespondDT = wbsC.RespondDT;
                    //v.RespondReason = wbsC.RespondReason;
                    //v.ServiceArriveDT = wbsC.ServiceArriveDT;
                    //v.ScheduleStartDT = wbsC.ScheduleStartDT;
                    //v.ScheduleEndDT = wbsC.ScheduleEndDT;
                    //v.ServiceHours = wbsC.ScheduleServiceHours ?? wbsC.QuestionServiceHours;
                    //v.ModifyDT = wbsC.AssignModifyDT;
                    //v.ModifyUserID = wbsC.ModifyUserID;

                    ret.Add(v);
                }
            //}

            return ret;
        }
        //手機-由我發動的案件列表->與我相關的案件(20151014)(20160222取消讀取沒用到得資訊,20160317加入客戶簡稱查詢)
        public List<WBSAssignViewModel> CreateByMeMobileRead(String uid, String customerStr)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();
            //qry2 = qry2.Where(x => x.CreateUserID == uid || x.AssignUserID == uid || x.ServiceCreateUserID == uid || x.ResponsibleClass.UserID == uid);
            var qryC = db.WBSEventC.Where(x => x.CreateUserID == uid || x.AssignUserID == uid || x.ServiceCreateUserID == uid 
                || x.ResponsibleClass.UserID == uid || x.WBSEventM.Customer1.SubArea.ResponsibleClass.UserID == uid);
            qryC = qryC.Where(x => x.EventStatus != 0 && x.WBSEventM.CaseMStatus != "9").OrderByDescending(p => p.CreateDateTime);
            if (customerStr != null && customerStr != "")
                qryC = qryC.Where(x => x.WBSEventM.Customer1.ShortName.Contains(customerStr)).OrderByDescending(p => p.CreateDateTime);

            //var qryC = db.WBSEventC.Where(x => x.CreateUserID == uid).OrderBy(p => p.ExpectArriveDateTime);
            foreach (WBSEventC wbsC in qryC)
            {
                WBSAssignViewModel v = new WBSAssignViewModel();

                if (wbsC.CreateUserID == uid)
                    v.EventNote += "開單 ";
                if (wbsC.WBSEventM.Customer1.SubArea.ResponsibleClass.UserID == uid)
                    v.EventNote += "責任區 ";
                if (wbsC.AssignUserID == uid)
                    v.EventNote += "派工 ";
                if (wbsC.ResponsibleClass != null)
                    if (wbsC.ResponsibleClass.UserID == uid)
                        v.EventNote += "執行 ";                
                if (wbsC.ServiceCreateUserID == uid)
                    v.EventNote += "實際執行 ";


                v.MCaseID = wbsC.WBSEventM.CaseID;
                v.CaseNumM = wbsC.WBSEventM.CaseClass + wbsC.WBSEventM.CaseDate + "-";
                if (wbsC.WBSEventM.CaseCode < 10)
                    v.CaseNumM += "0000" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 100 && wbsC.WBSEventM.CaseCode >= 10)
                    v.CaseNumM += "000" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 1000 && wbsC.WBSEventM.CaseCode >= 100)
                    v.CaseNumM += "00" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 10000 && wbsC.WBSEventM.CaseCode >= 1000)
                    v.CaseNumM += "0" + wbsC.WBSEventM.CaseCode.ToString();
                else
                    v.CaseNumM += wbsC.WBSEventM.CaseCode.ToString();
                v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                v.EventStatusStr = GetEventStatusStr(wbsC.EventStatus);
                v.CCaseNum = wbsC.Num;
                v.QuestionClass = wbsC.QuestionClass;
                //v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                v.QuestionDesc = wbsC.QuestionDescription;
                v.CaseCreateDT = wbsC.CreateDateTime;
                System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");

                ret.Add(v);
            }
            return ret;
        }
        //手機-實際執行人已解決未結案
        public List<WBSAssignViewModel> ResolvedMobileRead(String uid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();
            //int day = -7;
            //var qryR = db.ResponsibleClass.Where(x => x.UserID == uid);
            //DateTime temp2 = DateTime.Today.AddDays(day);
            //DateTime S = DateTime.ParseExact(Sdate,
            //                      "yyyyMMdd",
            //                       CultureInfo.InvariantCulture);
            //DateTime E = DateTime.ParseExact(Edate,
            //                      "yyyyMMdd",
            //                       CultureInfo.InvariantCulture).AddDays(1);
            //foreach (ResponsibleClass R in qryR)
            //{
            var qryC = db.WBSEventC.Where(x => x.ServiceModifyUserID == uid && x.EventStatus >= 46 && x.EventStatus <= 50 && x.WBSEventM.CaseMStatus != "9").OrderByDescending(p => p.CreateDateTime);
                //qryC = qryC.Where(x => x.CreateDateTime.Value.CompareTo(S) >= 0 && x.CreateDateTime.Value.CompareTo(E) <= 0)
            foreach (WBSEventC wbsC in qryC)
            {
                WBSAssignViewModel v = new WBSAssignViewModel();

                v.MCaseID = wbsC.WBSEventM.CaseID;
                v.EventStatusStr = GetEventStatusStr(wbsC.EventStatus);
                v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                    
                v.CCaseNum = wbsC.Num;                
                v.CreateUserID = wbsC.CreateUserID;
                v.QuestionClass = wbsC.QuestionClass;
                //v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                v.QuestionDesc = wbsC.QuestionDescription;
                v.CaseCreateDT = wbsC.CreateDateTime;
                v.CaseCreateDT = wbsC.CreateDateTime;
                ret.Add(v);
            }            

            return ret;
        }
        //手機-未結案案件列表
        public List<WBSAssignViewModel> UnresolvedMobileRead(String uid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            var qryC = db.WBSEventC.Where(x => x.ResponsibleClass.UserID == uid && x.EventStatus >= 21 && x.EventStatus < 50).OrderByDescending(p => p.CreateDateTime);
            foreach (WBSEventC wbsC in qryC)
            {
                WBSAssignViewModel v = new WBSAssignViewModel();
                v.MCaseID = wbsC.WBSEventM.CaseID;
                v.EventStatusStr = GetEventStatusStr(wbsC.EventStatus);
                v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                v.CCaseNum = wbsC.Num;
                v.CreateDT = wbsC.CreateDateTime;
                v.CreateUserID = wbsC.CreateUserID;
                v.QuestionClass = wbsC.QuestionClass;
                //v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                v.QuestionDesc = wbsC.QuestionDescription;
                v.CaseCreateDT = wbsC.CreateDateTime;
                v.RespondSW = wbsC.RespondSW;
                if(v.RespondSW != "N")
                    ret.Add(v);
            }
            return ret;
        }
        /*
        public IQueryable<WBSEventViewModel> Read(int CaseID)
        {
            List<WBSEventViewModel> ret = new List<WBSEventViewModel>();
            var qry = db.WBSEventM.Where(x => x.CaseID == CaseID);
            foreach (WBSEventM m in qry)
            {
                var qry2 = db.WBSEventC.Where(x => x.MomCaseID == m.CaseID);
                var qry3 = db.Customer.Where(x => x.CustomerID == m.CustomerID);
                String Addr = "";
                foreach (Customer o in qry3)
                {
                    Addr = o.Addr;
                }

                foreach (WBSEventC c in qry2)
                {
                    WBSEventViewModel v = new WBSEventViewModel();
                    v.M_ID = m.CaseID;
                    v.C_ID = c.Num.ToString();
                    v.RB_CasseDate = m.RB_CasseDate;
                    v.RB_CasseNumber = m.RB_CasseNumber.Value;
                    v.WBSCaseNumberM = m.CaseClass + m.CaseDate + m.CaseCode;
                    v.WBSCaseNumberC = (c.ChildCaseID < 10) ? ("0" + c.ChildCaseID.ToString()) : c.ChildCaseID.ToString();
                    v.EventClass = c.EventClass;
                    v.CustomerGroupSW = m.CustomerGroupSW;
                    v.CustomerID = m.CustomerID;
                    v.CustomerAddr = Addr;
                    v.CustomerUserID = m.CustomerUserID;
                    v.CustomerUserName = m.CustomerUserName;
                    v.CustomerUserPhone1 = m.CustomerUserPhone1;
                    v.CustomerUserPhone2 = m.CustomerUserPhone2;
                    v.ProductClass = c.ProductClass.ToString();
                    v.ItemClass = c.ItemClass.ToString();
                    v.QuestionClass = c.QuestionClass.ToString();
                    v.QuestionGrade = c.QuestionGrade.ToString();
                    v.QuestionServiceHours = c.QuestionServiceHours.ToString();
                    v.QuestionDescription = c.QuestionDescription;
                    v.QuestionActtachment = c.QuestionActtachment;
                    v.QuestionCreateUserID = c.QuestionCreateUserID;
                    v.QuestionCreateDT = c.QuestionCreateDateTime.ToString();
                    v.ExpectArriveDT = c.ExpectArriveDateTime.ToString();
                    v.ExpectFinishDT = c.ExpectFinishDateTime.ToString();
                    v.ExpectArriveSW = c.ExpectArriveSW;
                    v.DeleteMarkSW = c.DeleteMarkSW;
                    v.DeleteReason = c.DeleteReason;
                    v.DeleteMarkUserID = c.DeleteMarkUserID;
                    v.DeleteMarkDT = c.DeleteMarkDateTime.ToString();
                    v.CreateDT = c.CreateDateTime.ToString();
                    v.CreateUserID = c.CreateUserID;
                    v.ModifyDT = c.ModifyDateTime.ToString();
                    v.ModifyUserID = c.ModifyUserID;

                    ret.Add(v);
                }
            }
            return ret.AsQueryable();
        }

        public bool Update(WBSEventViewModel wBSEventViewModel)
        {
            bool ret = false;
            try
            {
                WBSEventM wBSEventM = new WBSEventM();
                wBSEventM.CaseID = wBSEventViewModel.M_ID;
                wBSEventM.CaseClass = wBSEventViewModel.WBSCaseNumberM.Substring(0, 1);
                wBSEventM.CaseDate = wBSEventViewModel.WBSCaseNumberM.Substring(1, 8);
                wBSEventM.CaseCode = Int32.Parse(wBSEventViewModel.WBSCaseNumberM.Substring(9, 5));
                wBSEventM.RB_CasseDate = wBSEventViewModel.RB_CasseDate;
                wBSEventM.RB_CasseNumber = wBSEventViewModel.RB_CasseNumber;
                wBSEventM.CustomerID = wBSEventViewModel.CustomerID;
                wBSEventM.CustomerGroupSW = wBSEventViewModel.CustomerGroupSW;
                wBSEventM.CustomerUserID = wBSEventViewModel.CustomerUserID;
                wBSEventM.CustomerUserName = wBSEventViewModel.CustomerUserName;
                wBSEventM.CustomerUserPhone1 = wBSEventViewModel.CustomerUserPhone1;
                wBSEventM.CustomerUserPhone2 = wBSEventViewModel.CustomerUserPhone2;

                db.SaveChanges();
            }
            catch
            {
            }
            return ret;
        }

        public bool Delete(WBSEventViewModel wBSEventViewModel)
        {
            bool ret = false;
            try
            {
                WBSEventM wBSEventM = db.WBSEventM.Find(wBSEventViewModel.M_ID);
                db.WBSEventM.Remove(wBSEventM);
                db.SaveChanges();
                ret = true;
            }
            catch
            {
            }
            return ret;
        }*/
                
        //手機-已派工未接受列表
        public List<WBSAssignViewModel> UnconfirmedMobileRead(String uid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            //var qryR = db.ResponsibleClass.Where(x => x.UserID == uid);
            //foreach (ResponsibleClass R in qryR)
            //{
                var qryC = db.WBSEventC.Where(x => x.ResponsibleClass.UserID == uid && x.RespondSW == null && x.EventStatus != 0).OrderBy(p => p.CreateDateTime);
                //var qryC = db.WBSEventC.Where(x => x.ResponsibleID == R.ID && x.RespondSW == null && x.EventStatus != 0).OrderBy(p => p.ExpectArriveDateTime);
                foreach (WBSEventC wbsC in qryC)
                {
                    WBSAssignViewModel v = new WBSAssignViewModel();

                    v.MCaseID = wbsC.WBSEventM.CaseID;
                    v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                    
                    v.CCaseNum = wbsC.Num;
                    v.CreateDT = wbsC.CreateDateTime;
                    v.CreateUserID = wbsC.CreateUserID;
                    v.QuestionDesc = wbsC.QuestionDescription;
                    v.CaseCreateDT = wbsC.CreateDateTime;
                    System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                    v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    if (wbsC.ExpectArriveSW == "0")
                    {
                        v.ExpectArriveSW = "自行約定";
                        v.ExpectArriveDTStr = "自行約定";
                    }
                    else if (wbsC.ExpectArriveSW == "1")
                        v.ExpectArriveSW = "合約規定";
                    else if (wbsC.ExpectArriveSW == "2")
                        v.ExpectArriveSW = "建議期望";
                    else if (wbsC.ExpectArriveSW == "4")
                        v.ExpectArriveSW = "自派新單";
                    else
                        v.ExpectArriveSW = "客戶指定";

                    ret.Add(v);
                }
            //}

            return ret;
        }
        //手機-異常案件列表
        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<WBSAssignViewModel> MaintainList(int cid) 
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            var qryE = db.EventRelation.Where(x => x.CID == cid);
            foreach (EventRelation e in qryE) { 
                var qryC = db.WBSEventC.Where(x => x.Num == e.RelationCID);
                foreach (WBSEventC wbsC in qryC) {
                    WBSAssignViewModel v = new WBSAssignViewModel();

                    var qryM = db.WBSEventM.Where(x => x.CaseID == wbsC.MomCaseID);
                    foreach (WBSEventM m in qryM) {
                        v.MCaseID = m.CaseID;
                        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.CustomerSName = m.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                        v.CustomerPhone1 = m.CustomerUserPhone1;
                        v.CustomerPhone = m.CustomerUserPhone1;
                        if (m.CustomerUserPhone2 != null && m.CustomerUserPhone2 != "")
                            v.CustomerPhone += " /" + m.CustomerUserPhone2;
                        v.CustomerUserName = m.CustomerUserName;
                        v.CustomerAddr = m.Customer1.Addr; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.Addr).First();
                        if (m.CustomerAddr != null)
                            v.CustomerAddr = m.CustomerAddr;

                        String temp = m.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();

                        int temp1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == temp).Select(x2 => x2.ResponsibleID).First();
                        temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == temp1).Select(x2 => x2.UserID).First();
                        v.DefaultUserName = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == temp1).Select(x2 => x2.Name).First() +"-" + db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                        if (wbsC.ResponsibleID != null)
                        {
                            temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                            v.ResponsibleUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                        }
                    }
                                        
                    v.CCaseNum = wbsC.Num;
                    v.CreateDT = wbsC.CreateDateTime;
                    v.CreateUserID = wbsC.CreateUserID;
                    v.ScheduleStartDT = wbsC.ExpectArriveDateTime;
                    v.ScheduleEndDT = wbsC.ExpectFinishDateTime;
                    v.ServiceHours = wbsC.QuestionClass1.QuestionServiceHours; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.QuestionServiceHours).First();
                    
                    v.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                    
                    v.HasActtachment = "";
                    if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 1).Count() > 0) //問題附件
                        v.HasActtachment = "@";

                    v.ProductNote = wbsC.ProductClass1.Note; //db.ProductClass.AsEnumerable().Where(x => x.ProductClassID == wbsC.ProductClass).Select(x => x.Note).First();
                    v.ProductClass = wbsC.ProductClass;
                    v.ItemClass = wbsC.ItemClass;
                    v.ItemNote = wbsC.ItemClass1.Note; //db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == wbsC.ItemClass).Select(x => x.Note).First();
                    v.QuestionClass = wbsC.QuestionClass;
                    v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                    v.QuestionDesc = wbsC.QuestionDescription;
                    v.CaseCreateDT = wbsC.CreateDateTime;
                    System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                    v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                    v.CaseCreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.CreateUserID).Select(x => x.UserName).First();

                    v.HasAssignActtachment = "";
                    if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 2).Count() > 0) //解決說明附件
                        v.HasAssignActtachment = "@";

                    v.HasKnowledge = "";
                    if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 3).Count() > 0) //解決說明路徑
                        v.HasKnowledge = "@";
                    v.ExpectArriveDT = wbsC.ExpectArriveDateTime;
                    v.ExpectArriveDTStr = (wbsC.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                    v.ExpectFinishDT = wbsC.ExpectArriveDateTime;
                    v.ExpectFinishDTStr = (wbsC.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");


                    if (wbsC.ExpectArriveSW == "0")
                        v.ExpectArriveSW = "自行約定";
                    else if (wbsC.ExpectArriveSW == "1")
                        v.ExpectArriveSW = "合約規定";
                    else if (wbsC.ExpectArriveSW == "2")
                        v.ExpectArriveSW = "建議期望";
                    else if (wbsC.ExpectArriveSW == "4")
                        v.ExpectArriveSW = "自派新單";
                    else
                        v.ExpectArriveSW = "客戶指定";
                    v.isSelect = false;

                    v.AssignUserID = wbsC.AssignUserID;
                    v.AssignDT = wbsC.AssignDT;

                    if (wbsC.AssignUserID != null) {
                        v.AssignDTStr = (wbsC.AssignDT ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                        v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.AssignUserID).Select(x => x.UserName).First();                        
                    }
                    else {
                        v.AssignDTStr = "";
                        v.AssignUserName = "";
                    }
                    v.ResponsibleID = wbsC.ResponsibleID.GetValueOrDefault();
                    v.RespondSW = wbsC.RespondSW;
                    v.RespondDT = wbsC.RespondDT;
                    v.RespondReason = wbsC.RespondReason;
                    v.ScheduleStartDT = wbsC.ScheduleStartDT;
                    v.ScheduleEndDT = wbsC.ScheduleEndDT;
                    v.ServiceHours = wbsC.ScheduleServiceHours ?? wbsC.QuestionServiceHours;
                    v.ModifyDT = wbsC.AssignModifyDT;
                    v.ModifyUserID = wbsC.ModifyUserID;
                    
                    ret.Add(v);
                }
            }

            return ret.AsQueryable().OrderBy(p => p.CaseNumM);
        }
        //客戶叫修服務-可預先保養列表
        public IQueryable<WBSAssignViewModel> ReadMaintainCase(String customerRef)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();
            int cRef = Int32.Parse(customerRef);
            var qryM = db.WBSEventM.Where(x => x.CustomerRef == cRef && x.CaseClass == "M");
            foreach (WBSEventM m in qryM) {
                var qryWbsC = db.WBSEventC.Where(x => x.MomCaseID == m.CaseID && x.EventStatus < 41); //未排程的
                foreach (WBSEventC wbsC in qryWbsC) {
                    WBSAssignViewModel v = new WBSAssignViewModel();

                    v.MCaseID = m.CaseID;
                    v.CCaseNum = wbsC.Num;

                    v.CreateDT = wbsC.CreateDateTime;
                    v.CreateUserID = wbsC.CreateUserID;
                    v.ScheduleStartDT = wbsC.ExpectArriveDateTime;
                    v.ScheduleEndDT = wbsC.ExpectFinishDateTime;
                    v.ServiceHours = wbsC.QuestionClass1.QuestionServiceHours; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.QuestionServiceHours).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.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                    v.HasActtachment = "";
                    var qryF = db.Files.Where(x => x.CID == wbsC.Num && x.Class == 1); //問題附件
                    foreach (Files f in qryF) {
                        v.HasActtachment = "@";
                    }

                    v.HasHyperlink = "";
                    if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 9).Count() > 0)
                        v.HasHyperlink = "@";
                    //DirectoryInfo di = null;                            
                    //if (wbsC.QuestionActtachment != null)
                    //{
                    //    if (Directory.Exists(HttpContext.Current.Server.MapPath("~" + wbsC.QuestionActtachment.Replace('\\', '/'))))
                    //    {
                    //        di = new DirectoryInfo(HttpContext.Current.Server.MapPath("~" + wbsC.QuestionActtachment.Replace('\\', '/')));
                    //        if (di.GetFiles().Length > 0)
                    //            v.HasActtachment = "@";
                    //    }
                    //}
                    v.CustomerSName = m.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == Int32.Parse(customerRef)).Select(x => x.ShortName).First();
                    v.CustomerPhone1 = m.CustomerUserPhone1;
                    v.CustomerPhone = m.CustomerUserPhone1;
                    if (m.CustomerUserPhone2 != null && m.CustomerUserPhone2 != "")
                        v.CustomerPhone += " /" + m.CustomerUserPhone2;
                    v.CustomerUserName = m.CustomerUserName;
                    v.CustomerAddr = m.Customer1.Addr; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == Int32.Parse(customerRef)).Select(x => x.Addr).First();
                    if (m.CustomerAddr != null)
                        v.CustomerAddr = m.CustomerAddr;
                    v.ProductNote = wbsC.ProductClass1.Note; //db.ProductClass.AsEnumerable().Where(x => x.ProductClassID == wbsC.ProductClass).Select(x => x.Note).First();
                    v.ProductClass = wbsC.ProductClass;
                    v.ItemClass = wbsC.ItemClass;
                    v.ItemNote = wbsC.ItemClass1.Note; //db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == wbsC.ItemClass).Select(x => x.Note).First();
                    v.QuestionClass = wbsC.QuestionClass;
                    v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                    v.QuestionDesc = wbsC.QuestionDescription;
                    v.CaseCreateDT = wbsC.CreateDateTime;
                    System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                    v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.CaseCreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.CreateUserID).Select(x => x.UserName).First();
                    
                    v.HasAssignActtachment = "";
                    var qryA = db.Files.Where(x => x.CID == wbsC.Num && x.Class == 2); //解決說明附件
                    foreach (Files f in qryA) {
                        v.HasAssignActtachment = "@";
                    }
                    //DirectoryInfo di = null;
                    //if (wbsC.QuestionActtachment != null)
                    //{
                    //    if (Directory.Exists(HttpContext.Current.Server.MapPath("~" + wbsC.QuestionActtachment.Replace('\\', '/'))))
                    //    {
                    //        di = new DirectoryInfo(HttpContext.Current.Server.MapPath("~" + wbsC.QuestionActtachment.Replace('\\', '/')));
                    //        if (di.GetFiles().Length > 0)
                    //            v.HasActtachment = "@";
                    //    }
                    //}


                    ////v.WaitingDT = Convert.ToDateTime(diff1.ToString());                
                    v.ExpectArriveDT = wbsC.ExpectArriveDateTime;
                    v.ExpectArriveDTStr = (wbsC.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                    v.ExpectFinishDT = wbsC.ExpectFinishDateTime;
                    v.ExpectFinishDTStr = (wbsC.ExpectFinishDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");


                    if (wbsC.ExpectArriveSW == "0")
                        v.ExpectArriveSW = "自行約定";
                    else if (wbsC.ExpectArriveSW == "1")
                        v.ExpectArriveSW = "合約規定";
                    else if (wbsC.ExpectArriveSW == "2")
                        v.ExpectArriveSW = "建議期望";
                    else if (wbsC.ExpectArriveSW == "4")
                        v.ExpectArriveSW = "自派新單";
                    else
                        v.ExpectArriveSW = "客戶指定";
                    v.isSelect = false;

                    v.AssignUserID = wbsC.AssignUserID;
                    v.AssignDT = wbsC.AssignDT;

                    if (wbsC.AssignUserID != null) {
                        v.AssignDTStr = (wbsC.AssignDT ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                        v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.AssignUserID).Select(x => x.UserName).First();
                    }
                    else {
                        v.AssignDTStr = "";
                        v.AssignUserName = "";
                    }
                    v.ResponsibleID = wbsC.ResponsibleID.GetValueOrDefault();
                    v.RespondSW = wbsC.RespondSW;
                    v.RespondDT = wbsC.RespondDT;
                    v.RespondReason = wbsC.RespondReason;
                    v.ScheduleStartDT = wbsC.ScheduleStartDT;
                    v.ScheduleEndDT = wbsC.ScheduleEndDT;
                    v.ServiceHours = wbsC.ScheduleServiceHours ?? wbsC.QuestionServiceHours;
                    v.ModifyDT = wbsC.AssignModifyDT;
                    v.ModifyUserID = wbsC.ModifyUserID;

                    String temp = m.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();

                    int temp1 = m.Customer1.SubArea.ResponsibleID; //db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == temp).Select(x2 => x2.ResponsibleID).First();
                    temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == temp1).Select(x2 => x2.UserID).First();
                    //v.DefaultUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();

                    v.DefaultUserName = m.Customer1.SubArea.ResponsibleClass.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                    if (wbsC.ResponsibleID != null) {
                        temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                        v.ResponsibleUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                    }

                    ret.Add(v);
                }
            }

            return ret.AsQueryable();
        }
        //案件總覽-本案詳情讀取
        public IQueryable<WBSAssignViewModel> ReadOneCase(int cid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            var qryC = db.WBSEventC.Where(x => x.Num == cid);
            foreach (WBSEventC wbsC in qryC)
            {
                WBSAssignViewModel v = new WBSAssignViewModel();

                v.MCaseID = wbsC.WBSEventM.CaseID;
                v.CCaseNum = wbsC.Num;

                v.CreateDT = wbsC.CreateDateTime;
                v.CreateUserID = wbsC.CreateUserID;
                v.ScheduleStartDT = wbsC.ExpectArriveDateTime;
                v.ScheduleEndDT = wbsC.ExpectFinishDateTime;
                v.ServiceHours = wbsC.QuestionClass1.QuestionServiceHours; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.QuestionServiceHours).First();

                v.CaseNumM = wbsC.WBSEventM.CaseClass + wbsC.WBSEventM.CaseDate + "-";
                if (wbsC.WBSEventM.CaseCode < 10)
                    v.CaseNumM += "0000" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 100 && wbsC.WBSEventM.CaseCode >= 10)
                    v.CaseNumM += "000" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 1000 && wbsC.WBSEventM.CaseCode >= 100)
                    v.CaseNumM += "00" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 10000 && wbsC.WBSEventM.CaseCode >= 1000)
                    v.CaseNumM += "0" + wbsC.WBSEventM.CaseCode.ToString();
                else
                    v.CaseNumM += wbsC.WBSEventM.CaseCode.ToString();
                v.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                v.CaseMStatus = wbsC.WBSEventM.CaseMStatus;
                if (v.CaseMStatus == "9")
                    v.CaseMStatusStr = "已結案";
                else
                    v.CaseMStatusStr = "待結案";


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

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

                v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName;
                v.CustomerPhone1 = wbsC.WBSEventM.CustomerUserPhone1;
                v.CustomerUserName = wbsC.WBSEventM.CustomerUserName;
                v.ProductNote = wbsC.ProductClass1.Note; 
                v.ProductClass = wbsC.ProductClass;
                v.ItemClass = wbsC.ItemClass;
                v.ItemNote = wbsC.ItemClass1.Note; 
                v.QuestionClass = wbsC.QuestionClass;
                v.QuestionNote = wbsC.QuestionClass1.Note; 
                v.QuestionDesc = wbsC.QuestionDescription;
                v.QuestionGrade = wbsC.QuestionGrade.Value;
                v.CaseCreateDT = wbsC.CreateDateTime;
                System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                v.CaseCreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.CreateUserID).Select(x => x.UserName).First();

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


                v.HasKnowledge = "";
                if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 3).Count() > 0)
                    v.HasKnowledge = "@";
                ////v.WaitingDT = Convert.ToDateTime(diff1.ToString());                
                v.ExpectArriveDT = wbsC.ExpectArriveDateTime;
                v.ExpectArriveDTStr = (wbsC.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                if (wbsC.ExpectArriveSW == "0")
                {
                    v.ExpectArriveSW = "自行約定";
                    v.ExpectArriveDTStr = "自行約定";
                }
                else if (wbsC.ExpectArriveSW == "1")
                    v.ExpectArriveSW = "合約規定";
                else if (wbsC.ExpectArriveSW == "2")
                    v.ExpectArriveSW = "建議期望";
                else
                    v.ExpectArriveSW = "客戶指定";
                v.isSelect = false;

                v.AssignUserID = wbsC.AssignUserID;
                v.AssignDT = wbsC.AssignDT;

                if (wbsC.AssignUserID != null)
                {
                    v.AssignDTStr = (wbsC.AssignDT ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                    v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.AssignUserID).Select(x => x.UserName).First();
                }
                else
                {
                    v.AssignDTStr = "";
                    v.AssignUserName = "";
                }
                v.ResponsibleID = wbsC.ResponsibleID.GetValueOrDefault();
                v.RespondSW = wbsC.RespondSW;
                v.RespondDT = wbsC.RespondDT;
                v.RespondReason = wbsC.RespondReason;
                v.ScheduleStartDT = wbsC.ScheduleStartDT;
                v.ScheduleEndDT = wbsC.ScheduleEndDT;
                v.ServiceHours = wbsC.ScheduleServiceHours ?? wbsC.QuestionServiceHours;
                v.ModifyDT = wbsC.AssignModifyDT;
                v.ModifyUserID = wbsC.ModifyUserID;

                string RegionID = wbsC.WBSEventM.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();
                string RegionID2 = "";
                int RID1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID).First();
                int RID2 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID2).First() ?? 0;
                RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID1).Select(x2 => x2.UserID).First();

                if (RID2 != 0)
                {
                    RegionID2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.UserID).First();
                    v.ResponsibleArea2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.SubAreaID).First();
                    
                    v.DefaultUserName2 = db.UserData.AsEnumerable().Where(x => x.UserID == RegionID2).Select(x => x.UserName).First();
                }

                //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.DefaultUserName = wbsC.WBSEventM.Customer1.SubArea.ResponsibleClass.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                if (wbsC.ResponsibleID != null)
                {
                    RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                    v.ResponsibleUserName = db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                }

                ret.Add(v);
            }

            return ret.AsQueryable();
        }
        //案件追蹤
        public List<WBSAssignViewModel> EventTraceRead(int cid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            int mid = db.WBSEventC.Find(cid).MomCaseID;
            //WBSEventM M = db.WBSEventM.Find(mid);

            var qryC = db.WBSEventC.Where(x => x.MomCaseID == mid);
            foreach (WBSEventC wbsC in qryC)
            {
                WBSAssignViewModel v = new WBSAssignViewModel();

                v.MCaseID = wbsC.WBSEventM.CaseID;
                v.CCaseNum = wbsC.Num;

                v.CreateDT = wbsC.CreateDateTime;
                v.CreateUserID = wbsC.CreateUserID;
                v.ScheduleStartDT = wbsC.ExpectArriveDateTime;
                v.ScheduleEndDT = wbsC.ExpectFinishDateTime;
                v.ServiceHours = wbsC.QuestionClass1.QuestionServiceHours; 
                v.EventStatus = wbsC.EventStatus.ToString();
                //if (wbsC.EventStatus >= 50 )
                //    v.TraceStatus = "完成";
                //else if(wbsC.EventStatus == 0)
                //    v.TraceStatus = "刪除";
                //else
                //    v.TraceStatus = "追蹤";
                if (wbsC.EventStatus == 0)
                    v.EventStatusStr = v.EventStatus + ".已刪除";
                else if (wbsC.EventStatus == 11)
                    v.EventStatusStr = v.EventStatus + ".已開單未派工";
                else if (wbsC.EventStatus == 20)
                    v.EventStatusStr = v.EventStatus + ".已拒絕待重派";
                else if (wbsC.EventStatus == 21)
                    v.EventStatusStr = v.EventStatus + ".已派工未接受";
                else if (wbsC.EventStatus == 30)
                    v.EventStatusStr = v.EventStatus + ".已接受未排程";
                else if (wbsC.EventStatus == 31)
                    v.EventStatusStr = v.EventStatus + ".已接受已排程";
                else if (wbsC.EventStatus == 41)
                    v.EventStatusStr = v.EventStatus + ".已到達未離開";
                else if (wbsC.EventStatus == 42)
                    v.EventStatusStr = v.EventStatus + ".已離開待附件";
                else if (wbsC.EventStatus == 46)
                    v.EventStatusStr = v.EventStatus + ".已解決未審核";
                else if (wbsC.EventStatus == 50)
                    v.EventStatusStr = v.EventStatus + ".已解決已審核";


                if (wbsC.ServiceReport != null)
                    v.ServiceReport = wbsC.ServiceReport;
                else
                    v.ServiceReport = "";

                v.EventClass = wbsC.EventClass;
                v.EventClassStr = v.EventClass + " : " + db.EventClass.AsEnumerable().Where(x => x.Class == wbsC.EventClass).Select(x => x.Name).First();

                v.CaseNumM = wbsC.WBSEventM.CaseClass + wbsC.WBSEventM.CaseDate + "-";
                if (wbsC.WBSEventM.CaseCode < 10)
                    v.CaseNumM += "0000" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 100 && wbsC.WBSEventM.CaseCode >= 10)
                    v.CaseNumM += "000" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 1000 && wbsC.WBSEventM.CaseCode >= 100)
                    v.CaseNumM += "00" + wbsC.WBSEventM.CaseCode.ToString();
                else if (wbsC.WBSEventM.CaseCode < 10000 && wbsC.WBSEventM.CaseCode >= 1000)
                    v.CaseNumM += "0" + wbsC.WBSEventM.CaseCode.ToString();
                else
                    v.CaseNumM += wbsC.WBSEventM.CaseCode.ToString();
                v.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                v.HasActtachment = "";
                if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 1).Count() > 0)
                    v.HasActtachment = "@";

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

                v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName;
                v.CustomerPhone1 = wbsC.WBSEventM.CustomerUserPhone1;
                v.CustomerUserName = wbsC.WBSEventM.CustomerUserName;
                v.ProductNote = wbsC.ProductClass1.Note;
                v.ProductClass = wbsC.ProductClass;
                v.ItemClass = wbsC.ItemClass;
                v.ItemNote = wbsC.ItemClass1.Note;
                v.QuestionClass = wbsC.QuestionClass;
                v.QuestionNote = wbsC.QuestionClass1.Note;
                v.QuestionDesc = wbsC.QuestionDescription;
                v.CaseCreateDT = wbsC.CreateDateTime;
                System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                v.CaseCreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.CreateUserID).Select(x => x.UserName).First();

                v.HasAssignActtachment = "";
                if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 2).Count() > 0)
                    v.HasAssignActtachment = "@";
                
                v.HasKnowledge = "";
                if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 3).Count() > 0)
                    v.HasKnowledge = "@";
                
                v.HasServiceActtachment = "";
                if (db.Files.Where(x => x.CID == wbsC.Num && x.Class == 4).Count() > 0)
                    v.HasServiceActtachment = "@";

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

                v.AssignUserID = wbsC.AssignUserID;
                v.AssignDT = wbsC.AssignDT;

                if (wbsC.AssignUserID != null)
                {
                    v.AssignDTStr = (wbsC.AssignDT ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                    v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.AssignUserID).Select(x => x.UserName).First();
                }
                else
                {
                    v.AssignDTStr = "";
                    v.AssignUserName = "";
                }
                v.ResponsibleID = wbsC.ResponsibleID.GetValueOrDefault();
                v.RespondSW = wbsC.RespondSW;
                v.RespondDT = wbsC.RespondDT;
                v.RespondReason = wbsC.RespondReason;
                v.ScheduleStartDT = wbsC.ScheduleStartDT;
                v.ScheduleEndDT = wbsC.ScheduleEndDT;
                v.ServiceHours = wbsC.ScheduleServiceHours ?? wbsC.QuestionServiceHours;
                v.ModifyDT = wbsC.AssignModifyDT;
                v.ModifyUserID = wbsC.ModifyUserID;

                if (wbsC.ServiceLeaveDT != null)
                {
                    v.ServiceFinishDTStr = (wbsC.ServiceLeaveDT ?? DateTime.Now).ToString("yyyy/MM/dd HH:mm");
                    v.ServiceFinishDT = wbsC.ServiceLeaveDT;
                }
                else
                {
                    v.ServiceFinishDTStr = "";
                }

                string RegionID = wbsC.WBSEventM.Customer1.RegionID; 
                string RegionID2 = "";
                int RID1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID).First();
                int RID2 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == RegionID).Select(x2 => x2.ResponsibleID2).First() ?? 0;
                RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID1).Select(x2 => x2.UserID).First();

                if (RID2 != 0)
                {
                    RegionID2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.UserID).First();
                    v.ResponsibleArea2 = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == RID2).Select(x2 => x2.SubAreaID).First();
                    v.DefaultUserName2 = db.UserData.AsEnumerable().Where(x => x.UserID == RegionID2).Select(x => x.UserName).First();
                }

                v.DefaultUserName = wbsC.WBSEventM.Customer1.SubArea.ResponsibleClass.Name + "-" + 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();
                if (wbsC.ResponsibleID != null)
                {
                    RegionID = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                    v.ResponsibleUserName = db.UserData.AsEnumerable().Where(x => x.UserID == RegionID).Select(x => x.UserName).First();
                }

                if (wbsC.ProvideSpareMach == "T")
                    v.ProvideSpareMachStr = "◎";
                else
                    v.ProvideSpareMachStr = "";
                if (wbsC.RetrieveSpareMach == "T")
                    v.RetrieveSpareMachStr = "◎";
                else
                    v.RetrieveSpareMachStr = "";
                if (wbsC.RetrieveUserMach == "T")
                    v.RetrieveUserMachStr = "◎";
                else
                    v.RetrieveUserMachStr = "";
                if (wbsC.ReturnUserMach == "T")
                    v.ReturnUserMachStr = "◎";
                else
                    v.ReturnUserMachStr = "";
                if (wbsC.ServiceArriveDT != null)
                    v.ArriveDTStr = wbsC.ServiceArriveDT.Value.ToString("yyyy/MM/dd HH:mm");
                //v.ArriveDTStr
                ret.Add(v);
            }

            return ret;
        }
        //手機-異常案件列表
        public List<WBSAssignViewModel> AbnormalMobileRead(string uid)
        {
            List<WBSAssignViewModel> ret = new List<WBSAssignViewModel>();

            //var qryR = db.ResponsibleClass.Where(x => x.UserID == uid);

            //foreach (ResponsibleClass R in qryR)
            //{
                int t = 0;
                while(t < 2){   //兩種異常狀態
                //var qryC = db.WBSEventC.Where(x => x.ResponsibleID == R.ID && x.RespondSW == "Y" && x.ServiceLeaveDT == null && x.ScheduleEndDT.Value.CompareTo(DateTime.Now) < 0).OrderBy(p => p.ExpectArriveDateTime);
                    //var qryC = db.WBSEventC.Where(x => x.ResponsibleID == R.ID);
                    var qryC = db.WBSEventC.Where(x => x.ResponsibleClass.UserID == uid);
                    if(t==0)
                        qryC = qryC.Where(x => x.EventStatus == 41 && x.ScheduleEndDT.Value.CompareTo(DateTime.Now) < 0).OrderBy(p => p.ExpectArriveDateTime);
                    else if (t == 1)
                        qryC = qryC.Where(x => x.EventStatus == 31 && x.ScheduleStartDT.Value.CompareTo(DateTime.Now) < 0).OrderBy(p => p.ExpectArriveDateTime);
                    foreach (WBSEventC wbsC in qryC)
                    {
                        WBSAssignViewModel v = new WBSAssignViewModel();
                        if (t == 0)
                            v.AbnormalReason = "逾時未離開";
                        else if(t == 1)
                            v.AbnormalReason = "逾時未到達";
                        //var qryM = db.WBSEventM.Where(x => x.CaseID == wbsC.MomCaseID);
                        //foreach (WBSEventM m in qryM)
                        //{
                        //    v.MCaseID = m.CaseID;
                        //    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.CustomerSName = m.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                        //    v.CustomerPhone1 = m.CustomerUserPhone1;
                        //    v.CustomerUserName = m.CustomerUserName;


                        //    string temp = m.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();

                        //    int temp1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == temp).Select(x2 => x2.ResponsibleID).First();
                        //    temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == temp1).Select(x2 => x2.UserID).First();
                        //    v.DefaultUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                        //    if (wbsC.ResponsibleID != null)
                        //    {
                        //        temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                        //        v.ResponsibleUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                        //    }
                        //}

                        v.MCaseID = wbsC.WBSEventM.CaseID;
                        v.CaseNumM = wbsC.WBSEventM.CaseClass + wbsC.WBSEventM.CaseDate + "-";
                        if (wbsC.WBSEventM.CaseCode < 10)
                            v.CaseNumM += "0000" + wbsC.WBSEventM.CaseCode.ToString();
                        else if (wbsC.WBSEventM.CaseCode < 100 && wbsC.WBSEventM.CaseCode >= 10)
                            v.CaseNumM += "000" + wbsC.WBSEventM.CaseCode.ToString();
                        else if (wbsC.WBSEventM.CaseCode < 1000 && wbsC.WBSEventM.CaseCode >= 100)
                            v.CaseNumM += "00" + wbsC.WBSEventM.CaseCode.ToString();
                        else if (wbsC.WBSEventM.CaseCode < 10000 && wbsC.WBSEventM.CaseCode >= 1000)
                            v.CaseNumM += "0" + wbsC.WBSEventM.CaseCode.ToString();
                        else
                            v.CaseNumM += wbsC.WBSEventM.CaseCode.ToString();
                        v.CustomerSName = wbsC.WBSEventM.Customer1.ShortName; //db.Customer.AsEnumerable().Where(x => x.CustomerRef == m.CustomerRef).Select(x => x.ShortName).First();
                        v.CustomerPhone1 = wbsC.WBSEventM.CustomerUserPhone1;
                        v.CustomerUserName = wbsC.WBSEventM.CustomerUserName;


                        string temp = wbsC.WBSEventM.Customer1.RegionID; //db.Customer.AsEnumerable().Where(x2 => x2.CustomerRef == m.CustomerRef).Select(x2 => x2.RegionID).First();

                        int temp1 = db.SubArea.AsEnumerable().Where(x2 => x2.SubAreaID == temp).Select(x2 => x2.ResponsibleID).First();
                        temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == temp1).Select(x2 => x2.UserID).First();
                        v.DefaultUserName = wbsC.WBSEventM.Customer1.SubArea.ResponsibleClass.Name + "-" + db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                        if (wbsC.ResponsibleID != null)
                        {
                            temp = db.ResponsibleClass.AsEnumerable().Where(x2 => x2.ID == wbsC.ResponsibleID).Select(x2 => x2.UserID).First();
                            v.ResponsibleUserName = db.UserData.AsEnumerable().Where(x => x.UserID == temp).Select(x => x.UserName).First();
                        }


                        v.CCaseNum = wbsC.Num;
                        v.CreateDT = wbsC.CreateDateTime;
                        v.CreateUserID = wbsC.CreateUserID;
                        v.ScheduleStartDT = wbsC.ExpectArriveDateTime;
                        v.ScheduleEndDT = wbsC.ExpectFinishDateTime;
                        v.ServiceHours = wbsC.QuestionClass1.QuestionServiceHours; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.QuestionServiceHours).First();

                        v.CaseNumC = (wbsC.ChildCaseID < 10) ? ("0" + wbsC.ChildCaseID.ToString()) : wbsC.ChildCaseID.ToString();
                        v.HasActtachment = "";
                        var qryF = db.Files.Where(x => x.CID == wbsC.Num);
                        foreach (Files f in qryF)
                        {
                            v.HasActtachment = "@";
                        }

                        v.ProductNote = wbsC.ProductClass1.Note; //db.ProductClass.AsEnumerable().Where(x => x.ProductClassID == wbsC.ProductClass).Select(x => x.Note).First();
                        v.ProductClass = wbsC.ProductClass;
                        v.ItemClass = wbsC.ItemClass;
                        v.ItemNote = wbsC.ItemClass1.Note; //db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == wbsC.ItemClass).Select(x => x.Note).First();
                        v.QuestionClass = wbsC.QuestionClass;
                        v.QuestionNote = wbsC.QuestionClass1.Note; //db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == wbsC.QuestionClass).Select(x => x.Note).First();
                        v.QuestionDesc = wbsC.QuestionDescription;
                        v.CaseCreateDT = wbsC.CreateDateTime;
                        System.TimeSpan diff1 = DateTime.Now.Subtract(wbsC.CreateDateTime ?? DateTime.Now);
                        v.CaseCreateDTStr = (wbsC.CreateDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                        v.CaseCreateUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.CreateUserID).Select(x => x.UserName).First();

                        v.HasAssignActtachment = "";
                        v.ExpectArriveDT = wbsC.ExpectArriveDateTime;
                        v.ExpectArriveDTStr = (wbsC.ExpectArriveDateTime ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                        if (wbsC.ExpectArriveSW == "0")
                        {
                            v.ExpectArriveSW = "自行約定";
                            v.ExpectArriveDTStr = "自行約定";
                        }
                        else if (wbsC.ExpectArriveSW == "1")
                            v.ExpectArriveSW = "合約規定";
                        else if (wbsC.ExpectArriveSW == "2")
                            v.ExpectArriveSW = "建議期望";
                        else
                            v.ExpectArriveSW = "客戶指定";
                        v.isSelect = false;

                        v.AssignUserID = wbsC.AssignUserID;
                        v.AssignDT = wbsC.AssignDT;

                        if (wbsC.AssignUserID != null)
                        {
                            v.AssignDTStr = (wbsC.AssignDT ?? DateTime.Now).ToString("yyyyMMdd HH:mm");
                            v.AssignUserName = db.UserData.AsEnumerable().Where(x => x.UserID == wbsC.AssignUserID).Select(x => x.UserName).First();
                        }
                        else
                        {
                            v.AssignDTStr = "";
                            v.AssignUserName = "";
                        }
                        v.ResponsibleID = wbsC.ResponsibleID.GetValueOrDefault();
                        v.RespondSW = wbsC.RespondSW;
                        v.RespondDT = wbsC.RespondDT;
                        v.RespondReason = wbsC.RespondReason;
                        v.ScheduleStartDT = wbsC.ScheduleStartDT;
                        v.ScheduleEndDT = wbsC.ScheduleEndDT;
                        v.ServiceHours = wbsC.ScheduleServiceHours ?? wbsC.QuestionServiceHours;
                        v.ModifyDT = wbsC.AssignModifyDT;
                        v.ModifyUserID = wbsC.ModifyUserID;

                        int temp2 = wbsC.EventStatus;

                        if (temp2 == 0)
                            v.EventStatusStr = "已刪除";
                        else if (temp2 == 11)
                            v.EventStatusStr = "已開單未派工";
                        else if (temp2 == 20)
                            v.EventStatusStr = "已拒絕待重派";
                        else if (temp2 == 21)
                            v.EventStatusStr = "已派工未接受";
                        else if (temp2 == 30)
                            v.EventStatusStr = "已接受未排程";
                        else if (temp2 == 31)
                            v.EventStatusStr = "已接受已排程";
                        else if (temp2 == 41)
                            v.EventStatusStr = "已到達未離開";
                        else if (temp2 == 42)
                            v.EventStatusStr = "已離開待附件";
                        else if (temp2 == 46)
                            v.EventStatusStr = "已解決未審核";
                        else if (temp2 == 50)
                            v.EventStatusStr = "已解決已審核";

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

                        ret.Add(v);
                    }
                    t++;
                }

            //}

            return ret;
        }