servicecontext.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package svc
  2. import (
  3. "fmt"
  4. "strings"
  5. "git.i2edu.net/i2/i2-bill-erp/internal/config"
  6. "git.i2edu.net/i2/i2-bill-erp/model"
  7. "github.com/thoas/go-funk"
  8. "git.i2edu.net/i2/go-zero/core/stores/sqlc"
  9. "git.i2edu.net/i2/go-zero/core/stores/sqlx"
  10. )
  11. type ServiceContext struct {
  12. Config config.Config
  13. SqlConn sqlx.SqlConn
  14. ErpUtil *ErpUtil
  15. StudentModel model.StudentModel
  16. SysUserModel model.SysUserModel
  17. MktPartTimeUser model.MktPartTimeUserModel
  18. }
  19. type ErpUtil struct {
  20. Config config.Config
  21. SqlConn sqlx.SqlConn
  22. }
  23. func (rp *ErpUtil) InRoles(userId string, roles ...string) (bool, error) {
  24. user := model.SysUser{}
  25. role := strings.Join(funk.Map(roles, func(r string) string { return fmt.Sprintf("'%v'", r) }).([]string), ",")
  26. err := rp.SqlConn.QueryRowPartial(&user, fmt.Sprintf(`SELECT sys_user_role.user_id id FROM sys_user_role
  27. left join sys_role on sys_role.id=sys_user_role.role_id
  28. where sys_role.code in (%v) and sys_role.del_flag=0 and sys_user_role.del_flag=0 and sys_user_role.user_id=?`, role), userId)
  29. switch err {
  30. case nil:
  31. return true, nil
  32. case sqlc.ErrNotFound:
  33. return false, nil
  34. default:
  35. return true, nil
  36. }
  37. }
  38. func NewServiceContext(c config.Config) *ServiceContext {
  39. sc := ServiceContext{
  40. Config: c,
  41. SqlConn: sqlx.NewMysql(c.DataSource),
  42. ErpUtil: &ErpUtil{},
  43. }
  44. sc.StudentModel = model.NewStudentModel(sc.SqlConn, c.Cache)
  45. sc.SysUserModel = model.NewSysUserModel(sc.SqlConn, c.Cache)
  46. sc.MktPartTimeUser = model.NewMktPartTimeUserModel(sc.SqlConn, c.Cache)
  47. sc.ErpUtil.SqlConn = sc.SqlConn
  48. sc.ErpUtil.Config = sc.Config
  49. return &sc
  50. }