You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.4 KiB

package internal
import (
"context"
"encoding/json"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/qiniu/qmgo"
"go.mongodb.org/mongo-driver/bson"
"strings"
"tyj_admin/internal/model/entity"
)
func GetOrdersByAccount(ctx context.Context, account string) (orders []*entity.Order, err error) {
bm := bson.M{"uid": account}
err = MongoDatabaseList[0].Collection("rechargeOrder").Find(ctx, bm).All(&orders)
return
}
func GetOrdersByTime(ctx context.Context, lowTime int64, upperTime int64) (orders []*entity.Order, err error) {
bm := bson.M{"timestamp": bson.M{"$gte": lowTime, "$lte": upperTime}}
err = MongoDatabaseList[0].Collection("rechargeOrder").Find(ctx, bm).All(&orders)
return
}
func GetOrdersByOrderId(ctx context.Context, orderId string) (orders []*entity.Order, err error) {
bm := bson.M{"_id": orderId}
err = MongoDatabaseList[0].Collection("rechargeOrder").Find(ctx, bm).All(&orders)
return
}
func GetOrderCount(ctx context.Context, filter bson.M, serverId int) (Orders []*entity.OrderCount, err error) {
if serverId == 0 {
for i := 1; i < len(MongoDatabaseList); i++ {
orderCount := new(entity.OrderCount)
orderCount.ServerId = i
serverMatch := bson.D{{"$match", bson.M{"server": i}}}
matchStage := bson.D{{"$match", filter}}
groupStage := bson.D{{"group", bson.M{"uid": `$uid`, "server": `$server`}}}
var in []bson.M
MongoDatabaseList[0].Collection("rechargeOrder").Aggregate(ctx, qmgo.Pipeline{serverMatch, matchStage, groupStage}).All(&in)
orderCount.Num = 1
groupStage = bson.D{{"group", bson.M{"totalMoney": bson.M{"$sum": "$money"}, "times": bson.M{"$sum": 1}}}}
var in2 []bson.M
MongoDatabaseList[0].Collection("rechargeOrder").Aggregate(ctx, qmgo.Pipeline{serverMatch, groupStage}).All(&in2)
}
} else {
orderCount := new(entity.OrderCount)
orderCount.ServerId = serverId
serverMatch := bson.D{{"$match", bson.M{"server": serverId}}}
matchStage := bson.D{{"$match", filter}}
groupStage := bson.D{{"group", bson.M{"uid": `$uid`, "server": `$server`}}}
var in []bson.M
MongoDatabaseList[0].Collection("rechargeOrder").Aggregate(ctx, qmgo.Pipeline{serverMatch, matchStage, groupStage}).All(&in)
orderCount.Num = 1
groupStage = bson.D{{"group", bson.M{"totalMoney": bson.M{"$sum": "$money"}, "times": bson.M{"$sum": 1}}}}
var in2 []bson.M
MongoDatabaseList[0].Collection("rechargeOrder").Aggregate(ctx, qmgo.Pipeline{serverMatch, groupStage}).All(&in2)
}
return
}
type MoneyBody struct {
Uids string `json:"uids"`
Money int32 `json:"money"`
Type int32 `json:"type"`
ModuleType string `json:"moduleType"`
}
type MqttChangeMoney struct {
ReqId int64 `json:"reqId"`
ModuleId string `json:"moduleId"`
Body MoneyBody `json:"body"`
}
func SendMqttMoney(msg interface{}, c chan bool, server string) {
registerFunc := func(client mqtt.Client, qtmsg mqtt.Message) {
ss := string(msg.([]byte))
fmt.Println(ss)
err := ClientSend("client", 0, false, ss)
if err != nil {
c <- false
}
}
mailFunc := func(client mqtt.Client, qtmsg mqtt.Message) {
res := &MqttResult{}
client.Disconnect(1)
ss := string(qtmsg.Payload())
ss = ss[1 : len(ss)-1]
ss = strings.Replace(ss, "\\", "", -1)
err := json.Unmarshal([]byte(ss), res)
if err == nil && res.RespId == 1 && res.Error == nil {
c <- true
return
}
c <- false
}
if NewMqttClient(registerFunc, mailFunc, server) != nil {
c <- false
}
}