//批次結案重啟(一次一件)
        public String RestartEvent(int mID, int cID, String uid, String restartReason)
        {
            String ret = "false";
            String eventNum, eventNumN = "", eventNumO = "";
            String errorStr = "";
            String memo = "";
            Boolean Chg = false;
            WBSEventM wBSEventM = db.WBSEventM.Find(mID);
            WBSEventC wBSEventC = db.WBSEventC.Find(cID);

            int ChildCaseID = db.WBSEventC.Find(cID).ChildCaseID;
            eventNum = wBSEventM.CaseClass + wBSEventM.CaseDate + "-";
            if (wBSEventM.CaseCode < 10)
                eventNum += "0000" + wBSEventM.CaseCode.ToString();
            else if (wBSEventM.CaseCode < 100 && wBSEventM.CaseCode >= 10)
                eventNum += "000" + wBSEventM.CaseCode.ToString();
            else if (wBSEventM.CaseCode < 1000 && wBSEventM.CaseCode >= 100)
                eventNum += "00" + wBSEventM.CaseCode.ToString();
            else if (wBSEventM.CaseCode < 10000 && wBSEventM.CaseCode >= 1000)
                eventNum += "0" + wBSEventM.CaseCode.ToString();
            else
                eventNum += wBSEventM.CaseCode.ToString();            
            eventNumO = eventNum+ "-" + ((ChildCaseID < 10) ? ("0" + ChildCaseID.ToString()) : ChildCaseID.ToString());

            try
            {
                if (wBSEventM.CaseMStatus != "9")
                    errorStr += "案件尚未結案,無法重啟。\n\r";
                else
                {
                    WBSEventC lastC = db.WBSEventC.Find(db.WBSEventC.Where(x => x.MomCaseID == wBSEventM.CaseID).OrderByDescending(x => x.Num).Select(x => x.Num).First());
                    WBSEventC NwBSEventC = new WBSEventC();
                    int num = db.WBSEventC.AsEnumerable().Select(x => x.Num).DefaultIfEmpty(0).Max(x => x) + 1;
                    NwBSEventC.Num = num;
                    NwBSEventC.MomCaseID = lastC.MomCaseID;
                    int childCaseID = db.WBSEventC.AsEnumerable().Where(x => x.MomCaseID == lastC.MomCaseID).Select(x => x.ChildCaseID).DefaultIfEmpty(0).Max(x => x) + 1;
                    NwBSEventC.ChildCaseID = childCaseID;

                    eventNumN = eventNum + "-" + ((NwBSEventC.ChildCaseID < 10) ? ("0" + NwBSEventC.ChildCaseID.ToString()) : NwBSEventC.ChildCaseID.ToString());
                    NwBSEventC.EventStatus = 11;
                    NwBSEventC.EventClass = lastC.EventClass;
                    NwBSEventC.ProductClass = lastC.ProductClass;
                    NwBSEventC.ItemClass = lastC.ItemClass;
                    NwBSEventC.QuestionClass = lastC.QuestionClass;
                    NwBSEventC.QuestionGrade = lastC.QuestionGrade;
                    NwBSEventC.QuestionServiceHours = lastC.QuestionServiceHours;
                    NwBSEventC.QuestionDescription = lastC.QuestionDescription;
                    NwBSEventC.QuestionCreateUserID = uid;
                    NwBSEventC.QuestionCreateDateTime = DateTime.Now;
                    NwBSEventC.ExpectArriveDateTime = lastC.ExpectArriveDateTime;
                    NwBSEventC.ExpectFinishDateTime = lastC.ExpectFinishDateTime;
                    NwBSEventC.ExpectArriveSW = lastC.ExpectArriveSW;
                    NwBSEventC.CreateDateTime = DateTime.Now;
                    NwBSEventC.CreateUserID = uid;
                    NwBSEventC.ModifyDateTime = DateTime.Now;
                    NwBSEventC.ModifyUserID = uid;
                    //20150811新增-Hsiuyang
                    NwBSEventC.SugProvideSpareMach = lastC.SugProvideSpareMach;
                    NwBSEventC.SugRetrieveSpareMach = lastC.SugRetrieveSpareMach;
                    NwBSEventC.SugRetrieveUserMach = lastC.SugRetrieveUserMach;
                    NwBSEventC.SugReturnUserMach = lastC.SugReturnUserMach;
                    NwBSEventC.SugMachMemo = lastC.SugMachMemo;   //建議備機說明
                    NwBSEventC.DeleteReason = "結案重啟 : " + restartReason + "\r\n";

                    wBSEventM.CaseMStatus = "5";
                    wBSEventM.CloseDT = null;
                    db.WBSEventC.Add(NwBSEventC);
                    Chg = true;
                }

                if (Chg)
                {
                    db.SaveChanges();

                    return wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-結案重啟成功,單號:" + eventNumN + "。\n\r";
                }
                else
                {
                    return wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-結案重啟失敗,失敗原因如下:\n\r" + errorStr + "單號:" + eventNumO + "。\n\r";                    
                }
            }
            catch
            {
                return wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-結案重啟失敗,失敗原因如下:\n\r" + errorStr + "單號:" + eventNumO + "。\n\r";                
            }
        }
        private String isAbnormal(WBSEventC wbsC, List<AlertDetailSettingViewModel> alertMList) //回傳""-查無異常 ,其他回傳原因
        {
            
            int temp = 0;

            if (wbsC.EventStatus == 11 || wbsC.EventStatus == 20 || wbsC.EventStatus == 21)
            {
                if (alertMList.Exists(x => x.EventClass == wbsC.EventClass))
                    temp = alertMList.Find(x => x.EventClass == wbsC.EventClass && x.AlertEvent == 1).FirstTime;
                else
                    temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 1).FirstTime;

                if (wbsC.CreateDateTime.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                {
                    //1.	派工逾時警示:已開單未接受超過 N [90]分鐘時
                    if (wbsC.EventStatus == 11)
                    {
                        return "逾時未派工";
                    }
                    else if (wbsC.EventStatus == 20)
                    {
                        return "逾時未重派";
                    }
                    else if (wbsC.EventStatus == 21)
                    {
                        return "逾時未接受";
                    }
                }
            }
            else if (wbsC.EventStatus == 30 && (wbsC.EventClass != "M01" && wbsC.EventClass != "C02" && wbsC.EventClass != "C03"))
            {
                if (alertMList.Exists(x => x.EventClass == wbsC.EventClass))
                    temp = alertMList.Find(x => x.EventClass == wbsC.EventClass && x.AlertEvent == 2).FirstTime;
                else
                    temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 2).FirstTime;

                if (wbsC.RespondDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                { //2.	案件排程提醒:案件已接受未排程,未於應完修時間前N [24]小時前完成排程時提示。(For.C02/C03)                          
                    return "逾時未排程";
                }
            }
            else if (wbsC.EventStatus == 31)
            {
                if (alertMList.Exists(x => x.EventClass == wbsC.EventClass))
                    temp = alertMList.Find(x => x.EventClass == wbsC.EventClass && x.AlertEvent == 3).FirstTime;
                else
                    temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 3).FirstTime;

                if (wbsC.ScheduleStartDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                { //3.	到達逾時警示:已接受已排程超過 N [30]分鐘未到時                       
                    return "逾時未到達";
                }
            }
            else if (wbsC.EventStatus == 41)
            {
                if (alertMList.Exists(x => x.EventClass == wbsC.EventClass))
                    temp = alertMList.Find(x => x.EventClass == wbsC.EventClass && x.AlertEvent == 4).FirstTime;
                else
                    temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 4).FirstTime;

                if (wbsC.ServiceArriveDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                { //4.	工時過長提醒:已到達未離開超過 N [12]小時未離開時                      
                    return "逾時未離開";
                }
            }
            else if (wbsC.EventStatus == 42)
            {
                if (alertMList.Exists(x => x.EventClass == wbsC.EventClass))
                    temp = alertMList.Find(x => x.EventClass == wbsC.EventClass && x.AlertEvent == 5).FirstTime;
                else
                    temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 5).FirstTime;

                if (wbsC.ServiceLeaveDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                { //5.	補登附件提醒:已離開已未解決超過 N [1]天 (表示未補登附件)
                    return "離開待附件";
                }
            }
            else if (wbsC.EventStatus == 46)
            {
                if (alertMList.Exists(x => x.EventClass == wbsC.EventClass))
                    temp = alertMList.Find(x => x.EventClass == wbsC.EventClass && x.AlertEvent == 6).FirstTime;
                else
                    temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 6).FirstTime;
                if (wbsC.ServiceLeaveDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                { //6.	審核逾時警示:已解決待審核案件超過 N [30]天未離開時
                    return "逾時未審核";
                }
            }
            //else if (wbsC.EventStatus == 43)
            //{
            //    if (alertMList.Exists(x => x.EventClass == wbsC.EventClass))
            //        temp = alertMList.Find(x => x.EventClass == wbsC.EventClass && x.AlertEvent == 9).FirstTime;
            //    else
            //        temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 9).FirstTime;

            //    if (wbsC.ServiceLeaveDT.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
            //    { //9.	案件已交付逾時未解決警示: SCL(V1.13)
            //        return "已交付逾時未解決";
            //    }
            //}

            if (wbsC.EventStatus >= 11 && wbsC.EventStatus < 50)
            {
                if (alertMList.Exists(x => x.EventClass == wbsC.EventClass))
                    temp = alertMList.Find(x => x.EventClass == wbsC.EventClass && x.AlertEvent == 7).FirstTime;
                else
                    temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 7).FirstTime;
                if (wbsC.CreateDateTime.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                { //7.	子單未解決警示:已開單未解決超過 N [7]天時(C01)警示
                    return "逾時未解決";
                }
            }
            ///???????
            ///

            WBSEventC MwbsC = db.WBSEventC.Where(x=>x.MomCaseID == wbsC.MomCaseID && x.ChildCaseID == 1).First();
            if (wbsC.WBSEventM.CaseMStatus != "9" && wbsC.WBSEventM.CaseMStatus != "0")
            {
                if (alertMList.Exists(x => x.EventClass == MwbsC.EventClass))
                    temp = alertMList.Find(x => x.EventClass == MwbsC.EventClass && x.AlertEvent == 8).FirstTime;
                else
                    temp = alertMList.Find(x => x.EventClass == null && x.AlertEvent == 8).FirstTime;
                if (MwbsC.CreateDateTime.Value.AddMinutes(temp).CompareTo(DateTime.Now) < 0)
                { //8.	案件未結案警示:已開單未結案超過 N [14]天時警示
                    return "逾時未結案";
                }
            }
            return "";
        }
        //建立後續叫修單
        public String[] FollowUpCreate(WBSEventViewModel wBSEventViewModel)
        {
            String[] ret = new String[4];

            ret[0] = "false";
            ret[1] = "";
            ret[2] = "";    //area
            ret[3] = "";    //auto assign user name
            int  num = 0, childCaseID = 0;                        
            
            //String groupSW = db.Customer.AsEnumerable().Where(x => x.CustomerRef == wBSEventViewModel.CustomerRef).Select(x => x.GroupSW).First();

            if (wBSEventViewModel.ExpectArriveDTStr == null)
                wBSEventViewModel.ExpectArriveDTStr = "19000101 00:01";
            if (wBSEventViewModel.ExpectFinishDTStr == null)
                wBSEventViewModel.ExpectFinishDTStr = "19000101 00:01";
            wBSEventViewModel.ExpectArriveDT = DateTime.ParseExact(wBSEventViewModel.ExpectArriveDTStr, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture);
            wBSEventViewModel.ExpectFinishDT = DateTime.ParseExact(wBSEventViewModel.ExpectFinishDTStr, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture);

            try
            {
                WBSEventM wBSEventM = db.WBSEventM.Find(wBSEventViewModel.M_ID);
                WBSEventC oldEventC = db.WBSEventC.Find(wBSEventViewModel.C_ID);

                if (oldEventC.EventStatus == 49)
                {
                    oldEventC.EventStatus = 50;
                    oldEventC.ModifyDateTime = DateTime.Now;
                    oldEventC.ModifyUserID = wBSEventViewModel.CreateUserID;
                }

                wBSEventM.CaseMStatus = "5";    //5為部分子單已解決
                num = db.WBSEventC.AsEnumerable().Select(x => x.Num).DefaultIfEmpty(0).Max(x => x) + 1;
                childCaseID = db.WBSEventC.AsEnumerable().Where(x => x.MomCaseID == wBSEventM.CaseID).Select(x => x.ChildCaseID).DefaultIfEmpty(0).Max(x => x) + 1;
                ret[2] = GetCustomerArea(wBSEventM.CustomerRef);

                WBSEventC wBSEventC = new WBSEventC();
                ret[1] = num.ToString();
                wBSEventC.Num = num;
                wBSEventC.MomCaseID = oldEventC.MomCaseID;
                wBSEventC.ChildCaseID = childCaseID;
                wBSEventViewModel.WBSCaseNumberC = (wBSEventC.ChildCaseID < 10) ? ("0" + wBSEventC.ChildCaseID.ToString()) : wBSEventC.ChildCaseID.ToString();
                wBSEventC.EventClass = wBSEventViewModel.EventClass;
                wBSEventC.ProductClass = oldEventC.ProductClass;
                wBSEventC.ItemClass = oldEventC.ItemClass;
                wBSEventC.QuestionClass = oldEventC.QuestionClass;
                wBSEventC.QuestionGrade = oldEventC.QuestionGrade;
                wBSEventC.QuestionServiceHours = oldEventC.QuestionServiceHours;
                wBSEventC.QuestionDescription = wBSEventViewModel.QuestionDescription;
                wBSEventC.QuestionCreateUserID = wBSEventViewModel.QuestionCreateUserID;
                wBSEventC.QuestionCreateDateTime = DateTime.Now;
                wBSEventC.ExpectArriveDateTime = wBSEventViewModel.ExpectArriveDT;
                wBSEventC.ExpectFinishDateTime = wBSEventViewModel.ExpectFinishDT;
                wBSEventC.ExpectArriveSW = wBSEventViewModel.ExpectArriveSW;
                //wBSEventC.DeleteMarkSW = wBSEventViewModel.DeleteMarkSW;
                //wBSEventC.DeleteReason = wBSEventViewModel.DeleteReason;
                //wBSEventC.DeleteMarkUserID = wBSEventViewModel.DeleteMarkUserID;
                //wBSEventC.DeleteMarkDateTime = wBSEventViewModel.DeleteMarkDT;
                wBSEventC.CreateDateTime = DateTime.Now;
                wBSEventC.CreateUserID = wBSEventViewModel.CreateUserID;
                wBSEventC.ModifyDateTime = DateTime.Now;
                wBSEventC.ModifyUserID = wBSEventViewModel.CreateUserID;
                
                wBSEventC.SugProvideSpareMach = "F";
                wBSEventC.SugRetrieveSpareMach = "F";
                wBSEventC.SugRetrieveUserMach = "F";
                wBSEventC.SugReturnUserMach = "F";
                wBSEventC.SugMachMemo = (wBSEventViewModel.SugMachMemo == null) ? "" : wBSEventViewModel.SugMachMemo;   //建議備機說明
                wBSEventC.EventStatus = 11; //11為問題已開立

                if (wBSEventViewModel.ExpectArriveSW == "4")//自派新單
                {
                    String subAreaID = db.Customer.AsEnumerable().Where(x => x.CustomerRef == wBSEventM.CustomerRef).Select(x => x.RegionID).First();
                    String AreaID = db.SubArea.AsEnumerable().Where(x => x.SubAreaID == subAreaID).Select(x => x.AreaID).First();
                    int rID = 0;
                    if (db.SubArea.AsEnumerable().Where(x => x.AreaID == AreaID && x.ResponsibleClass.UserID == wBSEventViewModel.CreateUserID).Count() > 0)
                    {
                        rID = db.SubArea.AsEnumerable().Where(x => x.AreaID == AreaID && x.ResponsibleClass.UserID == wBSEventViewModel.CreateUserID).Select(x => x.ResponsibleClass.ID).First();
                    }
                    else if (db.ResponsibleClass.AsEnumerable().Where(x => x.UserID == wBSEventViewModel.CreateUserID).Count() > 0)
                    {
                        rID = db.ResponsibleClass.AsEnumerable().Where(x => x.UserID == wBSEventViewModel.CreateUserID).Select(x => x.ID).First();
                    }
                    else
                    {
                        rID = oldEventC.ResponsibleID.Value;
                    }                    
                    wBSEventC.ResponsibleID = rID;

                    wBSEventC.AssignCreateDT = DateTime.Now;
                    wBSEventC.AssignCreateUserID = wBSEventViewModel.CreateUserID;
                    wBSEventC.AssignDT = DateTime.Now;
                    wBSEventC.AssignUserID = wBSEventViewModel.CreateUserID;
                    wBSEventC.AssignModifyDT = DateTime.Now;
                    wBSEventC.AssignModifyUserID = wBSEventViewModel.CreateUserID;

                    wBSEventC.RespondSW = "Y";
                    wBSEventC.RespondDT = DateTime.Now;
                    wBSEventC.RespondReason = "";
                    wBSEventC.ScheduleStartDT = wBSEventViewModel.ExpectArriveDT;
                    wBSEventC.ScheduleEndDT = wBSEventViewModel.ExpectFinishDT;
                    wBSEventC.ScheduleServiceHours = wBSEventC.QuestionServiceHours;
                    
                    wBSEventC.EventStatus = 31; //31任務已接受已排程

                    String ExpectArriveSW, EventStatusStr;
                    ExpectArriveSW = "自派新單";
                   

                    String ExpectArriveDTStr = wBSEventC.ExpectArriveDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                    if (wBSEventC.ExpectArriveSW == "0")
                        ExpectArriveDTStr = "自行約定";

                    EventStatusStr = "已接受已排程";


                    String UserID = wBSEventViewModel.CreateUserID;
                    String UserName = db.ResponsibleClass.Where(x => x.ID == rID).Select(x => x.UserData2.UserName).First();

                    String url = "http://" + wBSEventViewModel.Url + "/Mobile/MobileCaseDetail?type=2&&mid=" + wBSEventC.MomCaseID + "&&cid=" + wBSEventC.Num;
                    String title = "";      //[燈號]-[推播事件說明]
                    String content = "";    //[被推播人員姓名]您好:案件-[客戶簡稱]-[到達約定選擇]:[應到達時間],[事件說明],請您盡速處理。
                    title = "綠燈-執行人已接受已排程";
                    content = UserName + "您好:案件-" + wBSEventM.Customer1.ShortName + "-" + ExpectArriveSW + ":" + ExpectArriveDTStr + "," + EventStatusStr + ",請您盡速處理。";

                    //messageRepository.AddMsgbyUser(UserID, title, content, url);
                }
                else if (wBSEventViewModel.ResponsibleID != "" && wBSEventViewModel.ResponsibleID != null)
                {
                    //String subAreaID = db.Customer.AsEnumerable().Where(x => x.CustomerRef == wBSEventM.CustomerRef).Select(x => x.RegionID).First();
                    //String AreaID = db.SubArea.AsEnumerable().Where(x => x.SubAreaID == subAreaID).Select(x => x.AreaID).First();
                    //int rID = 0;

                    //if (db.SystemSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AutoAssign == true).Count() > 0) //事件個別指定
                    //{
                    //    if (db.SystemSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AutoAssignUserType == 2).Count() > 0)
                    //        rID = db.SubArea.AsEnumerable().Where(x => x.SubAreaID == subAreaID).Select(x => x.ResponsibleID).First();
                    //    else
                    //    {
                    //        if (db.DefaultAssignSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AreaID == AreaID).Count() > 0)
                    //            rID = db.DefaultAssignSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AreaID == AreaID).Select(x => x.ResponsibleClassID).First();
                    //        else if (db.DefaultAssignSetting.Where(x => x.isSysDefault == true && x.AreaID == AreaID).Count() > 0)
                    //            rID = db.DefaultAssignSetting.Where(x => x.isSysDefault == true && x.AreaID == AreaID).Select(x => x.ResponsibleClassID).First();                             
                    //    }
                    //}
                    //else //系統預設
                    //{
                    //    if (db.SystemSetting.Where(x => x.isSysDefault == true && x.AutoAssignUserType == 2).Count() > 0)
                    //        rID = db.SubArea.AsEnumerable().Where(x => x.SubAreaID == subAreaID).Select(x => x.ResponsibleID).First();
                    //    else
                    //    {
                    //        if (db.DefaultAssignSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AreaID == AreaID).Count() > 0)
                    //            rID = db.DefaultAssignSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AreaID == AreaID).Select(x => x.ResponsibleClassID).First();
                    //        else if (db.DefaultAssignSetting.Where(x => x.isSysDefault == true && x.AreaID == AreaID).Count() > 0)
                    //            rID = db.DefaultAssignSetting.Where(x => x.isSysDefault == true && x.AreaID == AreaID).Select(x => x.ResponsibleClassID).First();                             
                    //    }

                    //}
                    int rID = int.Parse(wBSEventViewModel.ResponsibleID);
                    wBSEventC.ResponsibleID = rID;

                    wBSEventC.AssignCreateDT = DateTime.Now;
                    wBSEventC.AssignCreateUserID = wBSEventViewModel.CreateUserID;
                    wBSEventC.AssignDT = DateTime.Now;
                    wBSEventC.AssignUserID = wBSEventViewModel.CreateUserID;
                    wBSEventC.AssignModifyDT = DateTime.Now;
                    wBSEventC.AssignModifyUserID = wBSEventViewModel.CreateUserID;

                    wBSEventC.RespondSW = "Y";
                    wBSEventC.RespondDT = DateTime.Now;
                    wBSEventC.RespondReason = "";
                    //wBSEventC.ScheduleStartDT = wBSEventViewModel.ExpectArriveDT;
                    //wBSEventC.ScheduleEndDT = wBSEventViewModel.ExpectFinishDT;
                    //wBSEventC.ScheduleServiceHours = wBSEventC.QuestionServiceHours;

                    wBSEventC.EventStatus = 30; //30任務已接受未排程

                    String ExpectArriveSW, EventStatusStr;
                    if (wBSEventC.ExpectArriveSW == "0")
                        ExpectArriveSW = "自行約定";
                    else if (wBSEventC.ExpectArriveSW == "1")
                        ExpectArriveSW = "合約規定";
                    else if (wBSEventC.ExpectArriveSW == "2")
                        ExpectArriveSW = "建議期望";
                    else if (wBSEventC.ExpectArriveSW == "4")
                        ExpectArriveSW = "自派新單";
                    else
                        ExpectArriveSW = "客戶指定";

                    String ExpectArriveDTStr = wBSEventC.ExpectArriveDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                    if (wBSEventC.ExpectArriveSW == "0")
                        ExpectArriveDTStr = "自行約定";

                    EventStatusStr = "已接受未排程";


                    String UserID = db.ResponsibleClass.Where(x => x.ID == rID).Select(x => x.UserID).First();
                    String UserName = db.ResponsibleClass.Where(x => x.ID == rID).Select(x => x.UserData2.UserName).First();
                    ret[3] = db.ResponsibleClass.Where(x => x.ID == rID).Select(x => x.Name).First() + "-" + UserName;

                    String url = "http://" + wBSEventViewModel.Url + "/Mobile/MobileCaseDetail?type=2&&mid=" + wBSEventC.MomCaseID + "&&cid=" + wBSEventC.Num;
                    String title = "";      //[燈號]-[推播事件說明]
                    String content = "";    //[被推播人員姓名]您好:案件-[客戶簡稱]-[到達約定選擇]:[應到達時間],[事件說明],請您盡速處理。
                    title = "綠燈-執行人已接受未排程";
                    content = UserName + "您好:案件-" + wBSEventM.Customer1.ShortName + "-" + ExpectArriveSW + ":" + ExpectArriveDTStr + "," + EventStatusStr + ",請您盡速處理。";

                    //messageRepository.AddMsgbyUser(UserID, title, content, url);
                }

                //db.WBSEventM.Add(wBSEventM);
                db.WBSEventC.Add(wBSEventC);
                db.SaveChanges();
                //ret = "true";
                ret[0] = wBSEventViewModel.WBSCaseNumberM + "-" + wBSEventViewModel.WBSCaseNumberC;
            }
            catch
            {
            }
            return ret;

        }
        //電腦建立二次派工叫修單(20151221-增加自動派工)
        public String[] TwiceCreate(WBSEventViewModel wBSEventViewModel)
        {
            String[] ret = new String[4];
            ret[0] = "false";
            ret[1] = "";
            ret[2] = "";    //area
            ret[3] = "";    //auto assign user name
            int num = 0, childCaseID = 0;
            
            
            num = db.WBSEventC.AsEnumerable().Select(x => x.Num).DefaultIfEmpty(0).Max(x => x) + 1;
            childCaseID = db.WBSEventC.AsEnumerable().Where(x => x.MomCaseID == wBSEventViewModel.M_ID).Select(x => x.ChildCaseID).DefaultIfEmpty(0).Max(x => x) + 1;
            int questionGrade = db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == Int32.Parse(wBSEventViewModel.QuestionClass)).Select(x => x.QuestionGrade).First();
            int serviceHours = db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == Int32.Parse(wBSEventViewModel.QuestionClass)).Select(x => x.QuestionServiceHours).First();
            String groupSW = db.Customer.AsEnumerable().Where(x => x.CustomerRef == wBSEventViewModel.CustomerRef).Select(x => x.GroupSW).First();

            if (wBSEventViewModel.ExpectArriveDTStr == null)
                wBSEventViewModel.ExpectArriveDTStr = "19000101 00:01";
            if (wBSEventViewModel.ExpectFinishDTStr == null)
                wBSEventViewModel.ExpectFinishDTStr = "19000101 00:01";
            wBSEventViewModel.ExpectArriveDT = DateTime.ParseExact(wBSEventViewModel.ExpectArriveDTStr, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture);
            wBSEventViewModel.ExpectFinishDT = DateTime.ParseExact(wBSEventViewModel.ExpectFinishDTStr, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture);
            ret[2] = GetCustomerArea(wBSEventViewModel.CustomerRef);


            try
            {
                bool isMChg = false;
                WBSEventM wBSEventM = db.WBSEventM.Find(wBSEventViewModel.M_ID);
                //20150811新增-Hsiuyang
                if (wBSEventViewModel.ChangeAddr != null)
                    if (wBSEventViewModel.ChangeAddr.Trim().Length > 0)
                    {
                        wBSEventM.CustomerAddr = wBSEventViewModel.ChangeAddr;
                        isMChg = true;
                    }
                //wBSEventM.SpareMach = "R";      //預設無借出備機
                //wBSEventM.UserMach = "R";       //預設無取回客戶機器
                if (Int32.Parse(wBSEventM.CaseMStatus) < 4 && wBSEventM.CaseMStatus != "0")
                {
                    isMChg = true;
                    wBSEventM.CaseMStatus = "4";    //4為已分配(另開子單)
                }
                if (wBSEventM.CaseMStatus == "6")   //6為全部子單以解決需退回5
                {   
                    isMChg = true;
                    wBSEventM.CaseMStatus = "5";    //5為部分子單已解決
                }
                if (isMChg)
                {
                    wBSEventM.ModifyDT = DateTime.Now;
                    wBSEventM.ModifyUserID = wBSEventViewModel.CreateUserID;
                }

                WBSEventC wBSEventC = new WBSEventC();
                ret[1] = num.ToString();
                wBSEventC.Num = num;
                wBSEventC.MomCaseID = wBSEventViewModel.M_ID;
                wBSEventC.ChildCaseID = childCaseID;
                wBSEventViewModel.WBSCaseNumberC = (wBSEventC.ChildCaseID < 10) ? ("0" + wBSEventC.ChildCaseID.ToString()) : wBSEventC.ChildCaseID.ToString();
                wBSEventC.EventClass = wBSEventViewModel.EventClass;
                wBSEventC.ProductClass = Int32.Parse(wBSEventViewModel.ProductClass);
                wBSEventC.ItemClass = Int32.Parse(wBSEventViewModel.ItemClass);
                wBSEventC.QuestionClass = Int32.Parse(wBSEventViewModel.QuestionClass);
                wBSEventC.QuestionGrade = questionGrade;
                wBSEventC.QuestionServiceHours = serviceHours;
                wBSEventC.QuestionDescription = wBSEventViewModel.QuestionDescription;
                wBSEventC.QuestionCreateUserID = wBSEventViewModel.QuestionCreateUserID;
                wBSEventC.QuestionCreateDateTime = DateTime.Now;
                wBSEventC.ExpectArriveDateTime = wBSEventViewModel.ExpectArriveDT;
                wBSEventC.ExpectFinishDateTime = wBSEventViewModel.ExpectFinishDT;
                wBSEventC.ExpectArriveSW = wBSEventViewModel.ExpectArriveSW;
                wBSEventC.CreateDateTime = DateTime.Now;
                wBSEventC.CreateUserID = wBSEventViewModel.CreateUserID;
                wBSEventC.ModifyDateTime = DateTime.Now;
                wBSEventC.ModifyUserID = wBSEventViewModel.CreateUserID;
                //20150811新增-Hsiuyang
                wBSEventC.SugProvideSpareMach = wBSEventViewModel.SugProvideSpareMach;
                wBSEventC.SugRetrieveSpareMach = wBSEventViewModel.SugRetrieveSpareMach;
                wBSEventC.SugRetrieveUserMach = wBSEventViewModel.SugRetrieveUserMach;
                wBSEventC.SugReturnUserMach = wBSEventViewModel.SugReturnUserMach;
                wBSEventC.SugMachMemo = (wBSEventViewModel.SugMachMemo == null) ? "" : wBSEventViewModel.SugMachMemo;   //建議備機說明

                wBSEventC.EventStatus = 11; //11為問題已開立
                //20150813新增-Hsiuyang
                if (wBSEventViewModel.Closed == "T") //線上處理結案影響
                {
                    //wBSEventM.CaseMStatus = "9";    //9為問題已結案
                    //wBSEventM.CloseDT = DateTime.Now;
                    wBSEventC.EventStatus = 50;     //50為任務已解決(維修附件上傳)
                    wBSEventC.QuestionFinishSW = "Y";
                    wBSEventC.QuestionFinishDT = DateTime.Now;
                    wBSEventC.QuestionFinishSysDT = DateTime.Now;
                    wBSEventC.ServiceReport = wBSEventViewModel.AssignDescription;  //當結案時解決方式即為服務報告
                }
                wBSEventC.AssignDescription = wBSEventViewModel.AssignDescription;  //解決方式(派單)描述


                //20150812新增-Hsiuyang-SugProduct建議部分
                if (wBSEventViewModel.MaterialJson != null)
                {
                    JArray SugProduct = JsonConvert.DeserializeObject<JArray>(wBSEventViewModel.MaterialJson);
                    for (int i = 0; i < SugProduct.Count; i++)
                    {
                        SugProduct sugProduct = new SugProduct();
                        sugProduct.CID = wBSEventC.Num;
                        sugProduct.Class = Int32.Parse(SugProduct[i]["Class"].ToString());
                        //sugProduct.Class = Int32.Parse(SugProduct[i][0].ToString());
                        //if (SugProduct[i][1].ToString() == "")
                        if (SugProduct[i]["ProductNum"].ToString() == "")
                            sugProduct.ProductName = SugProduct[i]["ProductName"].ToString();
                        //sugProduct.ProductName = SugProduct[i][2].ToString();
                        else
                            sugProduct.ProductNum = Int32.Parse(SugProduct[i]["ProductNum"].ToString());
                        //sugProduct.ProductNum = Int32.Parse(SugProduct[i][1].ToString());

                        db.SugProduct.Add(sugProduct);
                    }
                }
                //20150813新增-Hsiuyang-解決方式知識庫附件
                if (wBSEventViewModel.KnowledgeJson != null)
                {
                    JArray KnowledgeData = JsonConvert.DeserializeObject<JArray>(wBSEventViewModel.KnowledgeJson);
                    for (int i = 0; i < KnowledgeData.Count; i++)
                    {
                        Files knowledgefile = new Files();
                        knowledgefile.CID = wBSEventC.Num;
                        knowledgefile.Class = 3;
                        knowledgefile.FileAddr = "";
                        knowledgefile.FileName = "";
                        //knowledgefile.KnowledgeID = Int32.Parse(KnowledgeData[i][0].ToString());
                        knowledgefile.KnowledgeID = Int32.Parse(KnowledgeData[i]["KnowledgeID"].ToString());
                        db.Files.Add(knowledgefile);
                    }
                }
                //20150814新增-Hsiuyang-執行預先保養單
                if (wBSEventViewModel.MaintainCaseJson != null)
                {
                    JArray maintainCases = JsonConvert.DeserializeObject<JArray>(wBSEventViewModel.MaintainCaseJson);
                    for (int i = 0; i < maintainCases.Count; i++)
                    {
                        EventRelation maintainCase = new EventRelation();
                        maintainCase.CID = wBSEventC.Num;
                        maintainCase.RelationCID = Int32.Parse(maintainCases[i]["cID"].ToString());

                        db.EventRelation.Add(maintainCase);
                    }
                }
                //20151113新增-Hsiuyang-Hyperlink建議參考網頁(CLASS=9)
                if (wBSEventViewModel.HyperlinkJson != null)
                {
                    JArray Hyperlink = JsonConvert.DeserializeObject<JArray>(wBSEventViewModel.HyperlinkJson);
                    for (int i = 0; i < Hyperlink.Count; i++)
                    {
                        Files hyperlink = new Files();
                        hyperlink.CID = wBSEventC.Num;
                        hyperlink.Class = 9;
                        hyperlink.FileAddr = Hyperlink[i]["Href"].ToString();
                        hyperlink.FileName = Hyperlink[i]["Name"].ToString();

                        db.Files.Add(hyperlink);
                    }
                }
                //20151221-自動派單 20160906改轉派工時選擇人派工
                if (wBSEventViewModel.ResponsibleID != "" && wBSEventViewModel.ResponsibleID != null)
                {
                    //String subAreaID = db.Customer.AsEnumerable().Where(x => x.CustomerRef == wBSEventM.CustomerRef).Select(x => x.RegionID).First();
                    //String AreaID = db.SubArea.AsEnumerable().Where(x => x.SubAreaID == subAreaID).Select(x => x.AreaID).First();
                    //int rID = 0;

                    //if (db.SystemSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AutoAssign == true).Count() > 0) //事件個別指定
                    //{
                    //    if (db.SystemSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AutoAssignUserType == 2).Count() > 0)
                    //        rID = db.SubArea.AsEnumerable().Where(x => x.SubAreaID == subAreaID).Select(x => x.ResponsibleID).First();
                    //    else
                    //    {
                    //        if (db.DefaultAssignSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AreaID == AreaID).Count() > 0)
                    //            rID = db.DefaultAssignSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AreaID == AreaID).Select(x => x.ResponsibleClassID).First();
                    //        else if (db.DefaultAssignSetting.Where(x => x.isSysDefault == true && x.AreaID == AreaID).Count() > 0)
                    //            rID = db.DefaultAssignSetting.Where(x => x.isSysDefault == true && x.AreaID == AreaID).Select(x => x.ResponsibleClassID).First();
                            
                    //    }
                    //}
                    //else //系統預設
                    //{
                    //    if (db.SystemSetting.Where(x => x.isSysDefault == true && x.AutoAssignUserType == 2).Count() > 0)
                    //        rID = db.SubArea.AsEnumerable().Where(x => x.SubAreaID == subAreaID).Select(x => x.ResponsibleID).First();
                    //    else
                    //    {
                    //        if (db.DefaultAssignSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AreaID == AreaID).Count() > 0)
                    //            rID = db.DefaultAssignSetting.Where(x => x.EventClass == wBSEventViewModel.EventClass && x.AreaID == AreaID).Select(x => x.ResponsibleClassID).First();
                    //        else if (db.DefaultAssignSetting.Where(x => x.isSysDefault == true && x.AreaID == AreaID).Count() > 0)
                    //            rID = db.DefaultAssignSetting.Where(x => x.isSysDefault == true && x.AreaID == AreaID).Select(x => x.ResponsibleClassID).First();
                             
                    //    }

                    //}

                    //wBSEventC.ResponsibleID = rID;

                    wBSEventC.ResponsibleID = int.Parse(wBSEventViewModel.ResponsibleID);

                    wBSEventC.AssignCreateDT = DateTime.Now;
                    wBSEventC.AssignCreateUserID = wBSEventViewModel.CreateUserID;
                    wBSEventC.AssignDT = DateTime.Now;
                    wBSEventC.AssignUserID = wBSEventViewModel.CreateUserID;
                    wBSEventC.AssignModifyDT = DateTime.Now;
                    wBSEventC.AssignModifyUserID = wBSEventViewModel.CreateUserID;

                    wBSEventC.RespondSW = "Y";
                    wBSEventC.RespondDT = DateTime.Now;
                    wBSEventC.RespondReason = "";
                    //wBSEventC.ScheduleStartDT = wBSEventViewModel.ExpectArriveDT;
                    //wBSEventC.ScheduleEndDT = wBSEventViewModel.ExpectFinishDT;
                    //wBSEventC.ScheduleServiceHours = wBSEventC.QuestionServiceHours;
                    
                    wBSEventC.EventStatus = 30; //30任務已接受未排程

                    //String ExpectArriveSW, EventStatusStr;
                    //if (wBSEventC.ExpectArriveSW == "0")
                    //    ExpectArriveSW = "自行約定";
                    //else if (wBSEventC.ExpectArriveSW == "1")
                    //    ExpectArriveSW = "合約規定";
                    //else if (wBSEventC.ExpectArriveSW == "2")
                    //    ExpectArriveSW = "建議期望";
                    //else if (wBSEventC.ExpectArriveSW == "4")
                    //    ExpectArriveSW = "自派新單";
                    //else
                    //    ExpectArriveSW = "客戶指定";

                    //String ExpectArriveDTStr = wBSEventC.ExpectArriveDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                    //if (wBSEventC.ExpectArriveSW == "0")
                    //    ExpectArriveDTStr = "自行約定";
                                        
                    //EventStatusStr = "已接受未排程";


                    //String UserID = db.ResponsibleClass.Where(x => x.ID == rID).Select(x => x.UserID).First();
                    //String UserName = db.ResponsibleClass.Where(x => x.ID == rID).Select(x => x.UserData2.UserName).First();
                    //ret[3] = db.ResponsibleClass.Where(x => x.ID == rID).Select(x => x.Name).First() + "-" + UserName;

                    //String url = "http://" + wBSEventViewModel.Url + "/Mobile/MobileCaseDetail?type=2&&mid=" + wBSEventC.MomCaseID + "&&cid=" + wBSEventC.Num;
                    //String title = "";      //[燈號]-[推播事件說明]
                    //String content = "";    //[被推播人員姓名]您好:案件-[客戶簡稱]-[到達約定選擇]:[應到達時間],[事件說明],請您盡速處理。
                    //title = "綠燈-執行人已接受未排程";
                    //content = UserName + "您好:案件-" + wBSEventM.Customer1.ShortName + "-" + ExpectArriveSW + ":" + ExpectArriveDTStr + "," + EventStatusStr + ",請您盡速處理。";

                    //messageRepository.AddMsgbyUser(UserID, title, content, url);
                }

                WBSEventC tmp = db.WBSEventC.Find(wBSEventViewModel.C_ID);
                if (tmp.EventStatus == 49)
                {
                    tmp.EventStatus = 50;
                }

                //db.WBSEventM.Add(wBSEventM);
                db.WBSEventC.Add(wBSEventC);
                db.SaveChanges();
                //ret = "true";
                ret[0] = wBSEventViewModel.WBSCaseNumberM + "-" + wBSEventViewModel.WBSCaseNumberC;
            }
            catch
            {
            }
            return ret;
        }
        //派單主畫面-派單
        public String Assign(int mID, int cID, int rID, String uid, String url)
        {
            String ret = "false";
            String eventNum = "";
            String title = "";
            String UserID = db.ResponsibleClass.AsEnumerable().Where(x => x.ID == rID).Select(x => x.UserID).First();
            String UserName = db.UserData.AsEnumerable().Where(x => x.UserID == UserID).Select(x => x.UserName).First();
            String content = "";
            WBSEventM wBSEventM = db.WBSEventM.Find(mID);
            String ExpectArriveSW = "", ExpectArriveDTStr = "", EventStatusStr = "";
            eventNum = wBSEventM.CaseClass + wBSEventM.CaseDate + "-";
            if (wBSEventM.CaseCode < 10)
                eventNum += "0000" + wBSEventM.CaseCode.ToString();
            else if (wBSEventM.CaseCode < 100 && wBSEventM.CaseCode >= 10)
                eventNum += "000" + wBSEventM.CaseCode.ToString();
            else if (wBSEventM.CaseCode < 1000 && wBSEventM.CaseCode >= 100)
                eventNum += "00" + wBSEventM.CaseCode.ToString();
            else if (wBSEventM.CaseCode < 10000 && wBSEventM.CaseCode >= 1000)
                eventNum += "0" + wBSEventM.CaseCode.ToString();
            else
                eventNum += wBSEventM.CaseCode.ToString();
            int tmpMStatus = Int32.Parse(wBSEventM.CaseMStatus);
            if (tmpMStatus <= 3 && tmpMStatus >= 1)
            {
                wBSEventM.CaseMStatus = "2";
            }
            else if (tmpMStatus <= 6 && tmpMStatus >= 4)
            {
                wBSEventM.CaseMStatus = "5";
            }
            try {
                WBSEventC wBSEventC = db.WBSEventC.Find(cID);
                eventNum += "-" + ((wBSEventC.ChildCaseID < 10) ? ("0" + wBSEventC.ChildCaseID.ToString()) : wBSEventC.ChildCaseID.ToString());
                if (wBSEventC.EventStatus == 50)
                    ret = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-案件已解決,無法重新派遣人員,單號:" + eventNum + "。";

                if (wBSEventC.EventStatus == 0)
                    ret = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-案件已刪除,無法派遣人員,單號:" + eventNum + "。";

                else if (wBSEventC.EventStatus >= 11 && wBSEventC.EventStatus < 41)
                {
                    
                    wBSEventC.AssignUserID = uid;
                    wBSEventC.AssignDT = DateTime.Now;
                    wBSEventC.ResponsibleID = rID;
                    wBSEventC.EventStatus = 21;
                    wBSEventC.ModifyDateTime = DateTime.Now;
                    wBSEventC.ModifyUserID = uid;
                    if (wBSEventC.AssignCreateUserID == null)
                    {
                        wBSEventC.AssignCreateDT = DateTime.Now;
                        wBSEventC.AssignCreateUserID = uid;
                    }                    
                    wBSEventC.RespondSW = null;
                    wBSEventC.RespondReason = null;
                    wBSEventC.RespondDT = null;                    
                    wBSEventC.AssignModifyDT = DateTime.Now;
                    wBSEventC.AssignModifyUserID = uid;
                    wBSEventC.ScheduleStartDT = null;
                    wBSEventC.ScheduleEndDT = null;
                    wBSEventC.ScheduleServiceHours = null;
                    
                    

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

                    ExpectArriveDTStr = wBSEventC.ExpectArriveDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                    if (wBSEventC.ExpectArriveSW == "0")
                        ExpectArriveDTStr = "自行約定";

                    //int temp2 = wBSEventC.EventStatus;

                    EventStatusStr = GetEventStatusStr(wBSEventC.EventStatus);

                    var qryE = db.EventRelation.Where(x => x.CID == cID);  //尋找本單夾帶保養單,一併拒絕或接受;
                    foreach (EventRelation E in qryE)
                    {

                        if (E.WBSEventC.EventStatus == 0 || E.WBSEventC.EventStatus >= 41)
                            continue;
                        //ConfirmedData(uid, E.RelationCID.ToString(), note, isSave, time);
                        E.WBSEventC.ModifyDateTime = DateTime.Now;
                        E.WBSEventC.ModifyUserID = uid;
                        E.WBSEventC.AssignUserID = uid;
                        E.WBSEventC.AssignDT = DateTime.Now;
                        E.WBSEventC.ResponsibleID = rID;
                        E.WBSEventC.EventStatus = 21;
                        E.WBSEventC.RespondSW = null;
                        E.WBSEventC.RespondReason = null;
                        E.WBSEventC.RespondDT = null;
                        E.WBSEventC.ScheduleStartDT = null;
                        E.WBSEventC.ScheduleEndDT = null;
                        E.WBSEventC.ScheduleServiceHours = null;
                        if (E.WBSEventC.AssignCreateUserID == null)
                        {
                            E.WBSEventC.AssignCreateDT = DateTime.Now;
                            E.WBSEventC.AssignCreateUserID = uid;
                        }
                        E.WBSEventC.AssignModifyDT = DateTime.Now;
                        E.WBSEventC.AssignModifyUserID = uid;
                    }
                    db.SaveChanges();

                    url = "http://" + url + "/Mobile/MobileCaseDetail?type=0&&mid=" + mID + "&&cid=" + cID;
                    //[燈號]-[推播事件說明]
                    title = "綠燈-執行人未接受";
                    //[被推播人員姓名]您好:案件-[客戶簡稱]-[到達約定選擇]:[應到達時間],[事件說明],請您盡速處理。                    
                    content = UserName + "您好:案件-" + wBSEventM.Customer1.ShortName + "-" + ExpectArriveSW + ":" + ExpectArriveDTStr + "," + EventStatusStr + ",請您盡速處理。";

                    messageRepository.AddMsgbyUser(UserID, title, content, url);

                    ret = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-派工成功,單號:" + eventNum + "。";


                }
                else if (wBSEventC.EventStatus >= 41 && wBSEventC.EventStatus < 50)
                {
                    if (wBSEventC.EventStatus == 41)
                    {
                        wBSEventC.ServiceLeaveDT = DateTime.Parse("1900/01/01");
                        wBSEventC.ServiceLeaveSysDT = DateTime.Parse("1900/01/01");
                        wBSEventC.ServiceTakeHours = 0;
                    }
                    if (wBSEventC.EventStatus <= 42)
                    {
                        wBSEventC.ServiceReport = "";
                    }
                    if (wBSEventC.EventStatus <= 43)
                    {

                        if (wBSEventC.ProvideSpareMach == null)
                            wBSEventC.ProvideSpareMach = "F";
                        if (wBSEventC.RetrieveSpareMach == null)
                            wBSEventC.RetrieveSpareMach = "F";
                        if (wBSEventC.RetrieveUserMach == null)
                            wBSEventC.RetrieveUserMach = "F";
                        if (wBSEventC.ReturnUserMach == null)
                            wBSEventC.ReturnUserMach = "F";

                        wBSEventC.MachMemo = "";
                        wBSEventC.EventStatus = 50;
                        wBSEventC.QuestionFinishDT = DateTime.Now;
                        wBSEventC.QuestionFinishSysDT = DateTime.Now;
                        wBSEventC.QuestionFinishSW = "Y";
                        wBSEventC.ModifyDateTime = DateTime.Now;
                        wBSEventC.ModifyUserID = uid;
                        wBSEventC.AbnormalModifySW = "Y";
                    }

                    WBSEventC NwBSEventC = new WBSEventC();
                    int num = db.WBSEventC.AsEnumerable().Select(x => x.Num).DefaultIfEmpty(0).Max(x => x) + 1;
                    NwBSEventC.Num = num;
                    NwBSEventC.MomCaseID = wBSEventC.MomCaseID;
                    int childCaseID = db.WBSEventC.AsEnumerable().Where(x => x.MomCaseID == wBSEventC.MomCaseID).Select(x => x.ChildCaseID).DefaultIfEmpty(0).Max(x => x) + 1;
                    NwBSEventC.ChildCaseID = childCaseID;

                    string eventNumN = eventNum + "-" + ((NwBSEventC.ChildCaseID < 10) ? ("0" + NwBSEventC.ChildCaseID.ToString()) : NwBSEventC.ChildCaseID.ToString());
                    string eventNumO = eventNum + "-" + ((wBSEventC.ChildCaseID < 10) ? ("0" + wBSEventC.ChildCaseID.ToString()) : wBSEventC.ChildCaseID.ToString());
                    NwBSEventC.EventClass = wBSEventC.EventClass;
                    NwBSEventC.ProductClass = wBSEventC.ProductClass;
                    NwBSEventC.ItemClass = wBSEventC.ItemClass;
                    NwBSEventC.QuestionClass = wBSEventC.QuestionClass;
                    NwBSEventC.QuestionGrade = wBSEventC.QuestionGrade;
                    NwBSEventC.QuestionServiceHours = wBSEventC.QuestionServiceHours;
                    NwBSEventC.QuestionDescription = wBSEventC.QuestionDescription;
                    NwBSEventC.QuestionCreateUserID = uid;
                    NwBSEventC.QuestionCreateDateTime = DateTime.Now;
                    NwBSEventC.ExpectArriveDateTime = wBSEventC.ExpectArriveDateTime;
                    NwBSEventC.ExpectFinishDateTime = wBSEventC.ExpectFinishDateTime;
                    NwBSEventC.ExpectArriveSW = wBSEventC.ExpectArriveSW;
                    NwBSEventC.CreateDateTime = DateTime.Now;
                    NwBSEventC.CreateUserID = uid;
                    NwBSEventC.ModifyDateTime = DateTime.Now;
                    NwBSEventC.ModifyUserID = uid;
                    //20150811新增-Hsiuyang
                    NwBSEventC.SugProvideSpareMach = wBSEventC.SugProvideSpareMach;
                    NwBSEventC.SugRetrieveSpareMach = wBSEventC.SugRetrieveSpareMach;
                    NwBSEventC.SugRetrieveUserMach = wBSEventC.SugRetrieveUserMach;
                    NwBSEventC.SugReturnUserMach = wBSEventC.SugReturnUserMach;
                    NwBSEventC.SugMachMemo = wBSEventC.SugMachMemo;   //建議備機說明

                    NwBSEventC.EventStatus = 21;
                    //20150813新增-Hsiuyang

                    NwBSEventC.AssignDescription = wBSEventC.AssignDescription;  //解決方式(派單)描述
                    NwBSEventC.AssignUserID = uid;
                    NwBSEventC.AssignDT = DateTime.Now;
                    NwBSEventC.ResponsibleID = rID;

                    NwBSEventC.AssignCreateDT = DateTime.Now;
                    NwBSEventC.AssignCreateUserID = uid;

                    NwBSEventC.AssignModifyDT = DateTime.Now;
                    NwBSEventC.AssignModifyUserID = uid;
                    db.WBSEventC.Add(NwBSEventC);
                    db.SaveChanges();

                    //缺-Hsiuyang-SugProduct建議部分

                    //缺-Hsiuyang-解決方式知識庫附件

                    //缺-Hsiuyang-執行預先保養單

                    //缺-Hsiuyang-Hyperlink建議參考網頁(CLASS=9)

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

                    ExpectArriveDTStr = wBSEventC.ExpectArriveDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                    if (wBSEventC.ExpectArriveSW == "0")
                        ExpectArriveDTStr = "自行約定";

                    EventStatusStr = GetEventStatusStr(wBSEventC.EventStatus);

                    url = "http://" + url + "/Mobile/MobileCaseDetail?type=0&&mid=" + mID + "&&cid=" + num;
                    //[燈號]-[推播事件說明]
                    title = "綠燈-執行人未接受";
                    //[被推播人員姓名]您好:案件-[客戶簡稱]-[到達約定選擇]:[應到達時間],[事件說明],請您盡速處理。                    
                    content = UserName + "您好:案件-" + wBSEventM.Customer1.ShortName + "-" + ExpectArriveSW + ":" + ExpectArriveDTStr + "," + EventStatusStr + ",請您盡速處理。";
                    messageRepository.AddMsgbyUser(UserID, title, content, url);


                    ret = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-強制解決成功,單號:" + eventNumO + "。";
                    ret += wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-派工成功,單號:" + eventNumN + "。";
                }                
            }
            catch {
                ret = eventNum + "派工失敗。";
            }

            return ret;
        }
        //建立後續叫修單
        public string[] FollowUpCreate(WBSEventViewModel wBSEventViewModel)
        {
            string[] ret = new string[3];

            ret[0] = "false";
            ret[1] = "";
            ret[2] = "";    //area
            int  num = 0, childCaseID = 0;                        
            
            //string groupSW = db.Customer.AsEnumerable().Where(x => x.CustomerRef == wBSEventViewModel.CustomerRef).Select(x => x.GroupSW).First();

            if (wBSEventViewModel.ExpectArriveDTStr == null)
                wBSEventViewModel.ExpectArriveDTStr = "19000101 00:01";
            if (wBSEventViewModel.ExpectFinishDTStr == null)
                wBSEventViewModel.ExpectFinishDTStr = "19000101 00:01";
            wBSEventViewModel.ExpectArriveDT = DateTime.ParseExact(wBSEventViewModel.ExpectArriveDTStr, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture);
            wBSEventViewModel.ExpectFinishDT = DateTime.ParseExact(wBSEventViewModel.ExpectFinishDTStr, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture);

            try
            {
                WBSEventM wBSEventM = db.WBSEventM.Find(wBSEventViewModel.M_ID);
                WBSEventC oldEventC = db.WBSEventC.Find(wBSEventViewModel.C_ID);
                wBSEventM.CaseMStatus = "5";    //5為部分子單已解決
                num = db.WBSEventC.AsEnumerable().Select(x => x.Num).DefaultIfEmpty(0).Max(x => x) + 1;
                childCaseID = db.WBSEventC.AsEnumerable().Where(x => x.MomCaseID == wBSEventM.CaseID).Select(x => x.ChildCaseID).DefaultIfEmpty(0).Max(x => x) + 1;
                ret[2] = GetCustomerArea(wBSEventM.CustomerRef);

                WBSEventC wBSEventC = new WBSEventC();
                ret[1] = num.ToString();
                wBSEventC.Num = num;
                wBSEventC.MomCaseID = oldEventC.MomCaseID;
                wBSEventC.ChildCaseID = childCaseID;
                wBSEventViewModel.WBSCaseNumberC = (wBSEventC.ChildCaseID < 10) ? ("0" + wBSEventC.ChildCaseID.ToString()) : wBSEventC.ChildCaseID.ToString();
                wBSEventC.EventClass = wBSEventViewModel.EventClass;
                wBSEventC.ProductClass = oldEventC.ProductClass;
                wBSEventC.ItemClass = oldEventC.ItemClass;
                wBSEventC.QuestionClass = oldEventC.QuestionClass;
                wBSEventC.QuestionGrade = oldEventC.QuestionGrade;
                wBSEventC.QuestionServiceHours = oldEventC.QuestionServiceHours;
                wBSEventC.QuestionDescription = wBSEventViewModel.QuestionDescription;
                wBSEventC.QuestionCreateUserID = wBSEventViewModel.QuestionCreateUserID;
                wBSEventC.QuestionCreateDateTime = DateTime.Now;
                wBSEventC.ExpectArriveDateTime = wBSEventViewModel.ExpectArriveDT;
                wBSEventC.ExpectFinishDateTime = wBSEventViewModel.ExpectFinishDT;
                wBSEventC.ExpectArriveSW = wBSEventViewModel.ExpectArriveSW;
                //wBSEventC.DeleteMarkSW = wBSEventViewModel.DeleteMarkSW;
                //wBSEventC.DeleteReason = wBSEventViewModel.DeleteReason;
                //wBSEventC.DeleteMarkUserID = wBSEventViewModel.DeleteMarkUserID;
                //wBSEventC.DeleteMarkDateTime = wBSEventViewModel.DeleteMarkDT;
                wBSEventC.CreateDateTime = DateTime.Now;
                wBSEventC.CreateUserID = wBSEventViewModel.CreateUserID;
                wBSEventC.ModifyDateTime = DateTime.Now;
                wBSEventC.ModifyUserID = wBSEventViewModel.CreateUserID;
                
                wBSEventC.SugProvideSpareMach = "F";
                wBSEventC.SugRetrieveSpareMach = "F";
                wBSEventC.SugRetrieveUserMach = "F";
                wBSEventC.SugReturnUserMach = "F";
                wBSEventC.EventStatus = 11; //11為問題已開立
                
                //db.WBSEventM.Add(wBSEventM);
                db.WBSEventC.Add(wBSEventC);
                db.SaveChanges();
                //ret = "true";
                ret[0] = wBSEventViewModel.WBSCaseNumberM + "-" + wBSEventViewModel.WBSCaseNumberC;
            }
            catch
            {
            }
            return ret;

        }
        //建立新叫修單
        public string[] Create(WBSEventViewModel wBSEventViewModel)
        {
            string[] ret = new string[3];
            ret[0] = "false";
            ret[1] = "";
            ret[2] = "";    //area
            int casecode = 0, caseID = 0, num = 0, childCaseID = 0;
            casecode = db.WBSEventM.AsEnumerable().Where(x => x.CaseClass == "C" && x.CaseDate == DateTime.Now.ToString("yyyyMMdd")).Select(x => x.CaseCode).DefaultIfEmpty(0).Max(x => x) + 1;
            caseID = db.WBSEventM.AsEnumerable().Select(x => x.CaseID).DefaultIfEmpty(0).Max(x => x) + 1;
            num = db.WBSEventC.AsEnumerable().Select(x => x.Num).DefaultIfEmpty(0).Max(x => x) + 1;
            childCaseID = db.WBSEventC.AsEnumerable().Where(x => x.MomCaseID == caseID).Select(x => x.ChildCaseID).DefaultIfEmpty(0).Max(x => x) + 1;
            int questionGrade = db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == Int32.Parse(wBSEventViewModel.QuestionClass)).Select(x => x.QuestionGrade).First();
            int serviceHours = db.QuestionClass.AsEnumerable().Where(x => x.QuestionClassID == Int32.Parse(wBSEventViewModel.QuestionClass)).Select(x => x.QuestionServiceHours).First();
            string groupSW = db.Customer.AsEnumerable().Where(x => x.CustomerRef == wBSEventViewModel.CustomerRef).Select(x => x.GroupSW).First();

            if (wBSEventViewModel.ExpectArriveDTStr == null)
                wBSEventViewModel.ExpectArriveDTStr = "19000101 00:01";
            if (wBSEventViewModel.ExpectFinishDTStr == null)
                wBSEventViewModel.ExpectFinishDTStr = "19000101 00:01";
            wBSEventViewModel.ExpectArriveDT = DateTime.ParseExact(wBSEventViewModel.ExpectArriveDTStr, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture);
            wBSEventViewModel.ExpectFinishDT = DateTime.ParseExact(wBSEventViewModel.ExpectFinishDTStr, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture);
            ret[2] = GetCustomerArea(wBSEventViewModel.CustomerRef);


            try
            {
                WBSEventM wBSEventM = new WBSEventM();
                wBSEventM.CaseID = caseID;
                //wBSEventM.CaseClass = wBSEventViewModel.WBSCaseNumberM.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries)[0];
                //wBSEventM.CaseClass = wBSEventViewModel.WBSCaseNumberM.Substring(0, 1);
                wBSEventM.CaseClass = "C";
                //wBSEventM.CaseDate = wBSEventViewModel.WBSCaseNumberM.Substring(1, 8);
                wBSEventM.CaseDate = DateTime.Now.ToString("yyyyMMdd");
                wBSEventM.CaseCode = casecode;
                wBSEventViewModel.WBSCaseNumberM = wBSEventM.CaseClass + wBSEventM.CaseDate + "-";
                if (wBSEventM.CaseCode < 10)
                    wBSEventViewModel.WBSCaseNumberM += "0000" + wBSEventM.CaseCode.ToString();
                else if (wBSEventM.CaseCode < 100 && wBSEventM.CaseCode >= 10)
                    wBSEventViewModel.WBSCaseNumberM += "000" + wBSEventM.CaseCode.ToString();
                else if (wBSEventM.CaseCode < 1000 && wBSEventM.CaseCode >= 100)
                    wBSEventViewModel.WBSCaseNumberM += "00" + wBSEventM.CaseCode.ToString();
                else if (wBSEventM.CaseCode < 10000 && wBSEventM.CaseCode >= 1000)
                    wBSEventViewModel.WBSCaseNumberM += "0" + wBSEventM.CaseCode.ToString();
                else
                    wBSEventViewModel.WBSCaseNumberM += wBSEventM.CaseCode.ToString();
                wBSEventM.CustomerRef = wBSEventViewModel.CustomerRef;
                wBSEventM.CustomerUserID = wBSEventViewModel.CustomerUserID;
                wBSEventM.CustomerUserName = wBSEventViewModel.CustomerUserName;
                wBSEventM.CustomerUserPhone1 = wBSEventViewModel.CustomerUserPhone1;
                wBSEventM.CustomerUserPhone2 = wBSEventViewModel.CustomerUserPhone2;
                //20150811新增-Hsiuyang
                if (wBSEventViewModel.ChangeAddr != null)
                    if (wBSEventViewModel.ChangeAddr.Trim().Length > 0)
                        wBSEventM.CustomerAddr = wBSEventViewModel.ChangeAddr;
                wBSEventM.SpareMach = "R";      //預設無借出備機
                wBSEventM.UserMach = "R";       //預設無取回客戶機器
                wBSEventM.CaseMStatus = "1";    //1為開立新問題

                WBSEventC wBSEventC = new WBSEventC();
                ret[1] = num.ToString();
                wBSEventC.Num = num;
                wBSEventC.MomCaseID = caseID;
                wBSEventC.ChildCaseID = childCaseID;
                wBSEventViewModel.WBSCaseNumberC = (wBSEventC.ChildCaseID < 10) ? ("0" + wBSEventC.ChildCaseID.ToString()) : wBSEventC.ChildCaseID.ToString();
                wBSEventC.EventClass = "C01";
                wBSEventC.ProductClass = Int32.Parse(wBSEventViewModel.ProductClass);
                wBSEventC.ItemClass = Int32.Parse(wBSEventViewModel.ItemClass);
                wBSEventC.QuestionClass = Int32.Parse(wBSEventViewModel.QuestionClass);
                wBSEventC.QuestionGrade = questionGrade;
                wBSEventC.QuestionServiceHours = serviceHours;
                wBSEventC.QuestionDescription = wBSEventViewModel.QuestionDescription;                
                wBSEventC.QuestionCreateUserID = wBSEventViewModel.QuestionCreateUserID;
                wBSEventC.QuestionCreateDateTime = DateTime.Now;
                wBSEventC.ExpectArriveDateTime = wBSEventViewModel.ExpectArriveDT;
                wBSEventC.ExpectFinishDateTime = wBSEventViewModel.ExpectFinishDT;
                wBSEventC.ExpectArriveSW = wBSEventViewModel.ExpectArriveSW;
                wBSEventC.DeleteMarkSW = wBSEventViewModel.DeleteMarkSW;
                wBSEventC.DeleteReason = wBSEventViewModel.DeleteReason;
                wBSEventC.DeleteMarkUserID = wBSEventViewModel.DeleteMarkUserID;
                wBSEventC.DeleteMarkDateTime = wBSEventViewModel.DeleteMarkDT;
                wBSEventC.CreateDateTime = DateTime.Now;
                wBSEventC.CreateUserID = wBSEventViewModel.CreateUserID;
                wBSEventC.ModifyDateTime = DateTime.Now;
                wBSEventC.ModifyUserID = wBSEventViewModel.CreateUserID;
                //wBSEventC.ModifyDateTime = wBSEventViewModel.ModifyDT;
                //wBSEventC.ModifyUserID = wBSEventViewModel.ModifyUserID;
                //20150811新增-Hsiuyang
                wBSEventC.SugProvideSpareMach = wBSEventViewModel.SugProvideSpareMach;
                wBSEventC.SugRetrieveSpareMach = wBSEventViewModel.SugRetrieveSpareMach;
                wBSEventC.SugRetrieveUserMach = wBSEventViewModel.SugRetrieveUserMach;
                wBSEventC.SugReturnUserMach = wBSEventViewModel.SugReturnUserMach;
                wBSEventC.EventStatus = 11; //11為問題已開立
                //20150813新增-Hsiuyang
                if (wBSEventViewModel.Closed == "T") //線上處理結案影響
                {
                    wBSEventM.CaseMStatus = "9";    //9為問題已結案
                    wBSEventM.CloseDT = DateTime.Now;
                    wBSEventC.EventStatus = 50;     //50為任務已解決(維修附件上傳)
                    wBSEventC.QuestionFinishSW = "Y";
                    wBSEventC.QuestionFinishDT = DateTime.Now;
                    wBSEventC.ServiceReport = wBSEventViewModel.AssignDescription;  //當結案時解決方式即為服務報告
                }
                wBSEventC.AssignDescription = wBSEventViewModel.AssignDescription;  //解決方式(派單)描述
                

                //20150812新增-Hsiuyang-SugProduct建議部分
                JArray SugProduct = JsonConvert.DeserializeObject<JArray>(wBSEventViewModel.MaterialJson);
                for (int i = 0; i < SugProduct.Count; i++)
                {
                    SugProduct sugProduct = new SugProduct();
                    sugProduct.CID = wBSEventC.Num;
                    sugProduct.Class = Int32.Parse(SugProduct[i]["Class"].ToString());
                    //sugProduct.Class = Int32.Parse(SugProduct[i][0].ToString());
                    //if (SugProduct[i][1].ToString() == "")
                    if (SugProduct[i]["ProductNum"].ToString() == "")
                        sugProduct.ProductName = SugProduct[i]["ProductName"].ToString();
                    //sugProduct.ProductName = SugProduct[i][2].ToString();
                    else
                        sugProduct.ProductNum = Int32.Parse(SugProduct[i]["ProductNum"].ToString());
                    //sugProduct.ProductNum = Int32.Parse(SugProduct[i][1].ToString());

                    db.SugProduct.Add(sugProduct);
                }
                //20150813新增-Hsiuyang-解決方式知識庫附件
                JArray KnowledgeData = JsonConvert.DeserializeObject<JArray>(wBSEventViewModel.KnowledgeJson);
                for (int i = 0; i < KnowledgeData.Count; i++)
                {
                    Files knowledgefile = new Files();
                    knowledgefile.CID = wBSEventC.Num;
                    knowledgefile.Class = 3;
                    knowledgefile.FileAddr = "";
                    knowledgefile.FileName = "";
                    //knowledgefile.KnowledgeID = Int32.Parse(KnowledgeData[i][0].ToString());
                    knowledgefile.KnowledgeID = Int32.Parse(KnowledgeData[i]["KnowledgeID"].ToString());
                    db.Files.Add(knowledgefile);
                }
                //20150814新增-Hsiuyang-執行預先保養單
                JArray maintainCases = JsonConvert.DeserializeObject<JArray>(wBSEventViewModel.MaintainCaseJson);
                for (int i = 0; i < maintainCases.Count; i++)
                {
                    EventRelation maintainCase = new EventRelation();
                    maintainCase.CID = wBSEventC.Num;
                    maintainCase.RelationCID = Int32.Parse(maintainCases[i]["cID"].ToString());

                    db.EventRelation.Add(maintainCase);
                }



                db.WBSEventM.Add(wBSEventM);
                db.WBSEventC.Add(wBSEventC);
                db.SaveChanges();
                //ret = "true";
                ret[0] = wBSEventViewModel.WBSCaseNumberM + "-" + wBSEventViewModel.WBSCaseNumberC;
            }
            catch
            {
            }
            return ret;
        }