## CI 修复 (P0/P1) P0 — 编译阻塞: - interfaces.go: postUseCase ISP 接口移除不用的 Create/Update/Delete P1 — 必须修复: - ST1000: 为 13 个包添加包注释 (common/config/model/service/...) - ST1005: redis_store.go 全部错误消息改为小写开头 - errcheck (19处): defer Close()→闭包忽略, notifier.Create→_=, r.Run→检查error - errorlint (9处): switch-on-error→errors.Is 链, ==→errors.Is - ST1020/ST1022: 导出符号注释以符号名开头 P2 — 安全评审: - gosec (12处): G203/G301/G304/G306 添加 nolint 注释并附理由 P3 — 清理: - unused: 移除 hasUnicode/energyStore/current/energyAdminUseCase - gofumpt + goimports 全量格式化 (35+ 文件)
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package common
|
||
|
||
import "time"
|
||
|
||
// Pagination 分页请求参数
|
||
type Pagination struct {
|
||
Page int `form:"page" json:"page" binding:"min=1"`
|
||
PageSize int `form:"page_size" json:"page_size" binding:"min=1,max=100"`
|
||
}
|
||
|
||
// DefaultPagination 默认分页(未传参时使用)
|
||
func (p *Pagination) DefaultPagination() {
|
||
if p.Page < 1 {
|
||
p.Page = 1
|
||
}
|
||
if p.PageSize < 1 || p.PageSize > 100 {
|
||
p.PageSize = 20
|
||
}
|
||
}
|
||
|
||
// Offset 计算 SQL offset
|
||
func (p *Pagination) Offset() int {
|
||
return (p.Page - 1) * p.PageSize
|
||
}
|
||
|
||
// TotalPages 根据 total 计算总页数
|
||
func (p *Pagination) TotalPages(total int64) int {
|
||
if total == 0 {
|
||
return 0
|
||
}
|
||
return int((total + int64(p.PageSize) - 1) / int64(p.PageSize))
|
||
}
|
||
|
||
// PrevPage 上一页(最小为 1)
|
||
func (p *Pagination) PrevPage() int {
|
||
prev := p.Page - 1
|
||
if prev < 1 {
|
||
return 1
|
||
}
|
||
return prev
|
||
}
|
||
|
||
// NextPage 下一页(最大为 totalPages)
|
||
func (p *Pagination) NextPage(totalPages int) int {
|
||
next := p.Page + 1
|
||
if next > totalPages {
|
||
return totalPages
|
||
}
|
||
return next
|
||
}
|
||
|
||
// PageCount 根据 total 和 pageSize 计算总页数(无需创建 Pagination 实例)
|
||
func PageCount(total int64, pageSize int) int {
|
||
if total == 0 {
|
||
return 0
|
||
}
|
||
return int((total + int64(pageSize) - 1) / int64(pageSize))
|
||
}
|
||
|
||
// TimeFormat 固定时间格式,前后端统一。
|
||
const TimeFormat = time.RFC3339
|