video_applet_product.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. function M.delete(msg_body)
  119. local isok ,key = tools.checkData({"butler_id","id_list"},msg_body)
  120. if not isok then
  121. return false,string.format("缺少字段: %s.", key)
  122. end
  123. local table_name = "video_applet_product_"..msg_body.butler_id
  124. if msg_body.isOrigin == 1 then
  125. table_name = "video_applet_product"
  126. else
  127. if not M.check_table_exists(table_name) then
  128. return false,string.format("不存在 %s 这个数据.", table_name)
  129. end
  130. end
  131. local idString = table.concat(msg_body.id_list, ",")
  132. local sql = string.format("DELETE FROM %s WHERE id IN (%s) ",table_name,idString)
  133. skynet.error("sql:",sql)
  134. mysqldtaskbx.Singleton().query(sql)
  135. return true,{}
  136. end
  137. --搜索小程序书
  138. --tg_platform_id 平台id
  139. --product_id 书id
  140. --product_name 书名
  141. --main_id 主体ID
  142. --dy_small_applet_app_id 小程序ID
  143. function M.search_app_book_data(msg_body)
  144. local isok ,key = tools.checkData({
  145. "error_type",
  146. "start_first_time",
  147. "end_first_time",
  148. "error_info",
  149. "status",
  150. "product_name",
  151. "product_id",
  152. "tg_platform_id",
  153. "main_id",
  154. "app_id",
  155. "page_size",
  156. "page_number","butler_id"},msg_body)
  157. if not isok then
  158. return false,string.format("缺少字段: %s.", key)
  159. end
  160. local table_name = "video_applet_product_"..msg_body.butler_id
  161. if msg_body.isOrigin == 1 then
  162. table_name = "video_applet_product"
  163. else
  164. if not M.check_table_exists(table_name) then
  165. return false,string.format("不存在 %s 这个数据.", table_name)
  166. end
  167. end
  168. local page_size = msg_body.page_size
  169. local page_number = msg_body.page_number
  170. local offset = (page_number - 1) * page_size
  171. local error_type_param = ""
  172. if msg_body.error_type~="" then
  173. error_type_param = string.format(" AND error_type = %d ",msg_body.error_type)
  174. end
  175. local date_param = ""
  176. if msg_body.start_first_time~="" and msg_body.end_first_time~="" then
  177. date_param = " AND DATE(first_time) >= DATE(FROM_UNIXTIME(" .. (msg_body.start_first_time / 1000) .. ")) AND DATE(first_time) <= DATE(FROM_UNIXTIME(" .. (msg_body.end_first_time / 1000) .. ")) "
  178. end
  179. local error_info_param = ""
  180. if msg_body.error_info~="" then
  181. local searchTerm = string.gsub(msg_body.error_info, "%%", "\\%%")
  182. error_info_param = string.format(" AND error_info LIKE CONCAT('%%', '%s', '%%') ",searchTerm)
  183. end
  184. local product_param = ""
  185. if msg_body.product_id~="" then
  186. product_param =string.format(" AND product_id = '%s' ",msg_body.product_id)
  187. end
  188. local product_name_param = ""
  189. if msg_body.product_name~="" then
  190. product_name_param = string.format("AND product_name = '%s' ",msg_body.product_name)
  191. end
  192. local tg_platform_param = ""
  193. if msg_body.tg_platform_id~="" then
  194. tg_platform_param = "AND book_platform = "..msg_body.tg_platform_id.." "
  195. end
  196. local main_param = ""
  197. if msg_body.main_id~="" then
  198. main_param = "AND main_id = "..msg_body.main_id.." "
  199. end
  200. local app_param = ""
  201. if msg_body.app_id~="" then
  202. app_param = string.format("AND dy_small_applet_app_id = '%s' ",msg_body.app_id)
  203. end
  204. local status_param = ""
  205. if msg_body.status~="" then
  206. status_param = " AND status = "..msg_body.status.." "
  207. end
  208. local param = error_type_param..date_param..error_info_param..product_param..product_name_param..tg_platform_param..main_param..app_param..status_param;
  209. local sql = "SELECT * FROM "..table_name.." WHERE 1=1 "..param.." ORDER BY id DESC "..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  210. local list = mysqldtaskbx.Singleton().query(sql)
  211. sql = "SELECT COUNT(*) AS total FROM "..table_name.." WHERE 1=1 "..param.." ORDER BY id DESC "
  212. local total = mysqldtaskbx.Singleton().query(sql)
  213. return true,list,total[1].total
  214. end
  215. function mysqldtaskbx.start()
  216. local function on_connect(db)
  217. db:query("set charset utf8mb4");
  218. end
  219. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  220. db = mysql.connect{
  221. host=conf.ip,
  222. port=conf.port,
  223. database=conf.db,
  224. user=conf.user,
  225. password=conf.password,
  226. charset="utf8mb4",
  227. max_packet_size = 1024 * 1024,
  228. on_connect = on_connect
  229. }
  230. if not db then
  231. skynet.error("mysql connect fail")
  232. end
  233. end
  234. function mysqldtaskbx.Singleton()
  235. if db == nil then
  236. mysqldtaskbx.start()
  237. end
  238. return mysqldtaskbx
  239. end
  240. function mysqldtaskbx.query(sql)
  241. return db:query(sql)
  242. end
  243. return M