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

namespace GxPress.Repository.Implement.WorkProcess
{
    public class RuleApproverCheckRepository : IRuleApproverCheckRepository
    {
        private readonly Repository<RuleApproverCheck> _repository;

        public RuleApproverCheckRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
        {
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<RuleApproverCheck>(database);
        }

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

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

        public async Task<IEnumerable<RuleApproverCheck>> GetListAsync(int nodeId)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(RuleApproverCheck.NodeId), nodeId)
                .OrderBy(nameof(RuleApproverCheck.Id))
            );
        }

        public async Task DeleteByNodeIdListAsync(IEnumerable<int> nodeIdList)
        {
            await _repository.DeleteAsync(Q
                .WhereIn(nameof(RuleApproverCheck.NodeId), nodeIdList)
            );
        }
    }
}