EquipmentHistoryController.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. using Newtonsoft.Json;
  17. using Microsoft.AspNetCore.Http;
  18. using System.Collections.Generic;
  19. using System.Collections;
  20. namespace FMSApp.Controllers {
  21. [Authorize]
  22. [ApiController]
  23. [ApiVersion("1")]
  24. [Route("api/app/[controller]")]
  25. public class EquipmentHistoryController : Controller {
  26. private readonly ILogger<EquipmentHistoryController> _logger;
  27. private readonly FMSContext _context;
  28. private readonly EquipmentService _service;
  29. private readonly EquipmentHistoryService _historyService;
  30. private readonly StorageHelper _storage;
  31. public EquipmentHistoryController(
  32. ILogger<EquipmentHistoryController> logger,
  33. FMSContext context,
  34. EquipmentService service,
  35. EquipmentHistoryService historyService,
  36. StorageHelper storage
  37. ) {
  38. _logger = logger;
  39. _context = context;
  40. _service = service;
  41. _historyService = historyService;
  42. _storage = storage;
  43. }
  44. /// <summary>
  45. /// 공기구 등록 목록
  46. /// </summary>
  47. [HttpGet("[action]")]
  48. public IActionResult AddStockList([FromQuery] PagingRequest req) {
  49. return List(req, 1);
  50. }
  51. /// <summary>
  52. /// 공기구 대여 목록
  53. /// </summary>
  54. [HttpGet("[action]")]
  55. public IActionResult RentList([FromQuery] PagingRequest req) {
  56. return List(req, 2);
  57. }
  58. /// <summary>
  59. /// 공기구 반납/손실 목록
  60. /// </summary>
  61. [HttpGet("[action]")]
  62. public IActionResult ReturnList([FromQuery] PagingRequest req) {
  63. return List(req, 3);
  64. }
  65. /// <summary>
  66. /// 리스트
  67. /// </summary>
  68. /// <param name="req"></param>
  69. /// <param name="siteId"></param>
  70. /// <returns></returns>
  71. [HttpGet]
  72. public IActionResult List([FromQuery] PagingRequest req, int? stateType) {
  73. var query = _FilterAndSort(req, stateType);
  74. if (stateType != null) {
  75. if (stateType == 2) { // 대여
  76. query = query.Where(x => x.EquipmentStateTypeId != 1 && x.IsReturned == false);
  77. } else if (stateType == 3) {
  78. query = query.Where(x => x.EquipmentStateTypeId == 3 || x.EquipmentStateTypeId == 4 || x.EquipmentStateTypeId == 5);
  79. } else {
  80. query = query.Where(x => x.EquipmentStateTypeId == stateType);
  81. }
  82. } else {
  83. if (!_context.SiteConfig.Single().UseEquipmentRent) {
  84. query = query.Where(x => x.EquipmentStateTypeId != 2);
  85. }
  86. }
  87. var list = _ListSelect(query);
  88. var paging = PagingList.Pagination(list, req.page, req.limit);
  89. return Ok(paging);
  90. }
  91. /// <summary>
  92. /// 조회
  93. /// </summary>
  94. /// <param name="siteId"></param>
  95. /// <param name="id"></param>
  96. /// <returns></returns>
  97. [HttpGet("{siteId}/{id}")]
  98. public IActionResult Get(int siteId, int id) {
  99. var data = _historyService.Gets(id, siteId);
  100. if (data.Count() == 0) {
  101. return NotFound("공기구정보를 찾을 수 없습니다.");
  102. }
  103. var item = data.Select(x => new {
  104. x.SiteId,
  105. x.EquipmentId,
  106. x.EquipmentHistoryId,
  107. x.EquipmentRentId,
  108. Site = new {
  109. x.CmSite.Name,
  110. },
  111. EquipmentStateType = new {
  112. x.EquipmentStateType.Name
  113. },
  114. RentInfo = new {
  115. Title = x.FmsEquipmentRentInfo.Title,
  116. RentUserName = x.FmsEquipmentRentInfo.RentUserName,
  117. RentDate = x.FmsEquipmentRentInfo.RentDate,
  118. ReturnDueDate = x.FmsEquipmentRentInfo.ReturnDueDate,
  119. ReturnFixDate = x.FmsEquipmentRentInfo.ReturnFixDate
  120. },
  121. EquipmentInfo = new {
  122. x.FmsEquipment.Name,
  123. Standard = x.FmsEquipment.Standard,
  124. },
  125. EquipmentName = x.FmsEquipment.Name,
  126. x.RentCount,
  127. x.CurrentRentCount,
  128. x.LossCount,
  129. x.ReturnCount,
  130. x.ReturnReason,
  131. x.IsReturnApproval,
  132. x.ReturnApprovalDate,
  133. ReturnApprovalUser = x.CmUser.Name,
  134. CmFile = _storage.ParseFile(x.CmFile),
  135. });
  136. return Ok(item.First());
  137. }
  138. /// <summary>
  139. /// 등록
  140. /// </summary>
  141. /// <param name="data"></param>
  142. /// <returns></returns>
  143. [HttpPut]
  144. public IActionResult Create([FromBody] FmsEquipmentHistory data) {
  145. if (ModelState.IsValid) {
  146. _historyService.Create(data);
  147. } else {
  148. return BadRequest(ModelState);
  149. }
  150. return Ok();
  151. }
  152. /// <summary>
  153. /// 삭제
  154. /// </summary>
  155. /// <param name="siteId"></param>
  156. /// <param name="id"></param>
  157. /// <returns></returns>
  158. [HttpDelete("{siteId}/{id}")]
  159. public IActionResult Delete(int siteId, int id) {
  160. //_service.DeleteHistory(id, siteId);
  161. return Ok();
  162. }
  163. // 셀렉트 공통
  164. private dynamic _ListSelect(IQueryable<FmsEquipmentHistory> query) {
  165. var list = query.Select(x => new {
  166. x.EquipmentHistoryId,
  167. x.EquipmentId,
  168. x.EquipmentStateTypeId,
  169. EquipmentStateTypeName = x.EquipmentStateType.Name,
  170. x.SiteId,
  171. SiteName = x.CmSite.Name,
  172. x.FmsEquipment.EquipmentTypeId,
  173. EquipmentTypeName = x.FmsEquipment.FmsEquipmentCodeType.Name,
  174. x.FmsEquipment.Name,
  175. x.FmsEquipment.Standard,
  176. x.FmsEquipment.Unit,
  177. x.FmsEquipment.WarehouseId,
  178. WarehouseName = x.FmsEquipment.FmsMaterialWarehouse.Name,
  179. x.TotalStockCount,
  180. x.CurrentRentCount,
  181. x.CurrentStockCount,
  182. x.StoredCount,
  183. x.RentCount,
  184. x.ReturnCount,
  185. x.LossCount,
  186. x.ReturnReason,
  187. x.AddDate,
  188. x.EquipmentRentId,
  189. x.FmsEquipmentRentInfo.RentDate,
  190. x.FmsEquipmentRentInfo.ReturnDueDate,
  191. x.FmsEquipmentRentInfo.ReturnFixDate,
  192. x.FmsEquipment.SupplierName,
  193. x.FmsEquipment.SupplierPhoneNo,
  194. RentUserName = x.FmsEquipmentRentInfo.RentUserName,
  195. x.FmsEquipmentRentInfo.RentUserId,
  196. ReturnApprovalUserName = x.ReturnApprovalUserId != null ? x.CmUser.Name : "",
  197. x.ReturnApprovalDate,
  198. });
  199. return list;
  200. }
  201. // 검색 & 정렬 공통
  202. private IQueryable<FmsEquipmentHistory> _FilterAndSort(PagingRequest req, int? stateType) {
  203. var query = _service.GetHistory();
  204. // 기본 Entity 검색
  205. query = query.Filter(req.conditions);
  206. if (req.siteId != null) {
  207. query = query.Where(x => x.SiteId == Util.ToInt(req.siteId));
  208. }
  209. if (Util.IsNullOrEmpty(stateType)) {
  210. // 고정정렬 (사옥 준공순)
  211. var sortQuery = query.OrderBy(x => x.CmSite.SortOrderNo);
  212. // 추가 정렬
  213. sortQuery = sortQuery.ThenSort(req.sort);
  214. return sortQuery;
  215. } else {
  216. // 기본 Entity 정렬
  217. query = query.Sort(req.sort);
  218. return query;
  219. }
  220. }
  221. }
  222. }