//using System.Data; //using Common; //using Database; //using Database.Database; //using GuesssongNC.Worker.Utils; //using Quartz; //namespace GuesssongNC.Worker; //class MonthlyPrizeScheduler : IJob //{ // private static readonly log4net.ILog log = log4net.LogManager.GetLogger( // typeof(MonthlyPrizeScheduler) // ); // IConfiguration configuration; // ModelContext dbContext; // public MonthlyPrizeScheduler(IConfiguration _configuration) // { // configuration = _configuration; // dbContext = new DbHelper().GetDbContext(configuration.GetSection("Connection").Value!); // } // public async Task Execute(IJobExecutionContext context) // { // log.Info("Start MonthlyPrizeScheduler ..."); // try // { // DateTime now = DateTime.Now; // var dayOfMonth = now.Day; // if (dayOfMonth != 1) // { // return; // } // List> timeMonthly = CommonLogic.GetTimeMonthly(1); // var monthlyTerm = dbContext // .Terms.Where( // x => // x.Type == CommonConstants.Monthly // && x.Status == CommonConstants.ActiveInt // && x.DrawTime >= timeMonthly[0][0] // && x.DrawTime <= timeMonthly[0][1] // ) // .FirstOrDefault(); // if (monthlyTerm == null) // { // log.Info("No monthly term found."); // monthlyTerm = new Term() // { // Id = (decimal)await DbLogic.GenIdAsync(dbContext, "term_seq"), // Name = "Weekly Term", // Code = CommonConstants.Monthly, // FromTime = timeMonthly[0][0], // ToTime = timeMonthly[0][1], // DrawTime = timeMonthly[0][1].AddMinutes(-30), // Status = CommonConstants.ActiveInt, // Type = CommonConstants.Monthly // }; // dbContext.Terms.Add(monthlyTerm); // await dbContext.SaveChangesAsync(); // } // // check highest point // var termUserPrizeHis = dbContext // .TermUserPrizes.Where( // x => // x.TimeCalculate >= monthlyTerm.FromTime // && x.TimeCalculate <= monthlyTerm.ToTime // ) // .Count(); // if (termUserPrizeHis > 0) // { // log.Info($"Term {monthlyTerm.Id} has {termUserPrizeHis} prize"); // return; // } // // get point // var userHighestPoint = dbContext // .PlayTransactions.Where( // x => // x.Status == CommonConstants.Submited // && x.TimeSubmit > monthlyTerm.FromTime // && x.TimeSubmit < monthlyTerm.ToTime // ) // .GroupBy(x => x.Msisdn) // .Select(x => new { Msisdn = x.Key, Points = x.Sum(y => y.TotalPoint) }) // .OrderByDescending(x => x.Points) // .Skip(0) // .Take(10) // .ToList(); // var prizes = dbContext // .Prizes.Where(x => x.Type == CommonConstants.MonthlyPrize) // .OrderBy(x => x.Priority) // .ToList(); // for (int i = 0; i < prizes.Count(); i++) // { // var prize = prizes[i]; // if (userHighestPoint.Count <= i) // { // continue; // } // var user = userHighestPoint[i]; // TermUserPrize termUserPrize = new TermUserPrize() // { // Id = (decimal)await DbLogic.GenIdAsync(dbContext, "term_user_prize_seq"), // Msisdn = user.Msisdn, // Priority = prize.Priority, // TermId = monthlyTerm.Id, // PrizeId = prize.Id, // TimeCalculate = DateTime.Now, // Status = CommonErrorCode.SUCCESS, // TotalPoints = user.Points, // }; // dbContext.TermUserPrizes.Add(termUserPrize); // await dbContext.SaveChangesAsync(); // // send sms to user // var msg = dbContext // .Configs.Where( // x => x.ParamName == (CommonConstants.Monthly + "_" + prize.Amount) // ) // .FirstOrDefault(); // if (msg != null) // { // await CommonLogic.SendMtToUserAsync(dbContext, user.Msisdn, msg.ParamValueFr); // } // } // } // catch (Exception exception) // { // log.Error("Exception: ", exception); // } // } //}