go_dreamfactory/modules/sociaty/model_sociatylog.go
2023-04-13 18:41:34 +08:00

187 lines
4.7 KiB
Go

package sociaty
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"sort"
"strings"
"go.mongodb.org/mongo-driver/mongo"
)
type ModelSociatyLog struct {
modules.MCompModel
moduleSociaty *Sociaty
service core.IService
}
func (this *ModelSociatyLog) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableSociatyLog
err = this.MCompModel.Init(service, module, comp, options)
this.moduleSociaty = module.(*Sociaty)
this.service = service
return
}
// 记录日志
// tag 日志模板 sociatyId公会ID params 占位参数
func (this *ModelSociatyLog) addLog(tag Tag, sociatyId string, params ...string) error {
var content string
//日志 template
switch tag {
case Log_Add:
content = string(Log_Add)
count := strings.Count(content, "%s")
if count != len(params) {
return comm.NewCustomError(pb.ErrorCode_SociatyLogParams)
}
for i := 0; i < len(params); i++ {
user := this.moduleSociaty.ModuleUser.GetUser(params[i])
if user != nil {
content = strings.Replace(content, "%s", user.Name, 1)
}
}
case Log_Quit:
content = string(Log_Quit)
count := strings.Count(content, "%s")
if count != len(params) {
return comm.NewCustomError(pb.ErrorCode_SociatyLogParams)
}
for i := 0; i < len(params); i++ {
user := this.moduleSociaty.ModuleUser.GetUser(params[i])
if user != nil {
content = strings.Replace(content, "%s", user.Name, 1)
}
}
case Log_Job:
content = string(Log_Job)
count := strings.Count(content, "%s")
if count != len(params) {
return comm.NewCustomError(pb.ErrorCode_SociatyLogParams)
}
for i := 0; i < len(params); i++ {
if i == 2 {
var job string
switch params[i] {
case pb.SociatyJob_PRESIDENT.String():
job = "会长"
case pb.SociatyJob_VICEPRESIDENT.String():
job = "副会长"
case pb.SociatyJob_ADMIN.String():
job = "管理员"
case pb.SociatyJob_MEMBER.String():
job = "成员"
}
content = strings.Replace(content, "%s", job, 1)
} else {
user := this.moduleSociaty.ModuleUser.GetUser(params[i])
if user != nil {
content = strings.Replace(content, "%s", user.Name, 1)
}
}
}
case Log_Discharge:
content = string(Log_Job)
count := strings.Count(content, "%s")
if count != len(params) {
return comm.NewCustomError(pb.ErrorCode_SociatyLogParams)
}
for i := 0; i < len(params); i++ {
user := this.moduleSociaty.ModuleUser.GetUser(params[i])
if user != nil {
content = strings.Replace(content, "%s", params[i], 1)
}
}
case Log_Upgrade:
content = string(Log_Job)
count := strings.Count(content, "%s")
if count != len(params) {
return comm.NewCustomError(pb.ErrorCode_SociatyLogParams)
}
for i := 0; i < len(params); i++ {
content = strings.Replace(content, "%s", params[i], 1)
}
}
if content != "" {
log := &pb.DBSociatyLog{}
if err := this.Get(sociatyId, log); err != nil {
if err == mongo.ErrNoDocuments {
var list []*pb.SociatyLog
list = append(list, &pb.SociatyLog{
Content: content,
Ctime: configure.Now().Unix(),
})
err = this.Add(sociatyId, &pb.DBSociatyLog{
SociatyId: sociatyId,
List: list,
})
if err != nil {
return err
}
return nil
}
return err
}
//update
if log.List == nil {
log.List = []*pb.SociatyLog{}
}
log.List = append(log.List, &pb.SociatyLog{
Content: content,
Ctime: configure.Now().Unix(),
})
update := map[string]interface{}{
"sociatyId": sociatyId,
"list": log.List,
}
if err := this.Change(sociatyId, update); err != nil {
return err
}
// 发消息到公会聊天
if module, err := this.service.GetModule(comm.ModuleChat); err == nil {
if chat, ok := module.(comm.IChat); ok {
chat.SendUnionChat(&pb.DBChat{
Channel: pb.ChatChannel_Union,
Ctype: pb.ChatType_Text,
UnionId: sociatyId,
Content: content,
})
}
}
}
return nil
}
// 查询日志
func (this *ModelSociatyLog) logList(sociatyId string) (slist []*pb.SociatyLog) {
sociatyLog := &pb.DBSociatyLog{}
if err := this.Get(sociatyId, sociatyLog); err != nil {
log.Error("公会日志列表", log.Field{Key: "sociatyId", Value: sociatyId},
log.Field{Key: "err", Value: err})
return nil
}
for _, l := range sociatyLog.List {
slist = append(slist, &pb.SociatyLog{
Content: l.Content,
Ctime: l.Ctime,
})
}
// 创建时间排序 降序
sort.SliceStable(slist, func(i, j int) bool {
return slist[i].Ctime > slist[j].Ctime
})
return
}
// 删除日志
func (this *ModelSociatyLog) logDelete(sociatyId string) error {
return this.Del(sociatyId)
}