namespace BPA.MES.Base.Application.Services.AGVService.Services
{
    /// <summary>
    /// 名    称 ：AGV线路管理  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：  2023/8/7 10:03:21 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("AGV管理", Name = "AgvLine", Tag = "AGV线路", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class AgvLineService : IAgvLineService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public AgvLineService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(AgvLineAddInput input)
        {
            var r_entity = await _dbContext.Queryable<AgvLineEntity>().FirstAsync(x => x.Name == input.Name);
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            AgvLineEntity entity = input.Adapt<AgvLineEntity>();
            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(AgvLineDelInput input)
        {
            var res = await _dbContext.Deleteable<AgvLineEntity>().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<AgvLineOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<AgvLineEntity>().FirstAsync(x => x.Id == Id);
            AgvLineOutput output = entity.Adapt<AgvLineOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<AgvLineOutput>> List(AgvLineQueryInput input)
        {
            var entity = await _dbContext.Queryable<AgvLineEntity>().ToListAsync();
            List<AgvLineOutput> output = entity.Adapt<List<AgvLineOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<AgvLineOutput>> PagedList(AgvLineQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<AgvLineEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name))
                .WhereIF(!string.IsNullOrEmpty(input.Code), x => x.Code.Contains(input.Code))
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<AgvLineOutput> output = entity.Adapt<SqlSugarPagedList<AgvLineOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<AgvLineEntity>().Select(x => new
            {
                key = x.Id,
                value = x.Name
            }).ToListAsync();
            return entity;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(AgvLineUpdateInput input)
        {
            var entity = await _dbContext.Queryable<AgvLineEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<AgvLineEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}