using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Common.AppOptions;
using GxPress.Entity.WorkFlow;
using GxPress.Repository.Interface.WorkFlow;
using Microsoft.Extensions.Options;
using Datory;

namespace GxPress.Repository.Implement.WorkFlow
{
    public class FlowApproverCheckRepository : IFlowApproverCheckRepository
    {
        private readonly Repository<FlowApproverCheck> _repository;

        public FlowApproverCheckRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
        {
            var database = new Database(DatabaseType.Parse(dbOptionsAccessor.CurrentValue.DatabaseType), dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<FlowApproverCheck>(database);
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;

        public async Task<int> InsertAsync(FlowApproverCheck item)
        {
            return await _repository.InsertAsync(item);
        }

        public async Task<IEnumerable<FlowApproverCheck>> GetListAsync(int flowId)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(FlowApproverCheck.FlowId), flowId)
                .OrderByDesc(nameof(FlowApproverCheck.Id))
            );
        }

        public async Task<bool> DeleteAllAsync(int flowId)
        {
            return await _repository.DeleteAsync(Q
                       .Where(nameof(FlowApproverCheck.FlowId), flowId)
                   ) > 0;
        }
    }
}