137 lines
5.6 KiB
Go
137 lines
5.6 KiB
Go
package sys
|
|
|
|
import (
|
|
"context"
|
|
|
|
"hotgo/internal/consts"
|
|
"hotgo/internal/dao"
|
|
"hotgo/internal/library/contexts"
|
|
"hotgo/internal/library/hgorm/handler"
|
|
"hotgo/internal/library/hgorm/hook"
|
|
"hotgo/internal/model/entity"
|
|
"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"
|
|
)
|
|
|
|
type sSysCustomerService struct{}
|
|
|
|
func NewSysCustomerService() *sSysCustomerService { return &sSysCustomerService{} }
|
|
func init() { service.RegisterSysCustomerService(NewSysCustomerService()) }
|
|
func (s *sSysCustomerService) model(ctx context.Context) *gdb.Model {
|
|
return handler.Model(dao.CustomerService.Ctx(ctx), &handler.Option{FilterTenant: true})
|
|
}
|
|
|
|
func (s *sSysCustomerService) List(ctx context.Context, in *sysin.CustomerServiceListInp) (list []*entity.CustomerService, total int, err error) {
|
|
c := dao.CustomerService.Columns()
|
|
mod := s.model(ctx)
|
|
if in.Name != "" {
|
|
mod = mod.WhereLike(c.Name, "%"+in.Name+"%")
|
|
}
|
|
if in.Status > 0 {
|
|
mod = mod.Where(c.Status, in.Status)
|
|
}
|
|
err = mod.Page(in.Page, in.PerPage).OrderAsc(c.Sort).OrderDesc(c.Id).ScanAndCount(&list, &total, false)
|
|
if err != nil {
|
|
err = gerror.Wrap(err, "获取客服列表失败")
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *sSysCustomerService) View(ctx context.Context, in *sysin.CustomerServiceViewInp) (res *entity.CustomerService, err error) {
|
|
if err = s.model(ctx).WherePri(in.Id).Scan(&res); err != nil {
|
|
return nil, gerror.Wrap(err, "获取客服详情失败")
|
|
}
|
|
if res == nil {
|
|
return nil, gerror.New("客服配置不存在")
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *sSysCustomerService) Edit(ctx context.Context, in *sysin.CustomerServiceEditInp) error {
|
|
c := dao.CustomerService.Columns()
|
|
var before *entity.CustomerService
|
|
if in.Id > 0 {
|
|
if err := s.model(ctx).WherePri(in.Id).Scan(&before); err != nil {
|
|
return err
|
|
}
|
|
if before == nil {
|
|
return gerror.New("客服配置不存在")
|
|
}
|
|
}
|
|
count, err := s.model(ctx).Where(c.ServiceCode, in.ServiceCode).WhereNot(c.Id, in.Id).Count()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
return gerror.New("客服编码已存在")
|
|
}
|
|
if in.ImUserId != "" {
|
|
count, err = s.model(ctx).Where(c.ImTenantCode, in.ImTenantCode).Where(c.ImUserId, in.ImUserId).WhereNot(c.Id, in.Id).Count()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
return gerror.New("IM客服账号已被其他席位绑定")
|
|
}
|
|
}
|
|
data := g.Map{c.ServiceCode: in.ServiceCode, c.Name: in.Name, c.AvatarUrl: in.AvatarUrl, c.WorkTime: in.WorkTime, c.OnlineStatus: in.OnlineStatus, c.ServiceUrl: in.ServiceUrl, c.BackupContact: in.BackupContact, c.SessionMapping: in.SessionMapping, c.ImTenantCode: in.ImTenantCode, c.ImUserId: gdb.Raw("NULL"), c.ImUsername: gdb.Raw("NULL"), c.Status: in.Status, c.Sort: in.Sort, c.Remark: in.Remark, c.UpdatedBy: contexts.GetUserId(ctx)}
|
|
if in.ImUserId != "" {
|
|
data[c.ImUserId] = in.ImUserId
|
|
}
|
|
if in.ImUsername != "" {
|
|
data[c.ImUsername] = in.ImUsername
|
|
}
|
|
if in.Id > 0 {
|
|
if _, err = s.model(ctx).WherePri(in.Id).Data(data).Update(); err != nil {
|
|
return gerror.Wrap(err, "更新客服配置失败")
|
|
}
|
|
} else {
|
|
data[c.CreatedBy] = contexts.GetUserId(ctx)
|
|
result, e := s.model(ctx).Fields(c.ServiceCode, c.Name, c.AvatarUrl, c.WorkTime, c.OnlineStatus, c.ServiceUrl, c.BackupContact, c.SessionMapping, c.ImTenantCode, c.ImUserId, c.ImUsername, c.Status, c.Sort, c.Remark, c.CreatedBy, c.UpdatedBy).Hook(hook.SaveTenant).Data(data).Insert()
|
|
if e != nil {
|
|
return gerror.Wrap(e, "创建客服配置失败")
|
|
}
|
|
id, _ := result.LastInsertId()
|
|
in.Id = uint64(id)
|
|
}
|
|
return service.SysBizAudit().Record(ctx, &sysin.BizAuditRecordInp{Module: "customer_service", Action: "customer_service.edit", TargetType: "customer_service", TargetId: in.Id, Before: before, After: g.Map{"name": in.Name, "workTime": in.WorkTime, "onlineStatus": in.OnlineStatus, "serviceUrl": in.ServiceUrl, "imTenantCode": in.ImTenantCode, "imUserId": in.ImUserId, "status": in.Status}, Reason: in.Remark})
|
|
}
|
|
|
|
func (s *sSysCustomerService) Status(ctx context.Context, in *sysin.CustomerServiceStatusInp) error {
|
|
c := dao.CustomerService.Columns()
|
|
var row *entity.CustomerService
|
|
if err := s.model(ctx).WherePri(in.Id).Scan(&row); err != nil {
|
|
return err
|
|
}
|
|
if row == nil {
|
|
return gerror.New("客服配置不存在")
|
|
}
|
|
if _, err := s.model(ctx).WherePri(in.Id).Data(g.Map{c.Status: in.Status, c.UpdatedBy: contexts.GetUserId(ctx)}).Update(); err != nil {
|
|
return gerror.Wrap(err, "更新客服状态失败")
|
|
}
|
|
return service.SysBizAudit().Record(ctx, &sysin.BizAuditRecordInp{Module: "customer_service", Action: "customer_service.status", TargetType: "customer_service", TargetId: in.Id, Before: g.Map{"status": row.Status}, After: g.Map{"status": in.Status}})
|
|
}
|
|
|
|
func (s *sSysCustomerService) Delete(ctx context.Context, in *sysin.CustomerServiceDeleteInp) error {
|
|
c := dao.CustomerService.Columns()
|
|
var row *entity.CustomerService
|
|
if err := s.model(ctx).WherePri(in.Id).Scan(&row); err != nil {
|
|
return err
|
|
}
|
|
if row == nil {
|
|
return gerror.New("客服配置不存在")
|
|
}
|
|
if row.Status == consts.StatusEnabled {
|
|
return gerror.New("启用中的客服不能删除,请先停用")
|
|
}
|
|
if _, err := s.model(ctx).WherePri(in.Id).Data(g.Map{c.DeletedAt: gtime.Now(), c.DeletedBy: contexts.GetUserId(ctx), c.UpdatedBy: contexts.GetUserId(ctx)}).Update(); err != nil {
|
|
return gerror.Wrap(err, "删除客服配置失败")
|
|
}
|
|
return service.SysBizAudit().Record(ctx, &sysin.BizAuditRecordInp{Module: "customer_service", Action: "customer_service.delete", TargetType: "customer_service", TargetId: in.Id, Before: row, Reason: in.Reason})
|
|
}
|