get_erp_role_logic.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package logic
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "git.i2edu.net/i2/i2-bill-erp/internal/svc"
  7. "git.i2edu.net/i2/i2-bill-erp/transform"
  8. "github.com/tal-tech/go-zero/core/logx"
  9. )
  10. type GetErpRoleLogic struct {
  11. ctx context.Context
  12. svcCtx *svc.ServiceContext
  13. logx.Logger
  14. }
  15. func NewGetErpRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetErpRoleLogic {
  16. return &GetErpRoleLogic{
  17. ctx: ctx,
  18. svcCtx: svcCtx,
  19. Logger: logx.WithContext(ctx),
  20. }
  21. }
  22. // 获取 erp 用户角色
  23. func (l *GetErpRoleLogic) GetErpRole(in *transform.GetErpRoleReq) (*transform.GetErpRoleRes, error) {
  24. // todo: add your logic here and delete this line
  25. if in.Mobile == "" && in.UserId == "" {
  26. return nil, errors.New("mobile and user_id is empty")
  27. }
  28. sql := `select
  29. sys_user.id user_id,GROUP_CONCAT(sys_role.code) role
  30. from
  31. sys_user
  32. LEFT JOIN
  33. sys_user_role on sys_user.id=sys_user_role.user_id
  34. LEFT JOIN sys_role on sys_user_role.role_id= sys_role.id
  35. where
  36. sys_user_role.del_flag =0 and sys_user.status = 0 %s GROUP BY sys_user.mobile`
  37. var args string
  38. if in.Mobile != "" {
  39. args = in.Mobile
  40. sql = fmt.Sprintf(sql, " and sys_user.mobile = ? ")
  41. }
  42. if in.UserId != "" {
  43. args = in.UserId
  44. sql = fmt.Sprintf(sql, " and sys_user.id = ? ")
  45. }
  46. result, err := l.svcCtx.DB.SQL(sql, args).Query().List()
  47. if err != nil {
  48. return nil, err
  49. }
  50. if len(result) == 0 {
  51. return &transform.GetErpRoleRes{}, nil
  52. }
  53. user_id, _ := result[0]["user_id"].(string)
  54. role, _ := result[0]["role"].(string)
  55. return &transform.GetErpRoleRes{UserId: user_id, Role: role}, nil
  56. }