package main

import (
	"app.yhyue.com/data_processing/common_utils/udp"
	"encoding/json"
	"fmt"
	wlog "github.com/wcc4869/common_utils/log"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"net"
)

//GetJyKey 免费订阅：o_jy.a_key.key/appendkey
func GetJyKey(data map[string]interface{}) (res []interface{}) {
	// 获取o_jy.a_key.key的值
	if ojy, ok := data["o_jy"].(map[string]interface{}); ok {
		if akey, ok := ojy["a_key"].([]interface{}); ok {
			for _, v := range akey {
				if m, ok := v.(map[string]interface{}); ok {
					if key, ok := m["key"].([]interface{}); ok {
						res = append(res, key...)
					}
					if key, ok := m["appendkey"].([]interface{}); ok {
						res = append(res, key...)
					}
				}
			}
		}
	}

	return
}

//GetVipKey 超级订阅：o_vipjy.a_items.a_key.key/appendkey
func GetVipKey(data map[string]interface{}) (res []interface{}) {
	//o_vipjy.a_items.a_key.key
	if o_vipjy, ok := data["o_vipjy"].(map[string]interface{}); ok {
		if aItems, ok := o_vipjy["a_items"].([]interface{}); ok {
			for _, aItem := range aItems {
				if a_k, ok := aItem.(map[string]interface{}); ok {
					if aKey, ok := a_k["a_key"].([]interface{}); ok {
						for _, ak := range aKey {
							if va, ok := ak.(map[string]interface{}); ok {
								if key, ok := va["key"].([]interface{}); ok {
									res = append(res, key...)
								}
								if key, ok := va["appendkey"].([]interface{}); ok {
									res = append(res, key...)
								}
							}
						}
					}
				}
			}
		}
	}

	return
}

//GetMemberKey 大会员订阅：o_member_jy.a_items.a_key.key/appendkey
func GetMemberKey(data map[string]interface{}) (res []interface{}) {
	//o_member_jy.a_items.a_key.key
	if o_member_jy, ok := data["o_member_jy"].(map[string]interface{}); ok {
		if aItems, ok := o_member_jy["a_items"].([]interface{}); ok {
			for _, aItem := range aItems {
				if tem, ok := aItem.(map[string]interface{}); ok {
					if aKey, ok := tem["a_key"].([]interface{}); ok {
						for _, ak := range aKey {
							if va, ok := ak.(map[string]interface{}); ok {
								if key, ok := va["key"].([]interface{}); ok {
									res = append(res, key...)
								}
								if key, ok := va["appendkey"].([]interface{}); ok {
									res = append(res, key...)
								}
							}
						}
					}
				}
			}
		}
	}

	return
}

//RemoveDuplicates 过滤重复数据
func RemoveDuplicates(elements []interface{}) []interface{} {
	encountered := map[interface{}]bool{}
	result := []interface{}{}
	for _, v := range elements {
		if encountered[v] == true {
			continue
		} else {
			encountered[v] = true
			result = append(result, v)
		}
	}
	return result
}

//GetUserKeys 获取用户订阅关键词
func GetUserKeys(data map[string]interface{}) (res []interface{}) {
	keys := GetJyKey(data)
	vkeys := GetVipKey(data)
	mkeys := GetMemberKey(data)

	res = append(res, keys...)
	res = append(res, vkeys...)
	res = append(res, mkeys...)
	res = RemoveDuplicates(res)

	return
}

func toString(i interface{}) string {
	return fmt.Sprintf("%v", i)
}

//getLastUser 获取最新的用户
func getLastUser(coll string) (user map[string]interface{}) {
	rs, _ := Mgo.Find(coll, nil, map[string]interface{}{"_id": -1}, nil, true, -1, 1)
	for _, v := range *rs {
		user = v
	}

	return
}

//BsonIdToSId 根据bsonID转string
func BsonIdToSId(uid interface{}) string {
	if uid == nil {
		return ""
	} else if u, ok := uid.(string); ok {
		return u
	} else if u, ok := uid.(primitive.ObjectID); ok {
		return u.Hex()
	} else {
		return ""
	}
}

//StringTOBsonId StringTOBsonId
func StringTOBsonId(id string) (bid primitive.ObjectID) {
	if id != "" {
		bid, _ = primitive.ObjectIDFromHex(id)
	} else {
		bid = primitive.NilObjectID
	}

	return
}

//SendUdpMsg 通知处理企业新增数据
func SendUdpMsg(data map[string]interface{}, target *net.UDPAddr) {
	bytes, _ := json.Marshal(data)
	UdpClient.WriteUdp(bytes, udp.OP_TYPE_DATA, target)
	wlog.Info("SendUdpMsg", wlog.Any("data", data))
	wlog.Info("SendUdpMsg", wlog.Any("target", target))
}
