Пример #1
0
        public async Task <IActionResult> Create(AttainFile attainFile, IEnumerable <IFormFile> file)
        {
            //目前使用者資料
            var ur = _userRepo.Find(u => u.UserName == this.User.Identity.Name).FirstOrDefault();

            //文件臨時位置的完整路徑
            var filePath = Path.GetTempFileName();

            if (ModelState.IsValid)
            {
                try
                {
                    //AppUser appUser = db.AppUsers.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
                    //接收文件
                    //HttpPostedFileBase file = Request.Files[0];
                    //文件扩展名
                    //string extension = Path.GetExtension(file.FileName);
                    string s = "/Files/OutsideBmed";
#if DEBUG
                    s = "/Files";
#endif
                    switch (attainFile.DocType)
                    {
                    case "OutsideBmed":
                        s += "/OutsideBmed/";
                        break;

                    default:
                        s += "/Sign/";
                        break;
                    }
                    var i = _db.AttainFiles
                            .Where(a => a.DocType == attainFile.DocType)
                            .Where(a => a.DocId == attainFile.DocId).ToList();
                    attainFile.SeqNo = i.Count == 0 ? 1 : i.Select(a => a.SeqNo).Max() + 1;

                    string path = Path.Combine(@"D:\" + s + attainFile.DocId + "_"
                                               + attainFile.SeqNo.ToString() + Path.GetExtension(Request.Form.Files[0].FileName));
                    // Upload files.
                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await Request.Form.Files[0].CopyToAsync(stream);
                    }
                    string filelink = attainFile.DocId + "_"
                                      + attainFile.SeqNo.ToString() + Path.GetExtension(Request.Form.Files[0].FileName);

                    switch (attainFile.DocType)
                    {
                    case "OutsideBmed":
                        attainFile.FileLink = "OutsideBmed/" + filelink;
                        break;

                    default:
                        attainFile.FileLink = "Sign/" + filelink;
                        break;
                    }
                    attainFile.Rtt = DateTime.Now;
                    attainFile.Rtp = ur.Id;
                    _db.AttainFiles.Add(attainFile);
                    _db.SaveChanges();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
            else
            {
                string msg = "";
                foreach (var error in ViewData.ModelState.Values.SelectMany(modelState => modelState.Errors))
                {
                    msg += error.ErrorMessage + Environment.NewLine;
                }
                throw new Exception(msg);
            }

            return(new JsonResult(new
            {
                Data = new { success = true, error = "" }
            }));
        }
        public ActionResult NextFlow(Assign assign)
        {
            var ur = _userRepo.Find(u => u.UserName == this.User.Identity.Name).FirstOrDefault();

            if (assign.FlowCls == "結案" || assign.FlowCls == "廢除")
            {
                assign.FlowUid = ur.Id;
            }
            else
            {
                assign.FlowUid = _context.AppUsers.Where(u => u.FullName == assign.ClsNow).Select(u => u.Id).FirstOrDefault();
            }
            Instrument instrument = _db.Instruments.Find(assign.DocId);

            if (ModelState.IsValid)
            {
                OutsideBmedFlow of = _db.OutsideBmedFlows.Where(f => f.DocId == assign.DocId && f.Status == "?").FirstOrDefault();

                if (assign.FlowCls == "結案")
                {
                    of.Opinion = "[" + assign.AssignCls + "]";
                    if (!string.IsNullOrEmpty(assign.AssignOpn))
                    {
                        of.Opinion += (Environment.NewLine + assign.AssignOpn);
                    }
                    of.Status = "2";
                    of.Rtt    = DateTime.Now;
                    of.Rtp    = assign.FlowUid;
                    of.item1  = assign.item1;
                    of.item2  = assign.item2;
                    of.item3  = assign.item3;
                    of.item4  = assign.item4;
                    of.item5  = assign.item5;
                    of.item6  = assign.item6;
                    of.item7  = assign.item7;

                    _db.Entry(of).State = EntityState.Modified;
                    _db.SaveChanges();
                }
                else if (assign.FlowCls == "廢除")
                {
                    of.Opinion = "[廢除]";
                    if (!string.IsNullOrEmpty(assign.AssignOpn))
                    {
                        of.Opinion += (Environment.NewLine + assign.AssignOpn);
                    }
                    of.Status           = "3";
                    of.Rtt              = DateTime.Now;
                    of.Rtp              = assign.FlowUid;
                    of.item1            = assign.item1;
                    of.item2            = assign.item2;
                    of.item3            = assign.item3;
                    of.item4            = assign.item4;
                    of.item5            = assign.item5;
                    of.item6            = assign.item6;
                    of.item7            = assign.item7;
                    _db.Entry(of).State = EntityState.Modified;
                    _db.SaveChanges();
                }
                else
                {
                    //轉送下一關卡
                    of.Opinion = "[" + assign.AssignCls + "]";
                    if (!string.IsNullOrEmpty(assign.AssignOpn))
                    {
                        of.Opinion += (Environment.NewLine + assign.AssignOpn);
                    }
                    of.Status           = "1";
                    of.Rtt              = DateTime.Now;
                    of.Rtp              = ur.Id;
                    of.item1            = assign.item1;
                    of.item2            = assign.item2;
                    of.item3            = assign.item3;
                    of.item4            = assign.item4;
                    of.item5            = assign.item5;
                    of.item6            = assign.item6;
                    of.item7            = assign.item7;
                    _db.Entry(of).State = EntityState.Modified;
                    _db.SaveChanges();
                    //
                    OutsideBmedFlow flow = new OutsideBmedFlow();
                    flow.DocId    = assign.DocId;
                    flow.StepId   = of.StepId + 1;
                    flow.UserId   = assign.FlowUid.Value;
                    flow.UserName = _db.AppUsers.Find(assign.FlowUid.Value).UserName;
                    flow.Status   = "?";
                    flow.Cls      = assign.FlowCls;
                    flow.Rtt      = DateTime.Now;
                    flow.item1    = assign.item1;
                    flow.item2    = assign.item2;
                    flow.item3    = assign.item3;
                    flow.item4    = assign.item4;
                    flow.item5    = assign.item5;
                    flow.item6    = assign.item6;
                    flow.item7    = assign.item7;
                    _db.OutsideBmedFlows.Add(flow);
                    _db.SaveChanges();
                    //Send Mail
                    //To user and the next flow user.
                    Tmail        mail = new Tmail();
                    string       body = "";
                    AppUserModel u;
                    //RepairModel repair = _context.BMEDRepairs.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 = "醫工智能保修系統[外部醫療使用申請] : 品名 : " + instrument.Name;
                    body += "<p>表單編號:" + instrument.DocId + "</p>";
                    body += "<p>申請日期:" + instrument.ApplyDate.ToString("yyyy/MM/dd") + "</p>";
                    body += "<p>申請人:" + instrument.UserName + "</p>";
                    body += "<p>品名:" + instrument.Name + "</p>";
                    body += "<p><a href='http://dms.cch.org.tw/BMED/Account/Login'" + "?docId=" + instrument.DocId + "&dealType=BMEDRepEdit" + ">處理案件</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);
            }
        }