using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Ropin.Inspection.Api.Common; using Ropin.Inspection.Model; using Ropin.Inspection.Service; using Ropin.Inspection.Service.DEV.Interface; using System.Threading.Tasks; using System; using Ropin.Inspection.Model.ViewModel.DEV; using NPOI.SS.UserModel; using System.IO; using NPOI.Util; using NPOI.XWPF.UserModel; using NPOI.SS.Formula; using Newtonsoft.Json; namespace Ropin.Inspection.Api.Controllers.DEV { public class devHandController : BaseController { public ILogger _logger { get; } private readonly IDev_HandService _repository; /// /// 构造函数 /// /// /// public devHandController( ILogger logger, IDev_HandService repository) { _logger = logger; _repository = repository; } /// /// 创建设备手持信息 /// /// /// [HttpPost("CreatedevHandAsync")] public async Task CreatedevHandAsync(dev_HandModel content) { if (content == null) { return new ApiResult(ReturnCode.ArgsError); } try { await _repository.CreateOneAsync(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 删除设备手持信息 /// /// /// [HttpDelete("DeletedevHandAsync/{id}")] public async Task DeletedevHandAsync(Guid id) { if (id==Guid.Empty) { return new ApiResult(ReturnCode.GeneralError); } try { await _repository.DeleteAsync(id); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 更新设备手持信息 /// /// /// /// [HttpPut("UpdatedevHandAsync/{id}")] [AllowAnonymous] public async Task UpdatedevHandAsync(Guid id, dev_HandModel updateModel) { if (id==Guid.Empty) { return new ApiResult(ReturnCode.GeneralError); } try { await _repository.UpdateAsync(id, updateModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 获取设备手持信息 /// /// [HttpPost("GetdevHandAsync")] [AllowAnonymous] public async Task GetdevHandAsync(TdevHandSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } try { var contentList = await _repository.GetConditionAsync(searchModel); return new ApiResult>(new PagesModel(contentList, searchModel)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过id获取设备手持信息 /// /// [HttpPost("GetdevHandByIDAsync")] [AllowAnonymous] public async Task GetdevHandByIDAsync(Guid Id) { if (Id==Guid.Empty) { return new ApiResult(ReturnCode.ArgsError); } try { devHandViewModel data = new devHandViewModel(); data = await _repository.GetConditionByIdAsync(Id); return new ApiResult(data); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过id获取设备手持信息返回word文件 /// /// [HttpPost("GetdevHandFileByIDAsync")] [AllowAnonymous] public async Task GetdevHandFileByIDAsync(Guid Id) { try { devHandViewModel data = new devHandViewModel(); data = await _repository.GetConditionByIdAsync(Id); XWPFDocument doc = new XWPFDocument(); var paragraph = doc.CreateParagraph(); var run = paragraph.CreateRun(); run.FontSize = 16; string fileName = "null.docx"; if (data!=null) { string json = JsonConvert.SerializeObject(data); run.SetText(json); fileName = data.C_ID + ".docx"; } else { run.SetText("null"); } using (MemoryStream stream = new MemoryStream()) { doc.Write(stream); byte[] bytes = stream.ToArray(); string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; return File(bytes, contentType, fileName); } } catch (Exception ex) { throw; } } } }