using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using Renci.SshNet; using Ropin.Inspection.Api.Common; using Ropin.Inspection.Api.Controllers; using Ropin.Inspection.Model; using Ropin.Inspection.Model.ViewModel.DEV; using Ropin.Inspection.Service; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; namespace Ropin.Inspection.Api { public class TmtnDevOpsContentController : BaseController { public ILogger _logger { get; } private readonly ITmtnDevOpsContentService _TmtnDevOpsContentService; /// /// 构造函数 /// /// /// public TmtnDevOpsContentController(ITmtnDevOpsContentService TmtnDevOpsContentService, ILogger logger) { _TmtnDevOpsContentService = TmtnDevOpsContentService; _logger = logger; } /// /// 通过id获取运维内容信息 /// /// /// [HttpGet("GetDevOpsContentAsync/{id}")] public async Task GetDevOpsContentAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TmtnDevOpsContentService.GetConditionAsync(new TmtnDevOpsContentSearchModel { C_ID = id }); return new ApiResult(content.FirstOrDefault()); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 获取所有运维内容 /// /// [HttpGet("GetDevOpsContentsAsync")] public async Task GetDevOpsContentsAsync() { try { var contentList = await _TmtnDevOpsContentService.GetAllAsync(); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过运维内容名称条件查询 /// /// /// [HttpPost("GetDevOpsContentsByAsync")] public async Task GetDevOpsContentsByAsync(TmtnDevOpsContentSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } searchModel.IsPagination = false; try { var contentList = await _TmtnDevOpsContentService.GetConditionAsync(searchModel); return new ApiResult>(new PagesModel(contentList, searchModel)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 创建运维内容 /// /// /// [HttpPost("CreateDevOpsContentAsync")] public async Task CreateDevOpsContentAsync(TmtnDevOpsContentViewModel content) { if (content == null) { return new ApiResult(ReturnCode.ArgsError); } try { await _TmtnDevOpsContentService.CreateOneAsync(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 删除运维内容 /// /// /// [HttpDelete("DeleteDevOpsContentAsync/{id}")] public async Task DeleteDevOpsContentAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TmtnDevOpsContentService.DeleteAsync(id); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 更新运维内容 /// /// /// /// [HttpPut("UpdateDevOpsContentAsync/{id}")] public async Task UpdateDevOpsContentAsync(string id, TmtnDevOpsContentUpdateModel updateModel) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TmtnDevOpsContentService.UpdateAsync(id, updateModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } [HttpPost("ImportExcel")] public async Task ImportExcel([Required]string storeCode, [Required] IFormFile file) { try { var exName = file.FileName.Split('.').Last().ToLower(); if (exName != "xlsx" && exName != "xls") { throw new Exception(":不是Excel文件"); } IWorkbook wk = null; if (exName == "xlsx") { wk = new XSSFWorkbook(file.OpenReadStream()); } else { wk = new HSSFWorkbook(file.OpenReadStream()); } DevOpsContentImportModel importModel = new DevOpsContentImportModel(); importModel.DevOpsContents = new List(); //获取第一个sheet ISheet sheet = wk.GetSheetAt(0); for (int i = 1; i <= sheet.LastRowNum; i++) { var row = sheet.GetRow(i); importModel.DevOpsContents.Add(new TmtnDevOpsContentViewModel { C_ID = Guid.NewGuid().ToString(), C_Number = row.Cells[0].ToString(), I_Sort = Convert.ToInt32(row.Cells[1].ToString()), C_Name = row.Cells[2].ToString(), C_AlarmLevel = row.Cells[3].ToString(), C_Status = row.Cells[4].ToString(), C_Remark = row.Cells[5].ToString(), C_CreateBy = GetUserId(User), D_CreateOn = DateTime.Now, C_StoreCode = storeCode }); } await _TmtnDevOpsContentService.ImportExecl(importModel); return new ApiResult(ReturnCode.Success); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } } }