Browse Source

wip:测试流程提交

wangkaiyue 1 year ago
parent
commit
c9c4da6a7d

+ 2 - 0
config.yaml

@@ -37,6 +37,8 @@ company:
   taxNum: "91110105MA020L9F6R"
   tel: "13733157437"
   password: "13733157437Ph"
+  hfr: "贺鹏飞" #发票复核人
+  taxCode: "0fccdac71c36a8552ba662e7a2f42726" #开票项
 
 # 发票三方系统配置-接口回调
 callBack:

+ 6 - 0
internal/controller/callback.go

@@ -28,6 +28,12 @@ func CallBack(r *ghttp.Request) {
 			return service.InvoicingMakeRedCallBackLogic(r)
 		case "livenessDetection":
 			g.Log().Info(r.Context(), "活体认证:%s")
+		case "login": //扫码登录回调
+			var flag bool
+			if r.Get("code").String() == "200" {
+				flag = true
+			}
+			service.JyInvoiceManager.ScanLogin <- flag
 		default:
 			g.Log().Infof(r.Context(), "未设置消息回调:%s", callType)
 		}

+ 56 - 22
internal/service/invoiceManager.go

@@ -1,10 +1,13 @@
 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/util/gconv"
+	"strconv"
 	"time"
 )
 
@@ -18,6 +21,7 @@ type InvoiceManager struct {
 	OCRPass   bool        //活体检测状态是否通过
 	runPool   chan bool   //任务(每次只能进行一个开票任务)
 	phoneCode chan string //短信验证码池
+	ScanLogin chan bool   //扫码登录
 }
 
 func init() {
@@ -38,6 +42,7 @@ func createInvoiceManager() *InvoiceManager {
 		OCRPass:   true,
 		runPool:   make(chan bool, 1),   //开票只能单线程跑
 		phoneCode: make(chan string, 1), //手机验证码
+		ScanLogin: make(chan bool, 1),   //扫码登录通知
 	}
 }
 
@@ -51,7 +56,7 @@ func (im *InvoiceManager) MobileVerificationCode(code string) error {
 	}
 	select {
 	case <-time.After(time.Minute):
-		return gerror.New("验证码接超时")
+		return gerror.New("验证码接超时")
 	case im.phoneCode <- code:
 		return nil
 	}
@@ -73,6 +78,15 @@ func (im *InvoiceManager) RunJob(ctx context.Context) {
 		return
 	}
 
+	if im.Login {
+		if err := im.Auth.Login(); err != nil {
+			g.Log().Errorf(ctx, "模拟登录异常 %v", err)
+			return
+		} else {
+			g.Log().Infof(ctx, "登录成功")
+		}
+	}
+
 	total, okNum, err := im.simpleMakeInvoice(ctx)
 	if err != nil {
 		g.Log().Errorf(ctx, "开蓝票任务异常 %v", err)
@@ -84,51 +98,71 @@ func (im *InvoiceManager) RunJob(ctx context.Context) {
 }
 
 // simpleMakeInvoice 简单开票
-func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int64, err error) {
+func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int, 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")
+	res, err = g.DB().Query(ctx, "SELECT * FROM invoice WHERE invoice_status=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-查询待开票异常")
 	}
 	g.Log().Infof(ctx, "RunJob-simpleMakeInvoice-本次共加载%d条开票记录", res.Len())
-
-	for i, m := range res.List() {
+	total, okNum = res.Len(), 0
+	for _, 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:=
+		var (
+			orderCode = gconv.String(m["order_code"])
+			phone     = gconv.String(m["phone_num"])
+			iType     = gconv.String(m["invoice_type"])
+			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",
-			Gmfmc:     "北京拓普丰联信息科技股份有限公司",
-			Gmfnsrsbh: "91110105756025873C",
-			Id:        "123321",
-			Gmfdz:     "北京市朝阳区安定路5号院13号楼B座12层1201室",
-			Lxdh:      "010-58772571",
-			Yhyywdmc:  "郑州交通银行总行",
-			Yhzh:      "6320123123000121",
+			Type: "2",
+			Id:   orderCode,
+			Lxdh: phone,
+			Fhr:  g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
 			InvoiceArr: []MakeInvoiceItems{{
-				Xmmc:     "0fccdac71c36a8552ba662e7a2f42726",
-				WhStatus: 1,
-				Je:       "2",
-				Sl:       "1",
+				Xmmc:     g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
+				WhStatus: 1,                                                //开票项是否维护
+				Je:       strconv.FormatFloat(prices, 'f', -1, 64),         //金额
+				Sl:       "1",                                              //数量
 			}},
 		}
-		err := im.Auth.MakeSingleInvoice(c)
+		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, "RunJob-simpleMakeInvoice-身份过期,需要重新登录")
+				return
+			}
 			g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票接口调用异常 %v", err)
 			continue
 		}
