yunzer_go/server/controllers/supplier.go
2026-01-07 08:43:14 +08:00

227 lines
6.8 KiB
Go

package controllers
import (
"encoding/json"
"fmt"
"strconv"
"server/models"
"server/services"
"github.com/beego/beego/v2/client/orm"
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() {
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 {
// 确保business_scope字段被正确返回
result := map[string]interface{}{
"id": m.Id,
"tenant_id": m.TenantId,
"supplier_name": m.SupplierName,
"supplier_type": m.SupplierType,
"contact_person": m.ContactPerson,
"contact_phone": m.ContactPhone,
"contact_email": m.ContactEmail,
"supplier_level": m.SupplierLevel,
"industry": m.Industry,
"address": m.Address,
"register_time": m.RegisterTime,
"expire_time": m.ExpireTime,
"status": m.Status,
"remark": m.Remark,
"business_scope": m.BusinessScope, // 确保包含business_scope字段
"invoice_title": m.InvoiceTitle,
"tax_number": m.TaxNumber,
"bank_name": m.BankName,
"bank_account": m.BankAccount,
"registered_address": m.RegisteredAddress,
"registered_phone": m.RegisteredPhone,
"create_time": m.CreateTime,
"update_time": m.UpdateTime,
}
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok", "data": result}
}
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 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
}
// Extract and validate id
var idStr string
if idVal, ok := payload["id"]; ok {
switch v := idVal.(type) {
case float64:
idStr = strconv.Itoa(int(v))
case int:
idStr = strconv.Itoa(v)
case string:
idStr = v
default:
idStr = fmt.Sprintf("%v", v)
}
}
if idStr == "" {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
c.ServeJSON()
return
}
// Build update params - only include fields that are provided
params := orm.Params{}
fieldMappings := map[string]string{
"supplier_name": "SupplierName",
"supplier_type": "SupplierType",
"supplier_level": "SupplierLevel",
"industry": "Industry",
"contact_person": "ContactPerson",
"contact_phone": "ContactPhone",
"contact_email": "ContactEmail",
"address": "Address",
"register_time": "RegisterTime",
"expire_time": "ExpireTime",
"status": "Status",
"remark": "Remark",
"business_scope": "BusinessScope",
"invoice_title": "InvoiceTitle",
"tax_number": "TaxNumber",
"bank_name": "BankName",
"bank_account": "BankAccount",
"registered_address": "RegisteredAddress",
"registered_phone": "RegisteredPhone",
}
for jsonKey := range fieldMappings {
if value, exists := payload[jsonKey]; exists {
params[jsonKey] = value
}
}
if len(params) == 0 {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "没有要更新的字段"}
c.ServeJSON()
return
}
if err := services.UpdateSupplierFields(idStr, 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()
}
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()
}