namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：配方物料服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：配方物料数据维护 
    /// </summary>
    [ApiDescriptionSettings("配方工艺管理", Name = "RecipeMaterial", Tag = "配方物料服务", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class RecipeMaterialService : IRecipeMaterialService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public RecipeMaterialService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(RecipeMaterialAddInput input)
        {
            RecipeMaterialEntity entity = input.Adapt<RecipeMaterialEntity>();
            return await _dbContext.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandIdentityIntoEntityAsync();
        }
        /// <summary>
        /// 批量增加
        /// </summary>
        /// <param name="inputs"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> BatchAdd(List<RecipeMaterialAddInput> inputs)
        {
            bool result = false;
            List<RecipeMaterialEntity> entitys = inputs.Adapt<List<RecipeMaterialEntity>>();
            try
            {
                _dbContext.Ado.BeginTran();
                await _dbContext.Deleteable<RecipeMaterialEntity>().Where(x => x.RecipesId == inputs[0].RecipesId).ExecuteCommandAsync();
                await _dbContext.Insertable(entitys).ExecuteCommandAsync();
                _dbContext.Ado.CommitTran();
                result = true;
            }
            catch (Exception ex)
            {
                _dbContext.Ado.RollbackTran();
                throw Oops.Bah("保存失败!");
            }
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Del(RecipeMaterialDelInput input)
        {
            var res = await _dbContext.Deleteable<RecipeMaterialEntity>().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<RecipeMaterialOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<RecipeMaterialEntity>().FirstAsync(x => x.Id == Id);
            RecipeMaterialOutput output = entity.Adapt<RecipeMaterialOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost,AllowAnonymous]
        public async Task<List<RecipeMaterialOutput>> List(RecipeMaterialQueryInput input)
        {
            var output = await _dbContext.Queryable<RecipeMaterialEntity>()
                .LeftJoin<RecipesInfoEntity>((a,b)=>a.RecipesId==b.Id)
                .LeftJoin<MaterialInfoEntity>((a,b,c)=>a.MaterialId==c.Id)
                .WhereIF(!string.IsNullOrEmpty(input.RecipesId),(a,b,c)=>a.RecipesId==input.RecipesId)
                .Select((a,b,c)=>new RecipeMaterialOutput
                {
                    Id = a.Id,
                    MaterialName = c.Name,
                    MaterialCode = c.Code,
                    MaterialId = c.Id,
                    RecipesId = a.RecipesId,
                    RecipesName = b.Name,
                    Weight = a.Weight
                })
                .ToListAsync();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<RecipeMaterialOutput>> PagedList(RecipeMaterialQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<RecipeMaterialEntity>()
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<RecipeMaterialOutput> output = entity.Adapt<SqlSugarPagedList<RecipeMaterialOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(RecipeMaterialUpdateInput input)
        {
            var res = await _dbContext.Updateable<RecipeMaterialEntity>().ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
