WorkHistoryController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 FMSApp.Services;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.AspNetCore.Authorization;
  11. using FMSAdmin.Helpers;
  12. using System.Data;
  13. using System.IO;
  14. using OfficeOpenXml;
  15. namespace FMSApp.Controllers {
  16. [Authorize]
  17. [ApiController]
  18. [ApiVersion("1")]
  19. [Route("api/app/[controller]")]
  20. public class WorkHistoryController : Controller {
  21. private readonly ILogger<WorkHistoryController> _logger;
  22. private readonly FMSContext _context;
  23. private readonly WorkRequestService _service;
  24. private readonly StorageHelper _storage;
  25. public WorkHistoryController(
  26. ILogger<WorkHistoryController> logger,
  27. FMSContext context,
  28. WorkRequestService service,
  29. StorageHelper storage
  30. ) {
  31. _logger = logger;
  32. _context = context;
  33. _service = service;
  34. _storage = storage;
  35. }
  36. /// <summary>
  37. /// 작업이력 목록
  38. /// </summary>
  39. [HttpGet]
  40. public IActionResult List([FromQuery] PagingRequest req, [FromQuery] string siteId, bool businessAuth) {
  41. _logger.LogInformation("===========================");
  42. _logger.LogInformation(req.Dump());
  43. _logger.LogInformation("===========================");
  44. var query = _FilterAndSort(req);
  45. query = BusinessFieldHelper.Apply<FmsWorkRequest>(_context, User, businessAuth, query);
  46. query = query.Where(
  47. x => x.SiteId == Util.ToInt(siteId)
  48. && x.WorkProgressId > 1 // 작업계획 이후의 모든 프로세스
  49. && (x.FmsWorkSchedule == null || (x.FmsWorkSchedule != null && x.FmsWorkSchedule.IsUse == true))
  50. );
  51. var list = query.Select(x => new {
  52. x.SiteId,
  53. x.WorkRequestId,
  54. x.WorkTypeId,
  55. WorkType = new {
  56. x.WorkType.Name
  57. },
  58. x.WorkDetailTypeId,
  59. WorkDetailType = new {
  60. x.WorkDetailType.Code,
  61. x.WorkDetailType.Name,
  62. },
  63. x.BusinessFieldId,
  64. BusinessField = new {
  65. x.CmBusinessField.Name
  66. },
  67. x.WorkProgressId,
  68. WorkProgress = new {
  69. x.WorkProgress.Name
  70. },
  71. x.Title,
  72. x.Content,
  73. x.RequestUserId,
  74. RequestUserName = x.CmUserNavigation.Name,
  75. x.RequestDate,
  76. x.StartWorkDate,
  77. x.RejectUserId,
  78. RejectUserName = x.CmUser.Name,
  79. x.RejectDate,
  80. x.RejectReason,
  81. x.InspectionAgencyId,
  82. InspectionAgencyName = x.CmPartner.Name,
  83. x.WorkScheduleId,
  84. x.WorkPartnerId,
  85. WorkPartnerName = x.WorkPartner.Name,
  86. x.WorkPrice,
  87. x.WorkItem,
  88. x.WorkEndDate,
  89. FmsWorkOrder = new {
  90. x.FmsWorkOrder.OrderDate,
  91. x.FmsWorkOrder.Content,
  92. },
  93. FmsWorkResult = new {
  94. x.FmsWorkResult.StartDate,
  95. x.FmsWorkResult.EndDate,
  96. x.FmsWorkResult.ConfirmedDate,
  97. x.FmsWorkResult.ConfirmedUserId,
  98. x.FmsWorkResult.IsConfirmed,
  99. x.FmsWorkResult.CauseClassId,
  100. x.FmsWorkResult.Content,
  101. x.FmsWorkResult.ConfirmDesc,
  102. ConfirmUserName = x.FmsWorkResult.CmUser.Name,
  103. WorkerUserName = x.FmsWorkResult.CmUserNavigation.Name,
  104. },
  105. });
  106. var paging = PagingList.Pagination(list, req.page, req.limit);
  107. return Ok(paging);
  108. }
  109. /// <summary>
  110. /// 작업이력 상세 조회
  111. /// </summary>
  112. [HttpGet("[action]")]
  113. public IActionResult Get([FromQuery] string siteId, [FromQuery] string workRequestId) {
  114. var query = _service.GetAll()
  115. .Where(x => x.SiteId == Util.ToInt(siteId) && x.WorkRequestId == Util.ToInt(workRequestId));
  116. var item = query.Select(x => new {
  117. x.SiteId,
  118. x.WorkRequestId,
  119. x.WorkTypeId,
  120. WorkType = new {
  121. x.WorkType.Name
  122. },
  123. x.WorkDetailTypeId,
  124. WorkDetailType = new {
  125. x.WorkDetailType.Code,
  126. x.WorkDetailType.Name,
  127. },
  128. x.BusinessFieldId,
  129. BusinessField = new {
  130. x.CmBusinessField.Name
  131. },
  132. x.WorkProgressId,
  133. WorkProgress = new {
  134. x.WorkProgress.Name
  135. },
  136. x.Title,
  137. x.Content,
  138. x.RequestUserId,
  139. RequestUserName = x.CmUserNavigation.Name,
  140. x.RequestDate,
  141. x.StartWorkDate,
  142. x.RejectUserId,
  143. RejectUserName = x.CmUser.Name,
  144. x.RejectDate,
  145. x.RejectReason,
  146. x.InspectionAgencyId,
  147. InspectionAgencyName = x.CmPartner.Name,
  148. x.WorkScheduleId,
  149. x.WorkPartnerId,
  150. WorkPartnerName = x.WorkPartner.Name,
  151. x.WorkPrice,
  152. x.WorkItem,
  153. x.WorkEndDate,
  154. x.WorkUsers,
  155. CmFile = _storage.ParseFile(x.CmFile),
  156. FmsWorkOrder = new {
  157. x.FmsWorkOrder.OrderDate,
  158. x.FmsWorkOrder.Content,
  159. },
  160. FmsWorkResult = new {
  161. x.FmsWorkResult.StartDate,
  162. x.FmsWorkResult.EndDate,
  163. x.FmsWorkResult.ConfirmedDate,
  164. x.FmsWorkResult.ConfirmedUserId,
  165. x.FmsWorkResult.IsConfirmed,
  166. x.FmsWorkResult.CauseClassId,
  167. x.FmsWorkResult.Content,
  168. x.FmsWorkResult.ConfirmDesc,
  169. ConfirmUserName = x.FmsWorkResult.CmUser.Name,
  170. WorkerUserName = x.FmsWorkResult.CmUserNavigation.Name,
  171. },
  172. FmsWorkRequestToFacility = x.FmsWorkRequestToFacility.Select(f => new {
  173. f.SiteId,
  174. f.WorkRequestId,
  175. f.FacilityCode,
  176. CmFacility = new {
  177. f.CmFacility.Name,
  178. f.CmFacility.BuildingId,
  179. CmBuilding = new {
  180. f.CmFacility.CmBuilding.Name,
  181. },
  182. f.CmFacility.FloorId,
  183. CmFloor = new {
  184. f.CmFacility.CmFloor.Name,
  185. },
  186. f.CmFacility.FirstClassId,
  187. FirstClass = new {
  188. f.CmFacility.FmsFacilityCodeClass.Name,
  189. },
  190. f.CmFacility.SecondClassId,
  191. SecondClass = new {
  192. f.CmFacility.FmsFacilityCodeClass1.Name,
  193. },
  194. f.CmFacility.ThirdClassId,
  195. ThirdClass = new {
  196. f.CmFacility.FmsFacilityCodeClass2.Name,
  197. },
  198. },
  199. }).ToList(),
  200. FmsWorkRequestToAccident = x.FmsWorkRequestToAccident.Select(f => new {
  201. f.SiteId,
  202. f.WorkRequestId,
  203. f.AccidentId,
  204. FmsAccident = new {
  205. f.FmsAccident.SiteId,
  206. f.FmsAccident.AccidentId,
  207. f.FmsAccident.Name,
  208. f.FmsAccident.AccidentTypeId,
  209. AccidentTypeName = f.FmsAccident.FmsAccidentCodeType.Name,
  210. Dates = new {
  211. StartDate = f.FmsAccident.StartDate == null ? null : ((DateTime)f.FmsAccident.StartDate).ToString("yyyy.MM.dd"),
  212. EndDate = f.FmsAccident.EndDate == null ? null : ((DateTime)f.FmsAccident.EndDate).ToString("yyyy.MM.dd"),
  213. },
  214. f.FmsAccident.DepartmentId,
  215. DepartmentName = f.FmsAccident.CmDepartment.Name,
  216. f.FmsAccident.AccidentLocation,
  217. },
  218. }).ToList(),
  219. FmsWorkRequestToComplaint = x.FmsWorkRequestToComplaint.Select(f => new {
  220. f.SiteId,
  221. f.WorkRequestId,
  222. f.ComplaintId,
  223. FmsComplaints = new {
  224. f.FmsComplaints.SiteId,
  225. f.FmsComplaints.ComplaintId,
  226. f.FmsComplaints.Name,
  227. f.FmsComplaints.ComplaintTypeId,
  228. ComplaintTypeName = f.FmsComplaints.FmsComplaintCodeType.Name,
  229. Dates = new {
  230. StartDate = f.FmsComplaints.StartDate == null ? null : ((DateTime)f.FmsComplaints.StartDate).ToString("yyyy.MM.dd"),
  231. EndDate = f.FmsComplaints.EndDate == null ? null : ((DateTime)f.FmsComplaints.EndDate).ToString("yyyy.MM.dd"),
  232. },
  233. f.FmsComplaints.DepartmentId,
  234. DepartmentName = f.FmsComplaints.CmDepartment.Name,
  235. f.FmsComplaints.ComplaintLocation,
  236. },
  237. }).ToList(),
  238. }).FirstOrDefault();
  239. return Ok(item);
  240. }
  241. // 검색 & 정렬 공통
  242. private IQueryable<FmsWorkRequest> _FilterAndSort(PagingRequest req) {
  243. var query = _service.GetAll();
  244. // 시설유형 검색조건 처리
  245. if (req != null && req.conditions != null) {
  246. var cond = req.conditions.Where(x => x.field == "FacilityTypeId").FirstOrDefault();
  247. if (cond != null && !string.IsNullOrEmpty(cond.op) && !string.IsNullOrEmpty(cond.value)) {
  248. query = query.Where(
  249. x => _context.FmsWorkRequestToFacility.Where(
  250. y => y.SiteId == x.SiteId
  251. && y.WorkRequestId == x.WorkRequestId
  252. && y.CmFacility.FacilityTypeId == Util.ToInt(cond.value)
  253. ).Count() > 0
  254. );
  255. }
  256. }
  257. // 기본 Entity 검색
  258. query = query.Filter(req.conditions);
  259. // 기본 Entity 정렬
  260. if (Util.V(req, x => x.sort.field) == "Custom") {
  261. query = query.OrderByDescending(x => x.FmsWorkResult.StartDate).ThenBy(x => x.Title);
  262. } else {
  263. query = query.Sort(req.sort);
  264. }
  265. return query;
  266. }
  267. }
  268. }