system_service.go 20 KB

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