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 _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; private readonly Repository _groupUserRepository; public AdminVerifyRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _groupUserRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 添加 /// /// /// public async Task InsertAsync(Entity.AdminVerify adminVerify) { return await _repository.InsertAsync(adminVerify); } public async Task InsertAsync(List 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(); var isDelete = item.IsDelete ? 1 : 0; 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}',{isDelete}),"; } sql = sql + sqlValue.Remove(sqlValue.Length - 1, 1); var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection.ExecuteAsync(sql); return result > 0; } /// /// 清空 /// /// /// /// public async Task DeleteAllAsync(int userId) { var query = Q.NewQuery(); query.Where(nameof(Entity.AdminVerify.AdminId), userId); query.Set(nameof(Entity.AdminVerify.IsDelete), true); query.Set(nameof(Entity.AdminVerify.DisposeType), 2); return await _repository.UpdateAsync(query) > 0; } /// /// 修改 /// /// /// /// public async Task UpdateAsync(int id, AdminVerifyTypeConst disposeTyeConst) { return await _repository.UpdateAsync(Q.Set(nameof(Entity.AdminVerify.DisposeType), disposeTyeConst).Where(nameof(Entity.AdminVerify.Id), id)) > 0; } /// /// 根据ID查询 /// /// /// public async Task GetAsync(int id) { return await _repository.GetAsync(id); } /// /// 根据ID查询 /// /// /// public async Task GetAsync(SqlKata.Query query) { return await _repository.GetAsync(query); } /// /// 根据用户查询 /// /// /// /// public async Task> GetAllAsync(SqlKata.Query query) { return await _repository.GetAllAsync(query.Where(nameof(Entity.AdminVerify.IsDelete),false)); } /// /// 查询未处理 /// /// /// public async Task GetUDisposeCount(int userId) { var query = Q.NewQuery(); query.Where(nameof(Entity.AdminVerify.AdminId), userId); query.Where(nameof(Entity.AdminVerify.DisposeType), AdminVerifyTypeConst.UDispose); query.Where(nameof(Entity.AdminVerify.IsDelete), false); return await _repository.CountAsync(); } public async Task UpdateAsync(Query query) { return await _repository.UpdateAsync(query) > 0; } /// /// 获取用户的验证时间信息 /// /// /// public async Task GetConfirmationDate(int userId) { var query = Q.NewQuery(); query.Select(nameof(Entity.AdminVerify.CreatedDate)); query.Where(nameof(Entity.AdminVerify.AdminId), userId); query.OrderByDesc(nameof(Entity.AdminVerify.CreatedDate)); return await _repository.GetAsync(query); } } }