Browse Source

wip:消息提醒

wangkaiyue 1 year ago
parent
commit
a2a05ae1ab
2 changed files with 32 additions and 6 deletions
  1. 1 1
      config.yaml
  2. 31 5
      internal/service/invoiceManager.go

+ 1 - 1
config.yaml

@@ -69,7 +69,7 @@ invoiceJob:
   notice:
     qwRobotUrl: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e7792e7a-159d-4419-b1ed-27ea19b6ea54" #扫码登录企业微信消息机器人接口
     unFinish: #未开票提醒
-      day: 2 #几天未开票提醒时间
+      hours: 24 #小时未开票提醒时间
       cron: "# 0 10 * * *" #每天10点发送未开票提示消息
       notice:  ["WeiXin"]
 

+ 31 - 5
internal/service/invoiceManager.go

@@ -3,6 +3,7 @@ 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/os/gcron"
@@ -160,15 +161,40 @@ func (im *InvoiceManager) NoticeJob(ctx context.Context) {
 	var (
 		unMakeRed  = 0
 		unMakeBlue = 0
+
+		limitHours = g.Cfg().MustGet(ctx, "invoiceJob.notice.unFinish.hours", 24).Int64()
 	)
 
-	unMakeBlue, _ = g.DB().GetCount(ctx, "")
-	unMakeRed, _ = g.DB().GetCount(ctx, "")
+	if im.StopRunning {
+		return
+	}
+
+	createTimeLimit := time.Now().Unix() - limitHours*60*60
+
+	// 蓝票未完成发票数量查询
+	unMakeBlue, _ = g.DB().GetCount(ctx, `SELECT count(*) FROM(
+		SELECT a.id as onlyId FROM invoice a WHERE a.invoice_variety like '%电子%' AND ((a.invoice_status=0 AND a.invoice_changed=0) or (a.invoice_status=2 AND a.invoice_changed=1 )) AND  (a.invoice_order_code is NULL OR a.invoice_order_code not like '%,%') AND a.create_time > UNIX_TIMESTAMP('2024-04-23 00:00:00') AND a.create_time<?
+		UNION 
+		SELECT a.only_Identifying as onlyId FROM invoice a WHERE a.invoice_variety like '%电子%' AND ((a.invoice_status=0 AND a.invoice_changed=0) or (a.invoice_status=2 AND a.invoice_changed=1)) AND a.invoice_order_code like '%,%' AND a.create_time > UNIX_TIMESTAMP('2024-04-23 00:00:00') AND a.create_time<? GROUP BY invoice_order_code
+		) as data`, createTimeLimit, createTimeLimit)
+
+	// 红票未完成发票数量查询
+	unMakeRed, _ = g.DB().GetCount(ctx, `SELECT count(*) FROM(
+		SELECT a.invoice_number as onlyId FROM invoice a  WHERE a.invoice_variety like '%电子%' AND a.invoice_status=0 AND a.invoice_changed=1 AND (a.invoice_order_code is NULL OR a.invoice_order_code not like '%,%') AND a.create_time > UNIX_TIMESTAMP('2024-04-23 00:00:00') AND a.create_time<?
+		UNION
+		SELECT a.invoice_number as onlyId  FROM invoice a  WHERE a.invoice_variety like '%电子%' AND a.invoice_status=0 AND a.invoice_changed=1 AND a.invoice_order_code like '%,%' AND a.create_time > UNIX_TIMESTAMP('2024-04-23 00:00:00') AND a.create_time<? GROUP BY invoice_order_code 
+		) as data`, createTimeLimit, createTimeLimit)
 
 	if unMakeRed > 0 || unMakeBlue > 0 {
-		context := ``
-		err := util.SendSimpleMsg2ChatBot(context, g.Cfg().MustGet(ctx, "invoiceJob.notice.unFinish.notice").Strings()...)
-		if err != nil {
+		noticeMsg := fmt.Sprintf("开票超%d小时提醒\n", limitHours)
+		if unMakeBlue > 0 {
+			noticeMsg += fmt.Sprintf("蓝票:%d例\n", unMakeBlue)
+		}
+		if unMakeRed > 0 {
+			noticeMsg += fmt.Sprintf("红票:%d例\n", unMakeRed)
+		}
+
+		if err := util.SendSimpleMsg2ChatBot(noticeMsg, g.Cfg().MustGet(ctx, "invoiceJob.notice.unFinish.notice").Strings()...); err != nil {
 			g.Log().Errorf(ctx, "发送未开票消息异常 %v", err)
 		}
 	}