2026-08-06 16:33:45 +08:00
|
|
|
|
package web
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-08-06 16:56:18 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"log"
|
2026-08-06 16:33:45 +08:00
|
|
|
|
"net/http"
|
2026-08-07 15:16:05 +08:00
|
|
|
|
"net/url"
|
2026-08-06 16:33:45 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2026-08-06 16:56:18 +08:00
|
|
|
|
|
2026-08-07 15:16:05 +08:00
|
|
|
|
"cmautobuy/admin/repository"
|
2026-08-06 16:56:18 +08:00
|
|
|
|
"cmautobuy/admin/service"
|
2026-08-06 16:33:45 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 2. 顺运宝数据 ----------
|
|
|
|
|
|
|
|
|
|
|
|
// SybList 渲染货运单列表页。
|
|
|
|
|
|
// 「匹配状态」是算出来的(sku_mappings 里有没有记录),不是存的字段。
|
|
|
|
|
|
func (h *Handler) SybList(c *gin.Context) {
|
|
|
|
|
|
keyword := c.Query("order_no")
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(骨架): 查货运单,并左联 sku_mappings 得出匹配状态
|
|
|
|
|
|
var rows []gin.H
|
|
|
|
|
|
|
2026-08-06 16:56:18 +08:00
|
|
|
|
c.HTML(http.StatusOK, "syb/list", page(c, "syb", "顺运宝数据", gin.H{
|
2026-08-06 16:33:45 +08:00
|
|
|
|
"Keyword": keyword,
|
|
|
|
|
|
"Rows": rows,
|
|
|
|
|
|
"Status": "尚未实现:同步或手工录入后这里显示货运单",
|
|
|
|
|
|
}))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SybSync 从顺运宝同步待处理货运单。
|
|
|
|
|
|
//
|
|
|
|
|
|
// MVP 阶段只做占位,见 docs/admin/01-requirements.md §11 待确认 #1。
|
|
|
|
|
|
func (h *Handler) SybSync(c *gin.Context) {
|
|
|
|
|
|
// TODO(等待确认): 顺运宝的同步方式(接口?导出文件?)还没定。
|
|
|
|
|
|
// 定了之后在这里实现,注意完整响应要原样存进 syb_data 字段。
|
|
|
|
|
|
fail(c, http.StatusNotImplemented,
|
|
|
|
|
|
"同步功能待接入。顺运宝的对接方式尚未确定,"+
|
|
|
|
|
|
"当前可以先手工录入货运单用于联调。")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SybMatch 保存规格匹配结果。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 关键:保存的是**可复用的 SKU 映射**(写 sku_mappings),
|
|
|
|
|
|
// 不是这一张订单的临时数据。下次遇到同一个蝦皮 SKU 自动带出,
|
|
|
|
|
|
// 操作员只需确认。见 docs/admin/03-data-model.md §5。
|
|
|
|
|
|
func (h *Handler) SybMatch(c *gin.Context) {
|
|
|
|
|
|
// TODO(骨架): upsert sku_mappings
|
|
|
|
|
|
fail(c, http.StatusNotImplemented, "规格匹配尚未实现。")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SybCreateTask 由勾选的货运单创建采购任务。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 六条校验缺一不可,见 docs/admin/01-requirements.md §5:
|
|
|
|
|
|
// 1. 已填 PDD 链接
|
|
|
|
|
|
// 2. 已采集成功(pdd_data 非空)
|
|
|
|
|
|
// 3. 已有 SKU 映射
|
|
|
|
|
|
// 4. 数量 > 0
|
|
|
|
|
|
// 5. **价格上限已填且 > 0**(Client 的价格保护,没有它会拒绝执行)
|
|
|
|
|
|
// 6. 已选择分配的客户端
|
|
|
|
|
|
//
|
|
|
|
|
|
// 校验不过的**不要静默跳过**,要列出来告诉操作员缺什么。
|
|
|
|
|
|
func (h *Handler) SybCreateTask(c *gin.Context) {
|
|
|
|
|
|
// TODO(骨架): 调 service.CreatePurchaseTasks(sybIDs, clientID)
|
|
|
|
|
|
fail(c, http.StatusNotImplemented, "创建采购任务尚未实现。")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SybDelete 批量删除货运单。
|
|
|
|
|
|
func (h *Handler) SybDelete(c *gin.Context) {
|
|
|
|
|
|
// TODO(骨架)
|
|
|
|
|
|
fail(c, http.StatusNotImplemented, "删除功能尚未实现。")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-07 15:16:05 +08:00
|
|
|
|
// ---------- 3. 采集采购 ----------
|
2026-08-06 16:33:45 +08:00
|
|
|
|
//
|
2026-08-07 15:16:05 +08:00
|
|
|
|
// `tasks` 是一张表,用 task_type 区分采集和采购,这个模块把两种任务放在
|
|
|
|
|
|
// 同一个列表里显示,靠「目标」一列概括各自的业务信息,见 #19。
|
|
|
|
|
|
// 拼接和翻译逻辑全在 service/task.go,这里只做取参数 → 调 service → 渲染。
|
|
|
|
|
|
|
|
|
|
|
|
// TaskList 渲染「采集采购」列表页。
|
2026-08-06 16:33:45 +08:00
|
|
|
|
func (h *Handler) TaskList(c *gin.Context) {
|
2026-08-07 15:16:05 +08:00
|
|
|
|
filter := repository.TaskFilter{
|
|
|
|
|
|
Type: service.ParseTaskType(c.Query("type")),
|
|
|
|
|
|
Status: service.ParseTaskStatus(c.Query("status")),
|
|
|
|
|
|
Keyword: c.Query("q"),
|
|
|
|
|
|
}
|
2026-08-06 16:33:45 +08:00
|
|
|
|
|
2026-08-07 15:16:05 +08:00
|
|
|
|
result, err := service.ListTasksView(h.db, filter)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(c, http.StatusInternalServerError,
|
|
|
|
|
|
"读取任务列表失败,数据没有被改动。刷新页面重试;一直失败请把这句话报给维护者。")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-08-06 16:33:45 +08:00
|
|
|
|
|
2026-08-07 15:16:05 +08:00
|
|
|
|
// 底部状态条平时显示统计,刚做完删除操作时先显示操作结果,
|
|
|
|
|
|
// 跳转带过来的 msg 参数,见 h.taskRedirect。
|
|
|
|
|
|
statusLine := result.StatusLine()
|
|
|
|
|
|
if msg := c.Query("msg"); msg != "" {
|
|
|
|
|
|
statusLine = msg + " · " + statusLine
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "task/list", page(c, "tasks", "采集采购", gin.H{
|
|
|
|
|
|
"Rows": result.Rows,
|
|
|
|
|
|
"Keyword": filter.Keyword,
|
|
|
|
|
|
"Status": statusLine,
|
|
|
|
|
|
"TypeFilter": string(filter.Type),
|
|
|
|
|
|
"StatusFilter": string(filter.Status),
|
|
|
|
|
|
"TypeOptions": service.TaskTypeOptions(),
|
|
|
|
|
|
"StatusOptions": service.TaskStatusOptions(),
|
|
|
|
|
|
"IsFiltered": result.IsFiltered,
|
2026-08-06 16:33:45 +08:00
|
|
|
|
}))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-07 15:16:05 +08:00
|
|
|
|
// TaskDetail 渲染双击行弹出的只读详情弹窗内容(不是整页)。
|
|
|
|
|
|
//
|
|
|
|
|
|
// `[必须]` 只读。改派 / 重试 / 取消是后续工单的范围,这里不提供入口。
|
|
|
|
|
|
// 弹窗机制复用 #18 已有的那套(static/js/app.js 的 setupRowDetail),
|
|
|
|
|
|
// 不新造一套。
|
|
|
|
|
|
func (h *Handler) TaskDetail(c *gin.Context) {
|
|
|
|
|
|
taskID := c.Query("id")
|
|
|
|
|
|
if taskID == "" {
|
|
|
|
|
|
fail(c, http.StatusBadRequest, "任务编号不对,请刷新页面后重试。")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
detail, err := service.GetTaskDetail(h.db, taskID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(c, http.StatusInternalServerError, "读取任务详情失败,数据没有被改动。")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if detail == nil {
|
|
|
|
|
|
fail(c, http.StatusNotFound, "这个任务不存在,请刷新页面。")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "task/detail_modal", gin.H{"D": detail})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-06 16:33:45 +08:00
|
|
|
|
// TaskDelete 批量删除任务。
|
2026-08-07 15:16:05 +08:00
|
|
|
|
//
|
|
|
|
|
|
// `tasks` 表没有软删除列——加一列是数据库结构变更,不在本工单范围内,
|
|
|
|
|
|
// 见 repository.DeleteTasks 的注释。二次确认在前端做(data-confirm-delete)。
|
2026-08-06 16:33:45 +08:00
|
|
|
|
func (h *Handler) TaskDelete(c *gin.Context) {
|
2026-08-07 15:16:05 +08:00
|
|
|
|
ids := c.PostFormArray("ids")
|
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
|
|
h.taskRedirect(c, "没有勾选任何任务,没有删除")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
n, err := service.DeleteTasks(h.db, ids)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(c, http.StatusInternalServerError,
|
|
|
|
|
|
"删除失败:"+err.Error()+"。没有删除任何记录。")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
h.taskRedirect(c, fmt.Sprintf("已删除 %d 条", n))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// taskRedirect 处理完写操作后跳回列表页,带上当前筛选条件,
|
|
|
|
|
|
// 免得操作员每做一次删除就要重新筛一遍。用 303 而不是直接渲染,
|
|
|
|
|
|
// 是为了让浏览器地址栏变成 GET /tasks——按 F5 不会重复提交刚才的删除。
|
|
|
|
|
|
func (h *Handler) taskRedirect(c *gin.Context, msg string) {
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
|
|
if t := c.PostForm("type"); t != "" {
|
|
|
|
|
|
params.Set("type", t)
|
|
|
|
|
|
}
|
|
|
|
|
|
if s := c.PostForm("status"); s != "" {
|
|
|
|
|
|
params.Set("status", s)
|
|
|
|
|
|
}
|
|
|
|
|
|
if q := c.PostForm("q"); q != "" {
|
|
|
|
|
|
params.Set("q", q)
|
|
|
|
|
|
}
|
|
|
|
|
|
if msg != "" {
|
|
|
|
|
|
params.Set("msg", msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
target := "/tasks"
|
|
|
|
|
|
if len(params) > 0 {
|
|
|
|
|
|
target += "?" + params.Encode()
|
|
|
|
|
|
}
|
|
|
|
|
|
c.Redirect(http.StatusSeeOther, target)
|
2026-08-06 16:33:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 4. 客户端列表 ----------
|
|
|
|
|
|
|
|
|
|
|
|
// ClientList 渲染客户端列表页。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 「状态」是**算出来的**:last_seen_at 在 N 分钟内为在线,否则离线。
|
|
|
|
|
|
// 数据库里没有 status 字段,存成字段会和真实情况不同步。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 客户端执行长任务期间不调 claim,可能显示为离线,属正常现象
|
|
|
|
|
|
// (没有心跳是有意的,见 docs/admin/04-client-api.md §3)。
|
|
|
|
|
|
func (h *Handler) ClientList(c *gin.Context) {
|
|
|
|
|
|
keyword := c.Query("name")
|
|
|
|
|
|
|
2026-08-06 16:56:18 +08:00
|
|
|
|
views, err := service.ListClientViews(h.db, keyword, h.onlineThreshold)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(c, http.StatusInternalServerError,
|
|
|
|
|
|
"读取客户端列表失败,数据没有被改动。请稍后重试,或查看 data/logs/ 里的日志。")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-08-06 16:33:45 +08:00
|
|
|
|
|
2026-08-06 16:56:18 +08:00
|
|
|
|
online := 0
|
|
|
|
|
|
for _, v := range views {
|
|
|
|
|
|
if v.Status == "在线" {
|
|
|
|
|
|
online++
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
status := fmt.Sprintf("共 %d 台客户端 · 在线 %d · 离线 %d",
|
|
|
|
|
|
len(views), online, len(views)-online)
|
|
|
|
|
|
if len(views) == 0 {
|
|
|
|
|
|
status = "还没有客户端。客户端第一次调用领取接口时会自动登记。"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "client/list", page(c, "clients", "客户端列表", gin.H{
|
2026-08-06 16:33:45 +08:00
|
|
|
|
"Keyword": keyword,
|
2026-08-06 16:56:18 +08:00
|
|
|
|
"Rows": views,
|
|
|
|
|
|
"Status": status,
|
2026-08-06 16:33:45 +08:00
|
|
|
|
}))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ClientDelete 批量删除客户端。
|
2026-08-06 16:56:18 +08:00
|
|
|
|
//
|
|
|
|
|
|
// 二次确认在前端做(见 static/js/app.js),这里直接删。
|
|
|
|
|
|
// 删掉之后客户端下次调 claim 会重新登记,属于正常行为。
|
2026-08-06 16:33:45 +08:00
|
|
|
|
func (h *Handler) ClientDelete(c *gin.Context) {
|
2026-08-06 16:56:18 +08:00
|
|
|
|
ids := c.PostFormArray("ids")
|
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
|
|
fail(c, http.StatusBadRequest, "没有选中任何客户端,请勾选后再删除。")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
n, err := service.DeleteClients(h.db, ids)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(c, http.StatusInternalServerError,
|
|
|
|
|
|
"删除失败,数据没有被改动。请稍后重试,或查看 data/logs/ 里的日志。")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Printf("clients_deleted count=%d", n)
|
|
|
|
|
|
c.Redirect(http.StatusSeeOther, "/clients")
|
2026-08-06 16:33:45 +08:00
|
|
|
|
}
|