book_black_list.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. --黑名单
  2. local M = {}
  3. local mysqldbx = require "mysqldbx"
  4. local tools = require "tools"
  5. local skynet = require "skynet"
  6. function M.add_book(msg_body)
  7. local isok ,key = tools.checkData({"product_id","product_name"},msg_body)
  8. if not isok then
  9. return false,string.format("缺少字段: %s.", key)
  10. end
  11. local sql = string.format("SELECT * FROM black_list WHERE product_id = '%s' LIMIT 1", msg_body.product_id)
  12. local isok,res;
  13. res = mysqldbx.query(sql)
  14. if #res > 0 then
  15. return false ,"product_id :"..msg_body.product_id.." 已存在!"
  16. end
  17. sql = string.format("INSERT INTO `black_list` (product_id,product_name) VALUES ('%s','%s')",msg_body.product_id,msg_body.product_name)
  18. mysqldbx.query(sql)
  19. return true, {}
  20. end
  21. function M.delete_book(msg_body)
  22. local isok ,key = tools.checkData({"product_id"},msg_body)
  23. if not isok then
  24. return false,string.format("缺少字段: %s.", key)
  25. end
  26. local sql = string.format("DELETE FROM black_list WHERE product_id = '%s' ",msg_body.product_id)
  27. mysqldbx.query(sql)
  28. return true, {}
  29. end
  30. function M.search_book_list(msg_body)
  31. local isok ,key = tools.checkData({"page_size","page_number","product_id","product_name"},msg_body)
  32. if not isok then
  33. return false,string.format("缺少字段: %s.", key)
  34. end
  35. local page_size = msg_body.page_size
  36. local page_number = msg_body.page_number
  37. local offset = (page_number - 1) * page_size
  38. local isFirst = false
  39. local product_param = ""
  40. if msg_body.product_id~="" then
  41. isFirst = true
  42. product_param =string.format(" product_id = '%s' ",msg_body.product_id)
  43. end
  44. local product_name_param = ""
  45. if msg_body.product_name~="" then
  46. if isFirst ==true then
  47. product_name_param = string.format("AND product_name = '%s' ",msg_body.product_name)
  48. else
  49. isFirst = true
  50. product_name_param = string.format(" product_name = '%s' ",msg_body.product_name)
  51. end
  52. end
  53. local param = product_param..product_name_param
  54. if param ~= "" then
  55. param = " WHERE "..param
  56. end
  57. local sql = "SELECT * FROM black_list "..param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  58. local list = mysqldbx.query(sql)
  59. sql = "SELECT COUNT(*) AS total FROM black_list "..param
  60. local total = mysqldbx.query(sql)
  61. return true,list,total[1].total
  62. end
  63. return M