完善供应商和客户功能
This commit is contained in:
@@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"server/models"
|
||||
@@ -35,9 +36,25 @@ func (c *ContactController) List() {
|
||||
|
||||
// Add POST /api/crm/contact/add
|
||||
func (c *ContactController) Add() {
|
||||
// Debug: print the raw request body
|
||||
body := string(c.Ctx.Input.RequestBody)
|
||||
fmt.Printf("Received request body: %s\n", body)
|
||||
|
||||
// Try unmarshaling to a map first to see the raw data
|
||||
var rawData map[string]interface{}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &rawData); err != nil {
|
||||
fmt.Printf("JSON unmarshal error (raw): %v\n", err)
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误", "debug": body}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
fmt.Printf("Raw data: %+v\n", rawData)
|
||||
|
||||
// Now try unmarshaling to the struct
|
||||
var m models.Contact
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &m); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误"}
|
||||
fmt.Printf("JSON unmarshal error (struct): %v\n", err)
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误", "debug": body, "error": err.Error()}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"server/services"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
type CustomerController struct {
|
||||
@@ -67,67 +68,67 @@ func (c *CustomerController) Add() {
|
||||
|
||||
// Edit POST /api/crm/customer/edit body: {id, ...}
|
||||
func (c *CustomerController) Edit() {
|
||||
var body map[string]interface{}
|
||||
_ = json.Unmarshal(c.Ctx.Input.RequestBody, &body)
|
||||
id, _ := body["id"].(string)
|
||||
if id == "" {
|
||||
// 也允许前端直接在JSON中传 id 字段,下面会再从结构体取
|
||||
}
|
||||
var m models.Customer
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &m); err != nil {
|
||||
// 回退:从通用map中提取已知字段,避免类型不匹配导致失败
|
||||
if body == nil {
|
||||
_ = json.Unmarshal(c.Ctx.Input.RequestBody, &body)
|
||||
}
|
||||
toStr := func(v interface{}) string {
|
||||
switch t := v.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return t
|
||||
case json.Number:
|
||||
return t.String()
|
||||
default:
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
}
|
||||
m = models.Customer{
|
||||
Id: toStr(body["id"]),
|
||||
TenantId: toStr(body["tenant_id"]),
|
||||
CustomerName: toStr(body["customer_name"]),
|
||||
CustomerType: toStr(body["customer_type"]),
|
||||
ContactPerson: toStr(body["contact_person"]),
|
||||
ContactPhone: toStr(body["contact_phone"]),
|
||||
ContactEmail: toStr(body["contact_email"]),
|
||||
CustomerLevel: toStr(body["customer_level"]),
|
||||
Industry: toStr(body["industry"]),
|
||||
Address: toStr(body["address"]),
|
||||
Status: toStr(body["status"]),
|
||||
Remark: toStr(body["remark"]),
|
||||
}
|
||||
if m.Id == "" {
|
||||
if id == "" {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
m.Id = id
|
||||
}
|
||||
}
|
||||
if m.Id == "" {
|
||||
if id == "" {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
m.Id = id
|
||||
}
|
||||
if err := services.UpdateCustomer(&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()
|
||||
var body map[string]interface{}
|
||||
_ = json.Unmarshal(c.Ctx.Input.RequestBody, &body)
|
||||
id, _ := body["id"].(string)
|
||||
if id == "" {
|
||||
// 也允许前端直接在JSON中传 id 字段,下面会再从结构体取
|
||||
}
|
||||
var m models.Customer
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &m); err != nil {
|
||||
// 回退:从通用map中提取已知字段,避免类型不匹配导致失败
|
||||
if body == nil {
|
||||
_ = json.Unmarshal(c.Ctx.Input.RequestBody, &body)
|
||||
}
|
||||
toStr := func(v interface{}) string {
|
||||
switch t := v.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return t
|
||||
case json.Number:
|
||||
return t.String()
|
||||
default:
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
}
|
||||
m = models.Customer{
|
||||
Id: toStr(body["id"]),
|
||||
TenantId: toStr(body["tenant_id"]),
|
||||
CustomerName: toStr(body["customer_name"]),
|
||||
CustomerType: toStr(body["customer_type"]),
|
||||
ContactPerson: toStr(body["contact_person"]),
|
||||
ContactPhone: toStr(body["contact_phone"]),
|
||||
ContactEmail: toStr(body["contact_email"]),
|
||||
CustomerLevel: toStr(body["customer_level"]),
|
||||
Industry: toStr(body["industry"]),
|
||||
Address: toStr(body["address"]),
|
||||
Status: toStr(body["status"]),
|
||||
Remark: toStr(body["remark"]),
|
||||
}
|
||||
if m.Id == "" {
|
||||
if id == "" {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
m.Id = id
|
||||
}
|
||||
}
|
||||
if m.Id == "" {
|
||||
if id == "" {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
m.Id = id
|
||||
}
|
||||
if err := services.UpdateCustomer(&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()
|
||||
}
|
||||
|
||||
// Delete POST /api/crm/customer/delete body: {id, tenantId}
|
||||
@@ -153,3 +154,36 @@ func (c *CustomerController) Delete() {
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// 更新客户开票信息
|
||||
func (c *CustomerController) UpdateInvoice() {
|
||||
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
|
||||
}
|
||||
// Extract invoice fields from the request payload
|
||||
var payload map[string]interface{}
|
||||
_ = json.Unmarshal(c.Ctx.Input.RequestBody, &payload)
|
||||
params := orm.Params{}
|
||||
for _, key := range []string{"invoice_title", "tax_number", "bank_name", "bank_account", "registered_address", "registered_phone"} {
|
||||
if v, ok := payload[key]; ok {
|
||||
params[key] = v
|
||||
}
|
||||
}
|
||||
if err := services.UpdateInvoice(body.Id, 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()
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"server/services"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
type SupplierController struct {
|
||||
@@ -30,12 +31,18 @@ func (c *SupplierController) List() {
|
||||
}
|
||||
|
||||
func (c *SupplierController) Detail() {
|
||||
id := c.GetString("id")
|
||||
if id == "" {
|
||||
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()}
|
||||
@@ -63,20 +70,27 @@ func (c *SupplierController) Add() {
|
||||
func (c *SupplierController) Edit() {
|
||||
var body map[string]interface{}
|
||||
_ = json.Unmarshal(c.Ctx.Input.RequestBody, &body)
|
||||
id, _ := body["id"].(string)
|
||||
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 == "" {
|
||||
if id == "" {
|
||||
if m.Id == 0 {
|
||||
if idInt == 0 {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
m.Id = id
|
||||
m.Id = idInt
|
||||
}
|
||||
if err := services.UpdateSupplier(&m); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
|
||||
@@ -101,7 +115,43 @@ func (c *SupplierController) Delete() {
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
if err := services.SoftDeleteSupplier(body.Id); err != nil {
|
||||
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"}
|
||||
|
||||
Reference in New Issue
Block a user