package service import ( "ElectronicInvoice/util" "context" "fmt" "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" "strings" "time" ) // InvoicingCallBackLogic 开票回调逻辑 func InvoicingCallBackLogic(r *ghttp.Request) error { tType := r.Get("type").Int() //type [0 需要活体检测;1 成功 返回base64] switch tType { case 0: //需要活体检测 //imgVal, err := url.QueryUnescape(r.Get("img").String()) //if err != nil { // return gerror.Wrap(err, "活体检测二维码解析失败") //} JyInvoiceManager.OCRPass = false if err := util.SendQrImage2ChatBot(r.Get("img").String()); err != nil { return gerror.Wrap(err, "发送活体检测消息出错") } g.Log().Info(r.Context(), "需要活体认证,已发送二维码消息") case 1: //开票成功 var ( pdfBase64 = r.Get("pdf").String() orderCode = r.Get("id").String() num = r.Get("num").String() kptime = r.Get("kptime").String() invoiceTime time.Time err error path string ) if pdfBase64 == "" || orderCode == "" || num == "" || kptime == "" { return gerror.New("缺少回调参数") } invoiceTime, err = time.ParseInLocation(time.DateTime, kptime, time.Local) if err != nil { return gerror.Wrap(err, "时间格式化异常") } path, err = util.SavePdfFile(r.Context(), num, pdfBase64) if err != nil { return gerror.Wrap(err, "保存pdf文件失败") } g.Log().Infof(r.Context(), "pdf保存成功 orderCode:%s filePath:%s ", orderCode, path) return updateOrderInvoiceStatus(r.Context(), orderCode, num, invoiceTime, path) case 3: g.Log().Info(r.Context(), "活体验证已过期,未完成活体验证") case 4: g.Log().Info(r.Context(), "系统错误") case 6: g.Log().Info(r.Context(), "税率不存在") case 7: g.Log().Info(r.Context(), "非数电票试点纳税人,未核定数电票票种,不允许开票或其他原因") default: g.Log().Info(r.Context(), "InvoicingCallBackLogic tType:%s", tType) } return nil } // updateOrderInvoiceStatus 开票成功进行数据库操作 // orderCode 订单编号 // invoiceNum 发票号码 // createData 开票时间 // pdfPath 下载地址 func updateOrderInvoiceStatus(ctx context.Context, orderCode, invoiceNum string, invoiceTime time.Time, pdfPath string) error { var ( queryItem = "order_code" err error ) if strings.Contains(orderCode, "xx") { //自助开票 queryItem = "only_Identifying" } _, err = g.DB().Update(ctx, "invoice", g.Map{ "invoice_number": invoiceNum, "url": pdfPath, "billing_time": invoiceTime.Unix(), "invoice_status": 1, }, fmt.Sprintf("invoice_changed = 0 AND %s = ? ", queryItem), orderCode) if err != nil { return gerror.Wrap(err, "更新发票状态异常") } // 王浩说开票成功 订单状态不用更新;以下代码来自王浩 //if strings.Contains(orderCode, "xx") { // selfInvoicing(ctx, orderCode) //} if err := SendInvoiceSuccessMail(ctx, orderCode); err != nil { g.Log().Errorf(ctx, "%s 发送邮件失败 err: %v ", orderCode, err) } return nil } // selfInvoicing 自助开票 func selfInvoicing(ctx context.Context, orderCode string) { //自助开票 res, err := g.DB().GetOne(ctx, "SELECT GROUP_CONCAT(order_code) as order_code from invoice where only_Identifying=?", orderCode) if err != nil { g.Log().Errorf(ctx, "自助开票异常") } orderArr := strings.Split(gconv.String(res["order_code"]), ",") for _, v := range orderArr { if v == "" { continue } updateData := map[string]interface{}{} //if InvoiceStatusHandle(v) { // updateData["applybill_status"] = 2 //} else { // updateData["applybill_status"] = 3 //} g.DB().Update(ctx, "dataexport_order", map[string]interface{}{ "order_code": v, }, updateData) } }