|
@@ -1,10 +1,13 @@
|
|
package service
|
|
package service
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "ElectronicInvoice/internal/consts"
|
|
"context"
|
|
"context"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
|
+ "strconv"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -18,6 +21,7 @@ type InvoiceManager struct {
|
|
OCRPass bool //活体检测状态是否通过
|
|
OCRPass bool //活体检测状态是否通过
|
|
runPool chan bool //任务(每次只能进行一个开票任务)
|
|
runPool chan bool //任务(每次只能进行一个开票任务)
|
|
phoneCode chan string //短信验证码池
|
|
phoneCode chan string //短信验证码池
|
|
|
|
+ ScanLogin chan bool //扫码登录
|
|
}
|
|
}
|
|
|
|
|
|
func init() {
|
|
func init() {
|
|
@@ -38,6 +42,7 @@ func createInvoiceManager() *InvoiceManager {
|
|
OCRPass: true,
|
|
OCRPass: true,
|
|
runPool: make(chan bool, 1), //开票只能单线程跑
|
|
runPool: make(chan bool, 1), //开票只能单线程跑
|
|
phoneCode: make(chan string, 1), //手机验证码
|
|
phoneCode: make(chan string, 1), //手机验证码
|
|
|
|
+ ScanLogin: make(chan bool, 1), //扫码登录通知
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -51,7 +56,7 @@ func (im *InvoiceManager) MobileVerificationCode(code string) error {
|
|
}
|
|
}
|
|
select {
|
|
select {
|
|
case <-time.After(time.Minute):
|
|
case <-time.After(time.Minute):
|
|
- return gerror.New("验证码接受超时")
|
|
|
|
|
|
+ return gerror.New("验证码接收超时")
|
|
case im.phoneCode <- code:
|
|
case im.phoneCode <- code:
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
@@ -73,6 +78,15 @@ func (im *InvoiceManager) RunJob(ctx context.Context) {
|
|
return
|
|
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)
|
|
total, okNum, err := im.simpleMakeInvoice(ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "开蓝票任务异常 %v", err)
|
|
g.Log().Errorf(ctx, "开蓝票任务异常 %v", err)
|
|
@@ -84,51 +98,71 @@ func (im *InvoiceManager) RunJob(ctx context.Context) {
|
|
}
|
|
}
|
|
|
|
|
|
// simpleMakeInvoice 简单开票
|
|
// 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 (
|
|
var (
|
|
res gdb.Result
|
|
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 {
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-查询待开票异常 %s", err)
|
|
g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-查询待开票异常 %s", err)
|
|
return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
|
|
return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
|
|
}
|
|
}
|
|
g.Log().Infof(ctx, "RunJob-simpleMakeInvoice-本次共加载%d条开票记录", res.Len())
|
|
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 {
|
|
select {
|
|
case im.runPool <- true:
|
|
case im.runPool <- true:
|
|
case <-time.After(time.Minute * 5):
|
|
case <-time.After(time.Minute * 5):
|
|
g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票等待异常,结束此次任务")
|
|
g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票等待异常,结束此次任务")
|
|
return
|
|
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{
|
|
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{{
|
|
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 {
|
|
if err != nil {
|
|
im.ReleasePool()
|
|
im.ReleasePool()
|
|
|
|
+ if gerror.Is(err, consts.LoginOutErr) {
|
|
|
|
+ g.Log().Infof(ctx, "RunJob-simpleMakeInvoice-身份过期,需要重新登录")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票接口调用异常 %v", err)
|
|
g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票接口调用异常 %v", err)
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
|
|
+ okNum++
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|