video_product.lua 17 KB

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