编辑 | blame | 历史 | 原始文档
using CMS.Extensions.Abp.AspNetCore.Mvc.Filters;
using CMS.Framework.AspNetCore.Users;
using CMS.Plugin.$NameSpacePath$.Application.Contracts.Dtos.$EntityName$;
using CMS.Plugin.$NameSpacePath$.Application.Contracts.Services;
using CmsQueryExtensions.Entitys;
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.$NameSpacePath$.Controller
{
    /// 
    /// $ChinaComment$·þÎñ
    /// 
    [ApiController]
    [TypeFilter(typeof(CMSLanguageFilter))]
    [TypeFilter(typeof(CMSUowActionFilter))]
    [TypeFilter(typeof(CMSAuditActionFilter))]
    [TypeFilter(typeof(CMSExceptionFilter))]
    [Route("api/v{version:apiVersion}/$NameSpacePath$/[controller]")]
    public class $EntityName$Controller : ControllerBase
    {
        private readonly I$EntityName$AppService _$EntityInstanceName$AppService;
        private readonly ICurrentUser _currentUser;

        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The $EntityInstanceName$ application service.
        public $EntityName$Controller(I$EntityName$AppService $EntityInstanceName$AppService, ICurrentUser currentUser)
        {
            _$EntityInstanceName$AppService = $EntityInstanceName$AppService;
            _currentUser = currentUser;
        }

        /// 
        /// »ñÈ¡$ChinaComment$
        /// 
        /// Ö÷¼üID
        /// 
        [HttpGet]
        [Route("{id}")]
        public virtual Task<$EntityName$Dto> GetAsync(Guid id)
        {
            return _$EntityInstanceName$AppService.GetAsync(id);
        }

        /// 
        /// ·ÖÒ³»ñÈ¡$ChinaComment$µÄÁбí.
        /// 
        /// ²éѯ²ÎÊý
        /// 
        [HttpGet]
        [Route("Page")]
        public virtual Task> GetListAsync([FromQuery] Get$EntityName$Input input)
        {
            return _$EntityInstanceName$AppService.GetListAsync(input);
        }

        ///  
        /// ¸ù¾ÝÌõ¼þ»ñÈ¡$ChinaComment$
        ///  
        /// ²éѯ²ÎÊý 
        ///  
        [HttpPost]
        [Route("FindListByFilter")]
        public virtual async Task> FindListByFilterAsync([FromBody]Get$EntityName$Input input)
        {
            return await _$EntityInstanceName$AppService.FindListByFilterAsync(input);
        }
        ///  
        /// ¸ù¾ÝÌõ¼þ»ñÈ¡µ¥¸ö$ChinaComment$
        ///  
        /// ²éѯ²ÎÊý 
        ///  
        [HttpPost]
        [Route("FindSingleByFilter")]
        public virtual async Task<$EntityName$Dto> FindSingleByFilterAsync([FromBody]Get$EntityName$Input input)
        {

            return await _$EntityInstanceName$AppService.FindSingleByFilterAsync(input);
        }

        /// 
        /// ´´½¨$ChinaComment$
        /// 
        /// ´´½¨²ÎÊý
        /// 
        [Authorize]
        [HttpPost]
        public virtual Task<$EntityName$Dto> CreateAsync($EntityName$CreateDto input)
        {
            input.CreatorName = _currentUser.UserAccount;//´´½¨ÈË
            return _$EntityInstanceName$AppService.CreateAsync(input);
        }

        /// 
        /// ¸üÐÂ$ChinaComment$
        /// 
        /// Ö÷¼üID
        /// ¸üвÎÊý
        /// 
        [Authorize]
        [HttpPut]
        [Route("{id}")]
        public virtual Task<$EntityName$Dto> UpdateAsync(Guid id, $EntityName$UpdateDto input)
        {
            input.LastModifierName = _currentUser.UserAccount;//ÐÞ¸ÄÈË
            return _$EntityInstanceName$AppService.UpdateAsync(id, input);
        }

        /// 
        /// ¿Ë¡$ChinaComment$
        /// 
        /// Id¼¯ºÏ
        /// 
        [Authorize]
        [HttpPost]
        [Route("Clone")]
        public virtual Task> CloneAsync([FromBody] IEnumerable ids)
        {
            MyCurrentUser myCurrentUser = new MyCurrentUser()
            {
                UserAccount = _currentUser.UserAccount,
                UserId = _currentUser.UserId
            };
            return _$EntityInstanceName$AppService.CloneAsync(ids, myCurrentUser);
        }

        /// 
        /// ɾ³ý$ChinaComment$
        /// 
        /// Ö÷¼ü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 _$EntityInstanceName$AppService.DeletePermanentlyAsync(id, myCurrentUser);//ÎïÀíɾ³ý
        }

        /// 
        /// ÅúÁ¿É¾³ý$ChinaComment$
        /// 
        /// Ö÷¼ü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 _$EntityInstanceName$AppService.BatchDeletePermanentlyAsync(ids, myCurrentUser);//ÎïÀíɾ³ý
        }

        /// 
        /// µ÷ÕûÅÅÐò$ChinaComment$
        /// 
        /// Ö÷¼üID
        /// 
        [HttpPut]
        [Route("{id}/AdjustSort/{sort}")]
        public virtual Task AdjustSortAsync(Guid id, int sort)
        {
            return _$EntityInstanceName$AppService.AdjustSortAsync(id, sort);
        }

        /// 
        /// µ¼Èë$ChinaComment$
        /// 
        /// 
        [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 $EntityInstanceName$Rows = sheetNames.Contains("ÅäÖÃ") ? MiniExcel.Query<$EntityName$sImportModel.$EntityName$ImportModel>(stream, sheetName: "ÅäÖÃ").ToList() : new();

            if (!$EntityInstanceName$Rows.Any())
            {
                throw new UserFriendlyException("Çë¼ì²éµ¼ÈëµÄ±í¸ñ");
            }

            MyCurrentUser myCurrentUser = new MyCurrentUser()
            {
                UserAccount = _currentUser.UserAccount,
                UserId = _currentUser.UserId
            };
            await _$EntityInstanceName$AppService.ImportAsync(new $EntityName$sImportModel
            {
                $EntityName$s = $EntityInstanceName$Rows,
           },myCurrentUser); 

            return Ok();
        }

        /// 
        /// µ¼³ö$ChinaComment$
        /// 
        /// 
        [HttpGet]
        [Route("Export")]
        public virtual async Task ExportAsync([FromQuery] Get$EntityName$Input input)
        {
            input.MaxResultCount = int.MaxValue;
            var exportData = await _$EntityInstanceName$AppService.ExportAsync(input);
            var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Resources/Templates/$EntityName$µ¼³öÄ£°å.xlsx");
            if (!System.IO.File.Exists(templatePath))
            {
                templatePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty, $"Resources/Templates/$EntityName$µ¼³öÄ£°å.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" };
        }
    }
}