--黑名单 local M = {} local mysqldbx = require "mysqldbx" local tools = require "tools" local skynet = require "skynet" function M.add_book(msg_body) local isok ,key = tools.checkData({"product_id","product_name"},msg_body) if not isok then return false,string.format("缺少字段: %s.", key) end local sql = string.format("SELECT * FROM black_list WHERE product_id = '%s' LIMIT 1", msg_body.product_id) local isok,res; res = mysqldbx.query(sql) if #res > 0 then return false ,"product_id :"..msg_body.product_id.." 已存在!" end sql = string.format("INSERT INTO `black_list` (product_id,product_name) VALUES ('%s','%s')",msg_body.product_id,msg_body.product_name) mysqldbx.query(sql) return true, {} end function M.delete_book(msg_body) local isok ,key = tools.checkData({"product_id"},msg_body) if not isok then return false,string.format("缺少字段: %s.", key) end local sql = string.format("DELETE FROM black_list WHERE product_id = '%s' ",msg_body.product_id) mysqldbx.query(sql) return true, {} end function M.search_book_list(msg_body) local isok ,key = tools.checkData({"page_size","page_number","product_id","product_name"},msg_body) if not isok then return false,string.format("缺少字段: %s.", key) end local page_size = msg_body.page_size local page_number = msg_body.page_number local offset = (page_number - 1) * page_size local isFirst = false local product_param = "" if msg_body.product_id~="" then isFirst = true product_param =string.format(" product_id = '%s' ",msg_body.product_id) end local product_name_param = "" if msg_body.product_name~="" then if isFirst ==true then product_name_param = string.format("AND product_name = '%s' ",msg_body.product_name) else isFirst = true product_name_param = string.format(" product_name = '%s' ",msg_body.product_name) end end local param = product_param..product_name_param if param ~= "" then param = " WHERE "..param end local sql = "SELECT * FROM black_list "..param..string.format(" LIMIT %d OFFSET %d ",page_size, offset) local list = mysqldbx.query(sql) sql = "SELECT COUNT(*) AS total FROM black_list "..param local total = mysqldbx.query(sql) return true,list,total[1].total end return M