feat: 接入旧站管理端并完整迁移后台能力
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
package qualityowner
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ConfigKey = "quality_owner.rules"
|
||||
StatusNone = "none"
|
||||
StatusActive = "active"
|
||||
DepositSourcePaid = "paid"
|
||||
DepositSourceManual = "manual"
|
||||
defaultDepositCent = int64(20_000)
|
||||
defaultInsuranceBps = 800
|
||||
defaultPremiumRateBps = 400
|
||||
defaultAgreementReadSeconds = 15
|
||||
defaultAgreementTitle = "优质号主服务协议"
|
||||
MaxAgreementReadSeconds = 300
|
||||
)
|
||||
|
||||
var ErrInvalidRules = errors.New("invalid quality owner rules")
|
||||
|
||||
type Rules struct {
|
||||
DepositAmountCent int64 `json:"deposit_amount_cent"`
|
||||
InsuranceFeeBps int `json:"insurance_fee_bps"`
|
||||
PremiumInsuranceFeeBps int `json:"premium_insurance_fee_bps"`
|
||||
AgreementTitle string `json:"agreement_title"`
|
||||
AgreementContent string `json:"agreement_content"`
|
||||
AgreementReadSeconds int `json:"agreement_read_seconds"`
|
||||
}
|
||||
|
||||
func DefaultRules() Rules {
|
||||
return Rules{
|
||||
DepositAmountCent: defaultDepositCent,
|
||||
InsuranceFeeBps: defaultInsuranceBps,
|
||||
PremiumInsuranceFeeBps: defaultPremiumRateBps,
|
||||
AgreementTitle: defaultAgreementTitle,
|
||||
AgreementReadSeconds: defaultAgreementReadSeconds,
|
||||
AgreementContent: `本协议由大锤商行平台(以下简称“平台”)与申请成为“优质号主”的用户(以下简称“号主”)共同签署,自号主勾选同意并完成信用保证金冻结之日起生效。
|
||||
|
||||
一、总则
|
||||
|
||||
1.1 为保障平台账号租赁交易的秩序与安全,平台推出「优质号主」认证计划,旨在筛选信誉良好、服务高效的号主,为其提供专属权益与曝光资源,包括将账号发布至「极品专区」并享有专项收益比例等。
|
||||
|
||||
1.2 号主自愿申请成为优质号主,并承诺遵守本协议全部条款。
|
||||
|
||||
二、定义
|
||||
|
||||
| 术语 | 定义 |
|
||||
|---|---|
|
||||
| 优质号主 | 通过平台认证、完成200元信用保证金冻结并遵守本协议的高信誉号主 |
|
||||
| 《极品专区》 | 优质号主可选择发布的专属渠道,仅限大锤商行至尊SVIP用户租用;优质号主在该渠道发布账号可享有专项收益比例及专属标识 |
|
||||
| 《大锤商行》 | 优质号主可选择发布的公开渠道,面向全平台注册用户租用 |
|
||||
| 信用保证金(押金) | 号主为获得优质号主发布资格而授权平台冻结的200元信用保证金,仅冻结、不扣除 |
|
||||
| 大锤商行至尊SVIP | 平台最高等级VIP用户,须同时满足租客免押资格方可租用《极品专区》渠道账号 |
|
||||
|
||||
三、成为优质号主的条件
|
||||
|
||||
3.1 信用保证金标准
|
||||
|
||||
本计划仅设一个信用保证金档位,所有申请者统一适用:
|
||||
|
||||
| 申请类型 | 信用保证金 | 备注 |
|
||||
|---|---:|---|
|
||||
| 所有申请者 | 200元 | 仅冻结,不扣除;不设其他金额档位或免押档位 |
|
||||
|
||||
3.2 强制前置条件
|
||||
|
||||
所有申请者须强制阅读《账号出租号主须知》满15秒,方可继续申请流程。
|
||||
|
||||
3.3 信用保证金特别约定
|
||||
|
||||
3.3.1 保证金性质:200元为信用保证金,仅用于信用展示及发布资格核验,不属于租赁费用、平台服务收入、违约金或处罚金。平台不扣除、不没收该保证金。
|
||||
|
||||
3.3.2 冻结方式:号主申请成为优质号主时,授权平台冻结200元信用保证金。冻结期间,该保证金不参与扣罚。
|
||||
|
||||
3.3.3 解冻条件:号主在无正在进行中的订单且无未解决的纠纷时,可随时向平台申请解冻全部信用保证金。平台审核通过后,按原支付路径或平台规则允许的方式解冻退还。
|
||||
|
||||
3.3.4 校验机制:每次发布账号时,系统自动核验信用保证金是否足额冻结:
|
||||
|
||||
- 已足额冻结 → 可正常发布;
|
||||
- 未冻结或冻结不足 → 不可发布,系统弹窗提示“请先冻结200元信用保证金”。
|
||||
|
||||
3.3.5 违规处理与保证金:号主单方面违规的,平台有权依据本协议及平台规则采取警告、下架账号、限制发布、取消优质号主资格等措施,但信用保证金不扣除。如存在未完成订单或未解决纠纷,信用保证金可继续冻结至相关事项处理完毕。
|
||||
|
||||
四、优质号主的权利
|
||||
|
||||
成功成为优质号主后,享有以下权益:
|
||||
|
||||
| 序号 | 权益内容 | 具体说明 |
|
||||
|---|---|---|
|
||||
| 4.1 | 专项收益比例 | 优质号主将账号发布至「极品专区」的,享有专项收益比例:号主到手金额比例较普通渠道上浮1个百分点(即平台服务费比例相应下调1个百分点,具体以发布页面公示为准);该权益可与VIP权益及优惠券叠加使用 |
|
||||
| 4.2 | 发布渠道选择 | 发布时可自由选择投放至「极品专区」或「大锤商行」(详见第五条) |
|
||||
| 4.3 | 特殊曝光标识 | 优质号主发布的账号显示“优质号主”专属标识;发布至「极品专区」的账号同时展示“极品专区”专属标签 |
|
||||
| 4.4 | 包赔手续费减半 | 购买全额包赔服务,手续费由8%降至4% |
|
||||
| 4.5 | 平台兜底保障 | 非因号主明显过错导致的问题(如租客恶意损坏账号等),由平台按规则负责兜底赔偿 |
|
||||
|
||||
五、发布渠道规则
|
||||
|
||||
优质号主在每次发布账号时,必须从以下两个渠道中二选一:
|
||||
|
||||
5.1 渠道对比
|
||||
|
||||
| 对比项 | 《极品专区》 | 《大锤商行》 |
|
||||
|---|---|---|
|
||||
| 租用资格 | 仅限大锤商行至尊SVIP,且须具备租客免押资格 | 全平台注册用户 |
|
||||
| 租客筛选标准 | 累计消费≥1万元 + 无纠纷记录 + 无封禁记录 | 无特殊限制 |
|
||||
| 适用场景 | 高客单价、极致安全、专项收益比例 | 高曝光、快速出单 |
|
||||
|
||||
5.2 关于《极品专区》渠道
|
||||
|
||||
5.2.1 选择该渠道的账号,将展示“极品专区”专属标签。
|
||||
|
||||
5.2.2 非大锤商行至尊SVIP用户,或虽为大锤商行至尊SVIP但无租客免押资格的,不可查看及租用该渠道账号。
|
||||
|
||||
5.2.3 该渠道租客均为平台严选的优质租客,号主可享受“基本绝对安全”的出租体验。
|
||||
|
||||
5.2.4 优质号主在该渠道发布账号,可按本协议第4.1款享有专项收益比例。
|
||||
|
||||
5.3 关于《大锤商行》渠道
|
||||
|
||||
5.3.1 选择该渠道的账号,面向全体平台注册用户开放租用。
|
||||
|
||||
5.3.2 号主仍享有平台兜底保障权益(详见第4.5款)。
|
||||
|
||||
六、号主的义务与违规处理
|
||||
|
||||
6.1 号主的基本义务
|
||||
|
||||
优质号主在交易中应当:
|
||||
|
||||
6.1.1 按照约定时间交付账号,不得无故毁约、爽约;
|
||||
|
||||
6.1.2 在租客使用过程中,及时配合上号验证;
|
||||
|
||||
6.1.3 在需要时,积极配合完成人脸识别解除等操作;
|
||||
|
||||
6.1.4 不得在出租期间擅自顶号、修改密码或通过其他方式收回账号;
|
||||
|
||||
6.1.5 与租客保持友善沟通,不得出现辱骂、威胁等不文明行为。
|
||||
|
||||
6.2 违规行为与处罚
|
||||
|
||||
平台对违规行为采取警告、下架、限制发布、取消资格等措施;信用保证金不扣除。
|
||||
|
||||
| 违规行为 | 处理措施 |
|
||||
|---|---|
|
||||
| 顶号 | 警告、立即下架、限制发布;情节严重者取消优质号主资格 |
|
||||
| 毁约不出租 | 警告、下架、限制发布;情节严重者取消优质号主资格 |
|
||||
| 无法及时配合上号 | 警告、限制发布;多次违规者取消资格 |
|
||||
| 无法配合解除人脸 | 警告、下架、限制发布;情节严重者取消资格 |
|
||||
| 其他违反平台规则的行为 | 依据平台规则处理 |
|
||||
|
||||
上述处理均不扣除信用保证金;如存在未完成订单或未解决纠纷,信用保证金可继续冻结至相关事项处理完毕。
|
||||
|
||||
七、收益与结算
|
||||
|
||||
7.1 号主通过本平台获得的租赁收入,平台将按对应渠道的结算规则进行结算。
|
||||
|
||||
7.2 优质号主将账号发布至「极品专区」的,按本协议第4.1款享有专项收益比例;该权益可与VIP权益及优惠券叠加使用。
|
||||
|
||||
7.3 平台将在租客确认收货后将收益划转至号主账户余额,号主可随时申请提现。
|
||||
|
||||
八、免责条款
|
||||
|
||||
8.1 因不可抗力(包括但不限于自然灾害、战争、罢工、政府行为、网络故障、系统维护等)导致协议无法履行的,双方互不承担责任。
|
||||
|
||||
8.2 平台有权根据法律法规变化或业务运营需要,对本协议进行修订,修订后的协议将在平台公示。如号主继续使用优质号主服务,视为接受修订内容。
|
||||
|
||||
九、协议终止
|
||||
|
||||
9.1 号主可随时申请退出优质号主计划。满足信用保证金解冻条件的,200元信用保证金予以解冻退还,优质号主资格即时取消。
|
||||
|
||||
9.2 号主严重违反本协议或平台规则的,平台有权单方面取消其优质号主资格。信用保证金不扣除;如存在未完成订单或未解决纠纷,可继续冻结至相关事项处理完毕。
|
||||
|
||||
9.3 优质号主资格取消后,对应的权益(特殊曝光、包赔手续费减半、发布渠道选择、极品专区专项收益比例等)同步终止。
|
||||
|
||||
十、附则
|
||||
|
||||
10.1 本协议的解释权归大锤商行平台所有。
|
||||
|
||||
10.2 本协议自号主勾选同意并完成200元信用保证金冻结之日起生效。
|
||||
|
||||
10.3 如本协议与平台其他规则存在冲突,以本协议为准。
|
||||
|
||||
本人已详细阅读并充分理解本协议全部内容,同意遵守上述条款。`,
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultConfigValue() string {
|
||||
raw, _ := json.Marshal(DefaultRules())
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
func ParseConfigValue(value string) (Rules, error) {
|
||||
rules := DefaultRules()
|
||||
if value == "" {
|
||||
return rules, nil
|
||||
}
|
||||
if err := json.Unmarshal([]byte(value), &rules); err != nil {
|
||||
return Rules{}, ErrInvalidRules
|
||||
}
|
||||
if rules.DepositAmountCent <= 0 || rules.DepositAmountCent > 10_000_000 {
|
||||
return Rules{}, ErrInvalidRules
|
||||
}
|
||||
if rules.InsuranceFeeBps < 0 || rules.InsuranceFeeBps > 10_000 {
|
||||
return Rules{}, ErrInvalidRules
|
||||
}
|
||||
if rules.PremiumInsuranceFeeBps < 0 || rules.PremiumInsuranceFeeBps > rules.InsuranceFeeBps {
|
||||
return Rules{}, ErrInvalidRules
|
||||
}
|
||||
rules.AgreementTitle = strings.TrimSpace(rules.AgreementTitle)
|
||||
rules.AgreementContent = strings.TrimSpace(rules.AgreementContent)
|
||||
if rules.AgreementTitle == "" || rules.AgreementContent == "" {
|
||||
return Rules{}, ErrInvalidRules
|
||||
}
|
||||
if rules.AgreementReadSeconds < defaultAgreementReadSeconds ||
|
||||
rules.AgreementReadSeconds > MaxAgreementReadSeconds {
|
||||
return Rules{}, ErrInvalidRules
|
||||
}
|
||||
return rules, nil
|
||||
}
|
||||
|
||||
func MarshalConfig(rules Rules) (string, error) {
|
||||
raw, err := json.Marshal(rules)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(raw), nil
|
||||
}
|
||||
|
||||
type StatusDTO struct {
|
||||
UserID uint64 `json:"user_id"`
|
||||
Active bool `json:"active"`
|
||||
Status string `json:"status"`
|
||||
CanCancel bool `json:"can_cancel"`
|
||||
ActivePremiumListings int64 `json:"active_premium_listing_count"`
|
||||
DepositAmountCent int64 `json:"deposit_amount_cent"`
|
||||
DepositSource string `json:"deposit_source"`
|
||||
ActivatedAt *time.Time `json:"activated_at"`
|
||||
AvailableBalanceCent int64 `json:"available_balance_cent"`
|
||||
RequiredDepositCent int64 `json:"required_deposit_cent"`
|
||||
InsuranceFeeBps int `json:"insurance_fee_bps"`
|
||||
PremiumInsuranceFeeBps int `json:"premium_insurance_fee_bps"`
|
||||
AgreementTitle string `json:"agreement_title"`
|
||||
AgreementContent string `json:"agreement_content"`
|
||||
AgreementReadSeconds int `json:"agreement_read_seconds"`
|
||||
}
|
||||
|
||||
type AdminSetRequest struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Reason string `json:"reason" binding:"required"`
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package qualityowner
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseConfigValueDefaultsAndValidation(t *testing.T) {
|
||||
rules, err := ParseConfigValue(`{"insurance_fee_bps":800,"premium_insurance_fee_bps":400}`)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseConfigValue() error = %v", err)
|
||||
}
|
||||
if rules.DepositAmountCent != defaultDepositCent || rules.InsuranceFeeBps != 800 || rules.PremiumInsuranceFeeBps != 400 {
|
||||
t.Fatalf("unexpected rules: %+v", rules)
|
||||
}
|
||||
if rules.AgreementTitle != defaultAgreementTitle ||
|
||||
rules.AgreementReadSeconds != defaultAgreementReadSeconds ||
|
||||
rules.AgreementContent == "" {
|
||||
t.Fatalf("unexpected agreement defaults: %+v", rules)
|
||||
}
|
||||
|
||||
if _, err := ParseConfigValue(`{"deposit_amount_cent":20000,"insurance_fee_bps":400,"premium_insurance_fee_bps":800}`); err == nil {
|
||||
t.Fatal("expected premium rate above normal rate to be rejected")
|
||||
}
|
||||
if _, err := ParseConfigValue(`{"deposit_amount_cent":0}`); err == nil {
|
||||
t.Fatal("expected zero deposit to be rejected")
|
||||
}
|
||||
if _, err := ParseConfigValue(`{"agreement_read_seconds":14}`); err == nil {
|
||||
t.Fatal("expected agreement reading time below 15 seconds to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultConfigValueCanBeParsed(t *testing.T) {
|
||||
rules, err := ParseConfigValue(DefaultConfigValue())
|
||||
if err != nil {
|
||||
t.Fatalf("ParseConfigValue(DefaultConfigValue()) error = %v", err)
|
||||
}
|
||||
if rules.DepositAmountCent != 20000 || rules.InsuranceFeeBps != 800 || rules.PremiumInsuranceFeeBps != 400 {
|
||||
t.Fatalf("unexpected default rules: %+v", rules)
|
||||
}
|
||||
if rules.AgreementReadSeconds != 15 || rules.AgreementTitle == "" || rules.AgreementContent == "" {
|
||||
t.Fatalf("unexpected default agreement: %+v", rules)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package qualityowner
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"hfb_jysh/backend/internal/middleware"
|
||||
"hfb_jysh/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
func (h *Handler) Status(c *gin.Context) {
|
||||
userID, ok := currentUserID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
return
|
||||
}
|
||||
item, err := h.service.Status(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
func (h *Handler) Activate(c *gin.Context) {
|
||||
userID, ok := currentUserID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
return
|
||||
}
|
||||
item, err := h.service.Activate(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
func (h *Handler) Cancel(c *gin.Context) {
|
||||
userID, ok := currentUserID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
return
|
||||
}
|
||||
item, err := h.service.Cancel(c.Request.Context(), userID, AuditMeta{
|
||||
IP: c.ClientIP(),
|
||||
UserAgent: c.GetHeader("User-Agent"),
|
||||
RequestID: middleware.GetRequestID(c),
|
||||
})
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
func (h *Handler) AdminSet(c *gin.Context) {
|
||||
adminID, ok := currentAdminID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少管理员上下文")
|
||||
return
|
||||
}
|
||||
userID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil || userID == 0 {
|
||||
response.BadRequest(c, "用户 ID 不正确")
|
||||
return
|
||||
}
|
||||
var req AdminSetRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "请填写操作原因")
|
||||
return
|
||||
}
|
||||
item, err := h.service.AdminSet(c.Request.Context(), adminID, userID, req, AuditMeta{
|
||||
IP: c.ClientIP(),
|
||||
UserAgent: c.GetHeader("User-Agent"),
|
||||
RequestID: middleware.GetRequestID(c),
|
||||
})
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
func currentUserID(c *gin.Context) (uint64, bool) {
|
||||
value, ok := c.Get(middleware.ContextUserID)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
id, ok := value.(uint64)
|
||||
return id, ok
|
||||
}
|
||||
|
||||
func currentAdminID(c *gin.Context) (uint64, bool) {
|
||||
value, ok := c.Get(middleware.ContextAdminID)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
id, ok := value.(uint64)
|
||||
return id, ok
|
||||
}
|
||||
|
||||
func writeError(c *gin.Context, err error) {
|
||||
response.RecordError(c, err)
|
||||
switch {
|
||||
case errors.Is(err, ErrDependencyUnavailable):
|
||||
response.ServiceUnavailable(c, "数据库未连接")
|
||||
case errors.Is(err, ErrInvalidUser):
|
||||
response.BadRequest(c, "用户不符合开通条件,请先完成实名认证")
|
||||
case errors.Is(err, ErrDepositInsufficient):
|
||||
response.Error(c, http.StatusConflict, "insufficient_balance", "余额不足,请先确保可用余额足够缴纳保证金")
|
||||
case errors.Is(err, ErrAlreadyActive):
|
||||
response.Error(c, http.StatusConflict, "quality_owner_active", "当前已经是优质号主")
|
||||
case errors.Is(err, ErrPaymentPending):
|
||||
response.Error(c, http.StatusConflict, "quality_owner_payment_pending", "已有待完成的保证金支付,请先完成支付或稍后重试")
|
||||
case errors.Is(err, ErrInactive):
|
||||
response.Error(c, http.StatusConflict, "quality_owner_inactive", "当前不是优质号主")
|
||||
case errors.Is(err, ErrActivePremiumListings):
|
||||
var activeErr ActivePremiumListingsError
|
||||
errors.As(err, &activeErr)
|
||||
response.Error(
|
||||
c,
|
||||
http.StatusConflict,
|
||||
"quality_owner_active_premium_listings",
|
||||
fmt.Sprintf("极品专区还有 %d 个进行中账号,请先下架或完成订单后再取消", activeErr.Count),
|
||||
)
|
||||
case errors.Is(err, ErrInvalidReason):
|
||||
response.BadRequest(c, "请填写 2-200 字的操作原因")
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
response.Error(c, http.StatusNotFound, "not_found", "用户不存在")
|
||||
default:
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "优质号主服务暂时不可用")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,493 @@
|
||||
package qualityowner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_jysh/backend/internal/auditlog"
|
||||
"hfb_jysh/backend/internal/model"
|
||||
"hfb_jysh/backend/internal/modules/wallet"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
ErrInvalidUser = errors.New("invalid user")
|
||||
ErrAlreadyActive = errors.New("quality owner already active")
|
||||
ErrInactive = errors.New("quality owner inactive")
|
||||
ErrDepositInsufficient = errors.New("quality owner deposit insufficient")
|
||||
ErrInvalidReason = errors.New("invalid reason")
|
||||
ErrPaymentPending = errors.New("quality owner payment pending")
|
||||
ErrActivePremiumListings = errors.New("active premium listings exist")
|
||||
)
|
||||
|
||||
type ActivePremiumListingsError struct {
|
||||
Count int64
|
||||
}
|
||||
|
||||
func (e ActivePremiumListingsError) Error() string {
|
||||
return ErrActivePremiumListings.Error()
|
||||
}
|
||||
|
||||
func (e ActivePremiumListingsError) Is(target error) bool {
|
||||
return target == ErrActivePremiumListings
|
||||
}
|
||||
|
||||
type AuditMeta = auditlog.Meta
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) Rules(ctx context.Context) (Rules, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return Rules{}, ErrDependencyUnavailable
|
||||
}
|
||||
var row model.SystemConfig
|
||||
err := r.db.WithContext(ctx).Where("`key` = ?", ConfigKey).First(&row).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return DefaultRules(), nil
|
||||
}
|
||||
if err != nil {
|
||||
return Rules{}, err
|
||||
}
|
||||
return ParseConfigValue(row.Value)
|
||||
}
|
||||
|
||||
func (r *Repository) Status(ctx context.Context, userID uint64) (*StatusDTO, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
rules, err := r.Rules(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var user model.User
|
||||
if err := r.db.WithContext(ctx).First(&user, userID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var account model.WalletAccount
|
||||
walletErr := r.db.WithContext(ctx).
|
||||
Where("user_id = ? AND distribution_site_id = 0", userID).
|
||||
First(&account).Error
|
||||
if walletErr != nil && !errors.Is(walletErr, gorm.ErrRecordNotFound) {
|
||||
return nil, walletErr
|
||||
}
|
||||
activePremiumListings, err := activePremiumListingCount(r.db.WithContext(ctx), userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toStatusDTO(user, account, rules, activePremiumListings), nil
|
||||
}
|
||||
|
||||
// PaymentAmount 返回当前用户在线支付开通优质号主所需的保证金金额。
|
||||
// active=true 表示用户已经是优质号主,无需再创建支付单。
|
||||
func (r *Repository) PaymentAmount(ctx context.Context, userID uint64) (int64, bool, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return 0, false, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 {
|
||||
return 0, false, ErrInvalidUser
|
||||
}
|
||||
var user model.User
|
||||
if err := r.db.WithContext(ctx).First(&user, userID).Error; err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if user.QualityOwnerStatus == StatusActive {
|
||||
return 0, true, nil
|
||||
}
|
||||
if user.Status != "active" || user.RealnameStatus != "verified" {
|
||||
return 0, false, ErrInvalidUser
|
||||
}
|
||||
rules, err := r.Rules(ctx)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return rules.DepositAmountCent, false, nil
|
||||
}
|
||||
|
||||
// ConfirmQualityOwnerDepositPaidTx 在支付回调事务内完成在线保证金入账并开通资格。
|
||||
// 外部支付已经由渠道完成,因此这里只增加冻结余额,不再扣减可用余额。
|
||||
func (r *Repository) ConfirmQualityOwnerDepositPaidTx(tx *gorm.DB, userID uint64, amountCent int64, paymentNo string) error {
|
||||
paymentNo = strings.TrimSpace(paymentNo)
|
||||
if tx == nil || userID == 0 || amountCent <= 0 || paymentNo == "" {
|
||||
return ErrInvalidUser
|
||||
}
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var appliedCount int64
|
||||
if err := tx.Model(&model.WalletLedger{}).
|
||||
Where(
|
||||
"user_id = ? AND distribution_site_id = 0 AND biz_type = ? AND biz_no = ? AND direction = ? AND balance_type = ?",
|
||||
userID,
|
||||
"quality_owner_deposit",
|
||||
paymentNo,
|
||||
"in",
|
||||
"frozen",
|
||||
).
|
||||
Count(&appliedCount).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 同一支付单已消费过时保持幂等。即使资格后来被取消,也不能让旧支付回调重新开通。
|
||||
if appliedCount > 0 {
|
||||
return nil
|
||||
}
|
||||
// 同一支付回调可能重复到达;已经是优质号主时保持幂等。
|
||||
if user.QualityOwnerStatus == StatusActive {
|
||||
return nil
|
||||
}
|
||||
if err := wallet.EnsureAccountTx(tx, userID, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := wallet.AppendEntries(tx, wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "in",
|
||||
AmountCent: amountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "quality_owner_deposit",
|
||||
BizNo: paymentNo,
|
||||
Remark: "在线支付优质号主保证金",
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
beforeStatus := user.QualityOwnerStatus
|
||||
user.QualityOwnerStatus = StatusActive
|
||||
user.QualityOwnerDepositCent = amountCent
|
||||
user.QualityOwnerDepositSource = DepositSourcePaid
|
||||
user.QualityOwnerActivatedAt = &now
|
||||
if err := tx.Save(&user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return auditlog.Append(tx, auditlog.Entry{
|
||||
ActorType: "user",
|
||||
ActorID: userID,
|
||||
Action: "quality_owner.activate",
|
||||
BizType: "user",
|
||||
BizID: &userID,
|
||||
Detail: map[string]any{
|
||||
"before_status": beforeStatus,
|
||||
"after_status": user.QualityOwnerStatus,
|
||||
"deposit_amount_cent": amountCent,
|
||||
"deposit_source": DepositSourcePaid,
|
||||
"payment_no": paymentNo,
|
||||
"pay_method": "online",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Repository) Activate(ctx context.Context, userID uint64) (*StatusDTO, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
rules, err := r.Rules(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if user.RealnameStatus != "verified" {
|
||||
return ErrInvalidUser
|
||||
}
|
||||
if user.Status != "active" {
|
||||
return ErrInvalidUser
|
||||
}
|
||||
if user.QualityOwnerStatus == StatusActive {
|
||||
return ErrAlreadyActive
|
||||
}
|
||||
var pendingPaymentCount int64
|
||||
if err := tx.Model(&model.PaymentOrder{}).
|
||||
Where(
|
||||
"user_id = ? AND biz_type = ? AND status IN ? AND updated_at >= ?",
|
||||
userID,
|
||||
"quality_owner_pay",
|
||||
[]string{"created", "paying"},
|
||||
time.Now().Add(-30*time.Minute),
|
||||
).
|
||||
Count(&pendingPaymentCount).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if pendingPaymentCount > 0 {
|
||||
return ErrPaymentPending
|
||||
}
|
||||
if err := wallet.EnsureAccountTx(tx, userID, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
bizNo := fmt.Sprintf("QO%d", userID)
|
||||
err := wallet.AppendEntries(tx,
|
||||
wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "out",
|
||||
AmountCent: rules.DepositAmountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "quality_owner_deposit",
|
||||
BizNo: bizNo,
|
||||
Remark: "缴纳优质号主保证金",
|
||||
},
|
||||
wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "in",
|
||||
AmountCent: rules.DepositAmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "quality_owner_deposit",
|
||||
BizNo: bizNo,
|
||||
Remark: "优质号主保证金冻结",
|
||||
},
|
||||
)
|
||||
if errors.Is(err, wallet.ErrInsufficientBalance) {
|
||||
return ErrDepositInsufficient
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
beforeStatus := user.QualityOwnerStatus
|
||||
user.QualityOwnerStatus = StatusActive
|
||||
user.QualityOwnerDepositCent = rules.DepositAmountCent
|
||||
user.QualityOwnerDepositSource = DepositSourcePaid
|
||||
user.QualityOwnerActivatedAt = &now
|
||||
if err := tx.Save(&user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return auditlog.Append(tx, auditlog.Entry{
|
||||
ActorType: "user",
|
||||
ActorID: userID,
|
||||
Action: "quality_owner.activate",
|
||||
BizType: "user",
|
||||
BizID: &userID,
|
||||
Detail: map[string]any{
|
||||
"before_status": beforeStatus,
|
||||
"after_status": user.QualityOwnerStatus,
|
||||
"deposit_amount_cent": rules.DepositAmountCent,
|
||||
"deposit_source": DepositSourcePaid,
|
||||
},
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.Status(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *Repository) Cancel(ctx context.Context, userID uint64, meta AuditMeta) (*StatusDTO, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if user.QualityOwnerStatus != StatusActive {
|
||||
return ErrInactive
|
||||
}
|
||||
beforeDepositCent := user.QualityOwnerDepositCent
|
||||
beforeSource := user.QualityOwnerDepositSource
|
||||
if err := cancelActiveQualityOwnerTx(tx, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Save(&user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return auditlog.Append(tx, auditlog.Entry{
|
||||
ActorType: "user",
|
||||
ActorID: userID,
|
||||
Action: "quality_owner.cancel",
|
||||
BizType: "user",
|
||||
BizID: &userID,
|
||||
Meta: meta,
|
||||
Detail: map[string]any{
|
||||
"reason": "用户主动取消优质号主",
|
||||
"before_status": StatusActive,
|
||||
"after_status": user.QualityOwnerStatus,
|
||||
"before_deposit_cent": beforeDepositCent,
|
||||
"after_deposit_cent": user.QualityOwnerDepositCent,
|
||||
"before_deposit_source": beforeSource,
|
||||
"after_deposit_source": user.QualityOwnerDepositSource,
|
||||
},
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.Status(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *Repository) AdminSet(ctx context.Context, adminID uint64, userID uint64, req AdminSetRequest, meta AuditMeta) (*StatusDTO, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
reason := strings.TrimSpace(req.Reason)
|
||||
if userID == 0 || adminID == 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
if len([]rune(reason)) < 2 || len([]rune(reason)) > 200 {
|
||||
return nil, ErrInvalidReason
|
||||
}
|
||||
rules, err := r.Rules(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
beforeStatus := user.QualityOwnerStatus
|
||||
beforeDepositCent := user.QualityOwnerDepositCent
|
||||
beforeSource := user.QualityOwnerDepositSource
|
||||
if req.Enabled {
|
||||
if user.Status != "active" {
|
||||
return ErrInvalidUser
|
||||
}
|
||||
if beforeStatus != StatusActive {
|
||||
now := time.Now()
|
||||
user.QualityOwnerStatus = StatusActive
|
||||
user.QualityOwnerDepositCent = rules.DepositAmountCent
|
||||
user.QualityOwnerDepositSource = DepositSourceManual
|
||||
user.QualityOwnerActivatedAt = &now
|
||||
}
|
||||
} else {
|
||||
if beforeStatus != StatusActive {
|
||||
return ErrInactive
|
||||
}
|
||||
if err := cancelActiveQualityOwnerTx(tx, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Save(&user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := auditlog.Append(tx, auditlog.Entry{
|
||||
ActorType: "admin",
|
||||
ActorID: adminID,
|
||||
Action: "admin_user.set_quality_owner",
|
||||
BizType: "user",
|
||||
BizID: &userID,
|
||||
Meta: meta,
|
||||
Detail: map[string]any{
|
||||
"user_id": userID,
|
||||
"reason": reason,
|
||||
"before_status": beforeStatus,
|
||||
"after_status": user.QualityOwnerStatus,
|
||||
"before_deposit_cent": beforeDepositCent,
|
||||
"after_deposit_cent": user.QualityOwnerDepositCent,
|
||||
"before_deposit_source": beforeSource,
|
||||
"after_deposit_source": user.QualityOwnerDepositSource,
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.Status(ctx, userID)
|
||||
}
|
||||
|
||||
func activePremiumListingCount(db *gorm.DB, userID uint64) (int64, error) {
|
||||
var count int64
|
||||
err := db.Model(&model.RentalListing{}).
|
||||
Where(
|
||||
"owner_id = ? AND is_premium = ? AND (status IN ? OR in_transaction = ?)",
|
||||
userID,
|
||||
true,
|
||||
[]string{"published", "rented"},
|
||||
true,
|
||||
).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func cancelActiveQualityOwnerTx(tx *gorm.DB, user *model.User) error {
|
||||
if tx == nil || user == nil || user.ID == 0 {
|
||||
return ErrInvalidUser
|
||||
}
|
||||
activeListings, err := activePremiumListingCount(tx, user.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if activeListings > 0 {
|
||||
return ActivePremiumListingsError{Count: activeListings}
|
||||
}
|
||||
if user.QualityOwnerDepositSource == DepositSourcePaid && user.QualityOwnerDepositCent > 0 {
|
||||
if err := wallet.AppendEntries(tx,
|
||||
wallet.Entry{
|
||||
UserID: user.ID,
|
||||
Direction: "out",
|
||||
AmountCent: user.QualityOwnerDepositCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "quality_owner_deposit_refund",
|
||||
BizNo: fmt.Sprintf("QOR%d", user.ID),
|
||||
Remark: "取消优质号主,解冻保证金",
|
||||
},
|
||||
wallet.Entry{
|
||||
UserID: user.ID,
|
||||
Direction: "in",
|
||||
AmountCent: user.QualityOwnerDepositCent,
|
||||
BalanceType: "available",
|
||||
BizType: "quality_owner_deposit_refund",
|
||||
BizNo: fmt.Sprintf("QOR%d", user.ID),
|
||||
Remark: "优质号主保证金退回",
|
||||
},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
user.QualityOwnerStatus = StatusNone
|
||||
user.QualityOwnerDepositCent = 0
|
||||
user.QualityOwnerDepositSource = ""
|
||||
user.QualityOwnerActivatedAt = nil
|
||||
return tx.Model(&model.RentalListing{}).
|
||||
Where("owner_id = ? AND is_premium = ? AND status IN ?", user.ID, true, []string{"draft", "published"}).
|
||||
Updates(map[string]any{
|
||||
"status": "offline",
|
||||
"review_status": "none",
|
||||
"review_reason": "优质号主资格已取消",
|
||||
}).Error
|
||||
}
|
||||
|
||||
func toStatusDTO(user model.User, account model.WalletAccount, rules Rules, activePremiumListings int64) *StatusDTO {
|
||||
active := user.QualityOwnerStatus == StatusActive
|
||||
return &StatusDTO{
|
||||
UserID: user.ID,
|
||||
Active: active,
|
||||
Status: user.QualityOwnerStatus,
|
||||
CanCancel: active && activePremiumListings == 0,
|
||||
ActivePremiumListings: activePremiumListings,
|
||||
DepositAmountCent: user.QualityOwnerDepositCent,
|
||||
DepositSource: user.QualityOwnerDepositSource,
|
||||
ActivatedAt: user.QualityOwnerActivatedAt,
|
||||
AvailableBalanceCent: account.AvailableBalanceCent,
|
||||
RequiredDepositCent: rules.DepositAmountCent,
|
||||
InsuranceFeeBps: rules.InsuranceFeeBps,
|
||||
PremiumInsuranceFeeBps: rules.PremiumInsuranceFeeBps,
|
||||
AgreementTitle: rules.AgreementTitle,
|
||||
AgreementContent: rules.AgreementContent,
|
||||
AgreementReadSeconds: rules.AgreementReadSeconds,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package qualityowner
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"hfb_jysh/backend/internal/database"
|
||||
"hfb_jysh/backend/internal/model"
|
||||
)
|
||||
|
||||
func TestActivateAndAdminDisableRefundDeposit(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
user := model.User{
|
||||
Phone: "13900009301",
|
||||
RealnameStatus: "verified",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
t.Fatalf("创建用户失败: %v", err)
|
||||
}
|
||||
account := model.WalletAccount{
|
||||
UserID: user.ID,
|
||||
AvailableBalanceCent: 30_000,
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatalf("创建钱包失败: %v", err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
status, err := repo.Activate(t.Context(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("开通优质号主失败: %v", err)
|
||||
}
|
||||
if !status.Active || status.DepositAmountCent != 20_000 || status.DepositSource != DepositSourcePaid {
|
||||
t.Fatalf("开通后的状态不正确: %+v", status)
|
||||
}
|
||||
if status.AvailableBalanceCent != 10_000 {
|
||||
t.Fatalf("可用余额 = %d, want 10000", status.AvailableBalanceCent)
|
||||
}
|
||||
if err := db.First(&account, account.ID).Error; err != nil {
|
||||
t.Fatalf("读取钱包失败: %v", err)
|
||||
}
|
||||
if account.FrozenBalanceCent != 20_000 {
|
||||
t.Fatalf("冻结余额 = %d, want 20000", account.FrozenBalanceCent)
|
||||
}
|
||||
|
||||
status, err = repo.AdminSet(t.Context(), 99, user.ID, AdminSetRequest{
|
||||
Enabled: false,
|
||||
Reason: "用户申请取消优质号主",
|
||||
}, AuditMeta{RequestID: "quality-owner-disable"})
|
||||
if err != nil {
|
||||
t.Fatalf("取消优质号主失败: %v", err)
|
||||
}
|
||||
if status.Active || status.DepositAmountCent != 0 {
|
||||
t.Fatalf("取消后的状态不正确: %+v", status)
|
||||
}
|
||||
if status.AvailableBalanceCent != 30_000 {
|
||||
t.Fatalf("退款后可用余额 = %d, want 30000", status.AvailableBalanceCent)
|
||||
}
|
||||
if err := db.First(&account, account.ID).Error; err != nil {
|
||||
t.Fatalf("读取退款后钱包失败: %v", err)
|
||||
}
|
||||
if account.FrozenBalanceCent != 0 {
|
||||
t.Fatalf("退款后冻结余额 = %d, want 0", account.FrozenBalanceCent)
|
||||
}
|
||||
|
||||
var ledgerCount int64
|
||||
if err := db.Model(&model.WalletLedger{}).Where("user_id = ?", user.ID).Count(&ledgerCount).Error; err != nil {
|
||||
t.Fatalf("统计钱包流水失败: %v", err)
|
||||
}
|
||||
if ledgerCount != 4 {
|
||||
t.Fatalf("钱包流水条数 = %d, want 4", ledgerCount)
|
||||
}
|
||||
var auditCount int64
|
||||
if err := db.Model(&model.AuditLog{}).
|
||||
Where("action = ? AND biz_id = ?", "admin_user.set_quality_owner", user.ID).
|
||||
Count(&auditCount).Error; err != nil {
|
||||
t.Fatalf("统计审计日志失败: %v", err)
|
||||
}
|
||||
if auditCount != 1 {
|
||||
t.Fatalf("后台设置审计日志条数 = %d, want 1", auditCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminCanGrantQualityOwnerWithoutWalletDeposit(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
user := model.User{
|
||||
Phone: "13900009302",
|
||||
RealnameStatus: "verified",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
t.Fatalf("创建用户失败: %v", err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
status, err := repo.AdminSet(t.Context(), 99, user.ID, AdminSetRequest{
|
||||
Enabled: true,
|
||||
Reason: "客服核验后手动开通",
|
||||
}, AuditMeta{RequestID: "quality-owner-enable"})
|
||||
if err != nil {
|
||||
t.Fatalf("后台开通优质号主失败: %v", err)
|
||||
}
|
||||
if !status.Active || status.DepositSource != DepositSourceManual || status.DepositAmountCent != 20_000 {
|
||||
t.Fatalf("后台开通状态不正确: %+v", status)
|
||||
}
|
||||
var ledgerCount int64
|
||||
if err := db.Model(&model.WalletLedger{}).Where("user_id = ?", user.ID).Count(&ledgerCount).Error; err != nil {
|
||||
t.Fatalf("统计钱包流水失败: %v", err)
|
||||
}
|
||||
if ledgerCount != 0 {
|
||||
t.Fatalf("后台手动开通不应产生钱包流水,实际 %d 条", ledgerCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCancelQualityOwnerRequiresNoActivePremiumListings(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
user := model.User{
|
||||
Phone: "13900009303",
|
||||
RealnameStatus: "verified",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
t.Fatalf("创建用户失败: %v", err)
|
||||
}
|
||||
account := model.WalletAccount{
|
||||
UserID: user.ID,
|
||||
AvailableBalanceCent: 30_000,
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatalf("创建钱包失败: %v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
if _, err := repo.Activate(t.Context(), user.ID); err != nil {
|
||||
t.Fatalf("开通优质号主失败: %v", err)
|
||||
}
|
||||
premiumListing := model.RentalListing{
|
||||
ListingNo: "QO-CANCEL-ACTIVE",
|
||||
AccountID: 1,
|
||||
OwnerID: user.ID,
|
||||
Status: "published",
|
||||
ReviewStatus: "approved",
|
||||
IsPremium: true,
|
||||
}
|
||||
if err := db.Create(&premiumListing).Error; err != nil {
|
||||
t.Fatalf("创建极品专区商品失败: %v", err)
|
||||
}
|
||||
|
||||
status, err := repo.Status(t.Context(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("读取优质号主状态失败: %v", err)
|
||||
}
|
||||
if status.ActivePremiumListings != 1 || status.CanCancel {
|
||||
t.Fatalf("进行中极品商品统计不正确: %+v", status)
|
||||
}
|
||||
if _, err := repo.Cancel(t.Context(), user.ID, AuditMeta{}); !errors.Is(err, ErrActivePremiumListings) {
|
||||
t.Fatalf("用户取消 error = %v, want ErrActivePremiumListings", err)
|
||||
}
|
||||
if _, err := repo.AdminSet(t.Context(), 99, user.ID, AdminSetRequest{
|
||||
Enabled: false,
|
||||
Reason: "后台尝试取消",
|
||||
}, AuditMeta{}); !errors.Is(err, ErrActivePremiumListings) {
|
||||
t.Fatalf("后台取消 error = %v, want ErrActivePremiumListings", err)
|
||||
}
|
||||
|
||||
if err := db.Model(&premiumListing).Updates(map[string]any{
|
||||
"status": "offline",
|
||||
"review_status": "none",
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("下架极品商品失败: %v", err)
|
||||
}
|
||||
draftListing := model.RentalListing{
|
||||
ListingNo: "QO-CANCEL-DRAFT",
|
||||
AccountID: 2,
|
||||
OwnerID: user.ID,
|
||||
Status: "draft",
|
||||
ReviewStatus: "none",
|
||||
IsPremium: true,
|
||||
}
|
||||
if err := db.Create(&draftListing).Error; err != nil {
|
||||
t.Fatalf("创建极品专区草稿失败: %v", err)
|
||||
}
|
||||
|
||||
status, err = repo.Cancel(t.Context(), user.ID, AuditMeta{RequestID: "quality-owner-self-cancel"})
|
||||
if err != nil {
|
||||
t.Fatalf("自动取消优质号主失败: %v", err)
|
||||
}
|
||||
if status.Active || status.DepositAmountCent != 0 || status.AvailableBalanceCent != 30_000 {
|
||||
t.Fatalf("取消后的状态不正确: %+v", status)
|
||||
}
|
||||
var gotDraft model.RentalListing
|
||||
if err := db.First(&gotDraft, draftListing.ID).Error; err != nil {
|
||||
t.Fatalf("读取草稿失败: %v", err)
|
||||
}
|
||||
if gotDraft.Status != "offline" {
|
||||
t.Fatalf("取消后草稿状态 = %s, want offline", gotDraft.Status)
|
||||
}
|
||||
var auditCount int64
|
||||
if err := db.Model(&model.AuditLog{}).
|
||||
Where("action = ? AND biz_id = ?", "quality_owner.cancel", user.ID).
|
||||
Count(&auditCount).Error; err != nil {
|
||||
t.Fatalf("统计自动取消审计日志失败: %v", err)
|
||||
}
|
||||
if auditCount != 1 {
|
||||
t.Fatalf("自动取消审计日志条数 = %d, want 1", auditCount)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package qualityowner
|
||||
|
||||
import "context"
|
||||
|
||||
type Service struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) Status(ctx context.Context, userID uint64) (*StatusDTO, error) {
|
||||
if s == nil || s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Status(ctx, userID)
|
||||
}
|
||||
|
||||
func (s *Service) Activate(ctx context.Context, userID uint64) (*StatusDTO, error) {
|
||||
if s == nil || s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Activate(ctx, userID)
|
||||
}
|
||||
|
||||
func (s *Service) Cancel(ctx context.Context, userID uint64, meta AuditMeta) (*StatusDTO, error) {
|
||||
if s == nil || s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Cancel(ctx, userID, meta)
|
||||
}
|
||||
|
||||
func (s *Service) AdminSet(ctx context.Context, adminID uint64, userID uint64, req AdminSetRequest, meta AuditMeta) (*StatusDTO, error) {
|
||||
if s == nil || s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.AdminSet(ctx, adminID, userID, req, meta)
|
||||
}
|
||||
Reference in New Issue
Block a user