acquirer_student_total_logic.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. return &types.Response{500, err.Error(), nil}, nil
  45. }
  46. timeNow := time.Now()
  47. start := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
  48. end := start.Add(24*60*60*time.Second - 1*time.Second)
  49. totalDay, err := l.svcCtx.DB.NewSession().SQL(sqlTotalByToDay, userId, start, end).Query().List()
  50. if err != nil {
  51. return &types.Response{500, err.Error(), nil}, nil
  52. }
  53. var target int64
  54. l.svcCtx.DB.Table("i2bill_mkt_part_time_user").Select("target").Where("user_id = ? and del_flag = 0", userId).Get(&target)
  55. var res = make(map[string]interface{})
  56. var invalide, untreated, valide int64
  57. for _, t := range totalByCheck {
  58. switch t["check_state"].(int64) {
  59. case 54, 56, 59:
  60. untreated += t["total"].(int64)
  61. case 57:
  62. valide += t["total"].(int64)
  63. case 58:
  64. invalide += t["total"].(int64)
  65. }
  66. }
  67. res["invalide"] = invalide
  68. res["untreated"] = untreated
  69. res["valide"] = valide
  70. res["today_total"] = totalDay[0]["total"]
  71. res["target"] = target
  72. return &types.Response{200, "", res}, nil
  73. }