go_dreamfactory/modules/sociaty/model_sociatylog.go
2022-10-31 18:27:07 +08:00

170 lines
4.4 KiB
Go

package sociaty
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"sort"
"strings"
"time"
"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 fmt.Errorf("参数和模板参数不匹配 期望:%d 实际%d", count, len(params))
}
for i := 0; i < len(params); i++ {
user, err := this.moduleSociaty.ModuleUser.GetRemoteUser(params[i])
if err == nil && user.Uid != "" {
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 fmt.Errorf("参数和模板参数不匹配 期望:%d 实际%d", count, len(params))
}
for i := 0; i < len(params); i++ {
user, err := this.moduleSociaty.ModuleUser.GetRemoteUser(params[i])
if err == nil && user.Uid != "" {
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 fmt.Errorf("参数和模板参数不匹配 期望:%d 实际%d", count, len(params))
}
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, err := this.moduleSociaty.ModuleUser.GetRemoteUser(params[i])
if err == nil && user.Uid != "" {
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 fmt.Errorf("参数和模板参数不匹配 期望:%d 实际%d", count, len(params))
}
for i := 0; i < len(params); i++ {
user, err := this.moduleSociaty.ModuleUser.GetRemoteUser(params[i])
if err == nil && user.Uid != "" {
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 fmt.Errorf("参数和模板参数不匹配 期望:%d 实际%d", count, len(params))
}
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: time.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: time.Now().Unix(),
})
update := map[string]interface{}{
"sociatyId": sociatyId,
"list": log.List,
}
return this.Change(sociatyId, update)
}
return nil
}
// 查询日志
func (this *ModelSociatyLog) logList(sociatyId string) (slist []*pb.SociatyLog) {
log := &pb.DBSociatyLog{}
if err := this.Get(sociatyId, log); err != nil {
return nil
}
for _, l := range log.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)
}