public async Task DisableIt(PhaseCategoryAdapterModel paraObject)
        {
            await Task.Delay(100);

            PhaseCategory curritem = await context.PhaseCategory
                                     .AsNoTracking()
                                     .FirstOrDefaultAsync(x => x.Id == paraObject.Id);

            CleanTrackingHelper.Clean <PhaseCategory>(context);
            curritem.Enable = false;
            //context.Entry(curritem).State = EntityState.Modified;
            context.Update(curritem);
            await context.SaveChangesAsync();

            CleanTrackingHelper.Clean <PhaseCategory>(context);
            return;
        }
        async Task CountingWorkingHour(int WorkingLogId)
        {
            CleanTrackingHelper.Clean <WorkingLog>(context);
            var totalHours = await context.WorkingLogDetail
                             .AsNoTracking()
                             .Where(x => x.WorkingLogId == WorkingLogId)
                             .SumAsync(x => x.Hours);

            var item = await context.WorkingLog
                       .AsNoTracking()
                       .FirstOrDefaultAsync(x => x.Id == WorkingLogId);

            if (item != null)
            {
                item.TotalHours = totalHours;
                context.Update(item);
                await context.SaveChangesAsync();

                CleanTrackingHelper.Clean <WorkingLog>(context);
            }
        }
        async Task CountingExpense(int TravelExpenseId)
        {
            CleanTrackingHelper.Clean <TravelExpense>(context);
            var totalHours = await context.TravelExpenseDetail
                             .AsNoTracking()
                             .Where(x => x.TravelExpenseId == TravelExpenseId)
                             .SumAsync(x => x.Expense);

            var item = await context.TravelExpense
                       .AsNoTracking()
                       .FirstOrDefaultAsync(x => x.Id == TravelExpenseId);

            if (item != null)
            {
                item.TotalExpense = totalHours;
                context.Update(item);
                await context.SaveChangesAsync();

                CleanTrackingHelper.Clean <TravelExpense>(context);
            }
        }
        public async Task CheckPasswordAge(CancellationToken cancellationToken)
        {
            CleanTrackingHelper.Clean <AccountPolicy>(context);
            CleanTrackingHelper.Clean <MyUser>(context);

            AccountPolicy AccountPolicy = await context.AccountPolicy
                                          .OrderBy(x => x.Id)
                                          .FirstOrDefaultAsync();

            cancellationToken.ThrowIfCancellationRequested();
            List <MyUser> myUsers = await context.MyUser
                                    .ToListAsync();

            cancellationToken.ThrowIfCancellationRequested();

            var enableCheckPasswordAge = AccountPolicy.EnableCheckPasswordAge;
            var passwordAge            = AccountPolicy.PasswordAge;

            if (enableCheckPasswordAge == true)
            {
                foreach (var item in myUsers)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if (DateTime.Now > item.ForceChangePasswordDatetime)
                    {
                        #region 該使用者已經達到要變更密碼的時間
                        item.ForceChangePasswordDatetime = DateTime.Now.AddDays(passwordAge);
                        item.ForceChangePassword         = true;
                        context.Update(item);
                        await context.SaveChangesAsync();

                        #endregion
                    }
                }
            }

            CleanTrackingHelper.Clean <MyUser>(context);
            CleanTrackingHelper.Clean <AccountPolicy>(context);
        }
        public async Task AgreeAsync(FlowMasterAdapterModel flowMasterAdapterModel,
                                     ApproveOpinionModel approveOpinionModel)
        {
            CleanTrackingHelper.Clean <FlowMaster>(context);
            CleanTrackingHelper.Clean <FlowUser>(context);
            CleanTrackingHelper.Clean <FlowHistory>(context);
            CleanTrackingHelper.Clean <WorkOrder>(context);
            CleanTrackingHelper.Clean <MyUser>(context);

            var thisLevel = flowMasterAdapterModel.ProcessLevel;

            (var flowUsers, var user) = await GetUsersDataAsync(flowMasterAdapterModel);

            if (CheckCurrentActionUser(flowUsers, user, flowMasterAdapterModel) == false)
            {
                return;
            }

            var flowUserCurrentLevel = flowUsers.Where(x => x.Level == flowMasterAdapterModel.ProcessLevel).ToList();
            var currentUser          = flowUserCurrentLevel.FirstOrDefault(x => x.MyUserId == user.Id);

            if (currentUser != null)
            {
                currentUser.Completion = true;
            }
            var remindUsers = flowUsers
                              .Where(x => x.Level == flowMasterAdapterModel.ProcessLevel &&
                                     x.Completion == false).ToList();
            bool allProcessing = (remindUsers.Count() > 0) ? false : true;

            if (allProcessing)
            {
                #region 是否是最後一關關卡
                var lastLevel = flowUsers.OrderByDescending(x => x.Level)
                                .FirstOrDefault();
                if (flowMasterAdapterModel.ProcessLevel == lastLevel.Level)
                {
                    flowMasterAdapterModel.Status = 99;
                    flowMasterAdapterModel.ProcessLevel++;
                    #region 若不是直接申請,則需要同步更新來源紀錄的狀態碼
                    if (flowMasterAdapterModel.SourceType == FlowSourceTypeEnum.WorkOrder)
                    {
                        if (string.IsNullOrEmpty(flowMasterAdapterModel.SourceCode) == false)
                        {
                            var workOrder = await context.WorkOrder
                                            .AsNoTracking()
                                            .FirstOrDefaultAsync(x => x.Code == flowMasterAdapterModel.SourceCode);

                            if (workOrder != null)
                            {
                                workOrder.Status = MagicHelper.WorkOrderStatus結案;
                                context.Update(workOrder);
                                await context.SaveChangesAsync();
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    #region 還有關卡要繼續審核
                    flowMasterAdapterModel.Status = 1;
                    flowMasterAdapterModel.ProcessLevel++;

                    CopyUserAutoCompletion(flowUsers, flowMasterAdapterModel.ProcessLevel);
                    #endregion
                }
                #endregion
            }

            FindNextActionUser(flowUsers, flowMasterAdapterModel);

            await context.BulkUpdateAsync(flowUsers);
            await UpdateAsync(flowMasterAdapterModel);

            await AddHistoryRecord(user, flowMasterAdapterModel,
                                   $"{approveOpinionModel.Summary}", $"{approveOpinionModel.Comment}", true);

            if (thisLevel != flowMasterAdapterModel.ProcessLevel &&
                flowMasterAdapterModel.Status != 99)
            {
                #region 產生收件匣紀錄
                await NotifyInboxUsers(flowUsers, flowMasterAdapterModel, flowMasterAdapterModel.ProcessLevel);

                #endregion
            }

            CleanTrackingHelper.Clean <FlowMaster>(context);
            CleanTrackingHelper.Clean <FlowUser>(context);
            CleanTrackingHelper.Clean <WorkOrder>(context);
            CleanTrackingHelper.Clean <FlowHistory>(context);
            CleanTrackingHelper.Clean <MyUser>(context);
        }