package model import ( "fmt" operationPb "kpt-tmr-group/proto/go/backend/operation" "net/http" "strconv" "strings" "time" ) type SystemUser struct { Id int64 `json:"id"` Name string `json:"name"` EmployeeName string `json:"employee_name"` Phone string `json:"phone"` Password string `json:"password"` RoleIds string `json:"role_ids"` CreateUser string `json:"create_user"` IsShow operationPb.IsShow_Kind `json:"is_show"` IsDelete operationPb.IsShow_Kind `json:"is_delete"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } func (s *SystemUser) TableName() string { return "system_user" } func (s *SystemUser) SystemUserFormat(userRoles []*SystemRole, pastures []*operationPb.UserPasture) *operationPb.UserAuth { roles := make([]*operationPb.UserRole, len(userRoles)) for k, v := range userRoles { roles[k] = &operationPb.UserRole{ Id: int32(v.Id), Name: v.Name, } } return &operationPb.UserAuth{ Code: http.StatusOK, Msg: "ok", Data: &operationPb.UserAuthData{ UserName: s.Name, Phone: s.Phone, EmployeeName: s.EmployeeName, Roles: roles, Pastures: pastures, }, } } func (s *SystemUser) SystemUserRoleFormat(req *operationPb.AddSystemUser) { roleIds := "" for _, role := range req.Roles { roleIds += fmt.Sprintf("%d,", role.Id) } if roleIds != "" { s.RoleIds = strings.TrimRight(roleIds, ",") } } func (s *SystemUser) SystemUserRoleToSlice() []int { roleIds := make([]int, 0) if s.RoleIds != "" { roleIdsStr := strings.Split(s.RoleIds, ",") for _, v := range roleIdsStr { roleIdsInt, _ := strconv.Atoi(v) roleIds = append(roleIds, roleIdsInt) } } return roleIds } type SystemUserSlice []*SystemUser func (s SystemUserSlice) ToPB(roleList []*SystemRole) []*operationPb.AddSystemUser { res := make([]*operationPb.AddSystemUser, len(s)) for i, v := range s { res[i] = &operationPb.AddSystemUser{ Id: int32(v.Id), Name: v.Name, Phone: v.Phone, EmployeeName: v.EmployeeName, CreateUser: v.CreateUser, IsShow: v.IsShow, CreatedAt: int32(v.CreatedAt), CreatedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime), RoleName: strings.TrimRight(v.UserRoleFormat(roleList), ","), } } return res } func (s *SystemUser) UserRoleFormat(roleList []*SystemRole) string { ids := strings.Split(s.RoleIds, ",") roleListName := "" for _, id := range ids { for _, r := range roleList { if fmt.Sprintf("%d", r.Id) != id { continue } roleListName += fmt.Sprintf("%s,", r.Name) } } return roleListName } func (s *SystemUser) ToPb() *operationPb.AddSystemUser { roles := make([]int32, 0) roleIds := strings.Split(s.RoleIds, ",") for _, roleId := range roleIds { id, _ := strconv.Atoi(roleId) roles = append(roles, int32(id)) } return &operationPb.AddSystemUser{ Id: int32(s.Id), Name: s.Name, Phone: s.Phone, CreateUser: s.CreateUser, EmployeeName: s.EmployeeName, IsShow: s.IsShow, CreatedAt: int32(s.CreatedAt), CreatedAtFormat: time.Unix(s.CreatedAt, 0).Format(LayoutTime), RoleIds: roles, } }