acquirer_student_total_logic.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package logic
  2. import (
  3. "context"
  4. "time"
  5. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  6. "git.i2edu.net/i2/i2-bill-api/internal/types"
  7. "git.i2edu.net/i2/go-zero/core/logx"
  8. )
  9. type AcquirerStudentTotalLogic struct {
  10. logx.Logger
  11. ctx context.Context
  12. svcCtx *svc.ServiceContext
  13. }
  14. func NewAcquirerStudentTotalLogic(ctx context.Context, svcCtx *svc.ServiceContext) AcquirerStudentTotalLogic {
  15. return AcquirerStudentTotalLogic{
  16. Logger: logx.WithContext(ctx),
  17. ctx: ctx,
  18. svcCtx: svcCtx,
  19. }
  20. }
  21. func (l *AcquirerStudentTotalLogic) AcquirerStudentTotal() (*types.Response, error) {
  22. // todo: add your logic here and delete this line
  23. userId := l.svcCtx.GetUserIdByJwt(l.ctx)
  24. sqlTotalByCheck := `
  25. SELECT
  26. count(*) total ,check_state
  27. FROM
  28. i2bill_mkt_part_time_user
  29. WHERE
  30. del_flag = 0
  31. and user_id = ?
  32. GROUP BY check_state`
  33. sqlTotalByToDay := `
  34. SELECT
  35. count(*) total
  36. FROM
  37. i2bill_mkt_part_time_user
  38. WHERE
  39. del_flag = 0
  40. and user_id = ?
  41. and create_time between ? and ?`
  42. totalByCheck, err := l.svcCtx.DB.NewSession().SQL(sqlTotalByCheck, userId).Query().List()
  43. if err != nil {
  44. logx.Error(err.Error())
  45. return &types.Response{500, err.Error(), nil}, nil
  46. }
  47. timeNow := time.Now()
  48. start := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
  49. end := start.Add(24*60*60*time.Second - 1*time.Second)
  50. totalDay, err := l.svcCtx.DB.NewSession().SQL(sqlTotalByToDay, userId, start, end).Query().List()
  51. if err != nil {
  52. logx.Error(err.Error())
  53. return &types.Response{500, err.Error(), nil}, nil
  54. }
  55. var target int64
  56. l.svcCtx.DB.Table("i2bill_mkt_part_time_user").Select("target").Where("user_id = ? and del_flag = 0", userId).Get(&target)
  57. var res = make(map[string]interface{})
  58. var invalide, untreated, valide int64
  59. for _, t := range totalByCheck {
  60. switch t["check_state"].(int64) {
  61. case 54, 56, 59:
  62. untreated += t["total"].(int64)
  63. case 57:
  64. valide += t["total"].(int64)
  65. case 58:
  66. invalide += t["total"].(int64)
  67. }
  68. }
  69. res["invalide"] = invalide
  70. res["untreated"] = untreated
  71. res["valide"] = valide
  72. res["today_total"] = totalDay[0]["total"]
  73. res["target"] = target
  74. return &types.Response{200, "", res}, nil
  75. }