package service import ( "context" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "time" ) var ( JyInvoiceManager *InvoiceManager ) type InvoiceManager struct { Auth *TripartiteAuth Login bool //登录状态 OCRPass bool //活体检测状态是否通过 runPool chan bool //任务(每次只能进行一个开票任务) phoneCode chan string //短信验证码池 } func init() { JyInvoiceManager = createInvoiceManager() //_, err := gcron.Add(context.Background(), "", JyInvoiceManager.RunJob, "invoiceJob") //if err != nil { // panic(err) //} //go JyInvoiceManager.RunOneJob(context.Background()) //go JyInvoiceManager.Demo(context.Background()) //开票 //go JyInvoiceManager.RedDemo(context.Background())//红冲 } func createInvoiceManager() *InvoiceManager { return &InvoiceManager{ Auth: createTripartite(), Login: true, //默认已经登录 OCRPass: true, runPool: make(chan bool, 1), //开票只能单线程跑 phoneCode: make(chan string, 1), //手机验证码 } } func (im *InvoiceManager) ReleasePool() { <-im.runPool } func (im *InvoiceManager) MobileVerificationCode(code string) error { if code == "" { return gerror.New("验证码为空") } select { case <-time.After(time.Minute): return gerror.New("验证码接受超时") case im.phoneCode <- code: return nil } } func (im *InvoiceManager) MobileVerificationClear() error { select { case <-time.After(time.Minute): return gerror.New("清除验证码超时") case <-im.phoneCode: return nil } } // 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 } total, okNum, err := im.simpleMakeInvoice(ctx) if err != nil { g.Log().Errorf(ctx, "开蓝票任务异常 %v", err) } else { g.Log().Infof(ctx, "开蓝票任务完成 共%d个 完成%d个", total, okNum) } g.Log().Infof(ctx, "RunJob-开票任务完成") } // simpleMakeInvoice 简单开票 func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int64, err error) { var ( res gdb.Result ) total, okNum = -1, -1 //查询需要开票的数据 res, err = g.DB().Query(ctx, "SELECT * FROM invoice WHERE invoice_status=0 AND invoice_order_code is NULL") if err != nil { g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-查询待开票异常 %s", err) return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常") } g.Log().Infof(ctx, "RunJob-simpleMakeInvoice-本次共加载%d条开票记录", res.Len()) for i, m := range res.List() { select { case im.runPool <- true: case <-time.After(time.Minute * 5): g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票等待异常,结束此次任务") 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-simpleMakeInvoice-开票接口调用异常 %v", err) continue } } return }