//電腦建立二次派工叫修單(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[] 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;
        }
        //建立新叫修單
        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;
        }