143 lines
4.9 KiB
Go
143 lines
4.9 KiB
Go
package customer
|
||
|
||
import (
|
||
"backend/internal/dto"
|
||
customerservice "backend/internal/service/customer"
|
||
"backend/utils"
|
||
"fmt"
|
||
"net/http"
|
||
"strconv"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// CustomerController 客户控制器 // Logan.Li
|
||
type CustomerController struct {
|
||
svc *customerservice.CustomerService
|
||
db *gorm.DB
|
||
}
|
||
|
||
// NewCustomerController 创建控制器实例 // Logan.Li
|
||
func NewCustomerController(db *gorm.DB) *CustomerController {
|
||
return &CustomerController{svc: customerservice.NewCustomerService(db), db: db}
|
||
}
|
||
|
||
// List 客户列表(支持 product_id 筛选通知群体) // Logan.Li
|
||
func (ctrl *CustomerController) List(c *gin.Context) {
|
||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
||
keyword := c.Query("keyword")
|
||
productID, _ := strconv.ParseUint(c.Query("product_id"), 10, 32)
|
||
|
||
customers, total, err := ctrl.svc.List(page, pageSize, keyword, uint(productID))
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
|
||
"list": customers, "total": total, "page": page, "page_size": pageSize,
|
||
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
|
||
}})
|
||
}
|
||
|
||
// GetAll 全部客户(可选 product_id) // Logan.Li
|
||
func (ctrl *CustomerController) GetAll(c *gin.Context) {
|
||
productID, _ := strconv.ParseUint(c.Query("product_id"), 10, 32)
|
||
customers, err := ctrl.svc.GetAll(uint(productID))
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "data": customers})
|
||
}
|
||
|
||
// Get 客户详情 // Logan.Li
|
||
func (ctrl *CustomerController) Get(c *gin.Context) {
|
||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||
customer, err := ctrl.svc.GetByID(uint(id))
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{"code": 404, "message": err.Error()})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "data": customer})
|
||
}
|
||
|
||
// Create 创建客户 // Logan.Li
|
||
func (ctrl *CustomerController) Create(c *gin.Context) {
|
||
var req dto.CustomerCreate
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
|
||
return
|
||
}
|
||
customer, err := ctrl.svc.Create(req)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||
return
|
||
}
|
||
utils.LogAction(ctrl.db, c, "create", "customer", customer.ID, customer.Name, "", "", "创建客户")
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建成功", "data": customer})
|
||
}
|
||
|
||
// Update 更新客户 // Logan.Li
|
||
func (ctrl *CustomerController) Update(c *gin.Context) {
|
||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||
var req dto.CustomerUpdate
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
|
||
return
|
||
}
|
||
customer, err := ctrl.svc.Update(uint(id), req)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||
return
|
||
}
|
||
utils.LogAction(ctrl.db, c, "update", "customer", customer.ID, customer.Name, "", "", "更新客户")
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功", "data": customer})
|
||
}
|
||
|
||
// Delete 删除客户 // Logan.Li
|
||
func (ctrl *CustomerController) Delete(c *gin.Context) {
|
||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||
if err := ctrl.svc.Delete(uint(id)); err != nil {
|
||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||
return
|
||
}
|
||
utils.LogAction(ctrl.db, c, "delete", "customer", uint(id), "", "", "", "删除客户")
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
|
||
}
|
||
|
||
// UpdateStatus 切换状态 // Logan.Li
|
||
func (ctrl *CustomerController) UpdateStatus(c *gin.Context) {
|
||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||
var req struct {
|
||
Status int `json:"status"`
|
||
}
|
||
c.ShouldBindJSON(&req)
|
||
if err := ctrl.svc.UpdateStatus(uint(id), req.Status); err != nil {
|
||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||
return
|
||
}
|
||
utils.LogAction(ctrl.db, c, "update_status", "customer", uint(id), "", "", "", "切换客户状态")
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "状态更新成功"})
|
||
}
|
||
|
||
// BatchDelete 批量删除客户(名下有 License 的自动跳过) // Logan.Li
|
||
func (ctrl *CustomerController) BatchDelete(c *gin.Context) {
|
||
var req struct {
|
||
IDs []uint `json:"ids" binding:"required"`
|
||
}
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
|
||
return
|
||
}
|
||
result, err := ctrl.svc.BatchDelete(req.IDs)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
||
return
|
||
}
|
||
utils.LogAction(ctrl.db, c, "batch_delete", "customer", 0, "", "", "",
|
||
fmt.Sprintf("批量删除客户 %d 条,跳过 %d 条", result.Deleted, result.Skipped))
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "批量删除完成", "data": result})
|
||
}
|