| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- 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)
- }
|