public ActionResult NextFlow(AssignModel assign)
        {
            var ur = _userRepo.Find(u => u.UserName == this.User.Identity.Name).FirstOrDefault();

            /* 工程師的流程控管 */
            if (assign.Cls == "工務/營建工程師")
            {
                /* 如點選有費用、卻無輸入費用明細 */
                var isCharged = _context.RepairDtls.Where(d => d.DocId == assign.DocId).FirstOrDefault().IsCharged;
                if (isCharged == "Y")
                {
                    var CheckRepairCost = _context.RepairCosts.Where(c => c.DocId == assign.DocId).FirstOrDefault();
                    if (CheckRepairCost == null)
                    {
                        string msg = "尚未輸入費用明細!!";
                        return(BadRequest(msg));
                    }
                }
                var repairDtl = _context.RepairDtls.Where(d => d.DocId == assign.DocId).FirstOrDefault();
                /* 3 = 已完成,4 = 報廢 */
                if (repairDtl.DealState == 3 || repairDtl.DealState == 4)
                {
                    if (repairDtl.EndDate == null)
                    {
                        string msg = "報廢及已完成,需輸入完工日!!";
                        return(BadRequest(msg));
                    }
                }
                /* 工程師做結案 */
                if (assign.FlowCls == "結案")
                {
                    if (_context.RepairEmps.Where(emp => emp.DocId == assign.DocId).Count() <= 0)
                    {
                        string msg = "沒有維修工程師紀錄!!";
                        return(BadRequest(msg));
                    }
                    else if (_context.RepairDtls.Find(assign.DocId).EndDate == null)
                    {
                        string msg = "沒有完工日!!";
                        return(BadRequest(msg));
                    }
                    else if (_context.RepairDtls.Find(assign.DocId).DealState == 0)
                    {
                        string msg = "處理狀態不可空值!!";
                        return(BadRequest(msg));
                    }
                    if (_context.RepairDtls.Find(assign.DocId).FailFactor == 0)
                    {
                        string msg = "故障原因不可空白!!";
                        return(BadRequest(msg));
                    }
                    if (string.IsNullOrEmpty(_context.RepairDtls.Find(assign.DocId).InOut))
                    {
                        string msg = "維修方式不可空白!!";
                        return(BadRequest(msg));
                    }
                    if (_context.RepairDtls.Find(assign.DocId).DealState == 1 || _context.RepairDtls.Find(assign.DocId).DealState == 2)
                    {
                        string msg = "處理狀態不可為處理中或未處理!!";
                        return(BadRequest(msg));
                    }
                }
            }

            if (assign.FlowCls == "結案" || assign.FlowCls == "廢除")
            {
                assign.FlowUid = ur.Id;
            }
            if (ModelState.IsValid)
            {
                RepairFlowModel rf = _context.RepairFlows.Where(f => f.DocId == assign.DocId && f.Status == "?").FirstOrDefault();
                if (assign.FlowCls == "驗收人")
                {
                    if (_context.RepairEmps.Where(emp => emp.DocId == assign.DocId).Count() <= 0)
                    {
                        //throw new Exception("沒有維修工程師紀錄!!");
                        string msg = "沒有維修工程師紀錄!!";
                        return(BadRequest(msg));
                    }
                    else if (_context.RepairDtls.Find(assign.DocId).EndDate == null)
                    {
                        //throw new Exception("沒有完工日!!");
                        string msg = "沒有完工日!!";
                        return(BadRequest(msg));
                    }
                    else if (_context.RepairDtls.Find(assign.DocId).DealState == 0)
                    {
                        //throw new Exception("處理狀態不可空值!!");
                        string msg = "處理狀態不可空值!!";
                        return(BadRequest(msg));
                    }
                    if (_context.RepairDtls.Find(assign.DocId).FailFactor == 0)
                    {
                        //throw new Exception("故障原因不可空白!!");
                        string msg = "故障原因不可空白!!";
                        return(BadRequest(msg));
                    }
                    if (string.IsNullOrEmpty(_context.RepairDtls.Find(assign.DocId).InOut))
                    {
                        //throw new Exception("維修方式不可空白!!");
                        string msg = "維修方式不可空白!!";
                        return(BadRequest(msg));
                    }
                    if (_context.RepairDtls.Find(assign.DocId).DealState == 1 || _context.RepairDtls.Find(assign.DocId).DealState == 2)
                    {
                        //throw new Exception("維修方式不可空白!!");
                        string msg = "處理狀態不可為處理中或未處理!!";
                        return(BadRequest(msg));
                    }
                }
                if (assign.FlowCls == "結案")
                {
                    RepairDtlModel rd = _context.RepairDtls.Find(assign.DocId);
                    rd.CloseDate             = DateTime.Now;
                    rf.Opinions              = "[" + assign.AssignCls + "]" + Environment.NewLine + assign.AssignOpn;
                    rf.Status                = "2";
                    rf.UserId                = ur.Id;
                    rf.UserName              = _context.AppUsers.Find(ur.Id).FullName;
                    rf.Rtt                   = DateTime.Now;
                    rf.Rtp                   = ur.Id;
                    _context.Entry(rf).State = EntityState.Modified;
                    _context.Entry(rd).State = EntityState.Modified;

                    //If "結案", delete 1 dealing doc to the engineer.
                    //var repairDoc = _context.Repairs.Find(assign.DocId);
                    //var eng = _context.EngDealingDocs.Find(repairDoc.EngId);
                    //eng.DealingDocs = eng.DealingDocs - 1;
                    //_context.Entry(eng).State = EntityState.Modified;
                    _context.SaveChanges();

                    //Send Mail
                    //To all users in this repair's flow.
                    //try
                    //{
                    //    Tmail mail = new Tmail();
                    //    string body = "";
                    //    string sto = "";
                    //    AppUserModel u;
                    //    RepairModel repair = _context.Repairs.Find(assign.DocId);
                    //    if (repair.Building != null)
                    //    {
                    //        repair.BuildingName = _context.Buildings.Where(b => b.BuildingId == Convert.ToInt32(repair.Building)).FirstOrDefault().BuildingName;
                    //        repair.FloorName = _context.Floors.Where(f => f.BuildingId == Convert.ToInt32(repair.Building) && f.FloorId == repair.Floor).FirstOrDefault().FloorName;
                    //        repair.AreaName = _context.Places.Where(p => p.BuildingId == Convert.ToInt32(repair.Building) && p.FloorId == repair.Floor && p.PlaceId == repair.Area).FirstOrDefault().PlaceName;
                    //    }
                    //    else
                    //    {
                    //        repair.BuildingName = "(無資料)";
                    //        repair.FloorName = "";
                    //        repair.AreaName = "";
                    //    }
                    //    mail.from = new System.Net.Mail.MailAddress(ur.Email); //u.Email
                    //    /* If is charged, send mail to all flow users. */
                    //    if (rd.IsCharged == "Y")
                    //    {
                    //        _context.RepairFlows.Where(f => f.DocId == assign.DocId)
                    //            .ToList()
                    //            .ForEach(f =>
                    //            {
                    //                u = _context.AppUsers.Find(f.UserId);
                    //                sto += u.Email + ",";
                    //            });
                    //    }
                    //    else
                    //    {
                    //        _context.RepairFlows.Where(f => f.DocId == assign.DocId).Where(f => f.Cls.Contains("工程師") == false)
                    //            .ToList()
                    //            .ForEach(f =>
                    //            {
                    //                u = _context.AppUsers.Find(f.UserId);
                    //                sto += u.Email + ",";
                    //            });
                    //    }
                    //    var temp = _context.RepairFlows.Where(f => f.DocId == assign.DocId).Where(f => f.Cls.Contains("工程師") == false)
                    //            .ToList();
                    //    mail.sto = sto.TrimEnd(new char[] { ',' });

                    //    mail.message.Subject = "工務智能請修系統[請修案-結案通知]:設備名稱: " + repair.AssetName;
                    //    body += "<p>表單編號:" + repair.DocId + "</p>";
                    //    body += "<p>申請日期:" + repair.ApplyDate.ToString("yyyy/MM/dd") + "</p>";
                    //    body += "<p>申請人:" + repair.UserName + "</p>";
                    //    body += "<p>財產編號:" + repair.AssetNo + "</p>";
                    //    body += "<p>設備名稱:" + repair.AssetName + "</p>";
                    //    body += "<p>請修地點:" + repair.PlaceLoc + " " + repair.BuildingName + " " + repair.FloorName + " " + repair.AreaName + "</p>";
                    //    //body += "<p>放置地點:" + repair.PlaceLoc + "</p>";
                    //    body += "<p>故障描述:" + repair.TroubleDes + "</p>";
                    //    body += "<p>處理描述:" + rd.DealDes + "</p>";
                    //    body += "<p><a href='http://dms.cch.org.tw/EDIS/Account/Login" + "?DocId=" + repair.DocId + "&dealType=Views'" + ">檢視案件</a></p>";
                    //    body += "<br/>";
                    //    body += "<p>使用IE瀏覽器注意事項:</p>";
                    //    body += "<p>「工具」→「相容性檢視設定」→移除cch.org.tw</p>";
                    //    body += "<br/>";
                    //    body += "<h3>此封信件為系統通知郵件,請勿回覆。</h3>";
                    //    body += "<br/>";
                    //    body += "<h3 style='color:red'>如有任何疑問請聯絡工務部,分機3033或7033。<h3>";
                    //    mail.message.Body = body;
                    //    mail.message.IsBodyHtml = true;
                    //    mail.SendMail();
                    //}
                    //catch (Exception ex)
                    //{
                    //    throw new Exception(ex.Message);
                    //}
                }
                else if (assign.FlowCls == "廢除")
                {
                    rf.Opinions = "[廢除]" + Environment.NewLine + assign.AssignOpn;
                    rf.Status   = "3";
                    rf.Rtt      = DateTime.Now;
                    rf.Rtp      = ur.Id;
                    _context.Entry(rf).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                else
                {
                    //轉送下一關卡
                    rf.Opinions = "[" + assign.AssignCls + "]" + Environment.NewLine + assign.AssignOpn;
                    rf.Status   = "1";
                    rf.Rtt      = DateTime.Now;
                    rf.Rtp      = ur.Id;
                    _context.Entry(rf).State = EntityState.Modified;
                    _context.SaveChanges();
                    //
                    RepairFlowModel flow = new RepairFlowModel();
                    flow.DocId    = assign.DocId;
                    flow.StepId   = rf.StepId + 1;
                    flow.UserId   = assign.FlowUid.Value;
                    flow.UserName = _context.AppUsers.Find(assign.FlowUid.Value).FullName;
                    flow.Status   = "?";
                    flow.Cls      = assign.FlowCls;
                    flow.Rtt      = DateTime.Now;
                    _context.RepairFlows.Add(flow);
                    _context.SaveChanges();

                    //Send Mail
                    //To the next flow user.
                    try
                    {
                        Tmail        mail = new Tmail();
                        string       body = "";
                        AppUserModel u;
                        RepairModel  repair = _context.Repairs.Find(assign.DocId);
                        if (repair.Building != null)
                        {
                            repair.BuildingName = _context.Buildings.Where(b => b.BuildingId == Convert.ToInt32(repair.Building)).FirstOrDefault().BuildingName;
                            repair.FloorName    = _context.Floors.Where(f => f.BuildingId == Convert.ToInt32(repair.Building) && f.FloorId == repair.Floor).FirstOrDefault().FloorName;
                            repair.AreaName     = _context.Places.Where(p => p.BuildingId == Convert.ToInt32(repair.Building) && p.FloorId == repair.Floor && p.PlaceId == repair.Area).FirstOrDefault().PlaceName;
                        }
                        else
                        {
                            repair.BuildingName = "(無資料)";
                            repair.FloorName    = "";
                            repair.AreaName     = "";
                        }
                        mail.from = new System.Net.Mail.MailAddress(ur.Email); //ur.Email
                        u         = _context.AppUsers.Find(flow.UserId);
                        mail.to   = new System.Net.Mail.MailAddress(u.Email);  //u.Email
                                                                               //mail.cc = new System.Net.Mail.MailAddress("*****@*****.**");
                        mail.message.Subject = "工務智能請修系統[請修案]:設備名稱: " + repair.AssetName;
                        body += "<p>表單編號:" + repair.DocId + "</p>";
                        body += "<p>申請日期:" + repair.ApplyDate.ToString("yyyy/MM/dd") + "</p>";
                        body += "<p>申請人:" + repair.UserName + "</p>";
                        body += "<p>財產編號:" + repair.AssetNo + "</p>";
                        body += "<p>設備名稱:" + repair.AssetName + "</p>";
                        body += "<p>故障描述:" + repair.TroubleDes + "</p>";
                        body += "<p>請修地點:" + repair.PlaceLoc + " " + repair.BuildingName + " " + repair.FloorName + " " + repair.AreaName + "</p>";
                        //body += "<p>放置地點:" + repair.PlaceLoc + "</p>";
                        body += "<p><a href='http://dms.cch.org.tw/EDIS/Account/Login" + "?docId=" + repair.DocId + "&dealType=Edit'" + ">處理案件</a></p>";
                        body += "<br/>";
                        body += "<p>使用IE瀏覽器注意事項:</p>";
                        body += "<p>「工具」→「相容性檢視設定」→移除cch.org.tw</p>";
                        body += "<br/>";
                        body += "<h3>此封信件為系統通知郵件,請勿回覆。</h3>";
                        body += "<br/>";
                        body += "<h3 style='color:red'>如有任何疑問請聯絡工務部,分機3033或7033。<h3>";
                        mail.message.Body       = body;
                        mail.message.IsBodyHtml = true;
                        /* If next flow is not engineer, send mail. */
                        if (flow.Cls.Contains("工程師") == false)
                        {
                            mail.SendMail();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }

                return(new JsonResult(assign)
                {
                    Value = new { success = true, error = "" }
                });
            }
            else
            {
                string msg = "";
                foreach (var error in ViewData.ModelState.Values.SelectMany(modelState => modelState.Errors))
                {
                    msg += error.ErrorMessage + Environment.NewLine;
                }
                throw new Exception(msg);
            }
        }
        public IActionResult Create(KeepModel keep)
        {
            AppUserModel ur = _context.AppUsers.Where(u => u.UserName == this.User.Identity.Name).FirstOrDefault();

            //if (string.IsNullOrEmpty(keep.AssetNo))
            //{
            //    throw new Exception("財產編號不可空白!!");
            //}
            string msg = "";

            try
            {
                if (ModelState.IsValid)
                {
                    //更新申請人的Email
                    if (string.IsNullOrEmpty(keep.Email))
                    {
                        throw new Exception("電子信箱不可空白!!");
                    }
                    AppUserModel a = _context.AppUsers.Find(keep.UserId);
                    a.Email = keep.Email;
                    _context.Entry(a).State = EntityState.Modified;
                    _context.SaveChanges();
                    //
                    AssetKeepModel kp = _context.AssetKeeps.Find(keep.DeviceNo);
                    AssetModel     at = _context.Assets.Find(keep.DeviceNo);
                    //
                    keep.AssetNo   = _context.Assets.Find(keep.DeviceNo).AssetNo;
                    keep.AssetName = _context.Assets.Find(keep.DeviceNo).Cname;
                    keep.EngId     = kp.KeepEngId;
                    //keep.AccDpt = at.AccDpt;
                    keep.SentDate = DateTime.Now;
                    keep.Cycle    = "手動出單";
                    keep.Src      = "M";
                    _context.Entry(keep).State = EntityState.Modified;

                    //
                    KeepDtlModel dl = new KeepDtlModel();
                    //var notInExceptDevice = _context.ExceptDevice.Find(keep.AssetNo);

                    /* If can find data in ExceptDevice table, the device is "not" 統包.
                     * It means if value is "Y", the device is 統包
                     */
                    //if (notInExceptDevice == null)
                    //{
                    //    dl.NotInExceptDevice = "Y";
                    //}
                    //else
                    //{
                    //    dl.NotInExceptDevice = "N";
                    //}
                    dl.DocId = keep.DocId;
                    switch (kp == null ? "自行" : kp.InOut)
                    {
                    case "自行":
                        dl.InOut = "0";
                        break;

                    case "委外":
                        dl.InOut = "1";
                        break;

                    case "租賃":
                        dl.InOut = "2";
                        break;

                    case "保固":
                        dl.InOut = "3";
                        break;

                    default:
                        dl.InOut = "0";
                        break;
                    }
                    _context.KeepDtls.Add(dl);
                    _context.SaveChanges();
                    //
                    KeepFlowModel kf = new KeepFlowModel();
                    kf.DocId  = keep.DocId;
                    kf.StepId = 1;
                    kf.UserId = ur.Id;
                    kf.Status = "1";
                    //rf.Role = Roles.GetRolesForUser().FirstOrDefault();
                    kf.Rtp = ur.Id;
                    kf.Rdt = null;
                    kf.Rtt = DateTime.Now;
                    kf.Cls = "申請者";
                    _context.KeepFlows.Add(kf);
                    //
                    kf        = new KeepFlowModel();
                    kf.DocId  = keep.DocId;
                    kf.StepId = 2;
                    kf.UserId = kp == null ? ur.Id : kp.KeepEngId;
                    kf.Status = "?";
                    AppUserModel u = _context.AppUsers.Find(kf.UserId);
                    if (u == null)
                    {
                        throw new Exception("無工程師資料!!");
                    }
                    //rf.Role = Roles.GetRolesForUser(u.UserName).FirstOrDefault();
                    kf.Rtp = null;
                    kf.Rdt = null;
                    kf.Rtt = DateTime.Now;
                    kf.Cls = "設備工程師";
                    _context.KeepFlows.Add(kf);
                    _context.SaveChanges();
                    //send mail
                    Tmail  mail = new Tmail();
                    string body = "";
                    u         = _context.AppUsers.Find(ur.Id);
                    mail.from = new System.Net.Mail.MailAddress(u.Email); //u.Email
                    //u = _context.AppUsers.Find(kp.KeepEngId);
                    mail.to = new System.Net.Mail.MailAddress(u.Email);   //u.Email
                    mail.message.Subject = "醫工工務智能保修系統[醫工保養案]:設備名稱: " + keep.AssetName;
                    body += "<p>表單編號:" + keep.DocId + "</p>";
                    body += "<p>送單日期:" + keep.SentDate.Value.ToString("yyyy/MM/dd") + "</p>";
                    body += "<p>申請人:" + keep.UserName + "</p>";
                    body += "<p>財產編號:" + keep.AssetNo + "</p>";
                    body += "<p>設備名稱:" + keep.AssetName + "</p>";
                    body += "<p>放置地點:" + keep.PlaceLoc + "</p>";
                    body += "<p><a href='http://dms.cch.org.tw/EDIS/Account/Login'" + "?docId=" + keep.DocId + "&dealType=KeepEdit" + ">處理案件</a></p>";
                    body += "<br/>";
                    body += "<h3>此封信件為系統通知郵件,請勿回覆。</h3>";
                    body += "<br/>";
                    body += "<h3 style='color:red'>如有任何疑問請聯絡工務部,分機3033或7033。<h3>";
                    mail.message.Body       = body;
                    mail.message.IsBodyHtml = true;
                    //mail.SendMail();

                    return(Ok(keep));
                }
                else
                {
                    msg = "";
                    foreach (var error in ViewData.ModelState.Values.SelectMany(modelState => modelState.Errors))
                    {
                        msg += error.ErrorMessage + Environment.NewLine;
                    }
                    throw new Exception(msg);
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return(BadRequest(msg));
        }
        public IActionResult NextFlow(AssignModel assign)
        {
            var ur = _userRepo.Find(u => u.UserName == this.User.Identity.Name).FirstOrDefault();

            /* 工程師的流程控管 */
            if (assign.Cls == "工務/營建工程師")
            {
                /* 如點選有費用、卻無輸入費用明細 */
                var isCharged = _context.KeepDtls.Where(d => d.DocId == assign.DocId).FirstOrDefault().IsCharged;
                if (isCharged == "Y")
                {
                    var CheckKeepCost = _context.KeepCosts.Where(c => c.DocId == assign.DocId).FirstOrDefault();
                    if (CheckKeepCost == null)
                    {
                        string msg = "尚未輸入費用明細!!";
                        return(BadRequest(msg));
                    }
                }

                /* 工程師做結案 */
                if (assign.FlowCls == "結案")
                {
                    if (_context.KeepEmps.Where(emp => emp.DocId == assign.DocId).Count() <= 0)
                    {
                        string msg = "沒有維修工程師紀錄!!";
                        return(BadRequest(msg));
                    }
                    else if (_context.KeepDtls.Find(assign.DocId).EndDate == null)
                    {
                        string msg = "沒有完工日!!";
                        return(BadRequest(msg));
                    }
                    else if (_context.KeepDtls.Find(assign.DocId).Result == null)
                    {
                        string msg = "保養結果不可空白!!";
                        return(BadRequest(msg));
                    }
                    if (string.IsNullOrEmpty(_context.KeepDtls.Find(assign.DocId).InOut))
                    {
                        string msg = "保養方式不可空白!!";
                        return(BadRequest(msg));
                    }
                }
            }

            if (assign.FlowCls == "結案" || assign.FlowCls == "廢除")
            {
                assign.FlowUid = ur.Id;
            }
            if (ModelState.IsValid)
            {
                KeepFlowModel kf = _context.KeepFlows.Where(f => f.DocId == assign.DocId && f.Status == "?").FirstOrDefault();
                if (assign.FlowCls == "驗收人")
                {
                    if (_context.KeepEmps.Where(emp => emp.DocId == assign.DocId).Count() <= 0)
                    {
                        throw new Exception("沒有維修工程師紀錄!!");
                    }
                    else if (_context.KeepDtls.Find(assign.DocId).EndDate == null)
                    {
                        throw new Exception("沒有完工日!!");
                    }
                    if (_context.KeepDtls.Find(assign.DocId).Result == null)
                    {
                        throw new Exception("保養結果不可空白!!");
                    }
                    if (string.IsNullOrEmpty(_context.KeepDtls.Find(assign.DocId).InOut))
                    {
                        throw new Exception("保養方式不可空白!!");
                    }
                }
                if (assign.FlowCls == "結案")
                {
                    KeepDtlModel kd = _context.KeepDtls.Find(assign.DocId);
                    kd.CloseDate             = DateTime.Now;
                    kf.Opinions              = "[" + assign.AssignCls + "]" + Environment.NewLine + assign.AssignOpn;
                    kf.Status                = "2";
                    kf.UserId                = ur.Id;
                    kf.UserName              = _context.AppUsers.Find(ur.Id).FullName;
                    kf.Rtt                   = DateTime.Now;
                    kf.Rtp                   = ur.Id;
                    _context.Entry(kf).State = EntityState.Modified;
                    _context.Entry(kd).State = EntityState.Modified;

                    _context.SaveChanges();

                    //Send Mail
                    //To all users in this keep's flow.
                    Tmail        mail = new Tmail();
                    string       body = "";
                    string       sto  = "";
                    AppUserModel u;
                    KeepModel    keep = _context.Keeps.Find(assign.DocId);
                    mail.from = new System.Net.Mail.MailAddress(ur.Email); //u.Email
                    _context.KeepFlows.Where(f => f.DocId == assign.DocId)
                    .ToList()
                    .ForEach(f =>
                    {
                        u    = _context.AppUsers.Find(f.UserId);
                        sto += u.Email + ",";
                    });
                    mail.sto = sto.TrimEnd(new char[] { ',' });

                    mail.message.Subject = "工務智能保修系統[保養案-結案通知]:設備名稱: " + keep.AssetName;
                    body += "<p>表單編號:" + keep.DocId + "</p>";
                    body += "<p>送單日期:" + keep.SentDate.Value.ToString("yyyy/MM/dd") + "</p>";
                    body += "<p>申請人:" + keep.UserName + "</p>";
                    body += "<p>財產編號:" + keep.AssetNo + "</p>";
                    body += "<p>設備名稱:" + keep.AssetName + "</p>";
                    body += "<p>放置地點:" + keep.PlaceLoc + "</p>";
                    body += "<p>保養結果:" + kd.Result + "</p>";
                    body += "<p>保養描述:" + kd.Memo + "</p>";
                    body += "<p><a href='http://dms.cch.org.tw/EDIS/Account/Login'" + "?DocId=" + keep.DocId + "&dealType=KeepViews" + ">檢視案件</a></p>";
                    body += "<br/>";
                    body += "<h3>此封信件為系統通知郵件,請勿回覆。</h3>";
                    body += "<br/>";
                    body += "<h3 style='color:red'>如有任何疑問請聯絡工務部,分機3033或7033。<h3>";
                    mail.message.Body       = body;
                    mail.message.IsBodyHtml = true;
                    //mail.SendMail();
                }
                else if (assign.FlowCls == "廢除")
                {
                    kf.Opinions = "[廢除]" + Environment.NewLine + assign.AssignOpn;
                    kf.Status   = "3";
                    kf.Rtt      = DateTime.Now;
                    kf.Rtp      = ur.Id;
                    _context.Entry(kf).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                else
                {
                    //轉送下一關卡
                    kf.Opinions = "[" + assign.AssignCls + "]" + Environment.NewLine + assign.AssignOpn;
                    kf.Status   = "1";
                    kf.Rtt      = DateTime.Now;
                    kf.Rtp      = ur.Id;
                    _context.Entry(kf).State = EntityState.Modified;
                    _context.SaveChanges();
                    //
                    KeepFlowModel flow = new KeepFlowModel();
                    flow.DocId    = assign.DocId;
                    flow.StepId   = kf.StepId + 1;
                    flow.UserId   = assign.FlowUid.Value;
                    flow.UserName = _context.AppUsers.Find(assign.FlowUid.Value).FullName;
                    flow.Status   = "?";
                    flow.Cls      = assign.FlowCls;
                    flow.Rtt      = DateTime.Now;
                    _context.KeepFlows.Add(flow);
                    _context.SaveChanges();

                    //Send Mail
                    //To user and the next flow user.
                    Tmail        mail = new Tmail();
                    string       body = "";
                    AppUserModel u;
                    KeepModel    keep = _context.Keeps.Find(assign.DocId);
                    mail.from = new System.Net.Mail.MailAddress(ur.Email); //ur.Email
                    u         = _context.AppUsers.Find(flow.UserId);
                    mail.to   = new System.Net.Mail.MailAddress(u.Email);  //u.Email
                                                                           //mail.cc = new System.Net.Mail.MailAddress("*****@*****.**");
                    mail.message.Subject = "工務智能保修系統[保養案]:設備名稱: " + keep.AssetName;
                    body += "<p>表單編號:" + keep.DocId + "</p>";
                    body += "<p>送單日期:" + keep.SentDate.Value.ToString("yyyy/MM/dd") + "</p>";
                    body += "<p>申請人:" + keep.UserName + "</p>";
                    body += "<p>財產編號:" + keep.AssetNo + "</p>";
                    body += "<p>設備名稱:" + keep.AssetName + "</p>";
                    body += "<p>放置地點:" + keep.PlaceLoc + "</p>";
                    body += "<p><a href='http://dms.cch.org.tw/EDIS/Account/Login'" + "?docId=" + keep.DocId + "&dealType=KeepEdit" + ">處理案件</a></p>";
                    body += "<br/>";
                    body += "<h3>此封信件為系統通知郵件,請勿回覆。</h3>";
                    body += "<br/>";
                    body += "<h3 style='color:red'>如有任何疑問請聯絡工務部,分機3033或7033。<h3>";
                    mail.message.Body       = body;
                    mail.message.IsBodyHtml = true;
                    //mail.SendMail();
                }

                return(new JsonResult(assign)
                {
                    Value = new { success = true, error = "" }
                });
            }
            else
            {
                string msg = "";
                foreach (var error in ViewData.ModelState.Values.SelectMany(modelState => modelState.Errors))
                {
                    msg += error.ErrorMessage + Environment.NewLine;
                }
                throw new Exception(msg);
            }
        }