123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using System;
- using System.IO;
- using System.Linq;
- using FMSAdmin.Data;
- using FMSAdmin.Entities;
- using FMSAdmin.Helpers;
- using FMSAdmin.Models;
- using FMSAdmin.Services;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using OfficeOpenXml;
- namespace FMSAdmin.Controllers {
- [Authorize]
- [ApiController]
- [ApiVersion("1")]
- [Route("api/[controller]")]
- public class Investigation2Controller : Controller {
- private readonly ILogger<Investigation2Controller> _logger;
- private readonly FMSContext _context;
- private Investigation2Service _service;
- public Investigation2Controller(
- ILogger<Investigation2Controller> logger,
- FMSContext context,
- Investigation2Service service
- ) {
- _logger = logger;
- _context = context;
- _service = service;
- }
- /// <summary>
- /// 검색 & 정렬
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- private IQueryable<CmInvestigation2> _FilterAndSort(PagingRequest req) {
- var query = _service.GetAll();
- // 기본 Entity 검색
- query = query.Filter(req.conditions);
- if (req.siteId != null) {
- query = query.Where(x => x.SiteId == Util.ToInt(req.siteId) || x.IsAllSite == true);
- }
- if (req.siteIds != null && req.siteIds.Length > 0) {
- query = query.Where(x => req.siteIds.Contains(x.SiteId));
- }
- query = query.Sort(req.sort);
- return query;
- }
- /// <summary>
- /// 셀렉트 공통
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- private dynamic _ListSelect(IQueryable<CmInvestigation2> query) {
- var list = query.Select(x => new {
- x.SiteId,
- SiteName = x.CmSite.Name,
- Investigation2GroupName = x.CmInvestigation2CodeGroup.Name,
- Investigation2TypeName = x.CmInvestigation2CodeType.Name,
- x.InvestigationNo,
- Name = x.IsAllSite ? "[본사] " + x.Name : x.Name,
- x.InvestigationGroupId,
- x.InvestigationTypeId,
- x.InvestigationId,
- x.IsAllSite
- });
- return list;
- }
- /// <summary>
- /// 목록
- /// </summary>
- /// <param name="req"></param>
- /// <param name="siteId"></param>
- /// <returns></returns>
- [HttpGet]
- public IActionResult List([FromQuery] PagingRequest req) {
- var query = _FilterAndSort(req);
- var list = _ListSelect(query);
- var paging = PagingList.Pagination(list, req.page, req.limit);
- return Ok(paging);
- }
- // 엑셀 다운로드
- [AllowAnonymous]
- [HttpGet("excel")]
- public IActionResult ExportExcel([FromQuery] PagingRequest req, string filename) {
- var query = _FilterAndSort(req);
- var list = _ListSelect(query);
- var stream = new MemoryStream();
- using (var package = new ExcelPackage(stream)) {
- var workSheet = package.Workbook.Worksheets.Add("Sheet1");
- workSheet.Cells.LoadFromCollection(list, true);
- if (req.columns != null && req.columns.Length > 0) {
- workSheet.AddStyle(req.columns, req.sort?.order?.ToLower() == "asc", filename);
- }
- package.Save();
- }
- stream.Position = 0;
- string excelName = $"excel-{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx";
- return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelName);
- }
- /// <summary>
- /// 조회
- /// </summary>
- [HttpGet("{siteId}/{id}")]
- public IActionResult Get(int siteId, int id) {
- var data = _service.Get(id, siteId);
- if (data.Count() == 0) {
- return NotFound("사이트 정보를 찾을 수 없습니다.");
- }
- var list = data.Select(x => new {
- x.InvestigationId,
- x.Name,
- x.SiteId,
- x.InvestigationGroupId,
- x.InvestigationTypeId,
- x.InvestigationNo,
- x.IsAllSite,
- CmSite = new {
- x.CmSite.Name,
- },
- CmInvestigation2CodeGroup = new {
- x.CmInvestigation2CodeGroup.Name
- },
- CmInvestigation2CodeType = new {
- x.CmInvestigation2CodeType.Name
- },
- x.Description
- });
- return Ok(list.First());
- }
- /// <summary>
- /// 수정
- /// </summary>
- [HttpPost("{siteId}/{id}")]
- public IActionResult Edit(int siteId, int id, [FromBody] CmInvestigation2 data) {
- _logger.LogInformation("Edit");
- _logger.LogInformation("id:" + id);
- _logger.LogInformation("siteId:" + siteId);
- //_logger.LogInformation(data.Dump());
- if (id != data.InvestigationId) {
- return BadRequest("잘못된 접근입니다.>>");
- }
- if (ModelState.IsValid) {
- _service.Edit(id, siteId, data);
- } else {
- return BadRequest(ModelState);
- }
- return Ok();
- }
- /// <summary>
- /// 등록
- /// </summary>
- [HttpPut]
- public IActionResult Create([FromBody] CmInvestigation2 data) {
- _logger.LogInformation("Create");
- //_logger.LogInformation(data.Dump());
- if (ModelState.IsValid) {
- data.CreateUserId = User.Identity.Name;
- _service.Create(data);
- } else {
- return BadRequest(ModelState);
- }
- return Ok();
- }
- /// <summary>
- /// 삭제
- /// </summary>
- [HttpDelete("{siteId}/{id}")]
- public IActionResult Delete(int siteId, int id) {
- _service.Delete(id, siteId);
- return Ok();
- }
- }
- }
|