JuliangAccountReportChargeService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. * 巨量账户级订单回传业务
  8. */
  9. class JuliangAccountReportChargeService
  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 $rateConfig;
  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->judgeIsFirstCharge();
  41. $this->judgeChargeMoney();
  42. $this->judgePromotionProtect();
  43. $this->judgeCallbackRate();
  44. $this->reportJuliang();
  45. $this->saveRecord();
  46. }
  47. private function saveRecord() {
  48. if($this->result['save_record'] ?? true) {
  49. $now = date('Y-m-d H:i:s');
  50. if(($this->result['info_type'] ?? '') == 'promotion_protect') {
  51. if($this->result['report_success']) {
  52. $protectedRecord = DB::table('juliang_account_promotion_protect_record')
  53. ->where(['adv_promotion_id' => $this->trackRecord->adv_promotion_id,
  54. 'optimizer_uid' => $this->promotion->uid, 'is_enabled' => 1])
  55. ->orderBy('id','desc')
  56. ->first();
  57. if($protectedRecord) {
  58. DB::table('juliang_account_promotion_protect_record')
  59. ->where(['id' => $protectedRecord->id])
  60. ->where('protected_num', '<', $this->callbackConfig->protect_num)
  61. ->increment('protected_num', 1, ['updated_at' => $now]);
  62. } else {
  63. DB::table('juliang_account_promotion_protect_record')
  64. ->insert([
  65. 'adv_promotion_id' => $this->trackRecord->adv_promotion_id ?? '',
  66. 'optimizer_uid' => $this->promotion->uid,
  67. 'advertiser_id' => $this->callbackConfig->adv_account_id,
  68. 'protected_num' => 1,
  69. 'is_enabled' => 1,
  70. 'created_at' => $now,
  71. 'updated_at' => $now,
  72. ]);
  73. }
  74. }
  75. }
  76. $chargeRecord = DB::table('callback_report_charge_record')->where([
  77. 'order_no' => $this->order->trade_no
  78. ])->first();
  79. if(!$chargeRecord) {
  80. DB::table('callback_report_charge_record')
  81. ->insert([
  82. 'uid' => $this->uid,
  83. 'order_id' => $this->order->id,
  84. 'order_no' => $this->order->trade_no,
  85. 'optimizer_uid' => $this->promotion->uid,
  86. 'filter_type' => $this->result['info_type'] ?? 'ok',
  87. 'filter_reason' => $this->result['info_str'] ?? '允许回传',
  88. 'report_success' => intval($this->result['report_success'] ?? false),
  89. 'report_result' => \json_encode($this->result['report_result'] ?? []),
  90. 'report_param' => \json_encode($this->result['report_param'] ?? []),
  91. 'created_at' => $now,
  92. 'updated_at' => $now,
  93. 'advertiser_id' => $this->trackRecord->advertiser_id ?? '',
  94. 'adv_promotion_id' => $this->trackRecord->adv_promotion_id ?? '',
  95. 'config_rate' => $this->result['config_rate'],
  96. 'current_rate' => $this->result['current_rate'],
  97. 'user_ranse_start_at' => $this->trackRecord->ranse_start_at ?? null,
  98. 'order_price' => $this->order->price,
  99. 'order_created_at' => $this->order->created_at,
  100. 'user_ranse_ip' => $this->trackRecord->ranse_ip ?? '',
  101. ]);
  102. } else {
  103. DB::table('callback_report_charge_record')
  104. ->where(['id' => $chargeRecord->id])
  105. ->update([
  106. 'uid' => $this->uid,
  107. 'order_id' => $this->order->id,
  108. 'order_no' => $this->order->trade_no,
  109. 'optimizer_uid' => $this->promotion->uid,
  110. 'filter_type' => $this->result['info_type'] ?? 'ok',
  111. 'filter_reason' => $this->result['info_str'] ?? '允许回传',
  112. 'report_success' => intval($this->result['report_success'] ?? false),
  113. 'report_result' => \json_encode($this->result['report_result'] ?? []),
  114. 'report_param' => \json_encode($this->result['report_param'] ?? []),
  115. 'updated_at' => $now,
  116. 'advertiser_id' => $this->trackRecord->advertiser_id ?? '',
  117. 'adv_promotion_id' => $this->trackRecord->adv_promotion_id ?? '',
  118. 'config_rate' => $this->result['config_rate'],
  119. 'current_rate' => $this->result['current_rate'],
  120. 'user_ranse_start_at' => $this->trackRecord->ranse_start_at ?? null,
  121. 'order_price' => $this->order->price,
  122. 'order_created_at' => $this->order->created_at,
  123. 'user_ranse_ip' => $this->trackRecord->ranse_ip ?? '',
  124. ]);
  125. }
  126. if($this->rateConfig) {
  127. if('ok' == $this->result['info_type'] && $this->result['report_success']) {
  128. DB::table('juliang_account_rate_config_log')
  129. ->where(['id' => $this->rateConfig->id])
  130. ->update([
  131. 'report_count' => $this->rateConfig->report_count + 1,
  132. 'total_count' => $this->rateConfig->total_count + 1,
  133. 'updated_at' => $now,
  134. ]);
  135. } elseif ('rate_filter' == $this->result['info_type']) {
  136. DB::table('juliang_account_rate_config_log')
  137. ->where(['id' => $this->rateConfig->id])
  138. ->update([
  139. 'unreport_count' => $this->rateConfig->unreport_count + 1,
  140. 'total_count' => $this->rateConfig->total_count + 1,
  141. 'updated_at' => $now,
  142. ]);
  143. }
  144. }
  145. }
  146. }
  147. /**
  148. * 判断回传比例
  149. */
  150. private function judgeCallbackRate(){
  151. if(!$this->result['need_report']) {
  152. return;
  153. }
  154. if(!$this->result['continue_judge']) {
  155. return;
  156. }
  157. $this->rateConfig = DB::table('juliang_account_rate_config_log')
  158. ->where(['company_uid' => $this->promotion->uid,
  159. 'account_id' => $this->trackRecord->advertiser_id, 'is_enabled' => 1])
  160. ->first();
  161. if(!$this->rateConfig) {
  162. $this->result['need_report'] = false;
  163. $this->result['info_type'] = 'no_rate_config';
  164. $this->result['info_str'] = '没有可用的回传比率设置';
  165. return;
  166. }
  167. $configRate = $this->rateConfig->config_per;
  168. if(0 == $configRate) {
  169. $this->result['need_report'] = false;
  170. $this->result['info_type'] = 'rate_eq_0';
  171. $this->result['info_str'] = '设定比例为0不执行回传';
  172. return;
  173. }
  174. $currentTotalCount = $this->rateConfig->total_count;
  175. $currentReportCount = $this->rateConfig->report_count;
  176. if(0 == $currentTotalCount) {
  177. $currentRate = 0;
  178. } else {
  179. $currentRate = min(1, round($currentReportCount / $currentTotalCount, 4)) * 100;
  180. }
  181. if($currentRate <= $configRate) {
  182. $this->result['need_report'] = true;
  183. $this->result['config_rate'] = $configRate;
  184. $this->result['current_rate'] = $currentRate;
  185. } else {
  186. $this->result['need_report'] = false;
  187. $this->result['info_type'] = 'rate_filter';
  188. $this->result['info_str'] = '比率过滤';
  189. $this->result['config_rate'] = $configRate;
  190. $this->result['current_rate'] = $currentRate;
  191. }
  192. }
  193. /**
  194. * 判断计划是否在保护
  195. */
  196. private function judgePromotionProtect() {
  197. if(!$this->result['need_report']) {
  198. return;
  199. }
  200. if($this->callbackConfig->protect_num <= 0) {
  201. $this->result['need_report'] = true;
  202. $this->result['continue_judge'] = true;
  203. } else {
  204. $advPromotionId = $this->trackRecord->adv_promotion_id;
  205. $protectedRecord = DB::table('juliang_account_promotion_protect_record')
  206. ->where(['adv_promotion_id' => $advPromotionId, 'optimizer_uid' => $this->promotion->uid, 'is_enabled' => 1])
  207. ->first();
  208. if(!$protectedRecord || $protectedRecord->protected_num < $this->callbackConfig->protect_num) {
  209. $this->result['need_report'] = true;
  210. $this->result['save_record'] = true;
  211. $this->result['continue_judge'] = false;
  212. $this->result['save_record_type'] = 'protect_promotion';
  213. $this->result['info_type'] = 'promotion_protect';
  214. $this->result['info_str'] = '计划被保护';
  215. }
  216. }
  217. }
  218. /**
  219. * 判断订单是不是已经处理过了.如果处理过了,就不入库
  220. */
  221. private function judgeOrderAlreadyDeal() {
  222. if(!$this->result['need_report']) {
  223. return;
  224. }
  225. if(DB::table('callback_report_charge_record')
  226. ->where(['order_id' => $this->order->id])
  227. ->exists()) {
  228. $this->result['need_report'] = false;
  229. $this->result['save_record'] = false;
  230. $this->result['info_type'] = 'order_exists';
  231. $this->result['info_str'] = '订单已经处理';
  232. }
  233. }
  234. /**
  235. * 判断充值的金额区间
  236. */
  237. private function judgeChargeMoney() {
  238. if(!$this->result['need_report']) {
  239. return;
  240. }
  241. $moneyRange = $this->callbackConfig->moneyRange;
  242. if($this->order->price >= $moneyRange->min && $this->order->price <= $moneyRange->max) {
  243. $this->result['need_report'] = true;
  244. } else {
  245. $this->result['need_report'] = false;
  246. $this->result['info_type'] = 'money_filter';
  247. $this->result['info_str'] = sprintf('充值金额[%s元]不在回传区间范围内[%s - %s]', $this->order->price,
  248. $moneyRange->minStr, $moneyRange->maxStr);
  249. }
  250. }
  251. /**
  252. * 判断是否是首充,只有首充回传
  253. */
  254. private function judgeIsFirstCharge() {
  255. if(!$this->result['need_report']) {
  256. return;
  257. }
  258. $isFirstCharge = DB::table('orders')
  259. ->where([
  260. 'uid' => $this->uid,
  261. 'status' => 'paid',
  262. 'promotion_id' => $this->trackRecord->ranse_id,
  263. ])->where('created_at', '>=', $this->trackRecord->ranse_start_at)
  264. ->where('created_at', '<=', $this->trackRecord->ranse_end_at)
  265. ->where('id', '<', $this->order->id)
  266. ->count() == 0;
  267. if($isFirstCharge) {
  268. $this->result['need_report'] = true;
  269. } else {
  270. $this->result['need_report'] = false;
  271. $this->result['info_type'] = 'neq_first_charge';
  272. $this->result['info_str'] = '非首充';
  273. }
  274. }
  275. /**
  276. * 获取订单的染色信息,回传配置信息
  277. */
  278. private function fillRanseInfo() {
  279. $ranseId = $this->order->promotion_id;
  280. $this->promotion = DB::table('promotions')->where('id', $ranseId)->first();
  281. $this->callbackConfig = DB::table('juliang_account_callback_config')
  282. ->where(['id' => $this->promotion->callback_config_id])
  283. ->first();
  284. if(!$this->callbackConfig) {
  285. $this->result['need_report'] = false;
  286. $this->result['info_type'] = 'no_callback_config';
  287. $this->result['info_str'] = '没有回传配置';
  288. return;
  289. }
  290. if(0 == $this->callbackConfig->state) {
  291. $this->result['need_report'] = false;
  292. $this->result['info_type'] = 'callback_close';
  293. $this->result['info_str'] = '回传配置关闭';
  294. return;
  295. }
  296. $min = max(0, $this->callbackConfig->min_money);
  297. if (0 >= $this->callbackConfig->max_money) {
  298. $max = PHP_INT_MAX;
  299. $maxStr = '不限';
  300. } else {
  301. $max = $this->callbackConfig->max_money;
  302. $maxStr = $max . '元';
  303. }
  304. $minStr = $min . '元';
  305. $this->callbackConfig->moneyRange = (object)compact('min', 'max', 'minStr', 'maxStr');
  306. }
  307. private function reportJuliang() {
  308. if(!$this->result['need_report']) {
  309. return;
  310. }
  311. $service = new TiktokEventReportService;
  312. $reportResult = $service->reportCharge((object)[
  313. 'callback' => $this->trackRecord->callback
  314. ]);
  315. $reportContent = \json_decode($reportResult['content'], true);
  316. if($reportResult['result'] && 0 == $reportContent['code']) {
  317. $this->result['report_success'] = true;
  318. } else {
  319. $this->result['report_success'] = false;
  320. }
  321. $this->result['report_result'] = $reportResult;
  322. $this->result['need_report'] = false;
  323. }
  324. /**
  325. * 获取匹配的广告用户信息,主要是获取回传 callback
  326. */
  327. private function fillTrackInfo() {
  328. if(!$this->result['need_report']) {
  329. return;
  330. }
  331. $this->trackRecord = DB::table('callback_report_ranse_record')
  332. ->where(['uid' => $this->uid, 'ranse_id' => $this->order->promotion_id])
  333. ->where('ranse_start_at', '<=', $this->order->created_at)
  334. ->where('ranse_end_at', '>=', $this->order->created_at)
  335. ->orderBy('id', 'desc')
  336. ->first();
  337. if(!$this->trackRecord) {
  338. $this->result['need_report'] = false;
  339. $this->result['info_type'] = 'no_track_info';
  340. $this->result['info_str'] = '无匹配用户';
  341. return;
  342. }
  343. $this->result['report_param'] = [
  344. 'callback' => $this->trackRecord->callback,
  345. ];
  346. }
  347. }