namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：设备产品功能服务  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：  2023/7/31 18:03:25 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("设备产品模型", Name = "DeviceProductFunction", Tag = "设备产品功能", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class DeviceProductFunctionService : IDeviceProductFunctionService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public DeviceProductFunctionService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(DeviceProductFunctionAddInput input)
        {
            var r_entity = await _dbContext.Queryable<DeviceProductFunctionEntity>().Where(x=>x.DeviceProductId==input.DeviceProductId && x.Name == input.Name).FirstAsync();
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            DeviceProductFunctionEntity entity = input.Adapt<DeviceProductFunctionEntity>();
            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(DeviceProductFunctionDelInput input)
        {
            var res = await _dbContext.Deleteable<DeviceProductFunctionEntity>().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<DeviceProductFunctionOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<DeviceProductFunctionEntity>().FirstAsync(x => x.Id == Id);
            DeviceProductFunctionOutput output = entity.Adapt<DeviceProductFunctionOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<DeviceProductFunctionOutput>> List(DeviceProductFunctionQueryInput input)
        {
            var entity = await _dbContext.Queryable<DeviceProductFunctionEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.DeviceProductId), x => x.DeviceProductId==input.DeviceProductId)
                .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name))
                .WhereIF(!string.IsNullOrEmpty(input.Code), x => x.Code.Contains(input.Code))
                .ToListAsync();
            List<DeviceProductFunctionOutput> output = entity.Adapt<List<DeviceProductFunctionOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<DeviceProductFunctionOutput>> PagedList(DeviceProductFunctionQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<DeviceProductFunctionEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name))
                .WhereIF(!string.IsNullOrEmpty(input.Code), x => x.Code.Contains(input.Code))
                .WhereIF(!string.IsNullOrEmpty(input.DeviceProductId),x=>x.DeviceProductId==input.DeviceProductId)
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<DeviceProductFunctionOutput> output = entity.Adapt<SqlSugarPagedList<DeviceProductFunctionOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<DeviceProductFunctionEntity>().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(DeviceProductFunctionUpdateInput input)
        {

            var isEntity = await _dbContext.Queryable<DeviceProductFunctionEntity>()
               .Where(x =>x.Id != input.Id && x.DeviceProductId == input.DeviceProductId && x.Name == input.Name).FirstAsync();
            if (isEntity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            var entity = await _dbContext.Queryable<DeviceProductFunctionEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<DeviceProductFunctionEntity>();
           
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
