package service import ( "context" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gcron" "time" ) var ( jyInvoiceManager *InvoiceManager ) type InvoiceManager struct { Auth *TripartiteAuth runPool chan bool } func init() { jyInvoiceManager = createInvoiceManager() _, err := gcron.Add(context.Background(), "", jyInvoiceManager.RunJob, "invoiceJob") if err != nil { panic(err) } } func createInvoiceManager() *InvoiceManager { return &InvoiceManager{ Auth: createTripartite(), runPool: make(chan bool, 1), } } func (im *InvoiceManager) ReleasePool() { <-im.runPool } // RunJob 开票定时任务 func (im *InvoiceManager) RunJob(ctx context.Context) { if g.Cfg().MustGet(ctx, "invoiceJob.stop", false).Bool() { g.Log().Infof(ctx, "RunJob-开票程序任务已暂停,开启请删除 config.json > invoiceJob.stop") return } //查询需要开票的数据 res, err := g.DB().Query(ctx, "SELECT * FROM invoice WHERE invoice_status=0 AND invoice_variety='电子普通发票'") if err != nil { g.Log().Errorf(ctx, "RunJob-查询待开票异常 %s", err) } g.Log().Infof(ctx, "RunJob-本次共加载%d条开票记录", res.Len()) for i, m := range res.List() { select { case im.runPool <- true: case <-time.After(time.Minute): g.Log().Errorf(ctx, "RunJob-开票等待异常,结束此次任务 %s", err) return } g.Dump(i, m) //isEnt := gconv.String(m["invoice_type"]) == "单位" // 个人 //orderCode:= c := MakeInvoiceData{ Type: "2", Gmfmc: "北京拓普丰联信息科技股份有限公司", Gmfnsrsbh: "91110105756025873C", Id: "123321", Gmfdz: "北京市朝阳区安定路5号院13号楼B座12层1201室", Lxdh: "010-58772571", Yhyywdmc: "郑州交通银行总行", Yhzh: "6320123123000121", InvoiceArr: []MakeInvoiceItems{{ Xmmc: "0fccdac71c36a8552ba662e7a2f42726", WhStatus: 1, Je: "2", Sl: "1", }}, } err := im.Auth.MakeSingleInvoice(c) if err != nil { im.ReleasePool() g.Log().Errorf(ctx, "RunJob-开票接口调用异常 %v", err) continue } } g.Log().Infof(ctx, "RunJob-开票任务完成") }