tg_main.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. local mysql = require "skynet.db.mysql"
  8. local config = require "run_config"
  9. local mysqldtaskbx = {}
  10. local db
  11. function M.getOpenMainList()
  12. local sql = string.format("select * from `tg_main` where running_status = 1 ")
  13. local isok,res;
  14. res = mysqldbx.query(sql)
  15. if #res <= 0 then
  16. return true , {}
  17. end
  18. return true, res
  19. end
  20. --page_size 是你想要在每页中显示的记录数。
  21. --page_number 页数
  22. --获取所有主体
  23. function M.getMainList()
  24. local sql = string.format("select * from `tg_main` ")
  25. local isok,res;
  26. res = mysqldbx.query(sql)
  27. if #res <= 0 then
  28. return true , {}
  29. end
  30. return true, res
  31. end
  32. -- `main_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '主体名称',
  33. -- `app_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '小程序ID',
  34. -- `running_status` tinyint(1) DEFAULT '1' COMMENT '运行状态 0-停止 1-运行中',
  35. -- `ad_quantity` int NOT NULL DEFAULT '40' COMMENT '广告数量',
  36. -- `cpa_bid` bigint NOT NULL DEFAULT '1750' COMMENT 'CPA出价(分), 默认17.5元',
  37. -- `bid_type` tinyint(1) DEFAULT '1' COMMENT '出价方式 0-常规 1-最大化 2 两种存储',
  38. -- `bid_ratio` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '1' COMMENT '出价方式比例(如 1:2)',
  39. -- `budget` bigint NOT NULL DEFAULT '9000000' COMMENT '预算金额(分), 默认9万元',
  40. -- `tg_platform_id` int NOT NULL DEFAULT '0' COMMENT '平台',
  41. -- `daily_new_limit` int DEFAULT '0' COMMENT '今天新上数量上限',
  42. function M.addMain(msg_body)
  43. local isok ,key = tools.checkData({"no_bid_budget","max_ad_quantity","tg_link_config","tg_platform_id","app_id","main_name","running_status","ad_quantity","cpa_bid","bid_type","bid_ratio","custom_budget","daily_new_limit","valid_time"},msg_body)
  44. if not isok then
  45. return false,string.format("缺少字段: %s.", key)
  46. end
  47. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  48. msg_body.create_time = current_time
  49. local sql = string.format("INSERT INTO `tg_main` (no_bid_budget,max_ad_quantity,tg_link_config,tg_platform_id,app_id,main_name,running_status,ad_quantity,cpa_bid,bid_type,bid_ratio,custom_budget,daily_new_limit,valid_time) VALUES (%d,%d,'%s',%d,'%s','%s',%d,%d,%d,%d,'%s',%d,%d,%d)",
  50. msg_body.no_bid_budget,msg_body.max_ad_quantity,cjson.encode(msg_body.tg_link_config),msg_body.tg_platform_id,msg_body.app_id,msg_body.main_name
  51. ,msg_body.running_status,msg_body.ad_quantity,msg_body.cpa_bid,msg_body.bid_type,msg_body.bid_ratio,msg_body.custom_budget,msg_body.daily_new_limit,msg_body.valid_time)
  52. tools.dump(mysqldbx.query(sql))
  53. skynet.error("sql:",sql)
  54. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="updateMainConfig"}))
  55. pushAddMainMsg(msg_body)
  56. return true
  57. end
  58. function pushAddMainMsg(msg_body)
  59. local sql = string.format("select * from `tg_main` where main_name = '%s' and app_id = '%s' LIMIT 1 ",msg_body.main_name,msg_body.app_id)
  60. local isok,res;
  61. res = mysqldbx.query(sql)
  62. skynet.error("sql:",sql)
  63. tools.dump(res)
  64. if #res > 0 then
  65. local main_info = res[1]
  66. skynet.fork(function ()
  67. -- local getMainInfoByAppid = function(app_id)
  68. -- local temp_list = {}
  69. -- for i = 1, #main_list, 1 do
  70. -- local main_item = main_list[i]
  71. -- if (main_item.app_id==app_id) then
  72. -- table.insert(temp_list,#temp_list+1,main_item)
  73. -- end
  74. -- end
  75. -- return temp_list
  76. -- end
  77. --获取书籍小程序列表
  78. sql = string.format("SELECT * FROM video_applet_product ")
  79. res = mysqldtaskbx.Singleton().query(sql)
  80. local video_applet_product_list = {}
  81. if #res>0 then
  82. for i = 1, #res, 1 do
  83. table.insert(video_applet_product_list,i,res[i])
  84. end
  85. end
  86. local need_create_link_list = {}
  87. -- local isCreateLinkByMainIdAndProductId = function(main_id,product_id)
  88. -- local isCreate = false
  89. -- for i = 1, #video_applet_product_list, 1 do
  90. -- local item = video_applet_product_list[i]
  91. -- if (item.product_id==product_id and item.main_id == main_id) then
  92. -- isCreate = true
  93. -- break
  94. -- end
  95. -- end
  96. -- return isCreate
  97. -- end
  98. --根据小程序id 筛选出所有需要创建的书籍
  99. local video_applet_product_of_main_list = {}
  100. for i = 1, #video_applet_product_list, 1 do
  101. local applet_item = video_applet_product_list[i]
  102. if video_applet_product_of_main_list[applet_item.product_id]==nil then
  103. if applet_item.status == 1 and applet_item.dy_small_applet_app_id == main_info.app_id then
  104. table.insert(video_applet_product_of_main_list,#video_applet_product_of_main_list+1,{product_name=applet_item.product_name,product_id=applet_item.product_id,book_platform=applet_item.book_platform,dy_small_applet_app_id=applet_item.dy_small_applet_app_id})
  105. end
  106. end
  107. end
  108. skynet.error("找到所有小程序!",#video_applet_product_of_main_list)
  109. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="addMain",data={list=video_applet_product_of_main_list,main_info=main_info}}))
  110. end)
  111. end
  112. end
  113. function M.modifyMain(msg_body)
  114. local isok ,key = tools.checkData({"no_bid_budget","max_ad_quantity","tg_link_config","id","tg_platform_id","app_id","main_name","running_status","ad_quantity","cpa_bid","bid_type","bid_ratio","custom_budget","daily_new_limit","valid_time"},msg_body)
  115. if not isok then
  116. return false,string.format("缺少字段: %s.", key)
  117. end
  118. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  119. msg_body.update_time = current_time
  120. local sql = string.format("UPDATE `tg_main` SET no_bid_budget = %d , max_ad_quantity = %d ,tg_platform_id = %d , app_id = '%s' ,main_name = '%s' , update_time = '%s' , running_status = %d ,ad_quantity = %d ,cpa_bid = %d ,bid_type = %d ,bid_ratio = '%s' ,custom_budget = %d ,daily_new_limit = %d ,valid_time = %d ,tg_link_config = '%s' WHERE id = %d ",
  121. msg_body.no_bid_budget,msg_body.max_ad_quantity,msg_body.tg_platform_id,msg_body.app_id,msg_body.main_name,msg_body.update_time,msg_body.running_status,msg_body.ad_quantity,msg_body.cpa_bid,msg_body.bid_type,msg_body.bid_ratio,msg_body.custom_budget,
  122. msg_body.daily_new_limit,msg_body.valid_time,cjson.encode(msg_body.tg_link_config),msg_body.id)
  123. mysqldbx.query(sql)
  124. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="updateMainConfig"}))
  125. return true
  126. end
  127. --配置主体推广链接
  128. function M.tgLinkConfig(msg_body)
  129. local isok ,key = tools.checkData({"id","is_open","huichuan_id","chongzhi_id","kadian_id"},msg_body)
  130. if not isok then
  131. return false,string.format("缺少字段: %s.", key)
  132. end
  133. local sql = string.format("select tg_link_config from `tg_main` WHERE id = %d ",msg_body.id)
  134. local res = mysqldbx.query(sql)
  135. sql = string.format("UPDATE `tg_main` SET tg_link_config = '%s' WHERE id = %d ",
  136. cjson.encode(msg_body),msg_body.id)
  137. mysqldbx.query(sql)
  138. return true, {}
  139. end
  140. function mysqldtaskbx.start()
  141. local function on_connect(db)
  142. db:query("set charset utf8mb4");
  143. end
  144. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  145. db = mysql.connect{
  146. host=conf.ip,
  147. port=conf.port,
  148. database=conf.db,
  149. user=conf.user,
  150. password=conf.password,
  151. charset="utf8mb4",
  152. max_packet_size = 1024 * 1024,
  153. on_connect = on_connect
  154. }
  155. if not db then
  156. error("mysql connect fail")
  157. end
  158. end
  159. function mysqldtaskbx.Singleton()
  160. if db == nil then
  161. mysqldtaskbx.start()
  162. end
  163. return mysqldtaskbx
  164. end
  165. function mysqldtaskbx.query(sql)
  166. return db:query(sql)
  167. end
  168. return M