public ActionResult SaveApp(int flowTypeId, int flowCaseId, CreateFlowCaseInfo caseinfo, string[] nextApprover)
        {
            Applicant    manager = new Applicant(WFEntities, this.Username);
            FlowCaseInfo @case   = manager.GetFlowCaseInfo(flowCaseId);

            if ([email protected](this.Username))
            {
                ViewBag.DisplayButtons = false;
                return(View("_PartialError", "_ModalLayout", StringResource.YOU_CAN_NOT_MODIFY_APPLATION));
            }
            if (caseinfo.Deadline.HasValue && caseinfo.Deadline <= DateTime.Now)
            {
                ViewBag.DisplayButtons = false;
                return(View("_PartialError", "~/Views/Shared/_ModalLayout.cshtml", StringResource.INVALID_DEADLINE));
            }

            //if (!manager.CheckCancelLeaveBalance(caseinfo.Properties, flowTypeId))
            //{
            //    ViewBag.DisplayButtons = false;
            //    return View("_PartialError", "~/Views/Shared/_ModalLayout.cshtml", StringResource.ALREADY_CANCELED);
            //}

            if (nextApprover != null && nextApprover.Length > 0)
            {
                caseinfo.Approver = nextApprover;
                if (nextApprover.GroupBy(p => p).Any(p => p.Count() > 1))
                {
                    ViewBag.DisplayButtons = false;
                    return(View("_PartialError", "~/Views/Shared/_ModalLayout.cshtml", StringResource.DUPLICATE_APPROVERS));
                }
            }
            int selectedFlowId = manager.SelectFlow(flowTypeId, caseinfo);

            if (selectedFlowId == 0 || selectedFlowId != caseinfo.FlowId)
            {
                ViewBag.DisplayButtons = false;
                return(View("_PartialError", "~/Views/Shared/_ModalLayout.cshtml", StringResource.TEMPLATE_OUT_OF_DATE));
            }
            if (selectedFlowId > 0)
            {
                caseinfo.FlowId = selectedFlowId;
                if ((nextApprover == null || nextApprover.Length == 0) && manager.HasSteps(selectedFlowId))
                {
                    NextStepData nsd = manager.GetNextStepApprovers(selectedFlowId, caseinfo.Properties, this.Username);
                    if (nsd == null || nsd.EmployeeList == null || nsd.EmployeeList.Count == 0 || nsd.EmployeeList.Any(p => p == null || p.Length == 0))
                    {
                        ViewBag.DisplayButtons = false;
                        return(View("_PartialError", "_ModalLayout", StringResource.NO_NEXT_APPROVER_FOUND));
                    }
                    return(View("~/Views/Application/SelectNextApprover.cshtml", "_ModalLayout", nsd.EmployeeList));
                    //if (nsd.EmployeeList.Count == 1 && nsd.EmployeeList[0].Length == 1)
                    //{
                    //    caseinfo.Approver = new string[] {nsd.EmployeeList.FirstOrDefault()?.FirstOrDefault()?.UserNo};
                    //}
                    //else
                    //{
                    //    return View("~/Views/Application/SelectNextApprover.cshtml", "~/Views/Shared/_ModalLayout.cshtml", nsd.EmployeeList);
                    //}
                }
                (CreateFlowResult result, int flowCaseId)result;
                if (@case.IsDraft)
                {
                    manager.MarkFlowAsObsolete(flowCaseId);
                    result = manager.CreateFlowCase(caseinfo);
                }
                else
                {
                    int version = (@case.Ver ?? 0) + 1;
                    int baseId  = @case.BaseFlowCaseId ?? @case.FlowCaseId;
                    manager.MarkFlowAsObsolete(flowCaseId);
                    result = manager.CreateFlowCase(caseinfo, version, baseId);
                }
                ViewBag.FlowCaseId    = result.flowCaseId;
                ViewBag.NextApprovers = caseinfo.Approver;
                if (result.result == CreateFlowResult.Success)
                {
                    if (caseinfo.Approver != null)
                    {
                        EmailService.SendWorkFlowEmail(
                            WFEntities.GlobalUserView.FirstOrDefault(p => p.EmployeeID == User.Identity.Name)?.EmployeeName,
                            caseinfo.Approver, caseinfo.Subject, null);
                        manager.NotificationSender.PushInboxMessages(caseinfo.Approver, result.flowCaseId);
                        manager.NotificationSender.Send();
                    }

                    ViewBag.PendingCount = manager.CountPending();
                    return(PartialView("_CreateResult", manager.GetFlowAndCase(result.flowCaseId)));
                }
            }
            else
            {
                ViewBag.DisplayButtons = false;
                return(View("_PartialError", "_ModalLayout", StringResource.NO_ELIGIBLE_WORKFLOW_FOUND));
            }
            ViewBag.DisplayButtons = false;
            return(View("_PartialError", "~/Views/Shared/_ModalLayout.cshtml", StringResource.UNABLE_TO_CREATE_YOUR_APPLICATION));
        }
        public object edit_application(EditApplicationParameterModel model)
        {
            Singleton <ILogWritter> .Instance?.WriteLog("WorkFlow-edit_application", JsonConvert.SerializeObject(model));

            Applicant    manager = new Applicant(Entities, User.Identity.Name);
            FlowCaseInfo @case   = manager.GetFlowCaseInfo(model.flowCaseId);

            if ([email protected](User.Identity.Name))
            {
                return(new { ret_code = RetCode.Error, ret_msg = "Can only edit application yourself." });
            }

            if (model.caseInfo.Deadline.HasValue && model.caseInfo.Deadline.Value.ToUniversalTime() <= DateTime.UtcNow)
            {
                return(new { ret_code = RetCode.Error, ret_msg = "Invalid dead line" });
            }

            if (model.nextApprover != null && model.nextApprover.Length > 0)
            {
                model.caseInfo.Approver = model.nextApprover;
                if (model.nextApprover.GroupBy(p => p).Any(p => p.Count() > 1))
                {
                    return(new { ret_code = RetCode.Error, ret_msg = "Duplicate approvers." });
                }
            }

            int selectedFlowId = manager.SelectFlow(model.flowTypeId, model.caseInfo);

            if (selectedFlowId == 0)
            {
                return(new { ret_code = RetCode.Error, ret_msg = "Template out of date." });
            }

            if (selectedFlowId > 0)
            {
                model.caseInfo.FlowId = selectedFlowId;
                (CreateFlowResult result, int flowCaseId)res;
                if (@case.IsDraft)
                {
                    manager.MarkFlowAsObsolete(model.flowCaseId);
                    res = manager.CreateFlowCase(model.caseInfo);
                }
                else
                {
                    int version = (@case.Ver ?? 0) + 1;
                    int baseId  = @case.BaseFlowCaseId ?? @case.FlowCaseId;
                    manager.MarkFlowAsObsolete(model.flowCaseId);
                    res = manager.CreateFlowCase(model.caseInfo, version, baseId);
                }

                if (res.result == CreateFlowResult.Success)
                {
                    if (model.caseInfo.Approver != null)
                    {
                        EmailService.SendWorkFlowEmail(
                            Entities.GlobalUserView.FirstOrDefault(p => p.EmployeeID == User.Identity.Name)?.EmployeeName,
                            model.caseInfo.Approver, model.caseInfo.Subject, null);
                        manager.NotificationSender.PushInboxMessages(model.caseInfo.Approver, res.flowCaseId);
                        manager.NotificationSender.Send();
                    }

                    return(new { ret_code = RetCode.Success });
                }
            }
            else
            {
                return(new { ret_code = RetCode.Error, ret_msg = "未找到符合条件的工作流" });
            }

            return(new { ret_code = RetCode.Error, ret_msg = "创建失败" });
        }