Răsfoiți Sursa

wip:临时提交

wangkaiyue 1 an în urmă
părinte
comite
22b5eef7b0

+ 2 - 2
internal/controller/callback.go

@@ -42,12 +42,12 @@ func CallBack(r *ghttp.Request) {
 			qData := gconv.Map(r.Get("data"))
 			switch qData["taskType"] {
 			case "2": //电子发票开具
-				service.JyInvoiceManager.ReleasePool() //可进行下次开票请求
+				<-service.JyInvoiceManager.RunPool //可进行下次开票请求
 				g.Log().Infof(ctx, "开具发票结束")
 			case "4": //活体认证
 			case "5": //验证电子税局登录授权
 			case "9": //红冲
-				service.JyInvoiceManager.ReleasePool() //可进行下次开票请求
+				<-service.JyInvoiceManager.RunPool //可进行下次开票请求
 				g.Log().Infof(ctx, "发票冲红结束")
 			default:
 				g.Log().Infof(ctx, "未知回调任务退出 %+v", qData)

+ 30 - 0
internal/service/invoiceCallback_red.go

@@ -1,15 +1,45 @@
 package service
 
 import (
+	"ElectronicInvoice/util"
+	"fmt"
+	"github.com/gogf/gf/v2/errors/gerror"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/net/ghttp"
 )
 
+// 红票信息
+type RedMsg struct {
+	InvoiceCode      string `json:"invoice_code"`
+	InvoiceNumber    string `json:"invoice_number"`
+	Url              string `json:"url"`
+	InvoiceSerialnum string `json:"invoice_serialnum"`
+	InvoiceStatus    string `json:"invoice_status"`
+	BillingTime      int64  `json:"billing_time"`
+}
+
 // InvoicingMakeRedCallBackLogic 开票回调逻辑
 func InvoicingMakeRedCallBackLogic(r *ghttp.Request) error {
 	tType := r.Get("type").Int()
 	switch tType {
 	case 0: //红冲成功
+		var (
+			pdfBase64 = r.Get("pdf").String()
+			num       = r.Get("num") //蓝票号
+
+			err  error
+			path string
+		)
+		//更新旧蓝票状态
+
+		//保存红票信息
+		path, err = util.SavePdfFile(r.Context(), fmt.Sprintf("RED_%s", num), pdfBase64)
+		if err != nil {
+			return gerror.Wrap(err, "保存红冲pdf文件失败")
+		}
+
+		g.Dump(path)
+
 	case 1: //失败
 	case 2: //授信额度等信息
 		g.Log().Info(r.Context(), "授信额度等信息")

+ 2 - 2
internal/service/invoiceDemo.go

@@ -59,7 +59,7 @@ start:
 	//for i := 0; i < 3; i++ {
 
 	select {
-	case im.runPool <- true:
+	case im.RunPool <- true:
 	case <-time.After(time.Minute):
 		g.Log().Errorf(ctx, "RunJob-开票等待异常,结束此次任务")
 		return
@@ -79,7 +79,7 @@ start:
 	}
 	err := im.Auth.MakeSingleInvoice(c)
 	if err != nil {
-		im.ReleasePool()
+		<-im.RunPool
 	}
 	if gerror.Is(err, consts.LoginOutErr) {
 		g.Log().Infof(ctx, "RunJob-身份过期,需要重新登录")

+ 15 - 149
internal/service/invoiceManager.go

@@ -1,14 +1,10 @@
 package service
 
 import (
-	"ElectronicInvoice/internal/consts"
 	"context"
-	"github.com/gogf/gf/v2/database/gdb"
 	"github.com/gogf/gf/v2/errors/gerror"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/os/gcron"
-	"github.com/gogf/gf/v2/util/gconv"
-	"strconv"
 	"time"
 )
 
@@ -21,7 +17,7 @@ type InvoiceManager struct {
 	jobRunning  bool        //开票是否运行中
 	StopRunning bool        //财务占用账号,需要暂停任务
 	Login       bool        //登录状态
-	runPool     chan bool   //任务(每次只能进行一个开票任务)
+	RunPool     chan bool   //任务(每次只能进行一个开票任务)
 	phoneCode   chan string //短信验证码池
 	ScanLogin   chan bool   //扫码登录
 }
@@ -55,16 +51,12 @@ func createInvoiceManager() *InvoiceManager {
 	return &InvoiceManager{
 		Auth:      createTripartite(),
 		Login:     true,                 //默认已经登录
-		runPool:   make(chan bool, 1),   //开票只能单线程跑
+		RunPool:   make(chan bool, 1),   //开票只能单线程跑
 		phoneCode: make(chan string, 1), //手机验证码
 		ScanLogin: make(chan bool, 1),   //扫码登录通知
 	}
 }
 
-func (im *InvoiceManager) ReleasePool() {
-	<-im.runPool
-}
-
 func (im *InvoiceManager) MobileVerificationCode(code string) error {
 	if code == "" {
 		return gerror.New("验证码为空")
@@ -113,158 +105,32 @@ func (im *InvoiceManager) RunJob(ctx context.Context) {
 	}
 
 	//TODO 普通蓝票任务
-	total, okNum, err := im.simpleMakeInvoice(ctx)
+	total, okNum, end, err := im.simpleMakeInvoice(ctx)
 	if err != nil {
-		if gerror.Is(err, consts.LoginOutErr) {
-			g.Log().Infof(ctx, "RunJob-任务中止-开蓝票任务身份过期,需要重新登录")
-			return
-		} else if gerror.Is(err, consts.WaitTimeOut) {
-			g.Log().Errorf(ctx, "RunJob-任务中止-开蓝票任务等待超时异常 %v", err)
-			return
-		}
 		g.Log().Errorf(ctx, "RunJob-开蓝票任务异常 %v", err)
-	} else {
-		g.Log().Infof(ctx, "RunJob-开蓝票任务完成 共%d个 完成%d个", total, okNum)
-	}
-	// TODO 蓝票自助开票(管理后台扫码开票)
-	total, okNum, err = im.selfMakeInvoice(ctx)
-	if err != nil {
-		if gerror.Is(err, consts.LoginOutErr) {
-			g.Log().Infof(ctx, "RunJob-任务中止-开自助蓝票任务身份过期,需要重新登录")
-			return
-		} else if gerror.Is(err, consts.WaitTimeOut) {
-			g.Log().Errorf(ctx, "RunJob-任务中止-开自助蓝票任务等待超时异常 %v", err)
+		if end {
 			return
 		}
-		g.Log().Errorf(ctx, "RunJob-开自助蓝票任务异常 %v", err)
-	} else {
-		g.Log().Infof(ctx, "RunJob-开自助蓝票任务完成 共%d个 完成%d个", total, okNum)
 	}
-	//TODO 红票任务
-
-}
+	g.Log().Infof(ctx, "RunJob-开蓝票任务完成 共%d个 完成%d个", total, okNum)
 
-// simpleMakeInvoice 简单开票
-func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int, err error) {
-	var (
-		res gdb.Result
-	)
-	//查询需要开票的数据
-	res, err = g.DB().Query(ctx, "SELECT a.*,b.pay_way,b.order_money,b.pay_money FROM invoice a INNER JOIN dataexport_order b ON a.order_code=b.order_code WHERE a.invoice_status=0 AND a.invoice_changed=0 AND  a.invoice_variety='普通发票(电子发票)' AND a.invoice_order_code is NULL AND a.create_time > ? ", consts.InvoiceStartTime.Unix())
+	// TODO 蓝票自助开票(管理后台扫码开票)
+	total, okNum, end, err = im.selfMakeInvoice(ctx)
 	if err != nil {
-		return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
-	}
-	g.Log().Infof(ctx, "simpleMakeInvoice-本次共加载%d条开票记录", res.Len())
-	total, okNum = res.Len(), 0
-	for _, m := range res.List() {
-		select {
-		case im.runPool <- true:
-		case <-time.After(time.Minute * 5):
-			err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
+		g.Log().Errorf(ctx, "RunJob-开自助蓝票任务异常 %v", err)
+		if end {
 			return
 		}
-		var (
-			orderCode = gconv.String(m["order_code"])
-			iType     = gconv.String(m["invoice_type"])
-			remark    = gconv.String(m["remark"])
-			prices    float64
-		)
-
-		//公对公转账 账单金额可以修改 开发票应取实付金额 pay_money
-		//微信支付宝支付 pay_money为订单金额减去微信or支付包红包
-		if gconv.String(m["pay_way"]) == "transferAccounts" {
-			prices = gconv.Float64(m["pay_money"]) / float64(100)
-		} else {
-			prices = gconv.Float64(m["order_money"]) / float64(100)
-		}
-
-		c := MakeInvoiceData{
-			Type:  "2",
-			Id:    orderCode,
-			Notes: remark,
-			Fhr:   g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
-			InvoiceArr: []MakeInvoiceItems{{
-				Xmmc:     g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
-				WhStatus: 1,                                                //开票项是否维护
-				Je:       strconv.FormatFloat(prices, 'f', -1, 64),         //金额
-				Sl:       "1",                                              //数量
-			}},
-		}
-		if iType == "单位" {
-			c.Gmfmc = gconv.String(m["company_name"])
-			c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
-		} else {
-			c.Gmfmc = iType
-		}
-
-		err = im.Auth.MakeSingleInvoice(c)
-		if err != nil {
-			im.ReleasePool()
-			if gerror.Is(err, consts.LoginOutErr) {
-				g.Log().Infof(ctx, "simpleMakeInvoice-身份过期,需要重新登录")
-				return
-			}
-			g.Log().Errorf(ctx, "simpleMakeInvoice-开票接口调用异常 %v", err)
-			continue
-		}
-		okNum++
 	}
-	return
-}
+	g.Log().Infof(ctx, "RunJob-开自助蓝票任务完成 共%d个 完成%d个", total, okNum)
 
-// selfMakeInvoice 自助开票
-func (im *InvoiceManager) selfMakeInvoice(ctx context.Context) (total, okNum int, err error) {
-	var (
-		res gdb.Result
-	)
-	res, err = g.DB().Query(ctx, "SELECT a.invoice_money,a.only_Identifying,a.invoice_type,a.company_name,a.taxpayer_identnum,a.remark FROM invoice a WHERE a.invoice_status=0 AND a.invoice_changed=0 AND  a.invoice_variety='电子普通发票' AND a.invoice_order_code is not NULL AND a.create_time > ? GROUP BY invoice_order_code", consts.InvoiceStartTime.Unix())
+	// TODO 开红票
+	total, okNum, end, err = im.makeRedInvoice(ctx)
 	if err != nil {
-		return -1, -1, gerror.Wrap(err, "selfMakeInvoice-查询待开票异常")
-	}
-	total, okNum = res.Len(), 0
-	for _, m := range res.List() {
-		select {
-		case im.runPool <- true:
-		case <-time.After(time.Minute * 5):
-			err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
+		g.Log().Errorf(ctx, "RunJob-开红票任务异常 %v", err)
+		if end {
 			return
 		}
-		var (
-			orderCode = gconv.String(m["only_Identifying"])
-			iType     = gconv.String(m["invoice_type"])
-			prices    = gconv.Float64(m["invoice_money"]) / float64(100)
-			remark    = gconv.String(m["remark"])
-		)
-		c := MakeInvoiceData{
-			Type:  "2",
-			Id:    orderCode,
-			Notes: remark,
-			Fhr:   g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
-			InvoiceArr: []MakeInvoiceItems{{
-				Xmmc:     g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
-				WhStatus: 1,                                                //开票项是否维护
-				Je:       strconv.FormatFloat(prices, 'f', -1, 64),         //金额
-				Sl:       "1",                                              //数量
-			}},
-		}
-		if iType == "单位" {
-			c.Gmfmc = gconv.String(m["company_name"])
-			c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
-		} else {
-			c.Gmfmc = iType
-		}
-
-		err = im.Auth.MakeSingleInvoice(c)
-		if err != nil {
-			im.ReleasePool()
-			if gerror.Is(err, consts.LoginOutErr) {
-				g.Log().Infof(ctx, "selfMakeInvoice-身份过期,需要重新登录")
-				return
-			}
-			g.Log().Errorf(ctx, "selfMakeInvoice-开票接口调用异常 %v", err)
-			continue
-		}
-		okNum++
 	}
-	return
+	g.Log().Infof(ctx, "RunJob-开红票任务完成 共%d个 完成%d个", total, okNum)
 }