namespace BPA.MES.Base.Application.Entitys
{
    /// <summary>
    /// 名    称 ：物料服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：店铺数据维护 
    /// </summary>
    [ApiDescriptionSettings(Name = "MaterialsInfo", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class MaterialsInfoService : IMaterialsInfoService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public MaterialsInfoService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(MaterialsInfoAddInput input)
        {
            var r_entity = await _dbContext.Queryable<MaterialsInfoEntity>().FirstAsync(x => x.Name == input.Name);
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            MaterialsInfoEntity entity = input.Adapt<MaterialsInfoEntity>();
            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(MaterialsInfoDelInput input)
        {
            var res = await _dbContext.Deleteable<MaterialsInfoEntity>().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<MaterialsInfoOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<MaterialsInfoEntity>().FirstAsync(x => x.Id == Id);
            MaterialsInfoOutput output = entity.Adapt<MaterialsInfoOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<MaterialsInfoOutput>> List()
        {
            var entity = await _dbContext.Queryable<MaterialsInfoEntity>().ToListAsync();
            List<MaterialsInfoOutput> output = entity.Adapt<List<MaterialsInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<MaterialsInfoOutput>> PagedList(MaterialsInfoQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<MaterialsInfoEntity>()
                .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<MaterialsInfoOutput> output = entity.Adapt<SqlSugarPagedList<MaterialsInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<MaterialsInfoEntity>().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(MaterialsInfoUpdateInput input)
        {
            var entity = await _dbContext.Queryable<MaterialsInfoEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<MaterialsInfoEntity>();
            var res = await _dbContext.Updateable(entity).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
