//已解決案件修改手機(20160317-增加到達時間及離開時間的修改)
        public String[] EditClosedCase(EditCloseCaseViewModel editCase)
        {
            String[] ret = new String[3];
            ret[0] = "false";   //回饋訊息
            ret[1] = "";        //案號
            ret[2] = "";        //mid
            try
            {
                int tempCid = Int32.Parse(editCase.Cid);

                WBSEventC wBSEventC = db.WBSEventC.Find(tempCid);
                WBSEventM wBSEventM = db.WBSEventM.Find(wBSEventC.MomCaseID);
                String caseNum = "";
                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();
                caseNum += "-" + ((wBSEventC.ChildCaseID < 10) ? ("0" + wBSEventC.ChildCaseID.ToString()) : wBSEventC.ChildCaseID.ToString());

                if (wBSEventC.ServiceArriveDT != editCase.ArriveDate || wBSEventC.ServiceLeaveDT != editCase.LeaveDate)
                    wBSEventC.AbnormalModifySW = "Y";
                wBSEventC.ServiceArriveDT = editCase.ArriveDate;
                wBSEventC.ServiceLeaveDT = editCase.LeaveDate;

                wBSEventC.ModifyDateTime = DateTime.Now;
                wBSEventC.ModifyUserID = editCase.UserID;
                wBSEventC.ServiceModifyDT = DateTime.Now;
                wBSEventC.ServiceModifyUserID = editCase.UserID;

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

                //20150825新增-Hsiuyang-SugProduct實際更換削除部分
                JArray SugProduct = JsonConvert.DeserializeObject<JArray>(editCase.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);
                }

                ////20160201新增-Hsiuyang-刪除已上傳服務報告附件
                //JArray DeleteFile = JsonConvert.DeserializeObject<JArray>(editCase.DeleteFileJson);
                //for (int i = 0; i < DeleteFile.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);
                //}

                //20160201新增-Hsiuyang-刪除已設定實際/銷除零件列表
                JArray DeleteUsedProduct = JsonConvert.DeserializeObject<JArray>(editCase.DeleteUsedProductJson);
                for (int i = 0; i < DeleteUsedProduct.Count; i++) {
                    int t = Int32.Parse(DeleteUsedProduct[i]["ID"].ToString());
                    SugProduct sugProduct = db.SugProduct.Find(t);
                    db.SugProduct.Remove(sugProduct);
                }

                db.SaveChanges();
                ret[0] = wBSEventM.Customer1.ShortName + "-" + wBSEventC.ProductClass1.Note + "-修改成功,單號:" + caseNum + "。";
                //ret[0] = "修改成功";
                ret[1] = caseNum;
            }
            catch
            {
                ret[0] = "false";
            }
            return ret;
        }
        //更新叫修單
        public String[] Update(WBSEventViewModel wBSEventViewModel)
        {
            String[] ret = new String[3];
            ret[0] = "false";
            ret[1] = wBSEventViewModel.C_ID.ToString();
            ret[2] = "";    //area

            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 wBSEventM = db.WBSEventM.Find(wBSEventViewModel.M_ID);
                
                //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.RB_CasseDate = wBSEventViewModel.RB_CasseDate;
                //try
                //{
                //    wBSEventM.RB_CasseNumber = Int32.Parse(wBSEventViewModel.RB_CasseNumber);
                //}
                //catch
                //{
                //    wBSEventM.RB_CasseNumber = 0;
                //}
                wBSEventM.ProductID = wBSEventViewModel.ProductID;
                wBSEventM.CustomerRef = wBSEventViewModel.CustomerRef;
                wBSEventM.CustomerUserID = wBSEventViewModel.CustomerUserID;
                wBSEventM.CustomerUserName = wBSEventViewModel.CustomerUserName;
                wBSEventM.CustomerUserPhone1 = wBSEventViewModel.CustomerUserPhone1;
                wBSEventM.CustomerUserPhone2 = wBSEventViewModel.CustomerUserPhone2;
                wBSEventM.CustomerCaseNum = wBSEventViewModel.CustomerCaseNum;
                //20150811新增-Hsiuyang
                if (wBSEventViewModel.ChangeAddr != null)
                    if (wBSEventViewModel.ChangeAddr.Trim().Length > 0)
                        wBSEventM.CustomerAddr = wBSEventViewModel.ChangeAddr;
                wBSEventM.ModifyDT = DateTime.Now;
                wBSEventM.ModifyUserID = wBSEventViewModel.ModifyUserID;


                WBSEventC wBSEventC = db.WBSEventC.Find(wBSEventViewModel.C_ID);
                //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.ModifyUserID;
                //20150811新增-Hsiuyang
                wBSEventC.SugProvideSpareMach = wBSEventViewModel.SugProvideSpareMach;
                wBSEventC.SugRetrieveSpareMach = wBSEventViewModel.SugRetrieveSpareMach;
                wBSEventC.SugRetrieveUserMach = wBSEventViewModel.SugRetrieveUserMach;
                wBSEventC.SugReturnUserMach = wBSEventViewModel.SugReturnUserMach;
                wBSEventC.SugMachMemo = wBSEventViewModel.SugMachMemo;   //建議備機說明
                //20150817新增-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;  //當結案時解決方式即為服務報告
                }
                else
                {
                    //wBSEventM.CaseMStatus = "1";    //1為開立新問題
                    //wBSEventC.EventStatus = 11; //11為問題已開立
                    wBSEventC.QuestionFinishSW = null;
                    wBSEventC.QuestionFinishDT = null;
                    wBSEventC.QuestionFinishSysDT = null;
                    wBSEventC.ServiceReport = null;  //當結案時解決方式即為服務報告
                }
                wBSEventC.AssignDescription = wBSEventViewModel.AssignDescription;  //解決方式(派單)描述

                //刪除原子單建議零件
                var qryD = db.SugProduct.Where(x => x.CID == wBSEventC.Num && (x.Class == 1 || x.Class == 3));
                foreach (SugProduct d in qryD)
                {
                    db.SugProduct.Remove(d);
                }
                //刪除原子單解決方式知識庫附件
                var qryDK = db.Files.Where(x => x.CID == wBSEventC.Num && x.Class == 3);
                foreach (Files d in qryDK)
                {
                    db.Files.Remove(d);
                }
                //刪除原子單執行預先保養單
                var qryM = db.EventRelation.Where(x => x.CID == wBSEventC.Num);
                foreach (EventRelation d in qryM)
                {
                    db.EventRelation.Remove(d);
                }
                //刪除原子單建議參考網頁
                var qryDH = db.Files.Where(x => x.CID == wBSEventC.Num && x.Class == 9);
                foreach (Files d in qryDH)
                {
                    db.Files.Remove(d);
                }
                
                //20150817新增-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());                    
                    if (SugProduct[i]["ProductNum"].ToString() == "")
                        sugProduct.ProductName = SugProduct[i]["ProductName"].ToString();                    
                    else
                        sugProduct.ProductNum = Int32.Parse(SugProduct[i]["ProductNum"].ToString());
                    

                    db.SugProduct.Add(sugProduct);
                }
                //20150817新增-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]["KnowledgeID"].ToString());
                    db.Files.Add(knowledgefile);
                }
                //20150817新增-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);
                }

                //20151113新增-Hsiuyang-Hyperlink建議參考網頁(CLASS=9)
                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);
                }

                //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[] 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 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;
        }