123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package service
- import (
- "ElectronicInvoice/internal/consts"
- "ElectronicInvoice/util"
- "context"
- "fmt"
- "github.com/gogf/gf/v2/database/gdb"
- "github.com/gogf/gf/v2/encoding/gjson"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/gogf/gf/v2/util/gconv"
- "time"
- )
- // 红票信息
- type RedMsg struct {
- InvoiceCode string `json:"invoice_code"`
- InvoiceNumber string `json:"invoice_number"`
- Url string `json:"url"`
- SourceUrl string `json:"source_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: //红冲成功
- err := g.DB().Transaction(r.Context(), func(ctx context.Context, tx gdb.TX) error {
- var (
- pdfBase64 = r.Get("pdf").String()
- num = r.Get("num").String() //蓝票号
- offsetNum = r.Get("offsetNum").String() //红票号
- kptime = r.Get("kptime").String()
- data = r.Get("data").Map()
- invoiceTime time.Time
- sourceUrl string //税务局发票链接
- fileUrl string //剑鱼文件地址
- redByte []byte
- err error
- )
- invoiceTime, err = time.ParseInLocation(consts.DateFormat_Full, kptime, time.Local)
- if err != nil {
- return gerror.Wrap(err, "时间格式化异常")
- }
- if pdfBase64 == "" || offsetNum == "" || num == "" || kptime == "" || len(data) == 0 {
- return gerror.New("缺少回调参数")
- }
- // 需求变更,数据库存储税务局发票地址
- if sourceUrl = gconv.String(data["jfUrl"]); sourceUrl == "" {
- return gerror.New("获取pdf交付地址异常")
- }
- fileUrl, err = util.SavePdfFile(r.Context(), fmt.Sprintf("RED_%s", num), pdfBase64)
- if err != nil {
- return gerror.Wrap(err, "InvoicingMakeRedCallBackLogic 保存红冲pdf文件失败")
- }
- //保存红票信息
- redByte, _ = gjson.Marshal(RedMsg{
- InvoiceNumber: offsetNum,
- Url: fileUrl,
- SourceUrl: sourceUrl,
- InvoiceSerialNum: fmt.Sprintf("RED_%s", num),
- InvoiceStatus: "1",
- BillingTime: invoiceTime.Unix(),
- })
- if num == "" {
- return gerror.Wrap(err, "InvoicingMakeRedCallBackLogic 蓝票号异常")
- }
- //旧蓝票状态修改已红冲
- res1, sqlErr1 := tx.Update("invoice", g.Map{
- "invoice_status": -2,
- "red": string(redByte),
- }, "invoice_changed = 0 AND invoice_number= ? ", num)
- if sqlErr1 != nil {
- return gerror.Wrapf(sqlErr1, "InvoicingMakeRedCallBackLogic 旧蓝票状态修改已红冲异常 sql(invoice_changed = 0 AND invoice_number= %s)", num)
- }
- //新蓝票状态改为可以开票
- res2, sqlErr2 := tx.Update("invoice", g.Map{
- "invoice_status": 2,
- }, "invoice_changed = 1 AND invoice_number= ? ", num)
- if sqlErr2 != nil {
- return gerror.Wrapf(sqlErr2, "InvoicingMakeRedCallBackLogic 新蓝票状态改为可以开票异常 sql(invoice_changed = 1 AND invoice_number= %s)", num)
- }
- // 蓝票数量=红冲数量
- num1, _ := res1.RowsAffected()
- num2, _ := res2.RowsAffected()
- if !(num1 == num2 && num1 >= 1) {
- return gerror.Newf("更新校验异常 蓝(旧%d) 蓝(新%d)", num1, num2)
- }
- return nil
- })
- if err != nil {
- return gerror.Wrap(err, "InvoicingMakeRedCallBackLogic 更新发票状态异常")
- }
- return nil
- case 1: //失败
- case 2: //授信额度等信息
- g.Log().Info(r.Context(), "授信额度等信息")
- case 3:
- g.Log().Info(r.Context(), "活体验证已过期,未完成活体验证")
- case 4:
- g.Log().Info(r.Context(), "活体成功")
- case 5:
- if err := util.SendQrImage2ChatBot(r.Get("img").String()); err != nil {
- return gerror.Wrap(err, "发送活体检测消息出错")
- }
- g.Log().Info(r.Context(), "红票需要活体认证,已发送二维码消息")
- case 9:
- g.Log().Info(r.Context(), "活体码未知错误")
- case 10:
- g.Log().Info(r.Context(), "非数电票试点纳税人,未核定数电票票种,不允许开票")
- case 11:
- g.Log().Info(r.Context(), "获取授信额度失败")
- default:
- g.Log().Info(r.Context(), "InvoicingMakeRedCallBackLogic tType:%s", tType)
- }
- return nil
- }
|