video_product.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. --搜索书
  12. --tg_platform_id 平台id
  13. --product_id 书id
  14. --product_name 书名
  15. function M.search_book_data(msg_body)
  16. local isok ,key = tools.checkData({
  17. "is_top",
  18. "status",
  19. "start_publish_time",
  20. "end_publish_time",
  21. "up_or_down_publish_time",
  22. "min_book_word",
  23. "max_book_word",
  24. "alias_name",
  25. "product_name",
  26. "product_id",
  27. "tg_platform_id",
  28. "oce_material_id",
  29. "page_size",
  30. "page_number",
  31. "is_auto",
  32. "stat_cost","min_totalChapterNum","max_totalChapterNum"},msg_body)
  33. if not isok then
  34. return false,string.format("缺少字段: %s.", key)
  35. end
  36. local page_size = msg_body.page_size
  37. local page_number = msg_body.page_number
  38. local offset = (page_number - 1) * page_size
  39. local is_top_param = ""
  40. if msg_body.is_top~="" then
  41. is_top_param = string.format(" AND is_top = %d ",msg_body.is_top)
  42. end
  43. local status_param = ""
  44. if msg_body.status~="" then
  45. status_param = string.format(" AND status = %d ",msg_body.status)
  46. end
  47. local date_param = ""
  48. if msg_body.start_publish_time~="" and msg_body.end_publish_time~="" then
  49. date_param = " AND DATE(publish_time) >= DATE(FROM_UNIXTIME(" .. (msg_body.start_publish_time / 1000) .. ")) AND DATE(publish_time) <= DATE(FROM_UNIXTIME(" .. (msg_body.end_publish_time / 1000) .. ")) "
  50. end
  51. local up_or_down_publish_time_param = ""
  52. if msg_body.up_or_down_publish_time~="" then
  53. if(msg_body.up_or_down_publish_time == 0) then
  54. up_or_down_publish_time_param = " publish_time ASC"
  55. else
  56. up_or_down_publish_time_param = " publish_time DESC"
  57. end
  58. end
  59. local word_param = ""
  60. if msg_body.min_book_word~="" and msg_body.max_book_word~="" then
  61. word_param = string.format(" AND CAST(words AS UNSIGNED) >= %f AND CAST(words AS UNSIGNED) <= %f ",msg_body.min_book_word,msg_body.max_book_word)
  62. end
  63. local stat_cost_param = ""
  64. if msg_body.stat_cost~="" then
  65. if(msg_body.stat_cost == 0) then
  66. stat_cost_param = " stat_cost ASC"
  67. else
  68. stat_cost_param = " stat_cost DESC"
  69. end
  70. end
  71. local totalChapterNum_param = ""
  72. if msg_body.min_totalChapterNum~="" and msg_body.max_totalChapterNum~="" then
  73. totalChapterNum_param = string.format(" AND totalChapterNum >= %d AND totalChapterNum <= %d ",msg_body.min_totalChapterNum,msg_body.max_totalChapterNum)
  74. end
  75. local product_param = ""
  76. if msg_body.product_id~="" then
  77. product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
  78. end
  79. local product_name_param = ""
  80. if msg_body.product_name~="" then
  81. product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
  82. end
  83. local tg_platform_param = ""
  84. if msg_body.tg_platform_id~="" then
  85. tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
  86. end
  87. local is_auto_param = ""
  88. if msg_body.is_auto~="" then
  89. is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
  90. end
  91. local is_store_param = ""
  92. if msg_body.is_store~="" then
  93. is_store_param = " AND is_store = "..msg_body.is_store.." "
  94. end
  95. local genre_param = ""
  96. if msg_body.genre~="" then
  97. genre_param = " AND genre = "..msg_body.genre.." "
  98. end
  99. local alias_name_param = ""
  100. if msg_body.alias_name~="" then
  101. alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
  102. end
  103. local param = is_top_param..status_param..date_param..totalChapterNum_param..product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param..word_param;
  104. if stat_cost_param~="" and up_or_down_publish_time_param ~="" then
  105. up_or_down_publish_time_param = " , "..up_or_down_publish_time_param
  106. end
  107. local up_down_param = stat_cost_param..up_or_down_publish_time_param
  108. if up_down_param ~= "" then
  109. up_down_param = " ORDER BY " ..up_down_param
  110. else
  111. up_down_param = "ORDER BY id DESC"
  112. end
  113. local sql = "SELECT * FROM video_product where 1=1 "..param..up_down_param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  114. local res = mysqldtaskbx.Singleton().query(sql)
  115. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param.."ORDER BY id DESC"
  116. local total = mysqldtaskbx.Singleton().query(sql)
  117. return true,res,total[1].total
  118. end
  119. --一键发布搜索
  120. function M.search_book_data_on_main(msg_body)
  121. local isok ,key = tools.checkData({
  122. "start_publish_time",
  123. "end_publish_time",
  124. "alias_name",
  125. "product_name",
  126. "product_id",
  127. "tg_platform_id",
  128. "oce_material_id",
  129. "page_size",
  130. "page_number",
  131. "is_auto",
  132. "stat_cost",
  133. "min_book_word",
  134. "max_book_word",
  135. "min_stat_cost",
  136. "max_stat_cost"},msg_body)
  137. if not isok then
  138. return false,string.format("缺少字段: %s.", key)
  139. end
  140. local page_size = msg_body.page_size
  141. local page_number = msg_body.page_number
  142. local offset = (page_number - 1) * page_size
  143. local date_param = ""
  144. if msg_body.start_publish_time~="" and msg_body.end_publish_time~="" then
  145. date_param = " AND DATE(publish_time) >= DATE(FROM_UNIXTIME(" .. (msg_body.start_publish_time / 1000) .. ")) AND DATE(publish_time) <= DATE(FROM_UNIXTIME(" .. (msg_body.end_publish_time / 1000) .. ")) "
  146. end
  147. local word_param = ""
  148. if msg_body.min_book_word~="" and msg_body.max_book_word~="" then
  149. word_param = string.format(" AND CAST(words AS UNSIGNED) >= %d AND CAST(words AS UNSIGNED) <= %d ",msg_body.min_book_word,msg_body.max_book_word)
  150. end
  151. local stat_cost_param = ""
  152. if msg_body.min_stat_cost~="" and msg_body.max_stat_cost~="" then
  153. stat_cost_param = string.format(" AND stat_cost >= %f AND stat_cost <= %f ",msg_body.min_stat_cost,msg_body.max_stat_cost)
  154. end
  155. local product_param = ""
  156. if msg_body.product_id~="" then
  157. product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
  158. end
  159. local product_name_param = ""
  160. if msg_body.product_name~="" then
  161. product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
  162. end
  163. local tg_platform_param = ""
  164. if msg_body.tg_platform_id~="" then
  165. tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
  166. end
  167. local is_auto_param = ""
  168. if msg_body.is_auto~="" then
  169. is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
  170. end
  171. local is_store_param = ""
  172. if msg_body.is_store~="" then
  173. is_store_param = " AND is_store = "..msg_body.is_store.." "
  174. end
  175. local genre_param = ""
  176. if msg_body.genre~="" then
  177. genre_param = " AND genre = "..msg_body.genre.." "
  178. end
  179. local alias_name_param = ""
  180. if msg_body.alias_name~="" then
  181. alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
  182. end
  183. local param = date_param..stat_cost_param..word_param..product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param;
  184. local sql = string.format("SELECT v.* FROM video_product v JOIN ( SELECT id FROM video_product WHERE 1=1 %s ORDER BY is_top DESC , id ASC LIMIT %d OFFSET %d ) AS tmp ON v.id = tmp.id",param,page_size,offset)
  185. skynet.error("sql:",sql)
  186. local res = mysqldtaskbx.Singleton().query(sql)
  187. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param
  188. local total = mysqldtaskbx.Singleton().query(sql)
  189. return true,res,total[1].total
  190. end
  191. --设置书别名
  192. function M.set_video_product_alias_name(msg_body)
  193. local isok ,key = tools.checkData({"id","alias_name"},msg_body)
  194. if not isok then
  195. return false,string.format("缺少字段: %s.", key)
  196. end
  197. local sql = string.format("UPDATE `video_product` SET alias_name = '%s' WHERE id = %d ",
  198. msg_body.alias_name,msg_body.id)
  199. local res = mysqldtaskbx.Singleton().query(sql)
  200. return true,{}
  201. end
  202. --设置书类型(长篇,短片)
  203. function M.set_video_product_genre(msg_body)
  204. local isok ,key = tools.checkData({"genre","id_list"},msg_body)
  205. if not isok then
  206. return false,string.format("缺少字段: %s.", key)
  207. end
  208. local idString = table.concat(msg_body.id_list, ",")
  209. local sql = string.format("UPDATE video_product SET genre = %d WHERE id IN (%s) ",msg_body.genre,idString)
  210. mysqldtaskbx.Singleton().query(sql)
  211. return true, {}
  212. end
  213. --设置书状态
  214. function M.set_status(msg_body)
  215. local isok ,key = tools.checkData({"status","id_list"},msg_body)
  216. if not isok then
  217. return false,string.format("缺少字段: %s.", key)
  218. end
  219. local idString = table.concat(msg_body.id_list, ",")
  220. local sql = string.format("UPDATE video_product SET status = %d WHERE id IN (%s) ",msg_body.status,idString)
  221. mysqldtaskbx.Singleton().query(sql)
  222. return true, {}
  223. end
  224. --设置书优先级
  225. function M.set_top(msg_body)
  226. local isok ,key = tools.checkData({"expired_time","id_list"},msg_body)
  227. if not isok then
  228. return false,string.format("缺少字段: %s.", key)
  229. end
  230. -- local current_time = os.date("%Y-%m-%d %H:%M:%S")
  231. local sql = ""
  232. local isok,res;
  233. for i = 1, #msg_body.id_list, 1 do
  234. local id = msg_body.id_list[i]
  235. sql = string.format("SELECT * FROM video_product where id = %d ", id)
  236. res = mysqldtaskbx.Singleton().query(sql)
  237. if #res >0 then
  238. local publish_time = res[1].publish_time
  239. local y_publish_time = res[1].y_publish_time
  240. if y_publish_time~=nil then
  241. publish_time = y_publish_time
  242. end
  243. sql = string.format("UPDATE video_product SET is_top = 1 , expired_time = '%s' , y_publish_time = '%s' WHERE id = %d ",msg_body.expired_time,publish_time,id)
  244. mysqldtaskbx.Singleton().query(sql)
  245. end
  246. end
  247. -- local idString = table.concat(msg_body.id_list, ",")
  248. -- local sql = string.format("UPDATE video_product SET is_top = 1 , expired_time = '%s' WHERE id IN (%s) ",msg_body.expired_time,idString)
  249. -- mysqldtaskbx.Singleton().query(sql)
  250. return true, {}
  251. end
  252. function mysqldtaskbx.start()
  253. local function on_connect(db)
  254. db:query("set charset utf8mb4");
  255. end
  256. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  257. db = mysql.connect{
  258. host=conf.ip,
  259. port=conf.port,
  260. database=conf.db,
  261. user=conf.user,
  262. password=conf.password,
  263. charset="utf8mb4",
  264. max_packet_size = 1024 * 1024,
  265. on_connect = on_connect
  266. }
  267. if not db then
  268. skynet.error("mysql connect fail")
  269. end
  270. end
  271. function mysqldtaskbx.Singleton()
  272. if db == nil then
  273. mysqldtaskbx.start()
  274. end
  275. return mysqldtaskbx
  276. end
  277. function mysqldtaskbx.query(sql)
  278. return db:query(sql)
  279. end
  280. return M