using Ao.Lang;
using CMS.Extensions.Abp.AspNetCore.Mvc.Filters;
using CMS.Plugin.HIAWms.Application.Contracts.Dtos.WmsPlaces;
using CMS.Plugin.HIAWms.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;
namespace CMS.Plugin.HIAWms.Controller
{
    /// 
    /// WmsPlace服务
    /// 
    [ApiController]
    [TypeFilter(typeof(CMSLanguageFilter))]
    [TypeFilter(typeof(CMSUowActionFilter))]
    [TypeFilter(typeof(CMSAuditActionFilter))]
    [TypeFilter(typeof(CMSExceptionFilter))]
    [Route("api/v{version:apiVersion}/HIAWms/[controller]")]
    public class WmsPlaceController : ControllerBase
    {
        private readonly IWmsPlaceAppService _wmsplaceAppService;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The wmsplace application service.
        public WmsPlaceController(IWmsPlaceAppService wmsplaceAppService)
        {
            _wmsplaceAppService = wmsplaceAppService;
        }
        /// 
        /// 获取wmsplace.
        /// 
        /// 标识符.
        /// 
        [HttpGet]
        [Route("{id}")]
        public virtual Task GetAsync(Guid id)
        {
            return _wmsplaceAppService.GetAsync(id);
        }
        /// 
        /// 获取wmsplace的列表.
        /// 
        /// 输入.
        /// 
        [HttpGet]
        public virtual Task> GetListAsync([FromQuery] GetWmsPlacesInput input)
        {
            return _wmsplaceAppService.GetListAsync(input);
        }
        /// 
        /// 创建wmsplace.
        /// 
        /// 输入.
        /// 
        //[Authorize]
        [HttpPost]
        public virtual Task CreateAsync(WmsPlaceCreateDto input)
        {
            return _wmsplaceAppService.CreateAsync(input);
        }
        /// 
        /// 更新wmsplace.
        /// 
        /// 标识符.
        /// 输入.
        /// 
        //[Authorize]
        [HttpPut]
        [Route("{id}")]
        public virtual Task UpdateAsync(Guid id, WmsPlaceUpdateDto input)
        {
            return _wmsplaceAppService.UpdateAsync(id, input);
        }
        /// 
        /// 克隆WmsPlace.
        /// 
        /// Id集合.
        /// 
        //[Authorize]
        [HttpPost]
        [Route("Clone")]
        public virtual Task> CloneAsync([FromBody] IEnumerable ids)
        {
            return _wmsplaceAppService.CloneAsync(ids);
        }
        /// 
        /// 删除wmsplace.
        /// 
        /// 标识符.
        /// 
        //[Authorize]
        [HttpDelete]
        [Route("{id}")]
        public virtual Task DeleteAsync(Guid id)
        {
            return _wmsplaceAppService.DeleteAsync(id);
        }
        /// 
        /// 批量删除wmsplace.
        /// 
        /// The ids.
        /// 
        //[Authorize]
        [HttpDelete]
        public virtual Task DeleteAsync([FromBody] IEnumerable ids)
        {
            return _wmsplaceAppService.DeleteManyAsync(ids);
        }
        /// 
        /// 调整排序.
        /// 
        /// 标识符.
        /// 
        [HttpPut]
        [Route("{id}/AdjustSort/{sort}")]
        public virtual Task AdjustSortAsync(Guid id, int sort)
        {
            return _wmsplaceAppService.AdjustSortAsync(id, sort);
        }
        /// 
        /// 导入wmsplace.
        /// 
        /// 
        [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 wmsplaceRows = sheetNames.Contains("配置") ? MiniExcel.Query(stream, sheetName: "配置").ToList() : new();
            if (!wmsplaceRows.Any())
            {
                throw new UserFriendlyException("请检查导入的表格");
            }
            await _wmsplaceAppService.ImportAsync(new WmsPlacesImportModel
            {
                WmsPlaces = wmsplaceRows,
            });
            return Ok();
        }
        /// 
        /// 导出wmsplace.
        /// 
        /// 
        [HttpGet]
        [Route("Export")]
        public virtual async Task ExportAsync([FromQuery] GetWmsPlacesInput input)
        {
            input.MaxResultCount = int.MaxValue;
            var exportData = await _wmsplaceAppService.ExportAsync(input);
            var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Resources/Templates/WmsPlace导出模板.xlsx");
            if (!System.IO.File.Exists(templatePath))
            {
                templatePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty, $"Resources/Templates/WmsPlace导出模板.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" };
        }
    }
}