FolderUserRepository.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Dapper;
  6. using Datory;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity.Middle;
  10. using GxPress.Repository.Interface;
  11. using Microsoft.Extensions.Options;
  12. using SqlKata;
  13. namespace GxPress.Repository.Implement
  14. {
  15. public class FolderUserRepository : IFolderUserRepository
  16. {
  17. private readonly Repository<FolderUser> _repository;
  18. private readonly IMapper _mapper;
  19. private readonly string _connectionString;
  20. private readonly string _databaseTypeStr;
  21. public FolderUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  22. {
  23. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  24. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  25. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  26. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  27. _repository = new Repository<FolderUser>(database);
  28. _mapper = mapper;
  29. }
  30. public IDatabase Database => _repository.Database;
  31. public string TableName => _repository.TableName;
  32. public List<TableColumn> TableColumns => _repository.TableColumns;
  33. public async Task<int> InsertAsync(FolderUser folderUser)
  34. {
  35. return await _repository.InsertAsync(folderUser);
  36. }
  37. /// <summary>
  38. /// 插入文件夹共享用户
  39. /// </summary>
  40. /// <param name="folderUsers"></param>
  41. /// <returns></returns>
  42. public async Task<bool> InsertsAsync(List<FolderUser> folderUsers)
  43. {
  44. if (folderUsers.Count == 0)
  45. return false;
  46. string sql = $@"INSERT INTO `ccpph`.`tede_folder_user`(`Guid`,`CreatedDate`,`LastModifiedDate`,`MiddleId`,`UserId`,`UserName`) VALUES";
  47. foreach (var item in folderUsers)
  48. {
  49. var guid = Guid.NewGuid().ToString();
  50. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  51. sql += $"('{guid}','{createdDate}','{createdDate}',{item.MiddleId},{item.UserId},'{item.UserName}'),";
  52. }
  53. sql = sql.Remove(sql.Length - 1, 1);
  54. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  55. var database = new Database(databaseType, _connectionString);
  56. var connection = database.GetConnection();
  57. var result = await connection.ExecuteAsync(sql);
  58. return result > 0;
  59. }
  60. public async Task<bool> DeleteAsync(Query query)
  61. {
  62. return await _repository.DeleteAsync(query) > 0;
  63. }
  64. public async Task<IEnumerable<FolderUser>> GetAllAsync(int middleId)
  65. {
  66. return await _repository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), middleId));
  67. }
  68. public async Task<IEnumerable<FolderUser>> GetAllAsync(Query query)
  69. {
  70. return await _repository.GetAllAsync(query);
  71. }
  72. }
  73. }