LuckySpinController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Configuration;
  6. using SuperCms.Extensions;
  7. using SuperAdmin.Source;
  8. using Microsoft.AspNetCore.Http;
  9. using System.IO;
  10. using ReportWeb.Models;
  11. using ReportWeb;
  12. using System.Data;
  13. using Newtonsoft.Json;
  14. using ReportWeb.Source;
  15. using OfficeOpenXml;
  16. using ClosedXML.Excel;
  17. namespace SuperAdmin.Controllers
  18. {
  19. public class LuckySpinController : BaseController
  20. {
  21. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
  22. DbConnector db = new DbConnector();
  23. List<Services> listService;
  24. String sdf = "dd/MM/yyyy HH:mm";
  25. String df = "dd/MM/yyyy";
  26. public LuckySpinController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) : base(_configuration, hostEnvironment)
  27. {
  28. // init
  29. }
  30. // SPIN
  31. public ActionResult PrizeWinner()
  32. {
  33. Users user = HttpContext.Session.GetComplexData<Users>("user");
  34. if (user == null || (user.role <= 0))
  35. {
  36. return Redirect(subDomain + "/Home/Login");
  37. }
  38. else
  39. {
  40. return View();
  41. }
  42. }
  43. [HttpPost]
  44. public JsonResult PrizeWinnerSearch(String msisdn, String period, String fromDate, String toDate)
  45. {
  46. Users user = HttpContext.Session.GetComplexData<Users>("user");
  47. if (user == null || user.role <= 0)
  48. {
  49. ViewBag.username = "Welcome!";
  50. return Json(new
  51. {
  52. error = "10",
  53. content = "Timeout"
  54. });
  55. }
  56. msisdn = validateMsisdn(msisdn);
  57. //if (msisdn == null || msisdn == "")
  58. //{
  59. // return Json(new
  60. // {
  61. // error = "1",
  62. // content = "Enter msisdn"
  63. // });
  64. //}
  65. DateTime startTime = DateTime.ParseExact(fromDate, df, null);
  66. DateTime endTime = DateTime.ParseExact(toDate, df, null);
  67. List<PrizeObj> listPrize = db.GetListPrizeWinner(msisdn, period, startTime, endTime);
  68. return Json(new
  69. {
  70. error = "0",
  71. listPrize = listPrize
  72. });
  73. }
  74. [HttpPost]
  75. public IActionResult PrizeWinnerExport(String msisdn, String period, String fromDate, String toDate)
  76. {
  77. if (!CheckAuthToken())
  78. {
  79. return Json(new
  80. {
  81. error = "-1",
  82. content = "Not allow"
  83. });
  84. }
  85. return ExportPrizeWinner(msisdn, period, fromDate, toDate);
  86. }
  87. private FileContentResult ExportPrizeWinner(String msisdn, String period, String fromDate, String toDate)
  88. {
  89. try
  90. {
  91. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  92. msisdn = validateMsisdn(msisdn);
  93. DateTime startTime = DateTime.ParseExact(fromDate, df, null);
  94. DateTime endTime = DateTime.ParseExact(toDate, df, null);
  95. List<PrizeObj> list = db.GetListPrizeWinner(msisdn, period, startTime, endTime);
  96. string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  97. string fileName = "report_prize_winner.xlsx";
  98. using (var workbook = new XLWorkbook())
  99. {
  100. int i = 1;
  101. IXLWorksheet worksheet = workbook.Worksheets.Add("Report Prize Winner");
  102. worksheet.Column(i).Width = 15;
  103. worksheet.Cell(1, i++).Value = "No.";
  104. worksheet.Column(i).Width = 20;
  105. worksheet.Cell(1, i++).Value = "Phone number";
  106. worksheet.Column(i).Width = 15;
  107. worksheet.Cell(1, i++).Value = "Period";
  108. worksheet.Column(i).Width = 25;
  109. worksheet.Cell(1, i++).Value = "Prize";
  110. worksheet.Column(i).Width = 25;
  111. worksheet.Cell(1, i++).Value = "Spin Time";
  112. worksheet.Column(i).Width = 25;
  113. worksheet.Cell(1, i++).Value = "Process Time";
  114. worksheet.Column(i).Width = 15;
  115. worksheet.Cell(1, i++).Value = "Status";
  116. // make color
  117. worksheet.Row(1).Style.Font.Bold = true;
  118. worksheet.Row(1).Style.Fill.BackgroundColor = XLColor.Yellow;
  119. //worksheet.Cell(1, i++).Value = "BetId";
  120. if (list != null && list.Count > 0)
  121. {
  122. for (int index = 1; index <= list.Count; index++)
  123. {
  124. i = 1;
  125. var news = list[index - 1];
  126. worksheet.Cell(index + 1, i++).Value = index;
  127. worksheet.Cell(index + 1, i++).SetValue(news.msisdn);
  128. worksheet.Cell(index + 1, i++).SetValue(news.period == 1 ? "DAILY" : news.period == 2 ? "WEEKLY" : news.period == 3 ? "MONTHLY" : "PROMOTION");
  129. worksheet.Cell(index + 1, i++).SetValue(news.prize_name2);
  130. worksheet.Cell(index + 1, i++).SetValue(news.code_time.Value.ToString("dd/MM/yyyy HH:mm:ss"));
  131. worksheet.Cell(index + 1, i++).SetValue(news.process_time == null ? "" : news.process_time.Value.ToString("dd/MM/yyyy HH:mm:ss"));
  132. worksheet.Cell(index + 1, i++).SetValue(news.status == 0 ? "Success" : "Failure");
  133. }
  134. }
  135. using (var stream = new MemoryStream())
  136. {
  137. workbook.SaveAs(stream);
  138. var content = stream.ToArray();
  139. return File(content, contentType, fileName);
  140. }
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. return null;
  146. }
  147. }
  148. // SPIN
  149. public ActionResult Ranking()
  150. {
  151. Users user = HttpContext.Session.GetComplexData<Users>("user");
  152. if (user == null || (user.role <= 0))
  153. {
  154. return Redirect(subDomain + "/Home/Login");
  155. }
  156. else
  157. {
  158. return View();
  159. }
  160. }
  161. [HttpPost]
  162. public JsonResult GetRanking(String msisdn, String month)
  163. {
  164. Users user = HttpContext.Session.GetComplexData<Users>("user");
  165. if (user == null || user.role <= 0)
  166. {
  167. ViewBag.username = "Welcome!";
  168. return Json(new
  169. {
  170. error = "10",
  171. content = "Timeout"
  172. });
  173. }
  174. msisdn = validateMsisdn(msisdn);
  175. List<UserCoin> rankingCoin = db.GetRankingCoin(msisdn, month, 100);
  176. SearchModel model = new SearchModel();
  177. model.rankingCoin = rankingCoin;
  178. return Json(new
  179. {
  180. error = "0",
  181. model = model
  182. });
  183. }
  184. [HttpPost]
  185. public IActionResult RankingExport(String msisdn, String month)
  186. {
  187. if (!CheckAuthToken())
  188. {
  189. return Json(new
  190. {
  191. error = "-1",
  192. content = "Not allow"
  193. });
  194. }
  195. return ExportRanking(msisdn, month);
  196. }
  197. private FileContentResult ExportRanking(String msisdn, String month)
  198. {
  199. try
  200. {
  201. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  202. msisdn = validateMsisdn(msisdn);
  203. msisdn = validateMsisdn(msisdn);
  204. List<UserCoin> list = db.GetRankingCoin(msisdn, month, 0);
  205. string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  206. string fileName = "ranking.xlsx";
  207. using (var workbook = new XLWorkbook())
  208. {
  209. int i = 1;
  210. IXLWorksheet worksheet = workbook.Worksheets.Add("Ranking");
  211. worksheet.Column(i).Width = 15;
  212. worksheet.Cell(1, i++).Value = "No.";
  213. worksheet.Column(i).Width = 20;
  214. worksheet.Cell(1, i++).Value = "Phone number";
  215. worksheet.Column(i).Width = 15;
  216. worksheet.Cell(1, i++).Value = "Total coin";
  217. worksheet.Column(i).Width = 20;
  218. worksheet.Cell(1, i++).Value = "Month";
  219. worksheet.Column(i).Width = 20;
  220. worksheet.Cell(1, i++).Value = "Package";
  221. worksheet.Column(i).Width = 25;
  222. worksheet.Cell(1, i++).Value = "Last Update";
  223. // make color
  224. worksheet.Row(1).Style.Font.Bold = true;
  225. worksheet.Row(1).Style.Fill.BackgroundColor = XLColor.Yellow;
  226. //worksheet.Cell(1, i++).Value = "BetId";
  227. if (list != null && list.Count > 0)
  228. {
  229. for (int index = 1; index <= list.Count; index++)
  230. {
  231. i = 1;
  232. var news = list[index - 1];
  233. worksheet.Cell(index + 1, i++).SetValue(news.rank);
  234. worksheet.Cell(index + 1, i++).SetValue(news.msisdn);
  235. worksheet.Cell(index + 1, i++).SetValue(news.total_coin);
  236. worksheet.Cell(index + 1, i++).SetValue(month);
  237. worksheet.Cell(index + 1, i++).SetValue(news.product_name);
  238. worksheet.Cell(index + 1, i++).SetValue(news.last_update.Value.ToString("dd/MM/yyyy HH:mm:ss"));
  239. }
  240. }
  241. using (var stream = new MemoryStream())
  242. {
  243. workbook.SaveAs(stream);
  244. var content = stream.ToArray();
  245. return File(content, contentType, fileName);
  246. }
  247. }
  248. }
  249. catch (Exception ex)
  250. {
  251. return null;
  252. }
  253. }
  254. }
  255. }