JuliangAccountReportChargeService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. dump($this->result);
  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. private function judgeCallbackRate(){
  148. if(!$this->result['need_report']) {
  149. return;
  150. }
  151. $this->rateConfig = DB::table('juliang_account_rate_config_log')
  152. ->where(['company_uid' => $this->promotion->uid,
  153. 'account_id' => $this->trackRecord->advertiser_id, 'is_enabled' => 1])
  154. ->first();
  155. if(!$this->rateConfig) {
  156. $this->result['need_report'] = false;
  157. $this->result['info_type'] = 'no_rate_config';
  158. $this->result['info_str'] = '没有可用的回传比率设置';
  159. return;
  160. }
  161. $configRate = $this->rateConfig->config_per;
  162. if(0 == $configRate) {
  163. $this->result['need_report'] = false;
  164. $this->result['info_type'] = 'rate_eq_0';
  165. $this->result['info_str'] = '设定比例为0不执行回传';
  166. return;
  167. }
  168. $currentTotalCount = $this->rateConfig->total_count;
  169. $currentReportCount = $this->rateConfig->report_count;
  170. if(0 == $currentTotalCount) {
  171. $currentRate = 0;
  172. } else {
  173. $currentRate = min(1, round($currentReportCount / $currentTotalCount, 4)) * 100;
  174. }
  175. if($currentRate <= $configRate) {
  176. $this->result['need_report'] = true;
  177. $this->result['config_rate'] = $configRate;
  178. $this->result['current_rate'] = $currentRate;
  179. } else {
  180. $this->result['need_report'] = false;
  181. $this->result['info_type'] = 'rate_filter';
  182. $this->result['info_str'] = '比率过滤';
  183. $this->result['config_rate'] = $configRate;
  184. $this->result['current_rate'] = $currentRate;
  185. }
  186. }
  187. private function judgePromotionProtect() {
  188. if(!$this->result['need_report']) {
  189. return;
  190. }
  191. if(0 == $this->callbackConfig->protect_num) {
  192. $this->result['need_report'] = true;
  193. } else {
  194. $advPromotionId = $this->trackRecord->adv_promotion_id;
  195. $protectedRecord = DB::table('juliang_account_promotion_protect_record')
  196. ->where(['adv_promotion_id' => $advPromotionId, 'optimizer_uid' => $this->promotion->uid, 'is_enabled' => 1])
  197. ->first();
  198. if(!$protectedRecord || $protectedRecord->protected_num < $this->callbackConfig->protect_num) {
  199. $this->result['need_report'] = true;
  200. $this->result['save_record'] = true;
  201. $this->result['save_record_type'] = 'protect_promotion';
  202. $this->result['info_type'] = 'promotion_protect';
  203. $this->result['info_str'] = '计划被保护';
  204. }
  205. }
  206. }
  207. private function judgeOrderAlreadyDeal() {
  208. if(!$this->result['need_report']) {
  209. return;
  210. }
  211. if(DB::table('callback_report_charge_record')
  212. ->where(['order_id' => $this->order->id])
  213. ->exists()) {
  214. $this->result['need_report'] = false;
  215. $this->result['save_record'] = false;
  216. $this->result['info_type'] = 'order_exists';
  217. $this->result['info_str'] = '订单已经处理';
  218. }
  219. }
  220. private function judgeChargeMoney() {
  221. if(!$this->result['need_report']) {
  222. return;
  223. }
  224. $moneyRange = $this->callbackConfig->moneyRange;
  225. if($this->order->price >= $moneyRange->min && $this->order->price <= $moneyRange->max) {
  226. $this->result['need_report'] = true;
  227. } else {
  228. $this->result['need_report'] = false;
  229. $this->result['info_type'] = 'money_filter';
  230. $this->result['info_str'] = sprintf('充值金额[%s元]不在回传区间范围内[%s - %s]', $this->order->price,
  231. $moneyRange->minStr, $moneyRange->maxStr);
  232. }
  233. }
  234. private function judgeIsFirstCharge() {
  235. if(!$this->result['need_report']) {
  236. return;
  237. }
  238. $isFirstCharge = DB::table('orders')
  239. ->where([
  240. 'uid' => $this->uid,
  241. 'status' => 'paid',
  242. 'promotion_id' => $this->trackRecord->ranse_id,
  243. ])->where('created_at', '>=', $this->trackRecord->ranse_start_at)
  244. ->where('created_at', '<=', $this->trackRecord->ranse_end_at)
  245. ->where('id', '<', $this->order->id)
  246. ->count() == 0;
  247. if($isFirstCharge) {
  248. $this->result['need_report'] = true;
  249. } else {
  250. $this->result['need_report'] = false;
  251. $this->result['info_type'] = 'neq_first_charge';
  252. $this->result['info_str'] = '非首充';
  253. }
  254. }
  255. private function fillRanseInfo() {
  256. $ranseId = $this->order->promotion_id;
  257. $this->promotion = DB::table('promotions')->where('id', $ranseId)->first();
  258. $this->callbackConfig = DB::table('juliang_account_callback_config')
  259. ->where(['id' => $this->promotion->callback_config_id])
  260. ->first();
  261. if(!$this->callbackConfig) {
  262. $this->result['need_report'] = false;
  263. $this->result['info_type'] = 'no_callback_config';
  264. $this->result['info_str'] = '没有回传配置';
  265. return;
  266. }
  267. if(0 == $this->callbackConfig->state) {
  268. $this->result['need_report'] = false;
  269. $this->result['info_type'] = 'callback_close';
  270. $this->result['info_str'] = '回传配置关闭';
  271. return;
  272. }
  273. $min = max(0, $this->callbackConfig->min_money);
  274. if (0 >= $this->callbackConfig->max_money) {
  275. $max = PHP_INT_MAX;
  276. $maxStr = '不限';
  277. } else {
  278. $max = $this->callbackConfig->max_money;
  279. $maxStr = $max . '元';
  280. }
  281. $minStr = $min . '元';
  282. $this->callbackConfig->moneyRange = (object)compact('min', 'max', 'minStr', 'maxStr');
  283. }
  284. private function reportJuliang() {
  285. if(!$this->result['need_report']) {
  286. return;
  287. }
  288. $service = new TiktokEventReportService;
  289. $reportResult = $service->reportCharge((object)[
  290. 'callback' => $this->trackRecord->callback
  291. ]);
  292. $reportContent = \json_decode($reportResult['content'], true);
  293. if($reportResult['result'] && 0 == $reportContent['code']) {
  294. $this->result['report_success'] = true;
  295. } else {
  296. $this->result['report_success'] = false;
  297. }
  298. /**
  299. * 要删除
  300. */
  301. $this->result['report_success'] = (bool)rand(0,1);
  302. $this->result['report_result'] = $reportResult;
  303. $this->result['need_report'] = false;
  304. }
  305. private function fillTrackInfo() {
  306. if(!$this->result['need_report']) {
  307. return;
  308. }
  309. $this->trackRecord = DB::table('callback_report_ranse_record')
  310. ->where(['uid' => $this->uid, 'ranse_id' => $this->order->promotion_id])
  311. ->where('ranse_start_at', '<=', $this->order->created_at)
  312. ->where('ranse_end_at', '>=', $this->order->created_at)
  313. ->orderBy('id', 'desc')
  314. ->first();
  315. if(!$this->trackRecord) {
  316. $this->result['need_report'] = false;
  317. $this->result['info_type'] = 'no_track_info';
  318. $this->result['info_str'] = '无匹配用户';
  319. return;
  320. }
  321. $this->result['report_param'] = [
  322. 'callback' => $this->trackRecord->callback,
  323. ];
  324. }
  325. }