package svc import ( "fmt" "strings" "git.i2edu.net/i2/i2-bill-erp/internal/config" "git.i2edu.net/i2/i2-bill-erp/model" "github.com/thoas/go-funk" "git.i2edu.net/i2/go-zero/core/stores/sqlc" "git.i2edu.net/i2/go-zero/core/stores/sqlx" ) type ServiceContext struct { Config config.Config SqlConn sqlx.SqlConn ErpUtil *ErpUtil StudentModel model.StudentModel SysUserModel model.SysUserModel MktPartTimeUser model.MktPartTimeUserModel } type ErpUtil struct { Config config.Config SqlConn sqlx.SqlConn } func (rp *ErpUtil) InRoles(userId string, roles ...string) (bool, error) { user := model.SysUser{} role := strings.Join(funk.Map(roles, func(r string) string { return fmt.Sprintf("'%v'", r) }).([]string), ",") err := rp.SqlConn.QueryRowPartial(&user, fmt.Sprintf(`SELECT sys_user_role.user_id id FROM sys_user_role left join sys_role on sys_role.id=sys_user_role.role_id 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) switch err { case nil: return true, nil case sqlc.ErrNotFound: return false, nil default: return true, nil } } func NewServiceContext(c config.Config) *ServiceContext { sc := ServiceContext{ Config: c, SqlConn: sqlx.NewMysql(c.DataSource), ErpUtil: &ErpUtil{}, } sc.StudentModel = model.NewStudentModel(sc.SqlConn, c.Cache) sc.SysUserModel = model.NewSysUserModel(sc.SqlConn, c.Cache) sc.MktPartTimeUser = model.NewMktPartTimeUserModel(sc.SqlConn, c.Cache) sc.ErpUtil.SqlConn = sc.SqlConn sc.ErpUtil.Config = sc.Config return &sc }