video_product.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. "is_album_link",
  103. "start_create_at",
  104. "end_create_at",
  105. "is_top",
  106. "status",
  107. "start_publish_time",
  108. "end_publish_time",
  109. "up_or_down_publish_time",
  110. "alias_name",
  111. "product_name",
  112. "product_id",
  113. "tg_platform_id",
  114. "oce_material_id",
  115. "page_size",
  116. "page_number",
  117. "is_auto",
  118. "stat_cost"},msg_body)
  119. if not isok then
  120. return false,string.format("缺少字段: %s.", key)
  121. end
  122. local page_size = msg_body.page_size
  123. local page_number = msg_body.page_number
  124. local offset = (page_number - 1) * page_size
  125. local is_album_link_param = ""
  126. if msg_body.is_album_link~="" then
  127. if msg_body.is_album_link == 1 then
  128. is_album_link_param = string.format(" AND album_link IS NOT NULL ").."AND album_link!='' "
  129. else
  130. is_album_link_param = string.format(" AND ( album_link IS NULL OR album_link='' ) ")
  131. end
  132. end
  133. local create_date_param = ""
  134. if msg_body.start_create_at~="" and msg_body.end_create_at~="" then
  135. 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) .. ")) "
  136. end
  137. local is_top_param = ""
  138. if msg_body.is_top~="" then
  139. is_top_param = string.format(" AND is_top = %d ",msg_body.is_top)
  140. end
  141. local status_param = ""
  142. if msg_body.status~="" then
  143. status_param = string.format(" AND status = %d ",msg_body.status)
  144. end
  145. local date_param = ""
  146. if msg_body.start_publish_time~="" and msg_body.end_publish_time~="" then
  147. 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) .. ")) "
  148. end
  149. local up_or_down_publish_time_param = ""
  150. if msg_body.up_or_down_publish_time~="" then
  151. if(msg_body.up_or_down_publish_time == 0) then
  152. up_or_down_publish_time_param = " publish_time ASC"
  153. else
  154. up_or_down_publish_time_param = " publish_time DESC"
  155. end
  156. end
  157. local stat_cost_param = ""
  158. if msg_body.stat_cost~="" then
  159. if(msg_body.stat_cost == 0) then
  160. stat_cost_param = " stat_cost ASC"
  161. else
  162. stat_cost_param = " stat_cost DESC"
  163. end
  164. end
  165. local product_param = ""
  166. if msg_body.product_id~="" then
  167. product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
  168. end
  169. local product_name_param = ""
  170. if msg_body.product_name~="" then
  171. product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
  172. end
  173. local tg_platform_param = ""
  174. if msg_body.tg_platform_id~="" then
  175. tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
  176. end
  177. local is_auto_param = ""
  178. if msg_body.is_auto~="" then
  179. is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
  180. end
  181. local is_store_param = ""
  182. if msg_body.is_store~="" then
  183. is_store_param = " AND is_store = "..msg_body.is_store.." "
  184. end
  185. local genre_param = ""
  186. if msg_body.genre~="" then
  187. genre_param = " AND genre = "..msg_body.genre.." "
  188. end
  189. local alias_name_param = ""
  190. if msg_body.alias_name~="" then
  191. alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
  192. end
  193. local param = is_album_link_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;
  194. local up_down_param = ""
  195. if up_or_down_publish_time_param ~= "" then
  196. up_down_param = up_or_down_publish_time_param
  197. end
  198. if stat_cost_param ~= "" then
  199. if (up_down_param~="") then
  200. up_down_param = up_down_param.." , "..stat_cost_param
  201. else
  202. up_down_param = stat_cost_param
  203. end
  204. end
  205. if up_down_param ~= "" then
  206. up_down_param = " ORDER BY "..up_down_param
  207. else
  208. up_down_param = " ORDER BY id DESC "
  209. end
  210. local sql = "SELECT * FROM video_product where 1=1 "..param..up_down_param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  211. skynet.error("sql:",sql)
  212. local res = mysqldtaskbx.Singleton().query(sql)
  213. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param
  214. local total = mysqldtaskbx.Singleton().query(sql)
  215. return true,res,total[1].total
  216. end
  217. --设置书别名
  218. function M.set_video_product_alias_name(msg_body)
  219. local isok ,key = tools.checkData({"id","alias_name"},msg_body)
  220. if not isok then
  221. return false,string.format("缺少字段: %s.", key)
  222. end
  223. local sql = string.format("UPDATE `video_product` SET alias_name = '%s' WHERE id = %d ",
  224. msg_body.alias_name,msg_body.id)
  225. local res = mysqldtaskbx.Singleton().query(sql)
  226. return true,{}
  227. end
  228. --设置书类型(长篇,短片)
  229. function M.set_video_product_genre(msg_body)
  230. local isok ,key = tools.checkData({"genre","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 genre = %d WHERE id IN (%s) ",msg_body.genre,idString)
  236. mysqldtaskbx.Singleton().query(sql)
  237. return true, {}
  238. end
  239. --设置推荐章节
  240. function M.set_video_product_start_chapter(msg_body)
  241. local isok ,key = tools.checkData({"start_chapter","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 start_chapter = %d WHERE id IN (%s) ",msg_body.start_chapter,idString)
  247. mysqldtaskbx.Singleton().query(sql)
  248. return true, {}
  249. end
  250. --设置书状态
  251. function M.set_status(msg_body)
  252. local isok ,key = tools.checkData({"status","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 status = %d WHERE id IN (%s) ",msg_body.status,idString)
  258. mysqldtaskbx.Singleton().query(sql)
  259. return true, {}
  260. end
  261. --设置推荐付费
  262. function M.set_seq_num(msg_body)
  263. local isok ,key = tools.checkData({"seq_num","id_list"},msg_body)
  264. if not isok then
  265. return false,string.format("缺少字段: %s.", key)
  266. end
  267. local idString = table.concat(msg_body.id_list, ",")
  268. local sql = string.format("UPDATE video_product SET seq_num = %d WHERE id IN (%s) ",msg_body.seq_num,idString)
  269. mysqldtaskbx.Singleton().query(sql)
  270. return true, {}
  271. end
  272. --设置 专辑链接 album_link
  273. function M.set_album_link(msg_body)
  274. local isok ,key = tools.checkData({"album_link","id_list"},msg_body)
  275. if not isok then
  276. return false,string.format("缺少字段: %s.", key)
  277. end
  278. local idString = table.concat(msg_body.id_list, ",")
  279. local sql = string.format("UPDATE video_product SET album_link = '%s' WHERE id IN (%s) ",msg_body.album_link,idString)
  280. mysqldtaskbx.Singleton().query(sql)
  281. return true, {}
  282. end
  283. --设置上架时间
  284. function M.set_list_time(msg_body)
  285. local isok ,key = tools.checkData({"list_time","id_list"},msg_body)
  286. if not isok then
  287. return false,string.format("缺少字段: %s.", key)
  288. end
  289. local idString = table.concat(msg_body.id_list, ",")
  290. local sql = string.format("UPDATE video_product SET list_time = '%s' WHERE id IN (%s) ",msg_body.list_time,idString)
  291. mysqldtaskbx.Singleton().query(sql)
  292. return true, {}
  293. end
  294. --设置书优先级
  295. function M.set_top(msg_body)
  296. local isok ,key = tools.checkData({"expired_time","id_list"},msg_body)
  297. if not isok then
  298. return false,string.format("缺少字段: %s.", key)
  299. end
  300. -- local current_time = os.date("%Y-%m-%d %H:%M:%S")
  301. local sql = ""
  302. local isok,res;
  303. for i = 1, #msg_body.id_list, 1 do
  304. local id = msg_body.id_list[i]
  305. sql = string.format("SELECT * FROM video_product where id = %d ", id)
  306. res = mysqldtaskbx.Singleton().query(sql)
  307. if #res >0 then
  308. local publish_time = res[1].publish_time
  309. local y_publish_time = res[1].y_publish_time
  310. if y_publish_time~=nil then
  311. publish_time = y_publish_time
  312. end
  313. 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)
  314. mysqldtaskbx.Singleton().query(sql)
  315. end
  316. end
  317. -- local idString = table.concat(msg_body.id_list, ",")
  318. -- local sql = string.format("UPDATE video_product SET is_top = 1 , expired_time = '%s' WHERE id IN (%s) ",msg_body.expired_time,idString)
  319. -- mysqldtaskbx.Singleton().query(sql)
  320. return true, {}
  321. end
  322. --掌阅匹配专辑链接
  323. function M.zy_match_album_link_by_name(msg_body)
  324. local isok ,key = tools.checkData({"list"},msg_body)
  325. if not isok then
  326. return false,string.format("缺少字段: %s.", key)
  327. end
  328. local sql = ""
  329. local res = nil
  330. local notFindList = {}
  331. for i = 1, #msg_body.list, 1 do
  332. local item = msg_body.list[i]
  333. local name = item.name
  334. local album_link = item.album_link
  335. sql = string.format("SELECT * FROM video_product where product_name = '%s' AND book_platform = 7 LIMIT 1 ",name)
  336. res = mysqldtaskbx.Singleton().query(sql)
  337. if #res>0 then
  338. sql = string.format("UPDATE video_product SET album_link = '%s' WHERE id = %d ",album_link,res[1].id)
  339. mysqldtaskbx.Singleton().query(sql)
  340. else
  341. table.insert(notFindList,#notFindList+1,name)
  342. end
  343. end
  344. return true, notFindList
  345. end
  346. --添加书籍数据
  347. function M.add_book_data(msg_body)
  348. local isok ,key = tools.checkData({"list_time","free_time","album_link","fee_mode","episode_price","seq_num","status","tg_platform_id","product_id","product_name"
  349. ,"genre","is_store","start_chapter","publish_time"},msg_body)
  350. if not isok then
  351. return false,string.format("缺少字段: %s.", key)
  352. end
  353. local sql ;
  354. local isok,res;
  355. local sql = string.format("SELECT * FROM video_product where product_id = '%s' LIMIT 1 ",msg_body.product_id)
  356. res = mysqldtaskbx.Singleton().query(sql)
  357. if #res>0 then
  358. return false,"书籍已存在!"
  359. end
  360. sql = string.format("INSERT INTO `video_product` (list_time,free_time,album_link,fee_mode,episode_price,seq_num,status,book_platform,product_id, product_name, genre,is_store,is_auto,start_chapter,publish_time) VALUES ('%s','%s','%s',%d,%d,%d,%d,%d,'%s','%s',%d,%d,0,%d,'%s')",
  361. msg_body.list_time,
  362. msg_body.free_time,
  363. msg_body.album_link,
  364. msg_body.fee_mode,
  365. msg_body.episode_price,
  366. msg_body.seq_num,
  367. msg_body.status,
  368. msg_body.tg_platform_id,
  369. msg_body.product_id,
  370. msg_body.product_name,
  371. msg_body.genre,msg_body.is_store,msg_body.start_chapter,msg_body.publish_time)
  372. res = mysqldtaskbx.Singleton().query(sql)
  373. tools.dump(res)
  374. return true,{}
  375. end
  376. function mysqldtaskbx.start()
  377. local function on_connect(db)
  378. db:query("set charset utf8mb4");
  379. end
  380. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  381. db = mysql.connect{
  382. host=conf.ip,
  383. port=conf.port,
  384. database=conf.db,
  385. user=conf.user,
  386. password=conf.password,
  387. charset="utf8mb4",
  388. max_packet_size = 1024 * 1024,
  389. on_connect = on_connect
  390. }
  391. if not db then
  392. skynet.error("mysql connect fail")
  393. end
  394. end
  395. function mysqldtaskbx.Singleton()
  396. if db == nil then
  397. mysqldtaskbx.start()
  398. end
  399. return mysqldtaskbx
  400. end
  401. function mysqldtaskbx.query(sql)
  402. return db:query(sql)
  403. end
  404. return M