namespace BPA.MES.Base.Application.Services.LogService.Services
{
    /// <summary>
    /// 名    称 ：系统日志  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/8/21 11:16:20 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("数据日志", Name = "ProgramLog", Tag = "系统日志", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class ProgramLogService : IProgramLogService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public ProgramLogService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(ProgramLogAddInput input)
        {
            ProgramLogEntity entity = input.Adapt<ProgramLogEntity>();
            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(ProgramLogDelInput input)
        {
            var res = await _dbContext.Deleteable<ProgramLogEntity>().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<ProgramLogOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<ProgramLogEntity>().FirstAsync(x => x.Id == Id);
            ProgramLogOutput output = entity.Adapt<ProgramLogOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<ProgramLogOutput>> List(ProgramLogQueryInput input)
        {
            var entity = await _dbContext.Queryable<ProgramLogEntity>().ToListAsync();
            List<ProgramLogOutput> output = entity.Adapt<List<ProgramLogOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<ProgramLogOutput>> PagedList(ProgramLogQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<ProgramLogEntity>()
                .WhereIF(input.LogType != null, x => x.LogType == input.LogType)
                .WhereIF(!string.IsNullOrEmpty(input.MsgInfo), x => x.MsgInfo.Contains(input.MsgInfo))
                .WhereIF(input.StartTime.HasVal(), x => x.CreateDate >= input.StartTime.Value)
                .WhereIF(input.EndTime.HasVal(), x => x.CreateDate <= input.EndTime.Value)
                  .OrderByDescending(x => x.CreateDate)
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<ProgramLogOutput> output = entity.Adapt<SqlSugarPagedList<ProgramLogOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(ProgramLogUpdateInput input)
        {
            var entity = await _dbContext.Queryable<ProgramLogEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<ProgramLogEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
