/// <summary>
        /// 添加流程处理记录
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="curUser"></param>
        /// <param name="operation"></param>
        public void AddWorkflowProcess(MissionskyOAEntities dbContext, UserModel curUser,
                                       OperateOrderModel operation)
        {
            var process = new WorkflowProcessModel()
            {
                OrderNo       = operation.OrderNo,
                FlowId        = 0,
                StepId        = 0,
                Operator      = curUser.Id,
                OperationDesc = string.Format("{0}取消了申请单。", curUser.EnglishName),
                Operation     = operation.Operation,
                CreatedTime   = DateTime.Now
            };

            dbContext.WorkflowProcesses.Add(process.ToEntity());
        }
Esempio n. 2
0
        public ApiResponse <bool> RejecteOrder(OperateOrderModel model)
        {
            if (model == null || model.OrderNo < 1)
            {
                throw new Exception("Invalid order model.");
            }

            model.Operation = WorkflowOperation.Rejecte;

            ApiResponse <bool> response = new ApiResponse <bool>()
            {
                Result = this.OrderService.Rejecte(model, this.Member)
            };

            return(response);
        }
Esempio n. 3
0
        public ActionResult OperateOrder(int oid = -1, int actionType = -1, string actionDes = "")
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);

            if (orderInfo == null)
            {
                return(PromptView("订单不存在"));
            }

            if (actionDes.Length > 125)
            {
                OperateOrderModel model = new OperateOrderModel();
                model.Oid             = oid;
                model.OrderInfo       = orderInfo;
                model.OrderActionType = (OrderActionType)actionType;
                model.ActionDes       = actionDes;

                ModelState.AddModelError("actionDes", "最多只能输入125个字");
                return(View(model));
            }

            OrderActionType orderActionType = (OrderActionType)actionType;
            OrderState      orderState      = (OrderState)orderInfo.OrderState;

            if (orderActionType == OrderActionType.Confirm)//确认订单
            {
                if (orderState != OrderState.Confirming)
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "买家还未付款,不能确认订单"));
                }

                AdminOrders.ConfirmOrder(orderInfo);
                CreateOrderAction(oid, orderActionType, actionDes.Length == 0 ? "您的订单已经确认" : actionDes);
            }
            else if (orderActionType == OrderActionType.PreProduct)//备货
            {
                if (orderState != OrderState.Confirmed)
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "订单还未确认,不能备货"));
                }

                AdminOrders.PreProduct(orderInfo);
                CreateOrderAction(oid, orderActionType, actionDes.Length == 0 ? "您的订单正在备货" : actionDes);
            }
            else if (orderActionType == OrderActionType.Send)//发货
            {
                if (orderState != OrderState.PreProducting)
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "订单还未备货,不能发货"));
                }

                string shipSN = WebHelper.GetFormString("shipSN").Trim();
                if (shipSN.Length < 1)
                {
                    OperateOrderModel model = new OperateOrderModel();
                    model.Oid             = oid;
                    model.OrderInfo       = orderInfo;
                    model.OrderActionType = orderActionType;
                    model.ActionDes       = actionDes;

                    ModelState.AddModelError("shipSN", "请填写配送单号");
                    return(View(model));
                }
                AdminOrders.SendOrder(oid, OrderState.Sended, shipSN, DateTime.Now);
                CreateOrderAction(oid, orderActionType, actionDes.Length == 0 ? "您的订单已经发货,发货方式为:" + orderInfo.ShipFriendName + ",单号为:" + shipSN : actionDes);
            }
            else if (orderActionType == OrderActionType.Lock)//锁定订单
            {
                if (!(orderState == OrderState.WaitPaying || (orderState == OrderState.Confirming && orderInfo.PayMode == 0)))
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "订单当前不能锁定"));
                }

                AdminOrders.LockOrder(orderInfo);
                CreateOrderAction(oid, orderActionType, "订单已锁定:" + actionDes);
            }
            else if (orderActionType == OrderActionType.Cancel)//取消订单
            {
                if (!(orderState == OrderState.WaitPaying || (orderState == OrderState.Confirming && orderInfo.PayMode == 0)))
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "订单当前不能取消"));
                }

                PartUserInfo partUserInfo = Users.GetPartUserById(orderInfo.Uid);
                AdminOrders.CancelOrder(ref partUserInfo, orderInfo, WorkContext.Uid, DateTime.Now);
                CreateOrderAction(oid, orderActionType, actionDes.Length == 0 ? "订单已取消" : actionDes);
            }
            else
            {
                return(PromptView(Url.Action("orderinfo", new { oid = oid }), "当前操作不存在"));
            }

            AddAdminOperateLog("操作订单", "操作订单,订单ID为:" + oid);
            return(PromptView(Url.Action("orderinfo", new { oid = oid }), "操作已完成"));
        }
