namespace BPA.MES.Base.Application.Entitys
{
    /// <summary>
    /// 名    称 ：设备服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：店铺数据维护 
    /// </summary>
    [ApiDescriptionSettings(Name = "DeviceRecipe", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class DeviceRecipeService : IDeviceRecipeService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public DeviceRecipeService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(DeviceRecipeAddInput input)
        {
            var r_entity = await _dbContext.Queryable<DeviceRecipeEntity>().FirstAsync(x => x.DeviceId == input.DeviceId && x.RecipesId==input.RecipesId);
            if (r_entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            DeviceRecipeEntity entity = input.Adapt<DeviceRecipeEntity>();
            return await _dbContext.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandIdentityIntoEntityAsync();
        }
        /// <summary>
        /// 批量下发配方
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public async Task<bool> BatchUpdate(DeviceRecipeBatchAddInput input)
        {
            bool result = false;
            List<DeviceRecipeEntity> dr_entitys = new();
            foreach (var item in input.RecipesIds)
            {
                DeviceRecipeEntity entity = new();
                entity.RecipesId = item;
                entity.DeviceId = input.DeviceId;
                dr_entitys.Add(entity);
            }
            try
            {
                _dbContext.Ado.BeginTran();
                await _dbContext.Deleteable<DeviceRecipeEntity>().Where(x => x.DeviceId == input.DeviceId).ExecuteCommandAsync();
                if (input.RecipesIds.Any())
                {
                  await  _dbContext.Insertable(dr_entitys).ExecuteCommandAsync();
                }
                _dbContext.Ado.CommitTran();
                result = true;
            }
            catch (Exception ex)
            {
                throw Oops.Bah("更新失败!");
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Del(DeviceRecipeDelInput input)
        {
            var res = await _dbContext.Deleteable<DeviceRecipeEntity>().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<DeviceRecipeOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<DeviceRecipeEntity>().FirstAsync(x => x.Id == Id);
            DeviceRecipeOutput output = entity.Adapt<DeviceRecipeOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost,AllowAnonymous]
        public async Task<List<DeviceRecipeOutput>> List(DeviceRecipeQueryInput input)
        {
            var output = await _dbContext.Queryable<DeviceRecipeEntity>()
                .LeftJoin<RecipesInfoEntity>((a,b)=>a.RecipesId==b.Id)
                .LeftJoin<RecipesGroupEntity>((a,b,c)=>a.RecipesId==c.RecipesId)
                .LeftJoin<GroupInfoEntity>((a, b, c,d)=>c.GroupId==d.Id)
                .WhereIF(!string.IsNullOrEmpty(input.DeviceId),a=>a.DeviceId==input.DeviceId)
                .Select((a, b, c,d) => new DeviceRecipeOutput
                {
                     DeviceId = a.DeviceId,
                     RecipeName = b.Name,
                     RecipeCode = b.Code,
                     RecipesId = a.RecipesId,
                     RecipesPic =b.PicUrl,
                     GroupId = c.GroupId,
                     GroupName = d.Name
                })
                .ToListAsync();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<DeviceRecipeOutput>> PagedList(DeviceRecipeQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<DeviceRecipeEntity>()
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<DeviceRecipeOutput> output = entity.Adapt<SqlSugarPagedList<DeviceRecipeOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(DeviceRecipeUpdateInput input)
        {
            var res = await _dbContext.Updateable<DeviceRecipeEntity>().ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
