package model import ( "encoding/json" "fmt" "strconv" "strings" "time" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) type SystemUser struct { Id int64 `json:"id"` Name string `json:"name"` NickName string `json:"nickName"` Gender pasturePb.Genders_Kind `json:"gender"` Mobile string `json:"mobile"` Password string `json:"password"` Avatar string `json:"avatar"` RoleIds string `json:"roleIds"` DeptIds string `json:"deptIds"` IndicatorsKinds string `json:"indicatorsKinds"` PastureIds string `json:"pastureIds"` IsShow pasturePb.IsShow_Kind `json:"isShow"` IsDelete pasturePb.IsShow_Kind `json:"isDelete"` Remarks string `json:"remarks"` Spare string `json:"spare"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } func (s *SystemUser) TableName() string { return "system_user" } func (s *SystemUser) UserUpdate(req *pasturePb.SearchUserRequest, deptIds, pastureIds string) { s.Name = req.Name s.NickName = req.NickName s.Mobile = req.Mobile s.Gender = req.Gender s.DeptIds = deptIds s.Remarks = req.Remarks s.IsShow = req.IsShow s.PastureIds = pastureIds } func NewSystemUser(req *pasturePb.SearchUserRequest, deptIds, pastureIds string, pastureDepthList []*pasturePb.PastureDepthDetail) *SystemUser { spare := "" if len(pastureDepthList) > 0 { bt, _ := json.Marshal(pastureDepthList) spare = string(bt) } return &SystemUser{ Name: req.Name, NickName: req.NickName, Gender: req.Gender, Mobile: req.Mobile, Password: req.Password, Avatar: "https://avatars.githubusercontent.com/u/9510375", RoleIds: "", DeptIds: deptIds, IndicatorsKinds: fmt.Sprintf("%s,%s,%s,%s,%s,%s", AllCow, OutNumber, InputNumber, SalesVolume, FattenCattleNumber, AdultCow), PastureIds: pastureIds, IsShow: pasturePb.IsShow_Ok, IsDelete: pasturePb.IsShow_Ok, Remarks: req.Remarks, Spare: spare, } } func (s *SystemUser) GetPastureIds() []int32 { res := make([]int32, 0) if s.PastureIds != "" { pastureIds := strings.Split(s.PastureIds, ",") for _, idStr := range pastureIds { id, _ := strconv.Atoi(idStr) res = append(res, int32(id)) } } return res } func (s *SystemUser) GetRoleIds() []int32 { res := make([]int32, 0) if s.RoleIds != "" { roleIds := strings.Split(s.RoleIds, ",") for _, idStr := range roleIds { id, _ := strconv.Atoi(idStr) res = append(res, int32(id)) } } return res } func (s *SystemUser) GetDepthIds() []int32 { res := make([]int32, 0) if s.DeptIds != "" { depthIds := strings.Split(s.DeptIds, ",") for _, idStr := range depthIds { id, _ := strconv.Atoi(idStr) res = append(res, int32(id)) } } return res } type UserModel struct { SystemUser *SystemUser AppPasture *AppPastureList } type SystemUserSlice []*SystemUser func (s SystemUserSlice) ToPB(deptList []*SystemDept, roleList []*SystemRole, appPastureList []*AppPastureList) []*pasturePb.SearchUserRequest { deptMap := make(map[int32]*SystemDept) for _, v := range deptList { deptMap[int32(v.Id)] = v } roleMap := make(map[int32]*SystemRole) for _, v := range roleList { roleMap[int32(v.Id)] = v } appPastureMap := make(map[int32]*AppPastureList) for _, v := range appPastureList { appPastureMap[int32(v.Id)] = v } res := make([]*pasturePb.SearchUserRequest, len(s)) for i, v := range s { userDepthName := make([]string, 0) deptIds := v.GetDepthIds() for _, d := range deptIds { if de, ok := deptMap[d]; ok { userDepthName = append(userDepthName, de.Name) } } userRoleName := make([]string, 0) roleIds := v.GetRoleIds() for _, r := range roleIds { if ro, ok := roleMap[r]; ok { userRoleName = append(userRoleName, ro.Name) } } pastureName := make([]string, 0) for _, p := range v.GetPastureIds() { if pa, ok := appPastureMap[p]; ok { pastureName = append(pastureName, pa.Name) } } pastureDepthList := make([]*pasturePb.PastureDepthDetail, 0) if len(v.Spare) > 0 { json.Unmarshal([]byte(v.Spare), &pastureDepthList) } res[i] = &pasturePb.SearchUserRequest{ Id: int32(v.Id), Name: v.Name, NickName: v.NickName, Mobile: v.Mobile, Avatar: v.Avatar, Gender: v.Gender, IsShow: v.IsShow, IsDelete: v.IsDelete, Remarks: v.Remarks, PastureDepthList: pastureDepthList, DepthId: deptIds, DeptName: userDepthName, RoleList: nil, RoleId: v.GetRoleIds(), RoleName: userRoleName, PastureId: v.GetPastureIds(), PastureName: pastureName, CreatedAtFormat: time.Unix(v.CreatedAt, 0).Local().Format(LayoutTime), UpdatedAtFormat: time.Unix(v.UpdatedAt, 0).Local().Format(LayoutTime), } } return res } func (s SystemUserSlice) ToPB2() []*pasturePb.ConfigOptionsList { res := make([]*pasturePb.ConfigOptionsList, len(s)) for i, d := range s { res[i] = &pasturePb.ConfigOptionsList{ Value: int32(d.Id), Label: d.Name, Disabled: true, } } return res }