167 lines
6.3 KiB
Go
167 lines
6.3 KiB
Go
package sys
|
|
|
|
import (
|
|
"context"
|
|
"hotgo/internal/consts"
|
|
"hotgo/internal/model/input/sysin"
|
|
"hotgo/internal/service"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
)
|
|
|
|
const imAbuseReasonsTable = "im_abuse_reasons"
|
|
|
|
type sSysImAbuseReason struct{}
|
|
|
|
func NewSysImAbuseReason() *sSysImAbuseReason { return &sSysImAbuseReason{} }
|
|
|
|
func init() {
|
|
service.RegisterSysImAbuseReason(NewSysImAbuseReason())
|
|
}
|
|
|
|
func (s *sSysImAbuseReason) List(ctx context.Context, in *sysin.ImAbuseReasonListInp) (list []*sysin.ImAbuseReasonModel, totalCount int, err error) {
|
|
if err = in.Filter(ctx); err != nil {
|
|
return
|
|
}
|
|
db, tenant, err := resolveImTenantDB(ctx, in.TenantId)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err = s.ensureDefaults(ctx, db); err != nil {
|
|
return
|
|
}
|
|
mod := s.model(ctx, db)
|
|
if in.TargetType != "" {
|
|
mod = mod.Where("target_type", in.TargetType)
|
|
}
|
|
if in.ReasonCode != "" {
|
|
mod = mod.WhereLike("reason_code", "%"+in.ReasonCode+"%")
|
|
}
|
|
if in.Enabled != nil {
|
|
mod = mod.Where("enabled", *in.Enabled)
|
|
}
|
|
mod = mod.Page(in.Page, in.PerPage).OrderAsc("target_type").OrderAsc("sort_order").OrderAsc("id")
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取IM举报原因失败,请稍后重试!")
|
|
return
|
|
}
|
|
for _, item := range list {
|
|
item.TenantId = tenant.Id
|
|
item.TenantName = tenant.Name
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *sSysImAbuseReason) Edit(ctx context.Context, in *sysin.ImAbuseReasonEditInp) (err error) {
|
|
if err = in.Filter(ctx); err != nil {
|
|
return
|
|
}
|
|
db, tenant, err := resolveImTenantDB(ctx, in.TenantId)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err = s.ensureDefaults(ctx, db); err != nil {
|
|
return
|
|
}
|
|
data := g.Map{
|
|
"target_type": in.TargetType,
|
|
"reason_code": in.ReasonCode,
|
|
"label": in.Label,
|
|
"requires_description": in.RequiresDescription,
|
|
"allows_evidence": in.AllowsEvidence,
|
|
"sort_order": in.SortOrder,
|
|
"enabled": in.Enabled,
|
|
"updated_at": gtime.Now(),
|
|
}
|
|
action := "im_abuse_reason_create"
|
|
targetId := in.Id
|
|
if in.Id > 0 {
|
|
if _, err = db.Model(imAbuseReasonsTable).Safe().Ctx(ctx).WherePri(in.Id).Data(data).Update(); err != nil {
|
|
return gerror.Wrap(err, "更新IM举报原因失败,请稍后重试!")
|
|
}
|
|
action = "im_abuse_reason_update"
|
|
} else {
|
|
data["created_at"] = gtime.Now()
|
|
if _, err = db.Model(imAbuseReasonsTable).Safe().Ctx(ctx).Data(data).Insert(); err != nil {
|
|
return gerror.Wrap(err, "新增IM举报原因失败,请稍后重试!")
|
|
}
|
|
idValue, err := db.Model(imAbuseReasonsTable).Safe().Ctx(ctx).Fields("id").
|
|
Where("target_type", in.TargetType).Where("reason_code", in.ReasonCode).Value()
|
|
if err != nil {
|
|
return gerror.Wrap(err, "获取IM举报原因ID失败,请稍后重试!")
|
|
}
|
|
targetId = idValue.Int64()
|
|
}
|
|
_ = writeImAdminAudit(ctx, tenant.Id, action, "im_abuse_reason", targetId, "success", g.Map{
|
|
"target_type": in.TargetType, "reason_code": in.ReasonCode, "enabled": in.Enabled,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (s *sSysImAbuseReason) Delete(ctx context.Context, in *sysin.ImAbuseReasonDeleteInp) (err error) {
|
|
if err = in.Filter(ctx); err != nil {
|
|
return
|
|
}
|
|
db, tenant, err := resolveImTenantDB(ctx, in.TenantId)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if _, err = db.Model(imAbuseReasonsTable).Safe().Ctx(ctx).WherePri(in.Id).
|
|
Data(g.Map{"enabled": 0, "updated_at": gtime.Now()}).Update(); err != nil {
|
|
return gerror.Wrap(err, "禁用IM举报原因失败,请稍后重试!")
|
|
}
|
|
_ = writeImAdminAudit(ctx, tenant.Id, "im_abuse_reason_disable", "im_abuse_reason", in.Id, "success", nil)
|
|
return nil
|
|
}
|
|
|
|
func (s *sSysImAbuseReason) model(ctx context.Context, db gdb.DB) *gdb.Model {
|
|
return db.Model(imAbuseReasonsTable).Safe().Ctx(ctx).Fields(
|
|
"id",
|
|
"target_type",
|
|
"reason_code",
|
|
"label",
|
|
"requires_description",
|
|
"allows_evidence",
|
|
"sort_order",
|
|
"enabled",
|
|
"created_at",
|
|
"updated_at",
|
|
)
|
|
}
|
|
|
|
func (s *sSysImAbuseReason) ensureDefaults(ctx context.Context, db gdb.DB) (err error) {
|
|
defaults := []g.Map{
|
|
{"target_type": consts.ImAbuseTargetTypeUser, "reason_code": "fraud", "label": "诈骗欺诈", "requires_description": 0, "allows_evidence": 1, "sort_order": 10},
|
|
{"target_type": consts.ImAbuseTargetTypeUser, "reason_code": "harassment", "label": "骚扰辱骂", "requires_description": 0, "allows_evidence": 1, "sort_order": 20},
|
|
{"target_type": consts.ImAbuseTargetTypeUser, "reason_code": "illegal", "label": "违法违规", "requires_description": 1, "allows_evidence": 1, "sort_order": 30},
|
|
{"target_type": consts.ImAbuseTargetTypeGroup, "reason_code": "fraud", "label": "诈骗欺诈", "requires_description": 0, "allows_evidence": 1, "sort_order": 10},
|
|
{"target_type": consts.ImAbuseTargetTypeGroup, "reason_code": "spam", "label": "垃圾广告", "requires_description": 0, "allows_evidence": 1, "sort_order": 20},
|
|
{"target_type": consts.ImAbuseTargetTypeGroup, "reason_code": "illegal", "label": "违法违规", "requires_description": 1, "allows_evidence": 1, "sort_order": 30},
|
|
{"target_type": consts.ImAbuseTargetTypeMessage, "reason_code": "fraud", "label": "诈骗欺诈", "requires_description": 0, "allows_evidence": 1, "sort_order": 10},
|
|
{"target_type": consts.ImAbuseTargetTypeMessage, "reason_code": "spam", "label": "垃圾广告", "requires_description": 0, "allows_evidence": 1, "sort_order": 20},
|
|
{"target_type": consts.ImAbuseTargetTypeMessage, "reason_code": "harassment", "label": "骚扰辱骂", "requires_description": 0, "allows_evidence": 1, "sort_order": 30},
|
|
{"target_type": consts.ImAbuseTargetTypeMessage, "reason_code": "illegal", "label": "违法违规", "requires_description": 1, "allows_evidence": 1, "sort_order": 40},
|
|
}
|
|
now := gtime.Now()
|
|
for _, item := range defaults {
|
|
var count int
|
|
if count, err = db.Model(imAbuseReasonsTable).Safe().Ctx(ctx).
|
|
Where("target_type", item["target_type"]).Where("reason_code", item["reason_code"]).Count(); err != nil {
|
|
return gerror.Wrap(err, "检查IM举报默认原因失败,请稍后重试!")
|
|
}
|
|
if count > 0 {
|
|
continue
|
|
}
|
|
item["enabled"] = 1
|
|
item["created_at"] = now
|
|
item["updated_at"] = now
|
|
if _, err = db.Model(imAbuseReasonsTable).Safe().Ctx(ctx).Data(item).Insert(); err != nil {
|
|
return gerror.Wrap(err, "初始化IM举报默认原因失败,请稍后重试!")
|
|
}
|
|
}
|
|
return nil
|
|
}
|