video_product.lua 13 KB

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