namespace BPA.MES.Base.Application.Entitys
{
    /// <summary>
    /// 名    称 ：分组信息 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：  2023/3/10 14:03:06 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings(Name = "GroupInfo", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class GroupInfoService : IGroupInfoService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public GroupInfoService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(GroupInfoAddInput input)
        {
            GroupInfoEntity entity = input.Adapt<GroupInfoEntity>();
            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(GroupInfoDelInput input)
        {
            var res = await _dbContext.Deleteable<GroupInfoEntity>().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<GroupInfoOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<GroupInfoEntity>().FirstAsync();
            GroupInfoOutput output = entity.Adapt<GroupInfoOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost, AllowAnonymous]
        public async Task<List<GroupInfoOutput>> List()
        {
            var entity = await _dbContext.Queryable<GroupInfoEntity>().ToListAsync();
            List<GroupInfoOutput> output = entity.Adapt<List<GroupInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<GroupInfoOutput>> PagedList(GroupInfoQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<GroupInfoEntity>()
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<GroupInfoOutput> output = entity.Adapt<SqlSugarPagedList<GroupInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(GroupInfoUpdateInput input)
        {
            var entity = await _dbContext.Queryable<GroupInfoEntity>().FirstAsync(x=>x.Id==input.Id);
            if (entity == null)
            {
                throw Oops.Bah("数据不存在!");
            }
            entity = input.Adapt<GroupInfoEntity>();
            var res = await _dbContext.Updateable(entity).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
