This commit is contained in:
2026-01-07 08:43:14 +08:00
parent 09cbd721a0
commit e839da9398
21 changed files with 1953 additions and 159 deletions
+359
View File
@@ -449,3 +449,362 @@ func (c *ArticlesController) UpdateArticleStatus() {
}
c.ServeJSON()
}
// ListCategories 获取分类列表
func (c *ArticlesController) ListCategories() {
// 获取查询参数
tenantId, _ := c.GetInt("tenantId")
page, _ := c.GetInt("page", 1)
pageSize, _ := c.GetInt("pageSize", 20)
if pageSizeStr := c.GetString("size"); pageSizeStr != "" {
if size, err := strconv.Atoi(pageSizeStr); err == nil && size > 0 {
pageSize = size
}
}
keyword := c.GetString("keyword")
statusStr := c.GetString("status")
// 状态过滤:默认为启用状态(1),只有明确传递status参数时才按指定状态过滤
var status *int8
if statusStr != "" {
// 如果明确传递了status参数,按指定值过滤
if s, err := strconv.Atoi(statusStr); err == nil {
statusVal := int8(s)
status = &statusVal
}
} else {
// 如果没有传递status参数,默认只显示启用状态
statusVal := int8(1)
status = &statusVal
}
// 调用服务层获取分类列表
categories, total, err := services.GetArticleCategories(tenantId, page, pageSize, keyword, status)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "获取分类列表失败: " + err.Error(),
"data": nil,
}
c.ServeJSON()
return
}
// 格式化返回数据
categoryList := make([]map[string]interface{}, 0)
for _, category := range categories {
categoryList = append(categoryList, map[string]interface{}{
"id": category.Id,
"label": category.DictLabel,
"value": category.DictValue,
"status": category.Status,
"sort": category.Sort,
"color": category.Color,
"icon": category.Icon,
"remark": category.Remark,
"create_time": category.CreateTime,
"update_time": category.UpdateTime,
})
}
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "ok",
"data": map[string]interface{}{
"list": categoryList,
"total": total,
},
}
c.ServeJSON()
}
// GetCategory 获取分类详情
func (c *ArticlesController) GetCategory() {
// 从URL获取分类ID
categoryId, err := c.GetInt(":id")
if err != nil || categoryId <= 0 {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "无效的分类ID",
"data": nil,
}
c.ServeJSON()
return
}
// 调用服务层获取分类详情
category, err := services.GetArticleCategoryById(categoryId)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "获取分类详情失败: " + err.Error(),
"data": nil,
}
c.ServeJSON()
return
}
if category == nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "分类不存在",
"data": nil,
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "获取分类详情成功",
"data": map[string]interface{}{
"id": category.Id,
"label": category.DictLabel,
"value": category.DictValue,
"status": category.Status,
"sort": category.Sort,
"color": category.Color,
"icon": category.Icon,
"remark": category.Remark,
"create_time": category.CreateTime,
"update_time": category.UpdateTime,
},
}
c.ServeJSON()
}
// CreateCategory 创建分类
func (c *ArticlesController) CreateCategory() {
// 定义接收分类数据的结构体
var categoryData struct {
Label string `json:"label"`
Value string `json:"value"`
Status int8 `json:"status"`
Sort int `json:"sort"`
Color string `json:"color"`
Icon string `json:"icon"`
Remark string `json:"remark"`
}
// 解析请求体JSON数据
err := json.Unmarshal(c.Ctx.Input.RequestBody, &categoryData)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "请求参数格式错误: " + err.Error(),
"data": nil,
}
c.ServeJSON()
return
}
// 校验必要参数
if categoryData.Label == "" {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "分类名称不能为空",
"data": nil,
}
c.ServeJSON()
return
}
if categoryData.Value == "" {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "分类值不能为空",
"data": nil,
}
c.ServeJSON()
return
}
// 从URL参数或JWT获取租户ID,优先使用URL参数
tenantId, _ := c.GetInt("tenant_id", 0)
if tenantId == 0 {
tenantId, _ = c.GetInt("tenantId", 0)
}
// 从JWT上下文中获取用户ID
userId, _ := c.GetInt("userId", 0)
// 调用服务层创建分类
itemId, err := services.CreateArticleCategory(tenantId, userId, categoryData.Label, categoryData.Value, categoryData.Status, categoryData.Sort, categoryData.Color, categoryData.Icon, categoryData.Remark)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "创建分类失败: " + err.Error(),
"data": nil,
}
} else {
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "创建分类成功",
"data": map[string]interface{}{
"id": itemId,
},
}
}
c.ServeJSON()
}
// UpdateCategory 更新分类
func (c *ArticlesController) UpdateCategory() {
// 从URL获取分类ID
categoryId, err := c.GetInt(":id")
if err != nil || categoryId <= 0 {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "无效的分类ID",
"data": nil,
}
c.ServeJSON()
return
}
// 定义接收更新数据的结构体
var categoryData struct {
Label string `json:"label"`
Value string `json:"value"`
Status int8 `json:"status"`
Sort int `json:"sort"`
Color string `json:"color"`
Icon string `json:"icon"`
Remark string `json:"remark"`
}
// 解析请求体JSON数据
err = json.Unmarshal(c.Ctx.Input.RequestBody, &categoryData)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "请求参数格式错误: " + err.Error(),
"data": nil,
}
c.ServeJSON()
return
}
// 校验必要参数
if categoryData.Label == "" {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "分类名称不能为空",
"data": nil,
}
c.ServeJSON()
return
}
if categoryData.Value == "" {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "分类值不能为空",
"data": nil,
}
c.ServeJSON()
return
}
// 从JWT上下文中获取用户ID
userId, _ := c.GetInt("userId", 0)
// 调用服务层更新分类
err = services.UpdateArticleCategory(categoryId, userId, categoryData.Label, categoryData.Value, categoryData.Status, categoryData.Sort, categoryData.Color, categoryData.Icon, categoryData.Remark)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "更新分类失败: " + err.Error(),
"data": nil,
}
} else {
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "更新分类成功",
"data": nil,
}
}
c.ServeJSON()
}
// DeleteCategory 删除分类
func (c *ArticlesController) DeleteCategory() {
// 从URL获取分类ID
categoryId, err := c.GetInt(":id")
if err != nil || categoryId <= 0 {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "无效的分类ID",
"data": nil,
}
c.ServeJSON()
return
}
// 调用服务层删除分类
err = services.DeleteArticleCategory(categoryId)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "删除分类失败: " + err.Error(),
"data": nil,
}
} else {
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "删除分类成功",
"data": nil,
}
}
c.ServeJSON()
}
// UpdateCategoryStatus 更新分类状态
func (c *ArticlesController) UpdateCategoryStatus() {
// 从URL获取分类ID
categoryId, err := c.GetInt(":id")
if err != nil || categoryId <= 0 {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "无效的分类ID",
"data": nil,
}
c.ServeJSON()
return
}
// 定义接收状态数据的结构体
var statusData struct {
Status int8 `json:"status"`
}
// 解析请求体JSON数据
err = json.Unmarshal(c.Ctx.Input.RequestBody, &statusData)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "请求参数格式错误: " + err.Error(),
"data": nil,
}
c.ServeJSON()
return
}
// 调用服务层更新分类状态
err = services.UpdateArticleCategoryStatus(categoryId, statusData.Status)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "更新分类状态失败: " + err.Error(),
"data": nil,
}
} else {
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "更新分类状态成功",
"data": nil,
}
}
c.ServeJSON()
}
+110 -64
View File
@@ -8,8 +8,8 @@ import (
"server/models"
"server/services"
beego "github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
)
type CustomerController struct {
@@ -45,7 +45,33 @@ func (c *CustomerController) Detail() {
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}
// 确保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()
}
@@ -68,62 +94,70 @@ 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 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
}
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
// 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 m.Id == "" {
if id == "" {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
c.ServeJSON()
return
}
m.Id = id
if idStr == "" {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
c.ServeJSON()
return
}
if err := services.UpdateCustomer(&m); err != nil {
// 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"}
@@ -157,30 +191,42 @@ func (c *CustomerController) Delete() {
// 更新客户开票信息
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 {
// 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
}
if body.Id == "" {
// 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 request payload
var payload map[string]interface{}
_ = json.Unmarshal(c.Ctx.Input.RequestBody, &payload)
// 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(body.Id, params); err != nil {
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"}
+87 -21
View File
@@ -2,13 +2,14 @@ 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"
beego "github.com/beego/beego/v2/server/web"
)
type SupplierController struct {
@@ -47,7 +48,33 @@ func (c *SupplierController) Detail() {
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}
// 确保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()
}
@@ -68,31 +95,70 @@ func (c *SupplierController) Add() {
}
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 {
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
}
if m.Id == 0 {
if idInt == 0 {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
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)
}
m.Id = idInt
}
if err := services.UpdateSupplier(&m); err != nil {
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"}