Executable
+21
@@ -0,0 +1,21 @@
|
||||
package signrecordmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// SignRecordInfo 移动端返回内容
|
||||
type SignRecordInfo struct {
|
||||
ID primitive.ObjectID `json:"id"` // 文档id
|
||||
PID primitive.ObjectID `json:"pid"` // 白嫖卡ID
|
||||
UID uint64 `json:"uid"` // 用户ID
|
||||
TotalDays int64 `json:"totalDays"` // 总打卡天数
|
||||
CurrentSignDays int64 `json:"currentSignDays"` // 连续打卡天数
|
||||
ForgetSignDays int64 `json:"forgetSignDays"` // 忘记打卡天数
|
||||
RenewalSignDays int64 `json:"renewalSignDays"` // 补签打卡天数
|
||||
SignTime time.Time `json:"signTime"` // 打卡时间
|
||||
CreatedAt time.Time `json:"createdAt"` // 创建时间
|
||||
UpdateTime time.Time `json:"updateTime"` // 更新时间
|
||||
}
|
||||
Executable
+227
@@ -0,0 +1,227 @@
|
||||
package signrecordmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// GetList 获取列表
|
||||
func GetList(cond bson.M, skip, limit int64, sort bson.D) (res []SignRecord, count int64, hasNext bool, err error) {
|
||||
count, err = coll(nil).Count(cond)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetList", table, "Count", err),
|
||||
log.Any("skip", skip),
|
||||
log.Any("limit", limit),
|
||||
log.Any("cond", cond),
|
||||
log.Any("sort", sort),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if len(sort) == 0 {
|
||||
sort = bson.D{{Key: "_id", Value: -1}}
|
||||
}
|
||||
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit + 1)
|
||||
if err = coll(nil).Find(&res, cond, opts); err != nil {
|
||||
return
|
||||
}
|
||||
// 判断下一页
|
||||
if len(res) > int(limit) {
|
||||
hasNext = true
|
||||
res = res[:limit]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetAll 查询全部文档
|
||||
func GetAll(filter primitive.M, sort bson.D) (out []*SignRecord, err error) {
|
||||
if len(sort) == 0 {
|
||||
sort = bson.D{{Key: "_id", Value: -1}}
|
||||
}
|
||||
opts := options.Find().SetSort(sort)
|
||||
if err = coll(nil).Find(&out, filter, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAll", table, "Find", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("opts", opts),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// QueryAllCount 查询文档条目数
|
||||
func QueryAllCount(filter primitive.M) (int64, error) {
|
||||
if count, err := coll(nil).Count(filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllCount", table, "Count", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return 0, err
|
||||
} else {
|
||||
return count, nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetInfo 通过id获取详细信息
|
||||
func GetInfo(id primitive.ObjectID) (SignRecord, error) {
|
||||
v := SignRecord{}
|
||||
if err := coll(nil).FindOne(&v, bson.M{"_id": id}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfo", table, "FindOne", err),
|
||||
log.Any("id", id),
|
||||
)
|
||||
return v, err
|
||||
}
|
||||
|
||||
if v.ID.IsZero() {
|
||||
return v, errors.New("record not found")
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// Insert 插入记录
|
||||
func Insert(t *db.MongoTool, d SignRecord) (data primitive.ObjectID, err error) {
|
||||
result, err := coll(t).InsertOne(&d)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
||||
return
|
||||
}
|
||||
byteID, err := json.Marshal(result.InsertedID)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "Marshal", err))
|
||||
return
|
||||
}
|
||||
if err = data.UnmarshalJSON(byteID); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "UnmarshalJSON", err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// InsertOne 插入记录
|
||||
func InsertOne(t *db.MongoTool, d SignRecord) (data primitive.ObjectID, err error) {
|
||||
filter := bson.M{"uid": d.UID}
|
||||
|
||||
updateCond := bson.M{"totalDays": d.TotalDays, "forgetSignDays": d.ForgetSignDays,
|
||||
"currentSignDays": d.CurrentSignDays, "endTime": d.EndTime, "signTime": d.SignTime,
|
||||
"updateTime": d.UpdateTime, "renewalSignDays": d.RenewalSignDays, "createdAt": d.CreatedAt,
|
||||
"recordTime": d.RecordTime, "pid": d.PID}
|
||||
|
||||
err = coll(t).FindOneAndUpsert(&d, filter, bson.M{"$set": updateCond})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOneAndUpdate", table, "InsertOne", err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateByID 根据id更新数据
|
||||
func UpdateByID(t *db.MongoTool, id primitive.ObjectID, data map[string]interface{}) (int64, error) {
|
||||
cond := bson.M{"_id": id}
|
||||
return update(t, cond, data)
|
||||
}
|
||||
|
||||
// update 更新数据
|
||||
func update(t *db.MongoTool, cond primitive.M, data map[string]interface{}) (int64, error) {
|
||||
result, err := coll(t).UpdateMany(cond, bson.M{"$set": bson.M(data)})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateMany", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("update", data),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
// DeleteByID 删除数据
|
||||
func DeleteByID(t *db.MongoTool, id primitive.ObjectID) error {
|
||||
_, err := coll(t).DeleteMany(bson.M{"_id": id})
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil
|
||||
} else {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByID", table, "DeleteMany", err), log.Any("id", id))
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetInfoByCond 根据条件查询文档
|
||||
func GetInfoByCond(filter primitive.M) (out *SignRecord, err error) {
|
||||
opts := options.FindOne()
|
||||
if err = coll(nil).FindOne(&out, filter, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfoByCond", table, "FindOne", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("opts", opts),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetInfoByUID 根据条件查询文档
|
||||
func GetInfoByUID(uid uint64) (out []*SignRecord, err error) {
|
||||
opts := options.Find()
|
||||
filter := bson.M{"uid": uid}
|
||||
if err = coll(nil).Find(&out, filter, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfoByUID", table, "Find", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("opts", opts),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CreditIncByCond 根据条件新增数据
|
||||
func CreditIncByCond(t *db.MongoTool, filter bson.M, cond bson.M) (*SignRecord, error) {
|
||||
w := SignRecord{}
|
||||
if err := coll(t).FindOneAndUpsert(&w, filter, bson.M{
|
||||
"$inc": cond,
|
||||
}); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "CreditIncByCond", table, "FindOneAndUpsert", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return &w, nil
|
||||
}
|
||||
|
||||
// UpdateDataByCond 根据条件新增数据
|
||||
func UpdateDataByCond(t *db.MongoTool, filter bson.M, cond bson.M) (*SignRecord, error) {
|
||||
w := SignRecord{}
|
||||
if err := coll(t).FindOneAndUpsert(&w, filter, cond); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "CreditIncByCond", table, "FindOneAndUpsert", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return &w, nil
|
||||
}
|
||||
|
||||
// UpdateByCond 更新数据
|
||||
func UpdateByCond(t *db.MongoTool, cond primitive.M, data primitive.M) (int64, error) {
|
||||
result, err := coll(t).UpdateOne(cond, bson.M{"$set": data})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateByCond", table, "UpdateOne", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("update", data),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
package signrecordmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.SignRecord
|
||||
|
||||
type SignRecord struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // 文档id
|
||||
PID primitive.ObjectID `json:"pid" bson:"pid"` // 白嫖卡ID
|
||||
UID uint64 `json:"uid" bson:"uid"` // 用户ID
|
||||
TotalDays int64 `json:"totalDays" bson:"totalDays"` // 总打卡天数
|
||||
CurrentSignDays int64 `json:"currentSignDays" bson:"currentSignDays"` // 连续打卡天数
|
||||
ForgetSignDays int64 `json:"forgetSignDays" bson:"forgetSignDays"` // 忘记打卡天数
|
||||
RenewalSignDays int64 `json:"renewalSignDays" bson:"renewalSignDays"` // 补签打卡天数
|
||||
HasReturn bool `json:"hasReturn" bson:"hasReturn"` // 是否返现
|
||||
RecordTime []Record `json:"recordTime" bson:"recordTime"` // 忘记打卡记录
|
||||
SignTime time.Time `json:"signTime" bson:"signTime"` // 打卡时间
|
||||
EndTime time.Time `json:"endTime" bson:"endTime"` // 打卡结束时间
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdateTime time.Time `json:"updateTime" bson:"updateTime"` // 更新时间
|
||||
}
|
||||
|
||||
type Record struct {
|
||||
SignTime string `json:"signTime" bson:"signTime"` // 打卡时间
|
||||
IsSign bool `json:"isSign" bson:"isSign"` // 是否打卡
|
||||
}
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// Init 初始化索引
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "pid", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package signrecordmod
|
||||
Reference in New Issue
Block a user