public object CreateCancel(CreateCancelParameterModel model)
        {
            Applicant       applicant       = new Applicant(Entities, User.Identity.Name);
            ApplicationUser applicationUser = new ApplicationUser(Entities, User.Identity.Name);
            var             flowcase        = Entities.WF_FlowCases.FirstOrDefault(p => p.FlowCaseId == model.flowcaseid && p.StatusId > 0);

            if (flowcase == null)
            {
                return(new { ret_code = RetCode.Error, ret_msg = "Can not find workflow" });
            }

            if (Entities.WF_FlowCases.FirstOrDefault(p =>
                                                     p.StatusId > 0 && p.RelatedFlowCaseId == model.flowcaseid) != null)
            {
                return(new { ret_code = RetCode.Error, ret_msg = "Cancelled already" });
            }

            CreateFlowCaseInfo create = new CreateFlowCaseInfo();

            if (model.approvers != null && model.approvers.Length > 0)
            {
                create.Approver = model.approvers;
                if (model.approvers.GroupBy(p => p).Any(p => p.Count() > 1))
                {
                    return(new { ret_code = RetCode.Error, ret_msg = "Duplicate approvers" });
                }
            }
            create.FlowId        = flowcase.FlowId;
            create.Properties    = applicationUser.GetPropertyValues(model.flowcaseid);
            create.Subject       = flowcase.Subject + "[Cancel]";
            create.RelatedCaseId = model.flowcaseid;
            create.Deadline      = flowcase.Deadline;
            create.Dep           = flowcase.Department;
            create.NotifyUsers   = flowcase.WF_CaseNotificateUsers.Where(p => p.StatusId > 0).Select(p => p.UserNo)
                                   .ToArray();
            create.CoverDuties = flowcase.WF_CaseCoverUsers.Where(p => p.StatusId > 0).Select(p => p.UserNo)
                                 .ToArray();
            try
            {
                (CreateFlowResult result, int flowCaseId)res = applicant.CreateFlowCase(create);
                if (res.result == CreateFlowResult.Success)
                {
                    if (create.Approver != null)
                    {
                        EmailService.SendWorkFlowEmail(
                            Entities.GlobalUserView.FirstOrDefault(p => p.EmployeeID == User.Identity.Name)?.EmployeeName,
                            create.Approver, create.Subject, null);
                        applicant.NotificationSender.PushInboxMessages(create.Approver, res.flowCaseId);
                        applicant.NotificationSender.Send();
                    }

                    return(new { ret_code = RetCode.Success });
                }
            }
            catch (Exception ex)
            {
                return(new { ret_code = RetCode.Error, ret_msg = ex.Message });
            }
            return(new { ret_code = RetCode.Error, ret_msg = "Can not create application" });
        }
        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 ActionResult SaveApp(int flowTypeId, CreateFlowCaseInfo caseInfo, string[] nextApprover)
        {
            if (caseInfo.Deadline.HasValue && caseInfo.Deadline <= DateTime.Now)
            {
                ViewBag.DisplayButtons = false;
                return(View("_PartialError", "~/Views/Shared/_ModalLayout.cshtml", StringResource.INVALID_DEADLINE));
            }

            Applicant manager = new Applicant(WFEntities, this.Username);

            //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)
            {
                caseInfo.FlowId = selectedFlowId;
                bool HasStep = manager.HasSteps(selectedFlowId);
                if ((nextApprover == null || nextApprover.Length == 0) && HasStep)
                {
                    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("SelectNextApprover", "_ModalLayout", nsd.EmployeeList));
                }
                (CreateFlowResult result, int flowCaseId)res = manager.CreateFlowCase(caseInfo);
                ViewBag.FlowCaseId    = res.flowCaseId;
                ViewBag.NextApprovers = caseInfo.Approver;
                if (res.result == CreateFlowResult.Success)
                {
                    EmailService.SendWorkFlowEmail(
                        WFEntities.GlobalUserView.FirstOrDefault(p => p.EmployeeID == User.Identity.Name)?.EmployeeName,
                        caseInfo.Approver, caseInfo.Subject, null);
                    manager.NotificationSender.PushInboxMessages(caseInfo.Approver, res.flowCaseId);
                    manager.NotificationSender.Send();
                    ViewBag.PendingCount = manager.CountPending();
                    return(PartialView("_CreateResult", manager.GetFlowAndCase(res.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 ActionResult CreateCancel(int flowcaseid, string[] nextApprover)
        {
            Applicant       applicant       = new Applicant(WFEntities, this.Username);
            ApplicationUser applicationUser = new ApplicationUser(WFEntities, this.Username);
            var             flowcase        = WFEntities.WF_FlowCases.FirstOrDefault(p => p.FlowCaseId == flowcaseid && p.StatusId > 0);

            if (WFEntities.WF_FlowCases.FirstOrDefault(p =>
                                                       p.StatusId > 0 && p.RelatedFlowCaseId == flowcaseid) != null)
            {
                ViewBag.DisplayButtons = false;
                return(View("_PartialError", "~/Views/Shared/_ModalLayout.cshtml", "Cancelled already."));
            }
            if (flowcase?.FlowId > 0)
            {
                bool HasStep = applicant.HasSteps(flowcase.FlowId);
                if ((nextApprover == null || nextApprover.Length == 0) && HasStep)
                {
                    NextStepData nsd = applicant.GetNextStepApprovers(flowcase.FlowId,
                                                                      applicationUser.GetPropertyValues(flowcaseid), 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", "~/Views/Shared/_ModalLayout.cshtml", StringResource.NO_NEXT_APPROVER_FOUND));
                    }
                    return(View("SelectNextApprover", "~/Views/Shared/_ModalLayout.cshtml", nsd.EmployeeList));
                }
                CreateFlowCaseInfo create = new CreateFlowCaseInfo();
                if (nextApprover != null && nextApprover.Length > 0)
                {
                    create.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));
                    }
                }

                create.FlowId        = flowcase.FlowId;
                create.Properties    = applicationUser.GetPropertyValues(flowcaseid);
                create.Subject       = flowcase.Subject + "[Cancel]";
                create.RelatedCaseId = flowcaseid;
                create.Deadline      = flowcase.Deadline;
                create.Dep           = flowcase.Department;
                create.NotifyUsers   = flowcase.WF_CaseNotificateUsers.Where(p => p.StatusId > 0).Select(p => p.UserNo)
                                       .ToArray();
                create.CoverDuties = flowcase.WF_CaseCoverUsers.Where(p => p.StatusId > 0).Select(p => p.UserNo)
                                     .ToArray();
                (CreateFlowResult result, int flowCaseId)res = applicant.CreateFlowCase(create);
                ViewBag.FlowCaseId    = res.flowCaseId;
                ViewBag.NextApprovers = create.Approver;
                if (res.result == CreateFlowResult.Success)
                {
                    EmailService.SendWorkFlowEmail(
                        WFEntities.GlobalUserView.FirstOrDefault(p => p.EmployeeID == User.Identity.Name)?.EmployeeName,
                        create.Approver, create.Subject, null);
                    applicant.NotificationSender.PushInboxMessages(create.Approver, res.flowCaseId);
                    applicant.NotificationSender.Send();
                    ViewBag.PendingCount = applicant.CountPending();
                    return(PartialView("_CreateResult", applicant.GetFlowAndCase(res.flowCaseId)));
                }
            }
            ViewBag.DisplayButtons = false;
            return(View("_PartialError", "~/Views/Shared/_ModalLayout.cshtml", "Cancel flowcase failed."));
        }
        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 = "创建失败" });
        }
        public object PostApplication(SaveApplicationParameterModel model)
        {
            Singleton <ILogWritter> .Instance?.WriteLog("WorkFlow-PostApplication", JsonConvert.SerializeObject(model));

            //对于没有step的case,客户端传入的nextapprover需为空
            if (model.caseInfo.Deadline.HasValue && model.caseInfo.Deadline.Value.ToUniversalTime() <= DateTime.UtcNow)
            {
                return(new { ret_code = RetCode.Error, ret_msg = "Invalid dead line" });
            }

            Applicant manager        = new Applicant(Entities, User.Identity.Name);
            int       selectedFlowId = manager.SelectFlow(model.flowTypeId, model.caseInfo);
            bool      hasSteps       = manager.HasSteps(selectedFlowId);

            model.caseInfo.Approver = model.nextApprover;

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

            //if (!manager.CheckCancelLeaveBalance(model.caseInfo.Properties, model.flowTypeId))
            //{
            //    return new { ret_code = RetCode.Error, ret_msg = "The date you selected is cancelled already." };
            //}

            if (selectedFlowId > 0)
            {
                model.caseInfo.FlowId = selectedFlowId;
                if ((model.nextApprover == null || model.nextApprover.Length == 0) && hasSteps)
                {
                    return(new { ret_code = RetCode.Error, ret_msg = "Must provide next approver" });
                }

                try
                {
                    (CreateFlowResult result, int flowCaseId)res = manager.CreateFlowCase(model.caseInfo);
                    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 });
                    }
                }
                catch (Exception ex)
                {
                    return(new { ret_code = RetCode.Error, ret_msg = ex.Message });
                }
            }
            else
            {
                return(new { ret_code = RetCode.Error, ret_msg = "Can not find workflow" });
            }

            return(new { ret_code = RetCode.Error, ret_msg = "Can not create application" });
        }