JLEventReportChargeService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // ToDo,检测传几卡几
  226. $sm = explode(':', $this->configLog->callback_param);
  227. $this->chuanNkaM($this->configLog, $sm[0], $sm[1]);
  228. if($this->configLog->flag) {
  229. $this->configLog->flag_report++;
  230. } else {
  231. $this->configLog->flag_unreport++;
  232. $this->result['continue_judge'] = false;
  233. $this->result['need_report'] = false;
  234. $this->result['info_type'] = 'rate_filter';
  235. $this->result['info_str'] = '比率过滤';
  236. }
  237. }
  238. }
  239. }
  240. private function chuanNkaM($obj, $s, $m)
  241. {
  242. if($obj->flag) {
  243. if(($obj->flag_report) < $s){
  244. $obj->flag = 1;
  245. } else {
  246. $obj->flag_report = 0;
  247. $obj->flag = 0;
  248. }
  249. } else {
  250. if(($obj->flag_unreport) < $m) {
  251. $obj->flag = 0;
  252. } else {
  253. $obj->flag_unreport = 0;
  254. $obj->flag = 1;
  255. }
  256. }
  257. }
  258. /**
  259. * 判断充值行为
  260. */
  261. private function judgeChargeType() {
  262. if(!$this->result['need_report'] || !$this->result['continue_judge']) {
  263. return;
  264. }
  265. if(1 == $this->callbackConfig->charge_type) {
  266. $isFirstCharge = DB::table('orders')
  267. ->where([
  268. 'uid' => $this->uid,
  269. 'status' => 'paid',
  270. 'promotion_id' => $this->trackRecord->ranse_id,
  271. ])->where('created_at', '>=', $this->trackRecord->ranse_start_at)
  272. ->where('created_at', '<=', $this->trackRecord->ranse_end_at)
  273. ->where('id', '<', $this->order->id)
  274. ->count() == 0;
  275. if(!$isFirstCharge) {
  276. $this->result['need_report'] = false;
  277. $this->result['continue_judge'] = false;
  278. $this->result['info_type'] = 'neq_first_charge';
  279. $this->result['info_str'] = '非首充';
  280. }
  281. }
  282. }
  283. /**
  284. * 获取订单的染色信息,回传配置信息, 推广被禁用,就按照没有回传配置处理
  285. */
  286. private function fillRanseInfo() {
  287. $ranseId = $this->order->promotion_id;
  288. $this->promotion = DB::table('promotions')->where('id', $ranseId)->first();
  289. if(0 == $this->promotion->status || $this->promotion->callback_config_id < 1) {
  290. $this->result['need_report'] = false;
  291. $this->result['info_type'] = 'no_callback_config';
  292. $this->result['info_str'] = '没有回传配置';
  293. return;
  294. }
  295. $this->callbackConfig = DB::table('jl_event_callback_config')
  296. ->where(['id' => $this->promotion->callback_config_id])
  297. ->first();
  298. if(!$this->callbackConfig) {
  299. $this->result['need_report'] = false;
  300. $this->result['info_type'] = 'no_callback_config';
  301. $this->result['info_str'] = '没有回传配置';
  302. return;
  303. }
  304. }
  305. private function reportJuliang() {
  306. if(!$this->result['need_report']) {
  307. return;
  308. }
  309. $service = new TiktokEventReportService;
  310. if('roi_report_all' == $this->result['info_type']) {
  311. $reportResult = $service->roiReportCharge((object)[
  312. 'callback' => $this->trackRecord->callback
  313. ]);
  314. } else {
  315. $reportResult = $service->reportCharge((object)[
  316. 'callback' => $this->trackRecord->callback
  317. ]);
  318. }
  319. $reportContent = \json_decode($reportResult['content'], true);
  320. if($reportResult['result'] && 0 == $reportContent['code']) {
  321. $this->result['report_success'] = true;
  322. } else {
  323. $this->result['report_success'] = false;
  324. }
  325. $this->result['report_result'] = $reportResult;
  326. $this->result['need_report'] = false;
  327. }
  328. /**
  329. * 获取匹配的广告用户信息,主要是获取回传 callback
  330. */
  331. private function fillTrackInfo() {
  332. if(!$this->result['need_report']) {
  333. return;
  334. }
  335. $this->trackRecord = DB::table('callback_report_ranse_record')
  336. ->where(['uid' => $this->uid, 'ranse_id' => $this->order->promotion_id])
  337. ->where('ranse_start_at', '<=', $this->order->created_at)
  338. ->where('ranse_end_at', '>=', $this->order->created_at)
  339. ->orderBy('id', 'desc')
  340. ->first();
  341. if(!$this->trackRecord) {
  342. $this->result['need_report'] = false;
  343. $this->result['info_type'] = 'no_track_info';
  344. $this->result['info_str'] = '无匹配用户';
  345. return;
  346. }
  347. $this->result['report_param'] = [
  348. 'callback' => $this->trackRecord->callback,
  349. ];
  350. }
  351. }