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

namespace GxPress.Repository.Implement.WorkFlow
{
    public class FlowFieldValueRepository : IFlowFieldValueRepository
    {
        private readonly Repository<FlowFieldValue> _repository;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public FlowFieldValueRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
        {
            _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
            _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<FlowFieldValue>(database);
        }

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

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

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

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

        public async Task<IEnumerable<int>> GetFlowIdListAsync(IEnumerable<int> flowIdList, string keyword)
        {
            if (string.IsNullOrEmpty(keyword)) return flowIdList;

            var query = Q
                .Select(nameof(FlowFieldValue.FlowId))
                .WhereLike(nameof(FlowFieldValue.StringValue), $"%{keyword}%")
                .WhereIn(nameof(FlowFieldValue.FlowId), flowIdList)
                .Distinct();

            return await _repository.GetAllAsync<int>(query);
        }

        public async Task<IEnumerable<FlowFieldValue>> GetAllAsync(SqlKata.Query query)
        {
            return await _repository.GetAllAsync(query);
        }
        /// <summary>
        /// 查询是否有记录
        /// </summary>
        /// <param name="processId"></param>
        /// <param name="flowId"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<bool> IsSearchKeywordFlowFieldValue(int processId, int flowId, string keyword)
        {
            string sql = $"SELECT count(1) FROM ccpph.tede_flow_field_value a inner join ccpph.tede_user b on a.UserId=b.Id  where a.ProcessId={processId} and a.FlowId={flowId} and  (a.StringValue like '%{keyword}%' or b.Name like '%{keyword}%')";
            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            var result =
             await connection.ExecuteScalarAsync<int>(sql);
            return result > 0;
        }
    }
}