video_product.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. "alias_name",
  33. "product_name",
  34. "product_id",
  35. "tg_platform_id",
  36. "oce_material_id",
  37. "page_size",
  38. "page_number",
  39. "is_auto",
  40. "stat_cost",
  41. "min_stat_cost",
  42. "max_stat_cost"},msg_body)
  43. if not isok then
  44. return false,string.format("缺少字段: %s.", key)
  45. end
  46. local page_size = msg_body.page_size
  47. local page_number = msg_body.page_number
  48. local offset = (page_number - 1) * page_size
  49. local stat_cost_param = ""
  50. if msg_body.min_stat_cost~="" and msg_body.max_stat_cost~="" then
  51. stat_cost_param = string.format(" AND stat_cost >= %f AND stat_cost <= %f ",msg_body.min_stat_cost,msg_body.max_stat_cost)
  52. end
  53. local product_param = ""
  54. if msg_body.product_id~="" then
  55. product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
  56. end
  57. local product_name_param = ""
  58. if msg_body.product_name~="" then
  59. product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
  60. end
  61. local tg_platform_param = ""
  62. if msg_body.tg_platform_id~="" then
  63. tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
  64. end
  65. local is_auto_param = ""
  66. if msg_body.is_auto~="" then
  67. is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
  68. end
  69. local is_store_param = ""
  70. if msg_body.is_store~="" then
  71. is_store_param = " AND is_store = "..msg_body.is_store.." "
  72. end
  73. local genre_param = ""
  74. if msg_body.genre~="" then
  75. genre_param = " AND genre = "..msg_body.genre.." "
  76. end
  77. local alias_name_param = ""
  78. if msg_body.alias_name~="" then
  79. alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
  80. end
  81. local param = stat_cost_param..product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param;
  82. local sql = "SELECT * FROM video_product where 1=1 "..param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  83. skynet.error("sql:",sql)
  84. local res = mysqldtaskbx.Singleton().query(sql)
  85. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param.."ORDER BY id DESC"
  86. local total = mysqldtaskbx.Singleton().query(sql)
  87. return true,res,total[1].total
  88. end
  89. --搜索书
  90. --tg_platform_id 平台id
  91. --product_id 书id
  92. --product_name 书名
  93. function M.search_book_data(msg_body)
  94. local isok ,key = tools.checkData({
  95. "alias_name",
  96. "product_name",
  97. "product_id",
  98. "tg_platform_id",
  99. "oce_material_id",
  100. "page_size",
  101. "page_number",
  102. "is_auto",
  103. "stat_cost"},msg_body)
  104. if not isok then
  105. return false,string.format("缺少字段: %s.", key)
  106. end
  107. local page_size = msg_body.page_size
  108. local page_number = msg_body.page_number
  109. local offset = (page_number - 1) * page_size
  110. local stat_cost_param = ""
  111. if msg_body.stat_cost~="" then
  112. if(msg_body.stat_cost == 0) then
  113. stat_cost_param = " stat_cost ASC"
  114. else
  115. stat_cost_param = " stat_cost DESC"
  116. end
  117. end
  118. local product_param = ""
  119. if msg_body.product_id~="" then
  120. product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
  121. end
  122. local product_name_param = ""
  123. if msg_body.product_name~="" then
  124. product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
  125. end
  126. local tg_platform_param = ""
  127. if msg_body.tg_platform_id~="" then
  128. tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
  129. end
  130. local is_auto_param = ""
  131. if msg_body.is_auto~="" then
  132. is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
  133. end
  134. local is_store_param = ""
  135. if msg_body.is_store~="" then
  136. is_store_param = " AND is_store = "..msg_body.is_store.." "
  137. end
  138. local genre_param = ""
  139. if msg_body.genre~="" then
  140. genre_param = " AND genre = "..msg_body.genre.." "
  141. end
  142. local alias_name_param = ""
  143. if msg_body.alias_name~="" then
  144. alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
  145. end
  146. local param = product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param;
  147. local up_down_param = stat_cost_param
  148. if up_down_param ~= "" then
  149. up_down_param = " ORDER BY " ..up_down_param
  150. else
  151. up_down_param = "ORDER BY id DESC"
  152. end
  153. local sql = "SELECT * FROM video_product where 1=1 "..param..up_down_param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  154. local res = mysqldtaskbx.Singleton().query(sql)
  155. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param.."ORDER BY id DESC"
  156. local total = mysqldtaskbx.Singleton().query(sql)
  157. return true,res,total[1].total
  158. end
  159. --设置书别名
  160. function M.set_video_product_alias_name(msg_body)
  161. local isok ,key = tools.checkData({"id","alias_name"},msg_body)
  162. if not isok then
  163. return false,string.format("缺少字段: %s.", key)
  164. end
  165. local sql = string.format("UPDATE `video_product` SET alias_name = '%s' WHERE id = %d ",
  166. msg_body.alias_name,msg_body.id)
  167. local res = mysqldtaskbx.Singleton().query(sql)
  168. return true,{}
  169. end
  170. --设置书类型(长篇,短片)
  171. function M.set_video_product_genre(msg_body)
  172. local isok ,key = tools.checkData({"genre","id_list"},msg_body)
  173. if not isok then
  174. return false,string.format("缺少字段: %s.", key)
  175. end
  176. local idString = table.concat(msg_body.id_list, ",")
  177. local sql = string.format("UPDATE video_product SET genre = %d WHERE id IN (%s) ",msg_body.genre,idString)
  178. mysqldtaskbx.Singleton().query(sql)
  179. return true, {}
  180. end
  181. function mysqldtaskbx.start()
  182. local function on_connect(db)
  183. db:query("set charset utf8mb4");
  184. end
  185. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  186. db = mysql.connect{
  187. host=conf.ip,
  188. port=conf.port,
  189. database=conf.db,
  190. user=conf.user,
  191. password=conf.password,
  192. charset="utf8mb4",
  193. max_packet_size = 1024 * 1024,
  194. on_connect = on_connect
  195. }
  196. if not db then
  197. skynet.error("mysql connect fail")
  198. end
  199. end
  200. function mysqldtaskbx.Singleton()
  201. if db == nil then
  202. mysqldtaskbx.start()
  203. end
  204. return mysqldtaskbx
  205. end
  206. function mysqldtaskbx.query(sql)
  207. return db:query(sql)
  208. end
  209. return M