瀏覽代碼

wip:测试流程提交

wangkaiyue 1 年之前
父節點
當前提交
9dc55f813a
共有 4 個文件被更改,包括 49 次插入13 次删除
  1. 1 1
      config.yaml
  2. 6 0
      internal/controller/control.go
  3. 30 12
      internal/service/invoiceManager.go
  4. 12 0
      main.go

+ 1 - 1
config.yaml

@@ -52,7 +52,7 @@ invoiceJob:
   cron: "# */30 9-18 * * *"
 
 # 登录相关配置
-loginType: 0 # 税务系统登录方式【0 短信登录 1 扫码登录】
+loginType: 1 # 税务系统登录方式【0 短信登录 1 扫码登录】
 qwxRobotUrl: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e7792e7a-159d-4419-b1ed-27ea19b6ea54" #扫码登录企业微信消息机器人接口
 
 pdfFilePathRootDir: "./out"

+ 6 - 0
internal/controller/control.go

@@ -19,6 +19,12 @@ func Control(r *ghttp.Request) {
 			}
 		case "run": //控制程序暂停
 			g.Log().Infof(r.Context(), "runing")
+			switch r.Get("code").String() {
+			case "start":
+				service.JyInvoiceManager.StopRunning = false
+			case "stop":
+				service.JyInvoiceManager.StopRunning = true
+			}
 		default:
 			return gerror.New("未知操作")
 		}

+ 30 - 12
internal/service/invoiceManager.go

@@ -16,12 +16,14 @@ var (
 )
 
 type InvoiceManager struct {
-	Auth      *TripartiteAuth
-	Login     bool        //登录状态
-	OCRPass   bool        //活体检测状态是否通过
-	runPool   chan bool   //任务(每次只能进行一个开票任务)
-	phoneCode chan string //短信验证码池
-	ScanLogin chan bool   //扫码登录
+	Auth        *TripartiteAuth
+	jobRunning  bool        //开票是否运行中
+	StopRunning bool        //财务占用账号,需要暂停任务
+	Login       bool        //登录状态
+	OCRPass     bool        //活体检测状态是否通过
+	runPool     chan bool   //任务(每次只能进行一个开票任务)
+	phoneCode   chan string //短信验证码池
+	ScanLogin   chan bool   //扫码登录
 }
 
 func init() {
@@ -30,7 +32,7 @@ func init() {
 	//if err != nil {
 	//	panic(err)
 	//}
-	//go JyInvoiceManager.RunOneJob(context.Background())
+	//go JyInvoiceManager.RunOneJob(context.Background())//流程
 	//go JyInvoiceManager.Demo(context.Background()) //开票
 	//go JyInvoiceManager.RedDemo(context.Background())//红冲
 }
@@ -73,6 +75,16 @@ func (im *InvoiceManager) MobileVerificationClear() error {
 
 // RunJob 开票定时任务
 func (im *InvoiceManager) RunJob(ctx context.Context) {
+	if im.jobRunning || im.StopRunning {
+		g.Log().Infof(ctx, "RunJob-程序本次任务中断 jobRunning:%v StopRunning:%v", im.jobRunning, im.StopRunning)
+		return
+	}
+	im.jobRunning = true
+	defer func() {
+		im.jobRunning = false
+		g.Log().Infof(ctx, "RunJob-开票任务完成")
+	}()
+
 	if g.Cfg().MustGet(ctx, "invoiceJob.stop", false).Bool() {
 		g.Log().Infof(ctx, "RunJob-开票程序任务已暂停,开启请删除 config.json > invoiceJob.stop")
 		return
@@ -87,14 +99,20 @@ func (im *InvoiceManager) RunJob(ctx context.Context) {
 		}
 	}
 
+	//TODO 普通蓝票任务
 	total, okNum, err := im.simpleMakeInvoice(ctx)
 	if err != nil {
-		g.Log().Errorf(ctx, "开蓝票任务异常 %v", err)
+		if gerror.Is(err, consts.LoginOutErr) {
+			g.Log().Infof(ctx, "RunJob-任务中止-身份过期,需要重新登录")
+			return
+		}
+		g.Log().Errorf(ctx, "RunJob-开蓝票任务异常 %v", err)
 	} else {
-		g.Log().Infof(ctx, "开蓝票任务完成 共%d个 完成%d个", total, okNum)
+		g.Log().Infof(ctx, "RunJob-开蓝票任务完成 共%d个 完成%d个", total, okNum)
 	}
 
-	g.Log().Infof(ctx, "RunJob-开票任务完成")
+	//TODO 红票任务
+
 }
 
 // simpleMakeInvoice 简单开票
@@ -104,7 +122,7 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 	)
 
 	//查询需要开票的数据
-	res, err = g.DB().Query(ctx, "SELECT * FROM invoice WHERE invoice_status=0 AND  invoice_variety='普通发票(电子发票)' AND invoice_order_code is NULL  ")
+	res, err = g.DB().Query(ctx, "SELECT * FROM invoice WHERE invoice_status=0 AND invoice_changed=0 AND  invoice_variety='普通发票(电子发票)' AND invoice_order_code is NULL  ")
 	if err != nil {
 		g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-查询待开票异常 %s", err)
 		return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
@@ -115,7 +133,7 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 		select {
 		case im.runPool <- true:
 		case <-time.After(time.Minute * 5):
-			g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票等待异常,结束此次任务")
+			g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票等待超时,结束此次任务")
 			return
 		}
 		var (

+ 12 - 0
main.go

@@ -2,11 +2,23 @@ package main
 
 import (
 	"ElectronicInvoice/internal/cmd"
+	"context"
 	_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
 	_ "github.com/gogf/gf/contrib/nosql/redis/v2"
+	"github.com/gogf/gf/v2/os/gcron"
 	"github.com/gogf/gf/v2/os/gctx"
+	"time"
 )
 
 func main() {
+	go func() {
+		_, err := gcron.Add(context.Background(), "", func(ctx context.Context) {
+			time.Sleep(time.Second * 5)
+		}, "invoiceJob")
+		if err != nil {
+			panic(err)
+		}
+	}()
+
 	cmd.Main.Run(gctx.GetInitCtx())
 }