admin_delete.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package admin
  2. import (
  3. "context"
  4. "gd_admin/apis"
  5. "gd_admin/common.in/storage"
  6. "gd_admin/common.in/utils"
  7. "gd_admin/errors"
  8. "gd_admin/impl/dbmodel"
  9. "fmt"
  10. "github.com/astaxie/beego/orm"
  11. "go.uber.org/zap"
  12. "time"
  13. )
  14. func AdminDelete(ctx context.Context, req *apis.AdminDeleteReq, reply *apis.AdminDeleteReply) error {
  15. if req.AdminId == 0 {
  16. return errors.ArgsError
  17. }
  18. delAccessTask := func(db orm.Ormer) error {
  19. p := dbmodel.TGdAdminRbacAccess{}
  20. // where
  21. filter := map[string]interface{}{
  22. "uid": req.AdminId,
  23. }
  24. _, err := p.Del(db, filter)
  25. if err != nil {
  26. l.Error("mysql",
  27. zap.String("sql", fmt.Sprintf("SELECT * FROM %s", p.TableName())),
  28. zap.String("fields", utils.MarshalJsonString(filter)),
  29. zap.String("error", err.Error()))
  30. return errors.DataBaseError
  31. }
  32. return nil
  33. }
  34. delUserTask := func(db orm.Ormer) error {
  35. _, err := orm.NewOrm().Raw("update t_gd_admin set status = ?, updated_at = ? where id = ?", 1, time.Now().Format("2006-01-02 15:04:05"), req.AdminId).Exec()
  36. if err != nil {
  37. return errors.DataBaseError
  38. }
  39. return nil
  40. }
  41. //处理事务
  42. tasks := []storage.DbaTasker{}
  43. tasks = append(tasks, storage.GenerateDbaTask(delAccessTask))
  44. tasks = append(tasks, storage.GenerateDbaTask(delUserTask))
  45. storage.ExecTrans(tasks...)
  46. return nil
  47. }