package controllers import ( "encoding/json" "fmt" "strconv" "server/models" "server/services" beego "github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/client/orm" ) type CustomerController struct { beego.Controller } // List GET /api/crm/customer/list func (c *CustomerController) 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.ListCustomers(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() } // Detail GET /api/crm/customer/detail?id=... func (c *CustomerController) Detail() { id := c.GetString("id") if id == "" { c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"} c.ServeJSON() return } m, err := services.GetCustomer(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() } // Add POST /api/crm/customer/add func (c *CustomerController) Add() { var m models.Customer 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.CreateCustomer(&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() } // 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() } // Delete POST /api/crm/customer/delete body: {id, tenantId} func (c *CustomerController) 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.SoftDeleteCustomer(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() } // 更新客户开票信息 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() }