build / build-and-test (push) Successful in 3m19s
1) 工作台速录 - 手机尾号输入框下方新增 3×4 触摸小键盘(1-9、0、⌫、✓),点 ✓ 直接提交;实体键盘仍可输入 - 抽屉顶部新增秒级时钟(HH:MM:SS),位于日期标题上方 2) 日历格子 - 移除排单缩略信息(客户名/套餐色签/“x/y 单”小字) - 保留左侧打手颜色竖条,中间改为大字「待完成 / 总数」,例如 1/2 = 还剩 1 单、共 2 单 - 大屏格子按此前约定保持原样 3) 客户停用 / 恢复 / 彻底删除 - customers 新增 archived 列(0/1);停用后默认从客户列表、日历、待办、大屏、到期提醒、徽标中隐藏,订阅与打卡记录原样保留 - 恢复后立即回到日历;列表右上角可切换「显示已停用」,已停用行带标记 - 彻底删除为二次确认操作,依赖外键级联清理订阅、打卡、暂停、追加记录与加急标记 - 停用/恢复/删除都会写入操作日志 4) 按钮收纳 - 任务行只露出「完成」,跳过 / 加急(取消加急)/ 追加收进「⋯」菜单;已完成行只露「撤销打卡」等,点空白处自动收起 - 加急、跳过等原有能力不变,仅入口折叠 5) 顶栏 - 在「今日到期」「3 天内到期」右侧新增「7 天内到期」徽标,固定统计未来 7 天 6) 视觉 - 移除管理端背景的横线装饰,改为纯底色;大屏网格背景保留 数据库兼容性: - 只新增 customers.archived 一列,老库启动时自动 ALTER,可重复迁移;原有表结构与接口不变 测试: - 新增单测 TestCustomerArchiveFilters:停用后日历/到期/徽标/搜索/列表全部过滤、恢复回日历、彻底删除级联清理订阅与打卡 - 功能验证 20 项(键盘、格子计数、折叠菜单、7 天徽标、时钟、停用/恢复/删除、背景)全部通过 - 原有 119 项验收回归全绿;go vet / go test ./... 通过;版本号 1.5.0
396 lines
12 KiB
Go
396 lines
12 KiB
Go
package web
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
"sort"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"daili/internal/domain"
|
||
"daili/internal/store"
|
||
)
|
||
|
||
type customersData struct {
|
||
List []store.Customer
|
||
Pager Pager
|
||
Q string
|
||
Total int
|
||
PackageCount int
|
||
NoPackages bool
|
||
ShowArchived bool
|
||
ArchivedCount int
|
||
}
|
||
|
||
func (s *Server) handleCustomers(w http.ResponseWriter, r *http.Request) {
|
||
today := domain.Today()
|
||
warn := s.warnDays()
|
||
q := r.URL.Query().Get("q")
|
||
pageNo := s.queryInt(r, "page", 1)
|
||
if pageNo < 1 {
|
||
pageNo = 1
|
||
}
|
||
showArchived := r.URL.Query().Get("archived") == "1"
|
||
list, total, err := s.db.ListCustomers(q, pageSize, (pageNo-1)*pageSize, domain.Date(today), warn, showArchived)
|
||
if err != nil {
|
||
http.Error(w, "读取客户失败", http.StatusInternalServerError)
|
||
return
|
||
}
|
||
pager := newPager(r, pageNo, pageSize, total)
|
||
if pager.Page != pageNo {
|
||
list, total, err = s.db.ListCustomers(q, pageSize, (pager.Page-1)*pageSize, domain.Date(today), warn, showArchived)
|
||
if err != nil {
|
||
http.Error(w, "读取客户失败", http.StatusInternalServerError)
|
||
return
|
||
}
|
||
}
|
||
pkgs, _ := s.db.ListPackages(false)
|
||
archivedN, _ := s.db.CountArchivedCustomers()
|
||
data := &customersData{List: list, Pager: pager, Q: q, Total: total, PackageCount: len(pkgs), NoPackages: len(pkgs) == 0,
|
||
ShowArchived: showArchived, ArchivedCount: archivedN}
|
||
p := s.newPage(r, "客户", "customers")
|
||
p.D = data
|
||
s.render(w, r, "customers", p)
|
||
}
|
||
|
||
type customerFormData struct {
|
||
Customer *store.Customer
|
||
IsNew bool
|
||
Users []store.User
|
||
}
|
||
|
||
func (s *Server) handleCustomerNewPage(w http.ResponseWriter, r *http.Request) {
|
||
p := s.newPage(r, "新建客户", "customers")
|
||
users, _ := s.db.ActiveUsers()
|
||
p.D = &customerFormData{Customer: &store.Customer{}, IsNew: true, Users: users}
|
||
s.render(w, r, "customer_form", p)
|
||
}
|
||
|
||
func (s *Server) readCustomerForm(r *http.Request) *store.Customer {
|
||
return &store.Customer{
|
||
Tail: strings.TrimSpace(r.PostFormValue("tail")),
|
||
Name: strings.TrimSpace(r.PostFormValue("name")),
|
||
GameName: strings.TrimSpace(r.PostFormValue("game_name")),
|
||
GameServer: strings.TrimSpace(r.PostFormValue("game_server")),
|
||
Contact: strings.TrimSpace(r.PostFormValue("contact")),
|
||
Tags: normalizeTags(r.PostFormValue("tags")),
|
||
Source: strings.TrimSpace(r.PostFormValue("source")),
|
||
Note: strings.TrimSpace(r.PostFormValue("note")),
|
||
AssigneeID: int64(s.formInt(r, "assignee_id", 0)),
|
||
}
|
||
}
|
||
|
||
func normalizeTags(v string) string {
|
||
parts := strings.FieldsFunc(v, func(r rune) bool { return r == ',' || r == ',' || r == ' ' || r == ';' || r == ';' })
|
||
seen := map[string]bool{}
|
||
out := []string{}
|
||
for _, p := range parts {
|
||
p = strings.TrimSpace(p)
|
||
if p == "" || seen[p] {
|
||
continue
|
||
}
|
||
seen[p] = true
|
||
out = append(out, p)
|
||
}
|
||
return strings.Join(out, ",")
|
||
}
|
||
|
||
func validateCustomer(c *store.Customer) string {
|
||
if c.Tail == "" {
|
||
return "手机尾号不能为空"
|
||
}
|
||
if len([]rune(c.Tail)) > 8 {
|
||
return "手机尾号最长 8 位"
|
||
}
|
||
if c.Name == "" {
|
||
return "客户名不能为空"
|
||
}
|
||
if len([]rune(c.Name)) > 32 {
|
||
return "客户名最长 32 字"
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func (s *Server) handleCustomerCreate(w http.ResponseWriter, r *http.Request) {
|
||
c := s.readCustomerForm(r)
|
||
p := s.newPage(r, "新建客户", "customers")
|
||
if msg := validateCustomer(c); msg != "" {
|
||
users, _ := s.db.ActiveUsers()
|
||
p.D = &customerFormData{Customer: c, IsNew: true, Users: users}
|
||
s.fail(w, r, "customer_form", msg, p)
|
||
return
|
||
}
|
||
id, err := s.db.CreateCustomer(c)
|
||
if err != nil {
|
||
msg := "创建失败:" + err.Error()
|
||
if errors.Is(err, store.ErrDuplicate) {
|
||
msg = "该「尾号 + 姓名」的客户已存在,请直接搜索后追加订阅"
|
||
}
|
||
users, _ := s.db.ActiveUsers()
|
||
p.D = &customerFormData{Customer: c, IsNew: true, Users: users}
|
||
s.fail(w, r, "customer_form", msg, p)
|
||
return
|
||
}
|
||
s.logOp(r, "新建客户", "customer", id, "新建客户 "+c.Tail+" "+c.Name)
|
||
s.redirect(w, r, "/customers/"+itoa(id), "客户已创建,接着为他添加订阅")
|
||
}
|
||
|
||
func (s *Server) handleCustomerEditPage(w http.ResponseWriter, r *http.Request) {
|
||
c, err := s.db.Customer(pathID(r))
|
||
if err != nil {
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
p := s.newPage(r, "编辑客户", "customers")
|
||
p.D = &customerFormData{Customer: c, IsNew: false}
|
||
s.render(w, r, "customer_form", p)
|
||
}
|
||
|
||
func (s *Server) handleCustomerUpdate(w http.ResponseWriter, r *http.Request) {
|
||
id := pathID(r)
|
||
old, err := s.db.Customer(id)
|
||
if err != nil {
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
c := s.readCustomerForm(r)
|
||
c.ID = id
|
||
p := s.newPage(r, "编辑客户", "customers")
|
||
if msg := validateCustomer(c); msg != "" {
|
||
p.D = &customerFormData{Customer: c, IsNew: false}
|
||
s.fail(w, r, "customer_form", msg, p)
|
||
return
|
||
}
|
||
if err := s.db.UpdateCustomer(c); err != nil {
|
||
msg := "保存失败:" + err.Error()
|
||
if errors.Is(err, store.ErrDuplicate) {
|
||
msg = "该「尾号 + 姓名」的客户已存在"
|
||
}
|
||
p.D = &customerFormData{Customer: c, IsNew: false}
|
||
s.fail(w, r, "customer_form", msg, p)
|
||
return
|
||
}
|
||
s.logOp(r, "编辑客户", "customer", id, "修改客户 "+c.Tail+" "+c.Name+"(原 "+old.Tail+" "+old.Name+")")
|
||
s.redirect(w, r, "/customers/"+itoa(id), "客户资料已保存")
|
||
}
|
||
|
||
// CheckinRow 是打卡历史的展示行。
|
||
type CheckinRow struct {
|
||
store.Checkin
|
||
SubName string
|
||
SubColor string
|
||
}
|
||
|
||
type customerDetailData struct {
|
||
Customer *store.Customer
|
||
Subs []SubView
|
||
Adjustments []store.Adjustment
|
||
AdjSubName map[int64]string
|
||
AdjSubColor map[int64]string
|
||
Checkins []CheckinRow
|
||
Packages []store.Package
|
||
Users []store.User
|
||
Timeline *Timeline
|
||
Extend *ExtendPanel
|
||
TotalRemain string
|
||
}
|
||
|
||
func (s *Server) handleCustomerDetail(w http.ResponseWriter, r *http.Request) {
|
||
id := pathID(r)
|
||
c, err := s.db.Customer(id)
|
||
if err != nil {
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
today := domain.Today()
|
||
warn := s.warnDays()
|
||
|
||
subs, err := s.db.SubsByCustomer(id)
|
||
if err != nil {
|
||
http.Error(w, "读取订阅失败", http.StatusInternalServerError)
|
||
return
|
||
}
|
||
pkgs, _ := s.db.ListPackages(false)
|
||
ids := make([]int64, 0, len(subs))
|
||
subByID := map[int64]store.Subscription{}
|
||
for _, sub := range subs {
|
||
ids = append(ids, sub.ID)
|
||
subByID[sub.ID] = sub
|
||
}
|
||
pauses, _ := s.db.PausesForSubs(ids)
|
||
|
||
users, _ := s.db.ActiveUsers()
|
||
data := &customerDetailData{
|
||
Users: users,
|
||
Customer: c,
|
||
Subs: subViews(subs, today, warn),
|
||
Packages: pkgs,
|
||
Timeline: buildTimeline(subs, pauses, today),
|
||
AdjSubName: map[int64]string{},
|
||
AdjSubColor: map[int64]string{},
|
||
}
|
||
for _, sub := range subs {
|
||
data.AdjSubName[sub.ID] = sub.PackageName
|
||
data.AdjSubColor[sub.ID] = sub.PackageColor
|
||
adj, err := s.db.AdjustmentsBySub(sub.ID)
|
||
if err == nil {
|
||
data.Adjustments = append(data.Adjustments, adj...)
|
||
}
|
||
if ci, err := s.db.CheckinsForSub(sub.ID, 30); err == nil {
|
||
for _, one := range ci {
|
||
data.Checkins = append(data.Checkins, CheckinRow{Checkin: one, SubName: sub.PackageName, SubColor: sub.PackageColor})
|
||
}
|
||
}
|
||
}
|
||
// 按时间排序(新→旧)
|
||
sortAdjustments(data.Adjustments)
|
||
sortCheckins(data.Checkins)
|
||
|
||
if eid := s.queryInt(r, "extend", 0); eid > 0 {
|
||
if sub, ok := subByID[int64(eid)]; ok {
|
||
if pkg, err := s.db.Package(sub.PackageID); err == nil {
|
||
back := r.URL.Path + "?" + r.URL.RawQuery
|
||
data.Extend = buildExtendPanel(&sub, pkg, today, back, warn)
|
||
}
|
||
}
|
||
}
|
||
|
||
p := s.newPage(r, c.Tail+" "+c.Name, "customers")
|
||
p.D = data
|
||
s.render(w, r, "customer_detail", p)
|
||
}
|
||
|
||
// handleCustomerArchive 停用客户:默认从列表、日历、大屏隐藏,订阅保留。
|
||
func (s *Server) handleCustomerArchive(w http.ResponseWriter, r *http.Request) {
|
||
id := pathID(r)
|
||
c, err := s.db.Customer(id)
|
||
if err != nil {
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
if err := s.db.SetCustomerArchived(id, true); err != nil {
|
||
s.redirect(w, r, "/customers/"+itoa(id), "停用失败:"+err.Error())
|
||
return
|
||
}
|
||
s.logOp(r, "停用客户", "customer", id, c.Tail+" "+c.Name+"(订阅保留,不再上日历)")
|
||
s.redirect(w, r, "/customers", "已停用客户 "+c.Tail+" "+c.Name+",可在「已停用」里恢复")
|
||
}
|
||
|
||
// handleCustomerRestore 恢复客户,订单一并回到日历。
|
||
func (s *Server) handleCustomerRestore(w http.ResponseWriter, r *http.Request) {
|
||
id := pathID(r)
|
||
c, err := s.db.Customer(id)
|
||
if err != nil {
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
if err := s.db.SetCustomerArchived(id, false); err != nil {
|
||
s.redirect(w, r, "/customers/"+itoa(id), "恢复失败:"+err.Error())
|
||
return
|
||
}
|
||
s.logOp(r, "恢复客户", "customer", id, c.Tail+" "+c.Name)
|
||
s.redirect(w, r, "/customers/"+itoa(id), "已恢复客户 "+c.Tail+" "+c.Name)
|
||
}
|
||
|
||
// handleCustomerDelete 彻底删除客户及其订阅/打卡/追加记录(外键级联)。
|
||
func (s *Server) handleCustomerDelete(w http.ResponseWriter, r *http.Request) {
|
||
id := pathID(r)
|
||
c, err := s.db.Customer(id)
|
||
if err != nil {
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
if r.PostFormValue("confirm") != "delete" {
|
||
s.redirect(w, r, "/customers/"+itoa(id), "请在弹窗里确认删除")
|
||
return
|
||
}
|
||
if err := s.db.DeleteCustomer(id); err != nil {
|
||
s.redirect(w, r, "/customers/"+itoa(id), "删除失败:"+err.Error())
|
||
return
|
||
}
|
||
s.logOp(r, "删除客户", "customer", id, c.Tail+" "+c.Name+"(含订阅与打卡记录)")
|
||
s.redirect(w, r, "/customers", "已彻底删除客户 "+c.Tail+" "+c.Name)
|
||
}
|
||
|
||
// handleCustomerAssignee 从客户详情快速指派/取消打手。
|
||
func (s *Server) handleCustomerAssignee(w http.ResponseWriter, r *http.Request) {
|
||
id := pathID(r)
|
||
userID := int64(s.formInt(r, "assignee_id", 0))
|
||
if err := s.db.SetCustomerAssignee(id, userID); err != nil {
|
||
s.redirect(w, r, "/customers/"+itoa(id), "指派失败:"+err.Error())
|
||
return
|
||
}
|
||
c, _ := s.db.Customer(id)
|
||
word := "已取消指派"
|
||
if userID > 0 {
|
||
if u, err := s.db.UserByID(userID); err == nil {
|
||
name := u.DisplayName
|
||
if name == "" {
|
||
name = u.Username
|
||
}
|
||
word = "已指派给 " + name
|
||
}
|
||
}
|
||
if c != nil {
|
||
s.logOp(r, "指派打手", "customer", id, c.Tail+" "+c.Name+" "+word)
|
||
}
|
||
s.redirect(w, r, "/customers/"+itoa(id), word)
|
||
}
|
||
|
||
func (s *Server) handleSubCreate(w http.ResponseWriter, r *http.Request) {
|
||
customerID := pathID(r)
|
||
c, err := s.db.Customer(customerID)
|
||
if err != nil {
|
||
http.NotFound(w, r)
|
||
return
|
||
}
|
||
in := store.CreateSubInput{
|
||
CustomerID: customerID,
|
||
PackageID: int64(s.formInt(r, "package_id", 0)),
|
||
StartDate: r.PostFormValue("start_date"),
|
||
EndDate: r.PostFormValue("end_date"),
|
||
Days: s.formInt(r, "days", 0),
|
||
TotalCount: s.formInt(r, "total_count", 0),
|
||
Note: strings.TrimSpace(r.PostFormValue("note")),
|
||
}
|
||
u := s.currentUser(r)
|
||
if u != nil {
|
||
in.OperatorID = u.ID
|
||
}
|
||
sub, err := s.db.CreateSub(in)
|
||
if err != nil {
|
||
s.redirect(w, r, "/customers/"+itoa(customerID), "创建订阅失败:"+err.Error())
|
||
return
|
||
}
|
||
s.logOp(r, "新建订阅", "subscription", sub.ID,
|
||
"为 "+c.Tail+" "+c.Name+" 开通 "+sub.PackageName+"("+unitLabel(sub.Unit)+")")
|
||
s.redirect(w, r, "/customers/"+itoa(customerID), "已开通「"+sub.PackageName+"」")
|
||
}
|
||
|
||
func unitLabel(u string) string {
|
||
if u == domain.UnitCount {
|
||
return "按次"
|
||
}
|
||
return "按日"
|
||
}
|
||
|
||
func itoa(v int64) string { return strconv.FormatInt(v, 10) }
|
||
|
||
func sortAdjustments(list []store.Adjustment) {
|
||
sort.SliceStable(list, func(i, j int) bool {
|
||
if list[i].CreatedAt != list[j].CreatedAt {
|
||
return list[i].CreatedAt > list[j].CreatedAt
|
||
}
|
||
return list[i].ID > list[j].ID
|
||
})
|
||
}
|
||
|
||
func sortCheckins(list []CheckinRow) {
|
||
sort.SliceStable(list, func(i, j int) bool {
|
||
if list[i].ServiceDate != list[j].ServiceDate {
|
||
return list[i].ServiceDate > list[j].ServiceDate
|
||
}
|
||
return list[i].ID > list[j].ID
|
||
})
|
||
}
|