namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：料仓  
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/8/21 11:02:36 
    /// 描    述 ： 
    [ApiDescriptionSettings("料仓管理", Name = "Godown", Tag = "料仓管理", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class GodownService : IGodownService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public GodownService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(GodownAddInput input)
        {
            var r_entity = await _dbContext.Queryable<GodownEntity>().FirstAsync(x => x.Code == input.Code);
            if (r_entity != null)
            {
                throw Oops.Bah("编码已存在!");
            }
            GodownEntity entity = input.Adapt<GodownEntity>();
            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(GodownDelInput input)
        {
            var res = await _dbContext.Deleteable<GodownEntity>().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<GodownOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<GodownEntity>().FirstAsync(x => x.Id == Id);
            GodownOutput output = entity.Adapt<GodownOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<GodownOutput>> List(GodownQueryInput input)
        {
            var entity = await _dbContext.Queryable<GodownEntity>().ToListAsync();
            List<GodownOutput> output = entity.Adapt<List<GodownOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<GodownOutput>> PagedList(GodownQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<GodownEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name))
                 .WhereIF(!string.IsNullOrEmpty(input.Code), x => x.Code.Contains(input.Code))
                .OrderByDescending(x => x.CreateTime)
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<GodownOutput> output = entity.Adapt<SqlSugarPagedList<GodownOutput>>();
            return output;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(GodownUpdateInput input)
        {
            var entity = await _dbContext.Queryable<GodownEntity>().FirstAsync(x => x.Id != input.Id && x.Code == input.Code);
            if (entity != null)
            {
                throw Oops.Bah("编码已存在!");
            }
            var r_entity = await _dbContext.Queryable<GodownEntity>().Where(x => x.Id == input.Id).FirstAsync();
            r_entity = input.Adapt<GodownEntity>();
            var res = await _dbContext.Updateable(r_entity).IgnoreColumns(true).IgnoreColumns(x => x.CreateTime).ExecuteCommandHasChangeAsync();
            return res;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<GodownEntity>().Select(x => new
            {
                key = x.Id,
                value = x.Name
            }).ToListAsync();
            return entity;
        }
    }
}