tg_main.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. function M.getCloseMainList()
  21. local sql = string.format("select * from `tg_main` where running_status = 0 ")
  22. local isok,res;
  23. res = mysqldbx.query(sql)
  24. if #res <= 0 then
  25. return true , {}
  26. end
  27. return true, res
  28. end
  29. function M.getOpenCreateLinkMainList()
  30. local sql = string.format("select * from `tg_main` where is_open_create_link = 1 ")
  31. local isok,res;
  32. res = mysqldbx.query(sql)
  33. if #res <= 0 then
  34. return true , {}
  35. end
  36. return true, res
  37. end
  38. function M.getCloseCreateLinkMainList()
  39. local sql = string.format("select * from `tg_main` where is_open_create_link = 0 ")
  40. local isok,res;
  41. res = mysqldbx.query(sql)
  42. if #res <= 0 then
  43. return true , {}
  44. end
  45. return true, res
  46. end
  47. --page_size 是你想要在每页中显示的记录数。
  48. --page_number 页数
  49. --获取所有主体
  50. function M.getMainList()
  51. local sql = string.format("select * from `tg_main` ")
  52. local isok,res;
  53. res = mysqldbx.query(sql)
  54. if #res <= 0 then
  55. return true , {}
  56. end
  57. return true, res
  58. end
  59. -- `main_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '主体名称',
  60. -- `app_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '小程序ID',
  61. -- `running_status` tinyint(1) DEFAULT '1' COMMENT '运行状态 0-停止 1-运行中',
  62. -- `ad_quantity` int NOT NULL DEFAULT '40' COMMENT '广告数量',
  63. -- `cpa_bid` bigint NOT NULL DEFAULT '1750' COMMENT 'CPA出价(分), 默认17.5元',
  64. -- `bid_type` tinyint(1) DEFAULT '1' COMMENT '出价方式 0-常规 1-最大化 2 两种存储',
  65. -- `bid_ratio` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '1' COMMENT '出价方式比例(如 1:2)',
  66. -- `budget` bigint NOT NULL DEFAULT '9000000' COMMENT '预算金额(分), 默认9万元',
  67. -- `tg_platform_id` int NOT NULL DEFAULT '0' COMMENT '平台',
  68. -- `daily_new_limit` int DEFAULT '0' COMMENT '今天新上数量上限',
  69. function M.addMain(msg_body)
  70. local isok ,key = tools.checkData({"roi_goal","ql_bid","infra_status","pay_type","qm_id","ldy_template_site_id","yw_id","parent_platform_id","platform_shared_id","distributor_id","is_delete","ad_convert_type","aweme_id","tg_app_index","butler_id","mini_program_platform_id","landing_page","valid_timer_time","valid_timer_number","channelId","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","group_id"},msg_body)
  71. if not isok then
  72. return false,string.format("缺少字段: %s.", key)
  73. end
  74. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  75. msg_body.create_time = current_time
  76. local sql = string.format("INSERT INTO `tg_main` (roi_goal,ql_bid,infra_status,pay_type,qm_id,ldy_template_site_id,yw_id,parent_platform_id,platform_shared_id,distributor_id,is_delete,ad_convert_type,aweme_id,tg_app_index,butler_id,mini_program_platform_id,landing_page,valid_timer_time,valid_timer_number,channelId,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,group_id) VALUES (%d,%d,%d,%d,'%s','%s','%s',%d,%d,'%s',%d,'%s','%s', %d, %d, %d,'%s', %d, '%s', '%s',%d, %d, '%s', %d, '%s', '%s', %d, %d, %d,%d, '%s', %d, %d, '%s', '%s')",
  77. msg_body.roi_goal,
  78. msg_body.ql_bid,
  79. msg_body.infra_status,
  80. msg_body.pay_type,
  81. msg_body.qm_id,
  82. msg_body.ldy_template_site_id,
  83. msg_body.yw_id,
  84. msg_body.parent_platform_id,
  85. msg_body.platform_shared_id,
  86. msg_body.distributor_id,
  87. msg_body.is_delete,
  88. msg_body.ad_convert_type,
  89. msg_body.aweme_id,
  90. msg_body.tg_app_index,
  91. msg_body.butler_id,
  92. msg_body.mini_program_platform_id,
  93. msg_body.landing_page,
  94. msg_body.valid_timer_time,
  95. msg_body.valid_timer_number,
  96. msg_body.channelId,
  97. msg_body.no_bid_budget,
  98. msg_body.max_ad_quantity,
  99. cjson.encode(msg_body.tg_link_config),
  100. msg_body.tg_platform_id,
  101. msg_body.app_id,
  102. msg_body.main_name,
  103. msg_body.running_status,
  104. msg_body.ad_quantity,
  105. msg_body.cpa_bid,
  106. msg_body.bid_type,
  107. msg_body.bid_ratio,
  108. msg_body.custom_budget,
  109. msg_body.daily_new_limit,
  110. msg_body.valid_time,
  111. msg_body.group_id)
  112. skynet.error(sql)
  113. tools.dump(mysqldbx.query(sql))
  114. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="updateMainConfig"}))
  115. -- pushAddMainMsg(msg_body)
  116. return true
  117. end
  118. --此主体将书库的书都发布到小程序书籍
  119. function M.syncMain(msg_body)
  120. local isok ,key = tools.checkData({"id"},msg_body)
  121. if not isok then
  122. return false,string.format("缺少字段: %s.", key)
  123. end
  124. local sql = string.format("select * from `tg_main` where id = %d LIMIT 1 ",msg_body.id)
  125. local res = mysqldbx.query(sql)
  126. syncMainAppletProduct(res[1])
  127. return true,{}
  128. end
  129. function syncMainAppletProduct(main_info)
  130. -- local tg_platform_id = main_info.tg_platform_id
  131. -- local app_id = main_info.app_id
  132. -- local sql = string.format("SELECT * FROM video_product where book_platform = %d ",tg_platform_id)
  133. -- local video_product_list = mysqldtaskbx.Singleton().query(sql)
  134. -- function isHaveAppletProdut(product_id)
  135. -- local s = string.format("SELECT * FROM video_applet_product where dy_small_applet_app_id = '%s' and product_id = '%s' and main_id = %d and book_platform = %d LIMIT 1",app_id,product_id,main_info.id,tg_platform_id)
  136. -- local r = mysqldtaskbx.Singleton().query(sql)
  137. -- return #r>0
  138. -- end
  139. -- local need_create_link_list = {}
  140. -- local verify_list = {}
  141. -- for i = 1, #video_product_list, 1 do
  142. -- local product = video_product_list[i]
  143. -- if not isHaveAppletProdut(product.product_id) and not verify_list[product.product_id] then
  144. -- verify_list[product.product_id] = true
  145. -- table.insert(need_create_link_list,#need_create_link_list+1,{product_name=product.product_name,product_id=product.product_id,book_platform=product.book_platform})
  146. -- end
  147. -- end
  148. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="syncMain",data={main_info=main_info}}))
  149. end
  150. function pushAddMainMsg(msg_body)
  151. 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)
  152. local isok,res;
  153. res = mysqldbx.query(sql)
  154. skynet.error("sql:",sql)
  155. tools.dump(res)
  156. if #res > 0 then
  157. local main_info = res[1]
  158. skynet.fork(function ()
  159. -- local getMainInfoByAppid = function(app_id)
  160. -- local temp_list = {}
  161. -- for i = 1, #main_list, 1 do
  162. -- local main_item = main_list[i]
  163. -- if (main_item.app_id==app_id) then
  164. -- table.insert(temp_list,#temp_list+1,main_item)
  165. -- end
  166. -- end
  167. -- return temp_list
  168. -- end
  169. --获取书籍小程序列表
  170. sql = string.format("SELECT * FROM video_applet_product ")
  171. res = mysqldtaskbx.Singleton().query(sql)
  172. local video_applet_product_list = {}
  173. if #res>0 then
  174. for i = 1, #res, 1 do
  175. table.insert(video_applet_product_list,i,res[i])
  176. end
  177. end
  178. local need_create_link_list = {}
  179. -- local isCreateLinkByMainIdAndProductId = function(main_id,product_id)
  180. -- local isCreate = false
  181. -- for i = 1, #video_applet_product_list, 1 do
  182. -- local item = video_applet_product_list[i]
  183. -- if (item.product_id==product_id and item.main_id == main_id) then
  184. -- isCreate = true
  185. -- break
  186. -- end
  187. -- end
  188. -- return isCreate
  189. -- end
  190. --根据小程序id 筛选出所有需要创建的书籍
  191. local video_applet_product_of_main_list = {}
  192. local verify_list = {}
  193. for i = 1, #video_applet_product_list, 1 do
  194. local applet_item = video_applet_product_list[i]
  195. if verify_list[applet_item.product_id]==nil then
  196. if applet_item.status == 1 and applet_item.dy_small_applet_app_id == main_info.app_id then
  197. verify_list[applet_item.product_id] = true
  198. 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})
  199. end
  200. end
  201. end
  202. skynet.error("找到所有小程序!",#video_applet_product_of_main_list)
  203. 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}}))
  204. end)
  205. end
  206. end
  207. function M.modifyMain(msg_body)
  208. local isok ,key = tools.checkData({"roi_goal","ql_bid","infra_status","pay_type","pay_type","qm_id","ldy_template_site_id","yw_id","parent_platform_id","platform_shared_id","distributor_id","is_delete","ad_convert_type","aweme_id","tg_app_index","butler_id","mini_program_platform_id","landing_page","valid_timer_time","valid_timer_number","channelId","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","group_id"},msg_body)
  209. if not isok then
  210. return false,string.format("缺少字段: %s.", key)
  211. end
  212. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  213. msg_body.update_time = current_time
  214. local sql = string.format("UPDATE `tg_main` SET roi_goal = %d , ql_bid = %d, infra_status = %d , pay_type = %d ,qm_id = '%s' , ldy_template_site_id = '%s' , yw_id = '%s' , parent_platform_id = %d , platform_shared_id = %d ,distributor_id = '%s' , is_delete = %d , ad_convert_type = '%s', aweme_id = '%s' ,tg_app_index = %d , butler_id = %d , mini_program_platform_id = %d , landing_page = '%s' , valid_timer_time = %d , valid_timer_number = %d ,channelId = '%s' , 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' ,group_id = '%s' WHERE id = %d ",
  215. msg_body.roi_goal,msg_body.ql_bid,msg_body.infra_status,msg_body.pay_type,msg_body.qm_id,msg_body.ldy_template_site_id,msg_body.yw_id,msg_body.parent_platform_id,msg_body.platform_shared_id,msg_body.distributor_id,msg_body.is_delete,msg_body.ad_convert_type,msg_body.aweme_id,msg_body.tg_app_index,msg_body.butler_id,msg_body.mini_program_platform_id,msg_body.landing_page,tonumber(msg_body.valid_timer_time),tonumber(msg_body.valid_timer_number),msg_body.channelId,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,
  216. tonumber(msg_body.daily_new_limit),tonumber(msg_body.valid_time),cjson.encode(msg_body.tg_link_config),msg_body.group_id,msg_body.id)
  217. tools.dump(mysqldbx.query(sql))
  218. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="updateMainConfig"}))
  219. return true
  220. end
  221. --配置主体推广链接
  222. function M.tgLinkConfig(msg_body)
  223. local isok ,key = tools.checkData({"id","is_open","huichuan_id","chongzhi_id","kadian_id","f_chongzhi_id","site_id"},msg_body)
  224. if not isok then
  225. return false,string.format("缺少字段: %s.", key)
  226. end
  227. local sql = string.format("select tg_link_config from `tg_main` WHERE id = %d ",msg_body.id)
  228. local res = mysqldbx.query(sql)
  229. sql = string.format("UPDATE `tg_main` SET tg_link_config = '%s' WHERE id = %d ",
  230. cjson.encode(msg_body),msg_body.id)
  231. mysqldbx.query(sql)
  232. return true, {}
  233. end
  234. --设置创建链接状态
  235. function M.set_open_create_link_status(msg_body)
  236. local isok ,key = tools.checkData({"id","is_open_create_link"},msg_body)
  237. if not isok then
  238. return false,string.format("缺少字段: %s.", key)
  239. end
  240. local sql = string.format("UPDATE `tg_main` SET is_open_create_link = %d WHERE id = %d ",
  241. msg_body.is_open_create_link,msg_body.id)
  242. mysqldbx.query(sql)
  243. return true, {}
  244. end
  245. --获取主体下的运行总数
  246. function M.get_main_ad_run_number(msg_body)
  247. local isok ,key = tools.checkData({"main_id"},msg_body)
  248. if not isok then
  249. return false,string.format("缺少字段: %s.", key)
  250. end
  251. local sql ="SELECT * FROM advertiser WHERE main_id = " .. msg_body.main_id;
  252. local res = mysqldbx.query(sql)
  253. local all_run_number = 0
  254. for i = 1, #res, 1 do
  255. local run_number = res[i].ad_run_number
  256. all_run_number = all_run_number + run_number
  257. end
  258. local nums = #res
  259. sql = string.format("select * from `tg_main` where id = %d LIMIT 1 ",msg_body.main_id)
  260. res = mysqldbx.query(sql)
  261. local max = nums * res[1].max_ad_quantity
  262. return true,{all_run_number=all_run_number,max=max}
  263. end
  264. function mysqldtaskbx.start()
  265. local function on_connect(db)
  266. db:query("set charset utf8mb4");
  267. end
  268. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  269. db = mysql.connect{
  270. host=conf.ip,
  271. port=conf.port,
  272. database=conf.db,
  273. user=conf.user,
  274. password=conf.password,
  275. charset="utf8mb4",
  276. max_packet_size = 1024 * 1024,
  277. on_connect = on_connect
  278. }
  279. if not db then
  280. error("mysql connect fail")
  281. end
  282. end
  283. function mysqldtaskbx.Singleton()
  284. if db == nil then
  285. mysqldtaskbx.start()
  286. end
  287. return mysqldtaskbx
  288. end
  289. function mysqldtaskbx.query(sql)
  290. return db:query(sql)
  291. end
  292. return M