video_applet_product.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. local config = require "run_config"
  8. local mysql = require "skynet.db.mysql"
  9. local db
  10. local mysqldtaskbx = {}
  11. function M.check_table_exists(table_name)
  12. local sql = string.format("SHOW TABLES LIKE '%s'", table_name)
  13. local res = mysqldtaskbx.Singleton().query(sql)
  14. return res and #res > 0
  15. end
  16. function M.add_applet_product(msg_body)
  17. local isok ,key = tools.checkData({"butler_id","product_name","product_id","book_platform","dy_small_program_start","dy_small_program_start_data","dy_small_applet_app_id","check_url","main_id","status"},msg_body)
  18. if not isok then
  19. return false,string.format("缺少字段: %s.", key)
  20. end
  21. local table_name = "video_applet_product_"..msg_body.butler_id
  22. if msg_body.isOrigin == 1 then
  23. table_name = "video_applet_product"
  24. else
  25. if not M.check_table_exists(table_name) then
  26. return false,string.format("不存在 %s 这个数据.", table_name)
  27. end
  28. end
  29. local sql = string.format("INSERT INTO %s (product_name,product_id,book_platform,dy_small_program_start,dy_small_program_start_data,dy_small_applet_app_id,check_url,main_id,status,wait_status) VALUES ('%s','%s',%d,'%s','%s','%s','%s',%d,%d,%d)",
  30. table_name,msg_body.product_name,msg_body.product_id,msg_body.book_platform,msg_body.dy_small_program_start,msg_body.dy_small_program_start_data,msg_body.dy_small_applet_app_id,msg_body.check_url,msg_body.main_id,msg_body.status,2)
  31. mysqldtaskbx.Singleton().query(sql)
  32. return true, {}
  33. end
  34. --开启小程序书籍任务
  35. function M.open_app_book_task(msg_body)
  36. local isok ,key = tools.checkData({"butler_id","id_list"},msg_body)
  37. if not isok then
  38. return false,string.format("缺少字段: %s.", key)
  39. end
  40. local table_name = "video_applet_product_"..msg_body.butler_id
  41. if msg_body.isOrigin == 1 then
  42. table_name = "video_applet_product"
  43. else
  44. if not M.check_table_exists(table_name) then
  45. return false,string.format("不存在 %s 这个数据.", table_name)
  46. end
  47. end
  48. for i = 1, #msg_body.id_list, 1 do
  49. local res,sql,id;
  50. id = msg_body.id_list[i]
  51. sql = string.format("UPDATE %s SET status = 0 , wait_status = 0 ,is_close_execution = 1 WHERE id = %d ",table_name,id)
  52. res = mysqldtaskbx.Singleton().query(sql)
  53. end
  54. return true,{}
  55. end
  56. --开启小程序书籍任务
  57. function M.open_app_book_task(msg_body)
  58. local isok ,key = tools.checkData({"butler_id","id_list"},msg_body)
  59. if not isok then
  60. return false,string.format("缺少字段: %s.", key)
  61. end
  62. local table_name = "video_applet_product_"..msg_body.butler_id
  63. if msg_body.isOrigin == 1 then
  64. table_name = "video_applet_product"
  65. else
  66. if not M.check_table_exists(table_name) then
  67. return false,string.format("不存在 %s 这个数据.", table_name)
  68. end
  69. end
  70. for i = 1, #msg_body.id_list, 1 do
  71. local res,sql,id;
  72. id = msg_body.id_list[i]
  73. sql = string.format("UPDATE %s SET status = 0 , wait_status = 0 ,is_close_execution = 1 WHERE id = %d ",table_name,id)
  74. res = mysqldtaskbx.Singleton().query(sql)
  75. end
  76. return true,{}
  77. end
  78. --开启检测链接任务
  79. function M.open_check_url_task(msg_body)
  80. local isok ,key = tools.checkData({"butler_id","id_list"},msg_body)
  81. if not isok then
  82. return false,string.format("缺少字段: %s.", key)
  83. end
  84. local table_name = "video_applet_product_"..msg_body.butler_id
  85. if msg_body.isOrigin == 1 then
  86. table_name = "video_applet_product"
  87. else
  88. if not M.check_table_exists(table_name) then
  89. return false,string.format("不存在 %s 这个数据.", table_name)
  90. end
  91. end
  92. local idString = table.concat(msg_body.id_list, ",")
  93. local sql = string.format("UPDATE %s SET status = 0 , wait_status = 1 WHERE id IN (%s) ",table_name,idString)
  94. skynet.error("sql:",sql)
  95. mysqldtaskbx.Singleton().query(sql)
  96. return true,{}
  97. end
  98. --设置任务权重
  99. function M.set_task_weight(msg_body)
  100. local isok ,key = tools.checkData({"butler_id","id_list","weight"},msg_body)
  101. if not isok then
  102. return false,string.format("缺少字段: %s.", key)
  103. end
  104. local table_name = "video_applet_product_"..msg_body.butler_id
  105. if msg_body.isOrigin == 1 then
  106. table_name = "video_applet_product"
  107. else
  108. if not M.check_table_exists(table_name) then
  109. return false,string.format("不存在 %s 这个数据.", table_name)
  110. end
  111. end
  112. local idString = table.concat(msg_body.id_list, ",")
  113. local sql = string.format("UPDATE %s SET weight = %d WHERE id IN (%s) ",table_name,msg_body.weight,idString)
  114. skynet.error("sql:",sql)
  115. mysqldtaskbx.Singleton().query(sql)
  116. return true,{}
  117. end
  118. --搜索小程序书
  119. --tg_platform_id 平台id
  120. --product_id 书id
  121. --product_name 书名
  122. --main_id 主体ID
  123. --dy_small_applet_app_id 小程序ID
  124. function M.search_app_book_data(msg_body)
  125. local isok ,key = tools.checkData({
  126. "error_info",
  127. "status",
  128. "product_name",
  129. "product_id",
  130. "tg_platform_id",
  131. "main_id",
  132. "app_id",
  133. "page_size",
  134. "page_number","butler_id"},msg_body)
  135. if not isok then
  136. return false,string.format("缺少字段: %s.", key)
  137. end
  138. local table_name = "video_applet_product_"..msg_body.butler_id
  139. if msg_body.isOrigin == 1 then
  140. table_name = "video_applet_product"
  141. else
  142. if not M.check_table_exists(table_name) then
  143. return false,string.format("不存在 %s 这个数据.", table_name)
  144. end
  145. end
  146. local page_size = msg_body.page_size
  147. local page_number = msg_body.page_number
  148. local offset = (page_number - 1) * page_size
  149. local error_info_param = ""
  150. if msg_body.error_info~="" then
  151. local searchTerm = string.gsub(msg_body.error_info, "%%", "\\%%")
  152. error_info_param = string.format(" AND error_info LIKE CONCAT('%%', '%s', '%%') ",searchTerm)
  153. end
  154. local product_param = ""
  155. if msg_body.product_id~="" then
  156. product_param =string.format(" AND product_id = '%s' ",msg_body.product_id)
  157. end
  158. local product_name_param = ""
  159. if msg_body.product_name~="" then
  160. product_name_param = string.format("AND product_name = '%s' ",msg_body.product_name)
  161. end
  162. local tg_platform_param = ""
  163. if msg_body.tg_platform_id~="" then
  164. tg_platform_param = "AND book_platform = "..msg_body.tg_platform_id.." "
  165. end
  166. local main_param = ""
  167. if msg_body.main_id~="" then
  168. main_param = "AND main_id = "..msg_body.main_id.." "
  169. end
  170. local app_param = ""
  171. if msg_body.app_id~="" then
  172. app_param = string.format("AND dy_small_applet_app_id = '%s' ",msg_body.app_id)
  173. end
  174. local status_param = ""
  175. if msg_body.status~="" then
  176. status_param = " AND status = "..msg_body.status.." "
  177. end
  178. local param = error_info_param..product_param..product_name_param..tg_platform_param..main_param..app_param..status_param;
  179. local sql = "SELECT * FROM "..table_name.." WHERE 1=1 "..param.." ORDER BY id DESC "..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  180. local list = mysqldtaskbx.Singleton().query(sql)
  181. sql = "SELECT COUNT(*) AS total FROM "..table_name.." WHERE 1=1 "..param.." ORDER BY id DESC "
  182. local total = mysqldtaskbx.Singleton().query(sql)
  183. return true,list,total[1].total
  184. end
  185. function mysqldtaskbx.start()
  186. local function on_connect(db)
  187. db:query("set charset utf8mb4");
  188. end
  189. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  190. db = mysql.connect{
  191. host=conf.ip,
  192. port=conf.port,
  193. database=conf.db,
  194. user=conf.user,
  195. password=conf.password,
  196. charset="utf8mb4",
  197. max_packet_size = 1024 * 1024,
  198. on_connect = on_connect
  199. }
  200. if not db then
  201. skynet.error("mysql connect fail")
  202. end
  203. end
  204. function mysqldtaskbx.Singleton()
  205. if db == nil then
  206. mysqldtaskbx.start()
  207. end
  208. return mysqldtaskbx
  209. end
  210. function mysqldtaskbx.query(sql)
  211. return db:query(sql)
  212. end
  213. return M