tg_zhanghu.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. --账户
  2. local M = {}
  3. local mysqldbx = require "mysqldbx"
  4. local tools = require "tools"
  5. local skynet = require "skynet"
  6. local httpc = require "http.httpc"
  7. --page_size 是你想要在每页中显示的记录数。
  8. --page_number 页数
  9. --获取所有账户
  10. function M.getZhangHuList(msg_body)
  11. local isok ,key = tools.checkData({"page_size","page_number"},msg_body)
  12. if not isok then
  13. return false,string.format("缺少字段: %s.", key)
  14. end
  15. local page_size = msg_body.page_size
  16. local page_number = msg_body.page_number
  17. local offset = (page_number - 1) * page_size
  18. local sql = string.format("SELECT * FROM advertiser ORDER BY id LIMIT %d OFFSET %d", page_size, offset)
  19. local isok,res;
  20. res = mysqldbx.query(sql)
  21. if #res <= 0 then
  22. return true ,{}
  23. end
  24. return true, res
  25. end
  26. function M.getTotal()
  27. local sql = "SELECT COUNT(*) AS total FROM advertiser"
  28. local res = mysqldbx.query(sql)
  29. return true,res[1]
  30. end
  31. function M.advertiserListOfMain(msg_body)
  32. local isok ,key = tools.checkData({"id_list","main_id"},msg_body)
  33. if not isok then
  34. return false,string.format("缺少字段: %s.", key)
  35. end
  36. local fail_list = {}
  37. for i = 1, #msg_body.id_list, 1 do
  38. local id = msg_body.id_list[i]
  39. local sql = string.format("SELECT * FROM advertiser where advertiser_id = '%s' and main_id = %d ", id,msg_body.main_id)
  40. local isok,res;
  41. res = mysqldbx.query(sql)
  42. if #res <1 then
  43. table.insert(fail_list,#fail_list+1,id)
  44. end
  45. end
  46. return true,fail_list
  47. end
  48. function M.setZhanghuOfMain(msg_body)
  49. local isok ,key = tools.checkData({"id_list","main_id"},msg_body)
  50. if not isok then
  51. return false,string.format("缺少字段: %s.", key)
  52. end
  53. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  54. msg_body.update_time = current_time
  55. for i = 1, #msg_body.id_list, 1 do
  56. local id = msg_body.id_list[i]
  57. local sql = string.format("UPDATE `advertiser` SET main_id = %d WHERE id = %d ",
  58. msg_body.main_id,id)
  59. skynet.error(sql)
  60. mysqldbx.query(sql)
  61. end
  62. return true,{}
  63. end
  64. function M.searchZhanghu(msg_body)
  65. local isok ,key = tools.checkData({"advertiser_id"},msg_body)
  66. if not isok then
  67. return false,string.format("缺少字段: %s.", key)
  68. end
  69. local sql = string.format("SELECT * FROM advertiser WHERE advertiser_id = '%s' LIMIT 1 ", tostring(msg_body.advertiser_id))
  70. local res;
  71. res = mysqldbx.query(sql)
  72. local info = {}
  73. if #res>0 then
  74. info = res[1]
  75. end
  76. return true,info
  77. end
  78. function M.searchName(msg_body)
  79. local isok ,key = tools.checkData({"content","page_size","page_number"},msg_body)
  80. if not isok then
  81. return false,string.format("缺少字段: %s.", key)
  82. end
  83. local page_size = msg_body.page_size
  84. local page_number = msg_body.page_number
  85. local offset = (page_number - 1) * page_size
  86. local sql = string.format(" SELECT COUNT(*) AS total FROM advertiser advertiser_name LIKE '%%%s%%' ", msg_body.content)
  87. local total = mysqldbx.query(sql)
  88. sql = string.format("SELECT * FROM advertiser WHERE advertiser_name LIKE '%%%s%%' ORDER BY id LIMIT %d OFFSET %d ", msg_body.content, page_size, offset)
  89. local res;
  90. res = mysqldbx.query(sql)
  91. return true,res,total[1].total
  92. end
  93. function M.switch(msg_body)
  94. local isok ,key = tools.checkData({"id_list","status"},msg_body)
  95. if not isok then
  96. return false,string.format("缺少字段: %s.", key)
  97. end
  98. for i = 1, #msg_body.id_list, 1 do
  99. local id = msg_body.id_list[i]
  100. local sql = string.format("UPDATE `advertiser` SET status = %d WHERE id = %d ",
  101. msg_body.status,id)
  102. local res;
  103. res = mysqldbx.query(sql)
  104. end
  105. return true,{}
  106. end
  107. function M.mainFilter(msg_body)
  108. local isok ,key = tools.checkData({"content","main_id","page_size","page_number"},msg_body)
  109. if not isok then
  110. return false,string.format("缺少字段: %s.", key)
  111. end
  112. local page_size = msg_body.page_size
  113. local page_number = msg_body.page_number
  114. local offset = (page_number - 1) * page_size
  115. local main_id_param = ""
  116. if msg_body.main_id ~= "" then
  117. main_id_param = " AND main_id = "..msg_body.main_id.." "
  118. end
  119. local content_param = ""
  120. if msg_body.content ~= "" then
  121. content_param = string.format(" AND (advertiser_name LIKE CONCAT( '%%%s%%')) OR (advertiser_id LIKE CONCAT('%%%s%%'))",msg_body.content,msg_body.content)
  122. end
  123. local sql = "SELECT COUNT(*) AS total FROM advertiser WHERE 1=1 "..main_id_param..content_param
  124. local total = mysqldbx.query(sql)
  125. sql = "SELECT * FROM advertiser WHERE 1=1 "..main_id_param..content_param..string.format(" ORDER BY id LIMIT %d OFFSET %d ",page_size, offset)
  126. local res;
  127. res = mysqldbx.query(sql)
  128. return true,res,total[1].total
  129. end
  130. function M.search(msg_body)
  131. local isok ,key = tools.checkData({"advertiser_grant_balance_valid","stat_cost","advertiser_id_list","avatar_status","property_status","main_id","content","page_size","page_number","status"},msg_body)
  132. if not isok then
  133. return false,string.format("缺少字段: %s.", key)
  134. end
  135. local page_size = msg_body.page_size
  136. local page_number = msg_body.page_number
  137. local offset = (page_number - 1) * page_size
  138. local advertiser_grant_balance_valid_param = ""
  139. if msg_body.advertiser_grant_balance_valid~="" then
  140. if msg_body.advertiser_grant_balance_valid>0 then
  141. advertiser_grant_balance_valid_param = string.format(" advertiser_grant_balance_valid DESC ")
  142. else
  143. advertiser_grant_balance_valid_param = string.format(" advertiser_grant_balance_valid ASC ")
  144. end
  145. end
  146. local stat_cost_param = ""
  147. if msg_body.stat_cost~="" then
  148. if msg_body.stat_cost>0 then
  149. stat_cost_param = string.format(" stat_cost DESC ")
  150. else
  151. stat_cost_param = string.format(" stat_cost ASC ")
  152. end
  153. end
  154. local advertiser_id_list_param = ""
  155. if msg_body.advertiser_id_list~="" then
  156. local idString = table.concat(msg_body.advertiser_id_list, ",")
  157. advertiser_id_list_param = string.format(" AND advertiser_id IN (%s) ",idString)
  158. end
  159. local property_status_param = ""
  160. if msg_body.property_status~="" then
  161. property_status_param = "AND property_status = "..msg_body.property_status.." "
  162. end
  163. local avatar_status_param = ""
  164. if msg_body.avatar_status~="" then
  165. avatar_status_param = "AND avatar_status = "..msg_body.avatar_status.." "
  166. end
  167. local statuss_param = ""
  168. if msg_body.status~="" then
  169. statuss_param = "AND status = "..msg_body.status.." "
  170. end
  171. local main_id_param = ""
  172. if msg_body.main_id ~= "" then
  173. main_id_param = " AND main_id = "..msg_body.main_id.." "
  174. end
  175. local content_param = ""
  176. if msg_body.content ~= "" then
  177. content_param = string.format(" AND (advertiser_name LIKE CONCAT( '%%%s%%')) OR (advertiser_id LIKE CONCAT('%%%s%%'))",msg_body.content,msg_body.content)
  178. end
  179. local order_param = advertiser_grant_balance_valid_param..stat_cost_param
  180. if order_param == "" then
  181. order_param = " id DESC "
  182. end
  183. local param = advertiser_id_list_param..avatar_status_param..property_status_param..statuss_param..main_id_param..content_param
  184. local sql = "SELECT COUNT(*) AS total FROM advertiser WHERE 1=1 "..param
  185. local total = mysqldbx.query(sql)
  186. sql = "SELECT * FROM advertiser WHERE 1=1 "..param..string.format(" ORDER BY %s LIMIT %d OFFSET %d ",order_param,page_size, offset)
  187. local res;
  188. skynet.error(sql)
  189. res = mysqldbx.query(sql)
  190. return true,res,total[1].total
  191. end
  192. function M.search_info_by_id(msg_body)
  193. local isok ,key = tools.checkData({"query_list"},msg_body)
  194. if not isok then
  195. return false,string.format("缺少字段: %s.", key)
  196. end
  197. local temp = {}
  198. for i = 1, #msg_body.query_list, 1 do
  199. local zhang_hu_id = msg_body.query_list[i].zhang_hu_id..""
  200. local key = msg_body.query_list[i].id
  201. local sql = string.format("SELECT * FROM advertiser WHERE advertiser_id = '%s' LIMIT 1",zhang_hu_id)
  202. local res = mysqldbx.query(sql)
  203. if #res >0 then
  204. table.insert(temp,#temp+1,res[1])
  205. end
  206. end
  207. return true,temp
  208. end
  209. function M.update_zhanghu(msg_body)
  210. httpc.dns() -- set dns server
  211. httpc.timeout = 100 -- set timeout 1 second
  212. local status, body = httpc.get('https://clipvideoup.s6kuwan.com/adoce/advertiser/sync_center_advertiser','',{})
  213. if status == 200 then
  214. return true,{}
  215. else
  216. return false,{}
  217. end
  218. return true,{}
  219. end
  220. function searchZhanghuName(name,id)
  221. local sql = string.format("SELECT * FROM advertiser WHERE advertiser_name LIKE '%%%s%%'", name)
  222. local res;
  223. res = mysqldbx.query(sql)
  224. local id_list = {}
  225. for i = 1, #res, 1 do
  226. table.insert(id_list,i,res[i].id)
  227. end
  228. if #id_list> 0 then
  229. M.setZhanghuOfMain({id_list=id_list,main_id=id})
  230. end
  231. end
  232. function M.init()
  233. local sql = string.format("select * from `tg_main` ")
  234. local isok,res;
  235. res = mysqldbx.query(sql)
  236. if #res > 0 then
  237. tools.dump(res)
  238. for i = 1, #res, 1 do
  239. local name = res[i].main_name
  240. searchZhanghuName(name,res[i].id)
  241. end
  242. end
  243. return true,{}
  244. end
  245. return M