|
@@ -23,39 +23,58 @@ function M.search_origin_data(msg_body)
|
|
|
local page_number = msg_body.page_number
|
|
|
local offset = (page_number - 1) * page_size
|
|
|
|
|
|
- local create_date_param = ""
|
|
|
- if msg_body.start_create_time~="" and msg_body.end_create_time~="" then
|
|
|
- create_date_param = " AND DATE(create_time) >= DATE(FROM_UNIXTIME(" .. (msg_body.start_create_time / 1000) .. ")) AND DATE(create_time) <= DATE(FROM_UNIXTIME(" .. (msg_body.end_create_time / 1000) .. "))"
|
|
|
+ -- 使用数组构建查询条件,避免字符串拼接
|
|
|
+ local conditions = {"1=1"}
|
|
|
+ local params = {}
|
|
|
+
|
|
|
+ -- 创建时间条件
|
|
|
+ if msg_body.start_create_time ~= "" and msg_body.end_create_time ~= "" then
|
|
|
+ table.insert(conditions, "DATE(create_time) BETWEEN DATE(FROM_UNIXTIME(?)) AND DATE(FROM_UNIXTIME(?))")
|
|
|
+ table.insert(params, msg_body.start_create_time / 1000)
|
|
|
+ table.insert(params, msg_body.end_create_time / 1000)
|
|
|
end
|
|
|
|
|
|
- local date_param = ""
|
|
|
- if msg_body.start_publish_time~="" and msg_body.end_publish_time~="" then
|
|
|
- date_param = " AND DATE(publish_time) >= DATE(FROM_UNIXTIME(" .. (msg_body.start_publish_time / 1000) .. ")) AND DATE(publish_time) <= DATE(FROM_UNIXTIME(" .. (msg_body.end_publish_time / 1000) .. "))"
|
|
|
+ -- 发布时间条件
|
|
|
+ if msg_body.start_publish_time ~= "" and msg_body.end_publish_time ~= "" then
|
|
|
+ table.insert(conditions, "DATE(publish_time) BETWEEN DATE(FROM_UNIXTIME(?)) AND DATE(FROM_UNIXTIME(?))")
|
|
|
+ table.insert(params, msg_body.start_publish_time / 1000)
|
|
|
+ table.insert(params, msg_body.end_publish_time / 1000)
|
|
|
end
|
|
|
|
|
|
-
|
|
|
- local guajian_info_param = ""
|
|
|
- if msg_body.guajian_info~="" then
|
|
|
- guajian_info_param = string.format(" AND ( guajian_link LIKE CONCAT( '%%%s%%')) ",msg_body.guajian_info)
|
|
|
+ -- 挂件信息条件
|
|
|
+ if msg_body.guajian_info ~= "" then
|
|
|
+ table.insert(conditions, "guajian_link LIKE ?")
|
|
|
+ table.insert(params, "%" .. msg_body.guajian_info .. "%")
|
|
|
end
|
|
|
|
|
|
- local materialId_param = ""
|
|
|
- if msg_body.materialId~="" then
|
|
|
- materialId_param = string.format(" AND materialId = '%s' ",msg_body.materialId)
|
|
|
+ -- 材料ID条件
|
|
|
+ if msg_body.materialId ~= "" then
|
|
|
+ table.insert(conditions, "materialId = ?")
|
|
|
+ table.insert(params, msg_body.materialId)
|
|
|
end
|
|
|
|
|
|
- local param = date_param..create_date_param..guajian_info_param..materialId_param
|
|
|
-
|
|
|
-
|
|
|
- local sql = "SELECT * FROM origin_data WHERE 1=1 "..param.."ORDER BY id DESC"..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
|
|
|
+ -- 构建WHERE子句
|
|
|
+ local where_clause = table.concat(conditions, " AND ")
|
|
|
|
|
|
- local res = mysqldbx.query(sql)
|
|
|
+ -- 使用子查询优化分页
|
|
|
+ local sql = string.format([[
|
|
|
+ SELECT * FROM (
|
|
|
+ SELECT id FROM origin_data
|
|
|
+ WHERE %s
|
|
|
+ ORDER BY id DESC
|
|
|
+ LIMIT %d OFFSET %d
|
|
|
+ ) AS tmp
|
|
|
+ JOIN origin_data USING(id)
|
|
|
+ ]], where_clause, page_size, offset)
|
|
|
|
|
|
- sql = "SELECT COUNT(*) AS total FROM origin_data WHERE 1=1 "..param
|
|
|
+ -- 使用 COUNT(*) 优化总数查询
|
|
|
+ local count_sql = string.format("SELECT COUNT(*) AS total FROM origin_data WHERE %s", where_clause)
|
|
|
|
|
|
- local total = mysqldbx.query(sql)
|
|
|
+ -- 执行查询
|
|
|
+ local res = mysqldbx.query(sql, table.unpack(params))
|
|
|
+ local total = mysqldbx.query(count_sql, table.unpack(params))
|
|
|
|
|
|
- return true,res,total[1].total
|
|
|
+ return true, res, total[1].total
|
|
|
end
|
|
|
|
|
|
return M
|