ContentBusinessImpl.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using Common;
  2. using Common.Constant;
  3. using Common.Http;
  4. using Common.Logic;
  5. using Esim.Apis.DTO.Content;
  6. using Esim.Apis.Singleton;
  7. using Database.Database;
  8. using log4net;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Newtonsoft.Json;
  11. namespace Esim.Apis.Business
  12. {
  13. public class ContentBusinessImpl : IContentBusiness
  14. {
  15. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
  16. typeof(ContentBusinessImpl)
  17. );
  18. private ModelContext dbContext;
  19. IConfiguration configuration;
  20. public ContentBusinessImpl(ModelContext _dbContext, IConfiguration _configuration)
  21. {
  22. dbContext = _dbContext;
  23. configuration = _configuration;
  24. }
  25. public async Task<IActionResult> BannerLoad(HttpRequest httpRequest, BannerLoadReq request)
  26. {
  27. var url = httpRequest.Path;
  28. var json = JsonConvert.SerializeObject(request);
  29. log.Debug("URL: " + url + " => Request: " + json);
  30. try
  31. {
  32. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  33. int pageNumber = request.pageNumber < 0 ? 0 : request.pageNumber;
  34. int pageSize = request.pageSize <= 0 ? 10 : request.pageSize;
  35. var query = dbContext.Banners
  36. .Where(b => b.Status.HasValue && b.Status.Value)
  37. .Where(b => b.StartDate == null || b.StartDate <= DateTime.Now)
  38. .Where(b => b.EndDate == null || b.EndDate >= DateTime.Now);
  39. if (!string.IsNullOrEmpty(request.position))
  40. {
  41. query = query.Where(b => b.Position == request.position);
  42. }
  43. int totalCount = query.Count();
  44. int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
  45. var banners = query
  46. .OrderBy(b => b.DisplayOrder)
  47. .ThenByDescending(b => b.CreatedDate)
  48. .Skip(pageNumber * pageSize)
  49. .Take(pageSize)
  50. .Select(b => new
  51. {
  52. b.Id,
  53. title = lang == "en" ? (b.TitleEn ?? b.Title)
  54. : lang == "vi" ? b.Title
  55. : (b.TitleLo ?? b.Title),
  56. subtitle = lang == "en" ? (b.SubtitleEn ?? b.Subtitle)
  57. : lang == "vi" ? b.Subtitle
  58. : (b.SubtitleLo ?? b.Subtitle),
  59. b.ImageUrl,
  60. b.ImageMobileUrl,
  61. b.LinkUrl,
  62. b.LinkTarget,
  63. b.Position,
  64. b.DisplayOrder
  65. })
  66. .ToList();
  67. return DotnetLib.Http.HttpResponse.BuildResponse(
  68. log, url, json,
  69. CommonErrorCode.Success,
  70. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  71. new { banners = banners, pagination = new { pageNumber, pageSize, totalCount, totalPages } }
  72. );
  73. }
  74. catch (Exception ex)
  75. {
  76. log.Error("Exception: ", ex);
  77. }
  78. return DotnetLib.Http.HttpResponse.BuildResponse(
  79. log, url, json,
  80. CommonErrorCode.SystemError,
  81. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  82. new { }
  83. );
  84. }
  85. public async Task<IActionResult> CustomerReviewLoad(HttpRequest httpRequest, CustomerReviewLoadReq request)
  86. {
  87. var url = httpRequest.Path;
  88. var json = JsonConvert.SerializeObject(request);
  89. log.Debug("URL: " + url + " => Request: " + json);
  90. try
  91. {
  92. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  93. int pageNumber = request.pageNumber < 0 ? 0 : request.pageNumber;
  94. int pageSize = request.pageSize <= 0 ? 10 : request.pageSize;
  95. var query = dbContext.CustomerReviews
  96. .Where(r => r.Status.HasValue && r.Status.Value);
  97. if (request.isFeatured.HasValue && request.isFeatured.Value)
  98. {
  99. query = query.Where(r => r.IsFeatured.HasValue && r.IsFeatured.Value);
  100. }
  101. int totalCount = query.Count();
  102. int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
  103. var reviews = query
  104. .OrderBy(r => r.DisplayOrder)
  105. .ThenByDescending(r => r.CreatedDate)
  106. .Skip(pageNumber * pageSize)
  107. .Take(pageSize)
  108. .Select(r => new
  109. {
  110. r.Id,
  111. r.CustomerName,
  112. r.AvatarUrl,
  113. r.Rating,
  114. reviewContent = lang == "en" ? (r.ReviewContentEn ?? r.ReviewContent)
  115. : lang == "vi" ? r.ReviewContent
  116. : (r.ReviewContentLo ?? r.ReviewContent),
  117. destination = lang == "en" ? (r.DestinationEn ?? r.Destination)
  118. : lang == "vi" ? r.Destination
  119. : (r.DestinationLo ?? r.Destination),
  120. r.IsFeatured,
  121. r.CreatedDate
  122. })
  123. .ToList();
  124. return DotnetLib.Http.HttpResponse.BuildResponse(
  125. log, url, json,
  126. CommonErrorCode.Success,
  127. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  128. new { reviews = reviews, pagination = new { pageNumber, pageSize, totalCount, totalPages } }
  129. );
  130. }
  131. catch (Exception ex)
  132. {
  133. log.Error("Exception: ", ex);
  134. }
  135. return DotnetLib.Http.HttpResponse.BuildResponse(
  136. log, url, json,
  137. CommonErrorCode.SystemError,
  138. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  139. new { }
  140. );
  141. }
  142. public async Task<IActionResult> CustomerReviewCreate(HttpRequest httpRequest, CustomerReviewCreateReq request)
  143. {
  144. var url = httpRequest.Path;
  145. var json = JsonConvert.SerializeObject(request);
  146. log.Debug("URL: " + url + " => Request: " + json);
  147. try
  148. {
  149. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  150. // Create new review (pending approval)
  151. var review = new CustomerReview
  152. {
  153. CustomerName = request.customerName,
  154. ReviewContent = request.reviewContent,
  155. Destination = request.destination,
  156. Rating = request.rating > 0 ? request.rating : 1,
  157. Status = false, // Pending approval
  158. IsFeatured = false,
  159. CreatedDate = DateTime.Now
  160. };
  161. dbContext.CustomerReviews.Add(review);
  162. await dbContext.SaveChangesAsync();
  163. return DotnetLib.Http.HttpResponse.BuildResponse(
  164. log, url, json,
  165. CommonErrorCode.Success,
  166. ConfigManager.Instance.GetConfigWebValue("REVIEW_SUBMITTED", lang),
  167. new { reviewId = review.Id }
  168. );
  169. }
  170. catch (Exception ex)
  171. {
  172. log.Error("Exception: ", ex);
  173. }
  174. return DotnetLib.Http.HttpResponse.BuildResponse(
  175. log, url, json,
  176. CommonErrorCode.SystemError,
  177. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  178. new { }
  179. );
  180. }
  181. public async Task<IActionResult> FaqCategoryLoad(HttpRequest httpRequest, FaqCategoryLoadReq request)
  182. {
  183. var url = httpRequest.Path;
  184. var json = JsonConvert.SerializeObject(request);
  185. log.Debug("URL: " + url + " => Request: " + json);
  186. try
  187. {
  188. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  189. int pageNumber = request.pageNumber < 0 ? 0 : request.pageNumber;
  190. int pageSize = request.pageSize <= 0 ? 10 : request.pageSize;
  191. var query = dbContext.FaqCategories
  192. .Where(c => c.Status.HasValue && c.Status.Value);
  193. int totalCount = query.Count();
  194. int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
  195. var categories = query
  196. .OrderBy(c => c.DisplayOrder)
  197. .ThenBy(c => c.Id)
  198. .Skip(pageNumber * pageSize)
  199. .Take(pageSize)
  200. .Select(c => new
  201. {
  202. c.Id,
  203. categoryName = lang == "en" ? (c.CategoryNameEn ?? c.CategoryName)
  204. : lang == "vi" ? c.CategoryName
  205. : (c.CategoryNameLo ?? c.CategoryName),
  206. c.CategorySlug,
  207. description = lang == "en" ? (c.DescriptionEn ?? c.Description)
  208. : lang == "vi" ? c.Description
  209. : (c.DescriptionLo ?? c.Description),
  210. c.IconUrl,
  211. c.DisplayOrder
  212. })
  213. .ToList();
  214. return DotnetLib.Http.HttpResponse.BuildResponse(
  215. log, url, json,
  216. CommonErrorCode.Success,
  217. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  218. new { categories = categories, pagination = new { pageNumber, pageSize, totalCount, totalPages } }
  219. );
  220. }
  221. catch (Exception ex)
  222. {
  223. log.Error("Exception: ", ex);
  224. }
  225. return DotnetLib.Http.HttpResponse.BuildResponse(
  226. log, url, json,
  227. CommonErrorCode.SystemError,
  228. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  229. new { }
  230. );
  231. }
  232. public async Task<IActionResult> FaqLoad(HttpRequest httpRequest, FaqLoadReq request)
  233. {
  234. var url = httpRequest.Path;
  235. var json = JsonConvert.SerializeObject(request);
  236. log.Debug("URL: " + url + " => Request: " + json);
  237. try
  238. {
  239. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  240. int pageNumber = request.pageNumber < 0 ? 0 : request.pageNumber;
  241. int pageSize = request.pageSize <= 0 ? 10 : request.pageSize;
  242. var query = dbContext.Faqs
  243. .Where(f => f.Status == true);
  244. if (request.categoryId.HasValue)
  245. {
  246. query = query.Where(f => f.CategoryId == request.categoryId);
  247. }
  248. if (request.isFeatured != null)
  249. {
  250. query = query.Where(f => f.IsFeatured == request.isFeatured);
  251. }
  252. int totalCount = query.Count();
  253. int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
  254. var faqs = query
  255. .OrderBy(f => f.DisplayOrder)
  256. .ThenByDescending(f => f.ViewCount)
  257. .Skip(pageNumber * pageSize)
  258. .Take(pageSize)
  259. .Select(f => new
  260. {
  261. f.Id,
  262. question = lang == "en" ? (f.QuestionEn ?? f.Question)
  263. : lang == "vi" ? f.Question
  264. : (f.QuestionLo ?? f.Question),
  265. answer = lang == "en" ? (f.AnswerEn ?? f.Answer)
  266. : lang == "vi" ? f.Answer
  267. : (f.AnswerLo ?? f.Answer),
  268. f.CategoryId,
  269. f.ViewCount,
  270. f.IsFeatured
  271. })
  272. .ToList();
  273. return DotnetLib.Http.HttpResponse.BuildResponse(
  274. log, url, json,
  275. CommonErrorCode.Success,
  276. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  277. new { faqs = faqs, pagination = new { pageNumber, pageSize, totalCount, totalPages } }
  278. );
  279. }
  280. catch (Exception ex)
  281. {
  282. log.Error("Exception: ", ex);
  283. }
  284. return DotnetLib.Http.HttpResponse.BuildResponse(
  285. log, url, json,
  286. CommonErrorCode.SystemError,
  287. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  288. new { }
  289. );
  290. }
  291. /// <summary>
  292. /// Load device eSIM compatibility list
  293. /// </summary>
  294. public async Task<IActionResult> DeviceCompatibilityLoad(HttpRequest httpRequest, DeviceCompatibilityReq request)
  295. {
  296. var url = httpRequest.Path;
  297. var json = JsonConvert.SerializeObject(request);
  298. log.Debug("URL: " + url + " => Request: " + json);
  299. try
  300. {
  301. string lang = CommonLogic.GetLanguage(httpRequest, request.lang);
  302. int pageNumber = request.pageNumber < 0 ? 0 : request.pageNumber;
  303. int pageSize = request.pageSize <= 0 ? 50 : request.pageSize;
  304. // Base query - only active devices
  305. var query = dbContext.DeviceEsimCompatibilities
  306. .Where(d => d.Status.HasValue && d.Status.Value && d.SupportsEsim.HasValue && d.SupportsEsim.Value);
  307. // Filter by brand
  308. if (!string.IsNullOrEmpty(request.brand))
  309. {
  310. query = query.Where(d => d.Brand == request.brand);
  311. }
  312. // Filter by category
  313. if (!string.IsNullOrEmpty(request.category))
  314. {
  315. query = query.Where(d => d.Category == request.category);
  316. }
  317. // Filter by popular flag
  318. if (request.isPopular.HasValue && request.isPopular.Value)
  319. {
  320. query = query.Where(d => d.IsPopular.HasValue && d.IsPopular.Value);
  321. }
  322. // Search by keyword in model name (all language variants)
  323. if (!string.IsNullOrEmpty(request.searchKeyword))
  324. {
  325. var keyword = request.searchKeyword.ToLower();
  326. query = query.Where(d =>
  327. d.ModelName.ToLower().Contains(keyword) ||
  328. (d.ModelNameEn != null && d.ModelNameEn.ToLower().Contains(keyword)) ||
  329. (d.ModelNameLo != null && d.ModelNameLo.ToLower().Contains(keyword))
  330. );
  331. }
  332. // Get total count for pagination
  333. int totalCount = query.Count();
  334. int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
  335. // Apply pagination and ordering
  336. var devices = query
  337. .OrderBy(d => d.DisplayOrder)
  338. .ThenBy(d => d.Brand)
  339. .ThenBy(d => d.ModelName)
  340. .Skip(pageNumber * pageSize)
  341. .Take(pageSize)
  342. .Select(d => new
  343. {
  344. d.Id,
  345. d.Brand,
  346. modelName = lang == "en" ? (d.ModelNameEn ?? d.ModelName)
  347. : lang == "vi" ? d.ModelName
  348. : (d.ModelNameLo ?? d.ModelName),
  349. d.Category,
  350. notes = lang == "en" ? (d.NotesEn ?? d.Notes)
  351. : lang == "vi" ? d.Notes
  352. : (d.NotesLo ?? d.Notes),
  353. supportsEsim = d.SupportsEsim ?? true,
  354. isPopular = d.IsPopular ?? false,
  355. displayOrder = d.DisplayOrder ?? 999
  356. })
  357. .ToList();
  358. return DotnetLib.Http.HttpResponse.BuildResponse(
  359. log, url, json,
  360. CommonErrorCode.Success,
  361. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  362. new
  363. {
  364. devices = devices,
  365. pagination = new
  366. {
  367. pageNumber,
  368. pageSize,
  369. totalCount,
  370. totalPages
  371. }
  372. }
  373. );
  374. }
  375. catch (Exception exception)
  376. {
  377. log.Error("Exception: ", exception);
  378. }
  379. return DotnetLib.Http.HttpResponse.BuildResponse(
  380. log, url, json,
  381. CommonErrorCode.SystemError,
  382. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  383. new { }
  384. );
  385. }
  386. /// <summary>
  387. /// Get list of device brands and categories (for filters/tabs)
  388. /// </summary>
  389. public async Task<IActionResult> GetDeviceBrandsAndCategories(HttpRequest httpRequest)
  390. {
  391. var url = httpRequest.Path;
  392. log.Debug("URL: " + url);
  393. try
  394. {
  395. // Get language from header
  396. string lang = CommonLogic.GetLanguage(httpRequest);
  397. // Get all active devices for client-side filtering
  398. var allDevices = dbContext.DeviceEsimCompatibilities
  399. .Where(d => d.Status.HasValue && d.Status.Value && d.SupportsEsim.HasValue && d.SupportsEsim.Value)
  400. .OrderBy(d => d.DisplayOrder)
  401. .ThenBy(d => d.ModelName)
  402. .ToList();
  403. // Group devices by brand and include full device list
  404. var brands = allDevices
  405. .GroupBy(d => d.Brand)
  406. .Select(g => new
  407. {
  408. brand = g.Key,
  409. deviceCount = g.Count(),
  410. popularCount = g.Count(d => d.IsPopular.HasValue && d.IsPopular.Value),
  411. devices = g.Select(d => new
  412. {
  413. d.Id,
  414. modelName = lang == "en" ? (d.ModelNameEn ?? d.ModelName)
  415. : lang == "vi" ? d.ModelName
  416. : (d.ModelNameLo ?? d.ModelName),
  417. d.Category,
  418. d.IsPopular,
  419. d.DisplayOrder,
  420. notes = lang == "en" ? (d.NotesEn ?? d.Notes)
  421. : lang == "vi" ? d.Notes
  422. : (d.NotesLo ?? d.Notes)
  423. }).ToList()
  424. })
  425. .OrderBy(b => b.brand)
  426. .ToList();
  427. // Get distinct categories with device count
  428. var categories = allDevices
  429. .GroupBy(d => d.Category)
  430. .Select(g => new
  431. {
  432. category = g.Key,
  433. deviceCount = g.Count()
  434. })
  435. .OrderBy(c => c.category)
  436. .ToList();
  437. return DotnetLib.Http.HttpResponse.BuildResponse(
  438. log, url, "",
  439. CommonErrorCode.Success,
  440. ConfigManager.Instance.GetConfigWebValue("LOAD_SUCCESS", lang),
  441. new
  442. {
  443. brands = brands,
  444. categories = categories
  445. }
  446. );
  447. }
  448. catch (Exception exception)
  449. {
  450. log.Error("Exception: ", exception);
  451. }
  452. return DotnetLib.Http.HttpResponse.BuildResponse(
  453. log, url, "",
  454. CommonErrorCode.SystemError,
  455. ConfigManager.Instance.GetConfigWebValue("SYSTEM_FAILURE"),
  456. new { }
  457. );
  458. }
  459. }
  460. }