Files
ai-agent/internal/controllers/dashboard/customer_controller.go
T

154 lines
4.0 KiB
Go
Raw Normal View History

package dashboard
2026-04-09 10:01:23 +08:00
import (
"cs-agent/internal/builders"
"cs-agent/internal/pkg/constants"
"cs-agent/internal/pkg/dto"
"cs-agent/internal/pkg/dto/request"
"cs-agent/internal/pkg/enums"
"cs-agent/internal/pkg/httpx"
2026-04-09 10:01:23 +08:00
"cs-agent/internal/services"
"cs-agent/internal/pkg/httpx/params"
"github.com/gin-gonic/gin"
2026-04-09 10:01:23 +08:00
"github.com/mlogclub/simple/web"
)
func CustomerPostList(ctx *gin.Context) {
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerView); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
var req request.CustomerListRequest
if err := params.ReadJSON(ctx, &req); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
list, paging := services.CustomerService.ListCustomers(req)
httpx.WriteJSON(ctx, &web.PageResult{Results: builders.BuildCustomerList(list), Page: paging})
return
2026-04-09 10:01:23 +08:00
}
func CustomerGetBy(ctx *gin.Context, id int64) {
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerView); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
item := services.CustomerService.Get(id)
if item == nil || item.Status == enums.StatusDeleted {
httpx.WriteJSON(ctx, nil)
return
2026-04-09 10:01:23 +08:00
}
ret := builders.BuildCustomer(item)
httpx.WriteJSON(ctx, &ret)
return
2026-04-09 10:01:23 +08:00
}
// PostSave_profile POST /save_profile — 客户主信息与联系方式在同一事务中保存。
func CustomerPostSave_profile(ctx *gin.Context) {
2026-04-09 10:01:23 +08:00
req := request.SaveCustomerProfileRequest{}
if err := params.ReadJSON(ctx, &req); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
createMode := req.ID == nil || *req.ID <= 0
var user *dto.AuthPrincipal
var err error
if createMode {
user, err = services.AuthService.RequirePermission(ctx, constants.PermissionCustomerCreate)
2026-04-09 10:01:23 +08:00
} else {
user, err = services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate)
2026-04-09 10:01:23 +08:00
}
if err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
item, err := services.CustomerService.SaveCustomerProfile(req, user)
if err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
ret := builders.BuildCustomer(item)
httpx.WriteJSON(ctx, &ret)
return
2026-04-09 10:01:23 +08:00
}
func CustomerPostCreate(ctx *gin.Context) {
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerCreate)
2026-04-09 10:01:23 +08:00
if err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
req := request.CreateCustomerRequest{}
if err := params.ReadJSON(ctx, &req); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
item, err := services.CustomerService.CreateCustomer(req, user)
if err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
ret := builders.BuildCustomer(item)
httpx.WriteJSON(ctx, &ret)
return
2026-04-09 10:01:23 +08:00
}
func CustomerPostUpdate(ctx *gin.Context) {
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate)
2026-04-09 10:01:23 +08:00
if err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
req := request.UpdateCustomerRequest{}
if err := params.ReadJSON(ctx, &req); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
if err := services.CustomerService.UpdateCustomer(req, user); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
httpx.WriteJSON(ctx, nil)
return
2026-04-09 10:01:23 +08:00
}
func CustomerPostDelete(ctx *gin.Context) {
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerDelete)
2026-04-09 10:01:23 +08:00
if err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
req := request.DeleteCustomerRequest{}
if err := params.ReadJSON(ctx, &req); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
if err := services.CustomerService.DeleteCustomer(req.ID, *user); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
httpx.WriteJSON(ctx, nil)
return
2026-04-09 10:01:23 +08:00
}
func CustomerPostUpdate_status(ctx *gin.Context) {
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate)
2026-04-09 10:01:23 +08:00
if err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
req := request.UpdateCustomerStatusRequest{}
if err := params.ReadJSON(ctx, &req); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
if err := services.CustomerService.UpdateStatus(req.ID, req.Status, user); err != nil {
httpx.WriteJSON(ctx, err)
return
2026-04-09 10:01:23 +08:00
}
httpx.WriteJSON(ctx, nil)
return
2026-04-09 10:01:23 +08:00
}