wangkaiyue 1 год назад
Родитель
Сommit
4eaa7f9185
3 измененных файлов с 55 добавлено и 41 удалено
  1. 27 24
      internal/service/invoiceCallback.go
  2. 8 8
      internal/service/invoiceMake.go
  3. 20 9
      internal/service/sendMail.go

+ 27 - 24
internal/service/invoiceCallback.go

@@ -24,16 +24,16 @@ func InvoicingCallBackLogic(r *ghttp.Request) error {
 		g.Log().Info(r.Context(), "蓝票需要活体认证,已发送二维码消息")
 	case 1: //开票成功
 		var (
-			pdfBase64 = r.Get("pdf").String()
-			orderCode = r.Get("id").String()
-			num       = r.Get("num").String()
-			kptime    = r.Get("kptime").String()
+			pdfBase64  = r.Get("pdf").String()
+			callBackId = r.Get("id").String()
+			num        = r.Get("num").String()
+			kptime     = r.Get("kptime").String()
 
 			invoiceTime time.Time
 			err         error
 			path        string
 		)
-		if pdfBase64 == "" || orderCode == "" || num == "" || kptime == "" {
+		if pdfBase64 == "" || callBackId == "" || num == "" || kptime == "" {
 			return gerror.New("缺少回调参数")
 		}
 
@@ -45,8 +45,8 @@ func InvoicingCallBackLogic(r *ghttp.Request) error {
 		if err != nil {
 			return gerror.Wrap(err, "保存pdf文件失败")
 		}
-		g.Log().Infof(r.Context(), "pdf保存成功 orderCode:%s filePath:%s ", orderCode, path)
-		return updateOrderInvoiceStatus(r.Context(), orderCode, num, invoiceTime, path)
+		g.Log().Infof(r.Context(), "pdf保存成功 sign:%s filePath:%s ", callBackId, path)
+		return updateOrderInvoiceStatus(r.Context(), callBackId, num, invoiceTime, path)
 	case 3:
 		g.Log().Info(r.Context(), "活体验证已过期,未完成活体验证")
 	case 4:
@@ -66,40 +66,43 @@ func InvoicingCallBackLogic(r *ghttp.Request) error {
 // invoiceNum 发票号码
 // createData 开票时间
 // pdfPath 下载地址
-func updateOrderInvoiceStatus(ctx context.Context, orderCode, invoiceNum string, invoiceTime time.Time, pdfPath string) error {
+func updateOrderInvoiceStatus(ctx context.Context, callBackId, invoiceNum string, invoiceTime time.Time, pdfPath string) error {
 	var (
-		queryItem = "order_code"
-		err       error
+		queryItem  = "order_code"
+		queryValue = ""
+		err        error
 	)
-	if strings.Contains(orderCode, "xx") {
-		//自助开票
-		queryItem = "only_Identifying"
+
+	queryItem, queryValue, err = GetQueryObj(callBackId)
+	if err != nil {
+		return err
 	}
+
 	_, err = g.DB().Update(ctx, "invoice", g.Map{
 		"invoice_number": invoiceNum,
 		"url":            pdfPath,
 		"billing_time":   invoiceTime.Unix(),
 		"invoice_status": 1,
-	}, fmt.Sprintf("invoice_changed = 0 AND %s = ? ", queryItem), orderCode)
+	}, fmt.Sprintf(" %s = ? ", queryItem), queryValue)
 	if err != nil {
 		return gerror.Wrap(err, "更新发票状态异常")
 	}
 
 	// 王浩说开票成功 订单状态不用更新;以下代码来自王浩
-	if strings.Contains(orderCode, "xx") {
-		selfInvoicing(ctx, orderCode)
+	if queryItem == "only_Identifying" {
+		selfInvoicing(ctx, queryValue)
 	}
 
-	if err := SendInvoiceSuccessMail(ctx, orderCode); err != nil {
-		g.Log().Errorf(ctx, "%s 发送邮件失败 err: %v ", orderCode, err)
+	if err := SendInvoiceSuccessMail(ctx, callBackId); err != nil {
+		g.Log().Errorf(ctx, "%s 发送邮件失败 err: %v ", callBackId, err)
 	}
 	return nil
 }
 
 // selfInvoicing 自助开票
-func selfInvoicing(ctx context.Context, orderCode string) {
+func selfInvoicing(ctx context.Context, identifyingValue string) {
 	//自助开票
-	res, err := g.DB().GetOne(ctx, "SELECT GROUP_CONCAT(order_code) as  order_code  from invoice where only_Identifying=?", orderCode)
+	res, err := g.DB().GetOne(ctx, "SELECT GROUP_CONCAT(order_code) as  order_code  from invoice where only_Identifying=?", identifyingValue)
 	if err != nil {
 		g.Log().Errorf(ctx, "自助开票异常")
 	}
@@ -109,7 +112,7 @@ func selfInvoicing(ctx context.Context, orderCode string) {
 			continue
 		}
 		updateData := map[string]interface{}{}
-		if InvoiceStatusHandle(ctx, v) {
+		if invoiceStatusHandle(ctx, v) {
 			updateData["applybill_status"] = 2
 		} else {
 			updateData["applybill_status"] = 3
@@ -125,7 +128,7 @@ func selfInvoicing(ctx context.Context, orderCode string) {
 
 // InvoiceStatusHandle 判断是全额还是部分回款
 // auth :王浩
-func InvoiceStatusHandle(ctx context.Context, orderCode string) bool {
+func invoiceStatusHandle(ctx context.Context, orderCode string) bool {
 	orderData, err := g.DB().GetOne(ctx, "SELECT pay_money FROM dataexport_order WHERE order_code=?", orderCode)
 	if err != nil || orderData.IsEmpty() {
 		return false
@@ -136,8 +139,8 @@ func InvoiceStatusHandle(ctx context.Context, orderCode string) bool {
 		return false
 	}
 	allMoney := gconv.Int64(invoiceData["money"])
-	pay_money := gconv.Int64(orderData["pay_money"])
-	if pay_money == allMoney {
+	payMoney := gconv.Int64(orderData["pay_money"])
+	if payMoney == allMoney {
 		return true
 	}
 	return false

+ 8 - 8
internal/service/invoiceMake.go

@@ -3,6 +3,7 @@ package service
 import (
 	"ElectronicInvoice/internal/consts"
 	"context"
+	"fmt"
 	"github.com/gogf/gf/v2/database/gdb"
 	"github.com/gogf/gf/v2/errors/gerror"
 	"github.com/gogf/gf/v2/frame/g"
@@ -16,8 +17,8 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 	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())
+	//查询需要开票的数据(新开发票【a.invoice_status=0 AND a.invoice_changed=0】,红冲后新开【a.invoice_status=2 AND a.invoice_changed=1】)
+	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) or (a.invoice_status=2 AND a.invoice_changed=1 )) AND  a.invoice_variety='普通发票(电子发票)' AND a.invoice_order_code is NULL AND a.create_time > ? ", consts.InvoiceStartTime.Unix())
 	if err != nil {
 		err = gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
 		return
@@ -31,10 +32,9 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 			return
 		}
 		var (
-			orderCode = gconv.String(m["order_code"])
-			iType     = gconv.String(m["invoice_type"])
-			remark    = gconv.String(m["remark"])
-			prices    float64
+			iType  = gconv.String(m["invoice_type"])
+			remark = gconv.String(m["remark"])
+			prices float64
 		)
 
 		//公对公转账 账单金额可以修改 开发票应取实付金额 pay_money
@@ -47,7 +47,7 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 
 		c := MakeInvoiceData{
 			Type:  "2",
-			Id:    orderCode,
+			Id:    fmt.Sprintf("id:%d", gconv.Int64(m["id"])), //因为orderCode不唯一,此处用订单发票数据库id
 			Notes: remark,
 			Fhr:   g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
 			InvoiceArr: []MakeInvoiceItems{{
@@ -106,7 +106,7 @@ func (im *InvoiceManager) selfMakeInvoice(ctx context.Context) (total, okNum int
 		)
 		c := MakeInvoiceData{
 			Type:  "2",
-			Id:    orderCode,
+			Id:    fmt.Sprintf("only_Identifying:%s", orderCode),
 			Notes: remark,
 			Fhr:   g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
 			InvoiceArr: []MakeInvoiceItems{{

+ 20 - 9
internal/service/sendMail.go

@@ -164,20 +164,21 @@ func SendMail(ctx context.Context, targetMail, html, title string) error {
 	return nil
 }
 
-func SendInvoiceSuccessMail(ctx context.Context, orderCode string) error {
+func SendInvoiceSuccessMail(ctx context.Context, callBackId string) error {
 	var (
-		queryItem = "order_code"
-		err       error
-		record    invoiceRecord
+		queryItem, queryValue string
+		err                   error
+		record                invoiceRecord
 	)
 
-	if strings.Contains(orderCode, "xx") {
-		//自助开票
-		queryItem = "only_Identifying"
+	queryItem, queryValue, err = GetQueryObj(callBackId)
+	if err != nil {
+		return err
 	}
-	res, err := g.DB().GetOne(ctx, fmt.Sprintf("SELECT invoice_type,mail,taxpayer_identnum,company_name,phone,url FROM invoice WHERE %s = ? ", queryItem), orderCode)
+
+	res, err := g.DB().GetOne(ctx, fmt.Sprintf("SELECT invoice_type,mail,taxpayer_identnum,company_name,phone,url,order_code FROM invoice WHERE %s = ? ", queryItem), queryValue)
 	if err != nil {
-		return gerror.Wrapf(err, "未查询到订单发票信息 %s:%s", queryItem, orderCode)
+		return gerror.Wrapf(err, "未查询到订单发票信息 %s:%s", queryItem, queryValue)
 	}
 
 	if err = res.Struct(&record); err != nil {
@@ -200,3 +201,13 @@ func (ir *invoiceRecord) GetMailHtmlContext() string {
 	}
 	return ""
 }
+
+func GetQueryObj(callBackId string) (query, value string, err error) {
+	arr := strings.Split(callBackId, ":")
+	if len(arr) != 2 {
+		err = gerror.Newf("callBackId id异常%s", callBackId)
+		return
+	}
+	query, value = arr[0], arr[1]
+	return
+}