namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：配方物料服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：配方物料数据维护 
    /// </summary>
    [ApiDescriptionSettings("配方工艺管理", Name = "FinalMaterial", Tag = "配方物料服务", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class FinalMaterialService : IFinalMaterialService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public FinalMaterialService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(FinalMaterialAddInput input)
        {
            FinalMaterialEntity entity = input.Adapt<FinalMaterialEntity>();
            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<FinalMaterialAddInput> inputs)
        {
            bool result = false;
            List<FinalMaterialEntity> entitys = inputs.Adapt<List<FinalMaterialEntity>>();
            try
            {
                _dbContext.Ado.BeginTran();
                await _dbContext.Deleteable<FinalMaterialEntity>().Where(x => x.FinalId == inputs[0].FinalId).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(FinalMaterialDelInput input)
        {
            var res = await _dbContext.Deleteable<FinalMaterialEntity>().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<FinalMaterialOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<FinalMaterialEntity>().FirstAsync(x => x.Id == Id);
            FinalMaterialOutput output = entity.Adapt<FinalMaterialOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost,AllowAnonymous]
        public async Task<List<FinalMaterialOutput>> List(FinalMaterialQueryInput input)
        {
            var output = await _dbContext.Queryable<FinalMaterialEntity>()
                .LeftJoin<Pztj_MaterialsInfoEntity>((a,b)=>a.MaterialId==b.Id)
                .WhereIF(!string.IsNullOrEmpty(input.FinalId),(a,b)=>a.FinalId==input.FinalId)
                .Select((a,b)=>new FinalMaterialOutput
                {
                    Id = a.Id,
                    MaterialName = b.Name,
                    MaterialCode = b.Code,
                    MaterialId = b.Id,
                    FinalId = a.FinalId,
                    RecipesName = b.Name,
                    Mode = a.Mode.ToString(),
                    ErrorWeight = a.ErrorWeight,
                    Weight = a.Weight
                })
                .ToListAsync();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<FinalMaterialOutput>> PagedList(FinalMaterialQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<FinalMaterialEntity>()
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<FinalMaterialOutput> output = entity.Adapt<SqlSugarPagedList<FinalMaterialOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(FinalMaterialUpdateInput input)
        {
            var res = await _dbContext.Updateable<FinalMaterialEntity>().ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