+		okNum++
 	}
 	return
 }

+ 0 - 1
internal/service/tripartite/login.go

@@ -51,7 +51,6 @@ func Login() error {
 			return gerror.Wrap(err, "发送企业微信登录消息异常")
 		}
 	} else {
-		return nil
 		//长期授权登录
 		if err := LoginAfterKeepOnline(); err != nil {
 			return gerror.Wrap(err, "短信验证失败")

+ 1 - 0
internal/service/tripartiteInvoice.go

@@ -24,6 +24,7 @@ type (
 		Lxdh       string             `json:"lxdh"`      //购买方联系方式
 		Yhyywdmc   string             `json:"yhyywdmc"`  //购买方开户行
 		Yhzh       string             `json:"yhzh"`      //购买方银行账号
+		Fhr        string             `json:"fhr"`       //复核人
 		Notes      string             `json:"notes"`     //发票备注
 		InvoiceArr []MakeInvoiceItems `json:"invoiceArr"`
 	}

+ 25 - 19
internal/service/tripartiteLogin.go

@@ -44,18 +44,32 @@ func (t *TripartiteAuth) Login() error {
 		return gerror.Wrap(err, "授权登录异常")
 	}
 
-	if loginType == 1 { //扫码登录
-		if err := util.SendQrImage2ChatBot(d.Ewm); err != nil {
-			return gerror.Wrap(err, "发送企业微信登录消息异常")
+	if loginType == 0 { //短信登录
+		//TODO 发送消息 取填写验证码
+		g.Log().Infof(ctx, "等待接收登录验证码")
+		select {
+		case <-time.After(time.Minute * 5):
+			g.Log().Infof(ctx, "已接接收登录验证码接收超时")
+			return consts.AuthTimeOut
+		case yzm := <-JyInvoiceManager.phoneCode:
+			if err := t.LoginAfterKeepOnline(yzm); err != nil {
+				return gerror.Wrap(err, "短信验证失败")
+			}
 		}
+	} else {
 		//ToDo be developed
 		// 等待扫完完成回调 默认扫码成功
-		time.Sleep(time.Minute * 5)
-	} else {
-		//TODO 发送消息 取填写验证码
-		//长期授权登录
-		if err := t.LoginAfterKeepOnline(); err != nil {
-			return gerror.Wrap(err, "短信验证失败")
+		if err := util.SendQrImage2ChatBot(d.Ewm); err != nil {
+			return gerror.Wrap(err, "发送企业微信登录消息异常")
+		}
+		select {
+		case <-time.After(time.Minute * 5):
+			g.Log().Infof(ctx, "已接接收登录验证码接收超时")
+			return consts.AuthTimeOut
+		case scanOk := <-JyInvoiceManager.ScanLogin:
+			if !scanOk {
+				return gerror.Wrap(err, "扫码登录验证失败")
+			}
 		}
 	}
 	JyInvoiceManager.Login = true
@@ -63,18 +77,10 @@ func (t *TripartiteAuth) Login() error {
 }
 
 // LoginAfterKeepOnline 长期登录授权接口
-func (t *TripartiteAuth) LoginAfterKeepOnline() error {
+func (t *TripartiteAuth) LoginAfterKeepOnline(yzm string) error {
 	var (
 		ctx = context.Background()
-		yzm string
 	)
-	g.Log().Infof(ctx, "等待接收登录验证码")
-	select {
-	case <-time.After(time.Minute * 5):
-		g.Log().Infof(ctx, "已接接收登录验证码接收超时")
-		return consts.AuthTimeOut
-	case yzm = <-JyInvoiceManager.phoneCode:
-	}
 
 	g.Log().Infof(ctx, "已接接收登录验证码 %s", yzm)
 	err := CommonDoPost("/index_index/onLine",
@@ -82,7 +88,7 @@ func (t *TripartiteAuth) LoginAfterKeepOnline() error {
 		g.MapStrAny{
 			"taxNum": g.Cfg().MustGet(ctx, "company.taxNum"),
 			"tel":    g.Cfg().MustGet(ctx, "company.tel"),
-			"yzm":    yzm, //手机短信验证码怎么获取??
+			"yzm":    yzm,
 		},
 		nil)
 	if err != nil {