video_product.lua 19 KB

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