SitePriceController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using FMSAdmin.Data;
  5. using FMSAdmin.Models;
  6. using FMSAdmin.Entities;
  7. using FMSAdmin.Services;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Microsoft.EntityFrameworkCore;
  12. using FMSAdmin.Helpers;
  13. using System.Data;
  14. using System.IO;
  15. using OfficeOpenXml;
  16. namespace FMSAdmin.Controllers {
  17. [Authorize]
  18. [ApiController]
  19. [ApiVersion("1")]
  20. [Route("api/[controller]")]
  21. public class SitePriceController : Controller {
  22. private readonly ILogger<SitePriceController> _logger;
  23. private readonly FMSContext _context;
  24. private readonly SitePriceService _service;
  25. public SitePriceController(
  26. ILogger<SitePriceController> logger,
  27. FMSContext context,
  28. SitePriceService service
  29. ) {
  30. _logger = logger;
  31. _context = context;
  32. _service = service;
  33. }
  34. /// <summary>
  35. /// 목록
  36. /// </summary>
  37. [HttpGet]
  38. public IActionResult List([FromQuery] PagingRequest req) {
  39. var query = _FilterAndSort(req);
  40. var list = _ListSelect(query);
  41. var paging = PagingList.Pagination(list, req.page, req.limit);
  42. return Ok(paging);
  43. }
  44. private dynamic _ListSelect(IQueryable<BemsSitePrice> query) {
  45. //query = query.Where(x => x.SiteId.Name));
  46. var list = query.Select(x => new {
  47. x.PriceCode,
  48. });
  49. return list;
  50. }
  51. // 검색 & 정렬 공통
  52. private IQueryable<BemsSitePrice> _FilterAndSort(PagingRequest req) {
  53. var query = _service.GetAll();
  54. // 기본 Entity 검색
  55. query = query.Filter(req.conditions);
  56. // 기본 Entity 정렬
  57. query = query.Sort(req.sort);
  58. return query;
  59. }
  60. [HttpGet("[action]")]
  61. public IActionResult GetAllPriceType(int fuelTypeId) {
  62. var list = _context.BemsPriceType.Where(x => x.FuelTypeId == fuelTypeId)
  63. .Select(x => new {
  64. x.PriceTypeId,
  65. x.PriceTypeIdDesc
  66. }).ToList();
  67. return Ok(list);
  68. }
  69. [HttpGet("[action]")]
  70. public IActionResult GetPriceType(int siteId, int fuelTypeId) {
  71. var query = from x in _context.BemsPriceType
  72. join s in _context.BemsSitePrice on new { x.PriceTypeId } equals new { s.PriceTypeId }
  73. where s.SiteId == siteId
  74. where s.FuelTypeId == fuelTypeId
  75. select new {
  76. PriceTypeId = x.PriceTypeId,
  77. PriceTypeIdDesc = x.PriceTypeIdDesc
  78. };
  79. var list = query.GroupBy(x => new { x.PriceTypeId, x.PriceTypeIdDesc }).Select(x => new { x.Key.PriceTypeId, x.Key.PriceTypeIdDesc }).ToList();
  80. return Ok(list);
  81. }
  82. [HttpGet("[action]")]
  83. public IActionResult GetPriceCode(int siteId, int fuelTypeId, int priceTypeId) {
  84. if (priceTypeId == 0) {
  85. var query = _context.BemsPriceCode.Where(x => x.FuelTypeId == fuelTypeId)
  86. .Select(x => new {
  87. SiteId = siteId,
  88. FuelTypeId = Convert.ToInt16(x.FuelTypeId),
  89. PriceTypeId = priceTypeId,
  90. PriceCode = x.PriceCode,
  91. PriceCodeDesc = x.PriceCodeDesc,
  92. UseYn = "Y",
  93. IsPriceCode = true
  94. });
  95. return Ok(query);
  96. } else {
  97. var query = from x in _context.BemsSitePrice
  98. join s in _context.BemsPriceCode on new { x.PriceCode } equals new { s.PriceCode }
  99. where x.SiteId == siteId
  100. where x.FuelTypeId == fuelTypeId
  101. where x.PriceTypeId == priceTypeId
  102. select new {
  103. SiteId = x.SiteId,
  104. FuelTypeId = x.FuelTypeId,
  105. PriceTypeId = x.PriceTypeId,
  106. PriceCode = x.PriceCode,
  107. PriceCodeDesc = s.PriceCodeDesc,
  108. UseYn = x.UseYn,
  109. IsPriceCode = x.UseYn == "Y" ? true : false
  110. };
  111. return Ok(query);
  112. }
  113. }
  114. /// <summary>
  115. /// 저장
  116. /// </summary>
  117. [HttpPost("{siteId}/{fuelTypeId}/{priceTypeId}")]
  118. public IActionResult Save(int siteId, int fuelTypeId, string priceTypeId, [FromBody] BemsSitePrice[] data) {
  119. _logger.LogInformation("Save");
  120. //_logger.LogInformation(data.Dump());
  121. if (ModelState.IsValid) {
  122. _service.Save(siteId, fuelTypeId, Convert.ToInt32(priceTypeId), data);
  123. } else {
  124. return BadRequest(ModelState);
  125. }
  126. return Ok();
  127. }
  128. /// <summary>
  129. /// 삭제
  130. /// </summary>
  131. [HttpDelete("{siteId}/{fuelTypeId}/{priceTypeId}")]
  132. public IActionResult Delete(int siteId, int fuelTypeId, string priceTypeId) {
  133. _service.Delete(siteId, fuelTypeId, Convert.ToInt32(priceTypeId));
  134. return Ok();
  135. }
  136. [HttpGet("[action]")]
  137. public IActionResult GetFuelType() {
  138. var query = _service.GetFuelType();
  139. var list = query.Select(x => new {
  140. x.FuelTypeId,
  141. x.Name
  142. }).ToList();
  143. return Ok(list);
  144. }
  145. }
  146. }