origin_data.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. --原始数据
  2. local M = {}
  3. local mysqldbx = require "mysqldbx"
  4. local tools = require "tools"
  5. local skynet = require "skynet"
  6. local cjson = require "cjson"
  7. function M.delete_origin_data_by_time(msg_body)
  8. local isok ,key = tools.checkData({"start_create_time","end_create_time"},msg_body)
  9. if not isok then
  10. return false,string.format("缺少字段: %s.", key)
  11. end
  12. local delete_time_param = " AND DATE(created_at) >= DATE(FROM_UNIXTIME(" .. (msg_body.start_create_time / 1000) .. ")) AND DATE(created_at) <= DATE(FROM_UNIXTIME(" .. (msg_body.end_create_time / 1000) .. "))"
  13. end
  14. function M.search_origin_data(msg_body)
  15. local isok ,key = tools.checkData({"page_size","page_number","start_create_time","end_create_time","guajian_info","start_publish_time","end_publish_time","materialId"},msg_body)
  16. if not isok then
  17. return false,string.format("缺少字段: %s.", key)
  18. end
  19. local page_size = msg_body.page_size
  20. local page_number = msg_body.page_number
  21. local offset = (page_number - 1) * page_size
  22. -- 使用数组构建查询条件,避免字符串拼接
  23. local conditions = {"1=1"}
  24. local params = {}
  25. -- 创建时间条件
  26. if msg_body.start_create_time ~= "" and msg_body.end_create_time ~= "" then
  27. table.insert(conditions, "DATE(create_time) BETWEEN DATE(FROM_UNIXTIME(?)) AND DATE(FROM_UNIXTIME(?))")
  28. table.insert(params, msg_body.start_create_time / 1000)
  29. table.insert(params, msg_body.end_create_time / 1000)
  30. end
  31. -- 发布时间条件
  32. if msg_body.start_publish_time ~= "" and msg_body.end_publish_time ~= "" then
  33. table.insert(conditions, "DATE(publish_time) BETWEEN DATE(FROM_UNIXTIME(?)) AND DATE(FROM_UNIXTIME(?))")
  34. table.insert(params, msg_body.start_publish_time / 1000)
  35. table.insert(params, msg_body.end_publish_time / 1000)
  36. end
  37. -- 挂件信息条件
  38. if msg_body.guajian_info ~= "" then
  39. table.insert(conditions, "guajian_link LIKE ?")
  40. table.insert(params, "%" .. msg_body.guajian_info .. "%")
  41. end
  42. -- 材料ID条件
  43. if msg_body.materialId ~= "" then
  44. table.insert(conditions, "materialId = ?")
  45. table.insert(params, msg_body.materialId)
  46. end
  47. -- 构建WHERE子句
  48. local where_clause = table.concat(conditions, " AND ")
  49. -- 使用子查询优化分页
  50. local sql = string.format([[
  51. SELECT * FROM (
  52. SELECT id FROM origin_data
  53. WHERE %s
  54. ORDER BY id DESC
  55. LIMIT %d OFFSET %d
  56. ) AS tmp
  57. JOIN origin_data USING(id)
  58. ]], where_clause, page_size, offset)
  59. -- 使用 COUNT(*) 优化总数查询
  60. local count_sql = string.format("SELECT COUNT(*) AS total FROM origin_data WHERE %s", where_clause)
  61. -- 执行查询
  62. local res = mysqldbx.query(sql, table.unpack(params))
  63. local total = mysqldbx.query(count_sql, table.unpack(params))
  64. return true, res, total[1].total
  65. end
  66. return M