LuckySpinController.cs 11 KB

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