123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- // 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/util"
- "property-company/consts"
- "property-company/errors"
- dbmodel "property-company/model"
- pb_v1 "property-company/pb/v1"
- "strings"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- func checkCompanyUserAddParam(req *pb_v1.CompanyUserAddRequest) error {
- switch {
- case req.Password == "":
- return status.Error(10003, "密码不能为空")
- case req.Phone == "":
- return status.Error(10003, "联系人电话不能为空")
- case req.UserName == "":
- return status.Error(10003, "账户名不能为空")
- case req.RealName == "":
- return status.Error(10003, "真实姓名不能为空")
- case req.Cid < 1:
- return status.Error(10003, "物业公司不能为空")
- }
- return nil
- }
- //
- func CompanyUserAdd(ctx context.Context, req *pb_v1.CompanyUserAddRequest) (reply *pb_v1.CompanyUserAddReply, err error) {
- reply = &pb_v1.CompanyUserAddReply{}
- // 捕获各个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"))
- }
- }
- }()
- err = checkCompanyUserAddParam(req)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
- user := dbmodel.TUser{
- Cid: req.Cid,
- User: req.UserName,
- Phone: req.Phone,
- Email: req.Email,
- Password: passwd,
- UpdatedAt: now,
- CreatedAt: now,
- Super: false,
- RealName: req.RealName,
- }
- db := database.DB().Begin()
- err = user.Insert(db)
- if err != nil {
- db.Rollback()
- if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
- return nil, errors.UserExist
- }
- return nil, errors.DataBaseError
- }
- /*
- openimId := fmt.Sprintf("property%d", user.ID)
- mreq := pb_v1.OpenImRegisterRequest{UserId: openimId, Platform: 5, NickName: req.RealName}
- _, err = pb.Thirdparty.OpenImRegister(ctx, &mreq)
- if err == nil {
- where := map[string]interface{}{
- "id": user.ID,
- }
- values := map[string]interface{}{
- "openim_id": openimId,
- }
- err = user.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- }*/
- db.Commit()
- reply.Id = user.ID
- return reply, nil
- }
|