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 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 { // 确保business_scope字段被正确返回 result := map[string]interface{}{ "id": m.Id, "tenant_id": m.TenantId, "customer_name": m.CustomerName, "customer_type": m.CustomerType, "contact_person": m.ContactPerson, "contact_phone": m.ContactPhone, "contact_email": m.ContactEmail, "customer_level": m.CustomerLevel, "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() } // 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 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{ "customer_name": "CustomerName", "customer_type": "CustomerType", "customer_level": "CustomerLevel", "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.UpdateCustomerFields(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() } // 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() { // Extract data from the request payload 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 } // Extract invoice fields from the 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(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() }