f
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
tianyuanapi "qnc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminQueryWhitelistAppendLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminQueryWhitelistAppendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminQueryWhitelistAppendLogic {
|
||||
return &AdminQueryWhitelistAppendLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminQueryWhitelistAppendLogic) AdminQueryWhitelistAppend(req *types.AdminQueryWhitelistAppendReq) (resp *types.AdminQueryWhitelistOpResp, err error) {
|
||||
createReq := &types.AdminQueryWhitelistCreateReq{
|
||||
Name: req.Name,
|
||||
IdCard: req.IdCard,
|
||||
ApiCodes: req.ApiCodes,
|
||||
Remark: req.Remark,
|
||||
}
|
||||
return executeQueryWhitelistOp(l.ctx, l.svcCtx, "append", createReq, func(client *tianyuanapi.Client, payload tianyuanapi.QueryWhitelistRequest) *tianyuanapi.QueryWhitelistResult {
|
||||
return client.AppendQueryWhitelistEntry(payload)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
tianyuanapi "qnc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminQueryWhitelistCreateLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminQueryWhitelistCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminQueryWhitelistCreateLogic {
|
||||
return &AdminQueryWhitelistCreateLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminQueryWhitelistCreateLogic) AdminQueryWhitelistCreate(req *types.AdminQueryWhitelistCreateReq) (resp *types.AdminQueryWhitelistOpResp, err error) {
|
||||
return executeQueryWhitelistOp(l.ctx, l.svcCtx, "create", req, func(client *tianyuanapi.Client, payload tianyuanapi.QueryWhitelistRequest) *tianyuanapi.QueryWhitelistResult {
|
||||
return client.CreateQueryWhitelistEntry(payload)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/common/globalkey"
|
||||
"qnc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/mr"
|
||||
)
|
||||
|
||||
type AdminQueryWhitelistOpLogListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminQueryWhitelistOpLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminQueryWhitelistOpLogListLogic {
|
||||
return &AdminQueryWhitelistOpLogListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminQueryWhitelistOpLogListLogic) AdminQueryWhitelistOpLogList(req *types.AdminQueryWhitelistOpLogListReq) (resp *types.AdminQueryWhitelistOpLogListResp, err error) {
|
||||
builder := l.svcCtx.QueryWhitelistOpLogModel.SelectBuilder().
|
||||
Where("del_state = ?", globalkey.DelStateNo)
|
||||
|
||||
if req.IdCard != nil && *req.IdCard != "" {
|
||||
builder = builder.Where("id_card = ?", *req.IdCard)
|
||||
}
|
||||
if req.Action != nil && *req.Action != "" {
|
||||
builder = builder.Where("action = ?", *req.Action)
|
||||
}
|
||||
if req.TianyuanCode != nil {
|
||||
builder = builder.Where("tianyuan_code = ?", *req.TianyuanCode)
|
||||
}
|
||||
|
||||
var total int64
|
||||
var logs []*model.QueryWhitelistOpLog
|
||||
err = mr.Finish(func() error {
|
||||
count, countErr := l.svcCtx.QueryWhitelistOpLogModel.FindCount(l.ctx, builder, "id")
|
||||
if countErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录总数失败: %v", countErr)
|
||||
}
|
||||
total = count
|
||||
return nil
|
||||
}, func() error {
|
||||
list, listErr := l.svcCtx.QueryWhitelistOpLogModel.FindPageListByPage(l.ctx, builder, req.Page, req.PageSize, "create_time DESC")
|
||||
if listErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录列表失败: %v", listErr)
|
||||
}
|
||||
logs = list
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = &types.AdminQueryWhitelistOpLogListResp{
|
||||
Total: total,
|
||||
Items: make([]types.AdminQueryWhitelistOpLogItem, 0, len(logs)),
|
||||
}
|
||||
for _, log := range logs {
|
||||
resp.Items = append(resp.Items, buildOpLogListItem(l.ctx, l.svcCtx, log))
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
141
app/main/api/internal/logic/admin_query_whitelist/op_helper.go
Normal file
141
app/main/api/internal/logic/admin_query_whitelist/op_helper.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
tianyuanapi "qnc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func executeQueryWhitelistOp(
|
||||
ctx context.Context,
|
||||
svcCtx *svc.ServiceContext,
|
||||
action string,
|
||||
req *types.AdminQueryWhitelistCreateReq,
|
||||
callFn func(client *tianyuanapi.Client, payload tianyuanapi.QueryWhitelistRequest) *tianyuanapi.QueryWhitelistResult,
|
||||
) (*types.AdminQueryWhitelistOpResp, error) {
|
||||
adminUserId, err := ctxdata.GetUidFromCtx(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.TOKEN_EXPIRE_ERROR), "获取管理员信息失败: %v", err)
|
||||
}
|
||||
|
||||
if svcCtx.TianyuanapiClient == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "天远API客户端未初始化")
|
||||
}
|
||||
|
||||
name := req.Name
|
||||
if name == "" {
|
||||
name = "*"
|
||||
}
|
||||
|
||||
payload := tianyuanapi.QueryWhitelistRequest{
|
||||
Name: name,
|
||||
IdCard: req.IdCard,
|
||||
ApiCodes: req.ApiCodes,
|
||||
Remark: req.Remark,
|
||||
}
|
||||
|
||||
result := callFn(svcCtx.TianyuanapiClient, payload)
|
||||
|
||||
apiCodesJSON, _ := json.Marshal(req.ApiCodes)
|
||||
opLog := &model.QueryWhitelistOpLog{
|
||||
AdminUserId: adminUserId,
|
||||
Action: action,
|
||||
Name: name,
|
||||
IdCard: req.IdCard,
|
||||
ApiCodes: string(apiCodesJSON),
|
||||
TianyuanCode: int64(result.Code),
|
||||
}
|
||||
if req.Remark != "" {
|
||||
opLog.Remark = sql.NullString{String: req.Remark, Valid: true}
|
||||
}
|
||||
if result.Message != "" {
|
||||
opLog.TianyuanMessage = sql.NullString{String: result.Message, Valid: true}
|
||||
}
|
||||
if result.TransactionID != "" {
|
||||
opLog.TransactionId = sql.NullString{String: result.TransactionID, Valid: true}
|
||||
}
|
||||
if result.Entry != nil {
|
||||
opLog.IdCardMasked = sql.NullString{String: result.Entry.IdCardMasked, Valid: result.Entry.IdCardMasked != ""}
|
||||
opLog.EntryId = sql.NullString{String: result.Entry.Id, Valid: result.Entry.Id != ""}
|
||||
opLog.EntryStatus = sql.NullString{String: result.Entry.Status, Valid: result.Entry.Status != ""}
|
||||
if len(result.Entry.ApiCodes) > 0 {
|
||||
entryApiCodesJSON, _ := json.Marshal(result.Entry.ApiCodes)
|
||||
opLog.EntryApiCodes = sql.NullString{String: string(entryApiCodesJSON), Valid: true}
|
||||
}
|
||||
}
|
||||
|
||||
if _, insertErr := svcCtx.QueryWhitelistOpLogModel.Insert(ctx, nil, opLog); insertErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "保存操作记录失败: %v", insertErr)
|
||||
}
|
||||
|
||||
resp := &types.AdminQueryWhitelistOpResp{
|
||||
TianyuanCode: result.Code,
|
||||
TianyuanMessage: result.Message,
|
||||
TransactionId: result.TransactionID,
|
||||
}
|
||||
if result.Entry != nil {
|
||||
resp.Entry = &types.AdminQueryWhitelistEntryItem{
|
||||
Id: result.Entry.Id,
|
||||
Name: result.Entry.Name,
|
||||
IdCardMasked: result.Entry.IdCardMasked,
|
||||
ApiCodes: result.Entry.ApiCodes,
|
||||
Status: result.Entry.Status,
|
||||
Remark: result.Entry.Remark,
|
||||
CreatedAt: result.Entry.CreatedAt,
|
||||
UpdatedAt: result.Entry.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func buildOpLogListItem(ctx context.Context, svcCtx *svc.ServiceContext, log *model.QueryWhitelistOpLog) types.AdminQueryWhitelistOpLogItem {
|
||||
item := types.AdminQueryWhitelistOpLogItem{
|
||||
Id: log.Id,
|
||||
AdminUserId: log.AdminUserId,
|
||||
Action: log.Action,
|
||||
Name: log.Name,
|
||||
IdCard: log.IdCard,
|
||||
IdCardMasked: log.IdCardMasked.String,
|
||||
Remark: log.Remark.String,
|
||||
TianyuanCode: int(log.TianyuanCode),
|
||||
TianyuanMessage: log.TianyuanMessage.String,
|
||||
TransactionId: log.TransactionId.String,
|
||||
EntryId: log.EntryId.String,
|
||||
EntryStatus: log.EntryStatus.String,
|
||||
CreateTime: log.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
|
||||
if log.ApiCodes != "" {
|
||||
var apiCodes []string
|
||||
if json.Unmarshal([]byte(log.ApiCodes), &apiCodes) == nil {
|
||||
item.ApiCodes = apiCodes
|
||||
}
|
||||
}
|
||||
if log.EntryApiCodes.Valid && log.EntryApiCodes.String != "" {
|
||||
var entryApiCodes []string
|
||||
if json.Unmarshal([]byte(log.EntryApiCodes.String), &entryApiCodes) == nil {
|
||||
item.EntryApiCodes = entryApiCodes
|
||||
}
|
||||
}
|
||||
|
||||
if log.AdminUserId != "" {
|
||||
if adminUser, err := svcCtx.AdminUserModel.FindOne(ctx, log.AdminUserId); err == nil {
|
||||
item.AdminUserName = adminUser.RealName
|
||||
if item.AdminUserName == "" {
|
||||
item.AdminUserName = adminUser.Username
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return item
|
||||
}
|
||||
Reference in New Issue
Block a user