video_product.lua 16 KB

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