Initial commit

This commit is contained in:
2026-02-12 14:31:30 +08:00
commit e01cdc9889
25 changed files with 3227 additions and 0 deletions

View File

@@ -0,0 +1,200 @@
package controllers
import (
"net/http"
"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} gin.H{message: string, data: []models.Company, pagination: gin.H{page: int, limit: int, total: int, totalPages: int}}
// @Failure 401 {object} gin.H{message: string}
// @Failure 500 {object} gin.H{message: string}
// @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 {
ctx.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"message": "获取企业列表成功",
"data": companies,
"pagination": gin.H{
"page": page,
"limit": limit,
"total": total,
"totalPages": totalPages,
},
})
}
// Create 创建企业
// @Summary 创建企业
// @Description 创建新的企业
// @Tags 企业管理
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param companyData body gin.H{companyName: string} true "企业数据"
// @Success 201 {object} gin.H{message: string, company: models.Company}
// @Failure 400 {object} gin.H{message: string}
// @Failure 401 {object} gin.H{message: string}
// @Failure 409 {object} gin.H{message: string}
// @Failure 500 {object} gin.H{message: string}
// @Router /companies [post]
func (c *CompaniesController) Create(ctx *gin.Context) {
var companyData struct {
CompanyName string `json:"companyName" validate:"required"`
}
if err := ctx.ShouldBindJSON(&companyData); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "无效的请求数据",
"error": err.Error(),
})
return
}
company, err := c.companiesService.Create(companyData.CompanyName)
if err != nil {
if err.Error() == "企业名称已存在" {
ctx.JSON(http.StatusConflict, gin.H{
"message": err.Error(),
})
} else {
ctx.JSON(http.StatusInternalServerError, gin.H{
"message": 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 gin.H{companyName?: string, isActive?: bool} true "企业数据"
// @Success 200 {object} gin.H{message: string, company: models.Company}
// @Failure 400 {object} gin.H{message: string}
// @Failure 401 {object} gin.H{message: string}
// @Failure 404 {object} gin.H{message: string}
// @Failure 409 {object} gin.H{message: string}
// @Failure 500 {object} gin.H{message: string}
// @Router /companies/{companyName} [put]
func (c *CompaniesController) Update(ctx *gin.Context) {
companyName := ctx.Param("companyName")
var companyData struct {
CompanyName string `json:"companyName"`
IsActive bool `json:"isActive"`
}
if err := ctx.ShouldBindJSON(&companyData); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "无效的请求数据",
"error": err.Error(),
})
return
}
company, err := c.companiesService.Update(companyName, companyData.CompanyName, companyData.IsActive)
if err != nil {
if err.Error() == "企业不存在" {
ctx.JSON(http.StatusNotFound, gin.H{
"message": err.Error(),
})
} else if err.Error() == "企业名称已存在" {
ctx.JSON(http.StatusConflict, gin.H{
"message": err.Error(),
})
} else {
ctx.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
}
return
}
ctx.JSON(http.StatusOK, gin.H{
"message": "企业信息更新成功",
"company": company,
})
}
// Delete 删除企业
// @Summary 删除企业
// @Description 删除企业
// @Tags 企业管理
// @Produce json
// @Security BearerAuth
// @Param companyName path string true "企业名称"
// @Success 200 {object} gin.H{message: string}
// @Failure 400 {object} gin.H{message: string}
// @Failure 401 {object} gin.H{message: string}
// @Failure 404 {object} gin.H{message: string}
// @Failure 500 {object} gin.H{message: string}
// @Router /companies/{companyName} [delete]
func (c *CompaniesController) Delete(ctx *gin.Context) {
companyName := ctx.Param("companyName")
err := c.companiesService.Delete(companyName)
if err != nil {
if err.Error() == "企业不存在" {
ctx.JSON(http.StatusNotFound, gin.H{
"message": err.Error(),
})
} else if err.Error() == "企业下还有序列号,无法删除" {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": err.Error(),
})
} else {
ctx.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
}
return
}
ctx.JSON(http.StatusOK, gin.H{
"message": "企业删除成功",
})
}