video_product.lua 15 KB

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