190 lines
6.2 KiB
Go
190 lines
6.2 KiB
Go
package modules
|
|
|
|
import (
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/core/cbase"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/sys/db"
|
|
"log"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
/*
|
|
基础组件 缓存组件 读写缓存数据
|
|
DB组件也封装进来
|
|
*/
|
|
type MCompModel struct {
|
|
cbase.ModuleCompBase
|
|
TableName string
|
|
Expired time.Duration //过期时间
|
|
Redis redis.ISys
|
|
DB mgo.ISys
|
|
DBModel *db.DBModel
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *MCompModel) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.ModuleCompBase.Init(service, module, comp, options)
|
|
var conn *db.DBConn
|
|
if conn, err = db.Local(); err != nil {
|
|
return
|
|
}
|
|
this.DB = conn.Mgo
|
|
this.Redis = conn.Redis
|
|
this.Expired = time.Hour
|
|
return
|
|
}
|
|
|
|
func (this *MCompModel) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
if this.TableName == "" {
|
|
log.Panicf("TableName is nil")
|
|
return
|
|
}
|
|
var conn *db.DBConn
|
|
if conn, err = db.Local(); err != nil {
|
|
return
|
|
}
|
|
this.DBModel = db.NewDBModel(this.TableName, conn)
|
|
return
|
|
}
|
|
|
|
func (this *MCompModel) InsertModelLogs(table string, uID string, target interface{}) (err error) {
|
|
return this.DBModel.InsertModelLogs(table, uID, target)
|
|
}
|
|
func (this *MCompModel) DeleteModelLogs(table string, uID string, where interface{}) (err error) {
|
|
return this.DBModel.DeleteModelLogs(table, uID, where)
|
|
}
|
|
|
|
func (this *MCompModel) UpdateModelLogs(table string, uID string, where bson.M, target interface{}) (err error) {
|
|
return this.DBModel.UpdateModelLogs(table, uID, where, target)
|
|
}
|
|
|
|
// 创建分布式锁
|
|
func (this *MCompModel) NewRedisMutex(key string, outtime int) (result *redis.RedisMutex, err error) {
|
|
return this.DBModel.NewRedisMutex(key, outtime)
|
|
}
|
|
|
|
// 添加新的数据
|
|
func (this *MCompModel) Add(uid string, data interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.Add(uid, data)
|
|
}
|
|
|
|
// 添加新的数据
|
|
func (this *MCompModel) Adds(datas map[string]interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.Adds(datas)
|
|
}
|
|
|
|
// 添加新的数据到列表
|
|
func (this *MCompModel) AddList(uid string, id string, data interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.AddList(uid, id, data, opt...)
|
|
}
|
|
|
|
// 添加新的多个数据到列表 data map[string]type
|
|
func (this *MCompModel) AddLists(uid string, data interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.AddLists(uid, data, opt...)
|
|
}
|
|
|
|
// 添加新的多个数据到队列中 data map[string]type
|
|
func (this *MCompModel) AddQueues(key string, uplimit int64, data interface{}) (outkey []string, err error) {
|
|
return this.DBModel.AddQueues(key, uplimit, data)
|
|
}
|
|
|
|
// 修改数据多个字段 uid 作为主键
|
|
func (this *MCompModel) Change(uid string, data map[string]interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.Change(uid, data, opt...)
|
|
}
|
|
|
|
// 修改数据多个字段 id 作为主键
|
|
func (this *MCompModel) ChangeById(id string, data map[string]interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.ChangeById(id, data, opt...)
|
|
}
|
|
|
|
// 修改数据多个字段 uid 作为主键
|
|
func (this *MCompModel) ChangeList(uid string, _id string, data map[string]interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.ChangeList(uid, _id, data, opt...)
|
|
}
|
|
|
|
// 修改列表中多个数据 datas key是 _id value是 这个数据对象
|
|
func (this *MCompModel) ChangeLists(uid string, datas map[string]interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.ChangeLists(uid, datas, opt...)
|
|
}
|
|
|
|
// 读取全部数据
|
|
func (this *MCompModel) Get(id string, data interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.Get(id, data, opt...)
|
|
}
|
|
|
|
// 读取全部数据
|
|
func (this *MCompModel) GetByID(id string, data interface{}, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.GetByID(id, data, opt...)
|
|
}
|
|
|
|
// 读取多个数据对象
|
|
func (this *MCompModel) Gets(ids []string, data interface{}, opt ...db.DBOption) (onfound []string, err error) {
|
|
return this.DBModel.Gets(ids, data, opt...)
|
|
}
|
|
|
|
// 读取多个数据对象 by 用户id
|
|
func (this *MCompModel) GetByUids(uids []string, data interface{}, opt ...db.DBOption) (onfound []string, err error) {
|
|
return this.DBModel.GetByUids(uids, data, opt...)
|
|
}
|
|
|
|
// 获取列表数据 注意 data 必须是 切片的指针 *[]type
|
|
func (this *MCompModel) GetList(uid string, data interface{}) (err error) {
|
|
return this.DBModel.GetList(uid, data)
|
|
}
|
|
|
|
// 查询队列信息
|
|
func (this *MCompModel) GetQueues(key string, count int, data interface{}) (err error) {
|
|
return this.DBModel.GetQueues(key, count, data)
|
|
}
|
|
|
|
// 读取单个数据中 多个字段数据
|
|
func (this *MCompModel) GetFields(uid string, data interface{}, fields ...string) (err error) {
|
|
return this.DBModel.GetFields(uid, data, fields...)
|
|
}
|
|
|
|
// 读取List列表中单个数据中 多个字段数据
|
|
func (this *MCompModel) GetListFields(uid string, id string, data interface{}, fields ...string) (err error) {
|
|
return this.DBModel.GetListFields(uid, id, data, fields...)
|
|
}
|
|
|
|
// 读取列表数据中单个数据
|
|
func (this *MCompModel) GetListObj(uid string, id string, data interface{}) (err error) {
|
|
return this.DBModel.GetListObj(uid, id, data)
|
|
}
|
|
|
|
// 批量读取列表中多个数据
|
|
func (this *MCompModel) GetListObjs(uid string, ids []string, data interface{}) (err error) {
|
|
return this.DBModel.GetListObjs(uid, ids, data)
|
|
}
|
|
|
|
// 批量读取列表中多个数据 只在redis中寻找
|
|
func (this *MCompModel) GetRedisListObjs(uid string, ids []string, data interface{}) (foundids []string, err error) {
|
|
return this.DBModel.GetRedisListObjs(uid, ids, data)
|
|
}
|
|
|
|
// 删除目标数据
|
|
func (this *MCompModel) Del(id string, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.Del(id, opt...)
|
|
}
|
|
|
|
// 删除用户数据
|
|
func (this *MCompModel) DelByUId(uid string, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.DelByUId(uid, opt...)
|
|
}
|
|
|
|
// 删除多条数据
|
|
func (this *MCompModel) DelListlds(uid string, ids []string, opt ...db.DBOption) (err error) {
|
|
return this.DBModel.DelListlds(uid, ids, opt...)
|
|
}
|
|
|
|
// 批量删除数据
|
|
func (this *MCompModel) BatchDelLists(uid string) (err error) {
|
|
return this.DBModel.BatchDelLists(uid)
|
|
}
|