// 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" "git.getensh.com/common/gopkgs/util" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" "property-company/consts" "property-company/errors" dbmodel "property-company/model" pb_v1 "property-company/pb/v1" "strings" "time" ) func checkCompanyUserUpdateParam(req *pb_v1.CompanyUserUpdateRequest) error { switch { 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, "物业公司不能为空") case req.Id < 1: return status.Error(10003, "用户id不能为空") } return nil } // func CompanyUserUpdate(ctx context.Context, req *pb_v1.CompanyUserUpdateRequest) (reply *pb_v1.CompanyUserUpdateReply, err error) { reply = &pb_v1.CompanyUserUpdateReply{} // 捕获各个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 = checkCompanyUserUpdateParam(req) if err != nil { return nil, err } now := time.Now() passwd := "" if req.Password != "" { passwd, _ = util.GetMd5Pass(req.Password, consts.CRYPTO_KEY) } where := map[string]interface{}{ "id": req.Id, "cid": req.Cid, } values := map[string]interface{}{ "email": req.Email, "user": req.UserName, "phone": req.Phone, "updated_at": now, "real_name": req.RealName, } if passwd != "" { values["password"] = passwd } src := &dbmodel.TUser{} err = src.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if src.ID == 0 { return nil, errors.ErrRecordNotFound } reply.Origin = &pb_v1.CompanyUserUpdateRequest{ Id: req.Id, Cid: req.Cid, UserName: src.User, Email: src.Email, Phone: src.Phone, RealName: src.RealName, } user := dbmodel.TUser{} db := database.DB().Begin() err = user.Update(db, where, values) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { db.Rollback() return nil, errors.UserExist } db.Rollback() return nil, errors.DataBaseError } db.Commit() return reply, nil }