video_product.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. end
  208. local sql = "SELECT * FROM video_product where 1=1 "..param..up_down_param.." ORDER BY id DESC "..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  209. skynet.error("sql:",sql)
  210. local res = mysqldtaskbx.Singleton().query(sql)
  211. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param
  212. local total = mysqldtaskbx.Singleton().query(sql)
  213. return true,res,total[1].total
  214. end
  215. --设置书别名
  216. function M.set_video_product_alias_name(msg_body)
  217. local isok ,key = tools.checkData({"id","alias_name"},msg_body)
  218. if not isok then
  219. return false,string.format("缺少字段: %s.", key)
  220. end
  221. local sql = string.format("UPDATE `video_product` SET alias_name = '%s' WHERE id = %d ",
  222. msg_body.alias_name,msg_body.id)
  223. local res = mysqldtaskbx.Singleton().query(sql)
  224. return true,{}
  225. end
  226. --设置书类型(长篇,短片)
  227. function M.set_video_product_genre(msg_body)
  228. local isok ,key = tools.checkData({"genre","id_list"},msg_body)
  229. if not isok then
  230. return false,string.format("缺少字段: %s.", key)
  231. end
  232. local idString = table.concat(msg_body.id_list, ",")
  233. local sql = string.format("UPDATE video_product SET genre = %d WHERE id IN (%s) ",msg_body.genre,idString)
  234. mysqldtaskbx.Singleton().query(sql)
  235. return true, {}
  236. end
  237. --设置推荐章节
  238. function M.set_video_product_start_chapter(msg_body)
  239. local isok ,key = tools.checkData({"start_chapter","id_list"},msg_body)
  240. if not isok then
  241. return false,string.format("缺少字段: %s.", key)
  242. end
  243. local idString = table.concat(msg_body.id_list, ",")
  244. local sql = string.format("UPDATE video_product SET start_chapter = %d WHERE id IN (%s) ",msg_body.start_chapter,idString)
  245. mysqldtaskbx.Singleton().query(sql)
  246. return true, {}
  247. end
  248. --设置书状态
  249. function M.set_status(msg_body)
  250. local isok ,key = tools.checkData({"status","id_list"},msg_body)
  251. if not isok then
  252. return false,string.format("缺少字段: %s.", key)
  253. end
  254. local idString = table.concat(msg_body.id_list, ",")
  255. local sql = string.format("UPDATE video_product SET status = %d WHERE id IN (%s) ",msg_body.status,idString)
  256. mysqldtaskbx.Singleton().query(sql)
  257. return true, {}
  258. end
  259. --设置推荐付费
  260. function M.set_seq_num(msg_body)
  261. local isok ,key = tools.checkData({"seq_num","id_list"},msg_body)
  262. if not isok then
  263. return false,string.format("缺少字段: %s.", key)
  264. end
  265. local idString = table.concat(msg_body.id_list, ",")
  266. local sql = string.format("UPDATE video_product SET seq_num = %d WHERE id IN (%s) ",msg_body.seq_num,idString)
  267. mysqldtaskbx.Singleton().query(sql)
  268. return true, {}
  269. end
  270. --设置 专辑链接 album_link
  271. function M.set_album_link(msg_body)
  272. local isok ,key = tools.checkData({"album_link","id_list"},msg_body)
  273. if not isok then
  274. return false,string.format("缺少字段: %s.", key)
  275. end
  276. local idString = table.concat(msg_body.id_list, ",")
  277. local sql = string.format("UPDATE video_product SET album_link = '%s' WHERE id IN (%s) ",msg_body.album_link,idString)
  278. mysqldtaskbx.Singleton().query(sql)
  279. return true, {}
  280. end
  281. --设置书优先级
  282. function M.set_top(msg_body)
  283. local isok ,key = tools.checkData({"expired_time","id_list"},msg_body)
  284. if not isok then
  285. return false,string.format("缺少字段: %s.", key)
  286. end
  287. -- local current_time = os.date("%Y-%m-%d %H:%M:%S")
  288. local sql = ""
  289. local isok,res;
  290. for i = 1, #msg_body.id_list, 1 do
  291. local id = msg_body.id_list[i]
  292. sql = string.format("SELECT * FROM video_product where id = %d ", id)
  293. res = mysqldtaskbx.Singleton().query(sql)
  294. if #res >0 then
  295. local publish_time = res[1].publish_time
  296. local y_publish_time = res[1].y_publish_time
  297. if y_publish_time~=nil then
  298. publish_time = y_publish_time
  299. end
  300. 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)
  301. mysqldtaskbx.Singleton().query(sql)
  302. end
  303. end
  304. -- local idString = table.concat(msg_body.id_list, ",")
  305. -- local sql = string.format("UPDATE video_product SET is_top = 1 , expired_time = '%s' WHERE id IN (%s) ",msg_body.expired_time,idString)
  306. -- mysqldtaskbx.Singleton().query(sql)
  307. return true, {}
  308. end
  309. --掌阅匹配专辑链接
  310. function M.zy_match_album_link_by_name(msg_body)
  311. local isok ,key = tools.checkData({"list"},msg_body)
  312. if not isok then
  313. return false,string.format("缺少字段: %s.", key)
  314. end
  315. local sql = ""
  316. local res = nil
  317. local notFindList = {}
  318. for i = 1, #msg_body.list, 1 do
  319. local item = msg_body.list[i]
  320. local name = item.name
  321. local album_link = item.album_link
  322. sql = string.format("SELECT * FROM video_product where product_name = '%s' AND book_platform = 7 LIMIT 1 ",name)
  323. res = mysqldtaskbx.Singleton().query(sql)
  324. if #res>0 then
  325. sql = string.format("UPDATE video_product SET album_link = '%s' WHERE id = %d ",album_link,res[1].id)
  326. mysqldtaskbx.Singleton().query(sql)
  327. else
  328. table.insert(notFindList,#notFindList+1,name)
  329. end
  330. end
  331. return true, notFindList
  332. end
  333. --添加书籍数据
  334. function M.add_book_data(msg_body)
  335. local isok ,key = tools.checkData({"free_time","album_link","fee_mode","episode_price","seq_num","status","tg_platform_id","product_id","product_name"
  336. ,"genre","is_store","start_chapter","publish_time"},msg_body)
  337. if not isok then
  338. return false,string.format("缺少字段: %s.", key)
  339. end
  340. local sql ;
  341. local isok,res;
  342. local sql = string.format("SELECT * FROM video_product where product_id = '%s' LIMIT 1 ",msg_body.product_id)
  343. res = mysqldtaskbx.Singleton().query(sql)
  344. if #res>0 then
  345. return false,"书籍已存在!"
  346. end
  347. sql = string.format("INSERT INTO `video_product` (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',%d,%d,%d,%d,%d,'%s','%s',%d,%d,0,%d,'%s')",
  348. msg_body.free_time,
  349. msg_body.album_link,
  350. msg_body.fee_mode,
  351. msg_body.episode_price,
  352. msg_body.seq_num,
  353. msg_body.status,
  354. msg_body.tg_platform_id,
  355. msg_body.product_id,
  356. msg_body.product_name,
  357. msg_body.genre,msg_body.is_store,msg_body.start_chapter,msg_body.publish_time)
  358. res = mysqldtaskbx.Singleton().query(sql)
  359. tools.dump(res)
  360. return true,{}
  361. end
  362. function mysqldtaskbx.start()
  363. local function on_connect(db)
  364. db:query("set charset utf8mb4");
  365. end
  366. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  367. db = mysql.connect{
  368. host=conf.ip,
  369. port=conf.port,
  370. database=conf.db,
  371. user=conf.user,
  372. password=conf.password,
  373. charset="utf8mb4",
  374. max_packet_size = 1024 * 1024,
  375. on_connect = on_connect
  376. }
  377. if not db then
  378. skynet.error("mysql connect fail")
  379. end
  380. end
  381. function mysqldtaskbx.Singleton()
  382. if db == nil then
  383. mysqldtaskbx.start()
  384. end
  385. return mysqldtaskbx
  386. end
  387. function mysqldtaskbx.query(sql)
  388. return db:query(sql)
  389. end
  390. return M