WorkScheduleController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. using System.Collections.Generic;
  16. using Microsoft.AspNetCore.Mvc.ModelBinding;
  17. namespace FMSApp.Controllers {
  18. [Authorize]
  19. [ApiController]
  20. [ApiVersion("1")]
  21. [Route("api/app/[controller]")]
  22. public class WorkScheduleController : Controller {
  23. private readonly ILogger<WorkScheduleController> _logger;
  24. private readonly FMSContext _context;
  25. private readonly WorkScheduleService _service;
  26. private readonly WorkRequestService _requestService;
  27. private readonly StorageHelper _storage;
  28. public WorkScheduleController(
  29. ILogger<WorkScheduleController> logger,
  30. FMSContext context,
  31. WorkScheduleService service,
  32. WorkRequestService requestService,
  33. StorageHelper storage
  34. ) {
  35. _logger = logger;
  36. _context = context;
  37. _service = service;
  38. _requestService = requestService;
  39. _storage = storage;
  40. }
  41. /// <summary>
  42. /// 정기점검 계획 목록
  43. /// </summary>
  44. [HttpGet("[action]")]
  45. public IActionResult RegularListFromRequest([FromQuery] PagingRequest req, [FromQuery] string siteId, bool businessAuth) {
  46. return ListFromRequest(req, siteId, new int[] { 1 }, businessAuth);
  47. }
  48. /// <summary>
  49. /// 법정검사/법정교육 계획 목록
  50. /// </summary>
  51. [HttpGet("[action]")]
  52. public IActionResult LegalListFromRequest([FromQuery] PagingRequest req, [FromQuery] string siteId, bool businessAuth) {
  53. return ListFromRequest(req, siteId, new int[] { 2, 7 }, businessAuth);
  54. }
  55. private IActionResult ListFromRequest(PagingRequest req, string siteId, int[] workTypeIds, bool businessAuth) {
  56. var query = _FilterAndSortFromRequest(req);
  57. query = query.Where(
  58. x => x.SiteId == Util.ToInt(siteId)
  59. && workTypeIds.Contains(x.WorkTypeId)
  60. && x.WorkProgressId == 1
  61. );
  62. query = BusinessFieldHelper.Apply<FmsWorkRequest>(_context, User, businessAuth, query);
  63. var list = query.Select(x => new {
  64. x.SiteId,
  65. x.WorkRequestId,
  66. x.WorkTypeId,
  67. WorkType = new {
  68. x.WorkType.Name
  69. },
  70. x.BusinessFieldId,
  71. BusinessField = new {
  72. x.CmBusinessField.Name
  73. },
  74. x.InspectionAgencyId,
  75. InspectionAgency = new {
  76. x.CmPartner.Name
  77. },
  78. x.Title,
  79. x.StartWorkDate,
  80. x.RequestUserId,
  81. WorkPartnerName = x.WorkPartner.Name,
  82. x.WorkPrice,
  83. x.WorkItem,
  84. x.WorkEndDate,
  85. x.FmsWorkSchedule.WorkScheduleId,
  86. x.FmsWorkSchedule.CycleSize,
  87. x.FmsWorkSchedule.CycleUnitId,
  88. CycleUnitName = x.FmsWorkSchedule.FmsWorkCodeCycleUnit.Name,
  89. x.FmsWorkSchedule.HolidayWorkTypeId,
  90. HolidayWorkTypeName = x.FmsWorkSchedule.FmsWorkCodeHolidayWorkType.Name,
  91. });
  92. var paging = PagingList.Pagination(list, req.page, req.limit);
  93. return Ok(paging);
  94. }
  95. private IQueryable<FmsWorkRequest> _FilterAndSortFromRequest(PagingRequest req) {
  96. var query = _requestService.GetAll();
  97. // 기본 Entity 검색
  98. query = query.Filter(req.conditions);
  99. // 기본 Entity 정렬
  100. query = query.Sort(req.sort);
  101. return query;
  102. }
  103. [HttpGet("[action]")]
  104. public IActionResult RegularFromRequest([FromQuery] string siteId, [FromQuery] string workScheduleId) {
  105. return GetFromRequest(siteId, workScheduleId, 1);
  106. }
  107. [HttpGet("[action]")]
  108. public IActionResult LegalFromRequest([FromQuery] string siteId, [FromQuery] string workScheduleId) {
  109. return GetFromRequest(siteId, workScheduleId, 2);
  110. }
  111. private IActionResult GetFromRequest(string siteId, string workScheduleId, int workTypeId) {
  112. var query = _requestService.GetAll()
  113. .Where(x => x.SiteId == Util.ToInt(siteId) && x.WorkScheduleId == Util.ToInt(workScheduleId));
  114. var item = query.Select(x => new {
  115. x.SiteId,
  116. x.WorkScheduleId,
  117. x.WorkTypeId,
  118. WorkType = new {
  119. x.WorkType.Name
  120. },
  121. x.BusinessFieldId,
  122. BusinessField = new {
  123. x.CmBusinessField.Name
  124. },
  125. x.InspectionAgencyId,
  126. InspectionAgency = new {
  127. x.CmPartner.Name
  128. },
  129. x.Title,
  130. x.StartWorkDate,
  131. x.RequestUserId,
  132. x.WorkPartnerId,
  133. WorkPartnerName = x.WorkPartner.Name,
  134. x.WorkPrice,
  135. x.WorkItem,
  136. x.WorkEndDate,
  137. CmFile = _storage.ParseFile(x.CmFile),
  138. FmsWorkRequestToFacility = x.FmsWorkRequestToFacility.Select(f => new {
  139. f.SiteId,
  140. f.FacilityCode,
  141. CmFacility = new {
  142. f.CmFacility.Name,
  143. f.CmFacility.BuildingId,
  144. CmBuilding = new {
  145. f.CmFacility.CmBuilding.Name,
  146. },
  147. f.CmFacility.FloorId,
  148. CmFloor = new {
  149. f.CmFacility.CmFloor.Name,
  150. },
  151. f.CmFacility.FirstClassId,
  152. FirstClass = new {
  153. f.CmFacility.FmsFacilityCodeClass.Name,
  154. },
  155. f.CmFacility.SecondClassId,
  156. SecondClass = new {
  157. f.CmFacility.FmsFacilityCodeClass1.Name,
  158. },
  159. f.CmFacility.ThirdClassId,
  160. ThirdClass = new {
  161. f.CmFacility.FmsFacilityCodeClass2.Name,
  162. },
  163. },
  164. }).ToList()
  165. }).FirstOrDefault();
  166. return Ok(item);
  167. }
  168. [HttpPost("[action]")]
  169. public IActionResult SaveExpectedRequest([FromBody] FmsWorkSchedule data) {
  170. _logger.LogInformation("SaveExpectedRequest");
  171. //_logger.LogInformation(data.Dump());
  172. ModelState.RemoveStartOf("FmsWorkScheduleToFacility");
  173. ModelState.RemoveStartOf("CmFile");
  174. if (ModelState.IsValid) {
  175. _service.SaveExpectedRequest(data);
  176. } else {
  177. return BadRequest(ModelState);
  178. }
  179. return Ok();
  180. }
  181. /// <summary>
  182. /// 정기점검 계획 목록
  183. /// </summary>
  184. [HttpGet("[action]")]
  185. public IActionResult RegularList([FromQuery] PagingRequest req, [FromQuery] string siteId, bool businessAuth) {
  186. return List(req, siteId, new int[] { 1 }, businessAuth);
  187. }
  188. /// <summary>
  189. /// 법정검사/법정교육 계획 목록
  190. /// </summary>
  191. [HttpGet("[action]")]
  192. public IActionResult LegalList([FromQuery] PagingRequest req, [FromQuery] string siteId, bool businessAuth) {
  193. return List(req, siteId, new int[] { 2, 7 }, businessAuth);
  194. }
  195. private IActionResult List(PagingRequest req, string siteId, int[] workTypeIds, bool businessAuth) {
  196. var query = _FilterAndSort(req);
  197. query = query.Where(
  198. x => x.SiteId == Util.ToInt(siteId)
  199. && workTypeIds.Contains(x.WorkTypeId)
  200. );
  201. query = BusinessFieldHelper.Apply<FmsWorkSchedule>(_context, User, businessAuth, query);
  202. var list = query.Select(x => new {
  203. x.SiteId,
  204. x.WorkScheduleId,
  205. x.WorkTypeId,
  206. WorkType = new {
  207. x.WorkType.Name
  208. },
  209. x.BusinessFieldId,
  210. BusinessField = new {
  211. x.CmBusinessField.Name
  212. },
  213. x.InspectionAgencyId,
  214. InspectionAgency = new {
  215. x.CmPartner.Name
  216. },
  217. x.Name,
  218. x.CycleSize,
  219. x.CycleUnitId,
  220. CycleUnit = new {
  221. x.FmsWorkCodeCycleUnit.Name
  222. },
  223. x.HolidayWorkTypeId,
  224. HolidayWorkType = new {
  225. x.FmsWorkCodeHolidayWorkType.Name
  226. },
  227. x.WorkDueDate,
  228. x.UpdateUserId,
  229. x.WorkPartnerName,
  230. x.WorkPrice,
  231. x.WorkItem,
  232. x.IsUse,
  233. x.IsAutoOrder,
  234. });
  235. var paging = PagingList.Pagination(list, req.page, req.limit);
  236. return Ok(paging);
  237. }
  238. [HttpGet("[action]")]
  239. public IActionResult Regular([FromQuery] string siteId, [FromQuery] string workScheduleId) {
  240. return Get(siteId, workScheduleId, 1);
  241. }
  242. [HttpGet("[action]")]
  243. public IActionResult Legal([FromQuery] string siteId, [FromQuery] string workScheduleId) {
  244. return Get(siteId, workScheduleId, 2);
  245. }
  246. private IActionResult Get(string siteId, string workScheduleId, int workTypeId) {
  247. var query = _service.GetAll()
  248. .Where(x => x.SiteId == Util.ToInt(siteId) && x.WorkScheduleId == Util.ToInt(workScheduleId));
  249. var item = query.Select(x => new {
  250. x.SiteId,
  251. x.WorkScheduleId,
  252. x.WorkTypeId,
  253. WorkType = new {
  254. x.WorkType.Name
  255. },
  256. x.BusinessFieldId,
  257. BusinessField = new {
  258. x.CmBusinessField.Name
  259. },
  260. x.InspectionAgencyId,
  261. InspectionAgency = new {
  262. x.CmPartner.Name
  263. },
  264. x.Name,
  265. x.CycleSize,
  266. x.CycleUnitId,
  267. CycleUnit = new {
  268. x.FmsWorkCodeCycleUnit.Name
  269. },
  270. x.HolidayWorkTypeId,
  271. HolidayWorkType = new {
  272. x.FmsWorkCodeHolidayWorkType.Name
  273. },
  274. x.WorkDueDate,
  275. x.UpdateUserId,
  276. x.WorkPartnerName,
  277. x.WorkPrice,
  278. x.WorkItem,
  279. x.IsUse,
  280. x.IsAutoOrder,
  281. FmsWorkScheduleToFacility = x.FmsWorkScheduleToFacility.Select(f => new {
  282. f.SiteId,
  283. f.WorkScheduleId,
  284. f.FacilityCode,
  285. CmFacility = new {
  286. f.CmFacility.Name,
  287. f.CmFacility.BuildingId,
  288. CmBuilding = new {
  289. f.CmFacility.CmBuilding.Name,
  290. },
  291. f.CmFacility.FloorId,
  292. CmFloor = new {
  293. f.CmFacility.CmFloor.Name,
  294. },
  295. f.CmFacility.FirstClassId,
  296. FirstClass = new {
  297. f.CmFacility.FmsFacilityCodeClass.Name,
  298. },
  299. f.CmFacility.SecondClassId,
  300. SecondClass = new {
  301. f.CmFacility.FmsFacilityCodeClass1.Name,
  302. },
  303. f.CmFacility.ThirdClassId,
  304. ThirdClass = new {
  305. f.CmFacility.FmsFacilityCodeClass2.Name,
  306. },
  307. },
  308. }).ToList()
  309. }).FirstOrDefault();
  310. return Ok(item);
  311. }
  312. [HttpPost("[action]")]
  313. public IActionResult Save([FromBody] FmsWorkSchedule data) {
  314. _logger.LogInformation("Save");
  315. //_logger.LogInformation(data.Dump());
  316. ModelState.RemoveStartOf("FmsWorkScheduleToFacility");
  317. if (ModelState.IsValid) {
  318. _service.Save(data);
  319. } else {
  320. return BadRequest(ModelState);
  321. }
  322. return Ok();
  323. }
  324. [HttpDelete("[action]/{siteId}/{workRequestId}")]
  325. public IActionResult DeleteWithResult(int siteId, int workRequestId) {
  326. _service.DeleteWithResult(siteId, workRequestId);
  327. return Ok();
  328. }
  329. [HttpDelete("{siteId}/{workScheduleId}")]
  330. public IActionResult Delete(int siteId, int workScheduleId) {
  331. _service.Delete(siteId, workScheduleId);
  332. return Ok();
  333. }
  334. [HttpGet("[action]")]
  335. public IActionResult GetUpdateScheduleWarningMessage([FromQuery] string siteId, [FromQuery] string workScheduleId) {
  336. return Ok(_service.GetUpdateScheduleWarningMessage(Util.ToInt(siteId), Util.ToInt(workScheduleId)));
  337. }
  338. // 검색 & 정렬 공통
  339. private IQueryable<FmsWorkSchedule> _FilterAndSort(PagingRequest req) {
  340. var query = _service.GetAll();
  341. // 기본 Entity 검색
  342. query = query.Filter(req.conditions);
  343. // 기본 Entity 정렬
  344. query = query.Sort(req.sort);
  345. return query;
  346. }
  347. }
  348. }