namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：码表类型管理  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：  2023/8/9 10:01:35 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("数据字典", Name = "DictType", Tag = "字典类型", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
        public class DictTypeService : IDictTypeService, ITransient, IDynamicApiController
        {
            private readonly ISqlSugarClient _dbContext;
            public DictTypeService(ISqlSugarClient db)
            {
                _dbContext = db;
            }
            /// <summary>
            /// 增加
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            [HttpPost]
            public async Task<bool> Add(DictTypeAddInput input)
            {
                DictTypeEntity entity = input.Adapt<DictTypeEntity>();
                return await _dbContext.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandIdentityIntoEntityAsync();
            }
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            /// <exception cref="NotImplementedException"></exception>
            [HttpPost]
            public async Task<bool> Del(DictTypeDelInput input)
            {
                var res = await _dbContext.Deleteable<DictTypeEntity>().Where(x => x.Id == input.Id).ExecuteCommandHasChangeAsync();
                return res;
            }
            /// <summary>
            /// 详情
            /// </summary>
            /// <param name="Id"></param>
            /// <returns></returns>
            /// <exception cref="NotImplementedException"></exception>
            [HttpGet]
            public async Task<DictTypeOutput> Detail(string Id)
            {
                var entity = await _dbContext.Queryable<DictTypeEntity>().FirstAsync(x => x.Id == Id);
                DictTypeOutput output = entity.Adapt<DictTypeOutput>();
                return output;
            }
            /// <summary>
            /// 列表
            /// </summary>
            /// <returns></returns>
            /// <exception cref="NotImplementedException"></exception>
            [HttpPost]
            public async Task<List<DictTypeOutput>> List(DictTypeQueryInput input)
            {
                var entity = await _dbContext.Queryable<DictTypeEntity>().ToListAsync();
                List<DictTypeOutput> output = entity.Adapt<List<DictTypeOutput>>();
                return output;
            }
            /// <summary>
            /// 分页
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            [HttpPost]
            public async Task<SqlSugarPagedList<DictTypeOutput>> PagedList(DictTypeQueryPageInput input)
            {
                var entity = await _dbContext.Queryable<DictTypeEntity>()
                    .WhereIF(!string.IsNullOrEmpty(input.Code), x => x.Code.Contains(input.Code))
                     .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name))
                    .ToPagedListAsync(input.PageIndex, input.PageSize);
                SqlSugarPagedList<DictTypeOutput> output = entity.Adapt<SqlSugarPagedList<DictTypeOutput>>();
                return output;
            }
            /// <summary>
            /// 下拉
            /// </summary>
            /// <returns></returns>
            /// <exception cref="NotImplementedException"></exception>
            [HttpGet]
            public async Task<dynamic> SelectList()
            {
                var entity = await _dbContext.Queryable<DictTypeEntity>().Select(x => new
                {
                    key = x.Id,
                    value = x.Name
                }).ToListAsync();
                return entity;
            }
            /// <summary>
            /// 更新
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            /// <exception cref="NotImplementedException"></exception>
            [HttpPost]
            public async Task<bool> Update(DictTypeUpdateInput input)
            {
                var entity = await _dbContext.Queryable<DictTypeEntity>().Where(x => x.Id == input.Id).FirstAsync();
                entity = input.Adapt<DictTypeEntity>();
                var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
                return res;
            }
        }
    }