addf
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tyc-server/app/main/api/internal/svc"
|
||||
"tyc-server/app/main/api/internal/types"
|
||||
"tyc-server/app/main/model"
|
||||
"tyc-server/common/globalkey"
|
||||
"tyc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/mr"
|
||||
)
|
||||
|
||||
type AdminGetQueryWhitelistOpLogListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminGetQueryWhitelistOpLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetQueryWhitelistOpLogListLogic {
|
||||
return &AdminGetQueryWhitelistOpLogListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminGetQueryWhitelistOpLogListLogic) AdminGetQueryWhitelistOpLogList(req *types.AdminGetQueryWhitelistOpLogListReq) (resp *types.AdminGetQueryWhitelistOpLogListResp, err error) {
|
||||
builder := l.svcCtx.QueryWhitelistOpLogModel.SelectBuilder().
|
||||
Where("del_state = ?", globalkey.DelStateNo)
|
||||
|
||||
if req.IdCard != "" {
|
||||
builder = builder.Where("id_card = ?", req.IdCard)
|
||||
}
|
||||
if req.Action != "" {
|
||||
builder = builder.Where("action = ?", req.Action)
|
||||
}
|
||||
switch req.TianyuanResult {
|
||||
case "success":
|
||||
builder = builder.Where("tianyuan_code = ?", 0)
|
||||
case "fail":
|
||||
builder = builder.Where("tianyuan_code != ?", 0)
|
||||
}
|
||||
|
||||
var total int64
|
||||
var logs []*model.QueryWhitelistOpLog
|
||||
err = mr.Finish(func() error {
|
||||
var err error
|
||||
total, err = l.svcCtx.QueryWhitelistOpLogModel.FindCount(l.ctx, builder, "id")
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录总数失败: %v", err)
|
||||
}
|
||||
return nil
|
||||
}, func() error {
|
||||
var err error
|
||||
logs, err = l.svcCtx.QueryWhitelistOpLogModel.FindPageListByPage(l.ctx, builder, req.Page, req.PageSize, "id DESC")
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录列表失败: %v", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = &types.AdminGetQueryWhitelistOpLogListResp{
|
||||
Total: total,
|
||||
Items: make([]types.QueryWhitelistOpLogListItem, 0, len(logs)),
|
||||
}
|
||||
|
||||
for _, log := range logs {
|
||||
item := types.QueryWhitelistOpLogListItem{
|
||||
Id: log.Id,
|
||||
AdminUserId: log.AdminUserId,
|
||||
Action: log.Action,
|
||||
Name: log.Name,
|
||||
IdCard: log.IdCard,
|
||||
ApiCodes: parseApiCodesJSON(log.ApiCodes),
|
||||
TianyuanCode: log.TianyuanCode,
|
||||
CreateTime: log.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
EntryApiCodes: parseApiCodesJSON(log.EntryApiCodes.String),
|
||||
}
|
||||
if log.IdCardMasked.Valid {
|
||||
item.IdCardMasked = log.IdCardMasked.String
|
||||
}
|
||||
if log.Remark.Valid {
|
||||
item.Remark = log.Remark.String
|
||||
}
|
||||
if log.TianyuanMessage.Valid {
|
||||
item.TianyuanMessage = log.TianyuanMessage.String
|
||||
}
|
||||
if log.TransactionId.Valid {
|
||||
item.TransactionId = log.TransactionId.String
|
||||
}
|
||||
if log.EntryId.Valid {
|
||||
item.EntryId = log.EntryId.String
|
||||
}
|
||||
if log.EntryStatus.Valid {
|
||||
item.EntryStatus = log.EntryStatus.String
|
||||
}
|
||||
resp.Items = append(resp.Items, item)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tyc-server/app/main/api/internal/svc"
|
||||
"tyc-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) {
|
||||
return callAndRecord(l.ctx, l.svcCtx, actionAppend, opParams{
|
||||
Name: req.Name,
|
||||
IdCard: req.IdCard,
|
||||
ApiCodes: req.ApiCodes,
|
||||
Remark: req.Remark,
|
||||
}, l.svcCtx.TianyuanapiClient.AppendQueryWhitelistEntry)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tyc-server/app/main/api/internal/svc"
|
||||
"tyc-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 callAndRecord(l.ctx, l.svcCtx, actionCreate, opParams{
|
||||
Name: req.Name,
|
||||
IdCard: req.IdCard,
|
||||
ApiCodes: req.ApiCodes,
|
||||
Remark: req.Remark,
|
||||
}, l.svcCtx.TianyuanapiClient.CreateQueryWhitelistEntry)
|
||||
}
|
||||
195
app/main/api/internal/logic/admin_query_whitelist/helper.go
Normal file
195
app/main/api/internal/logic/admin_query_whitelist/helper.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
tianyuanapi "tyc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"tyc-server/app/main/api/internal/svc"
|
||||
"tyc-server/app/main/api/internal/types"
|
||||
"tyc-server/app/main/model"
|
||||
"tyc-server/common/ctxdata"
|
||||
"tyc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
actionCreate = "create"
|
||||
actionAppend = "append"
|
||||
)
|
||||
|
||||
var idCardPattern = regexp.MustCompile(`^\d{17}[\dXx]$`)
|
||||
|
||||
type opParams struct {
|
||||
Name string
|
||||
IdCard string
|
||||
ApiCodes []string
|
||||
Remark string
|
||||
}
|
||||
|
||||
func validateOpParams(p opParams) error {
|
||||
if strings.TrimSpace(p.Name) == "" {
|
||||
return fmt.Errorf("姓名不能为空")
|
||||
}
|
||||
idCard := strings.TrimSpace(p.IdCard)
|
||||
if idCard == "" {
|
||||
return fmt.Errorf("身份证号不能为空")
|
||||
}
|
||||
if !idCardPattern.MatchString(idCard) {
|
||||
return fmt.Errorf("身份证号格式不正确")
|
||||
}
|
||||
if len(p.ApiCodes) == 0 {
|
||||
return fmt.Errorf("产品编码不能为空")
|
||||
}
|
||||
for _, code := range p.ApiCodes {
|
||||
if strings.TrimSpace(code) == "" {
|
||||
return fmt.Errorf("产品编码不能包含空值")
|
||||
}
|
||||
if code == "*" {
|
||||
return fmt.Errorf("产品编码不支持通配符 *")
|
||||
}
|
||||
}
|
||||
if len(p.Remark) > 500 {
|
||||
return fmt.Errorf("备注最长 500 字符")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func toTianyuanParams(p opParams) tianyuanapi.QueryWhitelistParams {
|
||||
name := strings.TrimSpace(p.Name)
|
||||
if name == "" {
|
||||
name = "*"
|
||||
}
|
||||
return tianyuanapi.QueryWhitelistParams{
|
||||
Name: name,
|
||||
IdCard: strings.ToUpper(strings.TrimSpace(p.IdCard)),
|
||||
ApiCodes: p.ApiCodes,
|
||||
Remark: strings.TrimSpace(p.Remark),
|
||||
}
|
||||
}
|
||||
|
||||
func toEntryResult(entry *tianyuanapi.QueryWhitelistEntry) *types.QueryWhitelistEntryResult {
|
||||
if entry == nil {
|
||||
return nil
|
||||
}
|
||||
return &types.QueryWhitelistEntryResult{
|
||||
Id: entry.ID,
|
||||
Name: entry.Name,
|
||||
IdCardMasked: entry.IdCardMasked,
|
||||
ApiCodes: entry.ApiCodes,
|
||||
Status: entry.Status,
|
||||
Remark: entry.Remark,
|
||||
CreatedAt: entry.CreatedAt,
|
||||
UpdatedAt: entry.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func toOpResp(result *tianyuanapi.QueryWhitelistResult) *types.AdminQueryWhitelistOpResp {
|
||||
if result == nil {
|
||||
return &types.AdminQueryWhitelistOpResp{}
|
||||
}
|
||||
return &types.AdminQueryWhitelistOpResp{
|
||||
TianyuanCode: result.Code,
|
||||
TianyuanMessage: result.Message,
|
||||
TransactionId: result.TransactionID,
|
||||
Entry: toEntryResult(result.Entry),
|
||||
}
|
||||
}
|
||||
|
||||
func saveOpLog(ctx context.Context, svcCtx *svc.ServiceContext, action string, params opParams, result *tianyuanapi.QueryWhitelistResult) error {
|
||||
adminUserID, err := ctxdata.GetUidFromCtx(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.TOKEN_EXPIRE_ERROR), "获取管理员ID失败: %v", err)
|
||||
}
|
||||
|
||||
apiCodesJSON, err := json.Marshal(params.ApiCodes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log := &model.QueryWhitelistOpLog{
|
||||
AdminUserId: adminUserID,
|
||||
Action: action,
|
||||
Name: strings.TrimSpace(params.Name),
|
||||
IdCard: strings.ToUpper(strings.TrimSpace(params.IdCard)),
|
||||
ApiCodes: string(apiCodesJSON),
|
||||
TianyuanCode: int64(result.Code),
|
||||
}
|
||||
|
||||
if params.Remark != "" {
|
||||
log.Remark = sql.NullString{String: params.Remark, Valid: true}
|
||||
}
|
||||
if result.Message != "" {
|
||||
log.TianyuanMessage = sql.NullString{String: result.Message, Valid: true}
|
||||
}
|
||||
if result.TransactionID != "" {
|
||||
log.TransactionId = sql.NullString{String: result.TransactionID, Valid: true}
|
||||
}
|
||||
if result.Entry != nil {
|
||||
if result.Entry.IdCardMasked != "" {
|
||||
log.IdCardMasked = sql.NullString{String: result.Entry.IdCardMasked, Valid: true}
|
||||
}
|
||||
if result.Entry.ID != "" {
|
||||
log.EntryId = sql.NullString{String: result.Entry.ID, Valid: true}
|
||||
}
|
||||
if result.Entry.Status != "" {
|
||||
log.EntryStatus = sql.NullString{String: result.Entry.Status, Valid: true}
|
||||
}
|
||||
if len(result.Entry.ApiCodes) > 0 {
|
||||
entryCodesJSON, err := json.Marshal(result.Entry.ApiCodes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.EntryApiCodes = sql.NullString{String: string(entryCodesJSON), Valid: true}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = svcCtx.QueryWhitelistOpLogModel.Insert(ctx, nil, log)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "保存操作记录失败: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func callAndRecord(
|
||||
ctx context.Context,
|
||||
svcCtx *svc.ServiceContext,
|
||||
action string,
|
||||
params opParams,
|
||||
call func(tianyuanapi.QueryWhitelistParams) (*tianyuanapi.QueryWhitelistResult, error),
|
||||
) (*types.AdminQueryWhitelistOpResp, error) {
|
||||
if err := validateOpParams(params); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.REUQEST_PARAM_ERROR), "%s", err.Error())
|
||||
}
|
||||
|
||||
if svcCtx.TianyuanapiClient == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "天远 API 客户端未初始化")
|
||||
}
|
||||
|
||||
result, err := call(toTianyuanParams(params))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "调用天远 API 失败: %v", err)
|
||||
}
|
||||
|
||||
if err := saveOpLog(ctx, svcCtx, action, params, result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return toOpResp(result), nil
|
||||
}
|
||||
|
||||
func parseApiCodesJSON(raw string) []string {
|
||||
if raw == "" {
|
||||
return []string{}
|
||||
}
|
||||
var codes []string
|
||||
if err := json.Unmarshal([]byte(raw), &codes); err != nil {
|
||||
return []string{}
|
||||
}
|
||||
return codes
|
||||
}
|
||||
Reference in New Issue
Block a user