system_service.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "kpt-pasture/model"
  7. "kpt-pasture/util"
  8. "net/http"
  9. "time"
  10. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  11. "go.uber.org/zap"
  12. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  13. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  14. "gitee.com/xuyiping_admin/pkg/jwt"
  15. "gitee.com/xuyiping_admin/pkg/xerr"
  16. "gorm.io/gorm"
  17. )
  18. const CurrentUserName = "userName"
  19. // Login 用户登录
  20. func (s *StoreEntry) Login(ctx context.Context, req *pasturePb.SearchUserRequest) (*pasturePb.SystemUserResponse, error) {
  21. systemUser := &model.SystemUser{}
  22. if err := s.DB.Where("name = ?", req.Name).
  23. Where("is_delete = ?", pasturePb.IsShow_Ok).
  24. Where("is_show = ?", pasturePb.IsShow_Ok).
  25. Find(systemUser).Error; err != nil {
  26. return nil, xerr.WithStack(err)
  27. }
  28. if systemUser.Password != req.Password {
  29. return nil, xerr.Customf("密码错误,来自用户:%s", req.Name)
  30. }
  31. if systemUser.IsShow == pasturePb.IsShow_No {
  32. return nil, xerr.Customf("该账号已被禁用,请联系管理员")
  33. }
  34. systemRole := &model.SystemRole{Id: systemUser.RoleId}
  35. if err := s.DB.Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
  36. First(systemRole).Error; err != nil {
  37. zaplog.Error("Login", zap.Any("systemRole", err))
  38. }
  39. jwtToken := jwt.NewJWTTokenGen(s.Cfg.AppName, s.Cfg.JwtTokenKeyConfig.PrivateKey)
  40. token, err := jwtToken.GenerateToken(systemUser.Name, s.Cfg.JwtExpireTime)
  41. if err != nil {
  42. return nil, xerr.WithStack(err)
  43. }
  44. if token == "" {
  45. return nil, xerr.Custom("获取token错误")
  46. }
  47. expires := time.Now().Add(time.Duration(s.Cfg.JwtExpireTime) * time.Second).Format(util.LayoutTime)
  48. return &pasturePb.SystemUserResponse{
  49. Code: http.StatusOK,
  50. Message: "ok",
  51. Data: &pasturePb.SystemUserData{
  52. AccessToken: token,
  53. Expires: expires,
  54. RefreshToken: token,
  55. Username: systemUser.Name,
  56. Roles: []string{systemRole.Name},
  57. Avatar: systemUser.Avatar,
  58. },
  59. }, nil
  60. }
  61. func (s *StoreEntry) GetSystemUserInfo(ctx context.Context, userId int64) (*model.SystemUser, error) {
  62. systemUser := &model.SystemUser{
  63. Id: userId,
  64. }
  65. if err := s.DB.First(systemUser).Error; err != nil {
  66. if errors.Is(err, gorm.ErrRecordNotFound) {
  67. return nil, xerr.Custom("该用户不存在")
  68. }
  69. return nil, xerr.WithStack(err)
  70. }
  71. return systemUser, nil
  72. }
  73. // SearchSystemUserList 查询系统用户
  74. func (s *StoreEntry) SearchSystemUserList(ctx context.Context, req *pasturePb.SearchUserRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchUserResponse, error) {
  75. systemUserList := make([]*model.SystemUser, 0)
  76. var count int64 = 0
  77. pref := s.DB.Model(new(model.SystemUser)).Where("is_delete = ?", operationPb.IsShow_OK)
  78. if req.Name != "" {
  79. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  80. }
  81. if req.Mobile != "" {
  82. pref.Where("mobile = ?", req.Mobile)
  83. }
  84. if req.DeptId > 0 {
  85. pref.Where("dept_id = ?", req.DeptId)
  86. }
  87. if req.IsShow > 0 {
  88. pref.Where("is_show = ?", req.IsShow)
  89. }
  90. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  91. Find(&systemUserList).Debug().Error; err != nil {
  92. return nil, xerr.WithStack(err)
  93. }
  94. roleList, err := s.SearchSystemRoleListByIds(ctx, []int64{})
  95. if err != nil {
  96. return nil, xerr.WithStack(err)
  97. }
  98. deptList, err := s.SearchSystemDeptListByIds(ctx, []int64{})
  99. if err != nil {
  100. return nil, xerr.WithStack(err)
  101. }
  102. return &pasturePb.SearchUserResponse{
  103. Code: http.StatusOK,
  104. Message: "ok",
  105. Data: &pasturePb.SearchUserData{
  106. List: model.SystemUserSlice(systemUserList).ToPB(deptList, roleList),
  107. Total: int32(count),
  108. PageSize: pagination.PageSize,
  109. Page: pagination.Page,
  110. },
  111. }, nil
  112. }
  113. // DeleteSystemUser 删除系统用户
  114. func (s *StoreEntry) DeleteSystemUser(ctx context.Context, userId int64) error {
  115. systemUser := &model.SystemUser{
  116. Id: userId,
  117. }
  118. if err := s.DB.First(systemUser).Error; err != nil {
  119. if errors.Is(err, gorm.ErrRecordNotFound) {
  120. return xerr.Custom("该用户不存在")
  121. }
  122. return xerr.WithStack(err)
  123. }
  124. if err := s.DB.Model(systemUser).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  125. return xerr.WithStack(err)
  126. }
  127. return nil
  128. }
  129. // IsShowSystemUser 用户是否启用
  130. func (s *StoreEntry) IsShowSystemUser(ctx context.Context, userId int64) error {
  131. systemUser := &model.SystemUser{
  132. Id: userId,
  133. }
  134. if err := s.DB.First(systemUser).Error; err != nil {
  135. if errors.Is(err, gorm.ErrRecordNotFound) {
  136. return xerr.Custom("该用户不存在")
  137. }
  138. return xerr.WithStack(err)
  139. }
  140. isShow := pasturePb.IsShow_No
  141. if systemUser.IsShow == pasturePb.IsShow_No {
  142. isShow = pasturePb.IsShow_Ok
  143. }
  144. if err := s.DB.Model(systemUser).Update("is_show", isShow).Error; err != nil {
  145. return xerr.WithStack(err)
  146. }
  147. return nil
  148. }
  149. // SystemUserCreateOrUpdate 创建或者更新系统用户
  150. func (s *StoreEntry) SystemUserCreateOrUpdate(ctx context.Context, req *pasturePb.SearchUserRequest) error {
  151. systemUser := &model.SystemUser{}
  152. if req.Id > 0 {
  153. systemUser.Id = int64(req.Id)
  154. if err := s.DB.First(systemUser).Error; err != nil {
  155. return xerr.WithStack(err)
  156. }
  157. req.Password = systemUser.Password
  158. } else {
  159. if err := s.DB.Where("name = ?", req.Name).First(systemUser).Error; err != nil {
  160. if !errors.Is(err, gorm.ErrRecordNotFound) {
  161. return xerr.WithStack(err)
  162. }
  163. } else if systemUser.Id > 0 {
  164. return xerr.Custom("该用户名称已经存在")
  165. }
  166. }
  167. if err := s.DB.Model(&model.SystemUser{}).Where(map[string]interface{}{
  168. "id": req.Id,
  169. }).Assign(map[string]interface{}{
  170. "name": req.Name,
  171. "nick_name": req.NickName,
  172. "mobile": req.Mobile,
  173. "gender": req.Gender,
  174. "password": req.Password,
  175. "dept_id": req.DeptId,
  176. "remarks": req.Remarks,
  177. "is_delete": operationPb.IsShow_OK,
  178. "is_show": req.IsShow,
  179. }).FirstOrCreate(&model.SystemUser{}).Error; err != nil {
  180. return xerr.WithStack(err)
  181. }
  182. return nil
  183. }
  184. // ResetPasswordSystemUser 重置系统用户密码
  185. func (s *StoreEntry) ResetPasswordSystemUser(ctx context.Context, req *pasturePb.ResetUserPasswordRequest) error {
  186. systemUser := &model.SystemUser{
  187. Id: int64(req.Id),
  188. }
  189. if err := s.DB.First(systemUser).Error; err != nil {
  190. if errors.Is(err, gorm.ErrRecordNotFound) {
  191. return xerr.Custom("该用户不存在")
  192. }
  193. return xerr.WithStack(err)
  194. }
  195. if err := s.DB.Model(systemUser).Update("password", req.Password).Error; err != nil {
  196. return xerr.WithStack(err)
  197. }
  198. return nil
  199. }
  200. func (s *StoreEntry) SystemUserRole(ctx context.Context, userId int64) (*pasturePb.SystemUserRoleResponse, error) {
  201. systemUser := &model.SystemUser{Id: userId}
  202. if err := s.DB.First(systemUser).Error; err != nil {
  203. if errors.Is(err, gorm.ErrRecordNotFound) {
  204. return nil, xerr.Custom("该用户不存在")
  205. }
  206. return nil, xerr.WithStack(err)
  207. }
  208. return &pasturePb.SystemUserRoleResponse{
  209. Code: http.StatusOK,
  210. Message: "ok",
  211. Data: int32(systemUser.RoleId),
  212. }, nil
  213. }
  214. func (s *StoreEntry) SystemUserRoleSave(ctx context.Context, req *pasturePb.SystemUserRoleRequest) error {
  215. systemUser := &model.SystemUser{Id: int64(req.UserId)}
  216. if err := s.DB.First(systemUser).Error; err != nil {
  217. if errors.Is(err, gorm.ErrRecordNotFound) {
  218. return xerr.Custom("该用户不存在")
  219. }
  220. return xerr.WithStack(err)
  221. }
  222. systemRole := &model.SystemRole{Id: int64(req.RoleId)}
  223. if err := s.DB.First(systemRole).Error; err != nil {
  224. if errors.Is(err, gorm.ErrRecordNotFound) {
  225. return xerr.Custom("该角色不存在")
  226. }
  227. return xerr.WithStack(err)
  228. }
  229. if err := s.DB.Model(systemUser).Update("role_id", systemRole.Id).Error; err != nil {
  230. return xerr.WithStack(err)
  231. }
  232. return nil
  233. }
  234. // GetCurrentUserName 获取当前用户名
  235. func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
  236. userNameInter := ctx.Value(CurrentUserName)
  237. if userNameInter == nil {
  238. return "", xerr.Customf("cannot userName")
  239. }
  240. if userName, ok := userNameInter.(string); ok {
  241. return userName, nil
  242. } else {
  243. return "", xerr.Customf("waring userName")
  244. }
  245. }
  246. func (s *StoreEntry) GetCurrentSystemUser(ctx context.Context) (*model.SystemUser, error) {
  247. // 解析token
  248. userName, err := s.GetCurrentUserName(ctx)
  249. if err != nil {
  250. return nil, xerr.WithStack(err)
  251. }
  252. // 根据用户token获取用户数据
  253. systemUser := &model.SystemUser{Name: userName}
  254. if err = s.DB.Where("name = ?", userName).
  255. Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
  256. First(systemUser).Error; err != nil {
  257. if errors.Is(err, gorm.ErrRecordNotFound) {
  258. return nil, xerr.Custom("该用户数据不存在")
  259. }
  260. return nil, xerr.WithStack(err)
  261. }
  262. return systemUser, nil
  263. }
  264. // GetSystemUserMenu 返回系统用户相关菜单权限
  265. func (s *StoreEntry) GetSystemUserMenu(ctx context.Context) (*pasturePb.SystemUserMenuTreeResponse, error) {
  266. // 解析token
  267. userName, err := s.GetCurrentUserName(ctx)
  268. if err != nil {
  269. return nil, xerr.WithStack(err)
  270. }
  271. // 根据用户token获取用户数据
  272. systemUser := &model.SystemUser{Name: userName}
  273. if err = s.DB.Where("name = ?", userName).
  274. Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
  275. First(systemUser).Error; err != nil {
  276. if errors.Is(err, gorm.ErrRecordNotFound) {
  277. return nil, xerr.Custom("该用户数据不存在")
  278. }
  279. return nil, xerr.WithStack(err)
  280. }
  281. if systemUser.RoleId <= 0 {
  282. return nil, xerr.Custom("该用户没有角色")
  283. }
  284. // 获取用户角色数据
  285. systemRole := &model.SystemRole{}
  286. if err = s.DB.Where("id = ?", systemUser.RoleId).First(systemRole).Error; err != nil {
  287. if errors.Is(err, gorm.ErrRecordNotFound) {
  288. return nil, xerr.Custom("该用户角色不存在")
  289. }
  290. return nil, xerr.WithStack(err)
  291. }
  292. // 获取用户菜单
  293. systemMenuList := make([]*model.SystemMenu, 0)
  294. if err = s.DB.Table(fmt.Sprintf("%s as a", new(model.SystemRoleMenu).TableName())).
  295. Select("b.*").
  296. Joins("LEFT JOIN system_menu AS b ON a.menu_id = b.id").
  297. Where("a.role_id = ? and b.is_delete = ?", systemRole.Id, pasturePb.IsShow_Ok).
  298. Find(&systemMenuList).Error; err != nil {
  299. return nil, xerr.WithStack(err)
  300. }
  301. // 获取菜单权限
  302. return &pasturePb.SystemUserMenuTreeResponse{
  303. Code: http.StatusOK,
  304. Message: "ok",
  305. Data: model.SystemMenuSlice(systemMenuList).ToTree(),
  306. }, nil
  307. }
  308. // SystemRoleCreateOrUpdate 添加角色
  309. func (s *StoreEntry) SystemRoleCreateOrUpdate(ctx context.Context, req *pasturePb.SearchRoleRequest) error {
  310. isShow := pasturePb.IsShow_Ok
  311. if req.Id > 0 {
  312. isShow = req.IsShow
  313. }
  314. if err := s.DB.Model(&model.SystemRole{}).Where(map[string]interface{}{
  315. "id": req.Id,
  316. }).Assign(map[string]interface{}{
  317. "name": req.Name,
  318. "remarks": req.Remarks,
  319. "is_delete": operationPb.IsShow_OK,
  320. "is_show": isShow,
  321. }).FirstOrCreate(&model.SystemRole{}).Error; err != nil {
  322. return xerr.WithStack(err)
  323. }
  324. return nil
  325. }
  326. // DeleteSystemRole 删除系统角色
  327. func (s *StoreEntry) DeleteSystemRole(ctx context.Context, roleId int64) error {
  328. systemRole := &model.SystemRole{
  329. Id: roleId,
  330. }
  331. if err := s.DB.First(systemRole).Error; err != nil {
  332. if errors.Is(err, gorm.ErrRecordNotFound) {
  333. return xerr.Custom("该数据不存在")
  334. }
  335. return xerr.WithStack(err)
  336. }
  337. if err := s.DB.Model(systemRole).Update("is_delete", pasturePb.IsShow_No).Error; err != nil {
  338. return xerr.WithStack(err)
  339. }
  340. return nil
  341. }
  342. // IsShowSystemRole 角色是否启用
  343. func (s *StoreEntry) IsShowSystemRole(ctx context.Context, roleId int64) error {
  344. systemRole := &model.SystemRole{
  345. Id: roleId,
  346. }
  347. if err := s.DB.First(systemRole).Error; err != nil {
  348. if errors.Is(err, gorm.ErrRecordNotFound) {
  349. return xerr.Custom("该角色不存在")
  350. }
  351. return xerr.WithStack(err)
  352. }
  353. isShow := pasturePb.IsShow_No
  354. if systemRole.IsShow == pasturePb.IsShow_No {
  355. isShow = pasturePb.IsShow_Ok
  356. }
  357. if err := s.DB.Model(systemRole).Update("is_show", isShow).Error; err != nil {
  358. return xerr.WithStack(err)
  359. }
  360. return nil
  361. }
  362. // SearchSystemRoleList 查询系统角色
  363. func (s *StoreEntry) SearchSystemRoleList(ctx context.Context, req *pasturePb.SearchRoleRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchRoleResponse, error) {
  364. systemRoleList := make([]*model.SystemRole, 0)
  365. var count int64 = 0
  366. pref := s.DB.Model(new(model.SystemRole)).Where("is_delete = ?", operationPb.IsShow_OK)
  367. if req.Name != "" {
  368. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  369. }
  370. if req.IsShow > 0 {
  371. pref.Where("is_show = ?", req.IsShow)
  372. }
  373. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).
  374. Offset(int(pagination.PageOffset)).Find(&systemRoleList).Error; err != nil {
  375. return nil, xerr.WithStack(err)
  376. }
  377. return &pasturePb.SearchRoleResponse{
  378. Code: http.StatusOK,
  379. Message: "ok",
  380. Data: &pasturePb.SearchRoleData{
  381. Page: pagination.Page,
  382. Total: int32(count),
  383. PageSize: pagination.PageSize,
  384. List: model.SystemRoleSlice(systemRoleList).ToPB(),
  385. },
  386. }, nil
  387. }
  388. // SearchSystemRoleListByIds 根据id查询角色列表
  389. func (s *StoreEntry) SearchSystemRoleListByIds(ctx context.Context, ids []int64) ([]*model.SystemRole, error) {
  390. systemRoleList := make([]*model.SystemRole, 0)
  391. if err := s.DB.Model(new(model.SystemRole)).
  392. Where("is_show = ?", operationPb.IsShow_OK).
  393. Find(&systemRoleList, ids).Error; err != nil {
  394. return nil, xerr.WithStack(err)
  395. }
  396. return systemRoleList, nil
  397. }
  398. // GetRoleMenuList 查询系统角色对应的菜单数据
  399. func (s *StoreEntry) GetRoleMenuList(ctx context.Context, roleId int64) (*pasturePb.SystemRoleMenuResponse, error) {
  400. systemRole := &model.SystemRole{
  401. Id: roleId,
  402. }
  403. if err := s.DB.First(systemRole).Error; err != nil {
  404. if errors.Is(err, gorm.ErrRecordNotFound) {
  405. return nil, xerr.Custom("该数据不存在")
  406. }
  407. return nil, xerr.WithStack(err)
  408. }
  409. if systemRole.IsDelete == pasturePb.IsShow_No {
  410. return nil, xerr.Custom("该数据已被删除")
  411. }
  412. menuList := make([]*model.SystemRoleMenu, 0)
  413. if err := s.DB.Where("role_id = ?", systemRole.Id).Find(&menuList).Error; err != nil {
  414. zaplog.Error("GetSystemRoleMenuList", zap.Any("Err", err))
  415. }
  416. return &pasturePb.SystemRoleMenuResponse{
  417. Code: http.StatusOK,
  418. Message: "ok",
  419. Data: model.SystemRoleMenuSlice(menuList).ToPB(),
  420. }, nil
  421. }
  422. // RoleMenuSave 保存系统角色对应的菜单数据
  423. func (s *StoreEntry) RoleMenuSave(ctx context.Context, res *pasturePb.SystemRoleMenuRequest) error {
  424. if len(res.MenuIds) <= 0 {
  425. return xerr.Custom("请选择菜单")
  426. }
  427. systemRole := &model.SystemRole{
  428. Id: int64(res.RoleId),
  429. }
  430. if err := s.DB.First(systemRole).Error; err != nil {
  431. if errors.Is(err, gorm.ErrRecordNotFound) {
  432. return xerr.Custom("该数据不存在")
  433. }
  434. return xerr.WithStack(err)
  435. }
  436. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  437. if err := tx.Model(new(model.SystemRoleMenu)).Where("role_id = ?", systemRole.Id).Delete(&model.SystemRoleMenu{}).Error; err != nil {
  438. return xerr.WithStack(err)
  439. }
  440. for _, menuId := range res.MenuIds {
  441. if err := tx.Create(&model.SystemRoleMenu{
  442. RoleId: systemRole.Id,
  443. MenuId: int64(menuId),
  444. }).Error; err != nil {
  445. return xerr.WithStack(err)
  446. }
  447. }
  448. return nil
  449. }); err != nil {
  450. return xerr.WithStack(err)
  451. }
  452. return nil
  453. }
  454. func (s *StoreEntry) SystemRoleList(ctx context.Context) (*pasturePb.GetRoleListResponse, error) {
  455. systemRoleList := make([]*model.SystemRole, 0)
  456. if err := s.DB.Model(new(model.SystemRole)).
  457. Where("is_delete = ? and is_show = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
  458. Find(&systemRoleList).Error; err != nil {
  459. return nil, xerr.WithStack(err)
  460. }
  461. return &pasturePb.GetRoleListResponse{
  462. Code: http.StatusOK,
  463. Message: "ok",
  464. Data: model.SystemRoleSlice(systemRoleList).ToPB(),
  465. }, nil
  466. }
  467. // CreateOrUpdateSystemMenu 添加或者更新系统菜单权限
  468. func (s *StoreEntry) CreateOrUpdateSystemMenu(ctx context.Context, req *pasturePb.SearchMenuRequest) error {
  469. if req.Id > 0 {
  470. systemMenu := &model.SystemMenu{Id: int64(req.Id)}
  471. if err := s.DB.Model(&model.SystemMenu{}).First(systemMenu).Error; err != nil {
  472. if !errors.Is(err, gorm.ErrRecordNotFound) {
  473. return xerr.WithStack(err)
  474. }
  475. }
  476. }
  477. if err := s.DB.Model(&model.SystemMenu{}).Where(map[string]interface{}{
  478. "id": req.Id,
  479. }).Assign(map[string]interface{}{
  480. "name": req.Name,
  481. "path": req.Path,
  482. "title": req.Title,
  483. "menu_type": req.MenuType,
  484. "parent_id": req.ParentId,
  485. "frame_src": req.FrameSrc,
  486. "frame_loading": model.BooleanToIsShow(req.FrameLoading),
  487. "keepalive": model.BooleanToIsShow(req.Keepalive),
  488. "hidden_tag": model.BooleanToIsShow(req.HiddenTag),
  489. "show_link": model.BooleanToIsShow(req.ShowLink),
  490. "show_parent": model.BooleanToIsShow(req.ShowParent),
  491. "icon": req.Icon,
  492. "component": req.Component,
  493. "redirect": req.Redirect,
  494. "auths": req.Auths,
  495. "rank": req.Rank,
  496. "extra_icon": req.ExtraIcon,
  497. "enter_transition": req.EnterTransition,
  498. "leave_transition": req.LeaveTransition,
  499. "active_path": req.ActivePath,
  500. "is_delete": pasturePb.IsShow_Ok,
  501. "is_show": pasturePb.IsShow_Ok,
  502. }).FirstOrCreate(&model.SystemMenu{}).Error; err != nil {
  503. return xerr.WithStack(err)
  504. }
  505. return nil
  506. }
  507. func (s *StoreEntry) SystemMenuTree(ctx context.Context) (*pasturePb.SystemMenuTreeResponse, error) {
  508. systemMenuList := make([]*model.SystemMenu, 0)
  509. if err := s.DB.Model(new(model.SystemMenu)).Where("is_delete = ?", pasturePb.IsShow_Ok).
  510. Where("is_show = ?", pasturePb.IsShow_Ok).Find(&systemMenuList).Error; err != nil {
  511. return nil, xerr.WithStack(err)
  512. }
  513. return &pasturePb.SystemMenuTreeResponse{
  514. Code: http.StatusOK,
  515. Message: "ok",
  516. Data: model.SystemMenuSlice(systemMenuList).ToPB(),
  517. }, nil
  518. }
  519. // SearchSystemMenuList 菜单列表查询
  520. func (s *StoreEntry) SearchSystemMenuList(ctx context.Context, req *pasturePb.SearchMenuRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchMenuResponse, error) {
  521. systemMenuList := make([]*model.SystemMenu, 0)
  522. var count int64 = 0
  523. if err := s.DB.Model(new(model.SystemMenu)).
  524. Where("is_delete = ? ", operationPb.IsShow_OK).
  525. Order("parent_id").Count(&count).
  526. Find(&systemMenuList).Error; err != nil {
  527. return nil, xerr.WithStack(err)
  528. }
  529. return &pasturePb.SearchMenuResponse{
  530. Code: http.StatusOK,
  531. Message: "ok",
  532. Data: &pasturePb.SearchMenuData{
  533. Page: pagination.Page,
  534. Total: int32(count),
  535. PageSize: pagination.PageSize,
  536. List: model.SystemMenuSlice(systemMenuList).ToPB(),
  537. },
  538. }, nil
  539. }
  540. // DeleteSystemMenu 删除系统菜单
  541. func (s *StoreEntry) DeleteSystemMenu(ctx context.Context, menuId int64) error {
  542. systemMenu := &model.SystemMenu{Id: menuId}
  543. if err := s.DB.First(systemMenu).Error; err != nil {
  544. if errors.Is(err, gorm.ErrRecordNotFound) {
  545. return xerr.Custom("该数据不存在")
  546. }
  547. return xerr.WithStack(err)
  548. }
  549. if err := s.DB.Model(systemMenu).Update("is_delete", pasturePb.IsShow_No).Error; err != nil {
  550. return xerr.WithStack(err)
  551. }
  552. return nil
  553. }
  554. func (s *StoreEntry) SearchSystemDeptList(ctx context.Context, req *pasturePb.SearchDeptRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDeptResponse, error) {
  555. deptList := make([]*model.SystemDept, 0)
  556. var count int64 = 0
  557. pref := s.DB.Model(new(model.SystemDept)).Where("is_delete = ?", operationPb.IsShow_OK)
  558. if req.Name != "" {
  559. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  560. }
  561. if req.IsShow > 0 {
  562. pref.Where("is_show = ?", req.IsShow)
  563. }
  564. if err := pref.Order("sort desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  565. Find(&deptList).Error; err != nil {
  566. return nil, xerr.WithStack(err)
  567. }
  568. return &pasturePb.SearchDeptResponse{
  569. Code: http.StatusOK,
  570. Message: "ok",
  571. Data: model.SystemDeptSlice(deptList).ToPB(),
  572. }, nil
  573. }
  574. func (s *StoreEntry) SystemDepDelete(ctx context.Context, id int64) error {
  575. dept := &model.SystemDept{Id: id}
  576. if err := s.DB.First(dept).Error; err != nil {
  577. return xerr.WithStack(err)
  578. }
  579. if dept.IsShow == pasturePb.IsShow_No {
  580. return nil
  581. }
  582. if err := s.DB.Model(dept).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  583. return xerr.WithStack(err)
  584. }
  585. return nil
  586. }
  587. func (s *StoreEntry) SystemDeptCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDeptRequest) error {
  588. if req.Id > 0 {
  589. barn := &model.SystemDept{Id: int64(req.Id)}
  590. if err := s.DB.Model(&model.SystemDept{}).First(barn).Error; err != nil {
  591. if !errors.Is(err, gorm.ErrRecordNotFound) {
  592. return xerr.WithStack(err)
  593. }
  594. }
  595. }
  596. if err := s.DB.Model(&model.SystemDept{}).Where(map[string]interface{}{
  597. "id": req.Id,
  598. }).Assign(map[string]interface{}{
  599. "name": req.Name,
  600. "sort": req.Sort,
  601. "parent_id": req.ParentId,
  602. "remarks": req.Remarks,
  603. "is_delete": operationPb.IsShow_OK,
  604. "is_show": req.IsShow,
  605. "dept_type": pasturePb.Depth_Department,
  606. "leader_id": req.LeaderId,
  607. }).FirstOrCreate(&model.SystemDept{}).Error; err != nil {
  608. return xerr.WithStack(err)
  609. }
  610. return nil
  611. }
  612. // SearchSystemDeptListByIds 根据id查询部门列表
  613. func (s *StoreEntry) SearchSystemDeptListByIds(ctx context.Context, ids []int64) ([]*model.SystemDept, error) {
  614. systemDeptList := make([]*model.SystemDept, 0)
  615. if err := s.DB.Model(new(model.SystemDept)).
  616. Where("is_show = ?", operationPb.IsShow_OK).
  617. Find(&systemDeptList, ids).Error; err != nil {
  618. return nil, xerr.WithStack(err)
  619. }
  620. return systemDeptList, nil
  621. }