using System; using System.Collections.Generic; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using SuperCms.Extensions; using SuperAdmin.Source; using Microsoft.AspNetCore.Http; using System.IO; using ReportWeb.Models; using ReportWeb; using System.Data; using Newtonsoft.Json; using ReportWeb.Source; using OfficeOpenXml; using ClosedXML.Excel; using ReportWebCore.Models.Http; namespace SuperAdmin.Controllers { public class LuckySpinController : BaseController { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); DbConnector db = new DbConnector(); List listService; String sdf = "dd/MM/yyyy HH:mm"; String df = "dd/MM/yyyy"; public LuckySpinController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) : base(_configuration, hostEnvironment) { // init } // SPIN public ActionResult PrizeWinner() { Users user = HttpContext.Session.GetComplexData("user"); if (user == null || (user.role <= 0)) { return Redirect(subDomain + "/Home/Login"); } else { return View(); } } [HttpPost] public JsonResult PrizeWinnerSearch(String msisdn, String period, String fromDate, String toDate) { Users user = HttpContext.Session.GetComplexData("user"); if (user == null || user.role <= 0) { ViewBag.username = "Welcome!"; return Json(new { error = "10", content = "Timeout" }); } msisdn = validateMsisdn(msisdn); //if (msisdn == null || msisdn == "") //{ // return Json(new // { // error = "1", // content = "Enter msisdn" // }); //} DateTime startTime = DateTime.ParseExact(fromDate, df, null); DateTime endTime = DateTime.ParseExact(toDate, df, null); List listPrize = db.GetListPrizeWinner(msisdn, period, startTime, endTime); return Json(new { error = "0", listPrize = listPrize }); } [HttpPost] public IActionResult PrizeWinnerExport(String msisdn, String period, String fromDate, String toDate) { if (!CheckAuthToken()) { return Json(new { error = "-1", content = "Not allow" }); } return ExportPrizeWinner(msisdn, period, fromDate, toDate); } private FileContentResult ExportPrizeWinner(String msisdn, String period, String fromDate, String toDate) { try { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; msisdn = validateMsisdn(msisdn); DateTime startTime = DateTime.ParseExact(fromDate, df, null); DateTime endTime = DateTime.ParseExact(toDate, df, null); List list = db.GetListPrizeWinner(msisdn, period, startTime, endTime); string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; string fileName = "report_prize_winner.xlsx"; using (var workbook = new XLWorkbook()) { int i = 1; IXLWorksheet worksheet = workbook.Worksheets.Add("Report Prize Winner"); worksheet.Column(i).Width = 15; worksheet.Cell(1, i++).Value = "No."; worksheet.Column(i).Width = 20; worksheet.Cell(1, i++).Value = "Phone number"; worksheet.Column(i).Width = 15; worksheet.Cell(1, i++).Value = "Period"; worksheet.Column(i).Width = 25; worksheet.Cell(1, i++).Value = "Prize"; worksheet.Column(i).Width = 25; worksheet.Cell(1, i++).Value = "Spin Time"; worksheet.Column(i).Width = 25; worksheet.Cell(1, i++).Value = "Process Time"; worksheet.Column(i).Width = 15; worksheet.Cell(1, i++).Value = "Status"; // make color worksheet.Row(1).Style.Font.Bold = true; worksheet.Row(1).Style.Fill.BackgroundColor = XLColor.Yellow; //worksheet.Cell(1, i++).Value = "BetId"; if (list != null && list.Count > 0) { for (int index = 1; index <= list.Count; index++) { i = 1; var news = list[index - 1]; worksheet.Cell(index + 1, i++).Value = index; worksheet.Cell(index + 1, i++).SetValue(news.msisdn); worksheet.Cell(index + 1, i++).SetValue(news.period == 1 ? "DAILY" : news.period == 2 ? "WEEKLY" : news.period == 3 ? "MONTHLY" : "PROMOTION"); worksheet.Cell(index + 1, i++).SetValue(news.prize_name2); worksheet.Cell(index + 1, i++).SetValue(news.code_time.Value.ToString("dd/MM/yyyy HH:mm:ss")); worksheet.Cell(index + 1, i++).SetValue(news.process_time == null ? "" : news.process_time.Value.ToString("dd/MM/yyyy HH:mm:ss")); worksheet.Cell(index + 1, i++).SetValue(news.status == 0 ? "Success" : "Failure"); } } using (var stream = new MemoryStream()) { workbook.SaveAs(stream); var content = stream.ToArray(); return File(content, contentType, fileName); } } } catch (Exception ex) { return null; } } // SPIN public ActionResult Ranking() { Users user = HttpContext.Session.GetComplexData("user"); if (user == null || (user.role <= 0)) { return Redirect(subDomain + "/Home/Login"); } else { return View(); } } [HttpPost] public JsonResult GetRanking(String msisdn, String month) { Users user = HttpContext.Session.GetComplexData("user"); if (user == null || user.role <= 0) { ViewBag.username = "Welcome!"; return Json(new { error = "10", content = "Timeout" }); } msisdn = validateMsisdn(msisdn); List rankingCoin = db.GetRankingCoin(msisdn, month, 100); SearchModel model = new SearchModel(); model.rankingCoin = rankingCoin; return Json(new { error = "0", model = model }); } [HttpPost] public IActionResult RankingExport(String msisdn, String month) { if (!CheckAuthToken()) { return Json(new { error = "-1", content = "Not allow" }); } return ExportRanking(msisdn, month); } private FileContentResult ExportRanking(String msisdn, String month) { try { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; msisdn = validateMsisdn(msisdn); msisdn = validateMsisdn(msisdn); List list = db.GetRankingCoin(msisdn, month, 0); string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; string fileName = "ranking.xlsx"; using (var workbook = new XLWorkbook()) { int i = 1; IXLWorksheet worksheet = workbook.Worksheets.Add("Ranking"); worksheet.Column(i).Width = 15; worksheet.Cell(1, i++).Value = "No."; worksheet.Column(i).Width = 20; worksheet.Cell(1, i++).Value = "Phone number"; worksheet.Column(i).Width = 15; worksheet.Cell(1, i++).Value = "Total coin"; worksheet.Column(i).Width = 20; worksheet.Cell(1, i++).Value = "Month"; worksheet.Column(i).Width = 20; worksheet.Cell(1, i++).Value = "Package"; worksheet.Column(i).Width = 25; worksheet.Cell(1, i++).Value = "Last Update"; // make color worksheet.Row(1).Style.Font.Bold = true; worksheet.Row(1).Style.Fill.BackgroundColor = XLColor.Yellow; //worksheet.Cell(1, i++).Value = "BetId"; if (list != null && list.Count > 0) { for (int index = 1; index <= list.Count; index++) { i = 1; var news = list[index - 1]; worksheet.Cell(index + 1, i++).SetValue(news.rank); worksheet.Cell(index + 1, i++).SetValue(news.msisdn); worksheet.Cell(index + 1, i++).SetValue(news.total_coin); worksheet.Cell(index + 1, i++).SetValue(month); worksheet.Cell(index + 1, i++).SetValue(news.product_name); worksheet.Cell(index + 1, i++).SetValue(news.last_update.Value.ToString("dd/MM/yyyy HH:mm:ss")); } } using (var stream = new MemoryStream()) { workbook.SaveAs(stream); var content = stream.ToArray(); return File(content, contentType, fileName); } } } catch (Exception ex) { return null; } } [HttpPost] public JsonResult AddMoneyRanking(String id) { Users user = HttpContext.Session.GetComplexData("user"); if (user == null || user.role <= 0) { ViewBag.username = "Welcome!"; return Json(new { error = "10", content = "Timeout" }); } ErrResponse dbRes = db.ProcessPrizeTop(long.Parse(id)); return Json(new { error = dbRes.error, content = dbRes.message }); } } }