package solutionmanagement import ( "context" "fmt" "git.x2erp.com/qdy/go-base/ctx" "git.x2erp.com/qdy/go-base/logger" "git.x2erp.com/qdy/go-base/model/request/queryreq" "git.x2erp.com/qdy/go-base/model/response" "git.x2erp.com/qdy/go-base/util" "git.x2erp.com/qdy/go-db/factory/database" "git.x2erp.com/qdy/go-svc-configure/internal/tables" ) // SyncSolutionQueryRequest 同步方案查询请求 // 嵌入通用的QueryRequest,同时保持向后兼容的旧字段 type SyncSolutionQueryRequest struct { queryreq.QueryRequest // 通用查询请求(分页、排序、筛选) // 向后兼容的旧字段(新客户端应使用QueryRequest中的Filters) SolutionCode string `json:"solutionCode,omitempty" form:"solutionCode"` // 方案代码模糊搜索 SolutionType string `json:"solutionType,omitempty" form:"solutionType"` // 方案类型精确搜索 SolutionName string `json:"solutionName,omitempty" form:"solutionName"` // 方案名称模糊搜索 SortField string `json:"sortField,omitempty" form:"sortField"` // 排序字段(单字段) SortOrder string `json:"sortOrder,omitempty" form:"sortOrder"` // 排序方向: asc/desc } // ListSyncSolution 查询同步方案列表(支持分页、搜索、排序) func ListSyncSolution(req *SyncSolutionQueryRequest, ctx context.Context, dbFactory *database.DBFactory, reqCtx *ctx.RequestContext) *response.QueryResult[[]tables.SyncSolutionDB] { if logger.IsDebug() { logger.Debug("ListSyncSolution req: %v", req) } // 检查请求参数是否为nil if req == nil { logger.ErrorC(reqCtx, "ListSyncSolution请求参数为nil,创建默认参数") req = &SyncSolutionQueryRequest{} req.Page = 0 req.PageSize = 10 } // 验证和规范化请求参数 req.Validate() // 将旧字段转换为新的Filters和Sorts(向后兼容) convertLegacyFields(req) logger.Debug(fmt.Sprintf("ListSyncSolution-开始查询同步方案,参数: page=%d, pageSize=%d, filters=%d, sorts=%d, legacyFields=[solutionCode=%s, solutionType=%s, solutionName=%s]", req.Page, req.PageSize, len(req.Filter), len(req.Filter), req.SolutionCode, req.SolutionType, req.SolutionName)) // 获取数据库连接 db := dbFactory.GetDB() // 定义字段映射函数:前端字段名 -> 数据库列名 fieldMapper := func(field string) string { switch field { case "id": return "id" case "solutionCode": return "solution_code" case "solutionType": return "solution_type" case "solutionName": return "solution_name" case "description": return "description" case "creator": return "creator" case "createdAt": return "created_at" case "updatedAt": return "updated_at" case "deletedAt": return "deleted_at" default: // 未知字段,返回空字符串(将被忽略) return "" } } // 使用通用查询构建器构建SQL whereClause, whereArgs := queryreq.BuildWhereClause(req.Filter, fieldMapper) orderByClause := queryreq.BuildOrderByClause(req.Sort, fieldMapper) // 构建基础查询 baseQuery := ` SELECT id, solution_code, solution_type, solution_name, description, creator, created_at, updated_at, deleted_at FROM sync_solution WHERE deleted_at IS NULL ` // 构建计数查询 countQuery := "SELECT COUNT(*) FROM sync_solution WHERE deleted_at IS NULL" if whereClause != "" { countQuery += " " + whereClause } // 构建数据查询 dataQuery := baseQuery if whereClause != "" { dataQuery += " " + whereClause } if orderByClause != "" { dataQuery += " " + orderByClause } else { // 默认排序 dataQuery += " ORDER BY solution_code" } // 添加分页限制 dataQuery += " LIMIT ? OFFSET ?" queryArgs := append(whereArgs, req.PageSize, req.GetOffset()) // 查询总记录数 var totalCount int64 err := db.GetContext(ctx, &totalCount, countQuery, whereArgs...) if err != nil { logger.ErrorC(reqCtx, fmt.Sprintf("查询同步方案总数失败: %v", err)) return util.CreateErrorResult[[]tables.SyncSolutionDB](fmt.Sprintf("查询同步方案总数失败: %v", err), reqCtx) } // 查询分页数据 var solutionList []tables.SyncSolutionDB err = db.SelectContext(ctx, &solutionList, dataQuery, queryArgs...) if err != nil { logger.ErrorC(reqCtx, fmt.Sprintf("查询同步方案失败: %v", err)) return util.CreateErrorResult[[]tables.SyncSolutionDB](fmt.Sprintf("查询同步方案失败: %v", err), reqCtx) } logger.Debug(fmt.Sprintf("查询到 %d 条同步方案,总记录数: %d", len(solutionList), totalCount)) // 创建结果,设置TotalCount result := util.CreateSuccessResultData[[]tables.SyncSolutionDB](solutionList, reqCtx) result.TotalCount = int(totalCount) // 计算总页数 pageSize := req.PageSize if pageSize <= 0 { pageSize = 10 // 默认每页10条 } lastPage := 1 if pageSize > 0 && totalCount > 0 { lastPage = (int(totalCount) + pageSize - 1) / pageSize } result.LastPage = lastPage logger.Debug(fmt.Sprintf("设置QueryResult TotalCount: %d, LastPage: %d", result.TotalCount, result.LastPage)) return result } // convertLegacyFields 将旧字段转换为新的Filters和Sorts(向后兼容) func convertLegacyFields(req *SyncSolutionQueryRequest) { // 转换旧搜索字段为Filters if req.SolutionCode != "" { req.Filter = append(req.Filter, queryreq.FilterParam{ Field: "solutionCode", Operator: queryreq.OpLike, Value: req.SolutionCode, }) } if req.SolutionType != "" { req.Filter = append(req.Filter, queryreq.FilterParam{ Field: "solutionType", Operator: queryreq.OpLike, Value: req.SolutionType, }) } if req.SolutionName != "" { req.Filter = append(req.Filter, queryreq.FilterParam{ Field: "solutionName", Operator: queryreq.OpLike, Value: req.SolutionName, }) } // 转换旧排序字段为Sorts if req.SortField != "" { // 映射可能的旧字段名 sortField := req.SortField switch sortField { case "solution_code": sortField = "solutionCode" case "solution_type": sortField = "solutionType" case "solution_name": sortField = "solutionName" case "description": sortField = "description" case "creator": sortField = "creator" case "created_at": sortField = "createdAt" case "updated_at": sortField = "updatedAt" } sortOrder := req.SortOrder if sortOrder != "asc" && sortOrder != "desc" { sortOrder = "asc" } req.Sort = append(req.Sort, queryreq.SortParam{ Field: sortField, Order: sortOrder, }) } } // SearchSyncSolution 搜索同步方案(简单搜索) func SearchSyncSolution(ctx context.Context, dbFactory *database.DBFactory, solutionCode, solutionType, solutionName string, reqCtx *ctx.RequestContext) *response.QueryResult[[]tables.SyncSolutionDB] { logger.Debug("SearchSyncSolution-开始搜索同步方案") // 获取数据库连接 db := dbFactory.GetDB() // 构建查询条件和参数 query := ` SELECT id, solution_code, solution_type, solution_name, description, creator, created_at, updated_at, deleted_at FROM sync_solution WHERE deleted_at IS NULL ` var args []interface{} if solutionCode != "" { query += " AND solution_code LIKE ?" args = append(args, "%"+solutionCode+"%") } if solutionType != "" { query += " AND solution_type = ?" args = append(args, solutionType) } if solutionName != "" { query += " AND solution_name LIKE ?" args = append(args, "%"+solutionName+"%") } query += " ORDER BY solution_code" var solutionList []tables.SyncSolutionDB err := db.SelectContext(ctx, &solutionList, query, args...) if err != nil { logger.ErrorC(reqCtx, fmt.Sprintf("搜索同步方案失败: %v", err)) return util.CreateErrorResult[[]tables.SyncSolutionDB](fmt.Sprintf("搜索同步方案失败: %v", err), reqCtx) } logger.Debug(fmt.Sprintf("搜索到 %d 条同步方案", len(solutionList))) return util.CreateSuccessResultData(solutionList, reqCtx) }