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>();
            var work_entity =await _dbContext.Queryable<WorkInfoMaterialsRecordEntity>().FirstAsync(x => x.Id == input.MaterialId);
            if (work_entity==null)
            {
                throw Oops.Bah("没有找到此物料信息!");
            }
            entity.WorkId = work_entity.WorkId;
            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>
        [HttpPost]
        public async Task<List<BucketMaterialsRecordOutput>> List(BucketMaterialsRecordQueryInput input)
        {
            var output = await _dbContext.Queryable<Pztj_BucketMaterialsRecordEntity>()
                .LeftJoin<WorkInfoMaterialsRecordEntity>((a, b) => a.MaterialId == b.Id)
                .LeftJoin<Pztj_BucketInfoEntity>((a, b, c) => a.BucketId == c.Id)
                .LeftJoin<Pztj_WorkInfoEntity>((a,b,c,d)=>b.WorkId==d.Id)
                .WhereIF(!string.IsNullOrEmpty(input.BucketId), (a, b, c) => a.BucketId == input.BucketId)
                .WhereIF(!string.IsNullOrEmpty(input.WorkId), (a, b, c) => b.WorkId == b.WorkId)
                .WhereIF(!string.IsNullOrEmpty(input.BucketCode),(a,b,c)=>c.Code==input.BucketCode)
                .Select((a, b, c, d) => new BucketMaterialsRecordOutput
                {
                    Id = a.Id,
                    MaterialId = a.MaterialId,
                    BucketId = a.BucketId,
                    Weight = a.Weight,
                    CreateTime = a.CreateTime,
                    SchemeWeight = b.Weight,
                    WorkId = d.Id,
                    MaterialName = b.MaterialName,
                    BucketName = c.Name,
                    WorkName = b.RecipeName,
                    PotNum = b.PotNum,
                    BucketCode = c.Code
                })
                .ToListAsync();
            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;
        }
        /// <summary>
        /// 清空桶
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Clear(BucketClearInput input)
        {
            var res = await _dbContext.Deleteable<Pztj_BucketMaterialsRecordEntity>().Where(x => x.BucketId == input.BucketId).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
