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

namespace GxPress.Repository.Implement.AdminVerify
{
    public class AdminVerifyRepository : IAdminVerifyRepository
    {

        private readonly Repository<Entity.AdminVerify> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        private readonly Repository<GroupUser> _groupUserRepository;
        public AdminVerifyRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            _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<Entity.AdminVerify>(database);
            _groupUserRepository = new Repository<GroupUser>(database);
            _mapper = mapper;
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="adminVerify"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(Entity.AdminVerify adminVerify)
        {
            return await _repository.InsertAsync(adminVerify);
        }


        public async Task<bool> InsertAsync(List<Entity.AdminVerify> adminVerifys)
        {
            string sql = @"INSERT INTO `tede_admin_verify`(`Guid`,`CreatedDate`,`LastModifiedDate`,`VerifyType`,`DisposeType`,
`AdminId`,`UserId`,`SourceId`,`Name`,`AvatarUrl`,`Remark`,`SourceType`,`SourceName`,`IsDelete`)VALUES";
            if (adminVerifys.Count == 0)
                return false;
            var sqlValue = "";
            foreach (var item in adminVerifys)
            {
                var guId = Guid.NewGuid().ToString();
                var createdDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                sqlValue += $@"('{guId}','{createdDate}','{createdDate}',{item.VerifyType.GetHashCode()},{item.DisposeType},{item.AdminId},{item.UserId},{item.SourceId},'{item.Name}','{item.AvatarUrl}','{item.Remark}',{item.SourceType},'{item.SourceName}','{item.IsDelete}'),";
            }
            sql = sql + sqlValue.Remove(sqlValue.Length - 1, 1);
            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            var result = await connection.ExecuteAsync(sql);
            return result > 0;
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAllAsync(int userId)
        {
            return await _repository.DeleteAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), userId)) > 0;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="disposeTyeConst"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(int id, AdminVerifyTypeConst disposeTyeConst)
        {
            return await _repository.UpdateAsync(Q.Set(nameof(Entity.AdminVerify.DisposeType), disposeTyeConst).Where(nameof(Entity.AdminVerify.Id), id)) > 0;
        }
        /// <summary>
        /// 根据ID查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Entity.AdminVerify> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }
        /// <summary>
        /// 根据ID查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Entity.AdminVerify> GetAsync(SqlKata.Query query)
        {
            return await _repository.GetAsync(query);
        }
        /// <summary>
        /// 根据用户查询
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Entity.AdminVerify>> GetAllAsync(SqlKata.Query query)
        {
            return await _repository.GetAllAsync(query);
        }

        /// <summary>
        /// 查询未处理
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<int> GetUDisposeCount(int userId)
        {
            return await _repository.CountAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), userId).Where(nameof(Entity.AdminVerify.DisposeType), AdminVerifyTypeConst.UDispose));
        }

        public async Task<bool> UpdateAsync(Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }
    }
}
