feat(smoke): support reason tags on smoke logs
This commit is contained in:
+7
-1
@@ -13,6 +13,7 @@
|
|||||||
{
|
{
|
||||||
"smoke_time": "2025-12-31",
|
"smoke_time": "2025-12-31",
|
||||||
"smoke_at": "2025-12-31 08:30:00",
|
"smoke_at": "2025-12-31 08:30:00",
|
||||||
|
"reason_tags": ["stress", "social"],
|
||||||
"remark": "压力大",
|
"remark": "压力大",
|
||||||
"level": 2,
|
"level": 2,
|
||||||
"num": 3
|
"num": 3
|
||||||
@@ -22,6 +23,7 @@
|
|||||||
说明:
|
说明:
|
||||||
- `smoke_time` 可选;不传则默认“当天”。
|
- `smoke_time` 可选;不传则默认“当天”。
|
||||||
- `smoke_at` 可选;真实抽烟时间(格式 `YYYY-MM-DD HH:MM:SS`)。用于“按时间节点分析/AI 建议”;不传则可用 `createtime` 近似。
|
- `smoke_at` 可选;真实抽烟时间(格式 `YYYY-MM-DD HH:MM:SS`)。用于“按时间节点分析/AI 建议”;不传则可用 `createtime` 近似。
|
||||||
|
- `reason_tags` 可选;结构化原因标签,传 JSON 数组,例如 `["stress","after_meal"]`。
|
||||||
- `level/num` 可选;不传时后端会按 `1` 处理。
|
- `level/num` 可选;不传时后端会按 `1` 处理。
|
||||||
- `POST /api/v1/smoke/logs` 仅用于“抽烟记录”,`num` 必须 `>0`。
|
- `POST /api/v1/smoke/logs` 仅用于“抽烟记录”,`num` 必须 `>0`。
|
||||||
- “想抽但忍住了”请使用 `POST /api/v1/smoke/logs/resisted`;系统以 `level=0 且 num=0` 作为“忍住”的判断条件。
|
- “想抽但忍住了”请使用 `POST /api/v1/smoke/logs/resisted`;系统以 `level=0 且 num=0` 作为“忍住”的判断条件。
|
||||||
@@ -32,7 +34,7 @@ curl 示例:
|
|||||||
curl -X POST 'http://127.0.0.1:8080/api/v1/smoke/logs' \
|
curl -X POST 'http://127.0.0.1:8080/api/v1/smoke/logs' \
|
||||||
-H 'Content-Type: application/json' \
|
-H 'Content-Type: application/json' \
|
||||||
-H 'Authorization: Bearer wx-session-key' \
|
-H 'Authorization: Bearer wx-session-key' \
|
||||||
-d '{"smoke_time":"2025-12-31","smoke_at":"2025-12-31 08:30:00","remark":"压力大","level":2,"num":3}'
|
-d '{"smoke_time":"2025-12-31","smoke_at":"2025-12-31 08:30:00","reason_tags":["stress","social"],"remark":"压力大","level":2,"num":3}'
|
||||||
```
|
```
|
||||||
|
|
||||||
成功响应示例(字段以实际为准):
|
成功响应示例(字段以实际为准):
|
||||||
@@ -46,6 +48,7 @@ curl -X POST 'http://127.0.0.1:8080/api/v1/smoke/logs' \
|
|||||||
"smoke_time": "2025-12-31T00:00:00+08:00",
|
"smoke_time": "2025-12-31T00:00:00+08:00",
|
||||||
"smoke_at": "2025-12-31T08:30:00+08:00",
|
"smoke_at": "2025-12-31T08:30:00+08:00",
|
||||||
"remark": "压力大",
|
"remark": "压力大",
|
||||||
|
"reason_tags": ["stress", "social"],
|
||||||
"createtime": 1735600000,
|
"createtime": 1735600000,
|
||||||
"updatetime": 1735600000,
|
"updatetime": 1735600000,
|
||||||
"deletetime": null,
|
"deletetime": null,
|
||||||
@@ -149,6 +152,7 @@ curl -X GET 'http://127.0.0.1:8080/api/v1/smoke/logs/5202' \
|
|||||||
"smoke_time": "2026-01-05T00:00:00+08:00",
|
"smoke_time": "2026-01-05T00:00:00+08:00",
|
||||||
"smoke_at": "2026-01-05T09:12:00+08:00",
|
"smoke_at": "2026-01-05T09:12:00+08:00",
|
||||||
"remark": "压力大",
|
"remark": "压力大",
|
||||||
|
"reason_tags": ["stress"],
|
||||||
"level": 3,
|
"level": 3,
|
||||||
"num": 2,
|
"num": 2,
|
||||||
"createtime": 1736049120
|
"createtime": 1736049120
|
||||||
@@ -168,6 +172,7 @@ curl -X GET 'http://127.0.0.1:8080/api/v1/smoke/logs/5202' \
|
|||||||
{
|
{
|
||||||
"smoke_time": "2026-01-01",
|
"smoke_time": "2026-01-01",
|
||||||
"smoke_at": "2026-01-01 21:10:00",
|
"smoke_at": "2026-01-01 21:10:00",
|
||||||
|
"reason_tags": ["social"],
|
||||||
"remark": "聚会",
|
"remark": "聚会",
|
||||||
"level": 3,
|
"level": 3,
|
||||||
"num": 1
|
"num": 1
|
||||||
@@ -464,6 +469,7 @@ curl -X GET 'http://127.0.0.1:8080/api/v1/smoke/logs/5202' \
|
|||||||
{
|
{
|
||||||
"smoke_time": "2026-01-05",
|
"smoke_time": "2026-01-05",
|
||||||
"smoke_at": "2026-01-05 10:20:00",
|
"smoke_at": "2026-01-05 10:20:00",
|
||||||
|
"reason_tags": ["deep_breath", "water"],
|
||||||
"remark": "压力大,想抽但忍住了",
|
"remark": "压力大,想抽但忍住了",
|
||||||
"level": 0,
|
"level": 0,
|
||||||
"num": 0
|
"num": 0
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS `fa_smoke_log` (
|
|||||||
`smoke_time` date DEFAULT NULL COMMENT '抽烟时间',
|
`smoke_time` date DEFAULT NULL COMMENT '抽烟时间',
|
||||||
`smoke_at` datetime DEFAULT NULL COMMENT '真实抽烟时间(可补录,精确到时分秒;为空则可用 createtime 近似)',
|
`smoke_at` datetime DEFAULT NULL COMMENT '真实抽烟时间(可补录,精确到时分秒;为空则可用 createtime 近似)',
|
||||||
`remark` text COMMENT '抽烟原因',
|
`remark` text COMMENT '抽烟原因',
|
||||||
|
`reason_tags` json DEFAULT NULL COMMENT '结构化原因标签(JSON数组)',
|
||||||
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
|
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
|
||||||
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
|
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
|
||||||
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
|
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ type smokeLogUpsertRequest struct {
|
|||||||
SmokeTime *string `json:"smoke_time"`
|
SmokeTime *string `json:"smoke_time"`
|
||||||
SmokeAt *string `json:"smoke_at"`
|
SmokeAt *string `json:"smoke_at"`
|
||||||
Remark *string `json:"remark"`
|
Remark *string `json:"remark"`
|
||||||
|
ReasonTags *smokemodel.StringSlice `json:"reason_tags"`
|
||||||
Level *int64 `json:"level"`
|
Level *int64 `json:"level"`
|
||||||
Num *int `json:"num"`
|
Num *int `json:"num"`
|
||||||
}
|
}
|
||||||
@@ -169,6 +170,7 @@ func buildSmokeLogInput(req smokeLogUpsertRequest) (adminservice.SmokeLogUpsertI
|
|||||||
input := adminservice.SmokeLogUpsertInput{
|
input := adminservice.SmokeLogUpsertInput{
|
||||||
UID: req.UID,
|
UID: req.UID,
|
||||||
Remark: req.Remark,
|
Remark: req.Remark,
|
||||||
|
ReasonTags: req.ReasonTags,
|
||||||
Level: req.Level,
|
Level: req.Level,
|
||||||
Num: req.Num,
|
Num: req.Num,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ type SmokeLogItem struct {
|
|||||||
SmokeTime *time.Time `json:"smoke_time,omitempty"`
|
SmokeTime *time.Time `json:"smoke_time,omitempty"`
|
||||||
SmokeAt *time.Time `json:"smoke_at,omitempty"`
|
SmokeAt *time.Time `json:"smoke_at,omitempty"`
|
||||||
Remark string `json:"remark"`
|
Remark string `json:"remark"`
|
||||||
|
ReasonTags smokemodel.StringSlice `json:"reason_tags,omitempty"`
|
||||||
Level int64 `json:"level"`
|
Level int64 `json:"level"`
|
||||||
Num int `json:"num"`
|
Num int `json:"num"`
|
||||||
CreateTime *int64 `json:"createtime,omitempty"`
|
CreateTime *int64 `json:"createtime,omitempty"`
|
||||||
@@ -49,6 +50,7 @@ type SmokeLogUpsertInput struct {
|
|||||||
SmokeTime **time.Time
|
SmokeTime **time.Time
|
||||||
SmokeAt **time.Time
|
SmokeAt **time.Time
|
||||||
Remark *string
|
Remark *string
|
||||||
|
ReasonTags *smokemodel.StringSlice
|
||||||
Level *int64
|
Level *int64
|
||||||
Num *int
|
Num *int
|
||||||
}
|
}
|
||||||
@@ -93,6 +95,7 @@ func (s *Service) ListSmokeLogs(ctx context.Context, query ListSmokeLogsQuery) (
|
|||||||
SmokeTime: row.SmokeTime,
|
SmokeTime: row.SmokeTime,
|
||||||
SmokeAt: row.SmokeAt,
|
SmokeAt: row.SmokeAt,
|
||||||
Remark: row.Remark,
|
Remark: row.Remark,
|
||||||
|
ReasonTags: row.ReasonTags,
|
||||||
Level: row.Level,
|
Level: row.Level,
|
||||||
Num: row.Num,
|
Num: row.Num,
|
||||||
CreateTime: row.CreateTime,
|
CreateTime: row.CreateTime,
|
||||||
@@ -126,6 +129,7 @@ func (s *Service) GetSmokeLog(ctx context.Context, id int) (*SmokeLogItem, error
|
|||||||
SmokeTime: row.SmokeTime,
|
SmokeTime: row.SmokeTime,
|
||||||
SmokeAt: row.SmokeAt,
|
SmokeAt: row.SmokeAt,
|
||||||
Remark: row.Remark,
|
Remark: row.Remark,
|
||||||
|
ReasonTags: row.ReasonTags,
|
||||||
Level: row.Level,
|
Level: row.Level,
|
||||||
Num: row.Num,
|
Num: row.Num,
|
||||||
CreateTime: row.CreateTime,
|
CreateTime: row.CreateTime,
|
||||||
@@ -164,6 +168,9 @@ func (s *Service) CreateSmokeLog(ctx context.Context, input SmokeLogUpsertInput)
|
|||||||
if input.Remark != nil {
|
if input.Remark != nil {
|
||||||
row.Remark = strings.TrimSpace(*input.Remark)
|
row.Remark = strings.TrimSpace(*input.Remark)
|
||||||
}
|
}
|
||||||
|
if input.ReasonTags != nil {
|
||||||
|
row.ReasonTags = *input.ReasonTags
|
||||||
|
}
|
||||||
|
|
||||||
if err := s.db.WithContext(ctx).Create(row).Error; err != nil {
|
if err := s.db.WithContext(ctx).Create(row).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -189,6 +196,9 @@ func (s *Service) UpdateSmokeLog(ctx context.Context, id int, input SmokeLogUpse
|
|||||||
if input.Remark != nil {
|
if input.Remark != nil {
|
||||||
updates["remark"] = strings.TrimSpace(*input.Remark)
|
updates["remark"] = strings.TrimSpace(*input.Remark)
|
||||||
}
|
}
|
||||||
|
if input.ReasonTags != nil {
|
||||||
|
updates["reason_tags"] = *input.ReasonTags
|
||||||
|
}
|
||||||
if input.Level != nil {
|
if input.Level != nil {
|
||||||
if *input.Level <= 0 {
|
if *input.Level <= 0 {
|
||||||
return nil, ErrInvalidInput
|
return nil, ErrInvalidInput
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"wx_service/internal/middleware"
|
"wx_service/internal/middleware"
|
||||||
"wx_service/internal/model"
|
"wx_service/internal/model"
|
||||||
quitcheckinservice "wx_service/internal/quitcheckin/service"
|
quitcheckinservice "wx_service/internal/quitcheckin/service"
|
||||||
|
smokemodel "wx_service/internal/smoke/model"
|
||||||
smokeservice "wx_service/internal/smoke/service"
|
smokeservice "wx_service/internal/smoke/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,6 +61,7 @@ type createSmokeLogRequest struct {
|
|||||||
// 真实抽烟时间(精确到时分秒,可补录)
|
// 真实抽烟时间(精确到时分秒,可补录)
|
||||||
SmokeAt string `json:"smoke_at"`
|
SmokeAt string `json:"smoke_at"`
|
||||||
Remark string `json:"remark"`
|
Remark string `json:"remark"`
|
||||||
|
ReasonTags smokemodel.StringSlice `json:"reason_tags"`
|
||||||
Level *int64 `json:"level"`
|
Level *int64 `json:"level"`
|
||||||
Num *int `json:"num"`
|
Num *int `json:"num"`
|
||||||
}
|
}
|
||||||
@@ -123,6 +125,7 @@ func (h *SmokeHandler) Create(c *gin.Context) {
|
|||||||
SmokeTime: smokeTime,
|
SmokeTime: smokeTime,
|
||||||
SmokeAt: smokeAt,
|
SmokeAt: smokeAt,
|
||||||
Remark: req.Remark,
|
Remark: req.Remark,
|
||||||
|
ReasonTags: req.ReasonTags,
|
||||||
Level: level,
|
Level: level,
|
||||||
Num: num,
|
Num: num,
|
||||||
})
|
})
|
||||||
@@ -138,6 +141,7 @@ type resistedSmokeLogRequest struct {
|
|||||||
SmokeTime string `json:"smoke_time"`
|
SmokeTime string `json:"smoke_time"`
|
||||||
SmokeAt string `json:"smoke_at"`
|
SmokeAt string `json:"smoke_at"`
|
||||||
Remark string `json:"remark"`
|
Remark string `json:"remark"`
|
||||||
|
ReasonTags smokemodel.StringSlice `json:"reason_tags"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resist 表示“想抽但忍住了”:在 fa_smoke_log 中写入 level=0,num=0。
|
// Resist 表示“想抽但忍住了”:在 fa_smoke_log 中写入 level=0,num=0。
|
||||||
@@ -174,6 +178,7 @@ func (h *SmokeHandler) Resist(c *gin.Context) {
|
|||||||
SmokeTime: smokeTime,
|
SmokeTime: smokeTime,
|
||||||
SmokeAt: smokeAt,
|
SmokeAt: smokeAt,
|
||||||
Remark: req.Remark,
|
Remark: req.Remark,
|
||||||
|
ReasonTags: req.ReasonTags,
|
||||||
Level: 0,
|
Level: 0,
|
||||||
Num: 0,
|
Num: 0,
|
||||||
})
|
})
|
||||||
@@ -334,6 +339,7 @@ type updateSmokeLogRequest struct {
|
|||||||
SmokeTime *string `json:"smoke_time"`
|
SmokeTime *string `json:"smoke_time"`
|
||||||
SmokeAt *string `json:"smoke_at"`
|
SmokeAt *string `json:"smoke_at"`
|
||||||
Remark *string `json:"remark"`
|
Remark *string `json:"remark"`
|
||||||
|
ReasonTags *smokemodel.StringSlice `json:"reason_tags"`
|
||||||
Level *int64 `json:"level"`
|
Level *int64 `json:"level"`
|
||||||
Num *int `json:"num"`
|
Num *int `json:"num"`
|
||||||
}
|
}
|
||||||
@@ -402,6 +408,7 @@ func (h *SmokeHandler) Update(c *gin.Context) {
|
|||||||
SmokeAtProvided: smokeAtProvided,
|
SmokeAtProvided: smokeAtProvided,
|
||||||
SmokeAt: smokeAt,
|
SmokeAt: smokeAt,
|
||||||
Remark: req.Remark,
|
Remark: req.Remark,
|
||||||
|
ReasonTags: req.ReasonTags,
|
||||||
Level: req.Level,
|
Level: req.Level,
|
||||||
Num: req.Num,
|
Num: req.Num,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type SmokeLog struct {
|
|||||||
SmokeAt *time.Time `gorm:"column:smoke_at;type:datetime;comment:真实抽烟时间(精确到秒)" json:"smoke_at,omitempty"`
|
SmokeAt *time.Time `gorm:"column:smoke_at;type:datetime;comment:真实抽烟时间(精确到秒)" json:"smoke_at,omitempty"`
|
||||||
|
|
||||||
Remark string `gorm:"column:remark;type:text;comment:原因/备注" json:"remark,omitempty"`
|
Remark string `gorm:"column:remark;type:text;comment:原因/备注" json:"remark,omitempty"`
|
||||||
|
ReasonTags StringSlice `gorm:"column:reason_tags;type:json;comment:结构化原因标签(JSON数组)" json:"reason_tags,omitempty"`
|
||||||
|
|
||||||
// createtime/updatetime/deletetime:秒级 Unix 时间戳(与 gorm 默认字段不同)
|
// createtime/updatetime/deletetime:秒级 Unix 时间戳(与 gorm 默认字段不同)
|
||||||
CreateTime *int64 `gorm:"column:createtime;comment:创建时间(秒)" json:"createtime,omitempty"`
|
CreateTime *int64 `gorm:"column:createtime;comment:创建时间(秒)" json:"createtime,omitempty"`
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ type smokeLogCreateRow struct {
|
|||||||
SmokeTime *time.Time `gorm:"column:smoke_time"`
|
SmokeTime *time.Time `gorm:"column:smoke_time"`
|
||||||
SmokeAt *time.Time `gorm:"column:smoke_at"`
|
SmokeAt *time.Time `gorm:"column:smoke_at"`
|
||||||
Remark string `gorm:"column:remark"`
|
Remark string `gorm:"column:remark"`
|
||||||
|
ReasonTags smokemodel.StringSlice `gorm:"column:reason_tags"`
|
||||||
CreateTime *int64 `gorm:"column:createtime"`
|
CreateTime *int64 `gorm:"column:createtime"`
|
||||||
UpdateTime *int64 `gorm:"column:updatetime"`
|
UpdateTime *int64 `gorm:"column:updatetime"`
|
||||||
DeleteTime *int64 `gorm:"column:deletetime"`
|
DeleteTime *int64 `gorm:"column:deletetime"`
|
||||||
@@ -45,6 +46,7 @@ type CreateSmokeLogRequest struct {
|
|||||||
SmokeTime *time.Time
|
SmokeTime *time.Time
|
||||||
SmokeAt *time.Time
|
SmokeAt *time.Time
|
||||||
Remark string
|
Remark string
|
||||||
|
ReasonTags smokemodel.StringSlice
|
||||||
Level int64
|
Level int64
|
||||||
Num int
|
Num int
|
||||||
}
|
}
|
||||||
@@ -82,6 +84,7 @@ func (s *SmokeLogService) Create(ctx context.Context, uid int, req CreateSmokeLo
|
|||||||
SmokeTime: smokeTime,
|
SmokeTime: smokeTime,
|
||||||
SmokeAt: smokeAt,
|
SmokeAt: smokeAt,
|
||||||
Remark: req.Remark,
|
Remark: req.Remark,
|
||||||
|
ReasonTags: req.ReasonTags,
|
||||||
CreateTime: &createTime,
|
CreateTime: &createTime,
|
||||||
UpdateTime: &updateTime,
|
UpdateTime: &updateTime,
|
||||||
Level: &level,
|
Level: &level,
|
||||||
@@ -98,6 +101,7 @@ func (s *SmokeLogService) Create(ctx context.Context, uid int, req CreateSmokeLo
|
|||||||
SmokeTime: insert.SmokeTime,
|
SmokeTime: insert.SmokeTime,
|
||||||
SmokeAt: insert.SmokeAt,
|
SmokeAt: insert.SmokeAt,
|
||||||
Remark: insert.Remark,
|
Remark: insert.Remark,
|
||||||
|
ReasonTags: insert.ReasonTags,
|
||||||
CreateTime: insert.CreateTime,
|
CreateTime: insert.CreateTime,
|
||||||
UpdateTime: insert.UpdateTime,
|
UpdateTime: insert.UpdateTime,
|
||||||
DeleteTime: insert.DeleteTime,
|
DeleteTime: insert.DeleteTime,
|
||||||
@@ -393,7 +397,7 @@ func (s *SmokeLogService) ListLatest(ctx context.Context, uid int, limit int) ([
|
|||||||
var items []smokemodel.SmokeLog
|
var items []smokemodel.SmokeLog
|
||||||
if err := s.db.WithContext(ctx).
|
if err := s.db.WithContext(ctx).
|
||||||
Model(&smokemodel.SmokeLog{}).
|
Model(&smokemodel.SmokeLog{}).
|
||||||
Select("id, uid, smoke_time, smoke_at, remark, level, num, createtime, updatetime, deletetime").
|
Select("id, uid, smoke_time, smoke_at, remark, reason_tags, level, num, createtime, updatetime, deletetime").
|
||||||
Where("uid = ? AND (deletetime IS NULL OR deletetime = 0)", uid).
|
Where("uid = ? AND (deletetime IS NULL OR deletetime = 0)", uid).
|
||||||
Order("COALESCE(smoke_at, FROM_UNIXTIME(createtime), smoke_time) DESC").
|
Order("COALESCE(smoke_at, FROM_UNIXTIME(createtime), smoke_time) DESC").
|
||||||
Order("id DESC").
|
Order("id DESC").
|
||||||
@@ -431,6 +435,7 @@ type UpdateSmokeLogRequest struct {
|
|||||||
SmokeAtProvided bool
|
SmokeAtProvided bool
|
||||||
SmokeAt *time.Time
|
SmokeAt *time.Time
|
||||||
Remark *string
|
Remark *string
|
||||||
|
ReasonTags *smokemodel.StringSlice
|
||||||
Level *int64
|
Level *int64
|
||||||
Num *int
|
Num *int
|
||||||
}
|
}
|
||||||
@@ -455,6 +460,9 @@ func (s *SmokeLogService) Update(ctx context.Context, uid int, id int, req Updat
|
|||||||
if req.Remark != nil {
|
if req.Remark != nil {
|
||||||
updates["remark"] = *req.Remark
|
updates["remark"] = *req.Remark
|
||||||
}
|
}
|
||||||
|
if req.ReasonTags != nil {
|
||||||
|
updates["reason_tags"] = *req.ReasonTags
|
||||||
|
}
|
||||||
if req.Level != nil {
|
if req.Level != nil {
|
||||||
if *req.Level < 0 {
|
if *req.Level < 0 {
|
||||||
updates["level"] = int64(1)
|
updates["level"] = int64(1)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ CREATE TABLE fa_smoke_log (
|
|||||||
smoke_time DATE NULL,
|
smoke_time DATE NULL,
|
||||||
smoke_at DATETIME NULL,
|
smoke_at DATETIME NULL,
|
||||||
remark TEXT,
|
remark TEXT,
|
||||||
|
reason_tags TEXT,
|
||||||
createtime INTEGER,
|
createtime INTEGER,
|
||||||
updatetime INTEGER,
|
updatetime INTEGER,
|
||||||
deletetime INTEGER,
|
deletetime INTEGER,
|
||||||
@@ -68,3 +69,60 @@ func TestSmokeLogServiceCreateKeepsZeroForResisted(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSmokeLogServiceCreatePersistsReasonTags(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
db := setupSmokeLogServiceTestDB(t)
|
||||||
|
svc := NewSmokeLogService(db)
|
||||||
|
|
||||||
|
smokeAt := time.Date(2026, 3, 4, 8, 30, 0, 0, time.Local)
|
||||||
|
_, err := svc.Create(context.Background(), 1002, CreateSmokeLogRequest{
|
||||||
|
SmokeAt: &smokeAt,
|
||||||
|
Remark: "压力大;会后补了一根",
|
||||||
|
ReasonTags: smokemodel.StringSlice{"stress", "social"},
|
||||||
|
Level: 3,
|
||||||
|
Num: 1,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("create log with reason tags: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var got smokemodel.SmokeLog
|
||||||
|
if err := db.Where("uid = ?", 1002).Order("id DESC").First(&got).Error; err != nil {
|
||||||
|
t.Fatalf("load created record: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(got.ReasonTags) != 2 || got.ReasonTags[0] != "stress" || got.ReasonTags[1] != "social" {
|
||||||
|
t.Fatalf("created reason_tags=%v, want=[stress social]", got.ReasonTags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSmokeLogServiceUpdatePersistsReasonTags(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
db := setupSmokeLogServiceTestDB(t)
|
||||||
|
svc := NewSmokeLogService(db)
|
||||||
|
|
||||||
|
smokeAt := time.Date(2026, 3, 4, 9, 0, 0, 0, time.Local)
|
||||||
|
record, err := svc.Create(context.Background(), 1003, CreateSmokeLogRequest{
|
||||||
|
SmokeAt: &smokeAt,
|
||||||
|
Remark: "old",
|
||||||
|
Level: 2,
|
||||||
|
Num: 1,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("create seed log: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
reasonTags := smokemodel.StringSlice{"after_meal", "other"}
|
||||||
|
updated, err := svc.Update(context.Background(), 1003, record.ID, UpdateSmokeLogRequest{
|
||||||
|
ReasonTags: &reasonTags,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("update reason_tags: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(updated.ReasonTags) != 2 || updated.ReasonTags[0] != "after_meal" || updated.ReasonTags[1] != "other" {
|
||||||
|
t.Fatalf("updated reason_tags=%v, want=[after_meal other]", updated.ReasonTags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user