JuliangAccountReportChargeService.php 14 KB

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