video_applet_product.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. "status",
  127. "product_name",
  128. "product_id",
  129. "tg_platform_id",
  130. "main_id",
  131. "app_id",
  132. "page_size",
  133. "page_number","butler_id"},msg_body)
  134. if not isok then
  135. return false,string.format("缺少字段: %s.", key)
  136. end
  137. local table_name = "video_applet_product_"..msg_body.butler_id
  138. if msg_body.isOrigin == 1 then
  139. table_name = "video_applet_product"
  140. else
  141. if not M.check_table_exists(table_name) then
  142. return false,string.format("不存在 %s 这个数据.", table_name)
  143. end
  144. end
  145. local page_size = msg_body.page_size
  146. local page_number = msg_body.page_number
  147. local offset = (page_number - 1) * page_size
  148. local product_param = ""
  149. if msg_body.product_id~="" then
  150. product_param =string.format(" AND product_id = '%s' ",msg_body.product_id)
  151. end
  152. local product_name_param = ""
  153. if msg_body.product_name~="" then
  154. product_name_param = string.format("AND product_name = '%s' ",msg_body.product_name)
  155. end
  156. local tg_platform_param = ""
  157. if msg_body.tg_platform_id~="" then
  158. tg_platform_param = "AND book_platform = "..msg_body.tg_platform_id.." "
  159. end
  160. local main_param = ""
  161. if msg_body.main_id~="" then
  162. main_param = "AND main_id = "..msg_body.main_id.." "
  163. end
  164. local app_param = ""
  165. if msg_body.app_id~="" then
  166. app_param = string.format("AND dy_small_applet_app_id = '%s' ",msg_body.app_id)
  167. end
  168. local status_param = ""
  169. if msg_body.status~="" then
  170. status_param = " AND status = "..msg_body.status.." "
  171. end
  172. local param = product_param..product_name_param..tg_platform_param..main_param..app_param..status_param;
  173. local sql = "SELECT * FROM "..table_name.." WHERE 1=1 "..param.." ORDER BY id DESC "..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  174. local list = mysqldtaskbx.Singleton().query(sql)
  175. sql = "SELECT COUNT(*) AS total FROM "..table_name.." WHERE 1=1 "..param.." ORDER BY id DESC "
  176. local total = mysqldtaskbx.Singleton().query(sql)
  177. return true,list,total[1].total
  178. end
  179. function mysqldtaskbx.start()
  180. local function on_connect(db)
  181. db:query("set charset utf8mb4");
  182. end
  183. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  184. db = mysql.connect{
  185. host=conf.ip,
  186. port=conf.port,
  187. database=conf.db,
  188. user=conf.user,
  189. password=conf.password,
  190. charset="utf8mb4",
  191. max_packet_size = 1024 * 1024,
  192. on_connect = on_connect
  193. }
  194. if not db then
  195. skynet.error("mysql connect fail")
  196. end
  197. end
  198. function mysqldtaskbx.Singleton()
  199. if db == nil then
  200. mysqldtaskbx.start()
  201. end
  202. return mysqldtaskbx
  203. end
  204. function mysqldtaskbx.query(sql)
  205. return db:query(sql)
  206. end
  207. return M