Esempio n. 4
0
        public ActionResult OperateOrder(int oid = -1, int actionType = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);

            if (orderInfo == null)
            {
                return(PromptView("订单不存在"));
            }

            OrderActionType orderActionType = (OrderActionType)actionType;
            OrderState      orderState      = (OrderState)orderInfo.OrderState;

            if (orderActionType == OrderActionType.Confirm)//确认订单
            {
                if (orderState != OrderState.Confirming)
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "买家还未付款,不能确认订单"));
                }
            }
            else if (orderActionType == OrderActionType.PreProduct)//备货
            {
                if (orderState != OrderState.Confirmed)
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "订单还未确认,不能备货"));
                }
            }
            else if (orderActionType == OrderActionType.Send)//发货
            {
                if (orderState != OrderState.PreProducting)
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "订单还未备货,不能发货"));
                }
            }
            else if (orderActionType == OrderActionType.Lock)//锁定订单
            {
                if (!(orderState == OrderState.WaitPaying || (orderState == OrderState.Confirming && orderInfo.PayMode == 0)))
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "订单当前不能锁定"));
                }
            }
            else if (orderActionType == OrderActionType.Cancel)//取消订单
            {
                if (!(orderState == OrderState.WaitPaying || (orderState == OrderState.Confirming && orderInfo.PayMode == 0)))
                {
                    return(PromptView(Url.Action("orderinfo", new { oid = oid }), "订单当前不能取消"));
                }
            }
            else
            {
                return(PromptView(Url.Action("orderinfo", new { oid = oid }), "当前操作不存在"));
            }

            OperateOrderModel model = new OperateOrderModel();

            model.Oid             = oid;
            model.OrderInfo       = orderInfo;
            model.OrderActionType = orderActionType;
            model.ActionDes       = "";

            return(View(model));
        }
        /// <summary>
        /// 拒绝请假加班申请(撤销)单
        /// </summary>
        /// <param name="model">申请单</param>
        /// <param name="approver">审批人</param>
        /// <returns>是否拒绝成功</returns>
        public bool Rejecte(OperateOrderModel model, UserModel approver)
        {
            if (model == null || model.OrderNo < 1 || approver == null)
            {
                Log.Error("无效的参数。");
                throw new InvalidOperationException("无效的参数。");
            }

            if (string.IsNullOrEmpty(model.Opinion))
            {
                Log.Error("拒拒理由为空。");
                throw new InvalidOperationException("拒拒理由为空。");
            }

            using (var dbContext = new MissionskyOAEntities())
            {
                #region 申请单详细
                //申请单详细
                var orderEntities = dbContext.Orders.Where(it => it.OrderNo == model.OrderNo);

                #region 验证申请是否有效
                var validateEntity = orderEntities.FirstOrDefault();

                //流程及步骤是否有效
                if (validateEntity == null || validateEntity.WorkflowId < 1 || validateEntity.NextStep < 1)
                {
                    Log.Error("申请单流程无效。");
                    throw new InvalidOperationException("申请单流程无效.");
                }

                var validateOrder = validateEntity.ToModel();

                //申请单为撤销单
                var isRevokedOrder = validateOrder.IsRevokedOrder();

                //领导审批中
                var isApproveStatus = validateOrder.IsApproveStatus();

                //行政财务审批中
                var isReviewStatus = validateOrder.IsReviewStatus();

                //申请状态不是审批状态
                if (!isApproveStatus && !isReviewStatus)
                {
                    Log.Error("申请单状态无效。");
                    throw new InvalidOperationException("申请单状态无效.");
                }

                //用户不是申请当前的审批人
                if (approver.Id != validateEntity.NextAudit)
                {
                    Log.Error("当前用户不能审批申请。");
                    throw new InvalidOperationException("当前用户不能审批申请.");
                }

                //申请人详细
                var applicant = dbContext.Users.FirstOrDefault(it => it.Id == validateEntity.ApplyUserId);
                if (applicant == null)
                {
                    Log.Error("无效的申请人。");
                    throw new KeyNotFoundException("无效的申请人。");
                }
                #endregion

                var hasSentToAapplicant = false;  //已经向申请人发送过拒绝申请的推送消息
                var operation           = new OperateOrderModel()
                {
                    Operation = WorkflowOperation.Rejecte, Opinion = model.Opinion
                };
                WorkflowProcessModel process = _workflowProcessService.Process(dbContext, validateOrder, approver, operation); //拒绝申请

                orderEntities.ToList().ForEach(o =>
                {
                    if (isRevokedOrder && !isApproveStatus) //拒绝撤销申请单,则更新原始申请单为通过状态
                    {
                        var oldOrder = dbContext.Orders.FirstOrDefault(it => it.Id == o.RefOrderId.Value);

                        if (oldOrder != null)
                        {
                            oldOrder.Status = (int)OrderStatus.Approved;
                        }
                    }

                    //更新申请单状态[拒绝]
                    o.Status    = (int)OrderStatus.Rejected;
                    o.NextAudit = null; //不再需要审批
                    o.NextStep  = null; //

                    //更新假期
                    //_askLeaveService.RecoverOrder(o.ToModel());

                    hasSentToAapplicant = (hasSentToAapplicant || o.UserId == applicant.Id);
                    AddNotification(dbContext, validateOrder, approver.Id, process, o.Id); //向用户推送拒绝消息
                });

                if (!hasSentToAapplicant)
                {
                    AddNotification(dbContext, validateOrder, approver.Id, process, applicant.Id); //向申请人推送拒绝消息
                }
                #endregion

                #region 添加审计信息
                //审计信息
                var message = new AuditMessageModel()
                {
                    Type        = AuditMessageType.None,
                    Status      = AuditMessageStatus.Unread,
                    CreatedTime = DateTime.Now,
                    Message     = "你拒绝{0}的{1}申请"
                };

                //同意加班申请/撤销消息
                if (validateOrder.IsOvertime())
                {
                    message.Type    = AuditMessageType.Recject_OT_Application_Message;
                    message.Message = string.Format(message.Message, applicant.EnglishName,
                                                    (validateOrder.IsRevokedOrder() ? "加班撤销" : "加班"));
                }

                //同意请假申请/撤销消息
                if (validateOrder.IsAskLeave())
                {
                    message.Type    = AuditMessageType.Recject_Leave_Application_Message;
                    message.Message = string.Format(message.Message, applicant.EnglishName,
                                                    (validateOrder.IsRevokedOrder() ? "请假撤销" : "请假"));
                }

                dbContext.AuditMessages.Add(message.ToEntity());
                #endregion

                //更新数据库
                dbContext.SaveChanges();

                return(true);
            }
        }
        /// <summary>
        /// 流程撤销
        /// </summary>
        /// <param name="dbContext">数据库上下文</param>
        /// <param name="order">申请单</param>
        /// <param name="owner">申请人</param>
        /// <param name="operation">流程操作</param>
        /// <returns>下一个审批步骤</returns>
        public WorkflowProcessModel Revoke(MissionskyOAEntities dbContext, OrderModel order, UserModel owner, OperateOrderModel operation)
        {
            if (order == null)
            {
                Log.Error("参数无效。");
                throw new InvalidOperationException("参数无效。");
            }

            // 流程审批
            WorkflowProcessModel process = _workflowProcessService.Process(dbContext, order, owner, operation);

            return(process);
        }
        /// <summary>
        /// 批准请假加班申请(撤销)单
        /// </summary>
        /// <param name="model">申请单</param>
        /// <param name="approver">审批人</param>
        /// <returns>是否审批成功</returns>
        public int Approve(OperateOrderModel model, UserModel approver)
        {
            if (model == null || model.OrderNo < 1 || approver == null)
            {
                Log.Error("无效的操作。");
                throw new InvalidOperationException("无效的操作。");
            }

            using (var dbContext = new MissionskyOAEntities())
            {
                #region 1 申请单详细

                //申请单详细
                var orderEntities = dbContext.Orders.Where(it => it.OrderNo == model.OrderNo); //所有申请单
                var entity        = orderEntities.FirstOrDefault();

                if (entity == null)
                {
                    Log.Error("无效的申请单。");
                    throw new KeyNotFoundException("无效的申请单。");
                }

                var order = entity.ToModel();
                if (order.OrderUsers == null || order.OrderUsers.Count < 1)
                {
                    order.OrderUsers = _userService.GetOrderUsers(dbContext, order.OrderNo);
                }

                //当前审批人是否为流程下一个审批人 || 当前审批人是否存在
                if (order.NextAudit == null && order.NextStep == null)
                {
                    Log.Error("申请流程已完成。");
                    throw new InvalidOperationException("申请流程已完成。");
                }

                if (approver.Id != order.NextAudit)
                {
                    Log.Error("当前用户不允许审批流程。");
                    throw new InvalidOperationException("当前用户不允许审批流程。");
                }

                //申请人详细
                var applicant = dbContext.Users.FirstOrDefault(it => it.Id == order.ApplyUserId);
                if (applicant == null)
                {
                    Log.Error("找不到申请人。");
                    throw new KeyNotFoundException("找不到申请人。");
                }

                #endregion

                #region 2 流程审批处理

                var process = _workflowProcessService.Process(dbContext, order, approver, model);

                if (process == null)
                {
                    Log.Error("找不到流程。");
                    throw new InvalidOperationException("找不到流程。");
                }

                orderEntities.ToList().ForEach(item =>
                {
                    item.Status = (int)process.NextStatus;

                    if (process.NextStep != null) //下一步骤不为null,则需要上级继续审批
                    {
                        item.NextStep  = process.NextStep.Id;
                        item.NextAudit = process.NextApprover.Id;
                    }
                    else //下一步骤为null,则审批结束
                    {
                        item.NextAudit = null;
                        item.NextStep  = null;
                        item.Status    = order.IsRevokedOrder()
                            ? (int)OrderStatus.Revoked
                            : (int)OrderStatus.Approved;  //批准撤销 || 批准申请
                    }
                });

                #endregion

                #region 3 添加审计信息

                //审计信息
                var message = new AuditMessageModel()
                {
                    Type        = AuditMessageType.None,
                    Status      = AuditMessageStatus.Unread,
                    CreatedTime = DateTime.Now,
                    Message     = "你同意{0}的{1}申请"
                };

                //同意加班申请/撤销消息
                if (order.IsOvertime())
                {
                    message.Type = order.IsRevokedOrder()
                        ? AuditMessageType.Approve_OT_Cance_Application_Message
                        : AuditMessageType.Approve_OT_Application_Message;
                    message.Message = string.Format(message.Message, applicant.EnglishName, "加班");
                }

                //同意请假申请/撤销消息
                if (order.IsAskLeave())
                {
                    message.Type = order.IsRevokedOrder()
                        ? AuditMessageType.Approve_Leave_Cacnel_Application_Message
                        : AuditMessageType.Approve_Leave_Application_Message;
                    message.Message = string.Format(message.Message, applicant.EnglishName, "请假");
                }

                dbContext.AuditMessages.Add(message.ToEntity());

                #endregion

                #region 4 工作流处理消息推送

                //流程结束 && 批量申请,向流程申请人发推送消息
                if (process.IsCompletedApproved() && order.IsBatchApply)
                {
                    AddNotification(dbContext, order, approver.Id, process); //默认发送到申请人
                }

                order.OrderUsers.ToList().ForEach(item =>
                {
                    if (process.IsCompletedApproved()) //审批通过, 流程结束 || 过[行政审阅]到[财务审阅]
                    {
                        AddNotification(dbContext, order, approver.Id, process, item.Id);
                    }
                    else if (process.NeedContinueApprove()) //需要下一步审批审批
                    {
                        AddNotification(dbContext, order, approver.Id, process, process.NextApprover.Id);
                    }
                    else
                    {
                        Log.Error("处理流程异常。");
                        throw new InvalidOperationException("处理流程异常。");
                    }
                });

                #endregion

                // 5更新数据库
                dbContext.SaveChanges();

                return((int)order.Status);
            }
        }
        /// <summary>
        /// 流程处理
        /// </summary>
        /// <param name="dbContext">数据库上下文</param>
        /// <param name="order">申请单</param>
        /// <param name="operation">流程操作</param>
        /// <param name="curUser">当前用户</param>
        /// <returns>流程处理对象</returns>
        /// <remarks>
        /// 流程逻辑:
        /// 1, 提交申请时,查找申请人的直接领导,
        /// 2, 如果申请人的直接领导可以审批通过,则转到行政审批
        /// 3, 如果申请人的直接领导没有权限审批, 则转到审批人的直接领导,重复2的验证
        /// 4, 如果申请人无直接领导,则转到行政审批
        /// </remarks>
        public WorkflowProcessModel Process(MissionskyOAEntities dbContext, OrderModel order, UserModel curUser, OperateOrderModel operation)
        {
            try
            {
                #region 1, 验证申请单数据及状态
                if (order == null || order.OrderDets == null || order.OrderDets.Count < 1)
                {
                    Log.Error("申请单数据无效。");
                    throw new InvalidOperationException("申请单数据无效。");
                }

                if (!order.IsApproveStatus() && !order.IsReviewStatus()) //无效的[审批或审阅]状态
                {
                    Log.Error("申请单状态无效。");
                    throw new KeyNotFoundException("申请单状态无效。");
                }

                if (operation.Operation == WorkflowOperation.Approve &&
                    (order.WorkflowId == null || order.WorkflowId.Value < 1 || order.NextStep == null ||
                     order.NextStep.Value < 1 || order.NextAudit == null || order.NextAudit.Value < 1))
                {
                    Log.Error("申请单流程无效。");
                    throw new KeyNotFoundException("申请单流程无效。");
                }

                var detail = order.OrderDets.FirstOrDefault(); //申请单详细

                if (detail == null || !detail.IOHours.HasValue)
                {
                    throw new KeyNotFoundException("申请单详细数据无效。");
                }
                #endregion

                //流程处理
                var workflow     = GetWorkflow(dbContext, order, curUser, operation);      //2, 选择流程
                var nextStep     = GotoNextStep(order, curUser, workflow, operation);      //3, 选择流程下一步
                var nextApprover = GotoNextApprover(dbContext, nextStep);                  //4, 转到流程下一步审批人
                var nextStatus   = SwitchStatus(order, nextStep, operation);               //5, 转换申请单流程状态

                UpdateAttendanceSummary(dbContext, order, workflow, nextStep, nextStatus); //6, 更新假期表

                var process = new WorkflowProcessModel()
                {
                    OrderNo       = order.OrderNo,
                    FlowId        = workflow.Id,
                    StepId        = operation.Operation == WorkflowOperation.Apply || !order.NextStep.HasValue? 0 : order.NextStep.Value,
                    NextStep      = nextStep,
                    NextApprover  = nextApprover,
                    NextStatus    = nextStatus,
                    Operator      = curUser.Id,
                    OperationDesc =
                        operation.Operation == WorkflowOperation.Apply ? detail.Description : operation.Opinion,
                    Operation =
                        operation.Operation == WorkflowOperation.Apply
                            ? WorkflowOperation.Apply
                            : operation.Operation,
                    CreatedTime = DateTime.Now
                };

                dbContext.WorkflowProcesses.Add(process.ToEntity());

                return(process);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 流程当前步骤
        /// </summary>
        /// <param name="order"></param>
        /// <param name="workflow"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        private WorkflowStepModel GetCurrentStep(OrderModel order, WorkflowModel workflow, OperateOrderModel operation)
        {
            WorkflowStepModel curStep = null;

            if (operation.Operation == WorkflowOperation.Apply)
            {
                curStep = workflow.GetUserApplyStep(); //当前步骤为[申请]步骤
            }
            else
            {
                if (order.NextStep.HasValue)
                {
                    curStep = (order.NextStep.Value == Constant.WORKFLOW_DIRECT_SUPERVISOR_APPROVE_NODE
                        ? workflow.GetDirectSupervisorApproveStep() //当前步骤为[直接领导审批]步骤
                        : workflow.WorkflowSteps.FirstOrDefault(it => it.Id == order.NextStep.Value));
                }
            }

            if (curStep == null)
            {
                Log.Error("当前流程步骤无效。");
                throw new KeyNotFoundException("当前流程步骤无效。");
            }

            return(curStep);
        }
        /// <summary>
        /// 申请单流程状态转换
        /// </summary>
        /// <param name="order">申请单</param>
        /// <param name="curUser">当前用户</param>
        /// <param name="workflow">流程下一步</param>
        /// <param name="operation">流程操作</param>
        /// <returns>流程下一步状态</returns>
        private WorkflowStepModel GotoNextStep(OrderModel order, UserModel curUser, WorkflowModel workflow, OperateOrderModel operation)
        {
            if (order == null || order.OrderDets == null || order.OrderDets.Count < 1)
            {
                Log.Error("无效的申请单。");
                throw new InvalidOperationException("无效的申请单。");
            }

            if (workflow == null || workflow.WorkflowSteps == null || workflow.WorkflowSteps.Count < 1)
            {
                Log.Error("无效的流程。");
                throw new InvalidOperationException("无效的流程。");
            }

            WorkflowStepModel nextStep = null; //流程下一步

            switch (operation.Operation)
            {
            case WorkflowOperation.Apply:     //申请
            case WorkflowOperation.Revoke:    //撤销
                //直接领导
                var director = curUser.DirectlySupervisorId.HasValue
                        ? _userService.GetUserDetail(curUser.DirectlySupervisorId.Value)
                        : null;

                nextStep = GetNextStepForUserApply(director, workflow);
                break;

            case WorkflowOperation.Approve:                                                                            //审批
                var curStep = GetCurrentStep(order, workflow, operation);                                              //获取流程当前步骤
                curStep = GetApproveStepForApprover(curUser, curStep, workflow);                                       //将[直接领导]审批步骤转换为有效审批步骤

                if (curStep.IsLeaderApproving())                                                                       //当前是[领导审批]
                {
                    nextStep = GetNextStepForLeaderApprove(order, curStep, workflow);                                  //获取[领导审批]的下一步骤
                }
                else if (curStep.IsAdminReviewing())                                                                   //当前是[行政审批]
                {
                    nextStep = workflow.WorkflowSteps.FirstOrDefault(it => it.Type == WorkflowStepType.FinanceReview); //需要[财务审批]
                }
                break;

            case WorkflowOperation.None:     //无效
            case WorkflowOperation.Rejecte:  //拒绝
            case WorkflowOperation.Cancel:   //取消
                break;
            }

            return(nextStep);
        }
        /// <summary>
        /// 获取申请单流程
        /// </summary>
        /// <param name="dbContext">数据库上下文对象</param>
        /// <param name="order">申请单</param>
        /// <param name="curUser">当前用户</param>
        /// <param name="operation">操作</param>
        /// <returns>流程对象</returns>
        private WorkflowModel GetWorkflow(MissionskyOAEntities dbContext, OrderModel order, UserModel curUser, OperateOrderModel operation)
        {
            var searchWorkflow = new SearchWorkflowModel();

            if (operation.Operation == WorkflowOperation.Apply) //申请
            {
                //获取流程Id
                searchWorkflow.WorkflowId = MatchWorkflow(dbContext, order, curUser); //匹配当前用户满足条件的申请流程
            }
            else
            {
                if (!order.WorkflowId.HasValue)
                {
                    Log.Error("无效的申请单流程。");
                    throw new InvalidOperationException("无效的申请单流程。");
                }

                searchWorkflow.WorkflowId = order.WorkflowId.Value;
            }

            var workflow = _workflowService.GetWorkflowDetail(dbContext, searchWorkflow.WorkflowId); //流程

            if (workflow == null)
            {
                Log.Error("找不到申请流程。");
                throw new KeyNotFoundException("找不到申请流程。");
            }

            return(workflow);
        }
        /// <summary>
        /// 申请单流程状态转换
        /// </summary>
        /// <param name="order">申请单</param>
        /// <param name="nextStep">流程下一步</param>
        /// <param name="operation">流程操作</param>
        /// <returns>流程下一步状态</returns>
        private OrderStatus SwitchStatus(OrderModel order, WorkflowStepModel nextStep, OperateOrderModel operation)
        {
            var nextStatus = OrderStatus.Apply;

            //1, 申请
            if (operation.Operation == WorkflowOperation.Apply)
            {
                nextStatus = OrderStatus.Apply;
            }
            //2, 拒绝
            else if (operation.Operation == WorkflowOperation.Rejecte)
            {
                nextStatus = OrderStatus.Rejected;
            }
            //3, 流程未完成
            else if (nextStep != null && nextStep.Id > 0)
            {
                //3.1, 行政、财务审阅(领导已批准)
                if (nextStep.IsAdminReviewing() || nextStep.IsFinanceReviewing())
                {
                    //nextStatus = OrderStatus.Reviewing;
                    nextStatus = (order.RefOrderId == null || order.RefOrderId.Value < 1
                        ? OrderStatus.Approved
                        : OrderStatus.Revoked);
                }
                //3.2, 领导审批
                else if (nextStep.IsLeaderApproving())
                {
                    nextStatus = OrderStatus.Approving;
                }
            }
            //4, 流程结束
            else
            {
                nextStatus = (order.RefOrderId == null || order.RefOrderId.Value < 1
                    ? OrderStatus.Approved
                    : OrderStatus.Revoked);
            }

            return(nextStatus);
        }
        /// <summary>
        /// 取消请假或者加班单
        /// </summary>
        /// <returns>取消</returns>
        public bool CancelOrder(int orderNo, UserModel model)
        {
            using (var dbContext = new MissionskyOAEntities())
            {
                //查询申请信息是否存在
                var orders           = dbContext.Orders.Where(it => it.OrderNo == orderNo);
                var auditMessageType = (int)AuditMessageType.Apply_Leave_Cancel_Application_Message;

                orders.ToList().ForEach(order =>
                {
                    var detail = dbContext.OrderDets.FirstOrDefault(item => item.OrderId == order.Id);

                    if (detail == null)
                    {
                        Log.Error("找不到订单详细。");
                        throw new KeyNotFoundException("找不到订单详细。");
                    }

                    //审阅信息类型
                    if (order.OrderType == (int)OrderType.Overtime)
                    {
                        auditMessageType = (int)AuditMessageType.Apply_OT_Cancel_Application_Message;
                    }

                    //对于未批准的订单直接取消
                    if (order.Status != (int)OrderStatus.Approved)
                    {
                        order.Status     = (int)OrderStatus.Canceled;
                        order.NextAudit  = null;
                        order.NextStep   = null;
                        order.RefOrderId = null;
                    }
                    else
                    {
                        Log.Error("已经在审批中,不能直接取消");
                        throw new InvalidOperationException("已经在审批中,不能直接取消");
                    }

                    //更新假期
                    //RecoverOrder(order.ToModel());
                });

                //添加请假撤销信息,请假为Apply_Leave_Cancel_Application_Message,加班为Apply_OT_Cancel_Application_Message
                var auditMessageEntitry = new AuditMessage()
                {
                    Type        = auditMessageType,
                    UserId      = model.Id,
                    Status      = (int)AuditMessageStatus.Unread,
                    Message     = auditMessageType.ToString(),
                    CreatedTime = DateTime.Now
                };

                dbContext.AuditMessages.Add(auditMessageEntitry);

                //添加流程处理记录
                var operate = new OperateOrderModel()
                {
                    Operation = WorkflowOperation.Cancel,
                    OrderNo   = orderNo,
                    Opinion   = string.Format("{0}取消了申请单。", model.EnglishName)
                };

                _workflowProcessService.AddWorkflowProcess(dbContext, model, operate);

                //更新数据库
                dbContext.SaveChanges();

                return(true);
            }
        }