video_product.lua 13 KB

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