MobileShortcutService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 FMSApp.Services {
  16. public class MobileShortcutService {
  17. private readonly ILogger<MobileShortcutService> _logger;
  18. private readonly FMSContext _context;
  19. private readonly AppSettings _appSettings;
  20. public MobileShortcutService(
  21. ILogger<MobileShortcutService> logger,
  22. FMSContext context,
  23. IOptions<AppSettings> appSettings) {
  24. _logger = logger;
  25. _context = context;
  26. _appSettings = appSettings.Value;
  27. }
  28. public void CopyFromDefault(string userId, ShortcutType type) {
  29. var userShortcutCount = _context.MobileShortcut.Where(
  30. x => x.UserId == userId
  31. && x.Type == type
  32. ).Count();
  33. if (userShortcutCount == 0) {
  34. var defaultShortcuts = _context.MobileShortcut.Where(
  35. x => (x.UserId == null || x.UserId == "")
  36. && x.Type == type
  37. ).OrderBy(x => x.Position).ToList();
  38. int position = 0;
  39. foreach (var def in defaultShortcuts) {
  40. position++;
  41. var shortcut = new MobileShortcut();
  42. shortcut.UserId = userId;
  43. shortcut.SitemapId = def.SitemapId;
  44. shortcut.Type = def.Type;
  45. shortcut.Icon = def.Icon;
  46. shortcut.MobileShortcutId = 0;
  47. shortcut.Position = position;
  48. shortcut.Created = DateTime.Now;
  49. shortcut.Updated = DateTime.Now;
  50. _context.MobileShortcut.Add(shortcut);
  51. }
  52. _context.SaveChanges();
  53. }
  54. }
  55. public void RePosition(string userId, ShortcutType type) {
  56. var list = _context.MobileShortcut.Where(
  57. x => x.UserId == userId
  58. && x.Type == type
  59. && x.IsDelete == false
  60. );
  61. int position = 0;
  62. foreach (var item in list) {
  63. position++;
  64. item.Position = position;
  65. _context.MobileShortcut.Update(item);
  66. }
  67. _context.SaveChanges();
  68. }
  69. public void PositionUp(string userId, int sitemapId, ShortcutType type) {
  70. CopyFromDefault(userId, type);
  71. var me = _context.MobileShortcut.FirstOrDefault(
  72. x => x.UserId == userId
  73. && x.SitemapId == sitemapId
  74. && x.Type == type
  75. && x.IsDelete == false
  76. );
  77. if (me == null) return;
  78. var prev = _context.MobileShortcut.Where(
  79. x => x.UserId == userId
  80. && x.Type == type
  81. && x.Position < me.Position
  82. && x.IsDelete == false
  83. ).OrderByDescending(x => x.Position).FirstOrDefault();
  84. if (prev == null) return;
  85. var myPosition = me.Position;
  86. me.Position = prev.Position;
  87. prev.Position = myPosition;
  88. _context.MobileShortcut.Update(me);
  89. _context.MobileShortcut.Update(prev);
  90. _context.SaveChanges();
  91. }
  92. public void PositionDown(string userId, int sitemapId, ShortcutType type) {
  93. CopyFromDefault(userId, type);
  94. var me = _context.MobileShortcut.FirstOrDefault(
  95. x => x.UserId == userId
  96. && x.SitemapId == sitemapId
  97. && x.Type == type
  98. && x.IsDelete == false
  99. );
  100. if (me == null) return;
  101. var next = _context.MobileShortcut.Where(
  102. x => x.UserId == userId
  103. && x.Type == type
  104. && x.Position > me.Position
  105. && x.IsDelete == false
  106. ).OrderBy(x => x.Position).FirstOrDefault();
  107. if (next == null) return;
  108. var myPosition = me.Position;
  109. me.Position = next.Position;
  110. next.Position = myPosition;
  111. _context.MobileShortcut.Update(me);
  112. _context.MobileShortcut.Update(next);
  113. _context.SaveChanges();
  114. }
  115. public void Save(MobileShortcut data) {
  116. CopyFromDefault(data.UserId, data.Type);
  117. var persist = _context.MobileShortcut.Where(
  118. x => x.UserId == data.UserId
  119. && x.SitemapId == data.SitemapId
  120. && x.Type == data.Type
  121. && x.IsDelete == false
  122. ).FirstOrDefault();
  123. // 등록시 중복체크
  124. if (data.MobileShortcutId == 0 && persist != null) {
  125. throw new ServiceException("이미 등록되어있는 메뉴입니다.");
  126. }
  127. if (data.MobileShortcutId != 0 && persist == null) {
  128. // 기존 데이터 수정
  129. persist = _context.MobileShortcut.FirstOrDefault(
  130. x => x.MobileShortcutId == data.MobileShortcutId
  131. );
  132. if (persist != null) {
  133. persist.SitemapId = data.SitemapId;
  134. persist.Icon = data.Icon;
  135. persist.Updated = DateTime.Now;
  136. }
  137. } else if (persist == null) {
  138. var maxPosition = _context.MobileShortcut.Where(
  139. x => x.UserId == data.UserId
  140. && x.Type == data.Type
  141. && x.IsDelete == false
  142. ).Max(x => x.Position);
  143. data.Created = DateTime.Now;
  144. data.Updated = DateTime.Now;
  145. data.Position = maxPosition + 1;
  146. _context.MobileShortcut.Add(data);
  147. _context.SaveChanges();
  148. } else {
  149. persist.SitemapId = data.SitemapId;
  150. persist.Icon = data.Icon;
  151. persist.Updated = DateTime.Now;
  152. _context.MobileShortcut.Update(persist);
  153. _context.SaveChanges();
  154. }
  155. RePosition(data.UserId, data.Type);
  156. }
  157. public IQueryable<MobileShortcut> GetAll() {
  158. var query = _context.MobileShortcut;
  159. return query;
  160. }
  161. public void Delete(string userId, int sitemapId, ShortcutType type) {
  162. CopyFromDefault(userId, type);
  163. var item = _context.MobileShortcut.FirstOrDefault(
  164. x => x.UserId == userId
  165. && x.SitemapId == sitemapId
  166. && x.Type == type
  167. && x.IsDelete == false
  168. );
  169. if (item != null) {
  170. _context.MobileShortcut.Remove(item);
  171. _context.SaveChanges();
  172. }
  173. RePosition(userId, type);
  174. }
  175. }
  176. }