namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：桶服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：基础数据管理 
    /// </summary>
    [ApiDescriptionSettings("基础数据管理", Order = 3, Tag = "桶服务", Name = "BucketInfo", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class BucketInfoService : IBucketInfoService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public BucketInfoService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(BucketInfoAddInput input)
        {
            var r_entity = await _dbContext.Queryable<Pztj_BucketInfoEntity>().FirstAsync(x => x.Name == input.Name || x.Code == input.Code);
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            Pztj_BucketInfoEntity entity = input.Adapt<Pztj_BucketInfoEntity>();
            return await _dbContext.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandIdentityIntoEntityAsync();
        }
        /// <summary>
        /// 新增并返回
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Pztj_BucketInfoEntity> AddRetrun(BucketInfoAddInput input)
        {
            var r_entity = await _dbContext.Queryable<Pztj_BucketInfoEntity>().FirstAsync(x => x.Name == input.Name || x.Code == input.Code);
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            Pztj_BucketInfoEntity entity = input.Adapt<Pztj_BucketInfoEntity>();
            var res = await _dbContext.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandIdentityIntoEntityAsync();
            if (!res)
            {
                throw Oops.Bah("新增桶失败!");
            }
            return entity;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="input"></exception>
        [HttpPost]
        public async Task<bool> Del(BucketInfoDelInput input)
        {
            var res = await _dbContext.Deleteable<Pztj_BucketInfoEntity>().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<BucketInfoOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<Pztj_BucketInfoEntity>().FirstAsync(x => x.Id == Id);
            BucketInfoOutput output = entity.Adapt<BucketInfoOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<BucketInfoOutput>> List()
        {
            var entity = await _dbContext.Queryable<Pztj_BucketInfoEntity>().ToListAsync();
            List<BucketInfoOutput> output = entity.Adapt<List<BucketInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<BucketInfoOutput>> PagedList(BucketInfoQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<Pztj_BucketInfoEntity>()
                .LeftJoin<Pztj_BucketMaterialsRecordEntity>((a,b)=>a.Id==b.BucketId && b.Id == SqlFunc.Subqueryable<Pztj_BucketMaterialsRecordEntity>().Where(s => s.BucketId == a.Id).OrderByDesc(s => s.CreateTime).Select(s => s.Id))
                .LeftJoin<Pztj_MaterialsInfoEntity>((a,b,c)=>b.MaterialId==c.Id)
                .Select((a,b,c)=>new BucketInfoOutput
                {
                    Id = a.Id.SelectAll(),
                    MaterialName = c.Name
                })
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<BucketInfoOutput> output = entity.Adapt<SqlSugarPagedList<BucketInfoOutput>>();
            return output;
        }




        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<Pztj_BucketInfoEntity>().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(BucketInfoUpdateInput input)
        {
            var isExit =await _dbContext.Queryable<Pztj_BucketInfoEntity>().Where(x => x.Id != input.Id && (x.Name == input.Name || x.Code == input.Code)).FirstAsync();
            if (isExit != null)
            {
                throw Oops.Bah("名称或编码已存在!");
            }
            Pztj_BucketInfoEntity entity = input.Adapt<Pztj_BucketInfoEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
        ///// <summary>
        ///// 根据桶Id查询记录
        ///// </summary>
        ///// <param name="bucketId"></param>
        ///// <returns></returns>
        //public async Task<BucketMaterialsRecordOutput> RecordList(string bucketId)
        //{
        //    var entity = await _dbContext.Queryable<Pztj_BucketMaterialsRecordEntity>()
        //       .LeftJoin<Pztj_MaterialsInfoEntity>((a, b) => a.MaterialId == b.Id)
        //       .Select((a, b) => new BucketMaterialsRecordOutput
        //       {
        //           Id = a.Id.SelectAll(),
        //           MaterialName = b.Name
        //       })
        //       .ToListAsync();
        //    SqlSugarPagedList<BucketMaterialsRecordOutput> output = entity.Adapt<SqlSugarPagedList<BucketMaterialsRecordOutput>>();
        //    return output;
        //}
    }
}

