system_service.go 20 KB

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