303 lines
9.1 KiB
Go
303 lines
9.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"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))
|
|
for _, company := range companies {
|
|
items = append(items, gin.H{
|
|
"companyName": company.CompanyName,
|
|
"firstCreated": company.CreatedAt,
|
|
"lastCreated": company.UpdatedAt,
|
|
"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
|
|
}
|
|
|
|
SuccessResponse(ctx, "获取企业详情成功", 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
|
|
}
|
|
|
|
SuccessResponse(ctx, "获取统计数据成功", stats)
|
|
}
|