123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package user
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "property-system/errors"
- dbmodel "property-system/model"
- "property-system/pb"
- pb_v1 "property-system/pb/v1"
- )
- func openimRegister(userId int64, realName string, gender int32) string {
- openimId := fmt.Sprintf("property%d", userId)
- mreq := pb_v1.OpenImRegisterRequest{UserId: openimId, Platform: 5, NickName: realName, Gender: gender}
- _, err := pb.Thirdparty.OpenImRegister(context.Background(), &mreq)
- if err != nil {
- return ""
- }
- user := dbmodel.TUser{}
- where := map[string]interface{}{
- "id": userId,
- }
- values := map[string]interface{}{
- "openim_id": openimId,
- }
- err = user.Update(database.DB(), where, values)
- if err != nil {
- return ""
- }
- return openimId
- }
- //
- func UserList(ctx context.Context, req *pb_v1.UserListRequest) (reply *pb_v1.UserListReply, err error) {
- reply = &pb_v1.UserListReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- if req.Cid == 0 && req.GardenId == 0 {
- return nil, errors.ParamsError
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- if req.Page == 0 {
- req.Page = 1
- }
- reply.Page = req.Page
- p := &dbmodel.TUserGroup{}
- where := map[string]interface{}{}
- if req.Cid > 0 {
- where["cid"] = req.Cid
- }
- if req.GardenId > 0 {
- where["garden_id"] = req.GardenId
- }
- if len(req.Ids) > 0 {
- where["id in"] = req.Ids
- }
- if req.UserName != "" {
- where["user like"] = "%" + req.UserName + "%"
- }
- if req.GardenName != "" {
- where["garden_name like"] = "%" + req.GardenName + "%"
- }
- if req.ByCompany {
- where["super"] = 1
- }
- if req.Phone != "" {
- where["phone"] = req.Phone
- }
- if req.GroupId > 0 {
- where["group_id"] = req.GroupId
- }
- if len(req.Exclude) > 0 {
- where["id notin"] = req.Exclude
- }
- reply.Total, err = p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.List = make([]*pb_v1.UserItem, len(list))
- for i, v := range list {
- item := &pb_v1.UserItem{
- Cid: v.Cid,
- GardenId: v.GardenId,
- Phone: v.Phone,
- UserName: v.User,
- Email: v.Email,
- GroupId: v.GroupId,
- GroupName: v.GroupName,
- RealName: v.RealName,
- DepartmentName: v.DepartmentName,
- CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
- Id: v.ID,
- OpenimId: v.OpenimId,
- BasePermission: v.BasePermission,
- Gender: v.Gender,
- }
- if item.DepartmentName == "" {
- item.DepartmentName = "暂未指定部门"
- }
- if item.OpenimId == "" {
- item.OpenimId = openimRegister(v.ID, v.RealName, v.Gender)
- }
- reply.List[i] = item
- }
- return reply, nil
- }
|