book_black_list.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.who_at_in_black(msg_body)
  49. local isok ,key = tools.checkData({"id_list"},msg_body)
  50. if not isok then
  51. return false,string.format("缺少字段: %s.", key)
  52. end
  53. local idString = table.concat(msg_body.id_list, ",")
  54. local sql = string.format("SELECT * FROM black_list WHERE product_id IN (%s)",idString)
  55. local isok,res;
  56. res = mysqldbx.query(sql)
  57. return true,res
  58. end
  59. function M.search_book_list(msg_body)
  60. local isok ,key = tools.checkData({"page_size","page_number","product_id","product_name"},msg_body)
  61. if not isok then
  62. return false,string.format("缺少字段: %s.", key)
  63. end
  64. local page_size = msg_body.page_size
  65. local page_number = msg_body.page_number
  66. local offset = (page_number - 1) * page_size
  67. local isFirst = false
  68. local product_param = ""
  69. if msg_body.product_id~="" then
  70. isFirst = true
  71. product_param =string.format(" product_id = '%s' ",msg_body.product_id)
  72. end
  73. local product_name_param = ""
  74. if msg_body.product_name~="" then
  75. if isFirst ==true then
  76. product_name_param = string.format("AND product_name = '%s' ",msg_body.product_name)
  77. else
  78. isFirst = true
  79. product_name_param = string.format(" product_name = '%s' ",msg_body.product_name)
  80. end
  81. end
  82. local param = product_param..product_name_param
  83. if param ~= "" then
  84. param = " WHERE "..param
  85. end
  86. local sql = "SELECT * FROM black_list "..param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  87. local list = mysqldbx.query(sql)
  88. sql = "SELECT COUNT(*) AS total FROM black_list "..param
  89. local total = mysqldbx.query(sql)
  90. return true,list,total[1].total
  91. end
  92. return M