Beispiel #1
0
        public static void ProcessInvoiceToGov()
        {
            if (Interlocked.Increment(ref __InvoiceBusyCount) == 1)
            {
                ThreadPool.QueueUserWorkItem(t =>
                {
                    try
                    {
                        using (var models = new ModelSource <UserProfile>())
                        {
                            do
                            {
                                IQueryable <InvoiceItem> items = models.GetTable <InvoiceItemDispatch>()
                                                                 .Select(d => d.InvoiceItem);
                                if (items.Count() > 0)
                                {
                                    foreach (var item in items.ToArray())
                                    {
                                        String fileName = Path.Combine(C0401Outbound, item.TrackCode + item.No + ".xml");
                                        item.CreateC0401().ConvertToXml().Save(fileName);
                                        models.ExecuteCommand("delete InvoiceItemDispatch where InvoiceID={0}", item.InvoiceID);
                                    }
                                }

                                var cancelledItems = models.GetTable <InvoiceCancellationDispatch>()
                                                     .Select(d => d.InvoiceCancellation);
                                if (cancelledItems.Count() > 0)
                                {
                                    foreach (var item in cancelledItems.Select(c => c.InvoiceItem).ToArray())
                                    {
                                        String fileName = Path.Combine(C0501Outbound, item.TrackCode + item.No + ".xml");
                                        item.CreateC0501().ConvertToXml().Save(fileName);
                                        models.ExecuteCommand("delete InvoiceCancellationDispatch where InvoiceID={0}", item.InvoiceID);
                                    }
                                }

                                var allowanceItems = models.GetTable <InvoiceAllowanceDispatch>()
                                                     .Select(d => d.InvoiceAllowance);
                                if (allowanceItems.Count() > 0)
                                {
                                    foreach (var item in allowanceItems.ToArray())
                                    {
                                        String fileName = Path.Combine(D0401Outbound, item.AllowanceNumber + ".xml");
                                        item.CreateD0401().ConvertToXml().Save(fileName);
                                        models.ExecuteCommand("delete InvoiceAllowanceDispatch where AllowanceID={0}", item.AllowanceID);
                                    }
                                }
                            } while (Interlocked.Decrement(ref __InvoiceBusyCount) > 0);
                        }
                    }
                    catch (Exception ex)
                    {
                        ApplicationLogging.LoggerFactory.CreateLogger(typeof(TaskExtensionMethods))
                        .LogError(ex, ex.Message);
                    }
                });
            }
        }
