// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package operation_log 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" "gorm.io/gorm" "time" "xingjia-management-gateway/apis" "xingjia-management-gateway/consts" "xingjia-management-gateway/errors" dbmodel "xingjia-management-gateway/model" ) func checkOperationLogAddParam(req *apis.OperationLogAddRequest) error { switch { case req.Uid == 0: return status.Error(10003, "操作日志用户名不能为空") } return nil } func checkAdmin(selfId int64) error { p := &dbmodel.TUser{} where := map[string]interface{}{ "id": selfId, } err := p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return errors.DataBaseError } if p.ID == 0 { return errors.ErrRecordNotFound } if p.UserType != consts.UserTypeSuper { return errors.PermissionError } return nil } func OperationLogAddHandle(req *apis.OperationLogAddRequest, db *gorm.DB) error { now := time.Now() p := &dbmodel.TOperationLog{ Uid: req.Uid, User: req.User, OriginContent: req.OriginContent, TargetContent: req.TargetContent, OperationTime: req.OperationTime, Operation: req.Operation, CreatedAt: now, UpdatedAt: now, Module: req.Module, } err := p.Insert(db) if err != nil { return errors.DataBaseError } return nil } // func OperationLogAdd(ctx context.Context, req *apis.OperationLogAddRequest) (reply *apis.OperationLogAddReply, err error) { reply = &apis.OperationLogAddReply{} // 捕获各个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 err = checkOperationLogAddParam(req); err != nil { return nil, err } err = OperationLogAddHandle(req, database.DB()) return reply, err }