编辑 | blame | 历史 | 原始文档
using Admin.NET.Core.BasicInformation.Entity;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.Extras.Admin.NET;
using Furion.Extras.Admin.NET.Extension;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using PandaWork.Infrastructure;
using System.Data;
using System.Linq.Dynamic.Core;
using Furion.LinqBuilder;
using System.Linq.Expressions;
using Admin.NET.Core.Helper.ExcelHelper;

namespace Admin.NET.Application
{
    /// 
	/// $ChinaComment$·þÎñ½Ó¿ÚʵÏÖ
	/// 	
    [ApiDescriptionSettings("$GroupName$", Name = "$ChinaComment$", Order = 100)]
    public class $EntityName$Service : ControllerBase, I$EntityName$Service, IDynamicApiController, ITransient
    {
        private readonly IRepository<$EntityName$, MasterDbContextLocator> _$EntityName_FirstLetterLower$Rep;
        /// 
        /// $ChinaComment$·þÎñ¹¹Ô캯Êý
        /// 
        /// $ChinaComment$·þÎñ²Ö´¢¶ÔÏó
        public $EntityName$Service(IRepository<$EntityName$, MasterDbContextLocator> $EntityName_FirstLetterLower$Rep)
        {
             _$EntityName_FirstLetterLower$Rep = $EntityName_FirstLetterLower$Rep;
        }

        /// 
        /// ·ÖÒ³²éѯ$ChinaComment$
        /// 
        /// ·ÖÒ³²éѯ²ÎÊý
        /// 
        [HttpGet("/$EntityName$/page")]
        [DisableOpLog]
        public async Task> Page([FromQuery] $EntityName$PageInput input)
        {
            if (input == null)
            {
                throw Oops.Oh(ErrorCode.D4008);
            }

            #region ¶¯Ì¬¹¹Ôì²éѯÌõ¼þ

            //¶¯Ì¬¹¹Ôì²éѯÌõ¼þ
            var whereConditions = DynamicGetQueryParams(input);

            #endregion

            var pageList= await _$EntityName_FirstLetterLower$Rep.DetachedEntities
                                     .Where(whereConditions.data)
                                     .OrderBy(PageInputOrder.OrderBuilder<$EntityName$PageInput>(input))
                                     .ProjectToType<$EntityName$Output>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);
            return pageList;
        }

        /// 
        /// Ôö¼Ó$ChinaComment$
        /// 
        /// Ôö¼Ó²ÎÊý
        /// 
        [HttpPost("/$EntityName$/add")]
        public async Task Add([FromBody] Add$EntityName$Input input)
        {
            if (input == null)
            {
                throw Oops.Oh(ErrorCode.D4008);
            }
            var $EntityName_FirstLetterLower$ = input.Adapt<$EntityName$>();
            EntityEntry<$EntityName$> entityEntry = await _$EntityName_FirstLetterLower$Rep.InsertAsync($EntityName_FirstLetterLower$);
            return entityEntry.Entity.Id;
        }

        /// 
        /// ¸üÐÂ$ChinaComment$
        /// 
        /// ¸üвÎÊý
        /// 
        [HttpPost("/$EntityName$/update")]
        public async Task Update([FromBody] Update$EntityName$Input input)
        {
            if (input == null || (input.Id) <= 0)
            {
                throw Oops.Oh(ErrorCode.D1012);
            }
            var isExist = await _$EntityName_FirstLetterLower$Rep.AnyAsync(u => u.Id == input.Id, false);
            if (!isExist)
            {
                throw Oops.Oh(ErrorCode.D1002);
            }
            var $EntityName_FirstLetterLower$ = input.Adapt<$EntityName$>();
            await _$EntityName_FirstLetterLower$Rep.UpdateAsync($EntityName_FirstLetterLower$, ignoreNullValues: true);
            return input.Id;
        }

        /// 
        /// ɾ³ý$ChinaComment$
        /// 
        /// ɾ³ý²ÎÊý
        /// 
        [HttpPost("/$EntityName$/delete")]
        public async Task Delete([FromBody] Delete$EntityName$Input input)
        {
            if (input == null || input.Id.Count == 0)
            {
                throw Oops.Oh(ErrorCode.D4008);
            }
            List<$EntityName$> list = new();
            foreach (var item in input.Id)
            {
                list.Add(new $EntityName$() { Id = item });
            }
            await _$EntityName_FirstLetterLower$Rep.DeleteAsync(list);
            return input.Id.Count;
        }

        /// 
        /// »ñÈ¡$ChinaComment$
        /// 
        /// »ñÈ¡¶ÔÏó²ÎÊý
        /// 
        [HttpGet("/$EntityName$/detail")]
        [DisableOpLog]
        public async Task<$EntityName$Output> Get([FromQuery] Query$EntityName$Input input)
        {
            if (input == null || input.Id <= 0)
            {
                throw Oops.Oh(ErrorCode.D1012);
            }
            return (await _$EntityName_FirstLetterLower$Rep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt<$EntityName$Output>();
        }

        /// 
        /// µ¼³ö$ChinaComment$Excel
        /// 
        /// ɸѡÌõ¼þ
        /// 
        [HttpPost("/$EntityName$/export")]
        [DisableOpLog]
        public async Task Export([FromQuery] $EntityName$PageInput input)
        {
            if (input == null)
            {
                throw Oops.Oh(ErrorCode.D4008);
            }

            #region ¶¯Ì¬¹¹Ôì²éѯÌõ¼þ

            //¶¯Ì¬¹¹Ôì²éѯÌõ¼þ
            var whereConditions = DynamicGetQueryParams(input);

            #endregion

            var deliveryOrderExportExcel = await _$EntityName_FirstLetterLower$Rep.DetachedEntities
                .Where(whereConditions.data)
                .ProjectToType()
               .ToListAsync();
            var excelBaseResult = new Excel2003Result(deliveryOrderExportExcel, "$ChinaComment$¼Ç¼" + DateTime.Now.ToString("yyyyMMdd"), false, "$ChinaComment$¼Ç¼");
            return File(excelBaseResult.GetExcelStream(), "application/vnd.ms-excel");
        }

         /// 
        /// ¶¯Ì¬¹¹Ôì²éѯÌõ¼þ
        /// 
        /// ÊäÈë²ÎÊý
        /// 
        private FunReturnResultModel>> DynamicGetQueryParams($EntityName$PageInput input)
        {
            //¶¯Ì¬¹¹Ôì²éѯÌõ¼þ
            var whereConditions = WhereConditionsExtensions.GetWhereConditions<$EntityName$, $EntityName$PageInput>(input);
            if (!whereConditions.IsSuccess)
            {
                throw Oops.Oh("¶¯Ì¬¹¹Ôì²éѯÌõ¼þʧ°Ü:" + whereConditions.ErrMsg);
            }

            //Ò²¿ÉÔÙ´Î×Ô¶¨Òå¹¹½¨²éѯÌõ¼þ
            Expression> extendExpression = a => a.IsDeleted == false;
            whereConditions.data = whereConditions.data.And(extendExpression);

            return whereConditions;
        }
    }
}