system_service.go 20 KB

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