|
@@ -5,6 +5,7 @@ import (
|
|
|
"dsbqj-admin/app/task"
|
|
"dsbqj-admin/app/task"
|
|
|
"dsbqj-admin/model/mongo/subscribe"
|
|
"dsbqj-admin/model/mongo/subscribe"
|
|
|
"dsbqj-admin/pkg/logger"
|
|
"dsbqj-admin/pkg/logger"
|
|
|
|
|
+ "dsbqj-admin/pkg/sender"
|
|
|
"dsbqj-admin/pkg/util"
|
|
"dsbqj-admin/pkg/util"
|
|
|
"errors"
|
|
"errors"
|
|
|
"fmt"
|
|
"fmt"
|
|
@@ -12,104 +13,101 @@ import (
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
|
+ "time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type SubscribeOpenService struct {
|
|
type SubscribeOpenService struct {
|
|
|
DeviceId string `json:"device_id"`
|
|
DeviceId string `json:"device_id"`
|
|
|
OpenId string `json:"open_id"`
|
|
OpenId string `json:"open_id"`
|
|
|
- Module string `json:"module"`
|
|
|
|
|
|
|
+ Module []int `json:"module"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (s *SubscribeOpenService) createSubscribe(deviceId, openId, module string) error {
|
|
|
|
|
- sub := &subscribe.Subscribe{
|
|
|
|
|
- OpenId: openId,
|
|
|
|
|
- DeviceId: deviceId,
|
|
|
|
|
- Modules: make(map[string]*subscribe.ModuleSubscribe),
|
|
|
|
|
|
|
+func updateSubscribeSetting(deviceId, openId string, modules []int, enabled bool) error {
|
|
|
|
|
+ if deviceId == "" || (enabled && openId == "") {
|
|
|
|
|
+ return errors.New("device_id or open_id is empty")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- sub.Modules[module] = &subscribe.ModuleSubscribe{
|
|
|
|
|
- Enabled: true,
|
|
|
|
|
- OpenAt: util.GetNowSecond(),
|
|
|
|
|
|
|
+ ids, err := subscribeSettingIDs(modules)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return mgm.Coll(sub).Create(sub)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func (this *SubscribeOpenService) updateSubscribe(deviceId string, openId string, module string) error {
|
|
|
|
|
now := util.GetNowSecond()
|
|
now := util.GetNowSecond()
|
|
|
-
|
|
|
|
|
- filter := bson.M{"device_id": deviceId}
|
|
|
|
|
-
|
|
|
|
|
- update := bson.M{
|
|
|
|
|
- "$set": bson.M{
|
|
|
|
|
- "device_id": deviceId,
|
|
|
|
|
- "open_id": openId,
|
|
|
|
|
- "modules." + module: bson.M{
|
|
|
|
|
- "enabled": true,
|
|
|
|
|
- "open_at": now,
|
|
|
|
|
- },
|
|
|
|
|
- "updated_at": now,
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ update := subscribeSettingUpdate(ids, openId, enabled, now)
|
|
|
|
|
+ if enabled {
|
|
|
|
|
+ _, err = mgm.Coll(&subscribe.Subscribe{}).UpdateOne(
|
|
|
|
|
+ context.Background(),
|
|
|
|
|
+ bson.M{"device_id": deviceId},
|
|
|
|
|
+ update,
|
|
|
|
|
+ options.Update().SetUpsert(true),
|
|
|
|
|
+ )
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- opts := options.Update().SetUpsert(true)
|
|
|
|
|
-
|
|
|
|
|
- _, err := mgm.Coll(&subscribe.Subscribe{}).
|
|
|
|
|
- UpdateOne(context.Background(), filter, update, opts)
|
|
|
|
|
-
|
|
|
|
|
|
|
+ _, err = mgm.Coll(&subscribe.Subscribe{}).UpdateOne(
|
|
|
|
|
+ context.Background(), bson.M{"device_id": deviceId}, update,
|
|
|
|
|
+ )
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (this *SubscribeOpenService) Open() error {
|
|
|
|
|
- subscribeOne := new(subscribe.Subscribe)
|
|
|
|
|
- err := mgm.Coll(&subscribe.Subscribe{}).First(bson.M{"device_id": this.DeviceId}, subscribeOne)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- // 1️⃣ 没有记录
|
|
|
|
|
- if err == mongo.ErrNoDocuments {
|
|
|
|
|
- // 走创建逻辑
|
|
|
|
|
- return this.createSubscribe(this.DeviceId, this.OpenId, this.Module)
|
|
|
|
|
|
|
+func subscribeSettingIDs(modules []int) ([]int, error) {
|
|
|
|
|
+ if len(modules) == 0 {
|
|
|
|
|
+ return nil, errors.New("module must contain at least one SubscribePush ID")
|
|
|
|
|
+ }
|
|
|
|
|
+ ids := make([]int, 0, len(modules))
|
|
|
|
|
+ seen := make(map[int]bool, len(modules))
|
|
|
|
|
+ for _, id := range modules {
|
|
|
|
|
+ if id <= 0 || !sender.SubscribePushConfigByID(id) {
|
|
|
|
|
+ return nil, fmt.Errorf("unknown SubscribePush ID %d", id)
|
|
|
|
|
+ }
|
|
|
|
|
+ if !seen[id] {
|
|
|
|
|
+ ids = append(ids, id)
|
|
|
|
|
+ seen[id] = true
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
+ return ids, nil
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- // 2️⃣ 其他错误
|
|
|
|
|
- return err
|
|
|
|
|
|
|
+func subscribeSettingUpdate(ids []int, openId string, enabled bool, now int64) bson.M {
|
|
|
|
|
+ set := bson.M{"updated_at": now}
|
|
|
|
|
+ update := bson.M{"$set": set}
|
|
|
|
|
+ if enabled {
|
|
|
|
|
+ set["open_id"] = openId
|
|
|
|
|
+ update["$addToSet"] = bson.M{"settings": bson.M{"$each": ids}}
|
|
|
|
|
+ update["$setOnInsert"] = bson.M{"created_at": time.Now()}
|
|
|
|
|
+ return update
|
|
|
}
|
|
}
|
|
|
- return this.updateSubscribe(this.DeviceId, this.OpenId, this.Module)
|
|
|
|
|
|
|
+ update["$pull"] = bson.M{"settings": bson.M{"$in": ids}}
|
|
|
|
|
+ return update
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (this *SubscribeOpenService) Open() error {
|
|
|
|
|
+ return updateSubscribeSetting(this.DeviceId, this.OpenId, this.Module, true)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type SubscribeCloseService struct {
|
|
type SubscribeCloseService struct {
|
|
|
DeviceId string `json:"device_id"`
|
|
DeviceId string `json:"device_id"`
|
|
|
- Module string `json:"module"`
|
|
|
|
|
|
|
+ Module []int `json:"module"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (this *SubscribeCloseService) Close() error {
|
|
func (this *SubscribeCloseService) Close() error {
|
|
|
- now := util.GetNowSecond()
|
|
|
|
|
-
|
|
|
|
|
- filter := bson.M{
|
|
|
|
|
- "device_id": this.DeviceId,
|
|
|
|
|
- "modules." + this.Module + ".enabled": true,
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- update := bson.M{
|
|
|
|
|
- "$set": bson.M{
|
|
|
|
|
- "modules." + this.Module + ".enabled": false,
|
|
|
|
|
- "modules." + this.Module + ".close_at": now,
|
|
|
|
|
- "updated_at": now,
|
|
|
|
|
- },
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err := mgm.Coll(&subscribe.Subscribe{}).
|
|
|
|
|
- UpdateOne(context.Background(), filter, update)
|
|
|
|
|
-
|
|
|
|
|
- return err
|
|
|
|
|
|
|
+ return updateSubscribeSetting(this.DeviceId, "", this.Module, false)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type SubscribeCheckService struct {
|
|
type SubscribeCheckService struct {
|
|
|
DeviceId string `json:"device_id"`
|
|
DeviceId string `json:"device_id"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (this *SubscribeCheckService) Check() error {
|
|
|
|
|
-
|
|
|
|
|
- return nil
|
|
|
|
|
|
|
+func (this *SubscribeCheckService) Check() ([]int, error) {
|
|
|
|
|
+ if this.DeviceId == "" {
|
|
|
|
|
+ return nil, errors.New("device_id is empty")
|
|
|
|
|
+ }
|
|
|
|
|
+ player := new(subscribe.Subscribe)
|
|
|
|
|
+ err := mgm.Coll(&subscribe.Subscribe{}).First(bson.M{"device_id": this.DeviceId}, player)
|
|
|
|
|
+ if err != nil && err != mongo.ErrNoDocuments {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ if player.Settings == nil {
|
|
|
|
|
+ return []int{}, nil
|
|
|
|
|
+ }
|
|
|
|
|
+ return player.Settings, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type SubscribeSendService struct {
|
|
type SubscribeSendService struct {
|
|
@@ -118,12 +116,17 @@ type SubscribeSendService struct {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (this *SubscribeSendService) Send() error {
|
|
func (this *SubscribeSendService) Send() error {
|
|
|
|
|
+ row, ok := sender.SubscribePushConfig(this.Module)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ return errors.New("unknown module")
|
|
|
|
|
+ }
|
|
|
|
|
+ if !sender.SubscribePushActive(this.Module) {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
var list = make([]*subscribe.Subscribe, 0)
|
|
var list = make([]*subscribe.Subscribe, 0)
|
|
|
// 更具deviceId 和 module 获取 需要推送的用户openid 列表
|
|
// 更具deviceId 和 module 获取 需要推送的用户openid 列表
|
|
|
// data 应该是个json对象
|
|
// data 应该是个json对象
|
|
|
- filter := bson.M{
|
|
|
|
|
- "modules." + this.Module + ".enabled": true,
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ filter := bson.M{"settings": row.ID, "open_id": bson.M{"$exists": true, "$ne": ""}}
|
|
|
|
|
|
|
|
if this.Module != "alliance" {
|
|
if this.Module != "alliance" {
|
|
|
filter["device_id"] = bson.M{"$in": this.Data}
|
|
filter["device_id"] = bson.M{"$in": this.Data}
|
|
@@ -164,8 +167,11 @@ func (this *SubscribeTaskService) Task() error {
|
|
|
default:
|
|
default:
|
|
|
return errors.New("unknown module")
|
|
return errors.New("unknown module")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if this.Status {
|
|
if this.Status {
|
|
|
|
|
+ if !sender.SubscribePushActive(this.Module) {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
logger.Info("Subscribe task append key %s, device id %s timestamp %d, task_time %s", key, this.DeviceId, this.Timestamp, util.TimeStr2DateTime(this.Timestamp, util.TimeLayout))
|
|
logger.Info("Subscribe task append key %s, device id %s timestamp %d, task_time %s", key, this.DeviceId, this.Timestamp, util.TimeStr2DateTime(this.Timestamp, util.TimeLayout))
|
|
|
task.SubscribeTask.Append(key, this.DeviceId, this.Timestamp, this.Module)
|
|
task.SubscribeTask.Append(key, this.DeviceId, this.Timestamp, this.Module)
|
|
|
} else {
|
|
} else {
|