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.Middle; using GxPress.Repository.Interface; using Microsoft.Extensions.Options; using SqlKata; namespace GxPress.Repository.Implement { public class FolderUserRepository : IFolderUserRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; public FolderUserRepository(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); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task InsertAsync(FolderUser folderUser) { return await _repository.InsertAsync(folderUser); } /// /// 插入文件夹共享用户 /// /// /// public async Task InsertsAsync(List folderUsers) { if (folderUsers.Count == 0) return false; string sql = $@"INSERT INTO `tede_folder_user`(`Guid`,`CreatedDate`,`LastModifiedDate`,`MiddleId`,`UserId`,`UserName`) VALUES"; foreach (var item in folderUsers) { var guid = Guid.NewGuid().ToString(); var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); sql += $"('{guid}','{createdDate}','{createdDate}',{item.MiddleId},{item.UserId},'{item.UserName}'),"; } sql = sql.Remove(sql.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 DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 0; } public async Task> GetAllAsync(int middleId) { return await _repository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), middleId)); } public async Task> GetAllAsync(Query query) { return await _repository.GetAllAsync(query); } } }