invoiceCallback.go 3.6 KB

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