Compare commits
6 Commits
4f404a11b0
...
f0b54b8b52
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f0b54b8b52 | ||
|
|
ce2977ce22 | ||
|
|
b3f66a01a8 | ||
|
|
afc7e5285e | ||
|
|
dc816b44d0 | ||
|
|
f274f8bb58 |
@@ -122,6 +122,11 @@ services:
|
||||
- JUNHONG_REDIS_DB=6
|
||||
# worker 并发轮询与按账户并行分发共用连接池,默认 10 不够
|
||||
- JUNHONG_REDIS_POOL_SIZE=100
|
||||
# 任务并发:默认 10 时流量扣减事件投递跟不上(白天积压 1 万+、延迟 2.5 小时),调到 50
|
||||
- JUNHONG_QUEUE_CONCURRENCY=50
|
||||
# 数据库连接池随任务并发放大(默认 25/10);测试库 max_connections=200,API 另占 25
|
||||
- JUNHONG_DATABASE_MAX_OPEN_CONNS=60
|
||||
- JUNHONG_DATABASE_MAX_IDLE_CONNS=20
|
||||
# JWT 配置(必填)
|
||||
- JUNHONG_JWT_SECRET_KEY=dev-secret-key-for-testing-only-32chars!
|
||||
# 客户端配置(可选,默认 true;false=不强制绑定手机号)
|
||||
|
||||
@@ -801,6 +801,15 @@ func packageStatusName(status int) string {
|
||||
}
|
||||
}
|
||||
|
||||
// clientDeviceLANIP 网关未返回局域网网关地址(空或仅空白)时使用设备默认网关地址,保证 C 端可进入设备后台。
|
||||
func clientDeviceLANIP(lanIP *string) *string {
|
||||
if lanIP != nil && strings.TrimSpace(*lanIP) != "" {
|
||||
return lanIP
|
||||
}
|
||||
defaultIP := constants.DefaultDeviceLANIP
|
||||
return &defaultIP
|
||||
}
|
||||
|
||||
// mapDeviceGatewayInfoToClientInfo 将 B 端 DeviceGatewayInfo 映射为 C 端 DeviceRealtimeInfo
|
||||
// B 端和 C 端结构独立,通过映射函数转换(per D-08)
|
||||
func mapDeviceGatewayInfoToClientInfo(g *dto.DeviceGatewayInfo) *dto.DeviceRealtimeInfo {
|
||||
@@ -844,7 +853,7 @@ func mapDeviceGatewayInfoToClientInfo(g *dto.DeviceGatewayInfo) *dto.DeviceRealt
|
||||
info.WifiPassword = g.WifiPassword
|
||||
info.IPAddress = g.IPAddress
|
||||
info.WANIP = g.WANIP
|
||||
info.LANIP = g.LANIP
|
||||
info.LANIP = clientDeviceLANIP(g.LANIP)
|
||||
info.MACAddress = g.MACAddress
|
||||
info.DailyUsage = g.DailyUsage
|
||||
info.DLStats = g.DLStats
|
||||
|
||||
39
internal/handler/app/client_asset_lan_ip_test.go
Normal file
39
internal/handler/app/client_asset_lan_ip_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
func TestMapDeviceGatewayInfoToClientInfo_LANIPDefault(t *testing.T) {
|
||||
strPtr := func(s string) *string { return &s }
|
||||
cases := []struct {
|
||||
name string
|
||||
lanIP *string
|
||||
want string
|
||||
}{
|
||||
{name: "网关未返回", lanIP: nil, want: constants.DefaultDeviceLANIP},
|
||||
{name: "空字符串", lanIP: strPtr(""), want: constants.DefaultDeviceLANIP},
|
||||
{name: "仅空白", lanIP: strPtr(" "), want: constants.DefaultDeviceLANIP},
|
||||
{name: "网关有值", lanIP: strPtr("192.168.0.1"), want: "192.168.0.1"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
info := mapDeviceGatewayInfoToClientInfo(&dto.DeviceGatewayInfo{LANIP: tc.lanIP})
|
||||
if info.LANIP == nil || *info.LANIP != tc.want {
|
||||
t.Fatalf("期望 %q,实际 %v", tc.want, info.LANIP)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 网关调用失败时同样返回默认地址,前端仍可进入设备后台。
|
||||
func TestMapDeviceGatewayInfoToClientInfo_GatewayFailedStillDefaultLANIP(t *testing.T) {
|
||||
msg := "网关调用失败"
|
||||
info := mapDeviceGatewayInfoToClientInfo(&dto.DeviceGatewayInfo{GatewayMsg: &msg})
|
||||
if info.LANIP == nil || *info.LANIP != constants.DefaultDeviceLANIP {
|
||||
t.Fatalf("期望默认地址,实际 %v", info.LANIP)
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ type CreateExchangeRequest struct {
|
||||
FlowType string `json:"flow_type" validate:"omitempty,oneof=shipping direct" enum:"shipping,direct" description:"换货流程类型 (shipping:物流换货, direct:直接换货)"`
|
||||
NewIdentifier string `json:"new_identifier" validate:"omitempty,min=1,max=100" minLength:"1" maxLength:"100" description:"新资产输入标识,direct 流程必填;卡支持 ICCID、接入号、虚拟号,设备支持虚拟号、IMEI、SN"`
|
||||
MigrateData *bool `json:"migrate_data" description:"是否执行全量迁移,direct 流程未传按 false 处理"`
|
||||
ExchangeReason string `json:"exchange_reason" validate:"required,min=1,max=100" required:"true" minLength:"1" maxLength:"100" description:"换货原因"`
|
||||
ExchangeReason string `json:"exchange_reason" validate:"required,min=1,max=500" required:"true" minLength:"1" maxLength:"500" description:"换货原因"`
|
||||
Remark *string `json:"remark" validate:"omitempty,max=500" maxLength:"500" description:"备注"`
|
||||
}
|
||||
|
||||
|
||||
34
internal/model/dto/exchange_dto_test.go
Normal file
34
internal/model/dto/exchange_dto_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
// 换货原因按字符计数,上限 500:从微信复制的多行说明(含换行)不超过 500 字都应通过校验。
|
||||
func TestCreateExchangeRequest_ExchangeReasonLength(t *testing.T) {
|
||||
validate := validator.New()
|
||||
multiLine := "862639070766110(门店:862639070766110机头,862639070743218后盖)\n\n" +
|
||||
"862639070743218(厂家:862639070766110后盖,862639070743218机头)\n\n这两台后盖机头窜了。"
|
||||
cases := []struct {
|
||||
name string
|
||||
reason string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "多行说明124字", reason: multiLine, wantErr: false},
|
||||
{name: "500个中文字", reason: strings.Repeat("换", 500), wantErr: false},
|
||||
{name: "501个中文字", reason: strings.Repeat("换", 501), wantErr: true},
|
||||
{name: "空", reason: "", wantErr: true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := CreateExchangeRequest{OldAssetType: "device", OldIdentifier: "862639070743218", ExchangeReason: tc.reason}
|
||||
err := validate.Struct(req)
|
||||
if (err != nil) != tc.wantErr {
|
||||
t.Fatalf("wantErr=%v, err=%v", tc.wantErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ type ExchangeOrder struct {
|
||||
CompletedAt *time.Time `gorm:"column:completed_at;comment:换货完成时间,物流换货和直接换货完成后写入" json:"completed_at,omitempty"`
|
||||
|
||||
// 业务信息
|
||||
ExchangeReason string `gorm:"column:exchange_reason;type:varchar(100);not null;comment:换货原因" json:"exchange_reason"`
|
||||
ExchangeReason string `gorm:"column:exchange_reason;type:varchar(500);not null;comment:换货原因" json:"exchange_reason"`
|
||||
Remark *string `gorm:"column:remark;type:text;comment:备注" json:"remark,omitempty"`
|
||||
Status int `gorm:"column:status;type:int;not null;default:1;index:idx_exchange_order_status;comment:换货状态 1-待填写信息 2-待发货 3-已发货待确认 4-已完成 5-已取消" json:"status"`
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ type stopResumeDecision struct {
|
||||
DeviceID uint // 设备维度动作的设备;0 表示单卡
|
||||
}
|
||||
|
||||
// decideCard 统一判断函数:暂停轮询(按场景) → 停机条件 → 复机条件。
|
||||
// 暂停轮询的卡只在套餐失效类场景(套餐到期、退款)下放行无套餐停机,其余自动停复机一律不做;
|
||||
// decideCard 统一判断函数:暂停轮询 → 停机条件 → 复机条件。
|
||||
// 卡或绑定设备暂停轮询(已交由其他卡管)时任何场景都不停机也不复机;
|
||||
// 套餐失效类场景不看库里的开停状态,资产无可用主套餐即停机。
|
||||
// 设备维度原因(无套餐、流量耗尽)与绑定设备的复机返回 DeviceID,由调用方按设备处理。通道阈值锁由复机执行方判断。
|
||||
func (s *StopResumeService) decideCard(ctx context.Context, card *model.IotCard, scenes []string) (stopResumeDecision, error) {
|
||||
@@ -37,11 +37,10 @@ func (s *StopResumeService) decideCard(ctx context.Context, card *model.IotCard,
|
||||
if err != nil {
|
||||
return stopResumeDecision{}, err
|
||||
}
|
||||
allowSuspendedStop := constants.StopResumeSceneAllowsSuspendedStop(scenes)
|
||||
if suspended && !allowSuspendedStop {
|
||||
if suspended {
|
||||
return stopResumeDecision{}, nil
|
||||
}
|
||||
if allowSuspendedStop {
|
||||
if constants.StopResumeSceneIgnoresNetworkStatus(scenes) {
|
||||
// 套餐失效类场景(套餐到期、退款):不看库里的开停状态,资产无可用主套餐即停机。
|
||||
// 停复机成功后上游状态有延迟,库里的开停状态可能是旧值;上游已停时调用按已是目标状态结束。
|
||||
reasons, err := s.checkStopReasons(ctx, card)
|
||||
@@ -55,9 +54,6 @@ func (s *StopResumeService) decideCard(ctx context.Context, card *model.IotCard,
|
||||
}
|
||||
return decision, nil
|
||||
}
|
||||
if suspended {
|
||||
return stopResumeDecision{}, nil
|
||||
}
|
||||
}
|
||||
switch card.NetworkStatus {
|
||||
case constants.NetworkStatusOnline:
|
||||
@@ -66,22 +62,12 @@ func (s *StopResumeService) decideCard(ctx context.Context, card *model.IotCard,
|
||||
return stopResumeDecision{}, err
|
||||
}
|
||||
primary := reasons[0]
|
||||
if suspended {
|
||||
// 暂停轮询:只有套餐失效(无可用主套餐)才停机。
|
||||
if !containsStopReason(reasons, constants.StopReasonNoPackage) {
|
||||
return stopResumeDecision{}, nil
|
||||
}
|
||||
primary = constants.StopReasonNoPackage
|
||||
}
|
||||
decision := stopResumeDecision{Action: constants.StopResumeActionStop, Reason: primary, Reasons: reasons}
|
||||
if !card.IsStandalone && isDeviceScopeReason(primary) {
|
||||
decision.DeviceID = s.boundDeviceID(ctx, card)
|
||||
}
|
||||
return decision, nil
|
||||
case constants.NetworkStatusOffline:
|
||||
if suspended {
|
||||
return stopResumeDecision{}, nil
|
||||
}
|
||||
// 不看停机原因,是否复机只由本系统规则决定。
|
||||
resume, err := s.shouldResume(ctx, card)
|
||||
if err != nil || !resume {
|
||||
|
||||
48
internal/service/iot_card/stop_resume_polling_guard_test.go
Normal file
48
internal/service/iot_card/stop_resume_polling_guard_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package iot_card
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// 关闭轮询的卡已交由其他卡管:任何场景(含套餐到期、退款)都不停机也不复机,
|
||||
// 不得进入停机原因判断或复机流程(服务未注入任何 store/网关,进入后续流程会直接 panic)。
|
||||
func TestDecideCard_PollingDisabledNeverActs(t *testing.T) {
|
||||
svc := &StopResumeService{logger: zap.NewNop()}
|
||||
cards := map[string]*model.IotCard{
|
||||
"在线卡": {NetworkStatus: constants.NetworkStatusOnline, EnablePolling: false},
|
||||
"设备下在线卡": {NetworkStatus: constants.NetworkStatusOnline, EnablePolling: false, IsStandalone: false},
|
||||
"无套餐停机的卡": {NetworkStatus: constants.NetworkStatusOffline, StopReason: constants.StopReasonNoPackage, EnablePolling: false},
|
||||
}
|
||||
sceneSets := map[string][]string{
|
||||
"无场景": nil,
|
||||
"套餐到期": {constants.StopResumeScenePackageExpired},
|
||||
"退款": {constants.StopResumeSceneRefund},
|
||||
"流量扣减": {constants.StopResumeSceneUsage},
|
||||
}
|
||||
for cardName, card := range cards {
|
||||
for sceneName, scenes := range sceneSets {
|
||||
t.Run(cardName+"/"+sceneName, func(t *testing.T) {
|
||||
decision, err := svc.decideCard(context.Background(), card, scenes)
|
||||
if err != nil {
|
||||
t.Fatalf("期望 nil,实际 %v", err)
|
||||
}
|
||||
if decision.Action != "" {
|
||||
t.Fatalf("关闭轮询的卡不应停复机,实际动作 %q", decision.Action)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateAndAct_NilCard(t *testing.T) {
|
||||
svc := &StopResumeService{logger: zap.NewNop()}
|
||||
if err := svc.EvaluateAndAct(context.Background(), nil); err != nil {
|
||||
t.Fatalf("期望 nil,实际 %v", err)
|
||||
}
|
||||
}
|
||||
@@ -134,6 +134,9 @@ func (s *StopResumeService) SetPollingCallback(callback PollingCallback) {
|
||||
// 停复机出口开关开启时只请求评估(生成指令或请求设备判断),不当场调用网关;关闭时当场执行。
|
||||
// 设备/单卡维度通过 card.IsStandalone 推导(false 则为设备维度,停/复机覆盖设备下所有卡)。触发场景见 WithStopResumeScene。
|
||||
func (s *StopResumeService) EvaluateAndAct(ctx context.Context, card *model.IotCard) error {
|
||||
if card == nil {
|
||||
return nil
|
||||
}
|
||||
scenes := stopResumeScenes(ctx)
|
||||
decision, err := s.decideCard(ctx, card, scenes)
|
||||
if err != nil {
|
||||
@@ -376,7 +379,7 @@ func (s *StopResumeService) stopDeviceCards(ctx context.Context, deviceID uint,
|
||||
// 查询设备下在线卡;套餐失效类场景(套餐到期、退款)不看库里的开停状态(可能因上游延迟是旧值),设备下全部卡都停。
|
||||
var cards []*model.IotCard
|
||||
var err error
|
||||
if stopReason == constants.StopReasonNoPackage && constants.StopResumeSceneAllowsSuspendedStop(stopResumeScenes(ctx)) {
|
||||
if stopReason == constants.StopReasonNoPackage && constants.StopResumeSceneIgnoresNetworkStatus(stopResumeScenes(ctx)) {
|
||||
cards, err = s.deviceCards(ctx, deviceID)
|
||||
} else {
|
||||
cards, err = s.iotCardStore.ListByDeviceIDAndNetworkStatus(ctx, deviceID, constants.NetworkStatusOnline)
|
||||
@@ -389,11 +392,11 @@ func (s *StopResumeService) stopDeviceCards(ctx context.Context, deviceID uint,
|
||||
|
||||
var cardErrors []error
|
||||
for _, card := range cards {
|
||||
// 暂停轮询的卡不随设备自动停机;套餐失效类场景(套餐到期、退款)照常停机。
|
||||
// 暂停轮询的卡已交由其他卡管,任何场景都不随设备自动停机。
|
||||
if suspended, suspendErr := s.autoControlSuspended(ctx, card); suspendErr != nil {
|
||||
cardErrors = append(cardErrors, suspendErr)
|
||||
continue
|
||||
} else if suspended && !(stopReason == constants.StopReasonNoPackage && constants.StopResumeSceneAllowsSuspendedStop(stopResumeScenes(ctx))) {
|
||||
} else if suspended {
|
||||
continue
|
||||
}
|
||||
if _, stopErr := s.stopCardWithRetry(ctx, card, stopReason); stopErr != nil {
|
||||
|
||||
@@ -72,7 +72,7 @@ func (s *Service) SendCode(ctx context.Context, phone string) error {
|
||||
|
||||
// 构造短信内容
|
||||
cfg := config.Get()
|
||||
content := fmt.Sprintf("您的验证码是%s,%d分钟内有效", code, int(constants.VerificationCodeExpiration.Minutes()))
|
||||
content := fmt.Sprintf("您的验证码是%s,请在%d分钟内输入验证。如非本人操作,请忽略本短信。", code, int(constants.VerificationCodeExpiration.Minutes()))
|
||||
|
||||
// 发送短信
|
||||
_, err = s.smsClient.SendMessage(ctx, content, []string{phone})
|
||||
|
||||
3
migrations/000270_widen_exchange_reason.down.sql
Normal file
3
migrations/000270_widen_exchange_reason.down.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
-- 回滚 000270:不缩回 varchar(100)。
|
||||
-- 放宽后可能已写入超过 100 字的换货原因,缩短长度会失败或需要截断历史数据;需要时由维护者评估后手工处理。
|
||||
SELECT 1;
|
||||
4
migrations/000270_widen_exchange_reason.up.sql
Normal file
4
migrations/000270_widen_exchange_reason.up.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
-- 换货原因长度上限从 100 放宽到 500(与创建换货单请求校验一致)。
|
||||
-- 生产库与测试库已于 2026-11-03 手动执行同一语句;重复执行结果不变。
|
||||
-- varchar 加长只改元数据,不重写表。
|
||||
ALTER TABLE tb_exchange_order ALTER COLUMN exchange_reason TYPE varchar(500);
|
||||
4
pkg/constants/device_network.go
Normal file
4
pkg/constants/device_network.go
Normal file
@@ -0,0 +1,4 @@
|
||||
package constants
|
||||
|
||||
// DefaultDeviceLANIP 网关未返回设备局域网网关地址时 C 端使用的默认值(设备出厂默认网关)。
|
||||
const DefaultDeviceLANIP = "192.168.1.1"
|
||||
@@ -22,8 +22,9 @@ const (
|
||||
StopResumeSceneUsage = "usage" // 流量扣减后
|
||||
)
|
||||
|
||||
// StopResumeSceneAllowsSuspendedStop 判断场景集合中是否含套餐失效类场景:暂停轮询的卡在这些场景下照常停机。
|
||||
func StopResumeSceneAllowsSuspendedStop(scenes []string) bool {
|
||||
// StopResumeSceneIgnoresNetworkStatus 判断场景集合中是否含套餐失效类场景(套餐到期、退款):
|
||||
// 这些场景下停机判断不看库里的开停状态(上游状态有延迟可能是旧值)。暂停轮询的卡在任何场景下都不停机。
|
||||
func StopResumeSceneIgnoresNetworkStatus(scenes []string) bool {
|
||||
for _, scene := range scenes {
|
||||
if scene == StopResumeScenePackageExpired || scene == StopResumeSceneRefund {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user