namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：设备产品  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：  2023/7/31 18:03:25 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("设备产品模型", Name = "DeviceProduct", Tag = "设备产品管理", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class DeviceProductService : IDeviceProductService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public DeviceProductService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(DeviceProductAddInput input)
        {
            var r_entity = await _dbContext.Queryable<DeviceProductEntity>().FirstAsync(x => x.Name == input.Name);
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            DeviceProductEntity entity = input.Adapt<DeviceProductEntity>();
            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(DeviceProductDelInput input)
        {
            //事务,删除所有子表
            try
            {
                _dbContext.Ado.BeginTran();
                await _dbContext.Deleteable<DeviceProductEntity>().Where(x => x.Id == input.Id).ExecuteCommandAsync();
                await _dbContext.Deleteable<DeviceProductFunctionEntity>().Where(x => x.DeviceProductId == input.Id).ExecuteCommandAsync();
                _dbContext.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                _dbContext.Ado.RollbackTran();
                throw Oops.Bah("删除失败!");
            }
            return true;
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<DeviceProductOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<DeviceProductEntity>().FirstAsync(x => x.Id == Id);
            DeviceProductOutput output = entity.Adapt<DeviceProductOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<DeviceProductOutput>> List(DeviceProductQueryInput input)
        {
            var entity = await _dbContext.Queryable<DeviceProductEntity>().ToListAsync();
            List<DeviceProductOutput> output = entity.Adapt<List<DeviceProductOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<DeviceProductOutput>> PagedList(DeviceProductQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<DeviceProductEntity>()
                .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<DeviceProductOutput> output = entity.Adapt<SqlSugarPagedList<DeviceProductOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<DeviceProductEntity>().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(DeviceProductUpdateInput input)
        {
            var entity = await _dbContext.Queryable<DeviceProductEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<DeviceProductEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
