namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：报警日志  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/8/21 11:02:36 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("数据日志", Name = "AlarmLog", Tag = "报警日志", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class AlarmLogService : IAlarmLogService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public AlarmLogService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(AlarmLogAddInput input)
        {
            AlarmLogEntity entity = input.Adapt<AlarmLogEntity>();
            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(AlarmLogDelInput input)
        {
            var res = await _dbContext.Deleteable<AlarmLogEntity>().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<AlarmLogOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<AlarmLogEntity>().FirstAsync(x => x.Id == Id);
            AlarmLogOutput output = entity.Adapt<AlarmLogOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<AlarmLogOutput>> List(AlarmLogQueryInput input)
        {
            var entity = await _dbContext.Queryable<AlarmLogEntity>().ToListAsync();
            List<AlarmLogOutput> output = entity.Adapt<List<AlarmLogOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<AlarmLogOutput>> PagedList(AlarmLogQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<AlarmLogEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.DeviceName), x => x.DeviceName.Contains(input.DeviceName))
                .WhereIF(!string.IsNullOrEmpty(input.Grade), x => x.Grade.Contains(input.Grade))
                .WhereIF(!string.IsNullOrEmpty(input.MsgInfo), x => x.MsgInfo.Contains(input.MsgInfo))
                .WhereIF(!string.IsNullOrEmpty(input.Value), x => x.Value.Contains(input.Value))
                .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<AlarmLogOutput> output = entity.Adapt<SqlSugarPagedList<AlarmLogOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(AlarmLogUpdateInput input)
        {
            var entity = await _dbContext.Queryable<AlarmLogEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<AlarmLogEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}