LandingPageLinkService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace General\Services\LandingPage;
  3. use App\Consts\BaseConst;
  4. use General\Models\Order\Order;
  5. use General\Models\LandingPage\LandingPageDomain;
  6. use General\Models\LandingPage\LandingPageLink;
  7. use General\Models\LandingPage\LandingPageLinkLog;
  8. use General\Models\OfficialAccount\OfficialAccount;
  9. use General\Models\Report\BaiDuAdAccount;
  10. use General\Services\Config\ConfigService;
  11. use General\Services\LandingPage\ChargeFeedBack\NormalChargeFeedBack;
  12. use General\Services\LandingPage\ChargeFeedBack\OrangeChargeFeedBack;
  13. use General\Services\LandingPage\ChargeFeedBack\QappRereport;
  14. use General\Services\LandingPage\ChargeFeedBack\Rereport;
  15. use Illuminate\Support\Collection;
  16. use Illuminate\Support\Facades\DB;
  17. use Redis;
  18. class LandingPageLinkService
  19. {
  20. /**
  21. * 新建状态
  22. */
  23. const NEW_STATUS = 1;
  24. /**
  25. * 待审核
  26. */
  27. const WAITTING_APPROVE_STATUS = 2;
  28. /**
  29. * 审核通过
  30. */
  31. const APPROVED_STATUS = 3;
  32. /**
  33. * 审核未通过
  34. */
  35. const UNAPPROVED_STATUS = 4;
  36. /**
  37. * 按付费时间回传
  38. */
  39. const COMMON = 'common';
  40. /**
  41. * 回传关注当天注册且充值用户
  42. */
  43. const CURRENT_DAY_REGISTER = 'current_day_register';
  44. /**
  45. * 按注册时间回传
  46. */
  47. const REGISTER_TIME_RETURN = 'register_time_return';
  48. /**
  49. * 注册24小时之内充值
  50. */
  51. const REGISTER_24_CHARGE = 'register_24_charge';
  52. /**
  53. * 回传类型
  54. */
  55. const REPORT_TYPE = [
  56. self::COMMON,
  57. self::CURRENT_DAY_REGISTER,
  58. self::REGISTER_TIME_RETURN,
  59. self::REGISTER_24_CHARGE,
  60. ];
  61. const tiktok = 'tiktok';
  62. const iqiyi = 'iqiyi';
  63. const uc = 'uc';
  64. const uchc = 'uchc';
  65. const weibo = 'weibo';
  66. const tencent = 'tencent';
  67. const kuaishou = 'kuaishou';
  68. const vivo = 'vivo';
  69. const baidu = 'baidu';
  70. const link_source_array = [
  71. self::tiktok,
  72. self::iqiyi,
  73. self::uc,
  74. self::uchc,
  75. self::weibo,
  76. self::tencent,
  77. self::kuaishou,
  78. self::vivo,
  79. self::baidu,
  80. ];
  81. public function saveLink(array $data)
  82. {
  83. if (isset($data['id'])) {
  84. $data['updated_at'] = now();
  85. return LandingPageLink::updateOrCreate(['id' => $data['id']], $data);
  86. } else {
  87. return LandingPageLink::create($data);
  88. }
  89. }
  90. public function deleteLink(int $id)
  91. {
  92. LandingPageLink::where('id', $id)->delete();
  93. }
  94. public function updateLinkStatus(int $id, int $status, string $remark)
  95. {
  96. if ($status) {
  97. $this->saveLink(['id' => $id, 'status' => $status, 'remark' => $remark]);
  98. LandingPageLinkLog::create(['link_id' => $id, 'status' => $status, 'remark' => $remark]);
  99. }
  100. }
  101. public function findLinks(array $query_params = [], bool $is_page = false)
  102. {
  103. $sql = LandingPageLink::orderBy('id', 'desc');
  104. if (isset($query_params['name']) && $query_params['name']) {
  105. $sql->where('name', 'like', $query_params['name'] . '%');
  106. }
  107. if (isset($query_params['gzh_name']) && $query_params['gzh_name']) {
  108. $sql->where('gzh_name', 'like', $query_params['gzh_name'] . '%');
  109. }
  110. if (isset($query_params['gzh_code']) && $query_params['gzh_code']) {
  111. $sql->where('gzh_code', $query_params['gzh_code']);
  112. }
  113. if (isset($query_params['status']) && $query_params['status']) {
  114. $sql->where('status', $query_params['status']);
  115. }
  116. if (isset($query_params['channel_id']) && $query_params['channel_id']) {
  117. $sql->where('channel_id', $query_params['channel_id']);
  118. }
  119. if ($is_page) {
  120. return $sql->paginate(20);
  121. } else {
  122. return $sql->get();
  123. }
  124. }
  125. public function findDomains()
  126. {
  127. return LandingPageDomain::where('is_enabled', 1)->get();
  128. }
  129. public function findOfficialAccounts(int $distribution_channel_id)
  130. {
  131. return OfficialAccount::select('nickname', 'appid', 'alias', 'head_img')
  132. ->where('distribution_channel_id', $distribution_channel_id)
  133. ->where('is_auth', 1)
  134. ->where('is_enabled', 1)
  135. ->get();
  136. }
  137. /**
  138. * 获取付费回传配置信息
  139. */
  140. public function getChargeFeedBackConfig(int $channel_id, int $channel_user_id)
  141. {
  142. $switch_status = (new NormalChargeFeedBack)->getSwitchStatus($channel_id);
  143. $orange_switch_status = (new OrangeChargeFeedBack)->getSwitchStatus($channel_id);
  144. $type = Redis::hGet('channel:setting:' . $channel_id, 'tiktok_report_type') ?? self::CURRENT_DAY_REGISTER;
  145. $service = new ConfigService;
  146. $source = BaseConst::Platform;
  147. $result = [
  148. 'orange_switch_status' => $orange_switch_status,
  149. 'switch_status' => $switch_status,
  150. 'type' => $type,
  151. 'weibo_monitor_link' => "https://newtrack.zhuishuyun.com/?dycallback=3&channel_id={$channel_id}&source={$source}&ip={ip}&ua={ua}&clicktime={clicktime}&IMP={IMP}&ad_id={ad_id}&creative_id={creative_id}",
  152. 'kuaishou_monitor_link' => "https://newtrack.zhuishuyun.com/?dycallback=6&channel_id={$channel_id}&source={$source}&aid=__AID__&dname=__DNAME__&cid=__CID__&imei=__IMEI__&androidid=__ANDROIDID2__&oaid=__OAID__&mac=__MAC__&ip=__IP__&ua=__UA__&ts=__TS__&callback=__CALLBACK__&imei=__IMEI2__&os=__OS__&did=__DID__&accountid=__ACCOUNTID__&csite=__CSITE__"
  153. ];
  154. $is_open = $service->hasAuth($channel_user_id, 'platform_charge_percent_report');
  155. if ($is_open) {
  156. $percent = Redis::hGet('channel:setting:' . $channel_id, 'tiktok_report_percent');
  157. if ($percent) {
  158. $percent = json_decode($percent, true);
  159. $molecule = round($percent['molecule'] / $percent['denominator'] * 100);
  160. } else {
  161. $molecule = (int) Redis::hGet('channel:setting:' . $channel_id, 'new_tiktok_report_percent');
  162. }
  163. $eligible_count = Redis::hGet('channel:setting:' . $channel_id, 'tiktok_report_eligible_count');
  164. return array_merge([
  165. 'molecule' => $molecule,
  166. 'eligible_count' => $eligible_count,
  167. ], $result);
  168. }
  169. return $result;
  170. }
  171. private function getService(bool $is_orange)
  172. {
  173. return $is_orange ? new OrangeChargeFeedBack : new NormalChargeFeedBack;
  174. }
  175. public function setSwitchStatus(int $channel_id, int $status, bool $is_orange = false)
  176. {
  177. $this->getService($is_orange)->setSwitchStatus($channel_id, $status);
  178. }
  179. public function syncSwitchStatus(array $channel_ids, int $status, bool $is_orange = false)
  180. {
  181. $this->getService($is_orange)->syncSwitchStatus($channel_ids, $status);
  182. }
  183. public function findUserReportInfos(int $channel_id, array $params, bool $is_page = true)
  184. {
  185. $sql = Order::where('orders.distribution_channel_id', $channel_id)
  186. ->where('orders.status', 'PAID')
  187. ->select('orders.*')
  188. ->orderBy('orders.id', 'desc');
  189. //限制用户的创建时间
  190. if (isset($params['user_begin_time']) || isset($params['user_end_time'])) {
  191. $sql->join('users','users.id','orders.uid');
  192. if (isset($params['user_begin_time']) && $params['user_begin_time']) {
  193. $date = substr($params['user_begin_time'],0,10);
  194. $sql->where('users.created_at', '>=', $date. ' 00:00:00');
  195. }
  196. if (isset($params['user_end_time']) && $params['user_end_time']) {
  197. $date = substr($params['user_end_time'],0,10);
  198. $sql->where('users.created_at', '<=', $date. ' 23:59:59');
  199. }
  200. }
  201. if (isset($params['begin_time']) && $params['begin_time']) {
  202. $date = substr($params['begin_time'],0,10);
  203. $sql->where('orders.created_at', '>=', $date. ' 00:00:00');
  204. }
  205. if (isset($params['end_time']) && $params['begin_time']) {
  206. $date = substr($params['end_time'],0,10);
  207. $sql->where('orders.created_at', '<=', $date. ' 23:59:59');
  208. }
  209. if (isset($params['uid']) && $params['uid']) {
  210. $sql->where('orders.uid', $params['uid']);
  211. }
  212. //限制派单ID
  213. if (isset($params['send_order_id']) && $params['send_order_id']) {
  214. $sql->where('orders.send_order_id', $params['send_order_id']);
  215. }
  216. if (isset($params['adid']) && $params['adid']) {
  217. $sql->whereExists(function ($query) use ($params) {
  218. $query->select(DB::raw(1))
  219. ->from('report_user_bind_records')
  220. ->whereRaw('report_user_bind_records.uid = orders.uid')
  221. ->where('report_user_bind_records.adid', $params['adid']);
  222. });
  223. }
  224. if (isset($params['link_source']) && $params['link_source']) {
  225. $sql->whereExists(function ($query) use ($params) {
  226. $query->select(DB::raw(1))
  227. ->from('report_user_charge_records')
  228. ->whereRaw('report_user_charge_records.order_no = orders.trade_no')
  229. ->where('report_user_charge_records.link_source', $params['link_source']);
  230. });
  231. }
  232. $qapp_account = isset($params['qapp_account']) && !empty($params['qapp_account']) ? $params['qapp_account']:'';
  233. \Log::info('findUserReportInfos:channel_id:'.$channel_id.' qapp_account:'.$qapp_account.' params:'.json_encode($params));
  234. // 注意:快应用的子账号必须要限制下权限
  235. if (!empty($qapp_account)) {
  236. $user = DB::table('qapp_channel_accounts')->where('account',$params['qapp_account'])->select(['is_owner_role','company_id','distribution_channel_id'])->first();
  237. $is_owner_role = $user->is_owner_role;
  238. if (!empty($params['nickname']) && $is_owner_role ) {
  239. //根据名称搜索
  240. $sql->join('qapp_send_orders', 'orders.send_order_id', 'qapp_send_orders.send_order_id')
  241. ->where('qapp_send_orders.account', $params['nickname']);
  242. }else{
  243. // 主账号
  244. if ($is_owner_role) {
  245. $qapp_account_arr = DB::table('qapp_channel_accounts')->where('company_id',$user->company_id)
  246. ->where('distribution_channel_id',$user->distribution_channel_id)
  247. ->pluck('account')->all();
  248. }else{
  249. $qapp_account_arr = [$params['qapp_account']];
  250. }
  251. $sql->join('qapp_send_orders', 'orders.send_order_id', 'qapp_send_orders.send_order_id')
  252. ->whereIn('qapp_send_orders.account', $qapp_account_arr);
  253. }
  254. // 限制 回传方式--》 根据回传方式找派单
  255. if(isset($params['report_type']) && $params['report_type']){
  256. $sql->where('report_type',$params['report_type']);
  257. }
  258. }
  259. if ($is_page) {
  260. return $sql->paginate();
  261. } else {
  262. return $sql->get();
  263. }
  264. }
  265. public function setReportType(int $channel_id, string $type, int $molecule, int $eligible_count)
  266. {
  267. if (in_array($type, LandingPageLinkService::REPORT_TYPE)) {
  268. $molecule = $molecule == 0 ? 100 : $molecule;
  269. Redis::hset('channel:setting:' . $channel_id, 'tiktok_report_type', $type);
  270. Redis::hDel('channel:setting:' . $channel_id, 'tiktok_report_percent');
  271. Redis::hset('channel:setting:' . $channel_id, 'new_tiktok_report_percent', $molecule);
  272. Redis::hset('channel:setting:' . $channel_id, 'tiktok_report_eligible_count', $eligible_count);
  273. $service = new Report;
  274. $service->report('api/report/config', [
  275. 'source' => BaseConst::Platform,
  276. 'channel_id' => $channel_id,
  277. 'type' => $type,
  278. 'molecule' => $molecule,
  279. 'denominator' => 100,
  280. 'eligible_count' => $eligible_count,
  281. ]);
  282. }
  283. }
  284. public function addBaiduAdAccount(array $data, int $channel_user_id)
  285. {
  286. $model = BaiDuAdAccount::create(
  287. [
  288. 'account_name' => $data['account_name'],
  289. 'token' => $data['token'],
  290. 'channel_user_id' => $channel_user_id,
  291. 'is_enabled' => 1,
  292. ]
  293. );
  294. $this->baiDuAdAccountReport($model);
  295. }
  296. public function delBaiduAdAccount(int $id, int $channel_user_id)
  297. {
  298. $model = BaiDuAdAccount::where('id', $id)->where('channel_user_id', $channel_user_id)->first();
  299. if ($model) {
  300. $model->is_enabled = 0;
  301. $model->save();
  302. $this->baiDuAdAccountReport($model);
  303. }
  304. }
  305. private function baiDuAdAccountReport(BaiDuAdAccount $model)
  306. {
  307. $service = new Report;
  308. $service->report('api/report/baidu/token', [
  309. 'source' => BaseConst::Platform,
  310. 'adid' => $model->id,
  311. 'token' => $model->token,
  312. 'is_enabled' => $model->is_enabled,
  313. ]);
  314. }
  315. public function findBaiduAdAccounts(int $channel_user_id): Collection
  316. {
  317. return BaiDuAdAccount::where('is_enabled', 1)->where('channel_user_id', $channel_user_id)->get();
  318. }
  319. public function reReport(int $bind_id, float $amount, string $order_no, bool $is_qapp = false)
  320. {
  321. $service = $is_qapp ? new QappRereport : new Rereport;
  322. $service->reReport($bind_id, $amount, $order_no);
  323. }
  324. }