invoiceCallback.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package service
  2. import (
  3. "ElectronicInvoice/util"
  4. "context"
  5. "fmt"
  6. "github.com/gogf/gf/v2/errors/gerror"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "strings"
  11. "time"
  12. )
  13. // InvoicingCallBackLogic 开票回调逻辑
  14. func InvoicingCallBackLogic(r *ghttp.Request) error {
  15. JyInvoiceManager.ReleasePool() //可进行下次开票请求
  16. tType := r.Get("type").Int() //type [0 需要活体检测;1 成功 返回base64]
  17. switch tType {
  18. case 0: //需要活体检测
  19. //imgVal, err := url.QueryUnescape(r.Get("img").String())
  20. //if err != nil {
  21. // return gerror.Wrap(err, "活体检测二维码解析失败")
  22. //}
  23. JyInvoiceManager.OCRPass = false
  24. if err := util.SendQrImage2ChatBot(r.Get("img").String()); err != nil {
  25. return gerror.Wrap(err, "发送活体检测消息出错")
  26. }
  27. g.Log().Info(r.Context(), "需要活体认证,已发送二维码消息")
  28. case 1: //开票成功
  29. var (
  30. pdfBase64 = r.Get("pdf").String()
  31. orderCode = r.Get("id").String()
  32. num = r.Get("num").String()
  33. kptime = r.Get("kptime").String()
  34. invoiceTime time.Time
  35. err error
  36. path string
  37. )
  38. if pdfBase64 == "" || orderCode == "" || num == "" || kptime == "" {
  39. return gerror.New("缺少回调参数")
  40. }
  41. invoiceTime, err = time.ParseInLocation(kptime, time.DateTime, time.Local)
  42. if err != nil {
  43. return gerror.Wrap(err, "时间格式化异常")
  44. }
  45. path, err = util.SavePdfFile(r.Context(), num, pdfBase64)
  46. if err != nil {
  47. return gerror.Wrap(err, "保存pdf文件失败")
  48. }
  49. g.Log().Infof(r.Context(), "pdf保存成功 orderCode:%s filePath:%s ", orderCode, path)
  50. return updateOrderInvoiceStatus(r.Context(), orderCode, num, invoiceTime, path)
  51. case 3:
  52. g.Log().Info(r.Context(), "活体验证已过期,未完成活体验证")
  53. case 4:
  54. g.Log().Info(r.Context(), "系统错误")
  55. case 6:
  56. g.Log().Info(r.Context(), "税率不存在")
  57. case 7:
  58. g.Log().Info(r.Context(), "非数电票试点纳税人,未核定数电票票种,不允许开票或其他原因")
  59. default:
  60. g.Log().Info(r.Context(), "InvoicingCallBackLogic tType:%s", tType)
  61. }
  62. return nil
  63. }
  64. // updateOrderInvoiceStatus 开票成功进行数据库操作
  65. // orderCode 订单编号
  66. // invoiceNum 发票号码
  67. // createData 开票时间
  68. // pdfPath 下载地址
  69. func updateOrderInvoiceStatus(ctx context.Context, orderCode, invoiceNum string, invoiceTime time.Time, pdfPath string) error {
  70. var (
  71. queryItem = "order_code"
  72. err error
  73. )
  74. if strings.Contains(orderCode, "xx") {
  75. //自助开票
  76. queryItem = "only_Identifying"
  77. }
  78. _, err = g.DB().Update(ctx, "invoice", g.Map{
  79. "invoice_number": invoiceNum,
  80. "url": pdfPath,
  81. "billing_time": invoiceTime.Unix(),
  82. "invoice_status": 1,
  83. }, fmt.Sprintf("invoice_changed = 0 AND %s ", queryItem), orderCode)
  84. if err != nil {
  85. return gerror.Wrap(err, "更新发票状态异常")
  86. }
  87. // 王浩说开票成功 订单状态不用更新;以下代码来自王浩
  88. //if strings.Contains(orderCode, "xx") {
  89. // selfInvoicing(ctx, orderCode)
  90. //}
  91. if err := SendInvoiceSuccessMail(ctx, orderCode); err != nil {
  92. g.Log().Errorf(ctx, "%s 发送邮件失败 err: %v ", orderCode, err)
  93. }
  94. return nil
  95. }
  96. // selfInvoicing 自助开票
  97. func selfInvoicing(ctx context.Context, orderCode string) {
  98. //自助开票
  99. res, err := g.DB().GetOne(ctx, "SELECT GROUP_CONCAT(order_code) as order_code from invoice where only_Identifying=?", orderCode)
  100. if err != nil {
  101. g.Log().Errorf(ctx, "自助开票异常")
  102. }
  103. orderArr := strings.Split(gconv.String(res["order_code"]), ",")
  104. for _, v := range orderArr {
  105. if v == "" {
  106. continue
  107. }
  108. updateData := map[string]interface{}{}
  109. //if InvoiceStatusHandle(v) {
  110. // updateData["applybill_status"] = 2
  111. //} else {
  112. // updateData["applybill_status"] = 3
  113. //}
  114. g.DB().Update(ctx, "dataexport_order", map[string]interface{}{
  115. "order_code": v,
  116. }, updateData)
  117. }
  118. }