namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：用户操作日志 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/8/21 11:16:44 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("数据日志", Name = "UserLog", Tag = "用户操作日志", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class UserLogService : IUserLogService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public UserLogService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(UserLogAddInput input)
        {
            UserLogEntity entity = input.Adapt<UserLogEntity>();
            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(UserLogDelInput input)
        {
            var res = await _dbContext.Deleteable<UserLogEntity>().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<UserLogOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<UserLogEntity>().FirstAsync(x => x.Id == Id);
            UserLogOutput output = entity.Adapt<UserLogOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<UserLogOutput>> List(UserLogQueryInput input)
        {
            var entity = await _dbContext.Queryable<UserLogEntity>().ToListAsync();
            List<UserLogOutput> output = entity.Adapt<List<UserLogOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<UserLogOutput>> PagedList(UserLogQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<UserLogEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.UserName), x => x.UserName.Contains(input.UserName))
                .WhereIF(!string.IsNullOrEmpty(input.MsgInfo), x => x.MsgInfo.Contains(input.MsgInfo))
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<UserLogOutput> output = entity.Adapt<SqlSugarPagedList<UserLogOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(UserLogUpdateInput input)
        {
            var entity = await _dbContext.Queryable<UserLogEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<UserLogEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
