invoiceManager.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package service
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/database/gdb"
  5. "github.com/gogf/gf/v2/errors/gerror"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "time"
  8. )
  9. var (
  10. JyInvoiceManager *InvoiceManager
  11. )
  12. type InvoiceManager struct {
  13. Auth *TripartiteAuth
  14. Login bool //登录状态
  15. OCRPass bool //活体检测状态是否通过
  16. runPool chan bool //任务(每次只能进行一个开票任务)
  17. phoneCode chan string //短信验证码池
  18. }
  19. func init() {
  20. JyInvoiceManager = createInvoiceManager()
  21. //_, err := gcron.Add(context.Background(), "", JyInvoiceManager.RunJob, "invoiceJob")
  22. //if err != nil {
  23. // panic(err)
  24. //}
  25. //go JyInvoiceManager.RunOneJob(context.Background())
  26. //go JyInvoiceManager.Demo(context.Background()) //开票
  27. //go JyInvoiceManager.RedDemo(context.Background())//红冲
  28. }
  29. func createInvoiceManager() *InvoiceManager {
  30. return &InvoiceManager{
  31. Auth: createTripartite(),
  32. Login: true, //默认已经登录
  33. OCRPass: true,
  34. runPool: make(chan bool, 1), //开票只能单线程跑
  35. phoneCode: make(chan string, 1), //手机验证码
  36. }
  37. }
  38. func (im *InvoiceManager) ReleasePool() {
  39. <-im.runPool
  40. }
  41. func (im *InvoiceManager) MobileVerificationCode(code string) error {
  42. if code == "" {
  43. return gerror.New("验证码为空")
  44. }
  45. select {
  46. case <-time.After(time.Minute):
  47. return gerror.New("验证码接受超时")
  48. case im.phoneCode <- code:
  49. return nil
  50. }
  51. }
  52. func (im *InvoiceManager) MobileVerificationClear() error {
  53. select {
  54. case <-time.After(time.Minute):
  55. return gerror.New("清除验证码超时")
  56. case <-im.phoneCode:
  57. return nil
  58. }
  59. }
  60. // RunJob 开票定时任务
  61. func (im *InvoiceManager) RunJob(ctx context.Context) {
  62. if g.Cfg().MustGet(ctx, "invoiceJob.stop", false).Bool() {
  63. g.Log().Infof(ctx, "RunJob-开票程序任务已暂停,开启请删除 config.json > invoiceJob.stop")
  64. return
  65. }
  66. total, okNum, err := im.simpleMakeInvoice(ctx)
  67. if err != nil {
  68. g.Log().Errorf(ctx, "开蓝票任务异常 %v", err)
  69. } else {
  70. g.Log().Infof(ctx, "开蓝票任务完成 共%d个 完成%d个", total, okNum)
  71. }
  72. g.Log().Infof(ctx, "RunJob-开票任务完成")
  73. }
  74. // simpleMakeInvoice 简单开票
  75. func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int64, err error) {
  76. var (
  77. res gdb.Result
  78. )
  79. total, okNum = -1, -1
  80. //查询需要开票的数据
  81. res, err = g.DB().Query(ctx, "SELECT * FROM invoice WHERE invoice_status=0 AND invoice_order_code is NULL")
  82. if err != nil {
  83. g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-查询待开票异常 %s", err)
  84. return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
  85. }
  86. g.Log().Infof(ctx, "RunJob-simpleMakeInvoice-本次共加载%d条开票记录", res.Len())
  87. for i, m := range res.List() {
  88. select {
  89. case im.runPool <- true:
  90. case <-time.After(time.Minute * 5):
  91. g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票等待异常,结束此次任务")
  92. return
  93. }
  94. g.Dump(i, m)
  95. //isEnt := gconv.String(m["invoice_type"]) == "单位" // 个人
  96. //orderCode:=
  97. c := MakeInvoiceData{
  98. Type: "2",
  99. Gmfmc: "北京拓普丰联信息科技股份有限公司",
  100. Gmfnsrsbh: "91110105756025873C",
  101. Id: "123321",
  102. Gmfdz: "北京市朝阳区安定路5号院13号楼B座12层1201室",
  103. Lxdh: "010-58772571",
  104. Yhyywdmc: "郑州交通银行总行",
  105. Yhzh: "6320123123000121",
  106. InvoiceArr: []MakeInvoiceItems{{
  107. Xmmc: "0fccdac71c36a8552ba662e7a2f42726",
  108. WhStatus: 1,
  109. Je: "2",
  110. Sl: "1",
  111. }},
  112. }
  113. err := im.Auth.MakeSingleInvoice(c)
  114. if err != nil {
  115. im.ReleasePool()
  116. g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票接口调用异常 %v", err)
  117. continue
  118. }
  119. }
  120. return
  121. }