namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：配方管理 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：店铺数据维护 
    /// </summary>
    [ApiDescriptionSettings("配方工艺管理", Name = "RecipesInfo", Tag = "配方服务", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class RecipesInfoService : IRecipesInfoService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public RecipesInfoService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(RecipesInfoAddInput input)
        {
            var r_entity = await _dbContext.Queryable<RecipesInfoEntity>().FirstAsync(x => x.Name == input.Name || x.Code==input.Code);
            if (r_entity != null)
            {
                throw Oops.Bah("名称或者编码已存在!");
            }
            RecipesInfoEntity entity = input.Adapt<RecipesInfoEntity>();
            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(RecipesInfoDelInput input)
        {
            var res = await _dbContext.Deleteable<RecipesInfoEntity>().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<RecipesInfoOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<RecipesInfoEntity>().FirstAsync(x => x.Id == Id);
            RecipesInfoOutput output = entity.Adapt<RecipesInfoOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<RecipesInfoOutput>> List()
        {
            var entity = await _dbContext.Queryable<RecipesInfoEntity>().ToListAsync();
            List<RecipesInfoOutput> output = entity.Adapt<List<RecipesInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<RecipesInfoOutput>> PagedList(RecipesInfoQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<RecipesInfoEntity>()
                .LeftJoin<RecipesGroupEntity>((a,b)=>a.Id==b.RecipesId)
                .WhereIF(!string.IsNullOrEmpty(input.Name), (a, b) => a.Name.Contains(input.Name))
                .WhereIF(!string.IsNullOrEmpty(input.GroupId), (a, b) => b.GroupId==input.GroupId)
                .Select<RecipesInfoOutput>()
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<RecipesInfoOutput> output = entity.Adapt<SqlSugarPagedList<RecipesInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<RecipesInfoEntity>().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(RecipesInfoUpdateInput input)
        {
            RecipesInfoEntity entity = await _dbContext.Queryable<RecipesInfoEntity>().FirstAsync(x => x.Id != input.Id && (x.Name == input.Name || x.Code == input.Code));
            if (entity != null)
            {
                throw Oops.Bah("名称或者编码已存在!");
            }
            entity = input.Adapt<RecipesInfoEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
        [HttpPost]
        public async Task<bool> ChangeGroup(RecipesGroupInput input)
        {
            var isExit =await  _dbContext.Queryable<RecipesGroupEntity>().FirstAsync(x=>x.RecipesId==input.RecipesId);
            var entity = input.Adapt<RecipesGroupEntity>();
            if (isExit==null)
            {
                return await _dbContext.Insertable(entity).ExecuteCommandIdentityIntoEntityAsync();
            }
            else
            {
                return await _dbContext.Updateable(entity).IgnoreColumns(true).Where(x => x.RecipesId == input.RecipesId).ExecuteCommandHasChangeAsync();
            }
            
        }
    }
}
