123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- // 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
- }
|