video_product.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. --搜索书
  12. --tg_platform_id 平台id
  13. --product_id 书id
  14. --product_name 书名
  15. function M.search_book_data(msg_body)
  16. local isok ,key = tools.checkData({
  17. "is_match",
  18. "start_create_at",
  19. "end_create_at",
  20. "product_parent_id",
  21. "is_top",
  22. "status",
  23. "start_publish_time",
  24. "end_publish_time",
  25. "up_or_down_publish_time",
  26. "min_book_word",
  27. "max_book_word",
  28. "alias_name",
  29. "product_name",
  30. "product_id",
  31. "tg_platform_id",
  32. "oce_material_id",
  33. "page_size",
  34. "page_number",
  35. "is_auto",
  36. "stat_cost","min_totalChapterNum","max_totalChapterNum"},msg_body)
  37. if not isok then
  38. return false,string.format("缺少字段: %s.", key)
  39. end
  40. local page_size = msg_body.page_size
  41. local page_number = msg_body.page_number
  42. local offset = (page_number - 1) * page_size
  43. local is_match_param = ""
  44. if msg_body.is_match~="" then
  45. if msg_body.is_match == 1 then
  46. is_match_param = string.format(" AND match_book != 'NONE' ")
  47. else
  48. is_match_param = string.format(" AND match_book='NONE' ")
  49. end
  50. end
  51. local product_parent_id_param = ""
  52. if msg_body.product_parent_id~="" then
  53. product_parent_id_param = string.format(" AND product_parent_id = '%s' ",msg_body.product_parent_id)
  54. end
  55. local is_top_param = ""
  56. if msg_body.is_top~="" then
  57. is_top_param = string.format(" AND is_top = %d ",msg_body.is_top)
  58. end
  59. local status_param = ""
  60. if msg_body.status~="" then
  61. status_param = string.format(" AND status = %d ",msg_body.status)
  62. end
  63. local date_param = ""
  64. if msg_body.start_publish_time~="" and msg_body.end_publish_time~="" then
  65. 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) .. ")) "
  66. end
  67. local create_date_param = ""
  68. if msg_body.start_create_at~="" and msg_body.end_create_at~="" then
  69. 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) .. ")) "
  70. end
  71. local up_or_down_publish_time_param = ""
  72. if msg_body.up_or_down_publish_time~="" then
  73. if(msg_body.up_or_down_publish_time == 0) then
  74. up_or_down_publish_time_param = " publish_time ASC"
  75. else
  76. up_or_down_publish_time_param = " publish_time DESC"
  77. end
  78. end
  79. local word_param = ""
  80. if msg_body.min_book_word~="" and msg_body.max_book_word~="" then
  81. word_param = string.format(" AND CAST(words AS UNSIGNED) >= %f AND CAST(words AS UNSIGNED) <= %f ",msg_body.min_book_word,msg_body.max_book_word)
  82. end
  83. local stat_cost_param = ""
  84. if msg_body.stat_cost~="" then
  85. if(msg_body.stat_cost == 0) then
  86. stat_cost_param = " stat_cost ASC"
  87. else
  88. stat_cost_param = " stat_cost DESC"
  89. end
  90. end
  91. local totalChapterNum_param = ""
  92. if msg_body.min_totalChapterNum~="" and msg_body.max_totalChapterNum~="" then
  93. totalChapterNum_param = string.format(" AND totalChapterNum >= %d AND totalChapterNum <= %d ",msg_body.min_totalChapterNum,msg_body.max_totalChapterNum)
  94. end
  95. local product_param = ""
  96. if msg_body.product_id~="" then
  97. product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
  98. end
  99. local product_name_param = ""
  100. if msg_body.product_name~="" then
  101. product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
  102. end
  103. local tg_platform_param = ""
  104. if msg_body.tg_platform_id~="" then
  105. tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
  106. end
  107. local is_auto_param = ""
  108. if msg_body.is_auto~="" then
  109. is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
  110. end
  111. local is_store_param = ""
  112. if msg_body.is_store~="" then
  113. is_store_param = " AND is_store = "..msg_body.is_store.." "
  114. end
  115. local genre_param = ""
  116. if msg_body.genre~="" then
  117. genre_param = " AND genre = "..msg_body.genre.." "
  118. end
  119. local alias_name_param = ""
  120. if msg_body.alias_name~="" then
  121. alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
  122. end
  123. local param = is_match_param..create_date_param..product_parent_id_param..is_top_param..status_param..date_param..totalChapterNum_param..product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param..word_param;
  124. if stat_cost_param~="" and up_or_down_publish_time_param ~="" then
  125. up_or_down_publish_time_param = " , "..up_or_down_publish_time_param
  126. end
  127. local up_down_param = stat_cost_param..up_or_down_publish_time_param
  128. if up_down_param ~= "" then
  129. up_down_param = " ORDER BY " ..up_down_param
  130. else
  131. up_down_param = "ORDER BY id DESC"
  132. end
  133. local sql = "SELECT * FROM video_product where 1=1 "..param..up_down_param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  134. local res = mysqldtaskbx.Singleton().query(sql)
  135. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param.."ORDER BY id DESC"
  136. local total = mysqldtaskbx.Singleton().query(sql)
  137. return true,res,total[1].total
  138. end
  139. --一键发布搜索
  140. function M.search_book_data_on_main(msg_body)
  141. local isok ,key = tools.checkData({
  142. "start_publish_time",
  143. "end_publish_time",
  144. "alias_name",
  145. "product_name",
  146. "product_id",
  147. "tg_platform_id",
  148. "oce_material_id",
  149. "page_size",
  150. "page_number",
  151. "is_auto",
  152. "stat_cost",
  153. "min_book_word",
  154. "max_book_word",
  155. "min_stat_cost",
  156. "max_stat_cost"},msg_body)
  157. if not isok then
  158. return false,string.format("缺少字段: %s.", key)
  159. end
  160. local page_size = msg_body.page_size
  161. local page_number = msg_body.page_number
  162. local offset = (page_number - 1) * page_size
  163. local status_param = string.format(" AND status = %d ",1)
  164. local date_param = ""
  165. if msg_body.start_publish_time~="" and msg_body.end_publish_time~="" then
  166. 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) .. ")) "
  167. end
  168. local word_param = ""
  169. if msg_body.min_book_word~="" and msg_body.max_book_word~="" then
  170. word_param = string.format(" AND CAST(words AS UNSIGNED) >= %d AND CAST(words AS UNSIGNED) <= %d ",msg_body.min_book_word,msg_body.max_book_word)
  171. end
  172. local stat_cost_param = ""
  173. if msg_body.min_stat_cost~="" and msg_body.max_stat_cost~="" then
  174. stat_cost_param = string.format(" AND stat_cost >= %f AND stat_cost <= %f ",msg_body.min_stat_cost,msg_body.max_stat_cost)
  175. end
  176. local product_param = ""
  177. if msg_body.product_id~="" then
  178. product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
  179. end
  180. local product_name_param = ""
  181. if msg_body.product_name~="" then
  182. product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
  183. end
  184. local tg_platform_param = ""
  185. if msg_body.tg_platform_id~="" then
  186. tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
  187. end
  188. local is_auto_param = ""
  189. if msg_body.is_auto~="" then
  190. is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
  191. end
  192. local is_store_param = ""
  193. if msg_body.is_store~="" then
  194. is_store_param = " AND is_store = "..msg_body.is_store.." "
  195. end
  196. local genre_param = ""
  197. if msg_body.genre~="" then
  198. genre_param = " AND genre = "..msg_body.genre.." "
  199. end
  200. local alias_name_param = ""
  201. if msg_body.alias_name~="" then
  202. alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
  203. end
  204. local param = status_param..date_param..stat_cost_param..word_param..product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param;
  205. 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)
  206. skynet.error("sql:",sql)
  207. local res = mysqldtaskbx.Singleton().query(sql)
  208. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param
  209. local total = mysqldtaskbx.Singleton().query(sql)
  210. return true,res,total[1].total
  211. end
  212. --设置书别名
  213. function M.set_video_product_alias_name(msg_body)
  214. local isok ,key = tools.checkData({"id","alias_name"},msg_body)
  215. if not isok then
  216. return false,string.format("缺少字段: %s.", key)
  217. end
  218. local sql = string.format("UPDATE `video_product` SET alias_name = '%s' WHERE id = %d ",
  219. msg_body.alias_name,msg_body.id)
  220. local res = mysqldtaskbx.Singleton().query(sql)
  221. return true,{}
  222. end
  223. --设置书类型(长篇,短片)
  224. function M.set_video_product_genre(msg_body)
  225. local isok ,key = tools.checkData({"genre","id_list"},msg_body)
  226. if not isok then
  227. return false,string.format("缺少字段: %s.", key)
  228. end
  229. local idString = table.concat(msg_body.id_list, ",")
  230. local sql = string.format("UPDATE video_product SET genre = %d WHERE id IN (%s) ",msg_body.genre,idString)
  231. mysqldtaskbx.Singleton().query(sql)
  232. return true, {}
  233. end
  234. --设置书状态
  235. function M.set_status(msg_body)
  236. local isok ,key = tools.checkData({"status","id_list"},msg_body)
  237. if not isok then
  238. return false,string.format("缺少字段: %s.", key)
  239. end
  240. local idString = table.concat(msg_body.id_list, ",")
  241. local sql = string.format("UPDATE video_product SET status = %d WHERE id IN (%s) ",msg_body.status,idString)
  242. mysqldtaskbx.Singleton().query(sql)
  243. return true, {}
  244. end
  245. --设置书优先级
  246. function M.set_top(msg_body)
  247. local isok ,key = tools.checkData({"expired_time","id_list"},msg_body)
  248. if not isok then
  249. return false,string.format("缺少字段: %s.", key)
  250. end
  251. -- local current_time = os.date("%Y-%m-%d %H:%M:%S")
  252. local sql = ""
  253. local isok,res;
  254. for i = 1, #msg_body.id_list, 1 do
  255. local id = msg_body.id_list[i]
  256. sql = string.format("SELECT * FROM video_product where id = %d ", id)
  257. res = mysqldtaskbx.Singleton().query(sql)
  258. if #res >0 then
  259. local publish_time = res[1].publish_time
  260. local y_publish_time = res[1].y_publish_time
  261. if y_publish_time~=nil then
  262. publish_time = y_publish_time
  263. end
  264. 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)
  265. mysqldtaskbx.Singleton().query(sql)
  266. end
  267. end
  268. -- local idString = table.concat(msg_body.id_list, ",")
  269. -- local sql = string.format("UPDATE video_product SET is_top = 1 , expired_time = '%s' WHERE id IN (%s) ",msg_body.expired_time,idString)
  270. -- mysqldtaskbx.Singleton().query(sql)
  271. return true, {}
  272. end
  273. --设置默认付费章节
  274. function M.set_default_pay_section(msg_body)
  275. local isok ,key = tools.checkData({"default_pay_section","id_list"},msg_body)
  276. if not isok then
  277. return false,string.format("缺少字段: %s.", key)
  278. end
  279. local idString = table.concat(msg_body.id_list, ",")
  280. local sql = string.format("UPDATE video_product SET default_pay_section = %d WHERE id IN (%s) ",msg_body.default_pay_section,idString)
  281. mysqldtaskbx.Singleton().query(sql)
  282. return true, {}
  283. end
  284. --设置价格
  285. function M.set_default_default_price(msg_body)
  286. local isok ,key = tools.checkData({"default_price","id_list"},msg_body)
  287. if not isok then
  288. return false,string.format("缺少字段: %s.", key)
  289. end
  290. local idString = table.concat(msg_body.id_list, ",")
  291. local sql = string.format("UPDATE video_product SET default_price = %f WHERE id IN (%s) ",msg_body.default_price,idString)
  292. mysqldtaskbx.Singleton().query(sql)
  293. return true, {}
  294. end
  295. --设置书籍定价方式
  296. function M.set_fee_unit(msg_body)
  297. local isok ,key = tools.checkData({"fee_unit","id_list"},msg_body)
  298. if not isok then
  299. return false,string.format("缺少字段: %s.", key)
  300. end
  301. local idString = table.concat(msg_body.id_list, ",")
  302. local sql = string.format("UPDATE video_product SET fee_unit = %d WHERE id IN (%s) ",msg_body.fee_unit,idString)
  303. mysqldtaskbx.Singleton().query(sql)
  304. return true, {}
  305. end
  306. --设置上架时间
  307. function M.set_list_time(msg_body)
  308. local isok ,key = tools.checkData({"list_time","id_list"},msg_body)
  309. if not isok then
  310. return false,string.format("缺少字段: %s.", key)
  311. end
  312. local idString = table.concat(msg_body.id_list, ",")
  313. local sql = string.format("UPDATE video_product SET list_time = '%s' WHERE id IN (%s) ",msg_body.list_time,idString)
  314. mysqldtaskbx.Singleton().query(sql)
  315. return true, {}
  316. end
  317. --设置 专辑链接 album_link
  318. function M.set_album_link(msg_body)
  319. local isok ,key = tools.checkData({"album_link","id_list"},msg_body)
  320. if not isok then
  321. return false,string.format("缺少字段: %s.", key)
  322. end
  323. local idString = table.concat(msg_body.id_list, ",")
  324. local sql = string.format("UPDATE video_product SET album_link = '%s' WHERE id IN (%s) ",msg_body.album_link,idString)
  325. mysqldtaskbx.Singleton().query(sql)
  326. return true, {}
  327. end
  328. --同步匹配书籍
  329. function M.sync_match_book_id(msg_body)
  330. local isok ,key = tools.checkData({"match_book","product_id"},msg_body)
  331. if not isok then
  332. return false,string.format("缺少字段: %s.", key)
  333. end
  334. local sql = ""
  335. sql = string.format("SELECT id FROM video_product where product_id = '%s' LIMIT 1 ",msg_body.match_book)
  336. local res = mysqldtaskbx.Singleton().query(sql)
  337. if #res<=0 then
  338. return true,{status=0,msg="请先创建对应的匹配书籍:"..msg_body.match_book}
  339. end
  340. sql = string.format("UPDATE video_product SET match_book = '%s' WHERE product_id = '%s' ",msg_body.match_book,msg_body.product_id)
  341. mysqldtaskbx.Singleton().query(sql)
  342. return true, {}
  343. end
  344. --添加书籍数据
  345. function M.add_book_data(msg_body)
  346. local isok ,key = tools.checkData({"list_time","words","author","fee_unit","default_price","default_pay_section","publish_time","status","product_parent_id","tg_platform_id","product_id","product_name"
  347. ,"genre","is_store"},msg_body)
  348. if not isok then
  349. return false,string.format("缺少字段: %s.", key)
  350. end
  351. local album_link = ""
  352. if msg_body.album_link~=nil then
  353. album_link = msg_body.album_link
  354. end
  355. local match_book = "NONE"
  356. if msg_body.match_book~=nil then
  357. match_book = msg_body.match_book
  358. end
  359. local totalChapterNum = 0
  360. if msg_body.totalChapterNum~=nil then
  361. totalChapterNum = msg_body.totalChapterNum
  362. end
  363. local sql ;
  364. local isok,res;
  365. local sql = string.format("SELECT id FROM video_product where product_id = '%s' LIMIT 1 ",msg_body.product_id)
  366. res = mysqldtaskbx.Singleton().query(sql)
  367. if #res>0 then
  368. return false,"书籍已存在!"
  369. end
  370. sql = string.format("INSERT INTO `video_product` (list_time,totalChapterNum,match_book,words,author,album_link,fee_unit,default_price,default_pay_section,publish_time,status,product_parent_id,book_platform,product_id, product_name, genre,is_store,is_auto) VALUES ('%s',%d,'%s','%s','%s','%s',%d,%f,%d,'%s',%d,'%s',%d,'%s','%s',%d,%d,0)",
  371. msg_body.list_time,
  372. totalChapterNum,
  373. match_book,
  374. msg_body.words,
  375. msg_body.author,
  376. album_link,
  377. msg_body.fee_unit,
  378. msg_body.default_price,
  379. msg_body.default_pay_section,
  380. msg_body.publish_time,
  381. msg_body.status,
  382. msg_body.product_parent_id,
  383. msg_body.tg_platform_id,
  384. msg_body.product_id,
  385. msg_body.product_name,
  386. msg_body.genre,msg_body.is_store)
  387. res = mysqldtaskbx.Singleton().query(sql)
  388. return true,{}
  389. end
  390. function mysqldtaskbx.start()
  391. local function on_connect(db)
  392. db:query("set charset utf8mb4");
  393. end
  394. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  395. db = mysql.connect{
  396. host=conf.ip,
  397. port=conf.port,
  398. database=conf.db,
  399. user=conf.user,
  400. password=conf.password,
  401. charset="utf8mb4",
  402. max_packet_size = 1024 * 1024,
  403. on_connect = on_connect
  404. }
  405. if not db then
  406. skynet.error("mysql connect fail")
  407. end
  408. end
  409. function mysqldtaskbx.Singleton()
  410. if db == nil then
  411. mysqldtaskbx.start()
  412. end
  413. return mysqldtaskbx
  414. end
  415. function mysqldtaskbx.query(sql)
  416. return db:query(sql)
  417. end
  418. return M