package model import ( "fmt" operationPb "kpt-tmr-group/proto/go/backend/operation" "strconv" "strings" "time" ) type SystemRole struct { Id int64 `json:"id,omitempty"` Name string `json:"name,omitempty"` Remarks string `json:"remarks,omitempty"` MenuId string `json:"menu_id"` MobileId string `json:"mobile_id"` PastureId string `json:"pasture_id"` IsShow operationPb.IsShow_Kind `json:"is_show,omitempty"` CreateUser string `json:"create_user,omitempty"` CreatedAt int64 `json:"created_at,omitempty"` UpdatedAt int64 `json:"updated_at,omitempty"` } func (s *SystemRole) TableName() string { return "system_role" } const LayoutTime = "2006-01-02 15:04:05" func NewSystemRole(req *operationPb.AddRoleRequest) *SystemRole { systemRole := &SystemRole{ Name: req.Name, Remarks: req.Remarks, IsShow: operationPb.IsShow_OK, CreateUser: req.CreateUser, } systemRole.RolePermissionsFormat(req) return systemRole } func (r *SystemRole) RolePermissionsFormat(req *operationPb.AddRoleRequest) { pastureId := "" for _, v := range req.PastureId { pastureId += fmt.Sprintf("%d,", v) } menuId := "" for _, v := range req.MenuId { menuId += fmt.Sprintf("%d,", v) } mobileId := "" for _, v := range req.MobileId { mobileId += fmt.Sprintf("%d", v) } if pastureId != "" { r.PastureId = strings.TrimRight(pastureId, ",") } if mobileId != "" { r.MobileId = strings.TrimRight(mobileId, ",") } if menuId != "" { r.MenuId = strings.TrimRight(menuId, ",") } } func (s *SystemRole) RoleForMenuToSlice() []int { menuIds := make([]int, 0) if s.MenuId != "" { menuIdsStr := strings.Split(s.MenuId, ",") for _, v := range menuIdsStr { menuIdsInt, _ := strconv.Atoi(v) menuIds = append(menuIds, menuIdsInt) } } return menuIds } func (s *SystemRole) RoleForMobileToSlice() []int { mobileIds := make([]int, 0) if s.MobileId != "" { mobileIdsStr := strings.Split(s.MenuId, ",") for _, v := range mobileIdsStr { mobileIdsInt, _ := strconv.Atoi(v) mobileIds = append(mobileIds, mobileIdsInt) } } return mobileIds } func (s *SystemRole) RoleForPastureToSlice() []int { pastureIds := make([]int, 0) if s.PastureId != "" { pastureIdsStr := strings.Split(s.MenuId, ",") for _, v := range pastureIdsStr { pastureIdsInt, _ := strconv.Atoi(v) pastureIds = append(pastureIds, pastureIdsInt) } } return pastureIds } type SystemRoleSlice []*SystemRole func (s SystemRoleSlice) ToPB() []*operationPb.AddRoleRequest { res := make([]*operationPb.AddRoleRequest, len(s)) for i, v := range s { res[i] = &operationPb.AddRoleRequest{ Id: v.Id, Name: v.Name, Remarks: v.Remarks, CreateUser: v.CreateUser, IsShow: v.IsShow, CreatedAt: v.CreatedAt, CratedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime), } } return res } func (s *SystemRole) ToPb() *operationPb.AddRoleRequest { return &operationPb.AddRoleRequest{ Id: s.Id, Name: s.Name, CreateUser: s.CreateUser, IsShow: s.IsShow, CreatedAt: s.CreatedAt, } } type SystemRoleResponse struct { Page int32 `json:"page"` Total int32 `json:"total"` List []*operationPb.AddRoleRequest `json:"list"` } func (s *SystemRoleResponse) ToPB() *operationPb.SearchRoleResponse { return &operationPb.SearchRoleResponse{ Page: s.Page, Total: s.Total, List: s.List, } }