go_dreamfactory/modules/comp_model.go
2022-08-09 18:39:21 +08:00

124 lines
4.0 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"
"go.mongodb.org/mongo-driver/bson"
)
/*
基础组件 缓存组件 读写缓存数据
DB组件也封装进来
*/
type MCompModel struct {
cbase.ModuleCompBase
TableName string
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)
this.DB = db.Local().Mgo
this.Redis = db.Local().Redis
if this.TableName == "" {
log.Panicf("TableName is nil")
return
}
this.DBModel = db.NewDBModel(this.TableName, db.Local())
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) Add(uid string, data interface{}, opt ...db.DBOption) (err error) {
return this.DBModel.Add(uid, data)
}
//添加新的数据到列表
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...)
}
//修改数据多个字段 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...)
}
//读取全部数据
func (this *MCompModel) Get(uid string, data interface{}, opt ...db.DBOption) (err error) {
return this.DBModel.Get(uid, data, opt...)
}
//获取列表数据 注意 data 必须是 切片的指针 *[]type
func (this *MCompModel) GetList(uid string, data interface{}) (err error) {
return this.DBModel.GetFields(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) Del(uid string, opt ...db.DBOption) (err error) {
return this.DBModel.Del(uid, opt...)
}
//删除多条数据
func (this *MCompModel) DelListlds(uid string, ids ...string) (err error) {
return this.DBModel.DelListlds(uid, ids...)
}
//批量删除数据
func (this *MCompModel) BatchDelLists(uid string) (err error) {
return this.DBModel.BatchDelLists(uid)
}