161 lines
4.7 KiB
Go
161 lines
4.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
|
|
"server/models"
|
|
"server/services"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
"github.com/beego/beego/v2/client/orm"
|
|
)
|
|
|
|
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() {
|
|
idStr := c.GetString("id")
|
|
if idStr == "" {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
id, errConv := strconv.ParseInt(idStr, 10, 64)
|
|
if errConv != nil {
|
|
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)
|
|
var idInt int64
|
|
if v, ok := body["id"].(string); ok && v != "" {
|
|
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
|
|
idInt = n
|
|
}
|
|
} else if v2, ok2 := body["id"].(float64); ok2 {
|
|
idInt = int64(v2)
|
|
}
|
|
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 == 0 {
|
|
if idInt == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
m.Id = idInt
|
|
}
|
|
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
|
|
}
|
|
id, errConv := strconv.ParseInt(body.Id, 10, 64)
|
|
if errConv != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id格式不正确"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
if err := services.SoftDeleteSupplier(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()
|
|
}
|
|
|
|
// 更新供应商开票信息
|
|
func (c *SupplierController) UpdateInvoice() {
|
|
var payload map[string]interface{}
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &payload); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
idVal, ok := payload["id"]
|
|
if !ok || idVal == "" {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
// Prepare params for service: include id and invoice fields
|
|
params := orm.Params{}
|
|
params["id"] = idVal
|
|
for _, key := range []string{"invoice_title", "tax_number", "bank_name", "bank_account", "registered_address", "registered_phone"} {
|
|
if v, exists := payload[key]; exists {
|
|
params[key] = v
|
|
}
|
|
}
|
|
if err := services.UpdateSupplierInvoice(params); 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()
|
|
}
|