namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：工艺信息 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/10 14:03:06 
    /// 描    述 ：配方工艺管理  
    /// </summary>
    [ApiDescriptionSettings("配方工艺管理", Order = 0, Tag = "工艺服务", Name = "CraftsInfo", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class CraftsInfoService : ICraftsInfoService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public CraftsInfoService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(CraftsInfoAddInput input)
        {
            Pztj_CraftsInfoEntity entity = await _dbContext.Queryable<Pztj_CraftsInfoEntity>().FirstAsync(x => x.Name ==input.Name);
            if (entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            entity = input.Adapt<Pztj_CraftsInfoEntity>();
            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(CraftsInfoDelInput input)
        {
            //事务,删除所有子表
            try
            {
                _dbContext.Ado.BeginTran();
                await _dbContext.Deleteable<Pztj_CraftsInfoEntity>().Where(x => x.Id == input.Id).ExecuteCommandAsync();
                await _dbContext.Deleteable<Pztj_CraftStepsEntity>().Where(x => x.CraftId == input.Id).ExecuteCommandAsync();
                _dbContext.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                _dbContext.Ado.RollbackTran();
                throw Oops.Bah("删除失败!");
            }
            return true;
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<CraftsInfoOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<Pztj_CraftsInfoEntity>().FirstAsync();
            CraftsInfoOutput output = entity.Adapt<CraftsInfoOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost, AllowAnonymous]
        public async Task<List<CraftsInfoOutput>> List(CraftsInfoQueryInput input)
        {
            var entity = await _dbContext.Queryable<Pztj_CraftsInfoEntity>().ToListAsync();
            List<CraftsInfoOutput> output = entity.Adapt<List<CraftsInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<CraftsInfoOutput>> PagedList(CraftsInfoQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<Pztj_CraftsInfoEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.DeviceProductId), x => x.DeviceProductId == input.DeviceProductId)
                .WhereIF(!string.IsNullOrEmpty(input.Name),x=>x.Name.Contains(input.Name))
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<CraftsInfoOutput> output = entity.Adapt<SqlSugarPagedList<CraftsInfoOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<Pztj_CraftsInfoEntity>().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(CraftsInfoUpdateInput input)
        {
            var entity = await _dbContext.Queryable<Pztj_CraftsInfoEntity>().FirstAsync(x => x.Id != input.Id && x.Name == input.Name);
            if (entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            entity = input.Adapt<Pztj_CraftsInfoEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
