Browse Source

消息推送

baishaojie 4 weeks ago
parent
commit
1e97d2004f

+ 1 - 0
apiserver/model/message.go

@@ -17,6 +17,7 @@ type Message struct {
 	Status     int       `gorm:"column:status"`
 	Status     int       `gorm:"column:status"`
 	PushCount  int       `gorm:"column:push_count"`
 	PushCount  int       `gorm:"column:push_count"`
 	PushLimit  int       `gorm:"column:push_limit"`
 	PushLimit  int       `gorm:"column:push_limit"`
+	ErrInfo    string    `gorm:"column:errInfo"`
 }
 }
 
 
 type MessageResp struct {
 type MessageResp struct {

+ 50 - 2
apiserver/service/job.go

@@ -18,9 +18,49 @@ func JobInit() {
 	// notice.SendMessage(nil)
 	// notice.SendMessage(nil)
 	c.AddFunc("30 * * * * *", ReadMsgs)
 	c.AddFunc("30 * * * * *", ReadMsgs)
 	c.Start()
 	c.Start()
+
+	// 定时清理数据(记录+删除)
+	c2 := cron.New()
+	c2.AddFunc("0 0 0/1 * * *", clearMessage)
+	c2.Start()
+
+	// 清理七天前的记录数据
+	c3 := cron.New()
+	c3.AddFunc("0 0 0 0/1 * *", clearMessageOfBeforeWeek)
+	c3.Start()
+}
+
+func clearMessageOfBeforeWeek() {
+	log.Errorf("%v\n", "-------------------定时清理七天前的备份数据--------------------")
+	errCopy := svc.DB.Exec(" delete from message_history where created_at< DATE_SUB(NOW(),INTERVAL 7 day)  ")
+	if errCopy.Error == nil {
+		log.Errorf("%v\n", "-------------------备份数据删除完成!--------------------")
+	} else {
+		log.Errorf("%v\n", "-------------------备份数据删除失败!--------------------")
+		log.Errorf("%v", errCopy.Error)
+	}
+}
+
+func clearMessage() {
+	log.Errorf("%v\n", "-------------------定时清理数据,开始copy数据--------------------")
+	errCopy := svc.DB.Exec(" insert into message_history select * from message where `status`!=0 or target='null' ")
+	if errCopy.Error == nil {
+		log.Errorf("%v\n", "-------------------copy数据完成,开始删除数据--------------------")
+		errDel := svc.DB.Exec(" delete from message where `status`!=0 or target='null' ")
+		if errDel.Error == nil {
+			log.Errorf("%v\n", "-------------------删除数据完成,定时清理数据完成!--------------------")
+		} else {
+			log.Errorf("%v\n", "-------------------删除数据失败!--------------------")
+			log.Errorf("%v", errDel.Error)
+		}
+	} else {
+		log.Errorf("%v\n", "-------------------copy数据失败!--------------------")
+		log.Errorf("%v", errCopy.Error)
+	}
 }
 }
 
 
 func ReadMsgs() {
 func ReadMsgs() {
+	log.Errorf("%v\n", "-------------------发送消息--------------------")
 	var msgs []model.Message
 	var msgs []model.Message
 	var err error
 	var err error
 	if msgs, err = svc.QueryMsg(); err != nil {
 	if msgs, err = svc.QueryMsg(); err != nil {
@@ -31,10 +71,11 @@ func ReadMsgs() {
 	if len(msgs) == 0 {
 	if len(msgs) == 0 {
 		return
 		return
 	}
 	}
-	// fmt.Printf("%v\n", msgs)
+	fmt.Printf("%v\n", msgs)
 	for _, val := range msgs {
 	for _, val := range msgs {
+
 		e := SendMsg(&val)
 		e := SendMsg(&val)
-		log.Error(e)
+		log.Error("----e----", e)
 	}
 	}
 }
 }
 
 
@@ -66,6 +107,13 @@ func SendMsg(val *model.Message) (err error) {
 			if resp, tmperr := notice.MsgTemplate(jsonstr); tmperr != nil {
 			if resp, tmperr := notice.MsgTemplate(jsonstr); tmperr != nil {
 				err = errors.Wrap(tmperr, "SendMessage{send message error}==%v")
 				err = errors.Wrap(tmperr, "SendMessage{send message error}==%v")
 				log.Errorf("%v", err)
 				log.Errorf("%v", err)
+				val.ErrInfo = tmperr.Error()
+				// 失败记录错误信息、状态
+				// if e := svc.UpdateMessageFail(val); e != nil {
+				// 	err = errors.Wrap(e, "SendMessage{update message error}=%v")
+				// 	log.Errorf("%v", err)
+				// }
+
 			} else {
 			} else {
 				log.Infof("send msg success%v", string(resp))
 				log.Infof("send msg success%v", string(resp))
 				if e := svc.UpdateMessage(val); e != nil {
 				if e := svc.UpdateMessage(val); e != nil {

+ 5 - 35
apiserver/service/message.go

@@ -65,42 +65,12 @@ func (svc *Service) UpdateMessageStatus(id int) (err error) {
 }
 }
 
 
 func (svc *Service) QueryMsg() (msgs []model.Message, err error) {
 func (svc *Service) QueryMsg() (msgs []model.Message, err error) {
+	var sql2 string
+	sql := `select sqltext from apisql where sqlname='getMessage' limit 1 `
 
 
-	sql := `select m.* from message m join message_type mt
-	on m.sys_name=mt.sys_name and m.pasture_id=mt.pasture_id and m.service_id=mt.service_id
-	where m.status=0 and (
-	   mt.remind_type=1 
-	   or
-	   (
-		   mt.remind_type=2 and 
-		   timestampdiff(minute,m.created_at,now())>=mt.delay_time	and
-		   (date_format(now(),'%H:%i')>=mt.push_time or mt.push_time='')	and 
-		   (
-			   cycle_type=1 or cycle_type=3 or
-			   ( 
-				   cycle_type=2 and timestampdiff(minute,m.updated_at,now()) >=mt.interval_time 
-				   and m.push_count<mt.push_limit
-			   )
-		   )
-	   )
-	   or 
-	   (
-		   mt.remind_type=3 and 
-			(
-			   mt.date_type=1 
-			   or (mt.date_type=2 and  weekday(now() ) +1 =mt.push_date)
-			   or (mt.date_type=3 and  (day(now()) =mt.push_date or DAYOFMONTH(now())<mt.push_date))
-			) 
-			and date_format(now(),'%H:%i')>=mt.push_time
-			and 
-			(
-				 push_count=0
-				or 
-				( day(now())> day(mt.updated_at) and push_count>0)
-			)
-	   )
-	) `
-	if err = svc.DB.Raw(sql).Scan(&msgs).Error; err != nil {
+	svc.DB.Raw(sql).Scan(&sql2)
+
+	if err = svc.DB.Raw(sql2).Scan(&msgs).Error; err != nil {
 		err = errors.Wrap(err, "queryMsg{query msg exec sql err}==%v")
 		err = errors.Wrap(err, "queryMsg{query msg exec sql err}==%v")
 		log.Errorf("%v", err)
 		log.Errorf("%v", err)
 		return
 		return

+ 0 - 7
apiserver/service/service.go

@@ -26,13 +26,6 @@ func New(c *config.Config) (s *Service) {
 	return
 	return
 }
 }
 
 
-// func (svc *Service) QueryDB(tname string, condition map[string]interface{}, result interface{}) (err error) {
-// 	if err = svc.DB.Table(tname).Where(condition).Find(result).Error; err != nil {
-// 		err = errors.Wrap(err, "service.QueryDB")
-// 	}
-// 	return
-// }
-
 func (svc *Service) QuerySysPastureUrl(con *model.SysPastureUrl) (result model.SysPastureUrl, err error) {
 func (svc *Service) QuerySysPastureUrl(con *model.SysPastureUrl) (result model.SysPastureUrl, err error) {
 
 
 	if err = svc.DB.Table("sys_pasture_url").Where(con).First(&result).Error; err != nil {
 	if err = svc.DB.Table("sys_pasture_url").Where(con).First(&result).Error; err != nil {

BIN
cmd/apiserver/apiserver.exe


+ 3 - 0
cmd/apiserver/windows.bat

@@ -0,0 +1,3 @@
+set GOARCH=amd64
+set GOOS=window
+go build -o notice.exe

BIN
cmd/client/client.exe


+ 0 - 1
middleware/notice/wxoffice.go

@@ -73,7 +73,6 @@ send tmp message to wx
 */
 */
 func MsgTemplate(msg []byte) (resp []byte, err error) {
 func MsgTemplate(msg []byte) (resp []byte, err error) {
 	if resp, err = template.Send(ServerAcc, msg); err != nil {
 	if resp, err = template.Send(ServerAcc, msg); err != nil {
-		err = errors.Wrap(err, "notice.MsgTemplate{}")
 		log.Error(err)
 		log.Error(err)
 		fmt.Println(err)
 		fmt.Println(err)
 	}
 	}