SitemapService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IdentityModel.Tokens.Jwt;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Text;
  7. using FMSAdmin.Data;
  8. using FMSAdmin.Helpers;
  9. using FMSAdmin.Entities;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. using Microsoft.IdentityModel.Tokens;
  14. using FMSAdmin.Models;
  15. namespace FMSAdmin.Services {
  16. public class SitemapService {
  17. private readonly ILogger<SitemapService> _logger;
  18. private readonly FMSContext _context;
  19. private readonly StorageHelper _storage;
  20. public SitemapService(
  21. ILogger<SitemapService> logger,
  22. FMSContext context,
  23. StorageHelper storage) {
  24. _logger = logger;
  25. _context = context;
  26. _storage = storage;
  27. }
  28. public void Save(Sitemap data) {
  29. var persist = _context.Sitemap.Find(data.SitemapId);
  30. if (persist == null) {
  31. var siblings = _context.Sitemap.AsNoTracking().Where(x => x.ParentId == data.ParentId);
  32. if (siblings.Any()) {
  33. data.Position = siblings.DefaultIfEmpty().Max(x => x.Position) + 1;
  34. }
  35. if (data.ParentId != null) {
  36. var parent = _context.Sitemap.AsNoTracking()
  37. .Where(x => x.SitemapId == data.ParentId).FirstOrDefault();
  38. if (parent != null) {
  39. data.Depth = parent.Depth + 1;
  40. }
  41. if (data.Depth > 4) {
  42. throw new ServiceException("최대 4뎁스까지 지원합니다.");
  43. }
  44. }
  45. data.Created = DateTime.Now;
  46. _context.Sitemap.Add(data);
  47. _context.SaveChanges();
  48. } else {
  49. persist.Name = data.Name;
  50. persist.Path = data.Path;
  51. persist.Icon = data.Icon;
  52. persist.Action1 = data.Action1;
  53. persist.Action2 = data.Action2;
  54. persist.Action3 = data.Action3;
  55. persist.Action4 = data.Action4;
  56. persist.Action5 = data.Action5;
  57. persist.Memo = data.Memo;
  58. persist.Updated = DateTime.Now;
  59. _context.Sitemap.Update(persist);
  60. _context.SaveChanges();
  61. }
  62. }
  63. public Sitemap Get(int id) {
  64. var query = _context.Sitemap.Single(x => x.SitemapId == id);
  65. return query;
  66. }
  67. public IQueryable<Sitemap> Gets(int id) {
  68. var query = _context.Sitemap.Where(x => x.SitemapId == id);
  69. return query;
  70. }
  71. public IQueryable<Sitemap> GetAll() {
  72. var query = _context.Sitemap/*.AsNoTracking()*/;
  73. return query;
  74. }
  75. public void Delete(int id) {
  76. var data = _context.Sitemap.Single(x => x.SitemapId == id);
  77. var siblings = GetAll().Where(x => x.Type == data.Type && x.Parent == data.Parent);
  78. if (siblings.Count() > 0) {
  79. var pos = 0;
  80. foreach (var sib in siblings.Where(x => x != data).OrderBy(x => x.Position)) {
  81. sib.Position = pos++;
  82. }
  83. }
  84. // 하위메뉴 탐색
  85. Func<Sitemap, bool> loopDelete = null;
  86. loopDelete = (item) => {
  87. foreach (var child in item.Childs) {
  88. loopDelete(child);
  89. }
  90. _context.Sitemap.Remove(item);
  91. return true;
  92. };
  93. loopDelete(data);
  94. _context.SaveChanges();
  95. }
  96. public void Up(Sitemap data) {
  97. var siblings = GetAll().Where(x => x.Type == data.Type && x.Parent == data.Parent);
  98. var upnode = siblings.Where(x => x.Position < data.Position).OrderByDescending(x => x.Position).FirstOrDefault();
  99. if (upnode != null) {
  100. var uppos = upnode.Position;
  101. upnode.Position = data.Position;
  102. data.Position = uppos;
  103. } else {
  104. data.Position = 0;
  105. }
  106. _context.SaveChanges();
  107. var pos = 0;
  108. foreach (var node in siblings.OrderBy(x => x.Position)) {
  109. node.Position = pos++;
  110. }
  111. _context.SaveChanges();
  112. }
  113. public void Down(Sitemap data) {
  114. var siblings = GetAll().Where(x => x.Type == data.Type && x.Parent == data.Parent);
  115. var downnode = siblings.Where(x => x.Position > data.Position).OrderBy(x => x.Position).FirstOrDefault();
  116. if (downnode != null) {
  117. var uppos = downnode.Position;
  118. downnode.Position = data.Position;
  119. data.Position = uppos;
  120. } else {
  121. data.Position = siblings.Count();
  122. }
  123. _context.SaveChanges();
  124. var pos = 0;
  125. foreach (var node in siblings.OrderBy(x => x.Position)) {
  126. node.Position = pos++;
  127. }
  128. _context.SaveChanges();
  129. }
  130. public void Upload(SitemapType type, Sitemap[] list) {
  131. var siblings = GetAll().Where(x => x.Type == type && x.ParentId == null);
  132. var posNullable = siblings.DefaultIfEmpty().Max(x => (int?)x.Position);
  133. var pos = (posNullable ?? 0) + 1;
  134. _logger.LogInformation("pos : " + pos);
  135. _logger.LogInformation(list.Dump());
  136. // 하위메뉴 탐색
  137. Func<Sitemap, int, bool> loop = null;
  138. loop = (item, depth) => {
  139. item.Type = type;
  140. item.Depth = depth;
  141. item.Created = DateTime.Now;
  142. if (depth >= 4) {
  143. item.Childs = null;
  144. return true;
  145. }
  146. var pos = 0;
  147. foreach (var child in item.Childs) {
  148. child.Position = pos++;
  149. loop(child, depth + 1);
  150. }
  151. return true;
  152. };
  153. foreach (var data in list) {
  154. data.Position = pos++;
  155. loop(data, 0);
  156. }
  157. _context.Sitemap.AddRange(list);
  158. _context.SaveChanges();
  159. }
  160. }
  161. }