namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：设备服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：基础数据管理 
    /// </summary>
    [ApiDescriptionSettings("基础数据管理",Order = 0, Tag = "设备服务", Name = "DevicesInfo", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class DevicesInfoService : IDevicesInfoService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public DevicesInfoService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(DevicesInfoAddInput input)
        {
            var r_entity = await _dbContext.Queryable<Pztj_DevicesInfoEntity>().FirstAsync(x => x.Name == input.Name || x.Code == input.Code);
            if (r_entity != null)
            {
                throw Oops.Bah("名称或编码已存在!");
            }
            Pztj_DevicesInfoEntity entity = input.Adapt<Pztj_DevicesInfoEntity>();
            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(DevicesInfoDelInput input)
        {
            var res = await _dbContext.Deleteable<Pztj_DevicesInfoEntity>().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<DevicesInfoOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<Pztj_DevicesInfoEntity>().FirstAsync(x => x.Id == Id);
            DevicesInfoOutput output = entity.Adapt<DevicesInfoOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<DevicesInfoOutput>> List()
        {
            var entity = await _dbContext.Queryable<Pztj_DevicesInfoEntity>().ToListAsync();
            List<DevicesInfoOutput> output = entity.Adapt<List<DevicesInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<DevicesInfoOutput>> PagedList(DevicesInfoQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<Pztj_DevicesInfoEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.Name),x=>x.Name.Contains(input.Name))
                .WhereIF(!string.IsNullOrEmpty(input.Code), x => x.Name.Contains(input.Code))
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<DevicesInfoOutput> output = entity.Adapt<SqlSugarPagedList<DevicesInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<Pztj_DevicesInfoEntity>().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(DevicesInfoUpdateInput input)
        {
            var isExit =await _dbContext.Queryable<Pztj_DevicesInfoEntity>().Where(x => x.Id != input.Id && (x.Name == input.Name || x.Code == input.Code)).FirstAsync();
            if (isExit != null)
            {
                throw Oops.Bah("名称或编码已存在!");
            }
            Pztj_DevicesInfoEntity entity = input.Adapt<Pztj_DevicesInfoEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
