JuliangAccountReportChargeService.php 15 KB

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