yunzer_go/server/controllers/supplier.go
2025-11-13 17:24:59 +08:00

111 lines
3.1 KiB
Go

package controllers
import (
"encoding/json"
"strconv"
"server/models"
"server/services"
beego "github.com/beego/beego/v2/server/web"
)
type SupplierController struct {
beego.Controller
}
func (c *SupplierController) List() {
tenantId := c.GetString("tenantId")
keyword := c.GetString("keyword")
status := c.GetString("status")
page, _ := strconv.Atoi(c.GetString("page"))
pageSize, _ := strconv.Atoi(c.GetString("pageSize"))
list, total, err := services.ListSuppliers(tenantId, keyword, status, page, pageSize)
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
} else {
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok", "data": map[string]interface{}{"list": list, "total": total}}
}
c.ServeJSON()
}
func (c *SupplierController) Detail() {
id := c.GetString("id")
if id == "" {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
c.ServeJSON()
return
}
m, err := services.GetSupplier(id)
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
} else {
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok", "data": m}
}
c.ServeJSON()
}
func (c *SupplierController) Add() {
var m models.Supplier
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &m); err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误"}
c.ServeJSON()
return
}
if err := services.CreateSupplier(&m); err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
} else {
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok", "data": m}
}
c.ServeJSON()
}
func (c *SupplierController) Edit() {
var body map[string]interface{}
_ = json.Unmarshal(c.Ctx.Input.RequestBody, &body)
id, _ := body["id"].(string)
var m models.Supplier
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &m); err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误"}
c.ServeJSON()
return
}
if m.Id == "" {
if id == "" {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
c.ServeJSON()
return
}
m.Id = id
}
if err := services.UpdateSupplier(&m); err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
} else {
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok"}
}
c.ServeJSON()
}
func (c *SupplierController) Delete() {
var body struct {
Id string `json:"id"`
TenantId string `json:"tenantId"`
}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &body); err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误"}
c.ServeJSON()
return
}
if body.Id == "" {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
c.ServeJSON()
return
}
if err := services.SoftDeleteSupplier(body.Id); err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
} else {
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok"}
}
c.ServeJSON()
}