基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

496 lines
20 KiB

  1. using BPA.SAAS.Manage.Application.System.Dtos;
  2. using BPA.SAAS.Manage.Application.System.Interface;
  3. using BPA.SAAS.Manage.Comm.Const;
  4. using BPA.SAAS.Manage.Comm.Enum;
  5. using BPA.SAAS.Manage.Core.Org;
  6. using BPA.SAAS.Manage.Core.System;
  7. using Furion.LinqBuilder;
  8. using Mapster.Utils;
  9. using NPOI.SS.Formula.Functions;
  10. using NPOI.XSSF.UserModel;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Security.Cryptography;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace BPA.SAAS.Manage.Application.System.Services
  18. {
  19. public class MenuService : IMenuService, ITransient
  20. {
  21. private readonly ISqlSugarClient _db;
  22. public MenuService(ISqlSugarClient db)
  23. {
  24. _db = db;
  25. }
  26. /// <summary>
  27. /// 分页查询
  28. /// </summary>
  29. /// <param name="input"></param>
  30. /// <returns></returns>
  31. public async Task<List<MenuTreeDtoOutput>> GetMenuTreePage(MenuTreeSearchDtoInput input)
  32. {
  33. if (App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value == null)
  34. return null;
  35. var AllMenus =await _db.Queryable<BPA_Menu>().OrderBy(t => t.Sort).ToListAsync();
  36. var query = AllMenus.Where(t => t.Pid == "" || t.Pid == null);
  37. if (!input.Name.IsNullOrEmpty())
  38. query = query.Where(t => t.Name.Contains(input.Name));
  39. if (!input.Code.IsNullOrEmpty())
  40. query = query.Where(t => t.Code.Contains(input.Code));
  41. var data = query.Select(t => new MenuTreeDtoOutput
  42. {
  43. Id = t.Id,
  44. Name = t.Name,
  45. CreateAt = t.CreateAt,
  46. Code = t.Code,
  47. Icon = t.Icon,
  48. Router = t.Router,
  49. Pid = t.Pid,
  50. Sort = t.Sort,
  51. Component = t.Component,
  52. Status = t.Status,
  53. IsAdmin=t.IsAdmin,
  54. Type= t.Type,
  55. // TypeName= GetTypeName(t.Type),
  56. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeDtoOutput
  57. {
  58. Id = x.Id,
  59. Name = x.Name,
  60. CreateAt = x.CreateAt,
  61. Code = x.Code,
  62. Pid = x.Pid,
  63. Icon = x.Icon,
  64. Router = x.Router,
  65. Sort = x.Sort,
  66. Component = x.Component,
  67. Status = x.Status,
  68. IsAdmin=x.IsAdmin,
  69. Type = x.Type,
  70. //TypeName = GetTypeName(x.Type),
  71. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeDtoOutput
  72. {
  73. Id = y.Id.ToString(),
  74. Name = y.Name,
  75. CreateAt = y.CreateAt,
  76. Code = y.Code,
  77. Icon = y.Icon,
  78. Pid = y.Pid.ToString(),
  79. Router = y.Router,
  80. Sort = y.Sort,
  81. Component = y.Component,
  82. Status = y.Status,
  83. IsAdmin=y.IsAdmin,
  84. Type = y.Type,
  85. //TypeName = GetTypeName(y.Type),
  86. }).ToList()
  87. }).ToList()
  88. }).ToList();
  89. return data;
  90. }
  91. /// <summary>
  92. /// 新增菜单
  93. /// </summary>
  94. /// <param name="input"></param>
  95. /// <returns></returns>
  96. public async Task<bool> Add(MenuDtoInput input)
  97. {
  98. var dicttypes = _db.Queryable<BPA_Menu>().Where(a => a.Code == input.Code).ToList();
  99. if (dicttypes.Count() > 0)
  100. {
  101. throw Oops.Oh("编码已存在");
  102. }
  103. if (input.Name.Length > 20)
  104. {
  105. throw Oops.Oh("菜单名称过长");
  106. }
  107. var menu = input.Adapt<BPA_Menu>();
  108. return await _db.Insertable(menu).CallEntityMethod(t => t.Create()).ExecuteCommandAsync()> 0;
  109. }
  110. /// <summary>
  111. /// 编辑菜单
  112. /// </summary>
  113. /// <param name="input"></param>
  114. /// <returns></returns>
  115. public async Task<bool> Update(MenuDtoInput input)
  116. {
  117. var menu = await _db.Queryable< BPA_Menu >().Where(x=>x.Id== input.Id).FirstAsync();
  118. //menu = input.Adapt<BPA_Menu>();
  119. menu.Code = input.Code;
  120. menu.Component = input.Component;
  121. menu.Name= input.Name;
  122. menu.Pid= input.Pid;
  123. menu.Router = input.Router;
  124. menu.Remark= input.Remark;
  125. menu.Sort = input.Sort;
  126. menu.Icon = input.Icon;
  127. menu.IsAdmin= input.IsAdmin;
  128. menu.Type = input.Type;
  129. return await _db.Updateable(menu).ExecuteCommandAsync()>0;
  130. }
  131. /// <summary>
  132. /// 删除菜单
  133. /// </summary>
  134. /// <param name="input"></param>
  135. /// <returns></returns>
  136. public async Task<bool> Delete(List<string> input)
  137. {
  138. try
  139. {
  140. _db.Ado.BeginTran();
  141. // 查询数据库中是否存在未删除的活动信息
  142. var resEntitites = _db.Queryable<BPA_Menu>().In(input).ToList();
  143. for (int i = 0; i < resEntitites.Count; i++)
  144. {
  145. var childlist = _db.Queryable<BPA_Menu>().Where(x => x.Pid == resEntitites[i].Id).ToList();
  146. if(childlist.Count>0) resEntitites.AddRange(childlist);
  147. }
  148. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  149. _db.Ado.CommitTran();
  150. return res>0;
  151. }
  152. catch (Exception)
  153. {
  154. _db.Ado.RollbackTran();
  155. return false;
  156. }
  157. }
  158. /// <summary>
  159. /// 查询菜单树
  160. /// </summary>
  161. /// <param name="GetType"></param>
  162. /// <returns></returns>
  163. public async Task<List<MenuTreeOutput>> MenuTree(string GetType)
  164. {
  165. if (!string.IsNullOrWhiteSpace(GetType) && GetType.ToLower() == "user")
  166. {
  167. var userId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  168. // var GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  169. if ( string.IsNullOrWhiteSpace(userId))
  170. {
  171. return null;
  172. }
  173. List<BPA_Roles> dataRoles =await _db.Queryable<BPA_Roles>().ToListAsync();
  174. if (dataRoles.Count() <= 0)
  175. {
  176. return null;
  177. }
  178. var sysRoleId = dataRoles.Select(a => a.Id).ToArray();
  179. var menus = _db.Queryable<BPA_RoleMenu>().Where(t => sysRoleId.Contains(t.SysRoleId))
  180. .Select(x => x.SysMenuId).ToArray();
  181. var AllMenus = _db.Queryable<BPA_Menu>().Where(t => menus.Contains(t.Id)).OrderBy(t => t.Sort).ToList();
  182. var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
  183. {
  184. Key = t.Id,
  185. Title = t.Name,
  186. value = t.Id,
  187. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  188. {
  189. Key = x.Id,
  190. Title = x.Name,
  191. value = x.Id,
  192. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  193. {
  194. Key = y.Id,
  195. Title = y.Name,
  196. value = y.Id,
  197. }).ToList()
  198. }).ToList()
  199. }).ToList();
  200. return data;
  201. }
  202. else
  203. {
  204. var AllMenus = _db.Queryable<BPA_Menu>().OrderBy(t => t.Sort).ToList();
  205. var data = AllMenus.Where((t => t.Pid == null || t.Pid == "")).Select(t => new MenuTreeOutput
  206. {
  207. Key = t.Id,
  208. Title = t.Name,
  209. value = t.Id,
  210. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  211. {
  212. Key = x.Id,
  213. Title = x.Name,
  214. value = x.Id,
  215. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  216. {
  217. Key = y.Id,
  218. Title = y.Name,
  219. value = y.Name,
  220. }).ToList()
  221. }).ToList()
  222. }).ToList();
  223. var da = new List<MenuTreeOutput>();
  224. data.ForEach(x =>
  225. {
  226. if (x.Children.Count() > 0)
  227. {
  228. da.Add(x);
  229. }
  230. });
  231. return da;
  232. }
  233. }
  234. public async Task<List<MenuTreeOutput>> MenuParmyTree(string GetType, string type)
  235. {
  236. if (!string.IsNullOrWhiteSpace(GetType) && GetType.ToLower() == "user")
  237. {
  238. var userId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  239. // var GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  240. if (string.IsNullOrWhiteSpace(userId))
  241. {
  242. return null;
  243. }
  244. List<BPA_Roles> dataRoles = await _db.Queryable<BPA_Roles>().ToListAsync();
  245. if (dataRoles.Count() <= 0)
  246. {
  247. return null;
  248. }
  249. var sysRoleId = dataRoles.Select(a => a.Id).ToArray();
  250. var menus = _db.Queryable<BPA_RoleMenu>().Where(t => sysRoleId.Contains(t.SysRoleId))
  251. .Select(x => x.SysMenuId).ToArray();
  252. var AllMenus = _db.Queryable<BPA_Menu>().Where(t => menus.Contains(t.Id)).OrderBy(t => t.Sort).ToList();
  253. var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
  254. {
  255. Key = t.Id,
  256. Title = t.Name,
  257. value = t.Id,
  258. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  259. {
  260. Key = x.Id,
  261. Title = x.Name,
  262. value = x.Id,
  263. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  264. {
  265. Key = y.Id,
  266. Title = y.Name,
  267. value = y.Id,
  268. }).ToList()
  269. }).ToList()
  270. }).ToList();
  271. return data;
  272. }
  273. else
  274. {
  275. var AllMenus = _db.Queryable<BPA_Menu>().Where(t => t.IsAdmin == 0).OrderBy(t => t.Sort).ToList();
  276. var data = AllMenus.Where((t => t.Pid == null || t.Pid == "")).Select(t => new MenuTreeOutput
  277. {
  278. Key = t.Id,
  279. Title = t.Name,
  280. value = t.Id,
  281. type=t.Type,
  282. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  283. {
  284. Key = x.Id,
  285. Title = x.Name,
  286. value = x.Id,
  287. type = x.Type,
  288. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  289. {
  290. Key = y.Id,
  291. Title = y.Name,
  292. value = y.Name,
  293. type = y.Type,
  294. }).ToList()
  295. }).ToList()
  296. }).ToList();
  297. var da = new List<MenuTreeOutput>();
  298. //data.ForEach(x =>
  299. //{
  300. // if (x.Children.Count() > 0 || x.type== type)
  301. // {
  302. // da.Add(x);
  303. // }
  304. //});
  305. return data;
  306. }
  307. }
  308. public async Task<List<MenuTreeOutput>> MenuTree()
  309. {
  310. var AllMenus = _db.Queryable<BPA_Menu>().OrderBy(t => t.Sort).ToList();
  311. var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
  312. {
  313. Key = t.Id,
  314. Title = t.Name,
  315. value = t.Id,
  316. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  317. {
  318. Key = x.Id,
  319. Title = x.Name,
  320. value = x.Id,
  321. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  322. {
  323. Key = y.Id,
  324. Title = y.Name,
  325. value = y.Id,
  326. }).ToList()
  327. }).ToList()
  328. }).ToList();
  329. return data;
  330. }
  331. /// <summary>
  332. /// 动态菜单(菜单树)
  333. /// </summary>
  334. /// <returns></returns>
  335. public async Task<List<MenuRouteDtoOutput>> DymicMenuTree(int type)
  336. {
  337. var UserId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  338. var IsAdmin = App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value;
  339. var account = App.User.FindFirst(ClaimConst.CLAINM_ACCOUNT)?.Value;
  340. if (UserId == null)
  341. return null;
  342. List<BPA_Menu> AllMenus = await _db.Queryable<BPA_Menu>().OrderBy(t => t.Sort).ToListAsync();
  343. List<BPA_Menu> ParnMenus = new();
  344. if (IsAdmin == "1" && account == "admin")
  345. {
  346. var adminmenu = await _db.Queryable<BPA_Menu>().Where(t =>t.IsAdmin==1).OrderBy(t => t.Sort).ToListAsync();
  347. var pids= adminmenu.Select(t => t.Pid).ToList();
  348. var pidmenu=await _db.Queryable<BPA_Menu>().Where(t => pids.Contains(t.Id)).ToListAsync();
  349. adminmenu.AddRange(pidmenu);
  350. //去重
  351. adminmenu = adminmenu.Where((x, i) => adminmenu.FindIndex(z => z.Id == x.Id) == i).ToList();
  352. var data = adminmenu.Where(t => t.Pid == null || t.Pid == "").OrderBy(t => t.Sort).Select(t => new MenuRouteDtoOutput
  353. {
  354. Id = t.Id,
  355. Component = t.Component,
  356. Icon = t.Icon,
  357. Name = t.Name,
  358. Path = t.Router,
  359. Routes = adminmenu.Where(y => y.Pid == t.Id).Select(x => new MenuRouteChildDtoOutput
  360. {
  361. Id = t.Id,
  362. Component = x.Component,
  363. Name = x.Name,
  364. Path = x.Router,
  365. Routes = adminmenu.Where(y => y.Pid == x.Id).Select(y => new MenuRouteChildDtoOutput
  366. {
  367. Id = t.Id,
  368. Component = y.Component,
  369. Name = y.Name,
  370. Path = y.Router
  371. }).ToList()
  372. }).ToList()
  373. }).ToList();
  374. return data;
  375. }
  376. else
  377. {
  378. AllMenus= AllMenus.Where(t=> t.IsAdmin == 0).ToList();
  379. ParnMenus = await _db.Queryable<BPA_Menu>().LeftJoin<BPA_RoleMenu>((t, x) => t.Id == x.SysMenuId).LeftJoin<BPA_UserRole>((t, x, y) => x.SysRoleId == y.SysRoleId)
  380. .Where((t, x, y) => y.SysUserId == UserId && t.Status == 0 && t.IsAdmin==0).Select(t => t).ToListAsync();
  381. var data = ParnMenus.Where(t => t.Pid == null || t.Pid == "").OrderBy(t => t.Sort).Select(t => new MenuRouteDtoOutput
  382. {
  383. Id = t.Id,
  384. Component = t.Component,
  385. Icon = t.Icon,
  386. Name = t.Name,
  387. Path = t.Router,
  388. Routes = AllMenus.Where(y => y.Pid == t.Id).Select(x => new MenuRouteChildDtoOutput
  389. {
  390. Id = t.Id,
  391. Component = x.Component,
  392. Name = x.Name,
  393. Path = x.Router,
  394. Routes = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuRouteChildDtoOutput
  395. {
  396. Id = t.Id,
  397. Component = y.Component,
  398. Name = y.Name,
  399. Path = y.Router
  400. }).ToList()
  401. }).ToList()
  402. }).ToList();
  403. return data;
  404. }
  405. // ParnMenus = await _db.Queryable<BPA_Menu>().Where(t => (t.Name == "系统管理" || t.Name == "加盟商管理" || t.Name == "设备管理")).OrderBy(t => t.Sort).ToListAsync();
  406. //else
  407. // ParnMenus = await _db.Queryable<BPA_Menu>().LeftJoin<BPA_RoleMenu>((t, x) => t.Id == x.SysMenuId).LeftJoin<BPA_UserRole>((t, x, y) => x.SysRoleId == y.SysRoleId)
  408. // .Where((t, x, y) => y.SysUserId == UserId && t.Status == 0 && (t.Type== type || t.Type==3) && (t.Name!= "产品管理" || t.Name != "版本管理")).Select(t => t).ToListAsync();
  409. //菜单去重
  410. // ParnMenus = AllMenus.Where((x, i) => AllMenus.FindIndex(z => z.Id == x.Id) == i).ToList();
  411. //var data = ParnMenus.Where(t => t.Pid == null || t.Pid == "").OrderBy(t => t.Sort).Select(t => new MenuRouteDtoOutput
  412. //{
  413. // Id = t.Id,
  414. // Component = t.Component,
  415. // Icon = t.Icon,
  416. // Name = t.Name,
  417. // Path = t.Router,
  418. // Routes = AllMenus.Where(x => x.Pid == t.Id) .WhereIF(IsAdmin == "1" && account == "admin", x =>x.Name!="设备信息" && x.Name != "设备数据同步").Select(x => new MenuRouteChildDtoOutput
  419. // {
  420. // Id = t.Id,
  421. // Component = x.Component,
  422. // Name = x.Name,
  423. // Path = x.Router,
  424. // Routes = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuRouteChildDtoOutput
  425. // {
  426. // Id = t.Id,
  427. // Component = y.Component,
  428. // Name = y.Name,
  429. // Path = y.Router
  430. // }).ToList()
  431. // }).ToList()
  432. //}).ToList();
  433. // return data;
  434. }
  435. /// <summary>
  436. /// 启用
  437. /// </summary>
  438. /// <param name="Id"></param>
  439. /// <returns></returns>
  440. public async Task<bool> Enable(string Id)
  441. {
  442. var resEntitites = _db.Queryable<BPA_Menu>().In(Id).ToList();
  443. resEntitites.ForEach(x =>
  444. {
  445. x.Status = CommonStatus.ENABLE;
  446. });
  447. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  448. return res>0;
  449. }
  450. /// <summary>
  451. /// 禁用
  452. /// </summary>
  453. /// <param name="Id"></param>
  454. /// <returns></returns>
  455. public async Task<bool> Disable(string Id)
  456. {
  457. var resEntitites = _db.Queryable<BPA_Menu>().In(Id).ToList();
  458. resEntitites.ForEach(x =>
  459. {
  460. x.Status = CommonStatus.DISABLE;
  461. });
  462. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  463. return res>0;
  464. }
  465. }
  466. }