// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package application 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" "property-system/errors" "property-system/impl/v1/permission" dbmodel "property-system/model" pb_v1 "property-system/pb/v1" "strings" ) func checkApplicationOrderApproveParam(req *pb_v1.ApplicationOrderApproveRequest) error { switch { case req.OrderId < 1: return status.Error(10003, "id不能为空") } return nil } func UpdateGardenPermissions(gardenId int64, permissions []*pb_v1.SystemPermissionNotTreeItem, db *gorm.DB) error { p := dbmodel.TGardenApproved{} where := map[string]interface{}{ "id": gardenId, } err := p.Find(db, where) if err != nil && err != gorm.ErrRecordNotFound { return errors.DataBaseError } if p.ID == 0 { return errors.ErrRecordNotFound } origin := strings.Split(p.PermissionCodes, ";") originM := map[string]bool{} for _, v := range origin { originM[v] = true } for _, v := range permissions { if originM[v.Code] { continue } origin = append(origin, v.Code) } values := map[string]interface{}{ "permission_codes": strings.Join(origin, ";"), } err = p.Update(db, where, values) permission.GardenPermissionTreeClearRedis(gardenId) if err != nil { return errors.DataBaseError } return nil } func ApproveHandle(req *pb_v1.ApplicationOrderApproveRequest, db *gorm.DB) error { p := dbmodel.TApplicationOrder{} where := map[string]interface{}{ "id": req.OrderId, } err := p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return errors.DataBaseError } if p.ID == 0 { return errors.ErrRecordNotFound } if p.Status != 1 { return status.Error(10003, "状态错误") } status := 2 if !req.Status { status = 3 } values := map[string]interface{}{ "status": status, "feedback": req.Feedback, } p.ID = 0 err = p.Update(db, where, values) if err != nil { return errors.DataBaseError } application := pb_v1.ApplicationInfo{} json.Unmarshal([]byte(p.ApplicationContent), &application) return UpdateGardenPermissions(p.GardenId, application.Permissions, db) } // func ApplicationOrderApprove(ctx context.Context, req *pb_v1.ApplicationOrderApproveRequest) (reply *pb_v1.ApplicationOrderApproveReply, err error) { reply = &pb_v1.ApplicationOrderApproveReply{} // 捕获各个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 = checkApplicationOrderApproveParam(req) if err != nil { return nil, err } db := database.DB().Begin() if err = ApproveHandle(req, db); err != nil { db.Rollback() return nil, err } db.Commit() return reply, nil }