using Ao.Lang;
using CMS.Extensions.Abp.AspNetCore.Mvc.Filters;
using CMS.Framework.AspNetCore.Users;
using CMS.Plugin.PipeLineLems.Application.Contracts.Dtos.CallMaterialOrderRecord;
using CMS.Plugin.PipeLineLems.Application.Contracts.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System.Reflection;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using CmsQueryExtensions.Entitys;
namespace CMS.Plugin.PipeLineLems.Controller
{
///
/// 叫料记录表服务
///
[ApiController]
[TypeFilter(typeof(CMSLanguageFilter))]
[TypeFilter(typeof(CMSUowActionFilter))]
[TypeFilter(typeof(CMSAuditActionFilter))]
[TypeFilter(typeof(CMSExceptionFilter))]
[Route("api/v{version:apiVersion}/PipeLineLems/[controller]")]
public class CallMaterialOrderRecordController : ControllerBase
{
private readonly ICallMaterialOrderRecordAppService _callMaterialOrderRecordAppService;
private readonly ICurrentUser _currentUser;
///
/// Initializes a new instance of the class.
///
/// The callMaterialOrderRecord application service.
public CallMaterialOrderRecordController(ICallMaterialOrderRecordAppService callMaterialOrderRecordAppService, ICurrentUser currentUser)
{
_callMaterialOrderRecordAppService = callMaterialOrderRecordAppService;
_currentUser = currentUser;
}
///
/// 获取叫料记录表
///
/// 主键ID
///
[HttpGet]
[Route("{id}")]
public virtual Task GetAsync(Guid id)
{
return _callMaterialOrderRecordAppService.GetAsync(id);
}
///
/// 分页获取叫料记录表的列表.
///
/// 查询参数
///
[HttpGet]
[Route("Page")]
public virtual Task> GetListAsync([FromQuery] GetCallMaterialOrderRecordInput input)
{
return _callMaterialOrderRecordAppService.GetListAsync(input);
}
///
/// 创建叫料记录表
///
/// 创建参数
///
[Authorize]
[HttpPost]
public virtual Task CreateAsync(CallMaterialOrderRecordCreateDto input)
{
input.CreatorName = _currentUser.UserAccount;//创建人
return _callMaterialOrderRecordAppService.CreateAsync(input);
}
///
/// 更新叫料记录表
///
/// 主键ID
/// 更新参数
///
[Authorize]
[HttpPut]
[Route("{id}")]
public virtual Task UpdateAsync(Guid id, CallMaterialOrderRecordUpdateDto input)
{
input.LastModifierName = _currentUser.UserAccount;//修改人
return _callMaterialOrderRecordAppService.UpdateAsync(id, input);
}
///
/// 克隆叫料记录表
///
/// Id集合
///
[Authorize]
[HttpPost]
[Route("Clone")]
public virtual Task> CloneAsync([FromBody] IEnumerable ids)
{
MyCurrentUser myCurrentUser = new MyCurrentUser()
{
UserAccount = _currentUser.UserAccount,
UserId = _currentUser.UserId
};
return _callMaterialOrderRecordAppService.CloneAsync(ids, myCurrentUser);
}
///
/// 删除叫料记录表
///
/// 主键ID
///
[Authorize]
[HttpDelete]
[Route("{id}")]
public virtual Task DeleteAsync(Guid id)
{
MyCurrentUser myCurrentUser = new MyCurrentUser()
{
UserAccount = _currentUser.UserAccount,
UserId = _currentUser.UserId
};
//return _wmsMaterialAppService.DeleteAsync(id,myCurrentUser);//逻辑删除
return _callMaterialOrderRecordAppService.DeletePermanentlyAsync(id, myCurrentUser);//物理删除
}
///
/// 批量删除叫料记录表
///
/// 主键ID集合
///
[Authorize]
[HttpDelete]
public virtual Task DeleteAsync([FromBody] IEnumerable ids)
{
MyCurrentUser myCurrentUser = new MyCurrentUser()
{
UserAccount = _currentUser.UserAccount,
UserId = _currentUser.UserId
};
// return _wmsMaterialAppService.DeleteManyAsync(ids,myCurrentUser);//逻辑删除
return _callMaterialOrderRecordAppService.BatchDeletePermanentlyAsync(ids, myCurrentUser);//物理删除
}
///
/// 调整排序叫料记录表
///
/// 主键ID
///
[HttpPut]
[Route("{id}/AdjustSort/{sort}")]
public virtual Task AdjustSortAsync(Guid id, int sort)
{
return _callMaterialOrderRecordAppService.AdjustSortAsync(id, sort);
}
///
/// 导入叫料记录表
///
///
[Authorize]
[HttpPost]
[Route("Import")]
public virtual async Task ImportAsync(IFormFile file)
{
using var stream = new MemoryStream();
await file.CopyToAsync(stream);
stream.Seek(0L, SeekOrigin.Begin);
var sheetNames = stream.GetSheetNames();
var callMaterialOrderRecordRows = sheetNames.Contains("配置") ? MiniExcel.Query(stream, sheetName: "配置").ToList() : new();
if (!callMaterialOrderRecordRows.Any())
{
throw new UserFriendlyException("请检查导入的表格");
}
MyCurrentUser myCurrentUser = new MyCurrentUser()
{
UserAccount = _currentUser.UserAccount,
UserId = _currentUser.UserId
};
await _callMaterialOrderRecordAppService.ImportAsync(new CallMaterialOrderRecordsImportModel
{
CallMaterialOrderRecords = callMaterialOrderRecordRows,
},myCurrentUser);
return Ok();
}
///
/// 导出叫料记录表
///
///
[HttpGet]
[Route("Export")]
public virtual async Task ExportAsync([FromQuery] GetCallMaterialOrderRecordInput input)
{
input.MaxResultCount = int.MaxValue;
var exportData = await _callMaterialOrderRecordAppService.ExportAsync(input);
var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Resources/Templates/CallMaterialOrderRecord导出模板.xlsx");
if (!System.IO.File.Exists(templatePath))
{
templatePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty, $"Resources/Templates/CallMaterialOrderRecord导出模板.xlsx");
}
var memoryStream = new MemoryStream();
await memoryStream.SaveAsByTemplateAsync(templatePath, exportData.Sheets);
memoryStream.Seek(0L, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = $"{exportData.FileName}_{DateTime.Now:yyyyMMddhhmmss}.xlsx" };
}
}
}