book_black_list.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. function M.get_all_black_books()
  8. local sql = "SELECT * FROM black_list "
  9. local res = mysqldbx.query(sql)
  10. return true,res
  11. end
  12. function M.add_book(msg_body)
  13. local isok ,key = tools.checkData({"product_id","product_name"},msg_body)
  14. if not isok then
  15. return false,string.format("缺少字段: %s.", key)
  16. end
  17. local sql = string.format("SELECT * FROM black_list WHERE product_id = '%s' LIMIT 1", msg_body.product_id)
  18. local isok,res;
  19. res = mysqldbx.query(sql)
  20. if #res > 0 then
  21. return false ,"product_id :"..msg_body.product_id.." 已存在!"
  22. end
  23. sql = string.format("INSERT INTO `black_list` (product_id,product_name) VALUES ('%s','%s')",msg_body.product_id,msg_body.product_name)
  24. mysqldbx.query(sql)
  25. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="updateBlackBooks"}))
  26. return true, {}
  27. end
  28. function M.delete_book(msg_body)
  29. local isok ,key = tools.checkData({"product_id"},msg_body)
  30. if not isok then
  31. return false,string.format("缺少字段: %s.", key)
  32. end
  33. local sql = string.format("DELETE FROM black_list WHERE product_id = '%s' ",msg_body.product_id)
  34. mysqldbx.query(sql)
  35. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="updateBlackBooks"}))
  36. return true, {}
  37. end
  38. function M.is_black_book(msg_body)
  39. local isok ,key = tools.checkData({"product_id"},msg_body)
  40. if not isok then
  41. return false,string.format("缺少字段: %s.", key)
  42. end
  43. local sql = string.format("SELECT * FROM black_list WHERE product_id = '%s' LIMIT 1", msg_body.product_id)
  44. local isok,res;
  45. res = mysqldbx.query(sql)
  46. return true, {is_black_book = #res > 0}
  47. end
  48. function M.search_book_list(msg_body)
  49. local isok ,key = tools.checkData({"page_size","page_number","product_id","product_name"},msg_body)
  50. if not isok then
  51. return false,string.format("缺少字段: %s.", key)
  52. end
  53. local page_size = msg_body.page_size
  54. local page_number = msg_body.page_number
  55. local offset = (page_number - 1) * page_size
  56. local isFirst = false
  57. local product_param = ""
  58. if msg_body.product_id~="" then
  59. isFirst = true
  60. product_param =string.format(" product_id = '%s' ",msg_body.product_id)
  61. end
  62. local product_name_param = ""
  63. if msg_body.product_name~="" then
  64. if isFirst ==true then
  65. product_name_param = string.format("AND product_name = '%s' ",msg_body.product_name)
  66. else
  67. isFirst = true
  68. product_name_param = string.format(" product_name = '%s' ",msg_body.product_name)
  69. end
  70. end
  71. local param = product_param..product_name_param
  72. if param ~= "" then
  73. param = " WHERE "..param
  74. end
  75. local sql = "SELECT * FROM black_list "..param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  76. local list = mysqldbx.query(sql)
  77. sql = "SELECT COUNT(*) AS total FROM black_list "..param
  78. local total = mysqldbx.query(sql)
  79. return true,list,total[1].total
  80. end
  81. return M