201 lines
5.4 KiB
Go
201 lines
5.4 KiB
Go
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} 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 {
|
|
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 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 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 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 := 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} 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 := 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": "企业删除成功",
|
|
})
|
|
}
|