MiddleLableService.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Threading.Tasks;
  2. using GxPress.Repository.Interface.Navigation;
  3. using GxPress.Service.Interface.Navigation;
  4. using System.Transactions;
  5. using GxPress.Common.Tools;
  6. using System.Linq;
  7. using System.Collections.Generic;
  8. using GxPress.Request.Navigation;
  9. using AutoMapper;
  10. namespace GxPress.Service.Implement.Navigation
  11. {
  12. public class MiddleLableService : IMiddleLableService
  13. {
  14. private readonly IMiddleLableRepository middleLableRepository;
  15. private readonly IMiddleLableNexusRepository middleLableNexusRepository;
  16. private readonly IMapper _mapper;
  17. public MiddleLableService(IMiddleLableRepository middleLableRepository, IMiddleLableNexusRepository middleLableNexusRepository, IMapper mapper)
  18. {
  19. this.middleLableRepository = middleLableRepository;
  20. this.middleLableNexusRepository = middleLableNexusRepository;
  21. _mapper = mapper;
  22. }
  23. /// <summary>
  24. /// 添加中间件页面
  25. /// </summary>
  26. /// <param name="model"></param>
  27. /// <returns></returns>
  28. public async Task<bool> InsertAsync(MiddleLableInRequest request)
  29. {
  30. try
  31. {
  32. using (var transactionScope = new System.Transactions.TransactionScope())
  33. {
  34. var model = _mapper.Map<Entity.Navigations.MiddleLable>(request);
  35. var middleLableId = await middleLableRepository.InsertAsync(model);
  36. var ids = StringUtils.StringCollectionToStringList(request.LabelId);
  37. var idInts = new List<int>();
  38. foreach (var item in ids)
  39. idInts.Add(int.Parse(item));
  40. await middleLableNexusRepository.InsertAsync(middleLableId, idInts);
  41. transactionScope.Complete();
  42. }
  43. }
  44. catch
  45. {
  46. return false;
  47. }
  48. return true;
  49. }
  50. /// <summary>
  51. /// 删除
  52. /// </summary>
  53. /// <param name="id"></param>
  54. /// <returns></returns>
  55. public async Task<bool> DeleteAsync(int id)
  56. {
  57. try
  58. {
  59. using (var transactionScope = new System.Transactions.TransactionScope())
  60. {
  61. await middleLableRepository.DeleteAsync(id);
  62. await middleLableNexusRepository.DeleteByMiddleLableIdAsync(id);
  63. transactionScope.Complete();
  64. }
  65. }
  66. catch
  67. {
  68. return false;
  69. }
  70. return true;
  71. }
  72. public async Task<bool> UpdateAsync(MiddleLableUpRequest request)
  73. {
  74. try
  75. {
  76. using (var transactionScope = new System.Transactions.TransactionScope())
  77. {
  78. await middleLableRepository.UpdateAsync(request);
  79. if (!string.IsNullOrEmpty(request.LabelId))
  80. await middleLableNexusRepository.UpdateAsync(request.Id, request.LabelId);
  81. transactionScope.Complete();
  82. }
  83. }
  84. catch
  85. {
  86. return false;
  87. }
  88. return true;
  89. }
  90. }
  91. }