namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：桶记录 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：基础数据管理 
    /// </summary>
    [ApiDescriptionSettings("基础数据管理", Order = 3, Tag = "桶服务", Name = "BucketMaterialsRecord", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class BucketMaterialsRecordServcie : IBucketMaterialsRecordService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public BucketMaterialsRecordServcie(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(BucketMaterialsRecordAddInput input)
        {
            
            Pztj_BucketMaterialsRecordEntity entity = input.Adapt<Pztj_BucketMaterialsRecordEntity>();
            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(BucketMaterialsRecordDelInput input)
        {
            var res = await _dbContext.Deleteable<Pztj_BucketMaterialsRecordEntity>().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<BucketMaterialsRecordOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<Pztj_BucketMaterialsRecordEntity>().FirstAsync(x => x.Id == Id);
            BucketMaterialsRecordOutput output = entity.Adapt<BucketMaterialsRecordOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<BucketMaterialsRecordOutput>> List()
        {
            var entity = await _dbContext.Queryable<Pztj_BucketMaterialsRecordEntity>().ToListAsync();
            List<BucketMaterialsRecordOutput> output = entity.Adapt<List<BucketMaterialsRecordOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<BucketMaterialsRecordOutput>> PagedList(BucketMaterialsRecordQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<Pztj_BucketMaterialsRecordEntity>()
                .LeftJoin<Pztj_BucketInfoEntity>((a,b)=>a.BucketId==b.Id)
                .LeftJoin<Pztj_MaterialsInfoEntity>((a,b,c)=>a.MaterialId==c.Id)
                .WhereIF(!string.IsNullOrEmpty(input.BucketId),(a,b,c)=>a.BucketId==input.BucketId)
                .OrderByDescending((a,b,c)=>a.CreateTime)
                .Select((a,b,c)=>new BucketMaterialsRecordOutput
                {
                    Id = a.Id.SelectAll(),
                    MaterialName = c.Name,
                    BucketName = b.Name
                })
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<BucketMaterialsRecordOutput> output = entity.Adapt<SqlSugarPagedList<BucketMaterialsRecordOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(BucketMaterialsRecordUpdateInput input)
        {
            Pztj_BucketMaterialsRecordEntity entity = input.Adapt<Pztj_BucketMaterialsRecordEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
