JLEventReportChargeService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace App\Service\Callback;
  3. use App\Service\Callback\Tiktok\TiktokEventReportService;
  4. use App\Service\Util\Support\Trace\TraceContext;
  5. use Illuminate\Support\Facades\DB;
  6. /**
  7. * 巨量2.0事件
  8. */
  9. class JLEventReportChargeService
  10. {
  11. private $uid;
  12. /**
  13. * @var TraceContext
  14. */
  15. private $traceContext;
  16. private $order;
  17. private $result;
  18. private $trackRecord;
  19. private $callbackConfig;
  20. private $promotion;
  21. private $configLog;
  22. public function __construct($uid, $order, $traceInfo)
  23. {
  24. $this->order = $order;
  25. $this->uid = $uid;
  26. $this->traceContext = TraceContext::newFromParent($traceInfo);
  27. $this->result = [
  28. 'need_report' => true,
  29. 'config_rate' => 0,
  30. 'current_rate' => 0,
  31. 'report_success' => false,
  32. 'info_type' => 'ok',
  33. 'continue_judge' => true,
  34. ];
  35. }
  36. public function report() {
  37. $this->fillRanseInfo();
  38. $this->fillTrackInfo();
  39. $this->judgeOrderAlreadyDeal();
  40. $this->judgeIsRoi();
  41. $this->judgeChargeType();
  42. $this->judgeChargeMoney();
  43. $this->reportJuliang();
  44. $this->saveRecord();
  45. }
  46. /**
  47. * 判断是否roi全量回传
  48. */
  49. private function judgeIsRoi() {
  50. if(!$this->result['need_report']) {
  51. return;
  52. }
  53. if(1 == $this->callbackConfig->is_roi) {
  54. $this->result['continue_judge'] = false;
  55. $this->result['info_type'] = 'roi_report_all';
  56. $this->result['info_str'] = 'roi全量上报';
  57. }
  58. }
  59. private function saveRecord() {
  60. if($this->result['save_record'] ?? true) {
  61. $now = date('Y-m-d H:i:s');
  62. $chargeRecord = DB::table('callback_report_charge_record')->where([
  63. 'order_no' => $this->order->trade_no
  64. ])->first();
  65. if(!$chargeRecord) {
  66. DB::table('callback_report_charge_record')
  67. ->insert([
  68. 'uid' => $this->uid,
  69. 'order_id' => $this->order->id,
  70. 'order_no' => $this->order->trade_no,
  71. 'optimizer_uid' => $this->promotion->uid,
  72. 'filter_type' => $this->result['info_type'] ?? 'ok',
  73. 'filter_reason' => $this->result['info_str'] ?? '允许回传',
  74. 'report_success' => intval($this->result['report_success'] ?? false),
  75. 'report_result' => \json_encode($this->result['report_result'] ?? []),
  76. 'report_param' => \json_encode($this->result['report_param'] ?? []),
  77. 'created_at' => $now,
  78. 'updated_at' => $now,
  79. 'advertiser_id' => $this->trackRecord->advertiser_id ?? '',
  80. 'adv_promotion_id' => $this->trackRecord->adv_promotion_id ?? '',
  81. 'config_rate' => $this->result['config_rate'],
  82. 'current_rate' => $this->result['current_rate'],
  83. 'user_ranse_start_at' => $this->trackRecord->ranse_start_at ?? null,
  84. 'order_price' => $this->order->price,
  85. 'order_created_at' => $this->order->created_at,
  86. 'user_ranse_ip' => $this->trackRecord->ranse_ip ?? '',
  87. 'miniprogram_id' => $this->promotion->miniprogram_id,
  88. 'user_ranse_id' => $this->promotion->id,
  89. 'callback_type' => $this->promotion->callback_type,
  90. 'callback_config_id' => $this->promotion->callback_config_id,
  91. 'ext_a' => $this->configLog->callback_param ?? '',
  92. ]);
  93. } else {
  94. DB::table('callback_report_charge_record')
  95. ->where(['id' => $chargeRecord->id])
  96. ->update([
  97. 'uid' => $this->uid,
  98. 'order_id' => $this->order->id,
  99. 'order_no' => $this->order->trade_no,
  100. 'optimizer_uid' => $this->promotion->uid,
  101. 'filter_type' => $this->result['info_type'] ?? 'ok',
  102. 'filter_reason' => $this->result['info_str'] ?? '允许回传',
  103. 'report_success' => intval($this->result['report_success'] ?? false),
  104. 'report_result' => \json_encode($this->result['report_result'] ?? []),
  105. 'report_param' => \json_encode($this->result['report_param'] ?? []),
  106. 'updated_at' => $now,
  107. 'advertiser_id' => $this->trackRecord->advertiser_id ?? '',
  108. 'adv_promotion_id' => $this->trackRecord->adv_promotion_id ?? '',
  109. 'config_rate' => $this->result['config_rate'],
  110. 'current_rate' => $this->result['current_rate'],
  111. 'user_ranse_start_at' => $this->trackRecord->ranse_start_at ?? null,
  112. 'order_price' => $this->order->price,
  113. 'order_created_at' => $this->order->created_at,
  114. 'user_ranse_ip' => $this->trackRecord->ranse_ip ?? '',
  115. 'miniprogram_id' => $this->promotion->miniprogram_id,
  116. 'user_ranse_id' => $this->promotion->id,
  117. 'callback_type' => $this->promotion->callback_type,
  118. 'callback_config_id' => $this->promotion->callback_config_id,
  119. 'ext_a' => $this->configLog->callback_param ?? '',
  120. ]);
  121. }
  122. if($this->configLog) {
  123. if('ok' == $this->result['info_type'] && $this->result['report_success']) {
  124. DB::table('jl_event_rate_config_log')
  125. ->where(['id' => $this->configLog->id])
  126. ->update([
  127. 'report_count' => $this->configLog->report_count + 1,
  128. 'updated_at' => $now,
  129. ]);
  130. if(3 == $this->configLog->callback_type) {
  131. DB::table('jl_event_rate_config_log')
  132. ->where(['id' => $this->configLog->id])
  133. ->update([
  134. 'flag' => $this->configLog->flag,
  135. 'flag_report' => $this->configLog->flag_report,
  136. 'flag_unreport' => $this->configLog->flag_unreport,
  137. 'updated_at' => $now,
  138. ]);
  139. }
  140. } elseif ('rate_filter' == $this->result['info_type']) {
  141. DB::table('jl_event_rate_config_log')
  142. ->where(['id' => $this->configLog->id])
  143. ->update([
  144. 'unreport_count' => $this->configLog->unreport_count + 1,
  145. 'updated_at' => $now,
  146. ]);
  147. if(3 == $this->configLog->callback_type) {
  148. DB::table('jl_event_rate_config_log')
  149. ->where(['id' => $this->configLog->id])
  150. ->update([
  151. 'flag' => $this->configLog->flag,
  152. 'flag_report' => $this->configLog->flag_report,
  153. 'flag_unreport' => $this->configLog->flag_unreport,
  154. 'updated_at' => $now,
  155. ]);
  156. }
  157. }
  158. $newConfigLog = DB::table('jl_event_rate_config_log')
  159. ->where(['id' => $this->configLog->id])
  160. ->select('report_count', 'unreport_count')
  161. ->first();
  162. if(($newConfigLog->report_count ?? 0 ) + ($newConfigLog->unreport_count ?? 0 )) {
  163. $currentRate = bcdiv(($newConfigLog->report_count ?? 0 ) * 100,
  164. (($newConfigLog->report_count ?? 0 ) + ($newConfigLog->unreport_count ?? 0 )), 2);
  165. } else {
  166. $currentRate = 0;
  167. }
  168. DB::table('callback_report_charge_record')->where([
  169. 'order_no' => $this->order->trade_no
  170. ])->update([
  171. 'current_rate' => $currentRate,
  172. ]);
  173. }
  174. }
  175. }
  176. /**
  177. * 判断订单是不是已经处理过了.如果处理过了,就不入库
  178. */
  179. private function judgeOrderAlreadyDeal() {
  180. if(!$this->result['need_report']) {
  181. return;
  182. }
  183. if(DB::table('callback_report_charge_record')
  184. ->where(['order_id' => $this->order->id])
  185. ->exists()) {
  186. $this->result['need_report'] = false;
  187. $this->result['save_record'] = false;
  188. $this->result['info_type'] = 'order_exists';
  189. $this->result['info_str'] = '订单已经处理';
  190. }
  191. }
  192. /**
  193. * 判断充值的金额区间
  194. */
  195. private function judgeChargeMoney() {
  196. if(!$this->result['need_report'] || !$this->result['continue_judge']) {
  197. return;
  198. }
  199. $this->configLog = DB::table('jl_event_rate_config_log')
  200. ->where([
  201. 'user_id' => $this->promotion->uid,
  202. 'config_id' => $this->callbackConfig->id,
  203. 'is_enabled' => 1,
  204. ])->where('min_money', '<=', $this->order->price)
  205. ->where('max_money', '>', $this->order->price)
  206. ->orderBy('id', 'desc')
  207. ->first();
  208. if(!$this->configLog) {
  209. $this->result['need_report'] = false;
  210. $this->result['continue_judge'] = false;
  211. $this->result['info_type'] = 'money_filter';
  212. $this->result['info_str'] = '订单金额不在所有金额项区间内';
  213. return;
  214. } else {
  215. if(1 == $this->configLog->callback_type) {
  216. $this->result['continue_judge'] = false;
  217. }elseif(2 == $this->configLog->callback_type) {
  218. $this->result['continue_judge'] = false;
  219. $this->result['need_report'] = false;
  220. $this->result['info_type'] = 'money_filter';
  221. $this->result['info_str'] = sprintf('订单金额在区间:[%s元-%s元]全部不回传',
  222. $this->configLog->min_money, $this->configLog->max_money);
  223. return;
  224. } elseif (3 == $this->configLog->callback_type) {
  225. $sm = explode(':', $this->configLog->callback_param);
  226. $this->chuanNkaM($this->configLog, $sm[0], $sm[1]);
  227. if($this->configLog->flag) {
  228. $this->configLog->flag_report++;
  229. } else {
  230. $this->configLog->flag_unreport++;
  231. $this->result['continue_judge'] = false;
  232. $this->result['need_report'] = false;
  233. $this->result['info_type'] = 'rate_filter';
  234. $this->result['info_str'] = '比率过滤';
  235. }
  236. }
  237. }
  238. }
  239. /**
  240. * 判断当前是回传还是不回传
  241. * @param $obj
  242. * @param $s 传几
  243. * @param $m 卡几
  244. */
  245. private function chuanNkaM($obj, $s, $m)
  246. {
  247. if($obj->flag) {
  248. if(($obj->flag_report) < $s){
  249. $obj->flag = 1;
  250. } else {
  251. $obj->flag_report = 0;
  252. $obj->flag = 0;
  253. }
  254. } else {
  255. if(($obj->flag_unreport) < $m) {
  256. $obj->flag = 0;
  257. } else {
  258. $obj->flag_unreport = 0;
  259. $obj->flag = 1;
  260. }
  261. }
  262. }
  263. /**
  264. * 判断充值行为
  265. */
  266. private function judgeChargeType() {
  267. if(!$this->result['need_report'] || !$this->result['continue_judge']) {
  268. return;
  269. }
  270. if(1 == $this->callbackConfig->charge_type) {
  271. $isFirstCharge = DB::table('orders')
  272. ->where([
  273. 'uid' => $this->uid,
  274. 'status' => 'paid',
  275. 'promotion_id' => $this->trackRecord->ranse_id,
  276. ])->where('created_at', '>=', $this->trackRecord->ranse_start_at)
  277. ->where('created_at', '<=', $this->trackRecord->ranse_end_at)
  278. ->where('id', '<', $this->order->id)
  279. ->count() == 0;
  280. if(!$isFirstCharge) {
  281. $this->result['need_report'] = false;
  282. $this->result['continue_judge'] = false;
  283. $this->result['info_type'] = 'neq_first_charge';
  284. $this->result['info_str'] = '非首充';
  285. }
  286. }
  287. }
  288. /**
  289. * 获取订单的染色信息,回传配置信息, 推广被禁用,就按照没有回传配置处理
  290. */
  291. private function fillRanseInfo() {
  292. $ranseId = $this->order->promotion_id;
  293. $this->promotion = DB::table('promotions')->where('id', $ranseId)->first();
  294. if(0 == $this->promotion->status || $this->promotion->callback_config_id < 1) {
  295. $this->result['need_report'] = false;
  296. $this->result['info_type'] = 'no_callback_config';
  297. $this->result['info_str'] = '没有回传配置';
  298. return;
  299. }
  300. $this->callbackConfig = DB::table('jl_event_callback_config')
  301. ->where(['id' => $this->promotion->callback_config_id])
  302. ->first();
  303. if(!$this->callbackConfig) {
  304. $this->result['need_report'] = false;
  305. $this->result['info_type'] = 'no_callback_config';
  306. $this->result['info_str'] = '没有回传配置';
  307. return;
  308. }
  309. }
  310. private function reportJuliang() {
  311. if(!$this->result['need_report']) {
  312. return;
  313. }
  314. $service = new TiktokEventReportService;
  315. if('roi_report_all' == $this->result['info_type']) {
  316. $reportResult = $service->roiReportCharge((object)[
  317. 'callback' => $this->trackRecord->callback
  318. ]);
  319. } else {
  320. $reportResult = $service->reportCharge((object)[
  321. 'callback' => $this->trackRecord->callback
  322. ]);
  323. }
  324. $reportContent = \json_decode($reportResult['content'], true);
  325. if($reportResult['result'] && 0 == $reportContent['code']) {
  326. $this->result['report_success'] = true;
  327. } else {
  328. $this->result['report_success'] = false;
  329. }
  330. $this->result['report_result'] = $reportResult;
  331. $this->result['need_report'] = false;
  332. }
  333. /**
  334. * 获取匹配的广告用户信息,主要是获取回传 callback
  335. */
  336. private function fillTrackInfo() {
  337. if(!$this->result['need_report']) {
  338. return;
  339. }
  340. $this->trackRecord = DB::table('callback_report_ranse_record')
  341. ->where(['uid' => $this->uid, 'ranse_id' => $this->order->promotion_id])
  342. ->where('ranse_start_at', '<=', $this->order->created_at)
  343. ->where('ranse_end_at', '>=', $this->order->created_at)
  344. ->orderBy('id', 'desc')
  345. ->first();
  346. if(!$this->trackRecord) {
  347. $this->result['need_report'] = false;
  348. $this->result['info_type'] = 'no_track_info';
  349. $this->result['info_str'] = '无匹配用户';
  350. return;
  351. }
  352. $this->result['report_param'] = [
  353. 'callback' => $this->trackRecord->callback,
  354. ];
  355. }
  356. }