invoiceCallback.go 3.8 KB

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