tg_main.lua 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. function M.search(msg_body)
  60. local isok ,key = tools.checkData({"is_group_record","tf_time_interval","mini_program_platform_id","is_comment","material_rule_id","parent_main_id","tg_day","id","ads_operation_status","statistics_platform_id","converted_time_duration","cbo_status","project_type","oce_product_id",
  61. "oce_product_platform_id",
  62. "delivery_mode","material_platform_id","wx_miniapp_create_status","template_bid","cpa_bid","page_size","page_number","main_name","running_status","custom_budget","pay_type","tg_platform_id","butler_id","tg_app_index","is_open_create_link","is_delete"},msg_body)
  63. if not isok then
  64. return false,string.format("缺少字段: %s.", key)
  65. end
  66. local page_size = msg_body.page_size
  67. local page_number = msg_body.page_number
  68. local offset = (page_number - 1) * page_size
  69. local is_group_record_param = ""
  70. if msg_body.is_group_record~="" then
  71. is_group_record_param = " AND is_group_record = "..msg_body.is_group_record
  72. end
  73. local tf_time_interval_param = ""
  74. if msg_body.tf_time_interval~="" then
  75. tf_time_interval_param = " AND tf_time_interval = "..msg_body.tf_time_interval
  76. end
  77. local mini_program_platform_id_param = ""
  78. if msg_body.mini_program_platform_id~="" then
  79. mini_program_platform_id_param = " AND mini_program_platform_id = "..msg_body.mini_program_platform_id
  80. end
  81. local is_comment_param = ""
  82. if msg_body.is_comment~="" then
  83. is_comment_param = " AND is_comment = "..msg_body.is_comment
  84. end
  85. local material_rule_id_param = ""
  86. if msg_body.material_rule_id~="" then
  87. material_rule_id_param = " AND material_rule_id = "..msg_body.material_rule_id
  88. end
  89. local parent_main_id_param = ""
  90. if msg_body.parent_main_id~="" then
  91. parent_main_id_param = " AND parent_main_id = "..msg_body.parent_main_id
  92. end
  93. local tg_day_param = ""
  94. if msg_body.tg_day~="" then
  95. tg_day_param = " AND tg_day = "..msg_body.tg_day
  96. end
  97. local id_param = ""
  98. if msg_body.id~="" then
  99. id_param = " AND id = "..msg_body.id
  100. end
  101. local ads_operation_status_param = ""
  102. if msg_body.ads_operation_status~="" then
  103. ads_operation_status_param = string.format(" AND ads_operation_status = '%s' ",msg_body.ads_operation_status)
  104. end
  105. local statistics_platform_id_param = ""
  106. if msg_body.statistics_platform_id~="" then
  107. statistics_platform_id_param = string.format(" AND statistics_platform_id = %d ",msg_body.statistics_platform_id)
  108. end
  109. local converted_time_duration_param = ""
  110. if msg_body.converted_time_duration~="" then
  111. converted_time_duration_param = string.format(" AND converted_time_duration = '%s' ",msg_body.converted_time_duration)
  112. end
  113. local infra_status_param = ""
  114. if msg_body.infra_status~="" then
  115. infra_status_param = string.format(" AND infra_status = %d ",msg_body.infra_status)
  116. end
  117. local cbo_script_status_param = ""
  118. if msg_body.cbo_script_status~="" then
  119. cbo_script_status_param = string.format(" AND cbo_script_status = %d ",msg_body.cbo_script_status)
  120. end
  121. local cbo_status_param = ""
  122. if msg_body.cbo_status~="" then
  123. cbo_status_param = string.format(" AND cbo_status = %d ",msg_body.cbo_status)
  124. end
  125. local project_type_param = ""
  126. if msg_body.project_type~="" then
  127. project_type_param = string.format(" AND project_type = %d ",msg_body.project_type)
  128. end
  129. local oce_product_id_param = ""
  130. if msg_body.oce_product_id~="" and msg_body.oce_product_id~=nil then
  131. oce_product_id_param = string.format(" AND oce_product_id = '%s' ",msg_body.oce_product_id)
  132. end
  133. local oce_product_platform_id_param = ""
  134. if msg_body.oce_product_platform_id~="" and msg_body.oce_oce_product_platform_idproduct_id~=nil then
  135. oce_product_platform_id_param = string.format(" AND oce_product_platform_id = '%s' ",msg_body.oce_product_platform_id)
  136. end
  137. local delivery_mode_param = ""
  138. if msg_body.delivery_mode~="" and msg_body.delivery_mode~=nil then
  139. delivery_mode_param = string.format(" AND delivery_mode = '%s' ",msg_body.delivery_mode)
  140. end
  141. local main_name_param = ""
  142. if msg_body.main_name~="" then
  143. main_name_param = string.format(" AND ( main_name LIKE CONCAT( '%%%s%%')) ",msg_body.main_name)
  144. end
  145. local wx_miniapp_create_status_param = ""
  146. if msg_body.wx_miniapp_create_status~="" then
  147. wx_miniapp_create_status_param = string.format(" AND wx_miniapp_create_status = %d ",msg_body.wx_miniapp_create_status)
  148. end
  149. local tg_platform_param = ""
  150. if msg_body.tg_platform_id~="" then
  151. tg_platform_param = string.format(" AND tg_platform_id = %d ",msg_body.tg_platform_id)
  152. end
  153. local material_platform_id_param = ""
  154. if msg_body.material_platform_id~="" then
  155. material_platform_id_param = string.format(" AND material_platform_id = %d ",msg_body.material_platform_id)
  156. end
  157. local running_status_param = ""
  158. if msg_body.running_status~="" then
  159. running_status_param = string.format(" AND running_status = %d ",msg_body.running_status)
  160. end
  161. local pay_type_param = ""
  162. if msg_body.pay_type~="" then
  163. pay_type_param = string.format(" AND pay_type = %d ",msg_body.pay_type)
  164. end
  165. local butler_id_param = ""
  166. if msg_body.butler_id~="" then
  167. butler_id_param = string.format(" AND butler_id = %d ",msg_body.butler_id)
  168. end
  169. local tg_app_index_param = ""
  170. if msg_body.tg_app_index~="" then
  171. tg_app_index_param = string.format(" AND tg_app_index = %d ",msg_body.tg_app_index)
  172. end
  173. local is_open_create_link_param = ""
  174. if msg_body.is_open_create_link~="" then
  175. is_open_create_link_param = string.format(" AND is_open_create_link = %d ",msg_body.is_open_create_link)
  176. end
  177. local is_delete_param = ""
  178. if msg_body.is_delete~="" then
  179. is_delete_param = string.format(" AND is_delete = %d ",msg_body.is_delete)
  180. end
  181. local custom_budget_param = ""
  182. if msg_body.custom_budget~="" then
  183. custom_budget_param = string.format(" AND custom_budget LIKE '%%%d%%' ", msg_body.custom_budget)
  184. end
  185. local cpa_bid_param = ""
  186. if msg_body.cpa_bid~="" then
  187. cpa_bid_param = string.format(" AND cpa_bid LIKE '%%%d%%' ", msg_body.cpa_bid)
  188. end
  189. local template_bid_param = ""
  190. if msg_body.template_bid~="" then
  191. template_bid_param = string.format(" AND template_bid LIKE '%%%d%%' ", msg_body.template_bid)
  192. end
  193. -- local genre_param = ""
  194. -- if msg_body.genre~="" then
  195. -- genre_param = string.format(" AND genre = %d ",msg_body.genre)
  196. -- end
  197. local param = is_group_record_param..tf_time_interval_param..mini_program_platform_id_param..is_comment_param..material_rule_id_param..parent_main_id_param..tg_day_param..id_param..ads_operation_status_param..statistics_platform_id_param..converted_time_duration_param..infra_status_param..cbo_script_status_param..cbo_status_param..project_type_param..oce_product_platform_id_param..oce_product_id_param..delivery_mode_param..main_name_param..tg_platform_param..running_status_param..pay_type_param..butler_id_param..tg_app_index_param..is_open_create_link_param..is_delete_param..custom_budget_param..cpa_bid_param..template_bid_param..wx_miniapp_create_status_param..material_platform_id_param;
  198. local sql = "SELECT * FROM tg_main where 1=1 "..param..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  199. local res = mysqldbx.query(sql)
  200. skynet.error("sql:",sql)
  201. sql = "SELECT COUNT(*) AS total FROM tg_main where 1=1 "..param
  202. local total = mysqldbx.query(sql)
  203. return true,res,total[1].total
  204. end
  205. -- `main_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '主体名称',
  206. -- `app_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '小程序ID',
  207. -- `running_status` tinyint(1) DEFAULT '1' COMMENT '运行状态 0-停止 1-运行中',
  208. -- `ad_quantity` int NOT NULL DEFAULT '40' COMMENT '广告数量',
  209. -- `cpa_bid` bigint NOT NULL DEFAULT '1750' COMMENT 'CPA出价(分), 默认17.5元',
  210. -- `bid_type` tinyint(1) DEFAULT '1' COMMENT '出价方式 0-常规 1-最大化 2 两种存储',
  211. -- `bid_ratio` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '1' COMMENT '出价方式比例(如 1:2)',
  212. -- `budget` bigint NOT NULL DEFAULT '9000000' COMMENT '预算金额(分), 默认9万元',
  213. -- `tg_platform_id` int NOT NULL DEFAULT '0' COMMENT '平台',
  214. -- `daily_new_limit` int DEFAULT '0' COMMENT '今天新上数量上限',
  215. function M.addMain(msg_body)
  216. local isok ,key = tools.checkData({"is_group_record","tf_time_interval","mat_roi","reduce_roi","boost_high_price_end_time","boost_high_price_number","reduce_consume_amount","reduce_bid_amount","is_comment","convert_cnt","material_rule_id","material_rule_number","parent_main_id","valid_timer_like_num","tg_day","ads_operation_status","statistics_platform_id","hide_if_converted","converted_time_duration","cbo_script_time","cbo_script_status","cbo_status","project_type","tf_status","tf_start_time","stat_cost","oce_product_id","oce_product_platform_id","material_platform_id","strategy_config","delivery_mode","price","pack_strategy_status","start_chapter","advertiser_id","wx_miniapp_create_status","ad_act","template_bid","zh_id","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)
  217. if not isok then
  218. return false,string.format("缺少字段: %s.", key)
  219. end
  220. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  221. msg_body.create_time = current_time
  222. local sql = string.format("INSERT INTO `tg_main` (is_group_record,tf_time_interval,mat_roi,reduce_roi,boost_high_price_end_time,boost_high_price_number,reduce_consume_amount,reduce_bid_amount,is_comment,convert_cnt,material_rule_id,material_rule_number,parent_main_id,valid_timer_like_num,tg_day,ads_operation_status,statistics_platform_id,hide_if_converted,converted_time_duration,cbo_script_time,cbo_script_status,cbo_status,project_type,tf_status,tf_start_time,stat_cost,oce_product_id,oce_product_platform_id,material_platform_id,strategy_config,delivery_mode,price,pack_strategy_status,start_chapter,advertiser_id,wx_miniapp_create_status,genre,ad_act,template_bid,zh_id,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,%f,%f,%d,%d,%f,%f,%d,%d,%d,%d, %d,%d,%d,'%s',%d, '%s','%s','%s',%d,%d,%d,%d,'%s',%d,'%s','%s',%d,'%s','%s',%d,%d,%d,%s,%d,%d,%d,%d,'%s',%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')",
  223. msg_body.is_group_record,
  224. msg_body.tf_time_interval,
  225. msg_body.mat_roi,
  226. msg_body.reduce_roi,
  227. msg_body.boost_high_price_end_time,
  228. msg_body.boost_high_price_number,
  229. msg_body.reduce_consume_amount,
  230. msg_body.reduce_bid_amount,
  231. msg_body.is_comment,
  232. msg_body.convert_cnt,
  233. msg_body.material_rule_id,
  234. msg_body.material_rule_number,
  235. msg_body.parent_main_id,
  236. msg_body.valid_timer_like_num,
  237. msg_body.tg_day,
  238. msg_body.ads_operation_status,
  239. msg_body.statistics_platform_id,
  240. msg_body.hide_if_converted,
  241. msg_body.converted_time_duration,
  242. msg_body.cbo_script_time,
  243. msg_body.cbo_script_status,
  244. msg_body.cbo_status,
  245. msg_body.project_type,
  246. msg_body.tf_status,
  247. msg_body.tf_start_time,
  248. msg_body.stat_cost,
  249. msg_body.oce_product_id,
  250. msg_body.oce_product_platform_id,
  251. msg_body.material_platform_id,
  252. msg_body.strategy_config,
  253. msg_body.delivery_mode,
  254. msg_body.price,
  255. msg_body.pack_strategy_status,
  256. msg_body.start_chapter,
  257. msg_body.advertiser_id,
  258. msg_body.wx_miniapp_create_status,
  259. 0,
  260. msg_body.ad_act,
  261. msg_body.template_bid,
  262. msg_body.zh_id,
  263. msg_body.roi_goal,
  264. msg_body.ql_bid,
  265. msg_body.infra_status,
  266. msg_body.pay_type,
  267. msg_body.qm_id,
  268. msg_body.ldy_template_site_id,
  269. msg_body.yw_id,
  270. msg_body.parent_platform_id,
  271. msg_body.platform_shared_id,
  272. msg_body.distributor_id,
  273. msg_body.is_delete,
  274. msg_body.ad_convert_type,
  275. msg_body.aweme_id,
  276. msg_body.tg_app_index,
  277. msg_body.butler_id,
  278. msg_body.mini_program_platform_id,
  279. msg_body.landing_page,
  280. msg_body.valid_timer_time,
  281. msg_body.valid_timer_number,
  282. msg_body.channelId,
  283. msg_body.no_bid_budget,
  284. msg_body.max_ad_quantity,
  285. cjson.encode(msg_body.tg_link_config),
  286. msg_body.tg_platform_id,
  287. msg_body.app_id,
  288. msg_body.main_name,
  289. msg_body.running_status,
  290. msg_body.ad_quantity,
  291. msg_body.cpa_bid,
  292. msg_body.bid_type,
  293. msg_body.bid_ratio,
  294. msg_body.custom_budget,
  295. msg_body.daily_new_limit,
  296. msg_body.valid_time,
  297. msg_body.group_id)
  298. skynet.error(sql)
  299. tools.dump(mysqldbx.query(sql))
  300. if msg_body.parent_main_id ~= 0 then
  301. M.updateParentMain(msg_body.parent_main_id)
  302. end
  303. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="updateMainConfig"}))
  304. -- pushAddMainMsg(msg_body)
  305. return true
  306. end
  307. --此主体将书库的书都发布到小程序书籍
  308. function M.syncMain(msg_body)
  309. local isok ,key = tools.checkData({"id","min_stat_cost","max_stat_cost"},msg_body)
  310. if not isok then
  311. return false,string.format("缺少字段: %s.", key)
  312. end
  313. local sql = string.format("select * from `tg_main` where id = %d LIMIT 1 ",msg_body.id)
  314. local res = mysqldbx.query(sql)
  315. res[1].min_stat_cost = msg_body.min_stat_cost
  316. res[1].max_stat_cost = msg_body.max_stat_cost
  317. res[1].start_publish_time = ""
  318. res[1].end_publish_time = ""
  319. if msg_body.start_publish_time~="" and msg_body.start_publish_time~=nil then
  320. res[1].start_publish_time = msg_body.start_publish_time
  321. end
  322. if msg_body.end_publish_time~="" and msg_body.end_publish_time~=nil then
  323. res[1].end_publish_time = msg_body.end_publish_time
  324. end
  325. syncMainAppletProduct(res[1])
  326. return true,{}
  327. end
  328. function syncMainAppletProduct(main_info)
  329. -- local tg_platform_id = main_info.tg_platform_id
  330. -- local app_id = main_info.app_id
  331. -- local sql = string.format("SELECT * FROM video_product where book_platform = %d ",tg_platform_id)
  332. -- local video_product_list = mysqldtaskbx.Singleton().query(sql)
  333. -- function isHaveAppletProdut(product_id)
  334. -- 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)
  335. -- local r = mysqldtaskbx.Singleton().query(sql)
  336. -- return #r>0
  337. -- end
  338. -- local need_create_link_list = {}
  339. -- local verify_list = {}
  340. -- for i = 1, #video_product_list, 1 do
  341. -- local product = video_product_list[i]
  342. -- if not isHaveAppletProdut(product.product_id) and not verify_list[product.product_id] then
  343. -- verify_list[product.product_id] = true
  344. -- 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})
  345. -- end
  346. -- end
  347. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="syncMain",data={main_info=main_info}}))
  348. end
  349. function pushAddMainMsg(msg_body)
  350. 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)
  351. local isok,res;
  352. res = mysqldbx.query(sql)
  353. skynet.error("sql:",sql)
  354. tools.dump(res)
  355. if #res > 0 then
  356. local main_info = res[1]
  357. skynet.fork(function ()
  358. -- local getMainInfoByAppid = function(app_id)
  359. -- local temp_list = {}
  360. -- for i = 1, #main_list, 1 do
  361. -- local main_item = main_list[i]
  362. -- if (main_item.app_id==app_id) then
  363. -- table.insert(temp_list,#temp_list+1,main_item)
  364. -- end
  365. -- end
  366. -- return temp_list
  367. -- end
  368. --获取书籍小程序列表
  369. sql = string.format("SELECT * FROM video_applet_product ")
  370. res = mysqldtaskbx.Singleton().query(sql)
  371. local video_applet_product_list = {}
  372. if #res>0 then
  373. for i = 1, #res, 1 do
  374. table.insert(video_applet_product_list,i,res[i])
  375. end
  376. end
  377. local need_create_link_list = {}
  378. -- local isCreateLinkByMainIdAndProductId = function(main_id,product_id)
  379. -- local isCreate = false
  380. -- for i = 1, #video_applet_product_list, 1 do
  381. -- local item = video_applet_product_list[i]
  382. -- if (item.product_id==product_id and item.main_id == main_id) then
  383. -- isCreate = true
  384. -- break
  385. -- end
  386. -- end
  387. -- return isCreate
  388. -- end
  389. --根据小程序id 筛选出所有需要创建的书籍
  390. local video_applet_product_of_main_list = {}
  391. local verify_list = {}
  392. for i = 1, #video_applet_product_list, 1 do
  393. local applet_item = video_applet_product_list[i]
  394. if verify_list[applet_item.product_id]==nil then
  395. if applet_item.status == 1 and applet_item.dy_small_applet_app_id == main_info.app_id then
  396. verify_list[applet_item.product_id] = true
  397. 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})
  398. end
  399. end
  400. end
  401. skynet.error("找到所有小程序!",#video_applet_product_of_main_list)
  402. 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}}))
  403. end)
  404. end
  405. end
  406. local function combine_query_and_params(query, params)
  407. -- 将 params 中的值替换到 query 中的占位符 ?
  408. local combined_query = query:gsub("?", function()
  409. local param = table.remove(params, 1) -- 从 params 中取出第一个参数
  410. if type(param) == "string" then
  411. return "'" .. param .. "'" -- 如果是字符串,用单引号包裹
  412. else
  413. return tostring(param) -- 如果是数字或其他类型,直接转换为字符串
  414. end
  415. end)
  416. return combined_query
  417. end
  418. function M.modifyMain(msg_body)
  419. local isok ,key = tools.checkData({"is_group_record","tf_time_interval","mat_roi","reduce_roi","boost_high_price_end_time","boost_high_price_number","reduce_consume_amount","reduce_bid_amount","is_comment","convert_cnt","material_rule_id","material_rule_number","valid_timer_like_num","tg_day","ads_operation_status" ,"statistics_platform_id","hide_if_converted","converted_time_duration","cbo_script_time","cbo_script_status","cbo_status","project_type","tf_status","tf_start_time","stat_cost","oce_product_id","oce_product_platform_id","material_platform_id","strategy_config","delivery_mode","price","pack_strategy_status","start_chapter","advertiser_id","wx_miniapp_create_status","ad_act","template_bid","zh_id","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)
  420. if not isok then
  421. return false,string.format("缺少字段: %s.", key)
  422. end
  423. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  424. msg_body.update_time = current_time
  425. local query = [[
  426. UPDATE `tg_main` SET
  427. is_group_record = ?,
  428. tf_time_interval = ?,
  429. mat_roi = ?,
  430. reduce_roi = ?,
  431. boost_high_price_end_time = ?,
  432. boost_high_price_number = ?,
  433. reduce_consume_amount = ?,
  434. reduce_bid_amount = ?,
  435. is_comment = ?,
  436. convert_cnt = ?,
  437. material_rule_id = ?,
  438. material_rule_number = ?,
  439. valid_timer_like_num = ?,
  440. tg_day = ?,
  441. ads_operation_status = ?,
  442. statistics_platform_id = ?,
  443. hide_if_converted = ?,
  444. converted_time_duration = ?,
  445. cbo_script_time = ?,
  446. cbo_script_status = ?,
  447. cbo_status = ?,
  448. project_type = ?,
  449. tf_status = ?,
  450. tf_start_time = ?,
  451. stat_cost = ?,
  452. oce_product_id = ?,
  453. oce_product_platform_id = ?,
  454. material_platform_id = ?,
  455. strategy_config = ?,
  456. delivery_mode = ?,
  457. price = ?,
  458. pack_strategy_status = ?,
  459. start_chapter = ?,
  460. advertiser_id = ?,
  461. wx_miniapp_create_status = ?,
  462. genre = ?,
  463. ad_act = ?,
  464. template_bid = ?,
  465. zh_id = ?,
  466. roi_goal = ?,
  467. ql_bid = ?,
  468. infra_status = ?,
  469. pay_type = ?,
  470. qm_id = ?,
  471. ldy_template_site_id = ?,
  472. yw_id = ?,
  473. parent_platform_id = ?,
  474. platform_shared_id = ?,
  475. distributor_id = ?,
  476. is_delete = ?,
  477. ad_convert_type = ?,
  478. aweme_id = ?,
  479. tg_app_index = ?,
  480. butler_id = ?,
  481. mini_program_platform_id = ?,
  482. landing_page = ?,
  483. valid_timer_time = ?,
  484. valid_timer_number = ?,
  485. channelId = ?,
  486. no_bid_budget = ?,
  487. max_ad_quantity = ?,
  488. tg_platform_id = ?,
  489. app_id = ?,
  490. main_name = ?,
  491. update_time = ?,
  492. running_status = ?,
  493. ad_quantity = ?,
  494. cpa_bid = ?,
  495. bid_type = ?,
  496. bid_ratio = ?,
  497. custom_budget = ?,
  498. daily_new_limit = ?,
  499. valid_time = ?,
  500. tg_link_config = ?,
  501. group_id = ?
  502. WHERE id = ?
  503. ]]
  504. local params = {
  505. msg_body.is_group_record,
  506. msg_body.tf_time_interval,
  507. msg_body.mat_roi,
  508. msg_body.reduce_roi,
  509. msg_body.boost_high_price_end_time,
  510. msg_body.boost_high_price_number,
  511. msg_body.reduce_consume_amount,
  512. msg_body.reduce_bid_amount,
  513. msg_body.is_comment,
  514. msg_body.convert_cnt,
  515. msg_body.material_rule_id,
  516. msg_body.material_rule_number,
  517. msg_body.valid_timer_like_num,
  518. msg_body.tg_day,
  519. msg_body.ads_operation_status,
  520. msg_body.statistics_platform_id,
  521. msg_body.hide_if_converted,
  522. msg_body.converted_time_duration,
  523. msg_body.cbo_script_time,
  524. msg_body.cbo_script_status,
  525. msg_body.cbo_status,
  526. msg_body.project_type,
  527. msg_body.tf_status,
  528. msg_body.tf_start_time,
  529. msg_body.stat_cost,
  530. msg_body.oce_product_id,
  531. msg_body.oce_product_platform_id,
  532. msg_body.material_platform_id,
  533. msg_body.strategy_config,
  534. msg_body.delivery_mode,
  535. msg_body.price,
  536. msg_body.pack_strategy_status,
  537. msg_body.start_chapter,
  538. msg_body.advertiser_id,
  539. msg_body.wx_miniapp_create_status,
  540. 0,
  541. msg_body.ad_act,
  542. msg_body.template_bid,
  543. msg_body.zh_id,
  544. msg_body.roi_goal,
  545. msg_body.ql_bid,
  546. msg_body.infra_status,
  547. msg_body.pay_type,
  548. msg_body.qm_id,
  549. msg_body.ldy_template_site_id,
  550. msg_body.yw_id,
  551. msg_body.parent_platform_id,
  552. msg_body.platform_shared_id,
  553. msg_body.distributor_id,
  554. msg_body.is_delete,
  555. msg_body.ad_convert_type,
  556. msg_body.aweme_id,
  557. msg_body.tg_app_index,
  558. msg_body.butler_id,
  559. msg_body.mini_program_platform_id,
  560. msg_body.landing_page,
  561. tonumber(msg_body.valid_timer_time),
  562. tonumber(msg_body.valid_timer_number),
  563. msg_body.channelId,
  564. msg_body.no_bid_budget,
  565. msg_body.max_ad_quantity,
  566. msg_body.tg_platform_id,
  567. msg_body.app_id,
  568. msg_body.main_name,
  569. msg_body.update_time,
  570. msg_body.running_status,
  571. msg_body.ad_quantity,
  572. msg_body.cpa_bid,
  573. msg_body.bid_type,
  574. msg_body.bid_ratio,
  575. msg_body.custom_budget,
  576. tonumber(msg_body.daily_new_limit),
  577. tonumber(msg_body.valid_time),
  578. cjson.encode(msg_body.tg_link_config),
  579. msg_body.group_id,
  580. msg_body.id
  581. }
  582. local sql = combine_query_and_params(query,params)
  583. skynet.error("sql:",sql)
  584. -- local sql = string.format("UPDATE `tg_main` SET strategy_config = '%s' , delivery_mode = '%s' , price = %d , pack_strategy_status = %d , start_chapter = %d , advertiser_id = '%s' ,wx_miniapp_create_status = %d , genre = %d , ad_act = %d ,template_bid = %d , zh_id = '%s', 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' ,advertiser_id = '%s' ,wx_miniapp_create_status = %d , genre = %d , ad_act = %d ,template_bid = %d , zh_id = '%s', 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 ",
  585. -- msg_body.strategy_config,msg_body.delivery_mode,msg_body.price,msg_body.pack_strategy_status,msg_body.start_chapter,msg_body.advertiser_id,msg_body.wx_miniapp_create_status,msg_body.genre,msg_body.ad_act,msg_body.template_bid,msg_body.zh_id,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,
  586. -- 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)
  587. tools.dump(mysqldbx.query(sql))
  588. skynet.send("backmgr","lua","on_recv",nil,"ws_push_msg",cjson.encode({cmd="updateMainConfig"}))
  589. return true
  590. end
  591. --配置主体推广链接
  592. function M.tgLinkConfig(msg_body)
  593. local isok ,key = tools.checkData({"id","is_open","huichuan_id","chongzhi_id","kadian_id","f_chongzhi_id","site_id"},msg_body)
  594. if not isok then
  595. return false,string.format("缺少字段: %s.", key)
  596. end
  597. local sql = string.format("select tg_link_config from `tg_main` WHERE id = %d ",msg_body.id)
  598. local res = mysqldbx.query(sql)
  599. sql = string.format("UPDATE `tg_main` SET tg_link_config = '%s' WHERE id = %d ",
  600. cjson.encode(msg_body),msg_body.id)
  601. mysqldbx.query(sql)
  602. return true, {}
  603. end
  604. --设置创建链接状态
  605. function M.set_open_create_link_status(msg_body)
  606. local isok ,key = tools.checkData({"id","is_open_create_link"},msg_body)
  607. if not isok then
  608. return false,string.format("缺少字段: %s.", key)
  609. end
  610. local sql = string.format("UPDATE `tg_main` SET is_open_create_link = %d WHERE id = %d ",
  611. msg_body.is_open_create_link,msg_body.id)
  612. mysqldbx.query(sql)
  613. return true, {}
  614. end
  615. --获取主体下的运行总数
  616. function M.get_main_ad_run_number(msg_body)
  617. local isok ,key = tools.checkData({"main_id"},msg_body)
  618. if not isok then
  619. return false,string.format("缺少字段: %s.", key)
  620. end
  621. local sql ="SELECT * FROM advertiser WHERE main_id = " .. msg_body.main_id;
  622. local res = mysqldbx.query(sql)
  623. local all_run_number = 0
  624. for i = 1, #res, 1 do
  625. local run_number = res[i].ad_run_number
  626. all_run_number = all_run_number + run_number
  627. end
  628. local nums = #res
  629. sql = string.format("select * from `tg_main` where id = %d LIMIT 1 ",msg_body.main_id)
  630. res = mysqldbx.query(sql)
  631. local max = nums * res[1].max_ad_quantity
  632. return true,{all_run_number=all_run_number,max=max}
  633. end
  634. function M.search_info_by_id(msg_body)
  635. local isok ,key = tools.checkData({"query_list"},msg_body)
  636. if not isok then
  637. return false,string.format("缺少字段: %s.", key)
  638. end
  639. local idString = table.concat(msg_body.query_list, ",")
  640. local sql = string.format("SELECT * FROM tg_main WHERE id IN (%s)",idString)
  641. local res = mysqldbx.query(sql)
  642. return true,res
  643. end
  644. function M.search_info_by_tg_app_index(msg_body)
  645. local isok ,key = tools.checkData({"query_list"},msg_body)
  646. if not isok then
  647. return false,string.format("缺少字段: %s.", key)
  648. end
  649. local idString = table.concat(msg_body.query_list, ",")
  650. local sql = string.format("SELECT * FROM tg_main WHERE tg_app_index IN (%s)",idString)
  651. local res = mysqldbx.query(sql)
  652. return true,res
  653. end
  654. function M.updateParentMain(parent_main_id)
  655. local sql = string.format("select * from `tg_main` where id = '%s' LIMIT 1 ",parent_main_id)
  656. local res = mysqldbx.query(sql)
  657. local main_info = nil
  658. if #res>0 then
  659. main_info = res[1]
  660. end
  661. if main_info~=nil then
  662. sql = string.format("select * from `tg_main` where parent_main_id = %d LIMIT 500 ",parent_main_id)
  663. res = mysqldbx.query(sql)
  664. local child_list = nil
  665. local id_list = {}
  666. if #res>0 then
  667. child_list = res
  668. for i = 1, #child_list, 1 do
  669. table.insert(id_list,i,child_list[i].id)
  670. end
  671. end
  672. if child_list ~= nil then
  673. local idString = table.concat(id_list, ",")
  674. sql = string.format("UPDATE `tg_main` SET child_main_list = '%s' WHERE id = %d ",idString,parent_main_id)
  675. mysqldbx.query(sql)
  676. end
  677. end
  678. end
  679. function mysqldtaskbx.start()
  680. local function on_connect(db)
  681. db:query("set charset utf8mb4");
  682. end
  683. local conf = config.db_cnf.book_server.mysqldb_task_cnf
  684. db = mysql.connect{
  685. host=conf.ip,
  686. port=conf.port,
  687. database=conf.db,
  688. user=conf.user,
  689. password=conf.password,
  690. charset="utf8mb4",
  691. max_packet_size = 1024 * 1024,
  692. on_connect = on_connect
  693. }
  694. if not db then
  695. error("mysql connect fail")
  696. end
  697. end
  698. function mysqldtaskbx.Singleton()
  699. if db == nil then
  700. mysqldtaskbx.start()
  701. end
  702. return mysqldtaskbx
  703. end
  704. function mysqldtaskbx.query(sql)
  705. return db:query(sql)
  706. end
  707. return M