runOnce.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package service
  2. import (
  3. "ElectronicInvoice/internal/consts"
  4. "context"
  5. "github.com/gogf/gf/v2/errors/gerror"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "time"
  8. )
  9. type redBatch struct {
  10. Date string `json:"date"`
  11. Code string `json:"code"`
  12. }
  13. func (im *InvoiceManager) RunRedBatchOnce(ctx context.Context) {
  14. var (
  15. arr []*redBatch
  16. okNum int
  17. )
  18. err := g.Cfg("redBatchTmp").MustGet(ctx, "list").Struct(&arr)
  19. if err != nil {
  20. g.Log().Errorf(ctx, "RunJob-RunRedBatchOnce 读取红冲列表异常")
  21. }
  22. for _, data := range arr {
  23. select {
  24. case im.RunPool <- true:
  25. case <-time.After(time.Minute * 5):
  26. err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
  27. return
  28. }
  29. err := im.Auth.MakeSingleRedInvoice(MakeRedInvoiceData{
  30. Num: data.Code,
  31. Date: data.Date,
  32. })
  33. if err != nil {
  34. <-im.RunPool
  35. if gerror.Is(err, consts.LoginOutErr) {
  36. g.Log().Errorf(ctx, "RunJob-RunRedBatchOnce-身份过期,需要重新登录")
  37. return
  38. }
  39. } else {
  40. okNum++
  41. }
  42. }
  43. g.Log().Infof(ctx, "RunJob-RunRedBatchOnce 共%d个 完成%d个", len(arr), okNum)
  44. }