namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：原料服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023年7月17日15:41:09
    /// 描    述 ：基础数据管理 
    /// </summary>
    [ApiDescriptionSettings("基础数据管理", Order = 2, Name = "MaterialsInfo", Tag = "原料服务", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class MaterialsInfoService : IMaterialsInfoService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public MaterialsInfoService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(MaterialsInfoAddInput input)
        {
            var r_entity = await _dbContext.Queryable<Pztj_MaterialsInfoEntity>().FirstAsync(x => x.Name == input.Name);
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            Pztj_MaterialsInfoEntity entity = input.Adapt<Pztj_MaterialsInfoEntity>();
            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(MaterialsInfoDelInput input)
        {
            var res = await _dbContext.Deleteable<Pztj_MaterialsInfoEntity>().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<MaterialsInfoOutput> Detail(string Id)
        {
            var output = await _dbContext.Queryable<Pztj_MaterialsInfoEntity>()
                .LeftJoin<DictDataEntity>((a,b)=>a.Type==b.Id)
                .Where((a,b)=> a.Id == Id)
                .Select((a,b)=>new MaterialsInfoOutput
                {
                    Id=a.Id.SelectAll(),
                    TypeName = b.Value
                })
                .FirstAsync();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<MaterialsInfoOutput>> List()
        {
            var output = await _dbContext.Queryable<Pztj_MaterialsInfoEntity>()
                .LeftJoin<DictDataEntity>((a, b) => a.Type == b.Id)
                .Select((a, b) => new MaterialsInfoOutput
                {
                    Id = a.Id.SelectAll(),
                    TypeName = b.Value
                })
                .ToListAsync();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<MaterialsInfoOutput>> PagedList(MaterialsInfoQueryPageInput input)
        {
            var output = await _dbContext.Queryable<Pztj_MaterialsInfoEntity>()
                .LeftJoin<DictDataEntity>((a, b) => a.Type == b.Id)
                .WhereIF(!string.IsNullOrEmpty(input.Name), (a, b) => a.Name.Contains(input.Name))
                .WhereIF(!string.IsNullOrEmpty(input.Code), (a, b) => a.Code.Contains(input.Code))
                .Select((a, b) => new MaterialsInfoOutput
                {
                    Id = a.Id.SelectAll(),
                    TypeName = b.Value
                })
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<Pztj_MaterialsInfoEntity>().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(MaterialsInfoUpdateInput input)
        {
            var currentity = await _dbContext.Queryable<Pztj_MaterialsInfoEntity>().FirstAsync(x => x.Id != input.Id && (x.Name == input.Name || x.Code==input.Code));
            if (currentity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            var entity = await _dbContext.Queryable<Pztj_MaterialsInfoEntity>().Where(x => x.Id == input.Id).FirstAsync();
            entity = input.Adapt<Pztj_MaterialsInfoEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
