video_titles.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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","pay_type","title_type_id"},msg_body)
  9. if not isok then
  10. return false,string.format("缺少字段: %s.", key)
  11. end
  12. local pay_type = msg_body.pay_type
  13. local title_type_id = msg_body.title_type_id
  14. for i = 1, #msg_body.title_list, 1 do
  15. local title = msg_body.title_list[i].title
  16. local md5_tag = msg_body.title_list[i].md5_tag
  17. local sql = string.format("SELECT * FROM video_titles WHERE md5_tag = '%s' LIMIT 1", md5_tag)
  18. local isok,res;
  19. res = mysqldbx.query(sql)
  20. if #res > 0 then
  21. else
  22. sql = string.format("INSERT INTO `video_titles` (title,md5_tag,pay_type,title_type_id) VALUES ('%s','%s',%d,%d)",title,md5_tag,pay_type,title_type_id)
  23. mysqldbx.query(sql)
  24. end
  25. end
  26. return true, {}
  27. end
  28. function M.delete_video_title(msg_body)
  29. local isok ,key = tools.checkData({"id_list"},msg_body)
  30. if not isok then
  31. return false,string.format("缺少字段: %s.", key)
  32. end
  33. for i = 1, #msg_body.id_list, 1 do
  34. local id = msg_body.id_list[i]
  35. local sql = string.format("DELETE FROM video_titles WHERE id = %d ",id)
  36. mysqldbx.query(sql)
  37. end
  38. return true, {}
  39. end
  40. function M.modify_video_title(msg_body)
  41. local isok ,key = tools.checkData({"title","md5_tag","id","pay_type","title_type_id"},msg_body)
  42. if not isok then
  43. return false,string.format("缺少字段: %s.", key)
  44. end
  45. local sql = string.format("UPDATE video_titles SET title_type_id = %d , pay_type = %d , title = '%s' , md5_tag = '%s' WHERE id = %d ",msg_body.title_type_id,msg_body.pay_type,msg_body.title,msg_body.md5_tag,msg_body.id)
  46. mysqldbx.query(sql)
  47. return true, {}
  48. end
  49. function M.modify_pay_type(msg_body)
  50. local isok ,key = tools.checkData({"id_list","pay_type"},msg_body)
  51. if not isok then
  52. return false,string.format("缺少字段: %s.", key)
  53. end
  54. local idString = table.concat(msg_body.id_list, ",")
  55. sql = string.format("UPDATE `video_titles` SET pay_type = %d WHERE id IN (%s) ",msg_body.pay_type,idString)
  56. mysqldbx.query(sql)
  57. return true, {}
  58. end
  59. function M.modify_title_type_id(msg_body)
  60. local isok ,key = tools.checkData({"id_list","title_type_id"},msg_body)
  61. if not isok then
  62. return false,string.format("缺少字段: %s.", key)
  63. end
  64. local idString = table.concat(msg_body.id_list, ",")
  65. sql = string.format("UPDATE `video_titles` SET title_type_id = %d WHERE id IN (%s) ",msg_body.title_type_id,idString)
  66. mysqldbx.query(sql)
  67. return true, {}
  68. end
  69. function M.search_video_title(msg_body)
  70. local isok ,key = tools.checkData({"page_size","page_number","content","pay_type","title_type_id"},msg_body)
  71. if not isok then
  72. return false,string.format("缺少字段: %s.", key)
  73. end
  74. local page_size = msg_body.page_size
  75. local page_number = msg_body.page_number
  76. local offset = (page_number - 1) * page_size
  77. local content_param = ""
  78. if msg_body.content~=""then
  79. content_param = string.format(" AND (title LIKE CONCAT( '%%%s%%')) ",msg_body.content)
  80. end
  81. local pay_type_param = ""
  82. if msg_body.pay_type~=""then
  83. pay_type_param = string.format(" AND pay_type = %d ",msg_body.pay_type)
  84. end
  85. local title_type_id_param = ""
  86. if msg_body.title_type_id~=""then
  87. title_type_id_param = string.format(" AND title_type_id = %d ",msg_body.title_type_id)
  88. end
  89. local param = content_param..pay_type_param..title_type_id_param
  90. local sql = "SELECT * FROM video_titles WHERE 1=1 "..param.." ORDER BY id DESC".. string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  91. local list = mysqldbx.query(sql)
  92. sql = "SELECT COUNT(*) AS total FROM video_titles WHERE 1=1 "..param
  93. local total = mysqldbx.query(sql)
  94. return true,list,total[1].total
  95. end
  96. function M.initAll()
  97. local sql = "SELECT * FROM video_titles "
  98. local list = mysqldbx.query(sql)
  99. for i = 1, #list, 1 do
  100. if (list[i].md5_tag==nil) then
  101. local id = list[i].id
  102. local md5_tag =md5.sumhexa(list[i].title)
  103. skynet.error("md5_tag:",md5_tag)
  104. sql = string.format("UPDATE video_titles SET md5_tag = '%s' WHERE id = %d ",md5_tag,id)
  105. mysqldbx.query(sql)
  106. end
  107. end
  108. return true,{}
  109. end
  110. return M