//執行人未結案轉派工或結案-手機
        public String[] CloseCCase(CloseCaseViewModel closeCase) {
            String[] ret = new String[3];
            ret[0] = "false";   //回饋訊息
            ret[1] = "";        //案號
            ret[2] = "";        //mid
            Boolean canClosed = true;       //判斷是否母單可以結案
            Boolean SpareMach = false;      //判斷目前備機是否提供 (true:提供中,flase:未提供)
            Boolean UserMach = false;       //判斷目前客戶機器是否取回 (true:取回中,flase:未取回)
            String SpareMachCaseNum = "";
            String UserMachCaseNum = "";
            int tempCid = Int32.Parse(closeCase.Cid);

            WBSEventC wBSEventC = db.WBSEventC.Find(tempCid);
            WBSEventM wBSEventM = db.WBSEventM.Find(wBSEventC.MomCaseID);
            String caseNum = "", eventNumM;
            ret[2] = wBSEventC.MomCaseID.ToString();

            caseNum = wBSEventM.CaseClass + wBSEventM.CaseDate + "-";
            if (wBSEventM.CaseCode < 10)
                caseNum += "0000" + wBSEventM.CaseCode.ToString();
            else if (wBSEventM.CaseCode < 100 && wBSEventM.CaseCode >= 10)
                caseNum += "00" + wBSEventM.CaseCode.ToString();
            else if (wBSEventM.CaseCode < 10000 && wBSEventM.CaseCode >= 1000)
                caseNum += "0" + wBSEventM.CaseCode.ToString();
            else
                caseNum += wBSEventM.CaseCode.ToString();
            eventNumM = caseNum;
            caseNum += "-" + ((wBSEventC.ChildCaseID < 10) ? ("0" + wBSEventC.ChildCaseID.ToString()) : wBSEventC.ChildCaseID.ToString());

            wBSEventC.ModifyDateTime = DateTime.Now;
            wBSEventC.ModifyUserID = closeCase.UserID;
            wBSEventC.ServiceModifyDT = DateTime.Now;
            wBSEventC.ServiceModifyUserID = closeCase.UserID;
            wBSEventC.QuestionFinishDT = DateTime.Now;
            wBSEventC.QuestionFinishSysDT = DateTime.Now;
            wBSEventC.QuestionFinishSW = "Y";

            wBSEventC.ProvideSpareMach = closeCase.ProvideSpareMach;
            wBSEventC.RetrieveSpareMach = closeCase.RetrieveSpareMach;
            wBSEventC.RetrieveUserMach = closeCase.RetrieveUserMach;
            wBSEventC.ReturnUserMach = closeCase.ReturnUserMach;
            wBSEventC.MachMemo = closeCase.MachMemo;
            wBSEventC.ServiceReport = closeCase.ServiceReport;

            //20150825新增-Hsiuyang-SugProduct實際更換削除部分
            JArray SugProduct = JsonConvert.DeserializeObject<JArray>(closeCase.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());                
                if (SugProduct[i]["ProductNum"].ToString() == "")
                    sugProduct.ProductName = SugProduct[i]["ProductName"].ToString();                
                else
                    sugProduct.ProductNum = Int32.Parse(SugProduct[i]["ProductNum"].ToString());                

                db.SugProduct.Add(sugProduct);
            }

            if (wBSEventC.EventStatus != 42) //未離開直接結案
            {
                wBSEventC.ServiceLeaveDT = DateTime.Now;
                wBSEventC.ServiceLeaveSysDT = DateTime.Now;

                System.TimeSpan diff1 = DateTime.Now.Subtract(wBSEventC.ServiceArriveDT ?? DateTime.Now);                
                int day = Int32.Parse(diff1.ToString("%d"));
                int hour = Int32.Parse(diff1.ToString("%h"));
                int minute = Int32.Parse(diff1.ToString("%m"));
                hour += day * 24;
                if (minute > 0)
                    hour++;
                wBSEventC.ServiceTakeHours = hour;
            }

            try
            {
                if (closeCase.EndType == "2") //暫存並離開
                {
                    wBSEventC.EventStatus = 42;
                    wBSEventM.ModifyDT = DateTime.Now;
                    wBSEventM.ModifyUserID = closeCase.UserID;
                    db.SaveChanges();

                    ret[0] = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-處理資訊暫存成功,單號:" + caseNum + "。";

                    ret[0] += "注意事項:「實際更換項目、處理說明、維修附件上傳、備機更換資訊」等暫存資訊不會於子單詳情中顯示,需待案件狀態>=43.已交付待回覆後,才可顯示。";

                    return ret;
                }

                if (closeCase.IsEnd == "true")
                {                    
                    //按照case的最後修改時間小到大排序,保證最後一次借出或歸還順序是否正確(本單與被刪除子單不算在內)
                    var qryC = db.WBSEventC.Where(x => x.MomCaseID == wBSEventC.MomCaseID && x.Num != wBSEventC.Num && x.EventStatus != 0).OrderBy(x => x.ModifyDateTime);
                    foreach (WBSEventC C in qryC)
                    {
                        if (C.EventStatus < 50)
                            canClosed = false;          //某子單未解決不可結案
                        if (C.RetrieveSpareMach == "T") //先判斷此case是否取回備機,若有,表示之前的全取回
                            SpareMach = false;
                        if (C.ReturnUserMach == "T")    //先判斷此case是否歸還客戶機器,若有,表示之前的全歸還
                            UserMach = false;
                        if (C.ProvideSpareMach == "T") //再判斷此case是否借出備機
                        {
                            SpareMachCaseNum = eventNumM + "-" + ((C.ChildCaseID < 10) ? ("0" + C.ChildCaseID.ToString()) : C.ChildCaseID.ToString());
                            SpareMach = true;
                        }
                        if (C.RetrieveUserMach == "T")  //再判斷此case是否取回客戶機器
                        {
                            UserMachCaseNum = eventNumM + "-" + ((C.ChildCaseID < 10) ? ("0" + C.ChildCaseID.ToString()) : C.ChildCaseID.ToString());
                            UserMach = true;
                        }
                    }

                    if (wBSEventC.RetrieveSpareMach == "T") //先判斷本case是否取回備機,若有,表示之前的全取回
                        SpareMach = false;
                    if (wBSEventC.ReturnUserMach == "T")    //先判斷本case是否歸還客戶機器,若有,表示之前的全歸還
                        UserMach = false;
                    if (wBSEventC.ProvideSpareMach == "T")  //再判斷本次是否借出備機
                        SpareMach = true;
                    if (wBSEventC.RetrieveUserMach == "T")  //再判斷本次是否取回客戶機器
                        UserMach = true;

                    if (SpareMach == true)
                    {
                        wBSEventM.SpareMach = "L";
                        canClosed = false;
                    }
                    else
                    {
                        wBSEventM.SpareMach = "R";
                    }
                    if (UserMach == true)
                    {
                        wBSEventM.UserMach = "L";
                        canClosed = false;
                    }
                    else
                    {
                        wBSEventM.UserMach = "R";
                    }
                    
                    if (canClosed)
                    {
                        wBSEventC.EventStatus = 50;
                        wBSEventM.CaseMStatus = "9";
                        wBSEventM.CloseDT = DateTime.Now;
                        wBSEventM.ModifyDT = DateTime.Now;
                        wBSEventM.ModifyUserID = closeCase.UserID;
                        db.SaveChanges();
                        //ret[0] = "案件" + caseNum + "已解決,所有子單均解決,完成結案。";
                        ret[0] = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-所有子單均解決,完成結案,單號:" + caseNum + "。";
                    }
                    else
                    {
                        wBSEventC.EventStatus = 42;
                        //wBSEventM.CaseMStatus = "6";
                        wBSEventM.ModifyDT = DateTime.Now;
                        wBSEventM.ModifyUserID = closeCase.UserID;
                        db.SaveChanges();
                        //ret[0] = "案件" + caseNum + "已解決,有";

                        ret[0] = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-無法結案,處理資訊暫存成功,單號:" + caseNum + "。";

                        //if (SpareMach == true)
                        //    ret[0] += "未取回備機,";
                        //if (UserMach == true)
                        //    ret[0] += "未歸還客戶機器,";
                        //ret[0] += "子單未解決,無法結案。";

                        if (SpareMach == true)
                            ret[0] += "注意事項:單號:" + SpareMachCaseNum + ",已勾選「提供備機」但後續子任務未勾選「拿回備機」。";
                        if (UserMach == true)
                            ret[0] += "注意事項:單號:" + UserMachCaseNum + ",已勾選「拿回客戶機器」但後續子任務未勾選「歸還客戶機器」。";
                    }
                }
                else
                {
                    
                    if (wBSEventC.RetrieveSpareMach == "T") //先判斷本case是否取回備機,若有,表示之前的全取回
                        wBSEventM.SpareMach = "R"; ;
                    if (wBSEventC.ReturnUserMach == "T")    //先判斷本case是否歸還客戶機器,若有,表示之前的全歸還
                        wBSEventM.UserMach = "R"; ;
                    if (wBSEventC.ProvideSpareMach == "T")  //再判斷本次是否借出備機
                        wBSEventM.SpareMach = "L";
                    if (wBSEventC.RetrieveUserMach == "T")  //再判斷本次是否取回客戶機器
                        wBSEventM.UserMach = "L";
                    wBSEventC.EventStatus = 49;
                    //wBSEventM.CaseMStatus = "6";
                    db.SaveChanges();
                    ret[0] = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-已解決未派工,單號:" + caseNum + "。";
                    //ret[0] = "案件" + caseNum + "已解決。";
                }
                ret[1] = caseNum;
            }
            catch
            {
                ret[0] = "false";
            }
            return ret;
        }
        public ActionResult ServiceDataInput2(string cid, string isEnd, string materialJson, string note, string provideSpareMach, string retrieveSpareMach, string retrieveUserMach, string returnUserMach, IEnumerable<HttpPostedFileBase> files)
        {
            if (!User.Identity.IsAuthenticated)
                return RedirectToAction("Logon2", "Mobile");

            // 先取得該使用者的 FormsIdentity
            FormsIdentity id = (FormsIdentity)User.Identity;
            // 再取出使用者的 FormsAuthenticationTicket
            FormsAuthenticationTicket ticket = id.Ticket;

            string[] ret;
            String msg = "結案失敗。";
            CloseCaseViewModel closeCase = new CloseCaseViewModel();
            closeCase.Cid = cid;
            closeCase.IsEnd = isEnd;
            closeCase.MaterialJson = materialJson;
            closeCase.ServiceReport = note;
            //closeCase.MachMemo = machMemo;
            closeCase.ProvideSpareMach = provideSpareMach;
            closeCase.RetrieveSpareMach = retrieveSpareMach;
            closeCase.RetrieveUserMach = retrieveUserMach;
            closeCase.ReturnUserMach = returnUserMach;
            closeCase.UserID = ticket.Name;

            ret = wBSEventRepository.CloseCCase(closeCase);
            if (ret[0] != "false") {
                if (files != null) {
                    Save(files, ret[1], cid, 4);
                }
                msg = ret[0];

                if (isEnd == "true") //結案
                {
                    return RedirectToAction("EventList2", "Mobile", new { data = msg, type = "1" });
                }
                else                //轉派
                {
                    return RedirectToAction("FollowUpEvent", "Mobile", new { data = msg, mid = ret[2], cid = cid });
                }
            }
            return RedirectToAction("EventList2", "Mobile", new { data = msg, type = "1" });
        }