Beispiel #2
0
        public static void UpdateExerciseGameContestant(this ExerciseGameContestant contestant)
        {
            if (contestant == null)
            {
                return;
            }

            ThreadPool.QueueUserWorkItem(t =>
            {
                try
                {
                    using (var models = new ModelSource <UserProfile>())
                    {
                        if (contestant.Status == (int)Naming.GeneralStatus.Failed)
                        {
                            models.ExecuteCommand(@"UPDATE ExerciseGameRank
                                    SET        RankingScore = NULL, Rank = NULL
                                    WHERE   (UID = {0})", contestant.UID);
                        }

                        foreach (var item in models.GetTable <ExerciseGameItem>())
                        {
                            models.RefreshExerciseGameRank(item.ExerciseID);
                        }

                        models.RefreshPersonalRank();
                    }
                }
                catch (Exception ex)
                {
                    ApplicationLogging.LoggerFactory.CreateLogger(typeof(ExerciseGameExtensionMethods))
                    .LogError(ex, ex.Message);
                }
            });
        }
Beispiel #3
0
 public static void InitializeSystemAnnouncement <TEntity>(this UserProfile profile, ModelSource <TEntity> models)
     where TEntity : class, new()
 {
     models.ExecuteCommand(@"INSERT INTO UserEvent
                             (UID, Title, StartDate, EndDate, SystemEventID)
                 SELECT  {0}, Title, StartDate, EndDate, EventID
                 FROM     SystemEventBulletin 
                 where EndDate >= {1} And (EventID NOT IN
                    (SELECT  SystemEventID
                    FROM     UserEvent
                    WHERE   (UID = {0})))", profile.UID, DateTime.Today);
 }
 public void DoJob()
 {
     using (ModelSource <UserProfile> models = new ModelSource <UserProfile>())
     {
         try
         {
             models.ExecuteCommand(@"UPDATE CourseContract
                     SET                Status = {0}
                     WHERE        (Status = {1}) AND (Expiration < {2})
                     AND (NOT EXISTS (SELECT NULL 
                        FROM CourseContractRevision
                        WHERE (CourseContract.ContractID = RevisionID)))",
                                   (int)Naming.CourseContractStatus.已過期, (int)Naming.CourseContractStatus.已生效, DateTime.Today);
         }
         catch (Exception ex)
         {
             Logger.Error(ex);
         }
     }
 }
        public static void RefreshPersonalRank <TEntity>(this ModelSource <TEntity> models)
            where TEntity : class, new()
        {
            int rankIndex;

            models.ExecuteCommand("DELETE FROM ExerciseGamePersonalRank");
            rankIndex = 1;
            var personalRank = models.GetTable <ExerciseGamePersonalRank>();

            foreach (var p in models.GetTable <ExerciseGameRank>()
                     .Where(r => r.RankingScore.HasValue)
                     .GroupBy(r => r.UID).Select(g => new { UID = g.Key, TotalScore = g.Sum(p => p.RankingScore) })
                     .GroupBy(s => s.TotalScore)
                     .OrderByDescending(s => s.Key))
            {
                foreach (var rank in p)
                {
                    var item = models.GetTable <ExerciseGameContestant>().Where(c => c.UID == rank.UID).First();
                    item.TotalScope = p.Key;
                    item.Rank       = rankIndex;
                    item.ExerciseGamePersonalRank = new ExerciseGamePersonalRank
                    {
                        TotalScope = p.Key.Value,
                        Rank       = rankIndex
                    };

                    //personalRank.InsertOnSubmit(new ExerciseGamePersonalRank
                    //{
                    //    UID = rank.UID,
                    //    TotalScope = p.Key.Value,
                    //    Rank = rankIndex
                    //});
                }
                rankIndex++;
            }

            models.SubmitChanges();
        }
        public static void RefreshExerciseGameContestant <TEntity>(this ModelSource <TEntity> models, ExerciseGameContestant contestant)
            where TEntity : class, new()
        {
            try
            {
                if (contestant != null && contestant.Status == (int)Naming.GeneralStatus.Failed)
                {
                    models.ExecuteCommand(@"UPDATE ExerciseGameRank
                                    SET        RankingScore = NULL, Rank = NULL
                                    WHERE   (UID = {0})", contestant.UID);
                }

                foreach (var item in models.GetTable <ExerciseGameItem>())
                {
                    models.RefreshExerciseGameRank(item.ExerciseID);
                }

                models.RefreshPersonalRank();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }
Beispiel #7
0
        public static void ExecuteLessonPerformanceSettlement <TEntity>(this ModelSource <TEntity> models, DateTime startDate, DateTime endExclusiveDate)
            where TEntity : class, new()
        {
            var settlement = models.GetTable <Settlement>().Where(s => startDate <= s.SettlementDate && endExclusiveDate > s.SettlementDate).FirstOrDefault();

            if (settlement == null)
            {
                return;
            }

            LessonTimeAchievementHelper <TEntity> helper = new LessonTimeAchievementHelper <TEntity>(models)
            {
                LessonItems = models.GetTable <V_Tuition>().Where(l => l.ClassTime >= settlement.StartDate && l.ClassTime < settlement.EndExclusiveDate)
            };

            foreach (var item in helper.SettlementFullAchievement)
            {
                models.ExecuteCommand("update LessonTimeSettlement set SettlementStatus = {0},SettlementID = {1} where LessonID={2}", (int)Naming.LessonSettlementStatus.FullAchievement, settlement.SettlementID, item.LessonID);
            }

            foreach (var item in helper.SettlementHalfAchievement)
            {
                models.ExecuteCommand("update LessonTimeSettlement set SettlementStatus = {0},SettlementID = {1} where LessonID={2}", (int)Naming.LessonSettlementStatus.HalfAchievement, settlement.SettlementID, item.LessonID);
            }

            foreach (var item in helper.SettlementVainAchievement)
            {
                models.ExecuteCommand("update LessonTimeSettlement set SettlementID = {0} where LessonID={1}", settlement.SettlementID, item.LessonID);
            }

            var coachItems     = models.PromptEffectiveCoach();
            var salaryTable    = models.GetTable <CoachMonthlySalary>();
            var countableItems = helper.PerformanceCountableLesson;

            var paymentItems = models.GetTable <Payment>().Where(p => p.PayoffDate >= settlement.StartDate && p.PayoffDate < settlement.EndExclusiveDate);
            //.FilterByEffective();
            IQueryable <TuitionAchievement> achievementItems = paymentItems.GetPaymentAchievement(models);

            foreach (var coach in coachItems)
            {
                var lessonItem = countableItems.Where(l => l.AttendingCoach == coach.CoachID).FirstOrDefault();
                var salary     = salaryTable.Where(s => s.CoachID == coach.CoachID && s.SettlementID == settlement.SettlementID).FirstOrDefault();
                if (salary == null)
                {
                    salary = new CoachMonthlySalary
                    {
                        CoachID      = coach.CoachID,
                        SettlementID = settlement.SettlementID,
                    };
                    salaryTable.InsertOnSubmit(salary);
                }

                if (coach.CoachWorkplace.Count == 1)
                {
                    salary.WorkPlace = coach.CoachWorkplace.First().BranchID;
                }

                if (lessonItem != null)
                {
                    salary.LevelID    = lessonItem.ProfessionalLevelID;
                    salary.GradeIndex = lessonItem.MarkedGradeIndex ?? 0;
                }
                else
                {
                    salary.LevelID    = coach.LevelID ?? 0;
                    salary.GradeIndex = coach.ProfessionalLevel?.GradeIndex ?? 0;
                }

                var performanceItems = achievementItems.Where(t => t.CoachID == coach.CoachID);
                salary.PerformanceAchievement = performanceItems.Sum(t => t.ShareAmount) ?? 0;

                models.SubmitChanges();

                void calcCoachAchievement()
                {
                    var attendingLessons = countableItems.Where(l => l.AttendingCoach == coach.CoachID);

                    foreach (var g in attendingLessons.GroupBy(l => l.BranchID))
                    {
                        var branchBonus = salary.CoachBranchMonthlyBonus.Where(b => b.BranchID == g.Key).FirstOrDefault();
                        if (branchBonus == null)
                        {
                            branchBonus = new CoachBranchMonthlyBonus
                            {
                                CoachMonthlySalary = salary,
                                BranchID           = g.Key.Value
                            };
                            salary.CoachBranchMonthlyBonus.Add(branchBonus);
                        }

                        LessonTimeAchievementHelper <TEntity> branchHelper = new LessonTimeAchievementHelper <TEntity>(models)
                        {
                            LessonItems = attendingLessons.Where(l => l.BranchID == g.Key)
                        };

                        branchBonus.AchievementAttendanceCount = branchHelper.LessonItems.Count()
                                                                 - branchHelper.SettlementPILesson.Count() / 2m;

                        branchBonus.Tuition = branchHelper.LessonItems.CalcTuition(models);

                        branchBonus.AttendanceBonus = branchHelper.LessonItems.CalcTuitionShare(models);

                        models.SubmitChanges();
                    }

                    var     attendanceCount = salary.CoachBranchMonthlyBonus.Sum(b => b.AchievementAttendanceCount);
                    decimal shareRatio      = 3.5m;

                    for (int i = 0; i < PerformanceAchievementIndex.Length; i++)
                    {
                        if (salary.PerformanceAchievement >= PerformanceAchievementIndex[i])
                        {
                            shareRatio += ShareRatioIncrementForPerformance[i];
                            break;
                        }
                    }
                    for (int i = 0; i < AttendingLessonIndex.Length; i++)
                    {
                        if (attendanceCount >= AttendingLessonIndex[i])
                        {
                            shareRatio += ShareRatioIncrementForAttendance[i];
                            break;
                        }
                    }

                    salary.AchievementShareRatio = shareRatio;
                    models.SubmitChanges();
                };

                BranchStore branch;
                if (coach.UserProfile.IsOfficer())
                {
                    foreach (var g in countableItems.GroupBy(l => l.BranchID))
                    {
                        var branchBonus = salary.CoachBranchMonthlyBonus.Where(b => b.BranchID == g.Key).FirstOrDefault();
                        if (branchBonus == null)
                        {
                            branchBonus = new CoachBranchMonthlyBonus
                            {
                                CoachMonthlySalary = salary,
                                BranchID           = g.Key.Value
                            };
                            salary.CoachBranchMonthlyBonus.Add(branchBonus);
                        }

                        branchBonus.BranchTotalAttendanceCount = g.Count();
                        branchBonus.BranchTotalTuition         = g.CalcTuition(models);
                        models.SubmitChanges();
                    }

                    calcCoachAchievement();
                }
                else if ((branch = models.GetTable <BranchStore>().Where(b => b.ManagerID == coach.CoachID || b.ViceManagerID == coach.CoachID).FirstOrDefault()) != null)
                {
                    var branchBonus = salary.CoachBranchMonthlyBonus.Where(b => b.BranchID == branch.BranchID).FirstOrDefault();
                    if (branchBonus == null)
                    {
                        branchBonus = new CoachBranchMonthlyBonus
                        {
                            CoachMonthlySalary = salary,
                            BranchID           = branch.BranchID
                        };
                        salary.CoachBranchMonthlyBonus.Add(branchBonus);
                    }
                    var branchItems = countableItems.Where(w => w.WorkPlace == branch.BranchID);
                    branchBonus.BranchTotalAttendanceCount = branchItems.Count();
                    branchBonus.BranchTotalTuition         = branchItems.CalcTuition(models);
                    models.SubmitChanges();

                    calcCoachAchievement();
                }
                else
                {
                    calcCoachAchievement();
                }
            }
        }
        public static void RefreshExerciseGameRank <TEntity>(this ModelSource <TEntity> models, int exerciseID)
            where TEntity : class, new()
        {
            var exerciseItem = models.GetTable <ExerciseGameItem>().Where(r => r.ExerciseID == exerciseID).First();

            var table = models.GetTable <ExerciseGameRank>();

            models.ExecuteCommand(@"
                                UPDATE ExerciseGameRank
                                SET        RankingScore = NULL, Rank = NULL
                                WHERE   (ExerciseID = {0})", exerciseID);

            models.ExecuteCommand(@"
                                UPDATE ExerciseGameRank
                                SET        RecordID = NULL
                                FROM     ExerciseGameRank INNER JOIN
                                               ExerciseGameResult ON ExerciseGameRank.RecordID = ExerciseGameResult.TestID
                                WHERE   (ExerciseGameRank.ExerciseID = {0}) AND (ExerciseGameResult.TestDate < {1})",
                                  exerciseID, DateTime.Today.AddMonths(-3));

            IOrderedQueryable <IGrouping <decimal, ExerciseGameRank> > items;

            if (exerciseItem.Descending == false)
            {
                items = table
                        .Join(models.GetTable <ExerciseGameContestant>().Where(c => c.Status == (int)Naming.GeneralStatus.Successful),
                              r => r.UID, c => c.UID, (r, c) => r)
                        .Where(r => r.ExerciseID == exerciseID && r.RecordID.HasValue)
                        .OrderBy(r => r.ExerciseGameResult.Score) //.Take(10)
                        .GroupBy(r => r.ExerciseGameResult.Score)
                        .OrderBy(g => g.Key);
            }
            else
            {
                items = table
                        .Join(models.GetTable <ExerciseGameContestant>().Where(c => c.Status == (int)Naming.GeneralStatus.Successful),
                              r => r.UID, c => c.UID, (r, c) => r)
                        .Where(r => r.ExerciseID == exerciseID && r.RecordID.HasValue)
                        .OrderByDescending(r => r.ExerciseGameResult.Score) //.Take(10)
                        .GroupBy(r => r.ExerciseGameResult.Score)
                        .OrderByDescending(g => g.Key);
            }

            //int score = Math.Min(10, models.GetTable<ExerciseGameContestant>().Where(c => c.Status == (int)Naming.GeneralStatus.Successful).Count());
            int score = models.GetTable <ExerciseGameContestant>().Where(c => c.Status == (int)Naming.GeneralStatus.Successful).Count();
            int range, rankIndex = 1;

            foreach (var g in items)
            {
                range = 0;
                foreach (var rank in g)
                {
                    rank.RankingScore = score;
                    rank.Rank         = rankIndex;
                    range++;
                }
                score -= range;
                rankIndex++;
            }
            models.SubmitChanges();
        }