enroll_logic.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package acquirer
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  8. "git.i2edu.net/i2/i2-bill-api/internal/types"
  9. "git.i2edu.net/i2/i2-bill-api/model"
  10. "git.i2edu.net/i2/go-zero/core/logx"
  11. "git.i2edu.net/i2/go-zero/core/stores/sqlc"
  12. )
  13. type EnrollLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. UserId int64
  18. SessionKey string
  19. }
  20. func NewEnrollLogic(ctx context.Context, svcCtx *svc.ServiceContext) EnrollLogic {
  21. return EnrollLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *EnrollLogic) Enroll(req types.EnrollRequest) (*types.Response, error) {
  28. var stu model.I2billAcquirerStudent
  29. var qrScence struct {
  30. UserId int64 `json:"userId"`
  31. Timestamp time.Time `json:"timestamp"`
  32. Type string `json:"type"`
  33. }
  34. err := l.svcCtx.Wechat.GetQrParams(svc.QrcodeJzPrefix, req.Scene, &qrScence)
  35. if err != nil {
  36. logx.Error(err.Error())
  37. return &types.Response{Code: 500, Msg: err.Error(), Data: nil}, nil
  38. }
  39. err = l.svcCtx.SqlConn.QueryRowPartial(&stu, fmt.Sprintf("select %s from i2bill_acquirer_student where `user_id`=? and `stu_phone`=? limit 1", model.I2billAcquirerStudentRows), qrScence.UserId, req.ContactPhone)
  40. if err != sqlc.ErrNotFound && err != nil {
  41. logx.Error(err.Error())
  42. return &types.Response{Code: 500, Msg: err.Error(), Data: nil}, nil
  43. } else if stu.Id > 0 {
  44. return &types.Response{Code: 500, Msg: "已录入成功, 请勿重复操作", Data: nil}, nil
  45. } else {
  46. stu.UserId = sql.NullInt64{Int64: qrScence.UserId, Valid: true}
  47. stu.StuPhone = sql.NullString{String: req.ContactPhone, Valid: true}
  48. _, err := l.svcCtx.I2billAcquirerStudentModel.Insert(stu)
  49. if err != nil {
  50. logx.Error(err.Error())
  51. return &types.Response{Code: 500, Msg: err.Error(), Data: nil}, nil
  52. }
  53. }
  54. return &types.Response{}, nil
  55. }