namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：运行日志  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/8/21 11:16:32 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("数据日志", Name = "RunLog", Tag = "运行日志", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class RunLogService : IRunLogService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public RunLogService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(RunLogAddInput input)
        {
            RunLogEntity entity = input.Adapt<RunLogEntity>();
            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(RunLogDelInput input)
        {
            var res = await _dbContext.Deleteable<RunLogEntity>().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<RunLogOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<RunLogEntity>().FirstAsync(x => x.Id == Id);
            RunLogOutput output = entity.Adapt<RunLogOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<RunLogOutput>> List(RunLogQueryInput input)
        {
            var entity = await _dbContext.Queryable<RunLogEntity>().ToListAsync();
            List<RunLogOutput> output = entity.Adapt<List<RunLogOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<RunLogOutput>> PagedList(RunLogQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<RunLogEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.LogType), x => x.LogType.Contains(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)
                //.SplitTable(input.StartTime.Value, input.EndTime.Value)
                  .OrderByDescending(x => x.CreateDate)
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<RunLogOutput> output = entity.Adapt<SqlSugarPagedList<RunLogOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(RunLogUpdateInput input)
        {
            var entity = await _dbContext.Queryable<RunLogEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<RunLogEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}