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 FlowCarbonCopyRepository : IFlowCarbonCopyRepository
    {
        private readonly Repository<FlowCarbonCopy> _repository;

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

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

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

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

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