video_titles.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --视频标题
  2. local M = {}
  3. local mysqldbx = require "mysqldbx"
  4. local tools = require "tools"
  5. local skynet = require "skynet"
  6. local md5 = require "md5"
  7. function M.add_video_title(msg_body)
  8. local isok ,key = tools.checkData({"title_list"},msg_body)
  9. if not isok then
  10. return false,string.format("缺少字段: %s.", key)
  11. end
  12. for i = 1, #msg_body.title_list, 1 do
  13. local title = msg_body.title_list[i].title
  14. local md5_tag = msg_body.title_list[i].md5_tag
  15. local sql = string.format("SELECT * FROM video_titles WHERE md5_tag = '%s' LIMIT 1", md5_tag)
  16. local isok,res;
  17. res = mysqldbx.query(sql)
  18. if #res > 0 then
  19. else
  20. sql = string.format("INSERT INTO `video_titles` (title,md5_tag) VALUES ('%s','%s')",title,md5_tag)
  21. mysqldbx.query(sql)
  22. end
  23. end
  24. return true, {}
  25. end
  26. function M.delete_video_title(msg_body)
  27. local isok ,key = tools.checkData({"id_list"},msg_body)
  28. if not isok then
  29. return false,string.format("缺少字段: %s.", key)
  30. end
  31. for i = 1, #msg_body.id_list, 1 do
  32. local id = msg_body.id_list[i]
  33. local sql = string.format("DELETE FROM video_titles WHERE id = %d ",id)
  34. mysqldbx.query(sql)
  35. end
  36. return true, {}
  37. end
  38. function M.modify_video_title(msg_body)
  39. local isok ,key = tools.checkData({"title","md5_tag","id"},msg_body)
  40. if not isok then
  41. return false,string.format("缺少字段: %s.", key)
  42. end
  43. local sql = string.format("UPDATE video_titles SET title = '%s' , md5_tag = '%s' WHERE id = %d ",msg_body.title,msg_body.md5_tag,msg_body.id)
  44. mysqldbx.query(sql)
  45. return true, {}
  46. end
  47. function M.search_video_title(msg_body)
  48. local isok ,key = tools.checkData({"page_size","page_number","content"},msg_body)
  49. if not isok then
  50. return false,string.format("缺少字段: %s.", key)
  51. end
  52. local page_size = msg_body.page_size
  53. local page_number = msg_body.page_number
  54. local offset = (page_number - 1) * page_size
  55. local sql = "SELECT * FROM video_titles "..string.format(" WHERE (title LIKE CONCAT( '%%%s%%')) ",msg_body.content)..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  56. local list = mysqldbx.query(sql)
  57. sql = "SELECT COUNT(*) AS total FROM video_titles "..string.format(" WHERE (title LIKE CONCAT( '%%%s%%')) ",msg_body.content)
  58. local total = mysqldbx.query(sql)
  59. return true,list,total[1].total
  60. end
  61. function M.initAll()
  62. local sql = "SELECT * FROM video_titles "
  63. local list = mysqldbx.query(sql)
  64. for i = 1, #list, 1 do
  65. if (list[i].md5_tag==nil) then
  66. local id = list[i].id
  67. local md5_tag =md5.sumhexa(list[i].title)
  68. skynet.error("md5_tag:",md5_tag)
  69. sql = string.format("UPDATE video_titles SET md5_tag = '%s' WHERE id = %d ",md5_tag,id)
  70. mysqldbx.query(sql)
  71. end
  72. end
  73. return true,{}
  74. end
  75. return M