refactor: remove company management APIs
This commit is contained in:
@@ -1,320 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/services"
|
||||
)
|
||||
|
||||
// CompaniesController 企业管理控制器
|
||||
type CompaniesController struct {
|
||||
companiesService services.CompaniesService
|
||||
}
|
||||
|
||||
// NewCompaniesController 创建企业管理控制器实例
|
||||
func NewCompaniesController() *CompaniesController {
|
||||
return &CompaniesController{
|
||||
companiesService: services.CompaniesService{},
|
||||
}
|
||||
}
|
||||
|
||||
// FindAll 获取企业列表
|
||||
// @Summary 获取企业列表
|
||||
// @Description 获取企业列表,支持分页和搜索
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param page query int false "页码"
|
||||
// @Param limit query int false "每页数量"
|
||||
// @Param search query string false "搜索关键词"
|
||||
// @Success 200 {object} models.PaginationResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies [get]
|
||||
func (c *CompaniesController) FindAll(ctx *gin.Context) {
|
||||
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
||||
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "20"))
|
||||
search := ctx.DefaultQuery("search", "")
|
||||
|
||||
companies, total, totalPages, err := c.companiesService.FindAll(page, limit, search)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]gin.H, 0, len(companies))
|
||||
now := time.Now()
|
||||
for _, company := range companies {
|
||||
serialCount := len(company.Serials)
|
||||
activeCount := 0
|
||||
for _, serial := range company.Serials {
|
||||
if serial.IsActive && (serial.ValidUntil == nil || serial.ValidUntil.After(now)) {
|
||||
activeCount++
|
||||
}
|
||||
}
|
||||
|
||||
items = append(items, gin.H{
|
||||
"companyName": company.CompanyName,
|
||||
"firstCreated": company.CreatedAt,
|
||||
"lastCreated": company.UpdatedAt,
|
||||
"serialCount": serialCount,
|
||||
"activeCount": activeCount,
|
||||
"status": map[bool]string{
|
||||
true: "active",
|
||||
false: "disabled",
|
||||
}[company.IsActive],
|
||||
})
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "获取企业列表成功", gin.H{
|
||||
"data": items,
|
||||
"pagination": gin.H{
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total": total,
|
||||
"totalPages": totalPages,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// FindOne 获取企业详情
|
||||
// @Summary 获取企业详情
|
||||
// @Description 获取指定企业详情(含序列号分页)
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyName path string true "企业名称"
|
||||
// @Param page query int false "页码"
|
||||
// @Param limit query int false "每页数量"
|
||||
// @Success 200 {object} models.DataResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/{companyName} [get]
|
||||
func (c *CompaniesController) FindOne(ctx *gin.Context) {
|
||||
companyName, _ := url.PathUnescape(ctx.Param("companyName"))
|
||||
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
||||
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "20"))
|
||||
|
||||
data, err := c.companiesService.FindOne(companyName, page, limit)
|
||||
if err != nil {
|
||||
if err.Error() == "企业不存在" {
|
||||
ErrorResponse(ctx, http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "获取企业详情成功",
|
||||
"data": data,
|
||||
})
|
||||
}
|
||||
|
||||
// Create 创建企业
|
||||
// @Summary 创建企业
|
||||
// @Description 创建新的企业
|
||||
// @Tags 企业管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyData body models.CompanyDataRequest true "企业数据"
|
||||
// @Success 201 {object} models.CompanyResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 409 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies [post]
|
||||
func (c *CompaniesController) Create(ctx *gin.Context) {
|
||||
var companyData struct {
|
||||
CompanyName string `json:"companyName" validate:"required"`
|
||||
}
|
||||
if !BindJSON(ctx, &companyData) {
|
||||
return
|
||||
}
|
||||
|
||||
company, err := c.companiesService.Create(companyData.CompanyName)
|
||||
if err != nil {
|
||||
if err.Error() == "企业名称已存在" {
|
||||
ErrorResponse(ctx, http.StatusConflict, err.Error())
|
||||
} else {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusCreated, gin.H{
|
||||
"message": "企业创建成功",
|
||||
"company": company,
|
||||
})
|
||||
}
|
||||
|
||||
// Update 更新企业信息
|
||||
// @Summary 更新企业信息
|
||||
// @Description 更新企业信息
|
||||
// @Tags 企业管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyName path string true "企业名称"
|
||||
// @Param companyData body models.CompanyUpdateRequest true "企业数据"
|
||||
// @Success 200 {object} models.CompanyResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 409 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/{companyName} [put]
|
||||
func (c *CompaniesController) Update(ctx *gin.Context) {
|
||||
companyName, _ := url.PathUnescape(ctx.Param("companyName"))
|
||||
|
||||
var companyData struct {
|
||||
CompanyName string `json:"companyName"`
|
||||
NewCompanyName string `json:"newCompanyName"`
|
||||
IsActive *bool `json:"isActive"`
|
||||
}
|
||||
if !BindJSON(ctx, &companyData) {
|
||||
return
|
||||
}
|
||||
|
||||
newName := companyData.NewCompanyName
|
||||
if newName == "" {
|
||||
newName = companyData.CompanyName
|
||||
}
|
||||
|
||||
company, err := c.companiesService.Update(companyName, newName, companyData.IsActive)
|
||||
if err != nil {
|
||||
switch err.Error() {
|
||||
case "企业不存在":
|
||||
ErrorResponse(ctx, http.StatusNotFound, err.Error())
|
||||
case "企业名称已存在":
|
||||
ErrorResponse(ctx, http.StatusConflict, err.Error())
|
||||
default:
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "企业信息更新成功", gin.H{"company": company})
|
||||
}
|
||||
|
||||
// Delete 删除企业
|
||||
// @Summary 删除企业
|
||||
// @Description 删除企业及其关联序列号
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyName path string true "企业名称"
|
||||
// @Success 200 {object} models.BaseResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/{companyName} [delete]
|
||||
func (c *CompaniesController) Delete(ctx *gin.Context) {
|
||||
companyName, _ := url.PathUnescape(ctx.Param("companyName"))
|
||||
|
||||
err := c.companiesService.Delete(companyName)
|
||||
if err != nil {
|
||||
if err.Error() == "企业不存在" {
|
||||
ErrorResponse(ctx, http.StatusNotFound, err.Error())
|
||||
} else {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "企业已完全删除,所有相关序列号已删除")
|
||||
}
|
||||
|
||||
// DeleteSerial 删除企业下指定序列号
|
||||
// @Summary 删除企业序列号
|
||||
// @Description 删除指定企业下的序列号
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyName path string true "企业名称"
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Success 200 {object} models.BaseResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/{companyName}/serials/{serialNumber} [delete]
|
||||
func (c *CompaniesController) DeleteSerial(ctx *gin.Context) {
|
||||
companyName, _ := url.PathUnescape(ctx.Param("companyName"))
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
err := c.companiesService.DeleteSerial(companyName, serialNumber)
|
||||
if err != nil {
|
||||
if err.Error() == "序列号不存在或不属于该企业" {
|
||||
ErrorResponse(ctx, http.StatusNotFound, err.Error())
|
||||
} else {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "序列号已成功删除", gin.H{
|
||||
"serialNumber": serialNumber,
|
||||
"companyName": companyName,
|
||||
})
|
||||
}
|
||||
|
||||
// Revoke 吊销企业
|
||||
// @Summary 吊销企业
|
||||
// @Description 吊销企业及其关联序列号
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyName path string true "企业名称"
|
||||
// @Success 200 {object} models.BaseResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/{companyName}/revoke [post]
|
||||
func (c *CompaniesController) Revoke(ctx *gin.Context) {
|
||||
companyName, _ := url.PathUnescape(ctx.Param("companyName"))
|
||||
|
||||
err := c.companiesService.Revoke(companyName)
|
||||
if err != nil {
|
||||
if err.Error() == "企业不存在" {
|
||||
ErrorResponse(ctx, http.StatusNotFound, err.Error())
|
||||
} else {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "企业已吊销,所有序列号已失效", gin.H{
|
||||
"companyName": companyName,
|
||||
})
|
||||
}
|
||||
|
||||
// StatsOverview 获取企业统计概览
|
||||
// @Summary 获取企业统计概览
|
||||
// @Description 获取企业、序列号统计数据
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {object} models.DataResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/stats/overview [get]
|
||||
func (c *CompaniesController) StatsOverview(ctx *gin.Context) {
|
||||
stats, err := c.companiesService.GetStats()
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "获取统计数据成功",
|
||||
"data": stats,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/services"
|
||||
)
|
||||
|
||||
// DashboardController 控制台统计控制器
|
||||
type DashboardController struct {
|
||||
dashboardService services.DashboardService
|
||||
}
|
||||
|
||||
// NewDashboardController 创建控制台统计控制器实例
|
||||
func NewDashboardController() *DashboardController {
|
||||
return &DashboardController{
|
||||
dashboardService: services.DashboardService{},
|
||||
}
|
||||
}
|
||||
|
||||
// Stats 获取控制台统计
|
||||
func (c *DashboardController) Stats(ctx *gin.Context) {
|
||||
stats, err := c.dashboardService.GetStats()
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "获取统计数据成功", stats)
|
||||
}
|
||||
@@ -1,345 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/models"
|
||||
"git.beifan.cn/trace-system/backend-go/services"
|
||||
)
|
||||
|
||||
// SerialsController 序列号控制器
|
||||
type SerialsController struct {
|
||||
serialsService services.SerialsService
|
||||
}
|
||||
|
||||
// NewSerialsController 创建序列号控制器实例
|
||||
func NewSerialsController() *SerialsController {
|
||||
return &SerialsController{
|
||||
serialsService: services.SerialsService{},
|
||||
}
|
||||
}
|
||||
|
||||
// Generate 生成序列号
|
||||
// @Summary 生成序列号
|
||||
// @Description 生成指定数量的序列号
|
||||
// @Tags 序列号管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param generateData body models.GenerateSerialDTO true "生成数据"
|
||||
// @Success 200 {object} models.DataResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /serials/generate [post]
|
||||
func (c *SerialsController) Generate(ctx *gin.Context) {
|
||||
userModel, ok := GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var generateData models.GenerateSerialDTO
|
||||
if !BindJSON(ctx, &generateData) {
|
||||
return
|
||||
}
|
||||
|
||||
serials, err := c.serialsService.Generate(
|
||||
generateData.CompanyName,
|
||||
generateData.Quantity,
|
||||
generateData.ValidDays,
|
||||
userModel.ID,
|
||||
)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]gin.H, 0, len(serials))
|
||||
for _, serial := range serials {
|
||||
items = append(items, gin.H{
|
||||
"serialNumber": serial.SerialNumber,
|
||||
"companyName": serial.CompanyName,
|
||||
"validUntil": serial.ValidUntil,
|
||||
"isActive": serial.IsActive,
|
||||
"createdAt": serial.CreatedAt,
|
||||
"createdBy": userModel.Name,
|
||||
})
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "成功生成"+strconv.Itoa(len(serials))+"个序列号", gin.H{
|
||||
"serials": items,
|
||||
})
|
||||
}
|
||||
|
||||
// GenerateWithPrefix 带前缀生成序列号
|
||||
// @Summary 带前缀生成序列号
|
||||
// @Description 生成带有指定前缀的序列号
|
||||
// @Tags 序列号管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param generateData body models.GenerateWithPrefixDTO true "生成数据"
|
||||
// @Success 200 {object} models.DataResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /serials/generate-with-prefix [post]
|
||||
func (c *SerialsController) GenerateWithPrefix(ctx *gin.Context) {
|
||||
userModel, ok := GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var generateData models.GenerateWithPrefixDTO
|
||||
if !BindJSON(ctx, &generateData) {
|
||||
return
|
||||
}
|
||||
|
||||
serials, err := c.serialsService.Generate(
|
||||
generateData.CompanyName,
|
||||
generateData.Quantity,
|
||||
generateData.ValidDays,
|
||||
userModel.ID,
|
||||
generateData.SerialPrefix,
|
||||
)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]gin.H, 0, len(serials))
|
||||
for _, serial := range serials {
|
||||
items = append(items, gin.H{
|
||||
"serialNumber": serial.SerialNumber,
|
||||
"companyName": serial.CompanyName,
|
||||
"validUntil": serial.ValidUntil,
|
||||
"isActive": serial.IsActive,
|
||||
"createdAt": serial.CreatedAt,
|
||||
"createdBy": userModel.Name,
|
||||
})
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "成功生成"+strconv.Itoa(len(serials))+"个序列号", gin.H{
|
||||
"serials": items,
|
||||
})
|
||||
}
|
||||
|
||||
// GenerateQRCode 生成二维码
|
||||
// @Summary 生成二维码
|
||||
// @Description 为指定序列号生成查询二维码
|
||||
// @Tags 序列号管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Param qrCodeData body models.QRCodeDTO false "二维码数据"
|
||||
// @Success 200 {object} models.QRCodeResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /serials/{serialNumber}/qrcode [post]
|
||||
func (c *SerialsController) GenerateQRCode(ctx *gin.Context) {
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
var qrCodeData models.QRCodeDTO
|
||||
if !BindJSON(ctx, &qrCodeData) {
|
||||
return
|
||||
}
|
||||
|
||||
protocol := "http"
|
||||
if ctx.Request.TLS != nil {
|
||||
protocol = "https"
|
||||
}
|
||||
|
||||
qrCodeBase64, queryUrl, err := c.serialsService.GenerateQRCode(
|
||||
serialNumber,
|
||||
qrCodeData.BaseUrl,
|
||||
ctx.Request.Host,
|
||||
protocol,
|
||||
)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "二维码生成成功", gin.H{
|
||||
"qrCodeData": qrCodeBase64,
|
||||
"queryUrl": queryUrl,
|
||||
})
|
||||
}
|
||||
|
||||
// Query 查询序列号信息
|
||||
// @Summary 查询序列号信息
|
||||
// @Description 查询指定序列号的详细信息
|
||||
// @Tags 序列号查询
|
||||
// @Produce json
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Success 200 {object} models.DataResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /serials/{serialNumber}/query [get]
|
||||
func (c *SerialsController) Query(ctx *gin.Context) {
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
serial, err := c.serialsService.Query(serialNumber)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
createdBy := ""
|
||||
if serial.User != nil {
|
||||
createdBy = serial.User.Name
|
||||
}
|
||||
|
||||
status := "active"
|
||||
if !serial.IsActive {
|
||||
status = "disabled"
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "查询成功", gin.H{
|
||||
"serial": gin.H{
|
||||
"serialNumber": serial.SerialNumber,
|
||||
"companyName": serial.CompanyName,
|
||||
"validUntil": serial.ValidUntil,
|
||||
"status": status,
|
||||
"isActive": serial.IsActive,
|
||||
"createdAt": serial.CreatedAt,
|
||||
"createdBy": createdBy,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// FindAll 获取序列号列表
|
||||
// @Summary 获取序列号列表
|
||||
// @Description 获取序列号列表,支持分页和搜索
|
||||
// @Tags 序列号管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param page query int false "页码"
|
||||
// @Param limit query int false " "每页数量"
|
||||
// @Param search query string false "搜索关键词"
|
||||
// @Success 200 {object} models.PaginationResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /serials [get]
|
||||
func (c *SerialsController) FindAll(ctx *gin.Context) {
|
||||
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
||||
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "20"))
|
||||
search := ctx.DefaultQuery("search", "")
|
||||
|
||||
serials, total, totalPages, err := c.serialsService.FindAll(page, limit, search)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]gin.H, 0, len(serials))
|
||||
for _, serial := range serials {
|
||||
createdBy := ""
|
||||
if serial.User != nil {
|
||||
createdBy = serial.User.Name
|
||||
}
|
||||
|
||||
items = append(items, gin.H{
|
||||
"serialNumber": serial.SerialNumber,
|
||||
"companyName": serial.CompanyName,
|
||||
"validUntil": serial.ValidUntil,
|
||||
"isActive": serial.IsActive,
|
||||
"createdAt": serial.CreatedAt,
|
||||
"createdBy": createdBy,
|
||||
})
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "获取序列号列表成功", gin.H{
|
||||
"data": items,
|
||||
"pagination": gin.H{
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total": total,
|
||||
"totalPages": totalPages,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Update 更新序列号信息
|
||||
// @Summary 更新序列号信息
|
||||
// @Description 更新指定序列号的信息
|
||||
// @Tags 序列号管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Param updateData body models.UpdateSerialDTO true "更新数据"
|
||||
// @Success 200 {object} models.DataResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /serials/{serialNumber} [put]
|
||||
func (c *SerialsController) Update(ctx *gin.Context) {
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
var updateData models.UpdateSerialDTO
|
||||
if !BindJSON(ctx, &updateData) {
|
||||
return
|
||||
}
|
||||
|
||||
serial, err := c.serialsService.Update(serialNumber, updateData)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
createdBy := ""
|
||||
if serial.User != nil {
|
||||
createdBy = serial.User.Name
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "序列号更新成功", gin.H{
|
||||
"serial": gin.H{
|
||||
"serialNumber": serial.SerialNumber,
|
||||
"companyName": serial.CompanyName,
|
||||
"validUntil": serial.ValidUntil,
|
||||
"isActive": serial.IsActive,
|
||||
"createdAt": serial.CreatedAt,
|
||||
"updatedAt": serial.UpdatedAt,
|
||||
"createdBy": createdBy,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Revoke 吊销序列号
|
||||
// @Summary 吊销序列号
|
||||
// @Description 吊销指定序列号
|
||||
// @Tags 序列号管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Success 200 {object} models.BaseResponse
|
||||
// @Failure 400 {object} models.ErrorResponse
|
||||
// @Failure 401 {object} models.ErrorResponse
|
||||
// @Failure 404 {object} models.ErrorResponse
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /serials/{serialNumber}/revoke [post]
|
||||
func (c *SerialsController) Revoke(ctx *gin.Context) {
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
err := c.serialsService.Revoke(serialNumber)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "序列号已吊销", gin.H{
|
||||
"data": gin.H{
|
||||
"serialNumber": strings.ToUpper(serialNumber),
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user