123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- namespace General\Services\LandingPage;
- use App\Consts\BaseConst;
- use General\Models\Order\Order;
- use General\Models\LandingPage\LandingPageDomain;
- use General\Models\LandingPage\LandingPageLink;
- use General\Models\LandingPage\LandingPageLinkLog;
- use General\Models\OfficialAccount\OfficialAccount;
- use General\Models\Report\BaiDuAdAccount;
- use General\Services\Config\ConfigService;
- use General\Services\LandingPage\ChargeFeedBack\NormalChargeFeedBack;
- use General\Services\LandingPage\ChargeFeedBack\OrangeChargeFeedBack;
- use General\Services\LandingPage\ChargeFeedBack\QappRereport;
- use General\Services\LandingPage\ChargeFeedBack\Rereport;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\DB;
- use Redis;
- class LandingPageLinkService
- {
- /**
- * 新建状态
- */
- const NEW_STATUS = 1;
- /**
- * 待审核
- */
- const WAITTING_APPROVE_STATUS = 2;
- /**
- * 审核通过
- */
- const APPROVED_STATUS = 3;
- /**
- * 审核未通过
- */
- const UNAPPROVED_STATUS = 4;
- /**
- * 按付费时间回传
- */
- const COMMON = 'common';
- /**
- * 回传关注当天注册且充值用户
- */
- const CURRENT_DAY_REGISTER = 'current_day_register';
- /**
- * 按注册时间回传
- */
- const REGISTER_TIME_RETURN = 'register_time_return';
- /**
- * 注册24小时之内充值
- */
- const REGISTER_24_CHARGE = 'register_24_charge';
- /**
- * 回传类型
- */
- const REPORT_TYPE = [
- self::COMMON,
- self::CURRENT_DAY_REGISTER,
- self::REGISTER_TIME_RETURN,
- self::REGISTER_24_CHARGE,
- ];
- const tiktok = 'tiktok';
- const iqiyi = 'iqiyi';
- const uc = 'uc';
- const uchc = 'uchc';
- const weibo = 'weibo';
- const tencent = 'tencent';
- const kuaishou = 'kuaishou';
- const vivo = 'vivo';
- const baidu = 'baidu';
- const link_source_array = [
- self::tiktok,
- self::iqiyi,
- self::uc,
- self::uchc,
- self::weibo,
- self::tencent,
- self::kuaishou,
- self::vivo,
- self::baidu,
- ];
- public function saveLink(array $data)
- {
- if (isset($data['id'])) {
- $data['updated_at'] = now();
- return LandingPageLink::updateOrCreate(['id' => $data['id']], $data);
- } else {
- return LandingPageLink::create($data);
- }
- }
- public function deleteLink(int $id)
- {
- LandingPageLink::where('id', $id)->delete();
- }
- public function updateLinkStatus(int $id, int $status, string $remark)
- {
- if ($status) {
- $this->saveLink(['id' => $id, 'status' => $status, 'remark' => $remark]);
- LandingPageLinkLog::create(['link_id' => $id, 'status' => $status, 'remark' => $remark]);
- }
- }
- public function findLinks(array $query_params = [], bool $is_page = false)
- {
- $sql = LandingPageLink::orderBy('id', 'desc');
- if (isset($query_params['name']) && $query_params['name']) {
- $sql->where('name', 'like', $query_params['name'] . '%');
- }
- if (isset($query_params['gzh_name']) && $query_params['gzh_name']) {
- $sql->where('gzh_name', 'like', $query_params['gzh_name'] . '%');
- }
- if (isset($query_params['gzh_code']) && $query_params['gzh_code']) {
- $sql->where('gzh_code', $query_params['gzh_code']);
- }
- if (isset($query_params['status']) && $query_params['status']) {
- $sql->where('status', $query_params['status']);
- }
- if (isset($query_params['channel_id']) && $query_params['channel_id']) {
- $sql->where('channel_id', $query_params['channel_id']);
- }
- if ($is_page) {
- return $sql->paginate(20);
- } else {
- return $sql->get();
- }
- }
- public function findDomains()
- {
- return LandingPageDomain::where('is_enabled', 1)->get();
- }
- public function findOfficialAccounts(int $distribution_channel_id)
- {
- return OfficialAccount::select('nickname', 'appid', 'alias', 'head_img')
- ->where('distribution_channel_id', $distribution_channel_id)
- ->where('is_auth', 1)
- ->where('is_enabled', 1)
- ->get();
- }
- /**
- * 获取付费回传配置信息
- */
- public function getChargeFeedBackConfig(int $channel_id, int $channel_user_id)
- {
- $switch_status = (new NormalChargeFeedBack)->getSwitchStatus($channel_id);
- $orange_switch_status = (new OrangeChargeFeedBack)->getSwitchStatus($channel_id);
- $type = Redis::hGet('channel:setting:' . $channel_id, 'tiktok_report_type') ?? self::CURRENT_DAY_REGISTER;
- $service = new ConfigService;
- $source = BaseConst::Platform;
- $result = [
- 'orange_switch_status' => $orange_switch_status,
- 'switch_status' => $switch_status,
- 'type' => $type,
- '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}",
- '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__"
- ];
- $is_open = $service->hasAuth($channel_user_id, 'platform_charge_percent_report');
- if ($is_open) {
- $percent = Redis::hGet('channel:setting:' . $channel_id, 'tiktok_report_percent');
- if ($percent) {
- $percent = json_decode($percent, true);
- $molecule = round($percent['molecule'] / $percent['denominator'] * 100);
- } else {
- $molecule = (int) Redis::hGet('channel:setting:' . $channel_id, 'new_tiktok_report_percent');
- }
- $eligible_count = Redis::hGet('channel:setting:' . $channel_id, 'tiktok_report_eligible_count');
- return array_merge([
- 'molecule' => $molecule,
- 'eligible_count' => $eligible_count,
- ], $result);
- }
- return $result;
- }
- private function getService(bool $is_orange)
- {
- return $is_orange ? new OrangeChargeFeedBack : new NormalChargeFeedBack;
- }
- public function setSwitchStatus(int $channel_id, int $status, bool $is_orange = false)
- {
- $this->getService($is_orange)->setSwitchStatus($channel_id, $status);
- }
- public function syncSwitchStatus(array $channel_ids, int $status, bool $is_orange = false)
- {
- $this->getService($is_orange)->syncSwitchStatus($channel_ids, $status);
- }
- public function findUserReportInfos(int $channel_id, array $params, bool $is_page = true)
- {
- $sql = Order::where('orders.distribution_channel_id', $channel_id)
- ->where('orders.status', 'PAID')
- ->select('orders.*')
- ->orderBy('orders.id', 'desc');
- //限制用户的创建时间
- if (isset($params['user_begin_time']) || isset($params['user_end_time'])) {
- $sql->join('users','users.id','orders.uid');
- if (isset($params['user_begin_time']) && $params['user_begin_time']) {
- $date = substr($params['user_begin_time'],0,10);
- $sql->where('users.created_at', '>=', $date. ' 00:00:00');
- }
- if (isset($params['user_end_time']) && $params['user_end_time']) {
- $date = substr($params['user_end_time'],0,10);
- $sql->where('users.created_at', '<=', $date. ' 23:59:59');
- }
- }
- if (isset($params['begin_time']) && $params['begin_time']) {
- $date = substr($params['begin_time'],0,10);
- $sql->where('orders.created_at', '>=', $date. ' 00:00:00');
- }
- if (isset($params['end_time']) && $params['begin_time']) {
- $date = substr($params['end_time'],0,10);
- $sql->where('orders.created_at', '<=', $date. ' 23:59:59');
- }
- if (isset($params['uid']) && $params['uid']) {
- $sql->where('orders.uid', $params['uid']);
- }
- //限制派单ID
- if (isset($params['send_order_id']) && $params['send_order_id']) {
- $sql->where('orders.send_order_id', $params['send_order_id']);
- }
- if (isset($params['adid']) && $params['adid']) {
- $sql->whereExists(function ($query) use ($params) {
- $query->select(DB::raw(1))
- ->from('report_user_bind_records')
- ->whereRaw('report_user_bind_records.uid = orders.uid')
- ->where('report_user_bind_records.adid', $params['adid']);
- });
- }
- if (isset($params['link_source']) && $params['link_source']) {
- $sql->whereExists(function ($query) use ($params) {
- $query->select(DB::raw(1))
- ->from('report_user_charge_records')
- ->whereRaw('report_user_charge_records.order_no = orders.trade_no')
- ->where('report_user_charge_records.link_source', $params['link_source']);
- });
- }
-
- $qapp_account = isset($params['qapp_account']) && !empty($params['qapp_account']) ? $params['qapp_account']:'';
- \Log::info('findUserReportInfos:channel_id:'.$channel_id.' qapp_account:'.$qapp_account.' params:'.json_encode($params));
-
- // 注意:快应用的子账号必须要限制下权限
- if (!empty($qapp_account)) {
- $user = DB::table('qapp_channel_accounts')->where('account',$params['qapp_account'])->select(['is_owner_role','company_id','distribution_channel_id'])->first();
- $is_owner_role = $user->is_owner_role;
- if (!empty($params['nickname']) && $is_owner_role ) {
- //根据名称搜索
- $sql->join('qapp_send_orders', 'orders.send_order_id', 'qapp_send_orders.send_order_id')
- ->where('qapp_send_orders.account', $params['nickname']);
- }else{
- // 主账号
- if ($is_owner_role) {
- $qapp_account_arr = DB::table('qapp_channel_accounts')->where('company_id',$user->company_id)
- ->where('distribution_channel_id',$user->distribution_channel_id)
- ->pluck('account')->all();
- }else{
- $qapp_account_arr = [$params['qapp_account']];
- }
- $sql->join('qapp_send_orders', 'orders.send_order_id', 'qapp_send_orders.send_order_id')
- ->whereIn('qapp_send_orders.account', $qapp_account_arr);
- }
- // 限制 回传方式--》 根据回传方式找派单
- if(isset($params['report_type']) && $params['report_type']){
- $sql->where('report_type',$params['report_type']);
- }
- }
-
- if ($is_page) {
- return $sql->paginate();
- } else {
- return $sql->get();
- }
- }
- public function setReportType(int $channel_id, string $type, int $molecule, int $eligible_count)
- {
- if (in_array($type, LandingPageLinkService::REPORT_TYPE)) {
- $molecule = $molecule == 0 ? 100 : $molecule;
- Redis::hset('channel:setting:' . $channel_id, 'tiktok_report_type', $type);
- Redis::hDel('channel:setting:' . $channel_id, 'tiktok_report_percent');
- Redis::hset('channel:setting:' . $channel_id, 'new_tiktok_report_percent', $molecule);
- Redis::hset('channel:setting:' . $channel_id, 'tiktok_report_eligible_count', $eligible_count);
- $service = new Report;
- $service->report('api/report/config', [
- 'source' => BaseConst::Platform,
- 'channel_id' => $channel_id,
- 'type' => $type,
- 'molecule' => $molecule,
- 'denominator' => 100,
- 'eligible_count' => $eligible_count,
- ]);
- }
- }
- public function addBaiduAdAccount(array $data, int $channel_user_id)
- {
- $model = BaiDuAdAccount::create(
- [
- 'account_name' => $data['account_name'],
- 'token' => $data['token'],
- 'channel_user_id' => $channel_user_id,
- 'is_enabled' => 1,
- ]
- );
- $this->baiDuAdAccountReport($model);
- }
- public function delBaiduAdAccount(int $id, int $channel_user_id)
- {
- $model = BaiDuAdAccount::where('id', $id)->where('channel_user_id', $channel_user_id)->first();
- if ($model) {
- $model->is_enabled = 0;
- $model->save();
- $this->baiDuAdAccountReport($model);
- }
- }
- private function baiDuAdAccountReport(BaiDuAdAccount $model)
- {
- $service = new Report;
- $service->report('api/report/baidu/token', [
- 'source' => BaseConst::Platform,
- 'adid' => $model->id,
- 'token' => $model->token,
- 'is_enabled' => $model->is_enabled,
- ]);
- }
- public function findBaiduAdAccounts(int $channel_user_id): Collection
- {
- return BaiDuAdAccount::where('is_enabled', 1)->where('channel_user_id', $channel_user_id)->get();
- }
- public function reReport(int $bind_id, float $amount, string $order_no, bool $is_qapp = false)
- {
- $service = $is_qapp ? new QappRereport : new Rereport;
- $service->reReport($bind_id, $amount, $order_no);
- }
- }
|