operate_credit_balance_logic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package credit_balance
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  6. "github.com/zeromicro/go-zero/core/errorx"
  7. "wechat-api/ent/creditbalance"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type OperateCreditBalanceLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewOperateCreditBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OperateCreditBalanceLogic {
  18. return &OperateCreditBalanceLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *OperateCreditBalanceLogic) OperateCreditBalance(req *types.CreditBalanceOperateReq) (*types.BaseMsgResp, error) {
  24. if req.Number == nil || *req.Number == 0 {
  25. return nil, errorx.NewInvalidArgumentError("Number参数非法")
  26. }
  27. if req.UserId == nil {
  28. return nil, errorx.NewInvalidArgumentError("UserId参数非法")
  29. }
  30. creditBalance, err := l.svcCtx.DB.CreditBalance.Query().Where(creditbalance.UserID(*req.UserId)).Only(l.ctx)
  31. if err != nil {
  32. l.Logger.Errorf("query credit_balance error:%v\n", err)
  33. return nil, errorx.NewInvalidArgumentError(err.Error())
  34. }
  35. var ntype int
  36. if *req.Number > 0 {
  37. ntype = 2
  38. } else {
  39. ntype = 1
  40. }
  41. userInfo, err := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: *req.UserId})
  42. if err != nil {
  43. l.Logger.Errorf("query user error:%v\n", err)
  44. return nil, errorx.NewInvalidArgumentError("用户不存在")
  45. }
  46. tx, err := l.svcCtx.DB.Tx(context.Background())
  47. _, err = tx.CreditUsage.Create().
  48. SetUserID(*req.UserId).
  49. SetNumber(*req.Number).
  50. SetNtype(ntype).
  51. SetNid(0).
  52. SetTable("").
  53. SetReason(*req.Reason).
  54. SetOrganizationID(*userInfo.DepartmentId).
  55. Save(l.ctx)
  56. if err != nil {
  57. l.Logger.Errorf("create credit_usage error:%v\n", err)
  58. _ = tx.Rollback()
  59. return nil, errorx.NewInvalidArgumentError("创建用户积分明细失败,请重试")
  60. }
  61. if *req.Number > 0 {
  62. if creditBalance == nil {
  63. _, err = l.svcCtx.DB.CreditBalance.Create().
  64. SetUserID(*req.UserId).
  65. SetBalance(*req.Number).
  66. Save(l.ctx)
  67. if err != nil {
  68. l.Logger.Errorf("create credit_balance error:%v\n", err)
  69. _ = tx.Rollback()
  70. return nil, errorx.NewInvalidArgumentError("创建用户积分余额失败,请重试")
  71. }
  72. } else {
  73. _, err = l.svcCtx.DB.CreditBalance.Update().Where(creditbalance.UserID(*req.UserId)).SetBalance(creditBalance.Balance + *req.Number).Save(l.ctx)
  74. if err != nil {
  75. l.Logger.Errorf("update credit_balance error:%v\n", err)
  76. _ = tx.Rollback()
  77. return nil, errorx.NewInvalidArgumentError("修改用户积分余额失败,请重试.")
  78. }
  79. }
  80. } else {
  81. _, err := l.svcCtx.DB.CreditBalance.Update().Where(creditbalance.UserID(*req.UserId)).SetBalance(creditBalance.Balance + *req.Number).Save(l.ctx)
  82. if err != nil {
  83. l.Logger.Errorf("update credit_balance error:%v\n", err)
  84. _ = tx.Rollback()
  85. return nil, errorx.NewInvalidArgumentError("修改用户积分余额失败,请重试...")
  86. }
  87. }
  88. _ = tx.Commit()
  89. return &types.BaseMsgResp{
  90. Code: 0,
  91. Msg: errormsg.Success,
  92. }, nil
  93. }