基础服务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.
 
 

525 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. public string GetTypeName(string type)
  92. {
  93. var ary = type.Split(',');
  94. var name = "";
  95. for (int i = 0; i < ary.Length; i++)
  96. {
  97. if (ary[i] == "0")
  98. {
  99. name += "团餐";
  100. }
  101. if (ary[i] == "1")
  102. {
  103. name += "门店";
  104. }
  105. if (ary[i] == "2")
  106. {
  107. name += "后厨";
  108. }
  109. if (ary[i] == "3")
  110. {
  111. name += "公共";
  112. }
  113. if(i< ary.Length - 1)
  114. {
  115. name += ",";
  116. }
  117. }
  118. return name;
  119. }
  120. /// <summary>
  121. /// 新增菜单
  122. /// </summary>
  123. /// <param name="input"></param>
  124. /// <returns></returns>
  125. public async Task<bool> Add(MenuDtoInput input)
  126. {
  127. var dicttypes = _db.Queryable<BPA_Menu>().Where(a => a.Code == input.Code).ToList();
  128. if (dicttypes.Count() > 0)
  129. {
  130. throw Oops.Oh("编码已存在");
  131. }
  132. if (input.Name.Length > 20)
  133. {
  134. throw Oops.Oh("菜单名称过长");
  135. }
  136. var menu = input.Adapt<BPA_Menu>();
  137. return await _db.Insertable(menu).CallEntityMethod(t => t.Create()).ExecuteCommandAsync()> 0;
  138. }
  139. /// <summary>
  140. /// 编辑菜单
  141. /// </summary>
  142. /// <param name="input"></param>
  143. /// <returns></returns>
  144. public async Task<bool> Update(MenuDtoInput input)
  145. {
  146. var menu = await _db.Queryable< BPA_Menu >().Where(x=>x.Id== input.Id).FirstAsync();
  147. //menu = input.Adapt<BPA_Menu>();
  148. menu.Code = input.Code;
  149. menu.Component = input.Component;
  150. menu.Name= input.Name;
  151. menu.Pid= input.Pid;
  152. menu.Router = input.Router;
  153. menu.Remark= input.Remark;
  154. menu.Sort = input.Sort;
  155. menu.Icon = input.Icon;
  156. menu.IsAdmin= input.IsAdmin;
  157. menu.Type = input.Type;
  158. return await _db.Updateable(menu).ExecuteCommandAsync()>0;
  159. }
  160. /// <summary>
  161. /// 删除菜单
  162. /// </summary>
  163. /// <param name="input"></param>
  164. /// <returns></returns>
  165. public async Task<bool> Delete(List<string> input)
  166. {
  167. try
  168. {
  169. _db.Ado.BeginTran();
  170. // 查询数据库中是否存在未删除的活动信息
  171. var resEntitites = _db.Queryable<BPA_Menu>().In(input).ToList();
  172. for (int i = 0; i < resEntitites.Count; i++)
  173. {
  174. var childlist = _db.Queryable<BPA_Menu>().Where(x => x.Pid == resEntitites[i].Id).ToList();
  175. if(childlist.Count>0) resEntitites.AddRange(childlist);
  176. }
  177. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  178. _db.Ado.CommitTran();
  179. return res>0;
  180. }
  181. catch (Exception)
  182. {
  183. _db.Ado.RollbackTran();
  184. return false;
  185. }
  186. }
  187. /// <summary>
  188. /// 查询菜单树
  189. /// </summary>
  190. /// <param name="GetType"></param>
  191. /// <returns></returns>
  192. public async Task<List<MenuTreeOutput>> MenuTree(string GetType)
  193. {
  194. if (!string.IsNullOrWhiteSpace(GetType) && GetType.ToLower() == "user")
  195. {
  196. var userId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  197. // var GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  198. if ( string.IsNullOrWhiteSpace(userId))
  199. {
  200. return null;
  201. }
  202. List<BPA_Roles> dataRoles =await _db.Queryable<BPA_Roles>().ToListAsync();
  203. if (dataRoles.Count() <= 0)
  204. {
  205. return null;
  206. }
  207. var sysRoleId = dataRoles.Select(a => a.Id).ToArray();
  208. var menus = _db.Queryable<BPA_RoleMenu>().Where(t => sysRoleId.Contains(t.SysRoleId))
  209. .Select(x => x.SysMenuId).ToArray();
  210. var AllMenus = _db.Queryable<BPA_Menu>().Where(t => menus.Contains(t.Id)).OrderBy(t => t.Sort).ToList();
  211. var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
  212. {
  213. Key = t.Id,
  214. Title = t.Name,
  215. value = t.Id,
  216. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  217. {
  218. Key = x.Id,
  219. Title = x.Name,
  220. value = x.Id,
  221. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  222. {
  223. Key = y.Id,
  224. Title = y.Name,
  225. value = y.Id,
  226. }).ToList()
  227. }).ToList()
  228. }).ToList();
  229. return data;
  230. }
  231. else
  232. {
  233. var AllMenus = _db.Queryable<BPA_Menu>().OrderBy(t => t.Sort).ToList();
  234. var data = AllMenus.Where((t => t.Pid == null || t.Pid == "")).Select(t => new MenuTreeOutput
  235. {
  236. Key = t.Id,
  237. Title = t.Name,
  238. value = t.Id,
  239. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  240. {
  241. Key = x.Id,
  242. Title = x.Name,
  243. value = x.Id,
  244. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  245. {
  246. Key = y.Id,
  247. Title = y.Name,
  248. value = y.Name,
  249. }).ToList()
  250. }).ToList()
  251. }).ToList();
  252. var da = new List<MenuTreeOutput>();
  253. data.ForEach(x =>
  254. {
  255. if (x.Children.Count() > 0)
  256. {
  257. da.Add(x);
  258. }
  259. });
  260. return da;
  261. }
  262. }
  263. public async Task<List<MenuTreeOutput>> MenuParmyTree(string GetType, string type)
  264. {
  265. if (!string.IsNullOrWhiteSpace(GetType) && GetType.ToLower() == "user")
  266. {
  267. var userId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  268. // var GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  269. if (string.IsNullOrWhiteSpace(userId))
  270. {
  271. return null;
  272. }
  273. List<BPA_Roles> dataRoles = await _db.Queryable<BPA_Roles>().ToListAsync();
  274. if (dataRoles.Count() <= 0)
  275. {
  276. return null;
  277. }
  278. var sysRoleId = dataRoles.Select(a => a.Id).ToArray();
  279. var menus = _db.Queryable<BPA_RoleMenu>().Where(t => sysRoleId.Contains(t.SysRoleId))
  280. .Select(x => x.SysMenuId).ToArray();
  281. var AllMenus = _db.Queryable<BPA_Menu>().Where(t => menus.Contains(t.Id)).OrderBy(t => t.Sort).ToList();
  282. var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
  283. {
  284. Key = t.Id,
  285. Title = t.Name,
  286. value = t.Id,
  287. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  288. {
  289. Key = x.Id,
  290. Title = x.Name,
  291. value = x.Id,
  292. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  293. {
  294. Key = y.Id,
  295. Title = y.Name,
  296. value = y.Id,
  297. }).ToList()
  298. }).ToList()
  299. }).ToList();
  300. return data;
  301. }
  302. else
  303. {
  304. var AllMenus = _db.Queryable<BPA_Menu>().Where(t => t.IsAdmin == 0).OrderBy(t => t.Sort).ToList();
  305. var data = AllMenus.Where((t => t.Pid == null || t.Pid == "")).Select(t => new MenuTreeOutput
  306. {
  307. Key = t.Id,
  308. Title = t.Name,
  309. value = t.Id,
  310. type=t.Type,
  311. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  312. {
  313. Key = x.Id,
  314. Title = x.Name,
  315. value = x.Id,
  316. type = x.Type,
  317. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  318. {
  319. Key = y.Id,
  320. Title = y.Name,
  321. value = y.Name,
  322. type = y.Type,
  323. }).ToList()
  324. }).ToList()
  325. }).ToList();
  326. var da = new List<MenuTreeOutput>();
  327. data.ForEach(x =>
  328. {
  329. if (x.Children.Count() > 0 || x.type== type)
  330. {
  331. da.Add(x);
  332. }
  333. });
  334. return da;
  335. }
  336. }
  337. public async Task<List<MenuTreeOutput>> MenuTree()
  338. {
  339. var AllMenus = _db.Queryable<BPA_Menu>().OrderBy(t => t.Sort).ToList();
  340. var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
  341. {
  342. Key = t.Id,
  343. Title = t.Name,
  344. value = t.Id,
  345. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  346. {
  347. Key = x.Id,
  348. Title = x.Name,
  349. value = x.Id,
  350. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  351. {
  352. Key = y.Id,
  353. Title = y.Name,
  354. value = y.Id,
  355. }).ToList()
  356. }).ToList()
  357. }).ToList();
  358. return data;
  359. }
  360. /// <summary>
  361. /// 动态菜单(菜单树)
  362. /// </summary>
  363. /// <returns></returns>
  364. public async Task<List<MenuRouteDtoOutput>> DymicMenuTree(int type)
  365. {
  366. var UserId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  367. var IsAdmin = App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value;
  368. var account = App.User.FindFirst(ClaimConst.CLAINM_ACCOUNT)?.Value;
  369. if (UserId == null)
  370. return null;
  371. List<BPA_Menu> AllMenus = await _db.Queryable<BPA_Menu>().OrderBy(t => t.Sort).ToListAsync();
  372. List<BPA_Menu> ParnMenus = new();
  373. if (IsAdmin == "1" && account == "admin")
  374. {
  375. var adminmenu = await _db.Queryable<BPA_Menu>().Where(t =>t.IsAdmin==1).OrderBy(t => t.Sort).ToListAsync();
  376. var pids= adminmenu.Select(t => t.Pid).ToList();
  377. var pidmenu=await _db.Queryable<BPA_Menu>().Where(t => pids.Contains(t.Id)).ToListAsync();
  378. adminmenu.AddRange(pidmenu);
  379. //去重
  380. adminmenu = adminmenu.Where((x, i) => adminmenu.FindIndex(z => z.Id == x.Id) == i).ToList();
  381. var data = adminmenu.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 = adminmenu.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 = adminmenu.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. else
  406. {
  407. AllMenus= AllMenus.Where(t=> t.IsAdmin == 0).ToList();
  408. 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)
  409. .Where((t, x, y) => y.SysUserId == UserId && t.Status == 0 && t.IsAdmin==0).Select(t => t).ToListAsync();
  410. var data = ParnMenus.Where(t => t.Pid == null || t.Pid == "").OrderBy(t => t.Sort).Select(t => new MenuRouteDtoOutput
  411. {
  412. Id = t.Id,
  413. Component = t.Component,
  414. Icon = t.Icon,
  415. Name = t.Name,
  416. Path = t.Router,
  417. Routes = AllMenus.Where(y => y.Pid == t.Id).Select(x => new MenuRouteChildDtoOutput
  418. {
  419. Id = t.Id,
  420. Component = x.Component,
  421. Name = x.Name,
  422. Path = x.Router,
  423. Routes = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuRouteChildDtoOutput
  424. {
  425. Id = t.Id,
  426. Component = y.Component,
  427. Name = y.Name,
  428. Path = y.Router
  429. }).ToList()
  430. }).ToList()
  431. }).ToList();
  432. return data;
  433. }
  434. // ParnMenus = await _db.Queryable<BPA_Menu>().Where(t => (t.Name == "系统管理" || t.Name == "加盟商管理" || t.Name == "设备管理")).OrderBy(t => t.Sort).ToListAsync();
  435. //else
  436. // 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)
  437. // .Where((t, x, y) => y.SysUserId == UserId && t.Status == 0 && (t.Type== type || t.Type==3) && (t.Name!= "产品管理" || t.Name != "版本管理")).Select(t => t).ToListAsync();
  438. //菜单去重
  439. // ParnMenus = AllMenus.Where((x, i) => AllMenus.FindIndex(z => z.Id == x.Id) == i).ToList();
  440. //var data = ParnMenus.Where(t => t.Pid == null || t.Pid == "").OrderBy(t => t.Sort).Select(t => new MenuRouteDtoOutput
  441. //{
  442. // Id = t.Id,
  443. // Component = t.Component,
  444. // Icon = t.Icon,
  445. // Name = t.Name,
  446. // Path = t.Router,
  447. // Routes = AllMenus.Where(x => x.Pid == t.Id) .WhereIF(IsAdmin == "1" && account == "admin", x =>x.Name!="设备信息" && x.Name != "设备数据同步").Select(x => new MenuRouteChildDtoOutput
  448. // {
  449. // Id = t.Id,
  450. // Component = x.Component,
  451. // Name = x.Name,
  452. // Path = x.Router,
  453. // Routes = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuRouteChildDtoOutput
  454. // {
  455. // Id = t.Id,
  456. // Component = y.Component,
  457. // Name = y.Name,
  458. // Path = y.Router
  459. // }).ToList()
  460. // }).ToList()
  461. //}).ToList();
  462. // return data;
  463. }
  464. /// <summary>
  465. /// 启用
  466. /// </summary>
  467. /// <param name="Id"></param>
  468. /// <returns></returns>
  469. public async Task<bool> Enable(string Id)
  470. {
  471. var resEntitites = _db.Queryable<BPA_Menu>().In(Id).ToList();
  472. resEntitites.ForEach(x =>
  473. {
  474. x.Status = CommonStatus.ENABLE;
  475. });
  476. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  477. return res>0;
  478. }
  479. /// <summary>
  480. /// 禁用
  481. /// </summary>
  482. /// <param name="Id"></param>
  483. /// <returns></returns>
  484. public async Task<bool> Disable(string Id)
  485. {
  486. var resEntitites = _db.Queryable<BPA_Menu>().In(Id).ToList();
  487. resEntitites.ForEach(x =>
  488. {
  489. x.Status = CommonStatus.DISABLE;
  490. });
  491. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  492. return res>0;
  493. }
  494. }
  495. }