add
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package admin_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/ctxdata"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminBatchCreateWhitelistLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminBatchCreateWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminBatchCreateWhitelistLogic {
|
||||
return &AdminBatchCreateWhitelistLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminBatchCreateWhitelistLogic) AdminBatchCreateWhitelist(req *types.AdminBatchCreateWhitelistReq) (resp *types.AdminBatchCreateWhitelistResp, err error) {
|
||||
if req.IdCard == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("身份证号不能为空"), "")
|
||||
}
|
||||
if len(req.FeatureIds) == 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("请至少选择一个模块"), "")
|
||||
}
|
||||
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取管理员信息失败, %v", err)
|
||||
}
|
||||
|
||||
amount := 0.0
|
||||
if req.Amount != nil {
|
||||
amount = *req.Amount
|
||||
}
|
||||
status, err := parseWhitelistStatus(req.Status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = &types.AdminBatchCreateWhitelistResp{
|
||||
Items: make([]types.AdminBatchCreateWhitelistResultItem, 0, len(req.FeatureIds)),
|
||||
}
|
||||
|
||||
for _, featureId := range req.FeatureIds {
|
||||
item := types.AdminBatchCreateWhitelistResultItem{
|
||||
FeatureId: featureId,
|
||||
}
|
||||
|
||||
feature, findErr := l.svcCtx.FeatureModel.FindOne(l.ctx, featureId)
|
||||
if findErr != nil {
|
||||
if errors.Is(findErr, model.ErrNotFound) {
|
||||
item.Action = "failed"
|
||||
item.Message = "模块不存在"
|
||||
resp.FailCount++
|
||||
} else {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询模块失败, %v", findErr)
|
||||
}
|
||||
resp.Items = append(resp.Items, item)
|
||||
continue
|
||||
}
|
||||
|
||||
result, createErr := createOrReactivateWhitelist(l.ctx, l.svcCtx, userID, req.IdCard, feature, amount, status)
|
||||
if createErr != nil {
|
||||
item.FeatureApiId = feature.ApiId
|
||||
item.FeatureName = feature.Name
|
||||
item.Action = "failed"
|
||||
item.Message = createErr.Error()
|
||||
resp.FailCount++
|
||||
resp.Items = append(resp.Items, item)
|
||||
continue
|
||||
}
|
||||
|
||||
item.FeatureApiId = result.FeatureApiId
|
||||
item.FeatureName = result.FeatureName
|
||||
item.Action = result.Action
|
||||
item.Message = result.Message
|
||||
switch result.Action {
|
||||
case whitelistActionSkipped:
|
||||
resp.SkipCount++
|
||||
case whitelistActionCreated, whitelistActionReactivated:
|
||||
resp.SuccessCount++
|
||||
}
|
||||
resp.Items = append(resp.Items, item)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package admin_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/ctxdata"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminCreateWhitelistLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminCreateWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminCreateWhitelistLogic {
|
||||
return &AdminCreateWhitelistLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminCreateWhitelistLogic) AdminCreateWhitelist(req *types.AdminCreateWhitelistReq) (resp *types.AdminCreateWhitelistResp, err error) {
|
||||
if req.IdCard == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("身份证号不能为空"), "")
|
||||
}
|
||||
if req.FeatureId == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("模块不能为空"), "")
|
||||
}
|
||||
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取管理员信息失败, %v", err)
|
||||
}
|
||||
|
||||
feature, err := l.svcCtx.FeatureModel.FindOne(l.ctx, req.FeatureId)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("模块不存在"), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询模块失败, %v", err)
|
||||
}
|
||||
|
||||
amount := 0.0
|
||||
if req.Amount != nil {
|
||||
amount = *req.Amount
|
||||
}
|
||||
status, err := parseWhitelistStatus(req.Status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := createOrReactivateWhitelist(l.ctx, l.svcCtx, userID, req.IdCard, feature, amount, status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.Action == whitelistActionSkipped {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("该身份证号已存在生效的白名单记录"), "")
|
||||
}
|
||||
|
||||
return &types.AdminCreateWhitelistResp{Id: result.Id}, nil
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package admin_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/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminDeleteWhitelistLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminDeleteWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminDeleteWhitelistLogic {
|
||||
return &AdminDeleteWhitelistLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminDeleteWhitelistLogic) AdminDeleteWhitelist(req *types.AdminDeleteWhitelistReq) (resp *types.AdminDeleteWhitelistResp, err error) {
|
||||
record, err := l.svcCtx.UserFeatureWhitelistModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("白名单记录不存在"), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单记录失败, %v", err)
|
||||
}
|
||||
|
||||
if err = l.svcCtx.UserFeatureWhitelistModel.DeleteSoft(l.ctx, nil, record); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "删除白名单记录失败, %v", err)
|
||||
}
|
||||
|
||||
return &types.AdminDeleteWhitelistResp{Success: true}, nil
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package admin_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 AdminGetWhitelistListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminGetWhitelistListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetWhitelistListLogic {
|
||||
return &AdminGetWhitelistListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminGetWhitelistListLogic) AdminGetWhitelistList(req *types.AdminGetWhitelistListReq) (resp *types.AdminGetWhitelistListResp, err error) {
|
||||
builder := l.svcCtx.UserFeatureWhitelistModel.SelectBuilder()
|
||||
|
||||
if req.IdCard != nil && *req.IdCard != "" {
|
||||
builder = builder.Where("id_card LIKE ?", "%"+*req.IdCard+"%")
|
||||
}
|
||||
if req.FeatureApiId != nil && *req.FeatureApiId != "" {
|
||||
builder = builder.Where("feature_api_id LIKE ?", "%"+*req.FeatureApiId+"%")
|
||||
}
|
||||
if req.Status != nil {
|
||||
builder = builder.Where("status = ?", *req.Status)
|
||||
}
|
||||
|
||||
total, err := l.svcCtx.UserFeatureWhitelistModel.FindCount(l.ctx, builder, "id")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单总数失败, %v", err)
|
||||
}
|
||||
|
||||
list, err := l.svcCtx.UserFeatureWhitelistModel.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)
|
||||
}
|
||||
|
||||
featureNameMap := l.buildFeatureNameMap(list)
|
||||
|
||||
items := make([]types.AdminWhitelistListItem, 0, len(list))
|
||||
for _, w := range list {
|
||||
items = append(items, types.AdminWhitelistListItem{
|
||||
Id: w.Id,
|
||||
IdCard: w.IdCard,
|
||||
FeatureId: w.FeatureId,
|
||||
FeatureApiId: w.FeatureApiId,
|
||||
FeatureName: featureNameMap[w.FeatureId],
|
||||
UserId: w.UserId,
|
||||
Amount: w.Amount,
|
||||
Status: w.Status,
|
||||
StatusText: whitelistStatusText(w.Status),
|
||||
CreateTime: w.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: w.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &types.AdminGetWhitelistListResp{
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetWhitelistListLogic) buildFeatureNameMap(list []*model.UserFeatureWhitelist) map[string]string {
|
||||
featureNameMap := make(map[string]string)
|
||||
if len(list) == 0 {
|
||||
return featureNameMap
|
||||
}
|
||||
|
||||
featureIds := make([]string, 0, len(list))
|
||||
seen := make(map[string]struct{}, len(list))
|
||||
for _, w := range list {
|
||||
if _, ok := seen[w.FeatureId]; ok {
|
||||
continue
|
||||
}
|
||||
seen[w.FeatureId] = struct{}{}
|
||||
featureIds = append(featureIds, w.FeatureId)
|
||||
}
|
||||
|
||||
featureBuilder := l.svcCtx.FeatureModel.SelectBuilder().Where(squirrel.Eq{"id": featureIds})
|
||||
features, err := l.svcCtx.FeatureModel.FindAll(l.ctx, featureBuilder, "")
|
||||
if err != nil {
|
||||
l.Errorf("批量查询模块名称失败: %v", err)
|
||||
return featureNameMap
|
||||
}
|
||||
for _, f := range features {
|
||||
featureNameMap[f.Id] = f.Name
|
||||
}
|
||||
return featureNameMap
|
||||
}
|
||||
|
||||
func whitelistStatusText(status int64) string {
|
||||
if status == 1 {
|
||||
return "生效"
|
||||
}
|
||||
return "已失效"
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package admin_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/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminUpdateWhitelistLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminUpdateWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUpdateWhitelistLogic {
|
||||
return &AdminUpdateWhitelistLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminUpdateWhitelistLogic) AdminUpdateWhitelist(req *types.AdminUpdateWhitelistReq) (resp *types.AdminUpdateWhitelistResp, err error) {
|
||||
if req.Status == nil && req.Amount == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("请至少提供一个更新字段"), "")
|
||||
}
|
||||
|
||||
record, err := l.svcCtx.UserFeatureWhitelistModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("白名单记录不存在"), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单记录失败, %v", err)
|
||||
}
|
||||
|
||||
if req.Status != nil {
|
||||
if *req.Status != 1 && *req.Status != 2 {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("状态值无效,仅支持 1=生效 或 2=已失效"), "")
|
||||
}
|
||||
record.Status = *req.Status
|
||||
}
|
||||
if req.Amount != nil {
|
||||
record.Amount = *req.Amount
|
||||
}
|
||||
|
||||
if err = l.svcCtx.UserFeatureWhitelistModel.UpdateWithVersion(l.ctx, nil, record); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新白名单记录失败, %v", err)
|
||||
}
|
||||
|
||||
return &types.AdminUpdateWhitelistResp{Success: true}, nil
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package admin_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
whitelistActionCreated = "created"
|
||||
whitelistActionReactivated = "reactivated"
|
||||
whitelistActionSkipped = "skipped"
|
||||
)
|
||||
|
||||
type createWhitelistResult struct {
|
||||
Id string
|
||||
FeatureApiId string
|
||||
FeatureName string
|
||||
Action string
|
||||
Message string
|
||||
}
|
||||
|
||||
func createOrReactivateWhitelist(
|
||||
ctx context.Context,
|
||||
svcCtx *svc.ServiceContext,
|
||||
userID string,
|
||||
idCard string,
|
||||
feature *model.Feature,
|
||||
amount float64,
|
||||
status int64,
|
||||
) (*createWhitelistResult, error) {
|
||||
exists, err := svcCtx.WhitelistService.CheckWhitelistExists(ctx, idCard, feature.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if exists {
|
||||
return &createWhitelistResult{
|
||||
FeatureApiId: feature.ApiId,
|
||||
FeatureName: feature.Name,
|
||||
Action: whitelistActionSkipped,
|
||||
Message: "已存在生效白名单",
|
||||
}, nil
|
||||
}
|
||||
|
||||
builder := svcCtx.UserFeatureWhitelistModel.SelectBuilder().
|
||||
Where("id_card = ? AND feature_id = ?", idCard, feature.Id)
|
||||
records, err := svcCtx.UserFeatureWhitelistModel.FindAll(ctx, builder, "")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单记录失败, %v", err)
|
||||
}
|
||||
for _, record := range records {
|
||||
if record.Status == 2 {
|
||||
record.Status = status
|
||||
record.Amount = amount
|
||||
record.UserId = userID
|
||||
if updateErr := svcCtx.UserFeatureWhitelistModel.UpdateWithVersion(ctx, nil, record); updateErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新白名单记录失败, %v", updateErr)
|
||||
}
|
||||
return &createWhitelistResult{
|
||||
Id: record.Id,
|
||||
FeatureApiId: feature.ApiId,
|
||||
FeatureName: feature.Name,
|
||||
Action: whitelistActionReactivated,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
wl := &model.UserFeatureWhitelist{
|
||||
Id: uuid.NewString(),
|
||||
IdCard: idCard,
|
||||
FeatureId: feature.Id,
|
||||
FeatureApiId: feature.ApiId,
|
||||
UserId: userID,
|
||||
Amount: amount,
|
||||
Status: status,
|
||||
}
|
||||
if _, err = svcCtx.UserFeatureWhitelistModel.Insert(ctx, nil, wl); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建白名单记录失败, %v", err)
|
||||
}
|
||||
|
||||
return &createWhitelistResult{
|
||||
Id: wl.Id,
|
||||
FeatureApiId: feature.ApiId,
|
||||
FeatureName: feature.Name,
|
||||
Action: whitelistActionCreated,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func parseWhitelistStatus(status *int64) (int64, error) {
|
||||
if status == nil {
|
||||
return 1, nil
|
||||
}
|
||||
if *status != 1 && *status != 2 {
|
||||
return 0, errors.Wrapf(xerr.NewErrMsg("状态值无效,仅支持 1=生效 或 2=已失效"), "")
|
||||
}
|
||||
return *status, nil
|
||||
}
|
||||
Reference in New Issue
Block a user