video_product.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "alias_name",
  18. "product_name",
  19. "product_id",
  20. "tg_platform_id",
  21. "oce_material_id",
  22. "page_size",
  23. "page_number","is_auto"},msg_body)
  24. if not isok then
  25. return false,string.format("缺少字段: %s.", key)
  26. end
  27. local page_size = msg_body.page_size
  28. local page_number = msg_body.page_number
  29. local offset = (page_number - 1) * page_size
  30. local product_param = ""
  31. if msg_body.product_id~="" then
  32. product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
  33. end
  34. local product_name_param = ""
  35. if msg_body.product_name~="" then
  36. product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
  37. end
  38. local tg_platform_param = ""
  39. if msg_body.tg_platform_id~="" then
  40. tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
  41. end
  42. local is_auto_param = ""
  43. if msg_body.is_auto~="" then
  44. is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
  45. end
  46. local is_store_param = ""
  47. if msg_body.is_store~="" then
  48. is_store_param = " AND is_store = "..msg_body.is_store.." "
  49. end
  50. local genre_param = ""
  51. if msg_body.genre~="" then
  52. genre_param = " AND genre = "..msg_body.genre.." "
  53. end
  54. local alias_name_param = ""
  55. if msg_body.alias_name~="" then
  56. alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
  57. end
  58. local param = product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param;
  59. local sql = "SELECT * FROM video_product where 1=1 "..param.." ORDER BY id DESC "..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  60. local res = mysqldtaskbx.Singleton().query(sql)
  61. sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param.."ORDER BY id DESC"
  62. local total = mysqldtaskbx.Singleton().query(sql)
  63. return true,res,total[1].total
  64. end
  65. --设置书别名
  66. function M.set_video_product_alias_name(msg_body)
  67. local isok ,key = tools.checkData({"id","alias_name"},msg_body)
  68. if not isok then
  69. return false,string.format("缺少字段: %s.", key)
  70. end
  71. local sql = string.format("UPDATE `video_product` SET alias_name = '%s' WHERE id = %d ",
  72. msg_body.alias_name,msg_body.id)
  73. local res = mysqldtaskbx.Singleton().query(sql)
  74. return true,{}
  75. end
  76. function mysqldtaskbx.start()
  77. local function on_connect(db)
  78. db:query("set charset utf8mb4");
  79. end
  80. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  81. db = mysql.connect{
  82. host=conf.ip,
  83. port=conf.port,
  84. database=conf.db,
  85. user=conf.user,
  86. password=conf.password,
  87. charset="utf8mb4",
  88. max_packet_size = 1024 * 1024,
  89. on_connect = on_connect
  90. }
  91. if not db then
  92. skynet.error("mysql connect fail")
  93. end
  94. end
  95. function mysqldtaskbx.Singleton()
  96. if db == nil then
  97. mysqldtaskbx.start()
  98. end
  99. return mysqldtaskbx
  100. end
  101. function mysqldtaskbx.query(sql)
  102. return db:query(sql)
  103. end
  104. return M