invoiceCallback.go 4.5 KB

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