namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：报警日志  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/8/21 11:02:36 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("工艺流程", Name = "Process", Tag = "工艺流程", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
        public class ProcessService : IProcessService, ITransient, IDynamicApiController
        {
            private readonly ISqlSugarClient _dbContext;
            public ProcessService(ISqlSugarClient db)
            {
                _dbContext = db;
            }
            /// <summary>
            /// 增加
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            [HttpPost]
            public async Task<bool> Add(ProcessAddInput input)
            {
                ProcessEntity entity = input.Adapt<ProcessEntity>();
                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(ProcessDelInput input)
            {
                var res = await _dbContext.Deleteable<ProcessEntity>().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<ProcessOutput> Detail(string Id)
            {
                var entity = await _dbContext.Queryable<ProcessEntity>().FirstAsync(x => x.Id == Id);
                ProcessOutput output = entity.Adapt<ProcessOutput>();
                return output;
            }
            /// <summary>
            /// 列表
            /// </summary>
            /// <returns></returns>
            /// <exception cref="NotImplementedException"></exception>
            [HttpPost]
            public async Task<List<ProcessOutput>> List(ProcessQueryInput input)
            {
                var entity = await _dbContext.Queryable<ProcessEntity>().ToListAsync();
                List<ProcessOutput> output = entity.Adapt<List<ProcessOutput>>();
                return output;
            }
            /// <summary>
            /// 分页
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            [HttpPost]
            public async Task<SqlSugarPagedList<ProcessOutput>> PagedList(ProcessQueryPageInput input)
            {
                var entity = await _dbContext.Queryable<ProcessEntity>()
                    .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name))
               
                    .OrderByDescending(x=>x.CreateTime)
                    .ToPagedListAsync(input.PageIndex, input.PageSize);
                SqlSugarPagedList<ProcessOutput> output = entity.Adapt<SqlSugarPagedList<ProcessOutput>>();
                return output;
            }
            /// <summary>
            /// 更新
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            /// <exception cref="NotImplementedException"></exception>
            [HttpPost]
            public async Task<bool> Update(ProcessUpdateInput input)
            {
                var entity = await _dbContext.Queryable<ProcessEntity>().Where(x => x.Id == input.Id).FirstAsync();
                entity = input.Adapt<ProcessEntity>();
                var res = await _dbContext.Updateable(entity).IgnoreColumns(true).IgnoreColumns(x=>x.CreateTime).ExecuteCommandHasChangeAsync();
                return res;
            }
            /// <summary>
            /// 下拉
            /// </summary>
            /// <returns></returns>
            [HttpGet]
            public async Task<dynamic> SelectList()
            {
                var entity = await _dbContext.Queryable<ProcessEntity>().Select(x => new
                {
                    key = x.Id,
                    value = x.Name
                }).ToListAsync();
                return entity;
            }
        }
    }