fadd
This commit is contained in:
91
app/main/api/desc/admin/admin_query_whitelist.api
Normal file
91
app/main/api/desc/admin/admin_query_whitelist.api
Normal file
@@ -0,0 +1,91 @@
|
||||
syntax = "v1"
|
||||
|
||||
info (
|
||||
title: "后台查询白名单管理服务"
|
||||
desc: "对接天远 API 查询白名单配置接口"
|
||||
version: "v1"
|
||||
)
|
||||
|
||||
@server (
|
||||
prefix: /api/v1/admin/query-whitelist
|
||||
group: admin_query_whitelist
|
||||
middleware: AdminAuthInterceptor
|
||||
)
|
||||
service main {
|
||||
// 创建查询白名单规则
|
||||
@handler AdminCreateQueryWhitelist
|
||||
post /create (AdminCreateQueryWhitelistReq) returns (AdminCreateQueryWhitelistResp)
|
||||
|
||||
// 追加产品编码到已有规则
|
||||
@handler AdminAppendQueryWhitelist
|
||||
// 获取操作记录列表
|
||||
@handler AdminGetQueryWhitelistOpLogList
|
||||
get /op-log/list (AdminGetQueryWhitelistOpLogListReq) returns (AdminGetQueryWhitelistOpLogListResp)
|
||||
}
|
||||
|
||||
type (
|
||||
AdminQueryWhitelistEntry {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Status string `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
AdminCreateQueryWhitelistReq {
|
||||
Name string `json:"name,optional"`
|
||||
IdCard string `json:"id_card"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark,optional"`
|
||||
}
|
||||
AdminCreateQueryWhitelistResp {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionId string `json:"transaction_id"`
|
||||
Entry *AdminQueryWhitelistEntry `json:"entry,optional"`
|
||||
}
|
||||
AdminAppendQueryWhitelistReq {
|
||||
Name string `json:"name,optional"`
|
||||
IdCard string `json:"id_card"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark,optional"`
|
||||
}
|
||||
AdminAppendQueryWhitelistResp {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionId string `json:"transaction_id"`
|
||||
Entry *AdminQueryWhitelistEntry `json:"entry,optional"`
|
||||
}
|
||||
AdminGetQueryWhitelistOpLogListReq {
|
||||
Page int64 `form:"page"`
|
||||
PageSize int64 `form:"pageSize"`
|
||||
IdCard *string `form:"id_card,optional"`
|
||||
Action *string `form:"action,optional"`
|
||||
TianyuanCode *int64 `form:"tianyuan_code,optional"`
|
||||
}
|
||||
AdminQueryWhitelistOpLogItem {
|
||||
Id string `json:"id"`
|
||||
AdminUserId string `json:"admin_user_id"`
|
||||
AdminUsername string `json:"admin_username"`
|
||||
Action string `json:"action"`
|
||||
ActionText string `json:"action_text"`
|
||||
Name string `json:"name"`
|
||||
IdCard string `json:"id_card"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark"`
|
||||
TianyuanCode int64 `json:"tianyuan_code"`
|
||||
TianyuanMessage string `json:"tianyuan_message"`
|
||||
TransactionId string `json:"transaction_id"`
|
||||
EntryId string `json:"entry_id"`
|
||||
EntryStatus string `json:"entry_status"`
|
||||
EntryApiCodes []string `json:"entry_api_codes"`
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
AdminGetQueryWhitelistOpLogListResp {
|
||||
Total int64 `json:"total"`
|
||||
Items []AdminQueryWhitelistOpLogItem `json:"items"`
|
||||
}
|
||||
)
|
||||
@@ -25,6 +25,7 @@ import "./admin/notification.api"
|
||||
import "./admin/admin_product.api"
|
||||
import "./admin/admin_feature.api"
|
||||
import "./admin/admin_whitelist.api"
|
||||
import "./admin/admin_query_whitelist.api"
|
||||
import "./admin/admin_query.api"
|
||||
import "./admin/admin_agent.api"
|
||||
import "./admin/admin_api.api"
|
||||
|
||||
@@ -94,6 +94,7 @@ Tianyuanapi:
|
||||
Key: "04c6b4c559be6d5ba5351c04c8713a64"
|
||||
BaseURL: "https://api.tianyuanapi.com"
|
||||
Timeout: 60
|
||||
WhitelistMgmtKey: "2R12TmEc1e8P3p69RdIoN5Ykjk%H@4orPy7DZv7MXpGByoEL" # 查询白名单管理密钥,向平台商务/技术支持申请
|
||||
Authorization:
|
||||
FileBaseURL: "https://www.onecha.cn/api/v1/auth-docs" # 授权书文件访问基础URL
|
||||
Promotion:
|
||||
|
||||
@@ -84,6 +84,7 @@ Tianyuanapi:
|
||||
Key: "04c6b4c559be6d5ba5351c04c8713a64"
|
||||
BaseURL: "https://api.tianyuanapi.com"
|
||||
Timeout: 60
|
||||
WhitelistMgmtKey: "2R12TmEc1e8P3p69RdIoN5Ykjk%H@4orPy7DZv7MXpGByoEL" # 查询白名单管理密钥,向平台商务/技术支持申请
|
||||
Authorization:
|
||||
FileBaseURL: "https://www.onecha.cn/api/v1/auth-docs" # 授权书文件访问基础URL
|
||||
Promotion:
|
||||
|
||||
@@ -102,10 +102,11 @@ type TaxConfig struct {
|
||||
TaxExemptionAmount float64
|
||||
}
|
||||
type TianyuanapiConfig struct {
|
||||
AccessID string
|
||||
Key string
|
||||
BaseURL string
|
||||
Timeout int64
|
||||
AccessID string
|
||||
Key string
|
||||
BaseURL string
|
||||
Timeout int64
|
||||
WhitelistMgmtKey string // 查询白名单管理密钥 Whitelist-Mgmt-Key
|
||||
}
|
||||
|
||||
type AuthorizationConfig struct {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"ycc-server/app/main/api/internal/logic/admin_query_whitelist"
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/common/result"
|
||||
"ycc-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func AdminAppendQueryWhitelistHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AdminAppendQueryWhitelistReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := admin_query_whitelist.NewAdminAppendQueryWhitelistLogic(r.Context(), svcCtx)
|
||||
resp, err := l.AdminAppendQueryWhitelist(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"ycc-server/app/main/api/internal/logic/admin_query_whitelist"
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/common/result"
|
||||
"ycc-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func AdminCreateQueryWhitelistHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AdminCreateQueryWhitelistReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := admin_query_whitelist.NewAdminCreateQueryWhitelistLogic(r.Context(), svcCtx)
|
||||
resp, err := l.AdminCreateQueryWhitelist(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"ycc-server/app/main/api/internal/logic/admin_query_whitelist"
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/common/result"
|
||||
"ycc-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func AdminGetQueryWhitelistOpLogListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AdminGetQueryWhitelistOpLogListReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := admin_query_whitelist.NewAdminGetQueryWhitelistOpLogListLogic(r.Context(), svcCtx)
|
||||
resp, err := l.AdminGetQueryWhitelistOpLogList(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
admin_platform_user "ycc-server/app/main/api/internal/handler/admin_platform_user"
|
||||
admin_product "ycc-server/app/main/api/internal/handler/admin_product"
|
||||
admin_query "ycc-server/app/main/api/internal/handler/admin_query"
|
||||
admin_query_whitelist "ycc-server/app/main/api/internal/handler/admin_query_whitelist"
|
||||
admin_role "ycc-server/app/main/api/internal/handler/admin_role"
|
||||
admin_role_api "ycc-server/app/main/api/internal/handler/admin_role_api"
|
||||
admin_user "ycc-server/app/main/api/internal/handler/admin_user"
|
||||
@@ -688,6 +689,30 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
rest.WithPrefix("/api/v1/admin/whitelist"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.AdminAuthInterceptor},
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/op-log/list",
|
||||
Handler: admin_query_whitelist.AdminGetQueryWhitelistOpLogListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/append",
|
||||
Handler: admin_query_whitelist.AdminAppendQueryWhitelistHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/create",
|
||||
Handler: admin_query_whitelist.AdminCreateQueryWhitelistHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/v1/admin/query-whitelist"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminAppendQueryWhitelistLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminAppendQueryWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminAppendQueryWhitelistLogic {
|
||||
return &AdminAppendQueryWhitelistLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminAppendQueryWhitelistLogic) AdminAppendQueryWhitelist(req *types.AdminAppendQueryWhitelistReq) (resp *types.AdminAppendQueryWhitelistResp, err error) {
|
||||
if err = validateQueryWhitelistReq(req.IdCard, req.ApiCodes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiResp, err := callQueryWhitelistMgmt(
|
||||
l.svcCtx,
|
||||
"/api/v1/query-whitelist/entries/append",
|
||||
req.Name,
|
||||
req.IdCard,
|
||||
req.ApiCodes,
|
||||
req.Remark,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
recordQueryWhitelistOpLog(l.ctx, l.svcCtx, "append", req.Name, req.IdCard, req.ApiCodes, req.Remark, apiResp)
|
||||
|
||||
return toAppendResp(apiResp), nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminCreateQueryWhitelistLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminCreateQueryWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminCreateQueryWhitelistLogic {
|
||||
return &AdminCreateQueryWhitelistLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminCreateQueryWhitelistLogic) AdminCreateQueryWhitelist(req *types.AdminCreateQueryWhitelistReq) (resp *types.AdminCreateQueryWhitelistResp, err error) {
|
||||
if err = validateQueryWhitelistReq(req.IdCard, req.ApiCodes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiResp, err := callQueryWhitelistMgmt(
|
||||
l.svcCtx,
|
||||
"/api/v1/query-whitelist/entries",
|
||||
req.Name,
|
||||
req.IdCard,
|
||||
req.ApiCodes,
|
||||
req.Remark,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
recordQueryWhitelistOpLog(l.ctx, l.svcCtx, "create", req.Name, req.IdCard, req.ApiCodes, req.Remark, apiResp)
|
||||
|
||||
return toCreateResp(apiResp), nil
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
if req.IdCard != nil && *req.IdCard != "" {
|
||||
builder = builder.Where("id_card LIKE ?", "%"+*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)
|
||||
}
|
||||
|
||||
total, err := l.svcCtx.QueryWhitelistOpLogModel.FindCount(l.ctx, builder, "id")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录总数失败, %v", err)
|
||||
}
|
||||
|
||||
list, err := l.svcCtx.QueryWhitelistOpLogModel.FindPageListByPage(l.ctx, builder, req.Page, req.PageSize, "create_time DESC")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录列表失败, %v", err)
|
||||
}
|
||||
|
||||
adminNameMap := l.buildAdminUsernameMap(list)
|
||||
items := make([]types.AdminQueryWhitelistOpLogItem, 0, len(list))
|
||||
for _, row := range list {
|
||||
items = append(items, types.AdminQueryWhitelistOpLogItem{
|
||||
Id: row.Id,
|
||||
AdminUserId: row.AdminUserId,
|
||||
AdminUsername: adminNameMap[row.AdminUserId],
|
||||
Action: row.Action,
|
||||
ActionText: queryWhitelistActionText(row.Action),
|
||||
Name: row.Name,
|
||||
IdCard: row.IdCard,
|
||||
IdCardMasked: row.IdCardMasked.String,
|
||||
ApiCodes: parseApiCodesJSON(row.ApiCodes),
|
||||
Remark: row.Remark.String,
|
||||
TianyuanCode: row.TianyuanCode,
|
||||
TianyuanMessage: row.TianyuanMessage.String,
|
||||
TransactionId: row.TransactionId.String,
|
||||
EntryId: row.EntryId.String,
|
||||
EntryStatus: row.EntryStatus.String,
|
||||
EntryApiCodes: parseApiCodesJSON(row.EntryApiCodes.String),
|
||||
CreateTime: row.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &types.AdminGetQueryWhitelistOpLogListResp{
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetQueryWhitelistOpLogListLogic) buildAdminUsernameMap(list []*model.QueryWhitelistOpLog) map[string]string {
|
||||
nameMap := make(map[string]string)
|
||||
if len(list) == 0 {
|
||||
return nameMap
|
||||
}
|
||||
|
||||
adminIds := make([]string, 0, len(list))
|
||||
seen := make(map[string]struct{}, len(list))
|
||||
for _, row := range list {
|
||||
if row.AdminUserId == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[row.AdminUserId]; ok {
|
||||
continue
|
||||
}
|
||||
seen[row.AdminUserId] = struct{}{}
|
||||
adminIds = append(adminIds, row.AdminUserId)
|
||||
}
|
||||
if len(adminIds) == 0 {
|
||||
return nameMap
|
||||
}
|
||||
|
||||
builder := l.svcCtx.AdminUserModel.SelectBuilder().Where(squirrel.Eq{"id": adminIds})
|
||||
users, err := l.svcCtx.AdminUserModel.FindAll(l.ctx, builder, "")
|
||||
if err != nil {
|
||||
l.Errorf("批量查询管理员名称失败: %v", err)
|
||||
return nameMap
|
||||
}
|
||||
for _, user := range users {
|
||||
displayName := user.RealName
|
||||
if displayName == "" {
|
||||
displayName = user.Username
|
||||
}
|
||||
nameMap[user.Id] = displayName
|
||||
}
|
||||
return nameMap
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
tianyuanapi "ycc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/ctxdata"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
var idCardPattern = regexp.MustCompile(`^\d{17}[\dXx]$`)
|
||||
|
||||
func validateQueryWhitelistReq(idCard string, apiCodes []string) error {
|
||||
if strings.TrimSpace(idCard) == "" {
|
||||
return errors.Wrapf(xerr.NewErrMsg("身份证号不能为空"), "")
|
||||
}
|
||||
if !idCardPattern.MatchString(strings.TrimSpace(idCard)) {
|
||||
return errors.Wrapf(xerr.NewErrMsg("身份证号格式不正确,需为18位中国大陆身份证号"), "")
|
||||
}
|
||||
if len(apiCodes) == 0 {
|
||||
return errors.Wrapf(xerr.NewErrMsg("请至少选择一个产品编码"), "")
|
||||
}
|
||||
for _, code := range apiCodes {
|
||||
if strings.TrimSpace(code) == "" {
|
||||
return errors.Wrapf(xerr.NewErrMsg("产品编码不能为空"), "")
|
||||
}
|
||||
if code == "*" {
|
||||
return errors.Wrapf(xerr.NewErrMsg("产品编码不支持通配符 *"), "")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildQueryWhitelistPayload(name, idCard string, apiCodes []string, remark string) map[string]interface{} {
|
||||
payload := map[string]interface{}{
|
||||
"name": name,
|
||||
"id_card": strings.TrimSpace(idCard),
|
||||
"api_codes": apiCodes,
|
||||
}
|
||||
if strings.TrimSpace(remark) != "" {
|
||||
payload["remark"] = strings.TrimSpace(remark)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func callQueryWhitelistMgmt(
|
||||
svcCtx *svc.ServiceContext,
|
||||
apiPath string,
|
||||
name, idCard string,
|
||||
apiCodes []string,
|
||||
remark string,
|
||||
) (*tianyuanapi.QueryWhitelistMgmtResponse, error) {
|
||||
mgmtKey := svcCtx.Config.Tianyuanapi.WhitelistMgmtKey
|
||||
if mgmtKey == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("查询白名单管理密钥未配置,请在服务端配置 Tianyuanapi.WhitelistMgmtKey"), "")
|
||||
}
|
||||
if svcCtx.TianyuanapiClient == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("天远 API 客户端未初始化"), "")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(name) == "" {
|
||||
name = "*"
|
||||
}
|
||||
|
||||
payload := buildQueryWhitelistPayload(name, idCard, apiCodes, remark)
|
||||
return svcCtx.TianyuanapiClient.CallQueryWhitelistMgmt(apiPath, payload, mgmtKey)
|
||||
}
|
||||
|
||||
func toAdminEntry(entry *tianyuanapi.QueryWhitelistEntry) *types.AdminQueryWhitelistEntry {
|
||||
if entry == nil {
|
||||
return nil
|
||||
}
|
||||
return &types.AdminQueryWhitelistEntry{
|
||||
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 toCreateResp(resp *tianyuanapi.QueryWhitelistMgmtResponse) *types.AdminCreateQueryWhitelistResp {
|
||||
return &types.AdminCreateQueryWhitelistResp{
|
||||
Code: resp.Code,
|
||||
Message: resp.Message,
|
||||
TransactionId: resp.TransactionID,
|
||||
Entry: toAdminEntry(resp.Entry),
|
||||
}
|
||||
}
|
||||
|
||||
func toAppendResp(resp *tianyuanapi.QueryWhitelistMgmtResponse) *types.AdminAppendQueryWhitelistResp {
|
||||
return &types.AdminAppendQueryWhitelistResp{
|
||||
Code: resp.Code,
|
||||
Message: resp.Message,
|
||||
TransactionId: resp.TransactionID,
|
||||
Entry: toAdminEntry(resp.Entry),
|
||||
}
|
||||
}
|
||||
|
||||
func resolveName(name string) string {
|
||||
if strings.TrimSpace(name) == "" {
|
||||
return "*"
|
||||
}
|
||||
return strings.TrimSpace(name)
|
||||
}
|
||||
|
||||
func parseApiCodesJSON(raw string) []string {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
var codes []string
|
||||
if err := json.Unmarshal([]byte(raw), &codes); err != nil {
|
||||
return nil
|
||||
}
|
||||
return codes
|
||||
}
|
||||
|
||||
func queryWhitelistActionText(action string) string {
|
||||
switch action {
|
||||
case "create":
|
||||
return "创建规则"
|
||||
case "append":
|
||||
return "追加接口"
|
||||
default:
|
||||
return action
|
||||
}
|
||||
}
|
||||
|
||||
func recordQueryWhitelistOpLog(
|
||||
ctx context.Context,
|
||||
svcCtx *svc.ServiceContext,
|
||||
action, name, idCard string,
|
||||
apiCodes []string,
|
||||
remark string,
|
||||
apiResp *tianyuanapi.QueryWhitelistMgmtResponse,
|
||||
) {
|
||||
adminUserId, err := ctxdata.GetUidFromCtx(ctx)
|
||||
if err != nil {
|
||||
logx.WithContext(ctx).Errorf("记录查询白名单操作日志失败: 获取管理员ID失败, %v", err)
|
||||
adminUserId = ""
|
||||
}
|
||||
|
||||
apiCodesJSON, marshalErr := json.Marshal(apiCodes)
|
||||
if marshalErr != nil {
|
||||
logx.WithContext(ctx).Errorf("记录查询白名单操作日志失败: 序列化 api_codes 失败, %v", marshalErr)
|
||||
return
|
||||
}
|
||||
|
||||
log := &model.QueryWhitelistOpLog{
|
||||
AdminUserId: adminUserId,
|
||||
Action: action,
|
||||
Name: resolveName(name),
|
||||
IdCard: strings.TrimSpace(idCard),
|
||||
ApiCodes: string(apiCodesJSON),
|
||||
TianyuanCode: int64(apiResp.Code),
|
||||
}
|
||||
|
||||
if strings.TrimSpace(remark) != "" {
|
||||
log.Remark = sql.NullString{String: strings.TrimSpace(remark), Valid: true}
|
||||
}
|
||||
if apiResp.Message != "" {
|
||||
log.TianyuanMessage = sql.NullString{String: apiResp.Message, Valid: true}
|
||||
}
|
||||
if apiResp.TransactionID != "" {
|
||||
log.TransactionId = sql.NullString{String: apiResp.TransactionID, Valid: true}
|
||||
}
|
||||
if apiResp.Entry != nil {
|
||||
if apiResp.Entry.IdCardMasked != "" {
|
||||
log.IdCardMasked = sql.NullString{String: apiResp.Entry.IdCardMasked, Valid: true}
|
||||
}
|
||||
if apiResp.Entry.ID != "" {
|
||||
log.EntryId = sql.NullString{String: apiResp.Entry.ID, Valid: true}
|
||||
}
|
||||
if apiResp.Entry.Status != "" {
|
||||
log.EntryStatus = sql.NullString{String: apiResp.Entry.Status, Valid: true}
|
||||
}
|
||||
if len(apiResp.Entry.ApiCodes) > 0 {
|
||||
entryCodesJSON, _ := json.Marshal(apiResp.Entry.ApiCodes)
|
||||
log.EntryApiCodes = sql.NullString{String: string(entryCodesJSON), Valid: true}
|
||||
}
|
||||
}
|
||||
|
||||
if _, insertErr := svcCtx.QueryWhitelistOpLogModel.Insert(ctx, nil, log); insertErr != nil {
|
||||
logx.WithContext(ctx).Errorf("记录查询白名单操作日志失败: %v", insertErr)
|
||||
}
|
||||
}
|
||||
105
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
105
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// QueryWhitelistEntry 查询白名单规则详情(解密后)
|
||||
type QueryWhitelistEntry struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Status string `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// QueryWhitelistMgmtResponse 查询白名单管理接口响应
|
||||
type QueryWhitelistMgmtResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionID string `json:"transaction_id"`
|
||||
Entry *QueryWhitelistEntry `json:"entry,omitempty"`
|
||||
Timeout int64 `json:"timeout"`
|
||||
}
|
||||
|
||||
// CallQueryWhitelistMgmt 调用查询白名单管理接口(创建/追加)
|
||||
func (c *Client) CallQueryWhitelistMgmt(apiPath string, params map[string]interface{}, mgmtKey string) (*QueryWhitelistMgmtResponse, error) {
|
||||
startTime := time.Now()
|
||||
|
||||
if mgmtKey == "" {
|
||||
return nil, fmt.Errorf("Whitelist-Mgmt-Key 不能为空")
|
||||
}
|
||||
if params == nil {
|
||||
return nil, fmt.Errorf("params不能为空")
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("参数序列化失败: %v", err)
|
||||
}
|
||||
|
||||
encryptedData, err := c.encrypt(string(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("数据加密失败: %v", err)
|
||||
}
|
||||
|
||||
requestBody := map[string]string{"data": encryptedData}
|
||||
requestBodyBytes, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求体序列化失败: %v", err)
|
||||
}
|
||||
|
||||
url := c.baseURL + apiPath
|
||||
httpReq, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(requestBodyBytes))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建HTTP请求失败: %v", err)
|
||||
}
|
||||
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Access-Id", c.accessID)
|
||||
httpReq.Header.Set("Whitelist-Mgmt-Key", mgmtKey)
|
||||
httpReq.Header.Set("User-Agent", "TianyuanAPI-Go-SDK/1.0.0")
|
||||
|
||||
resp, err := c.client.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取响应失败: %v", err)
|
||||
}
|
||||
|
||||
var apiResp ApiResponse
|
||||
if err := json.Unmarshal(body, &apiResp); err != nil {
|
||||
return nil, fmt.Errorf("响应解析失败: %v", err)
|
||||
}
|
||||
|
||||
result := &QueryWhitelistMgmtResponse{
|
||||
Code: apiResp.Code,
|
||||
Message: apiResp.Message,
|
||||
TransactionID: apiResp.TransactionID,
|
||||
Timeout: time.Since(startTime).Milliseconds(),
|
||||
}
|
||||
|
||||
if apiResp.Data != "" {
|
||||
decryptedData, decryptErr := c.decrypt(apiResp.Data)
|
||||
if decryptErr == nil {
|
||||
var entry QueryWhitelistEntry
|
||||
if json.Unmarshal([]byte(decryptedData), &entry) == nil {
|
||||
result.Entry = &entry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -37,11 +37,13 @@ type ServiceContext struct {
|
||||
// 天元API调用记录模型
|
||||
TianyuanapiCallLogModel model.TianyuanapiCallLogModel
|
||||
TianyuanapiCallLogService *service.TianyuanapiCallLogService
|
||||
TianyuanapiClient *tianyuanapi.Client
|
||||
|
||||
// 白名单相关模型
|
||||
UserFeatureWhitelistModel model.UserFeatureWhitelistModel
|
||||
WhitelistOrderModel model.WhitelistOrderModel
|
||||
WhitelistOrderItemModel model.WhitelistOrderItemModel
|
||||
UserFeatureWhitelistModel model.UserFeatureWhitelistModel
|
||||
WhitelistOrderModel model.WhitelistOrderModel
|
||||
WhitelistOrderItemModel model.WhitelistOrderItemModel
|
||||
QueryWhitelistOpLogModel model.QueryWhitelistOpLogModel
|
||||
|
||||
// 订单相关模型
|
||||
OrderModel model.OrderModel
|
||||
@@ -140,6 +142,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
userFeatureWhitelistModel := model.NewUserFeatureWhitelistModel(db, cacheConf)
|
||||
whitelistOrderModel := model.NewWhitelistOrderModel(db, cacheConf)
|
||||
whitelistOrderItemModel := model.NewWhitelistOrderItemModel(db, cacheConf)
|
||||
queryWhitelistOpLogModel := model.NewQueryWhitelistOpLogModel(db, cacheConf)
|
||||
|
||||
// ============================== 订单相关模型 ==============================
|
||||
orderModel := model.NewOrderModel(db, cacheConf)
|
||||
@@ -254,11 +257,13 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
// 天元API调用记录模型
|
||||
TianyuanapiCallLogModel: tianyuanapiCallLogModel,
|
||||
TianyuanapiCallLogService: tianyuanapiCallLogService,
|
||||
TianyuanapiClient: tianyuanapi,
|
||||
|
||||
// 白名单相关模型
|
||||
UserFeatureWhitelistModel: userFeatureWhitelistModel,
|
||||
WhitelistOrderModel: whitelistOrderModel,
|
||||
WhitelistOrderItemModel: whitelistOrderItemModel,
|
||||
QueryWhitelistOpLogModel: queryWhitelistOpLogModel,
|
||||
|
||||
// 订单相关模型
|
||||
OrderModel: orderModel,
|
||||
|
||||
@@ -197,6 +197,78 @@ type AdminCreateWhitelistResp struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
type AdminAppendQueryWhitelistReq struct {
|
||||
Name string `json:"name,optional"`
|
||||
IdCard string `json:"id_card"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark,optional"`
|
||||
}
|
||||
|
||||
type AdminAppendQueryWhitelistResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionId string `json:"transaction_id"`
|
||||
Entry *AdminQueryWhitelistEntry `json:"entry,optional"`
|
||||
}
|
||||
|
||||
type AdminCreateQueryWhitelistReq struct {
|
||||
Name string `json:"name,optional"`
|
||||
IdCard string `json:"id_card"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark,optional"`
|
||||
}
|
||||
|
||||
type AdminCreateQueryWhitelistResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionId string `json:"transaction_id"`
|
||||
Entry *AdminQueryWhitelistEntry `json:"entry,optional"`
|
||||
}
|
||||
|
||||
type AdminQueryWhitelistEntry struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Status string `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type AdminGetQueryWhitelistOpLogListReq struct {
|
||||
Page int64 `form:"page"`
|
||||
PageSize int64 `form:"pageSize"`
|
||||
IdCard *string `form:"id_card,optional"`
|
||||
Action *string `form:"action,optional"`
|
||||
TianyuanCode *int64 `form:"tianyuan_code,optional"`
|
||||
}
|
||||
|
||||
type AdminQueryWhitelistOpLogItem struct {
|
||||
Id string `json:"id"`
|
||||
AdminUserId string `json:"admin_user_id"`
|
||||
AdminUsername string `json:"admin_username"`
|
||||
Action string `json:"action"`
|
||||
ActionText string `json:"action_text"`
|
||||
Name string `json:"name"`
|
||||
IdCard string `json:"id_card"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark"`
|
||||
TianyuanCode int64 `json:"tianyuan_code"`
|
||||
TianyuanMessage string `json:"tianyuan_message"`
|
||||
TransactionId string `json:"transaction_id"`
|
||||
EntryId string `json:"entry_id"`
|
||||
EntryStatus string `json:"entry_status"`
|
||||
EntryApiCodes []string `json:"entry_api_codes"`
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
|
||||
type AdminGetQueryWhitelistOpLogListResp struct {
|
||||
Total int64 `json:"total"`
|
||||
Items []AdminQueryWhitelistOpLogItem `json:"items"`
|
||||
}
|
||||
|
||||
type AdminDeleteApiReq struct {
|
||||
Id string `path:"id"`
|
||||
}
|
||||
|
||||
27
app/main/model/queryWhitelistOpLogModel.go
Normal file
27
app/main/model/queryWhitelistOpLogModel.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ QueryWhitelistOpLogModel = (*customQueryWhitelistOpLogModel)(nil)
|
||||
|
||||
type (
|
||||
// QueryWhitelistOpLogModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customQueryWhitelistOpLogModel.
|
||||
QueryWhitelistOpLogModel interface {
|
||||
queryWhitelistOpLogModel
|
||||
}
|
||||
|
||||
customQueryWhitelistOpLogModel struct {
|
||||
*defaultQueryWhitelistOpLogModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewQueryWhitelistOpLogModel returns a model for the database table.
|
||||
func NewQueryWhitelistOpLogModel(conn sqlx.SqlConn, c cache.CacheConf) QueryWhitelistOpLogModel {
|
||||
return &customQueryWhitelistOpLogModel{
|
||||
defaultQueryWhitelistOpLogModel: newQueryWhitelistOpLogModel(conn, c),
|
||||
}
|
||||
}
|
||||
398
app/main/model/queryWhitelistOpLogModel_gen.go
Normal file
398
app/main/model/queryWhitelistOpLogModel_gen.go
Normal file
@@ -0,0 +1,398 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"ycc-server/common/globalkey"
|
||||
)
|
||||
|
||||
var (
|
||||
queryWhitelistOpLogFieldNames = builder.RawFieldNames(&QueryWhitelistOpLog{})
|
||||
queryWhitelistOpLogRows = strings.Join(queryWhitelistOpLogFieldNames, ",")
|
||||
queryWhitelistOpLogRowsExpectAutoSet = strings.Join(stringx.Remove(queryWhitelistOpLogFieldNames, "`create_time`", "`update_time`"), ",")
|
||||
queryWhitelistOpLogRowsWithPlaceHolder = strings.Join(stringx.Remove(queryWhitelistOpLogFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheYccQueryWhitelistOpLogIdPrefix = "cache:ycc:queryWhitelistOpLog:id:"
|
||||
)
|
||||
|
||||
type (
|
||||
queryWhitelistOpLogModel interface {
|
||||
Insert(ctx context.Context, session sqlx.Session, data *QueryWhitelistOpLog) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id string) (*QueryWhitelistOpLog, error)
|
||||
Update(ctx context.Context, session sqlx.Session, data *QueryWhitelistOpLog) (sql.Result, error)
|
||||
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *QueryWhitelistOpLog) error
|
||||
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
|
||||
SelectBuilder() squirrel.SelectBuilder
|
||||
DeleteSoft(ctx context.Context, session sqlx.Session, data *QueryWhitelistOpLog) error
|
||||
FindSum(ctx context.Context, sumBuilder squirrel.SelectBuilder, field string) (float64, error)
|
||||
FindCount(ctx context.Context, countBuilder squirrel.SelectBuilder, field string) (int64, error)
|
||||
FindAll(ctx context.Context, rowBuilder squirrel.SelectBuilder, orderBy string) ([]*QueryWhitelistOpLog, error)
|
||||
FindPageListByPage(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*QueryWhitelistOpLog, error)
|
||||
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*QueryWhitelistOpLog, int64, error)
|
||||
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*QueryWhitelistOpLog, error)
|
||||
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*QueryWhitelistOpLog, error)
|
||||
Delete(ctx context.Context, session sqlx.Session, id string) error
|
||||
}
|
||||
|
||||
defaultQueryWhitelistOpLogModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
QueryWhitelistOpLog struct {
|
||||
Id string `db:"id"` // UUID主键
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
|
||||
DelState int64 `db:"del_state"` // 删除状态:0=未删除,1=已删除
|
||||
Version int64 `db:"version"` // 版本号(乐观锁)
|
||||
AdminUserId string `db:"admin_user_id"` // 操作管理员ID
|
||||
Action string `db:"action"` // 操作类型:create=创建规则,append=追加接口
|
||||
Name string `db:"name"` // 姓名规则
|
||||
IdCard string `db:"id_card"` // 身份证号(明文,供后台审计)
|
||||
IdCardMasked sql.NullString `db:"id_card_masked"` // 天远返回的脱敏身份证号
|
||||
ApiCodes string `db:"api_codes"` // 本次提交的产品编码(JSON数组)
|
||||
Remark sql.NullString `db:"remark"` // 备注
|
||||
TianyuanCode int64 `db:"tianyuan_code"` // 天远 API 业务码
|
||||
TianyuanMessage sql.NullString `db:"tianyuan_message"` // 天远 API 返回描述
|
||||
TransactionId sql.NullString `db:"transaction_id"` // 天远 API 流水号
|
||||
EntryId sql.NullString `db:"entry_id"` // 天远规则ID(成功时)
|
||||
EntryStatus sql.NullString `db:"entry_status"` // 规则状态(成功时)
|
||||
EntryApiCodes sql.NullString `db:"entry_api_codes"` // 规则当前产品编码列表(JSON数组,成功时)
|
||||
}
|
||||
)
|
||||
|
||||
func newQueryWhitelistOpLogModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultQueryWhitelistOpLogModel {
|
||||
return &defaultQueryWhitelistOpLogModel{
|
||||
CachedConn: sqlc.NewConn(conn, c),
|
||||
table: "`query_whitelist_op_log`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) Insert(ctx context.Context, session sqlx.Session, data *QueryWhitelistOpLog) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
m.insertUUID(data)
|
||||
yccQueryWhitelistOpLogIdKey := fmt.Sprintf("%s%v", cacheYccQueryWhitelistOpLogIdPrefix, data.Id)
|
||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, queryWhitelistOpLogRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.Id, data.DeleteTime, data.DelState, data.Version, data.AdminUserId, data.Action, data.Name, data.IdCard, data.IdCardMasked, data.ApiCodes, data.Remark, data.TianyuanCode, data.TianyuanMessage, data.TransactionId, data.EntryId, data.EntryStatus, data.EntryApiCodes)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.Id, data.DeleteTime, data.DelState, data.Version, data.AdminUserId, data.Action, data.Name, data.IdCard, data.IdCardMasked, data.ApiCodes, data.Remark, data.TianyuanCode, data.TianyuanMessage, data.TransactionId, data.EntryId, data.EntryStatus, data.EntryApiCodes)
|
||||
}, yccQueryWhitelistOpLogIdKey)
|
||||
}
|
||||
func (m *defaultQueryWhitelistOpLogModel) insertUUID(data *QueryWhitelistOpLog) {
|
||||
t := reflect.TypeOf(data).Elem()
|
||||
v := reflect.ValueOf(data).Elem()
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
sf := t.Field(i)
|
||||
if sf.Tag.Get("db") == "id" {
|
||||
f := v.Field(i)
|
||||
if f.IsValid() && f.CanSet() && f.Kind() == reflect.String {
|
||||
if f.String() == "" {
|
||||
f.SetString(uuid.NewString())
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) FindOne(ctx context.Context, id string) (*QueryWhitelistOpLog, error) {
|
||||
yccQueryWhitelistOpLogIdKey := fmt.Sprintf("%s%v", cacheYccQueryWhitelistOpLogIdPrefix, id)
|
||||
var resp QueryWhitelistOpLog
|
||||
err := m.QueryRowCtx(ctx, &resp, yccQueryWhitelistOpLogIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", queryWhitelistOpLogRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) Update(ctx context.Context, session sqlx.Session, data *QueryWhitelistOpLog) (sql.Result, error) {
|
||||
yccQueryWhitelistOpLogIdKey := fmt.Sprintf("%s%v", cacheYccQueryWhitelistOpLogIdPrefix, data.Id)
|
||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, queryWhitelistOpLogRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.AdminUserId, data.Action, data.Name, data.IdCard, data.IdCardMasked, data.ApiCodes, data.Remark, data.TianyuanCode, data.TianyuanMessage, data.TransactionId, data.EntryId, data.EntryStatus, data.EntryApiCodes, data.Id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.AdminUserId, data.Action, data.Name, data.IdCard, data.IdCardMasked, data.ApiCodes, data.Remark, data.TianyuanCode, data.TianyuanMessage, data.TransactionId, data.EntryId, data.EntryStatus, data.EntryApiCodes, data.Id)
|
||||
}, yccQueryWhitelistOpLogIdKey)
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, data *QueryWhitelistOpLog) error {
|
||||
|
||||
oldVersion := data.Version
|
||||
data.Version += 1
|
||||
|
||||
var sqlResult sql.Result
|
||||
var err error
|
||||
|
||||
yccQueryWhitelistOpLogIdKey := fmt.Sprintf("%s%v", cacheYccQueryWhitelistOpLogIdPrefix, data.Id)
|
||||
sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, queryWhitelistOpLogRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.AdminUserId, data.Action, data.Name, data.IdCard, data.IdCardMasked, data.ApiCodes, data.Remark, data.TianyuanCode, data.TianyuanMessage, data.TransactionId, data.EntryId, data.EntryStatus, data.EntryApiCodes, data.Id, oldVersion)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.AdminUserId, data.Action, data.Name, data.IdCard, data.IdCardMasked, data.ApiCodes, data.Remark, data.TianyuanCode, data.TianyuanMessage, data.TransactionId, data.EntryId, data.EntryStatus, data.EntryApiCodes, data.Id, oldVersion)
|
||||
}, yccQueryWhitelistOpLogIdKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updateCount, err := sqlResult.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if updateCount == 0 {
|
||||
return ErrNoRowsUpdate
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) DeleteSoft(ctx context.Context, session sqlx.Session, data *QueryWhitelistOpLog) error {
|
||||
data.DelState = globalkey.DelStateYes
|
||||
data.DeleteTime = sql.NullTime{Time: time.Now(), Valid: true}
|
||||
if err := m.UpdateWithVersion(ctx, session, data); err != nil {
|
||||
return errors.Wrapf(errors.New("delete soft failed "), "QueryWhitelistOpLogModel delete err : %+v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) FindSum(ctx context.Context, builder squirrel.SelectBuilder, field string) (float64, error) {
|
||||
|
||||
if len(field) == 0 {
|
||||
return 0, errors.Wrapf(errors.New("FindSum Least One Field"), "FindSum Least One Field")
|
||||
}
|
||||
|
||||
builder = builder.Columns("IFNULL(SUM(" + field + "),0)")
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var resp float64
|
||||
err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) FindCount(ctx context.Context, builder squirrel.SelectBuilder, field string) (int64, error) {
|
||||
|
||||
if len(field) == 0 {
|
||||
return 0, errors.Wrapf(errors.New("FindCount Least One Field"), "FindCount Least One Field")
|
||||
}
|
||||
|
||||
builder = builder.Columns("COUNT(" + field + ")")
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var resp int64
|
||||
err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) FindAll(ctx context.Context, builder squirrel.SelectBuilder, orderBy string) ([]*QueryWhitelistOpLog, error) {
|
||||
|
||||
builder = builder.Columns(queryWhitelistOpLogRows)
|
||||
|
||||
if orderBy == "" {
|
||||
builder = builder.OrderBy("id DESC")
|
||||
} else {
|
||||
builder = builder.OrderBy(orderBy)
|
||||
}
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp []*QueryWhitelistOpLog
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) FindPageListByPage(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*QueryWhitelistOpLog, error) {
|
||||
|
||||
builder = builder.Columns(queryWhitelistOpLogRows)
|
||||
|
||||
if orderBy == "" {
|
||||
builder = builder.OrderBy("id DESC")
|
||||
} else {
|
||||
builder = builder.OrderBy(orderBy)
|
||||
}
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).Offset(uint64(offset)).Limit(uint64(pageSize)).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp []*QueryWhitelistOpLog
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) FindPageListByPageWithTotal(ctx context.Context, builder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*QueryWhitelistOpLog, int64, error) {
|
||||
|
||||
total, err := m.FindCount(ctx, builder, "id")
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
builder = builder.Columns(queryWhitelistOpLogRows)
|
||||
|
||||
if orderBy == "" {
|
||||
builder = builder.OrderBy("id DESC")
|
||||
} else {
|
||||
builder = builder.OrderBy(orderBy)
|
||||
}
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).Offset(uint64(offset)).Limit(uint64(pageSize)).ToSql()
|
||||
if err != nil {
|
||||
return nil, total, err
|
||||
}
|
||||
|
||||
var resp []*QueryWhitelistOpLog
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, total, nil
|
||||
default:
|
||||
return nil, total, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) FindPageListByIdDESC(ctx context.Context, builder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*QueryWhitelistOpLog, error) {
|
||||
|
||||
builder = builder.Columns(queryWhitelistOpLogRows)
|
||||
|
||||
if preMinId > 0 {
|
||||
builder = builder.Where(" id < ? ", preMinId)
|
||||
}
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).OrderBy("id DESC").Limit(uint64(pageSize)).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp []*QueryWhitelistOpLog
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) FindPageListByIdASC(ctx context.Context, builder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*QueryWhitelistOpLog, error) {
|
||||
|
||||
builder = builder.Columns(queryWhitelistOpLogRows)
|
||||
|
||||
if preMaxId > 0 {
|
||||
builder = builder.Where(" id > ? ", preMaxId)
|
||||
}
|
||||
|
||||
query, values, err := builder.Where("del_state = ?", globalkey.DelStateNo).OrderBy("id ASC").Limit(uint64(pageSize)).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp []*QueryWhitelistOpLog
|
||||
err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
|
||||
switch err {
|
||||
case nil:
|
||||
return resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
|
||||
|
||||
return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
return fn(ctx, session)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) SelectBuilder() squirrel.SelectBuilder {
|
||||
return squirrel.Select().From(m.table)
|
||||
}
|
||||
func (m *defaultQueryWhitelistOpLogModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
|
||||
yccQueryWhitelistOpLogIdKey := fmt.Sprintf("%s%v", cacheYccQueryWhitelistOpLogIdPrefix, id)
|
||||
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, yccQueryWhitelistOpLogIdKey)
|
||||
return err
|
||||
}
|
||||
func (m *defaultQueryWhitelistOpLogModel) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheYccQueryWhitelistOpLogIdPrefix, primary)
|
||||
}
|
||||
func (m *defaultQueryWhitelistOpLogModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", queryWhitelistOpLogRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary, globalkey.DelStateNo)
|
||||
}
|
||||
|
||||
func (m *defaultQueryWhitelistOpLogModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
Reference in New Issue
Block a user