123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- --书库
- local M = {}
- local mysqldbx = require "mysqldbx"
- local tools = require "tools"
- local skynet = require "skynet"
- local cjson = require "cjson"
- local config = require "run_config"
- local mysql = require "skynet.db.mysql"
- local db
- local mysqldtaskbx = {}
- function M.search_xia_jia(msg_body)
- local isok ,key = tools.checkData({"id_list","page_size",
- "page_number",},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 idString = table.concat(msg_body.id_list, ",")
- local sql = string.format("SELECT * FROM video_product WHERE product_id IN (%s)",idString)..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
- local isok,res;
- skynet.error("sql:",sql)
- local res = mysqldtaskbx.Singleton().query(sql)
- sql = string.format("SELECT COUNT(*) AS total FROM video_product WHERE product_id IN (%s)",idString)
- skynet.error("sql:",sql)
- local total = mysqldtaskbx.Singleton().query(sql)
- return true,res,total[1].total
- end
- function M.search_book_data_on_main(msg_body)
- local isok ,key = tools.checkData({
- "alias_name",
- "product_name",
- "product_id",
- "tg_platform_id",
- "oce_material_id",
- "page_size",
- "page_number",
- "is_auto",
- "stat_cost",
- "min_stat_cost",
- "max_stat_cost"},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 stat_cost_param = ""
- if msg_body.min_stat_cost~="" and msg_body.max_stat_cost~="" then
- stat_cost_param = string.format(" AND stat_cost >= %f AND stat_cost <= %f ",msg_body.min_stat_cost,msg_body.max_stat_cost)
- end
- local product_param = ""
- if msg_body.product_id~="" then
- product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
- end
- local product_name_param = ""
- if msg_body.product_name~="" then
- product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
- end
- local tg_platform_param = ""
- if msg_body.tg_platform_id~="" then
- tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
- end
- local is_auto_param = ""
- if msg_body.is_auto~="" then
- is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
- end
- local is_store_param = ""
- if msg_body.is_store~="" then
- is_store_param = " AND is_store = "..msg_body.is_store.." "
- end
- local genre_param = ""
- if msg_body.genre~="" then
- genre_param = " AND genre = "..msg_body.genre.." "
- end
- local alias_name_param = ""
- if msg_body.alias_name~="" then
- alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
- end
- local param = stat_cost_param..product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param;
- local sql = "SELECT * FROM video_product where 1=1 "..param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
-
- skynet.error("sql:",sql)
- local res = mysqldtaskbx.Singleton().query(sql)
- sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param.."ORDER BY id DESC"
- local total = mysqldtaskbx.Singleton().query(sql)
- return true,res,total[1].total
- end
- --搜索书
- --tg_platform_id 平台id
- --product_id 书id
- --product_name 书名
- function M.search_book_data(msg_body)
- local isok ,key = tools.checkData({
- "start_publish_time",
- "end_publish_time",
- "up_or_down_publish_time",
- "alias_name",
- "product_name",
- "product_id",
- "tg_platform_id",
- "oce_material_id",
- "page_size",
- "page_number",
- "is_auto",
- "stat_cost"},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 date_param = ""
- if msg_body.start_publish_time~="" and msg_body.end_publish_time~="" then
- date_param = " AND DATE(publish_time) >= DATE(FROM_UNIXTIME(" .. (msg_body.start_publish_time / 1000) .. ")) AND DATE(publish_time) <= DATE(FROM_UNIXTIME(" .. (msg_body.end_publish_time / 1000) .. ")) "
- end
-
- local up_or_down_publish_time_param = ""
- if msg_body.up_or_down_publish_time~="" then
- if(msg_body.up_or_down_publish_time == 0) then
- up_or_down_publish_time_param = " publish_time ASC"
- else
- up_or_down_publish_time_param = " publish_time DESC"
- end
- end
- local stat_cost_param = ""
- if msg_body.stat_cost~="" then
- if(msg_body.stat_cost == 0) then
- stat_cost_param = " stat_cost ASC"
- else
- stat_cost_param = " stat_cost DESC"
- end
- end
-
- local product_param = ""
- if msg_body.product_id~="" then
- product_param = string.format(" AND product_id = '%s' ",msg_body.product_id)
- end
- local product_name_param = ""
- if msg_body.product_name~="" then
- product_name_param = string.format(" AND ( product_name LIKE CONCAT( '%%%s%%')) ",msg_body.product_name)
- end
- local tg_platform_param = ""
- if msg_body.tg_platform_id~="" then
- tg_platform_param = string.format(" AND book_platform = %d ",msg_body.tg_platform_id)
- end
- local is_auto_param = ""
- if msg_body.is_auto~="" then
- is_auto_param = " AND is_auto = "..msg_body.is_auto.." "
- end
-
- local is_store_param = ""
- if msg_body.is_store~="" then
- is_store_param = " AND is_store = "..msg_body.is_store.." "
- end
-
- local genre_param = ""
- if msg_body.genre~="" then
- genre_param = " AND genre = "..msg_body.genre.." "
- end
-
- local alias_name_param = ""
- if msg_body.alias_name~="" then
- alias_name_param = string.format(" AND ( alias_name LIKE CONCAT( '%%%s%%')) ",msg_body.alias_name)
- end
-
- local param = date_param..product_param..product_name_param..tg_platform_param..is_auto_param..is_store_param..genre_param..alias_name_param;
-
- local up_down_param = ""
-
- if up_or_down_publish_time_param ~= "" then
- up_down_param = up_or_down_publish_time_param
- end
- if stat_cost_param ~= "" then
- if (up_down_param~="") then
- up_down_param = up_down_param.." , "..stat_cost_param
- else
- up_down_param = stat_cost_param
- end
-
- end
- if up_down_param ~= "" then
- up_down_param = " ORDER BY "..up_down_param
- end
- local sql = "SELECT * FROM video_product where 1=1 "..param..up_down_param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
-
- skynet.error("sql:",sql)
- local res = mysqldtaskbx.Singleton().query(sql)
-
- sql = "SELECT COUNT(*) AS total FROM video_product where 1=1 "..param.."ORDER BY id DESC"
-
- local total = mysqldtaskbx.Singleton().query(sql)
-
- return true,res,total[1].total
- end
- --设置书别名
- function M.set_video_product_alias_name(msg_body)
- local isok ,key = tools.checkData({"id","alias_name"},msg_body)
- if not isok then
- return false,string.format("缺少字段: %s.", key)
- end
- local sql = string.format("UPDATE `video_product` SET alias_name = '%s' WHERE id = %d ",
- msg_body.alias_name,msg_body.id)
- local res = mysqldtaskbx.Singleton().query(sql)
- return true,{}
- end
- --设置书类型(长篇,短片)
- function M.set_video_product_genre(msg_body)
- local isok ,key = tools.checkData({"genre","id_list"},msg_body)
- if not isok then
- return false,string.format("缺少字段: %s.", key)
- end
- local idString = table.concat(msg_body.id_list, ",")
- local sql = string.format("UPDATE video_product SET genre = %d WHERE id IN (%s) ",msg_body.genre,idString)
- mysqldtaskbx.Singleton().query(sql)
- return true, {}
- end
- --设置推荐章节
- function M.set_video_product_start_chapter(msg_body)
- local isok ,key = tools.checkData({"start_chapter","id_list"},msg_body)
- if not isok then
- return false,string.format("缺少字段: %s.", key)
- end
- local idString = table.concat(msg_body.id_list, ",")
- local sql = string.format("UPDATE video_product SET start_chapter = %d WHERE id IN (%s) ",msg_body.start_chapter,idString)
- mysqldtaskbx.Singleton().query(sql)
- return true, {}
- end
- --设置书状态
- function M.set_status(msg_body)
- local isok ,key = tools.checkData({"status","id_list"},msg_body)
- if not isok then
- return false,string.format("缺少字段: %s.", key)
- end
- local idString = table.concat(msg_body.id_list, ",")
- local sql = string.format("UPDATE video_product SET status = %d WHERE id IN (%s) ",msg_body.status,idString)
- mysqldtaskbx.Singleton().query(sql)
- return true, {}
- end
- function mysqldtaskbx.start()
- local function on_connect(db)
- db:query("set charset utf8mb4");
- end
- local conf = config.db_cnf.book_server.mysqldb_task_cnf
- db = mysql.connect{
- host=conf.ip,
- port=conf.port,
- database=conf.db,
- user=conf.user,
- password=conf.password,
- charset="utf8mb4",
- max_packet_size = 1024 * 1024,
- on_connect = on_connect
- }
- if not db then
- skynet.error("mysql connect fail")
- end
- end
- function mysqldtaskbx.Singleton()
- if db == nil then
- mysqldtaskbx.start()
- end
- return mysqldtaskbx
- end
- function mysqldtaskbx.query(sql)
- return db:query(sql)
- end
- return M
|