123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- // 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-system/consts"
- "property-system/errors"
- "property-system/impl/v1/permission"
- dbmodel "property-system/model"
- pb_v1 "property-system/pb/v1"
- "strings"
- "time"
- )
- func checkUserUpdateParam(req *pb_v1.UserUpdateRequest) error {
- switch {
- case req.Phone == "":
- return status.Error(10003, "联系人电话不能为空")
- case req.UserName == "":
- return status.Error(10003, "账户名不能为空")
- case req.GardenId < 1 || req.GardenName == "":
- 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不能为空")
- case !req.ByCompany && req.Uid < 1:
- return status.Error(10003, "当前uid不能为空")
- case req.BasePermission == 0:
- return status.Error(10003, "业务权限不能为空")
- case req.GroupId == 0:
- return status.Error(10003, "管理后台权限组不能为空")
- }
- return nil
- }
- func checkPersonUpdateParam(req *pb_v1.UserUpdateRequest) 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.Id < 1:
- return status.Error(10003, "用户id不能为空")
- }
- return nil
- }
- func PersonUpdate(ctx context.Context, req *pb_v1.UserUpdateRequest) (reply *pb_v1.UserUpdateReply, err error) {
- reply = &pb_v1.UserUpdateReply{}
- // 捕获各个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 = checkPersonUpdateParam(req)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- where := map[string]interface{}{
- "id": req.Id,
- }
- values := map[string]interface{}{
- //"email": req.Email,
- //"user": req.UserName,
- //"phone": req.Phone,
- "updated_at": now,
- //"real_name": req.RealName,
- }
- if req.UserName != "" {
- //values["user"] = req.UserName
- }
- if req.RealName != "" {
- //values["real_name"] = req.RealName
- }
- if req.Phone != "" {
- values["phone"] = req.Phone
- }
- if req.Email != "" {
- values["email"] = req.Email
- }
- if req.Gender > 0 {
- values["gender"] = req.Gender
- }
- 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
- }
- if req.Uid == src.ID && !req.ByCompany {
- if req.GroupId != src.GroupId || req.BasePermission != src.BasePermission {
- return nil, errors.UserSelfError
- }
- }
- reply.Origin = &pb_v1.UserUpdateRequest{
- Id: req.Id,
- Cid: src.Cid,
- GardenId: src.GardenId,
- GroupId: src.GroupId,
- UserName: src.User,
- Email: src.Email,
- Phone: src.Phone,
- RealName: src.RealName,
- DepartmentId: src.DepartmentId,
- }
- user := dbmodel.TUser{}
- err = user.Update(database.DB(), where, values)
- if err != nil {
- if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
- return nil, errors.UserExist
- }
- return nil, errors.DataBaseError
- }
- return reply, nil
- }
- //
- func UserUpdate(ctx context.Context, req *pb_v1.UserUpdateRequest) (reply *pb_v1.UserUpdateReply, err error) {
- if req.Person {
- return PersonUpdate(ctx, req)
- }
- reply = &pb_v1.UserUpdateReply{}
- // 捕获各个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 = checkUserUpdateParam(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{}{
- "garden_id": req.GardenId,
- "email": req.Email,
- "user": req.UserName,
- "phone": req.Phone,
- "updated_at": now,
- "garden_name": req.GardenName,
- "real_name": req.RealName,
- "department_id": req.DepartmentId,
- "gender": req.Gender,
- }
- if req.GroupId > 0 {
- values["group_id"] = req.GroupId
- }
- if req.BasePermission > 0 {
- values["base_permission"] = req.BasePermission
- }
- 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
- }
- if req.Uid == src.ID && !req.ByCompany {
- if req.GroupId != src.GroupId || req.BasePermission != src.BasePermission {
- return nil, errors.UserSelfError
- }
- }
- reply.Origin = &pb_v1.UserUpdateRequest{
- Id: req.Id,
- Cid: req.Cid,
- GardenId: src.GardenId,
- GroupId: src.GroupId,
- UserName: src.User,
- Email: src.Email,
- Phone: src.Phone,
- RealName: src.RealName,
- DepartmentId: src.DepartmentId,
- }
- 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
- }
- if src.GroupId != req.GroupId {
- err = permission.PermissionChanged([]dbmodel.TUser{{ID: src.ID}})
- if err != nil {
- db.Rollback()
- return nil, err
- }
- }
- db.Commit()
- return reply, nil
- }
|