namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：成品管理服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：基础数据管理 
    /// </summary>
    [ApiDescriptionSettings("基础数据管理", Order = 4, Name = "FinalsInfo", Tag = "成品管理服务", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class FinalsInfoService : IFinalsInfoService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public FinalsInfoService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(FinalsInfoAddInput input)
        {
            var r_entity = await _dbContext.Queryable<FinalInfoEntity>().FirstAsync(x => x.Name == input.Name);
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            FinalInfoEntity entity = input.Adapt<FinalInfoEntity>();
            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(FinalsInfoDelInput input)
        {
            var res = await _dbContext.Deleteable<FinalInfoEntity>().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<FinalsInfoOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<FinalInfoEntity>().FirstAsync(x => x.Id == Id);
            FinalsInfoOutput output = entity.Adapt<FinalsInfoOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<FinalsInfoOutput>> List()
        {
            var entity = await _dbContext.Queryable<FinalInfoEntity>().ToListAsync();
            List<FinalsInfoOutput> output = entity.Adapt<List<FinalsInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<FinalsInfoOutput>> PagedList(FinalsInfoQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<FinalInfoEntity>()
                .LeftJoin<RecipesInfoEntity>((a, b) => a.RecipeId == b.Id)
                .WhereIF(!string.IsNullOrEmpty(input.Name),a=>a.Name.Contains(input.Name))
                .WhereIF(!string.IsNullOrEmpty(input.Code), a => a.Name.Contains(input.Code))
                 .Select((a,b)=> new FinalsInfoOutput
                 {
                     Id = a.Id.SelectAll(),
                     RecipeName = b.Name,
                 })
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<FinalsInfoOutput> output = entity.Adapt<SqlSugarPagedList<FinalsInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<FinalInfoEntity>().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(FinalsInfoUpdateInput input)
        {
            FinalInfoEntity entity = input.Adapt<FinalInfoEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}

