video_product.lua 10.0 KB

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