Files
Ydisks-Xianyu-Helper/internal/server/default_reply_handlers.go
T
ChristandClaude Opus 4.8 36a7f74154 refactor(db): 阶段B+C - MySQL/Postgres migration + 方言适配器
migration
- migrations/mysql/ 8 个文件:BIGINT AUTO_INCREMENT、VARCHAR 主键、
  TINYINT(1) 布尔、INSERT IGNORE、ON DUPLICATE KEY UPDATE
- migrations/postgres/ 8 个文件:BIGSERIAL、BOOLEAN、ON CONFLICT DO UPDATE
- 跳过 SQLite 历史遗留列(auto_create_delivery_rule 等)在 mysql/pg 00006
  不创建,00008 不删除

方言适配器 (internal/db/dialect.go)
- DialectUpsert: SQLite/PG 用 ON CONFLICT DO UPDATE + EXCLUDED.col;
  MySQL 用 ON DUPLICATE KEY UPDATE + VALUES(col)
- DialectInsertIgnore + DialectInsertIgnorePrefix: MySQL INSERT IGNORE;
  SQLite/PG ON CONFLICT DO NOTHING
- DialectQuote: MySQL/SQLite 反引号,Postgres 双引号(处理 key 等保留字列名)

业务 SQL 改造(16 处 upsert)
- db: cookies/extras(系统设置/商品信息/通知绑定)/orders/reply/automation
  全部改用方言适配器;item_replay Set 改 DELETE+INSERT(无自然唯一键)
- server: notification_handlers/settings_handlers/default_reply_handlers
  的 ON CONFLICT/INSERT OR REPLACE 改用 db.DialectUpsert
- 各 repository struct 加 Dialect 字段,NewStore 注入

验证:SQLite 全测试通过(回归),MySQL/PG migration 已就位待真实实例验证

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 12:58:23 +08:00

149 lines
4.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
import (
"net/http"
"github.com/go-chi/chi/v5"
"xianyu-go/internal/db"
)
// btoi bool→int(SQLite 无原生 bool)。
func btoi(b bool) int {
if b {
return 1
}
return 0
}
// nullIfEmpty 空串存为 NULL。
func nullIfEmpty(s string) any {
if s == "" {
return nil
}
return s
}
// mountDefaultRepliesReal 默认回复端点。
func (s *Server) mountDefaultRepliesReal(r chi.Router) {
r.Get("/default-replies/{cid}", s.getDefaultReply)
r.Put("/default-replies/{cid}", s.setDefaultReply)
r.Get("/default-replies", s.listDefaultReplies)
r.Delete("/default-replies/{cid}", s.deleteDefaultReply)
r.Get("/api/default-replies", s.listDefaultRepliesMap)
r.Get("/api/default-reply/{cid}", s.getDefaultReply)
r.Put("/api/default-reply/{cid}", s.setDefaultReply)
r.Delete("/api/default-reply/{cid}", s.deleteDefaultReply)
r.Post("/api/default-reply/{cid}/clear-records", s.clearDefaultReplyRecords)
}
func (s *Server) getDefaultReply(w http.ResponseWriter, r *http.Request) {
cid := chi.URLParam(r, "cid")
dr, err := s.Store.DefaultReps.Get(r.Context(), cid)
if err != nil {
writeJSON(w, http.StatusOK, map[string]any{"enabled": false, "reply_content": "", "reply_once": false})
return
}
writeJSON(w, http.StatusOK, map[string]any{
"enabled": dr.Enabled, "reply_content": dr.ReplyContent,
"reply_image_url": dr.ReplyImageURL, "reply_once": dr.ReplyOnce,
})
}
func (s *Server) setDefaultReply(w http.ResponseWriter, r *http.Request) {
cid := chi.URLParam(r, "cid")
var req struct {
Enabled bool `json:"enabled"`
ReplyContent string `json:"reply_content"`
ReplyImageURL string `json:"reply_image_url"`
ReplyOnce bool `json:"reply_once"`
}
if err := decodeJSON(r, &req); err != nil {
writeErr(w, http.StatusBadRequest, "请求格式错误")
return
}
_, err := s.Store.DB.ExecContext(r.Context(),
`INSERT INTO default_replies (cookie_id, enabled, reply_content, reply_image_url, reply_once, updated_at)
VALUES (?,?,?,?,?,CURRENT_TIMESTAMP)`+db.DialectUpsert(s.Store.Dialect, []string{"cookie_id"}, map[string]string{
"enabled": "EXCLUDED.enabled",
"reply_content": "EXCLUDED.reply_content",
"reply_image_url": "EXCLUDED.reply_image_url",
"reply_once": "EXCLUDED.reply_once",
"updated_at": "CURRENT_TIMESTAMP",
}),
cid, btoi(req.Enabled), req.ReplyContent, nullIfEmpty(req.ReplyImageURL), btoi(req.ReplyOnce))
if err != nil {
writeErr(w, http.StatusInternalServerError, "保存失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true})
}
func (s *Server) listDefaultReplies(w http.ResponseWriter, r *http.Request) {
rows, err := s.Store.DB.QueryContext(r.Context(),
`SELECT cookie_id, enabled, reply_content, reply_once, reply_image_url FROM default_replies`)
if err != nil {
writeErr(w, http.StatusInternalServerError, "查询失败")
return
}
defer rows.Close()
var out []map[string]any
for rows.Next() {
var cid, content, imageURL string
var enabled, replyOnce int
if err := rows.Scan(&cid, &enabled, &content, &replyOnce, &imageURL); err != nil {
continue
}
out = append(out, map[string]any{
"cookie_id": cid, "enabled": enabled != 0, "reply_content": content,
"reply_once": replyOnce != 0, "reply_image_url": imageURL,
})
}
writeJSON(w, http.StatusOK, out)
}
func (s *Server) listDefaultRepliesMap(w http.ResponseWriter, r *http.Request) {
rows, err := s.Store.DB.QueryContext(r.Context(),
`SELECT cookie_id, enabled, COALESCE(reply_content, ''), reply_once, COALESCE(reply_image_url, '') FROM default_replies`)
if err != nil {
writeErr(w, http.StatusInternalServerError, "查询失败")
return
}
defer rows.Close()
out := make(map[string]any)
for rows.Next() {
var cid, content, imageURL string
var enabled, replyOnce int
if err := rows.Scan(&cid, &enabled, &content, &replyOnce, &imageURL); err != nil {
continue
}
out[cid] = map[string]any{
"cookie_id": cid,
"enabled": enabled != 0,
"reply_content": content,
"reply_once": replyOnce != 0,
"reply_image_url": imageURL,
}
}
writeJSON(w, http.StatusOK, out)
}
func (s *Server) deleteDefaultReply(w http.ResponseWriter, r *http.Request) {
cid := chi.URLParam(r, "cid")
_, err := s.Store.DB.ExecContext(r.Context(), `DELETE FROM default_replies WHERE cookie_id=?`, cid)
if err != nil {
writeErr(w, http.StatusInternalServerError, "删除失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true})
}
func (s *Server) clearDefaultReplyRecords(w http.ResponseWriter, r *http.Request) {
cid := chi.URLParam(r, "cid")
if _, err := s.Store.DB.ExecContext(r.Context(), `DELETE FROM default_reply_records WHERE cookie_id=?`, cid); err != nil {
writeErr(w, http.StatusInternalServerError, "清空失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true})
}