Re-migrate code
This commit is contained in:
@@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -41,15 +42,25 @@ func (c *CompaniesController) FindAll(ctx *gin.Context) {
|
||||
|
||||
companies, total, totalPages, err := c.companiesService.FindAll(page, limit, search)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "获取企业列表成功",
|
||||
"data": companies,
|
||||
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,
|
||||
@@ -59,6 +70,38 @@ func (c *CompaniesController) FindAll(ctx *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// 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 创建新的企业
|
||||
@@ -77,24 +120,16 @@ 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(),
|
||||
})
|
||||
if !BindJSON(ctx, &companyData) {
|
||||
return
|
||||
}
|
||||
|
||||
company, err := c.companiesService.Create(companyData.CompanyName)
|
||||
if err != nil {
|
||||
if err.Error() == "企业名称已存在" {
|
||||
ctx.JSON(http.StatusConflict, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
ErrorResponse(ctx, http.StatusConflict, err.Error())
|
||||
} else {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -122,47 +157,41 @@ func (c *CompaniesController) Create(ctx *gin.Context) {
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/{companyName} [put]
|
||||
func (c *CompaniesController) Update(ctx *gin.Context) {
|
||||
companyName := ctx.Param("companyName")
|
||||
companyName, _ := url.PathUnescape(ctx.Param("companyName"))
|
||||
|
||||
var companyData struct {
|
||||
CompanyName string `json:"companyName"`
|
||||
IsActive bool `json:"isActive"`
|
||||
CompanyName string `json:"companyName"`
|
||||
NewCompanyName string `json:"newCompanyName"`
|
||||
IsActive *bool `json:"isActive"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&companyData); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "无效的请求数据",
|
||||
"error": err.Error(),
|
||||
})
|
||||
if !BindJSON(ctx, &companyData) {
|
||||
return
|
||||
}
|
||||
|
||||
company, err := c.companiesService.Update(companyName, companyData.CompanyName, companyData.IsActive)
|
||||
newName := companyData.NewCompanyName
|
||||
if newName == "" {
|
||||
newName = companyData.CompanyName
|
||||
}
|
||||
|
||||
company, err := c.companiesService.Update(companyName, newName, 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(),
|
||||
})
|
||||
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
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "企业信息更新成功",
|
||||
"company": company,
|
||||
})
|
||||
SuccessResponse(ctx, "企业信息更新成功", gin.H{"company": company})
|
||||
}
|
||||
|
||||
// Delete 删除企业
|
||||
// @Summary 删除企业
|
||||
// @Description 删除企业
|
||||
// @Description 删除企业及其关联序列号
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
@@ -174,34 +203,87 @@ func (c *CompaniesController) Update(ctx *gin.Context) {
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/{companyName} [delete]
|
||||
func (c *CompaniesController) Delete(ctx *gin.Context) {
|
||||
companyName := ctx.Param("companyName")
|
||||
companyName, _ := url.PathUnescape(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(),
|
||||
})
|
||||
ErrorResponse(ctx, http.StatusNotFound, err.Error())
|
||||
} else {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "企业删除成功",
|
||||
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 获取企业、企业赋码、员工赋码的统计数据
|
||||
// @Description 获取企业、序列号统计数据
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
@@ -210,13 +292,11 @@ func (c *CompaniesController) Delete(ctx *gin.Context) {
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
// @Router /companies/stats/overview [get]
|
||||
func (c *CompaniesController) StatsOverview(ctx *gin.Context) {
|
||||
stats, err := c.companiesService.GetStatsOverview()
|
||||
stats, err := c.companiesService.GetStats()
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "获取企业统计概览成功", gin.H{
|
||||
"overview": stats,
|
||||
})
|
||||
SuccessResponse(ctx, "获取统计数据成功", stats)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user