using System.Reflection;

namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：码表管理  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：  2023/8/9 10:01:19 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("数据字典", Name = "DictData", Tag = "字典数据", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class DictDataService: IDictDataService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public DictDataService(ISqlSugarClient db)
        {
            _dbContext = db;
     
           
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(DictDataAddInput input)
        {
            DictDataEntity entity = input.Adapt<DictDataEntity>();
            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(DictDataDelInput input)
        {
            var res = await _dbContext.Deleteable<DictDataEntity>().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<DictDataOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<DictDataEntity>().FirstAsync(x => x.Id == Id);
            DictDataOutput output = entity.Adapt<DictDataOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<DictDataOutput>> List(DictDataQueryInput input)
        {
            if (!string.IsNullOrEmpty(input.TypeCode))
            {
                var typeentity = await _dbContext.Queryable<DictTypeEntity>().Where(x => x.Code == input.TypeCode).FirstAsync();
                if (typeentity != null)
                {
                    input.TypeId = typeentity.Id;
                }
            }
            var entity = await _dbContext.Queryable<DictDataEntity>().WhereIF(!string.IsNullOrEmpty(input.TypeId), x => x.TypeId == input.TypeId).ToListAsync();
            List<DictDataOutput> output = entity.Adapt<List<DictDataOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<DictDataOutput>> PagedList(DictDataQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<DictDataEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.Value), x => x.Value.Contains(input.Value))
                .WhereIF(!string.IsNullOrEmpty(input.Code), x => x.Code.Contains(input.Code))
                .WhereIF(!string.IsNullOrEmpty(input.TypeId),x=>x.TypeId==input.TypeId)
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<DictDataOutput> output = entity.Adapt<SqlSugarPagedList<DictDataOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<DictDataEntity>().Select(x => new
            {
                key = x.Id,
                value = x.Value
            }).ToListAsync();
            return entity;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(DictDataUpdateInput input)
        {
            var entity = await _dbContext.Queryable<DictDataEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<DictDataEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
