BidController.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. <?php
  2. /**
  3. * 竞价管理
  4. * @copyright 2021-浙江引擎力营销策划有限公司
  5. * @author Lc<sunshinecc1@163.com>
  6. * @since 2021-08-23
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Models\Role;
  11. use App\Http\Models\Site;
  12. use App\Http\Models\SitePayment;
  13. use App\Http\Models\User;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Support\Facades\DB;
  17. use App\Http\Models\Contract;
  18. class BidController extends Controller
  19. {
  20. //竞价列表
  21. public function siteList(Request $request, $type)
  22. {
  23. if (!$request->ajax()) {
  24. $userList = User::query()->select(['id', 'nickname'])->where(['status' => 1])->whereIn('role_id', [16, 14, 27])->get();
  25. return view('admin.bid.site_list', [
  26. 'userList' => $userList,
  27. 'type' => $type
  28. ]);
  29. }
  30. if ($status = $request->input('status')) {
  31. $condition['status'] = $status;
  32. }
  33. if ($request->input('bid_status') == 'on') {
  34. $condition['bid_status'] = 1;
  35. }
  36. //已投放个数
  37. if ($type == 1) {
  38. $condition['bid_status'] = 1;
  39. }
  40. //待投放个数
  41. if ($type == 2) {
  42. $condition['bid_status'] = 0;
  43. $status = [2, 3, 5, 6, 7, 8, 9];
  44. $condition[] = [function ($query) use ($status) {
  45. $query->whereIn('status', $status);
  46. }];
  47. }
  48. //已暂停放个数
  49. if ($type == 3) {
  50. $siteIdList = Site::query()->whereJsonContains('contract_ids', "3")->pluck('id');
  51. $siteIds = SitePayment::query()->whereIn('site_id', $siteIdList)->whereNotNull('bid_account_pause_time')->pluck('site_id')->toArray() ?? [];
  52. $condition[] = [function ($query) use ($siteIds) {
  53. $query->whereIn('id', $siteIds);
  54. }];
  55. }
  56. //已续费个数
  57. if ($type == 4) {
  58. $siteIdList = Site::query()->whereJsonContains('contract_ids', "3")->pluck('id');
  59. $payRecords = SitePayment::query()->whereIn('site_id', $siteIdList)->selectRaw('site_id,google_bid')->whereNotNull('google_bid')->get();
  60. $totalRenewalNum = [];
  61. foreach ($payRecords as $record) {
  62. $bids = $record->google_bid;
  63. $lastItem = array_pop($bids);
  64. if (!empty($lastItem['google_bid_time'])) {
  65. $totalRenewalNum[] = $record->site_id;
  66. }
  67. }
  68. $condition[] = [function ($query) use ($totalRenewalNum) {
  69. $query->whereIn('id', $totalRenewalNum);
  70. }];
  71. }
  72. if ($siteUserId = $request->input('siteUserId')) {
  73. $siteIds = DB::table('user_has_sites')->where(['user_id' => $siteUserId])->pluck('site_id')->toArray();
  74. }
  75. $keyword = $request->input('keyword');
  76. $builder = Site::query()->with(['users', 'sitePayment'])
  77. ->where($condition ?? [])->where(function (Builder $builder) use ($keyword) {
  78. if ($keyword) {
  79. $builder->where('domain', 'like', '%' . $keyword . '%')
  80. ->orWhere('cn_title', 'like', '%' . $keyword . '%');
  81. }
  82. })->whereJsonContains('contract_ids', "3");
  83. if (isset($siteIds)) {
  84. $builder = $builder->whereIn('id', $siteIds);
  85. }
  86. $sortName = $request->input('sortName');
  87. $sortOrder = $request->input('sortOrder');
  88. if ($sortName == 'statusTitle') {
  89. $builder->orderBy('status', $sortOrder);
  90. }
  91. $sites = $builder->orderByDesc('id')->paginate($request->input('pageSize') ?? TABLE_PAGE_SIZE);
  92. $items = $sites->items();
  93. foreach ($items as $item) {
  94. $item->designer = implode('-', $item->users->where('role_id', Role::TYPE_DESIGNER)->pluck('nickname')->toArray());
  95. $item->web_front = implode('-', $item->users->where('role_id', Role::TYPE_WEB)->pluck('nickname')->toArray());
  96. $item->bidder_title = implode('-', $item->users->where('role_id', Role::TYPE_BID)->pluck('nickname')->toArray());
  97. $item->statusTitle = Site::STATUS_MAP[$item->status] ?? '';
  98. $item->bid_give_fee = $item->sitePayment->bid_give_fee ?? '';
  99. $bidGiveFee = floatval($item->bid_give_fee);
  100. $gBid = $item->sitePayment->google_bid ?? [];
  101. $totalFee = $bidGiveFee;
  102. foreach ($gBid as $v) {
  103. $tempFee = floatval($v['google_bid_fee'] ?? 0);
  104. $totalFee += $tempFee;
  105. }
  106. $item->total_fee = $totalFee;
  107. $item->bid_account_online_time = empty($item->sitePayment->bid_account_online_time) ?
  108. '' : date('Y-m-d', strtotime($item->sitePayment->bid_account_online_time));
  109. $item->bid_account_pause_time = empty($item->sitePayment->bid_account_pause_time) ? ''
  110. : date('Y-m-d', strtotime($item->sitePayment->bid_account_pause_time));
  111. $item->bid_score = $item->sitePayment->bid_score ?? '';
  112. $item->sustain_days = '';
  113. if ($item->bid_account_online_time && $item->bid_account_pause_time) {
  114. $onlineAt = date_create($item->bid_account_online_time);
  115. $pauseAt = date_create($item->bid_account_pause_time);
  116. $diff = date_diff($pauseAt, $onlineAt);
  117. $item->sustain_days = $diff->days;
  118. }
  119. $item->is_google_bid = in_array(Contract::GOOGLE_BID, $item->contract_ids ?? []) ? 1 : 0;
  120. }
  121. $res = collect($items);
  122. if ($sortName == 'bid_account_online_time') {
  123. $res = $res->sortByDesc('bid_account_online_time');
  124. }
  125. if ($sortName == 'bid_score') {
  126. $res = $res->sortByDesc('bid_score');
  127. }
  128. if ($sortName == 'sustain_days') {
  129. $res = $res->sortByDesc('sustain_days');
  130. }
  131. return response()->json([
  132. 'rows' => array_values($res->toArray()),
  133. 'total' => $sites->total()
  134. ]);
  135. }
  136. //竞价仪表
  137. public function dashboard()
  138. {
  139. $threeMonthsAgo = date('Y-m-01 00:00:00', strtotime('first day of -2 month'));
  140. $thisMonth = date('Y-m-t 23:59:59');
  141. //竞价总费用
  142. $data['totalTotalFee'] = $this->totalFee();
  143. //竞价总费用三个月
  144. $data['totalFeeDiff'] = $this->totalFee2();
  145. //客户续费总和
  146. $data['totalAvgScore'] = $this->renew(1);
  147. //客户续费三个月
  148. $data['avgScoreDiff'] = $this->renew();
  149. //续费个数
  150. $data['totalRenewalNum'] = $this->renewal();
  151. //续费个数三个月
  152. $data['renewalNumDiff'] = $this->renewal2();
  153. //账户总个数
  154. $data['totalPpc'] = Site::query()->selectRaw('id,contract_ids')
  155. ->whereJsonContains('contract_ids', "3")->count();
  156. //账户三个月
  157. $data['ppcDiff'] = $this->ppcDiff($threeMonthsAgo, $thisMonth);
  158. //在投放个数
  159. $data['totalRedPpc'] = Site::query()->selectRaw('id,contract_ids')
  160. ->whereJsonContains('contract_ids', "3")->where('bid_status', 1)->count();
  161. //在投放个数三个月
  162. $data['redPpcDiff'] = $this->redPpcDiff($threeMonthsAgo, $thisMonth);
  163. //未投放个数
  164. $data['totalGrayPpc'] = Site::query()->whereIn('status', [2, 3, 5, 6, 7, 8, 9])->selectRaw('id,contract_ids')
  165. ->whereJsonContains('contract_ids', "3")->where('bid_status', 0)->count();
  166. //未投放个数三个月
  167. $data['grayPpcDiff'] = $this->grayPpcDiff($threeMonthsAgo, $thisMonth);
  168. //已暂停个数
  169. $data['totalLineRedPpc'] = Site::query()->join('sites_payment', 'sites.id', '=', 'sites_payment.site_id')
  170. ->whereNotNull('bid_account_pause_time')
  171. ->whereJsonContains('contract_ids', "3")->count();
  172. //已暂停个数三个月
  173. $data['lineRedPpcDiff'] = $this->lineRedPpcDiff($threeMonthsAgo, $thisMonth);
  174. //既没有暂停也没有充值过的项目
  175. $totalRedPpc = Site::query()->join('sites_payment', 'sites.id', '=', 'sites_payment.site_id')
  176. ->whereNull('bid_account_pause_time')
  177. ->whereJsonContains('contract_ids', "3")->where('bid_status', 1)->get();
  178. $num = 0;
  179. foreach ($totalRedPpc as $key => $value) {
  180. $result = json_decode($value->google_bid);
  181. if (!empty($result)) {
  182. foreach ($result as $kk => $vv) {
  183. if (empty($vv->google_bid_fee)) {
  184. $num++;
  185. break;
  186. }
  187. }
  188. } else {
  189. $num++;
  190. }
  191. }
  192. //续费率=已续费个数/(已投放个数 - 既没有暂停也没有充值过的项目)
  193. $data['totalRenewalRate'] = number_format($data['totalRenewalNum'] / ($data['totalRedPpc'] - $num), 2);
  194. //续费率三个月
  195. $data['renewalRateDiff'] = $this->ratio();
  196. $data['ppcPie'] = $this->pie();
  197. $data['feeLine'] = $this->line();
  198. return view('admin.bid.dashboard', $data);
  199. }
  200. public function renew($type = 0)
  201. {
  202. $list = Site::query()->join('sites_payment', 'sites.id', '=', 'sites_payment.site_id')
  203. ->select('google_bid')
  204. ->whereJsonContains('contract_ids', "3")
  205. ->get()->toArray() ?? [];
  206. $array = [];
  207. foreach ($list as $key => $value) {
  208. $array[] = json_decode($value['google_bid'], true);
  209. }
  210. $result = [];
  211. foreach ($array as $key => $value) {
  212. if (is_array($value)) {
  213. foreach ($value as $kk => $vv) {
  214. if ($vv['google_bid_time'] && $vv['google_bid_fee']) {
  215. $vv['date'] = date('Y-m', strtotime($vv['google_bid_time']));
  216. $result[] = $vv;
  217. }
  218. }
  219. }
  220. }
  221. if ($type != 0) {
  222. $num = 0;
  223. foreach ($result as $key => $value) {
  224. $num += (int)$value['google_bid_fee'];
  225. }
  226. return $num;
  227. }
  228. $threeMonthsAgo = date('Y-m-01 00:00:00', strtotime('first day of -2 month'));
  229. $res = $this->arrayGroupBy($result, 'date');
  230. $list1 = [];
  231. foreach ($res as $key => $value) {
  232. if (strtotime($key) >= strtotime($threeMonthsAgo)) {
  233. $num = 0;
  234. foreach ($value as $kk => $vv) {
  235. $num += (int)$vv['google_bid_fee'];
  236. }
  237. $array = [
  238. 'date' => $key,
  239. 'sum' => $num,
  240. ];
  241. $list1[] = $array;
  242. }
  243. }
  244. sort($list1);
  245. return [
  246. 'xAxis' => array_column($list1, 'date'),
  247. 'yAxis' => array_column($list1, 'sum'),
  248. ];
  249. }
  250. public function lineRedPpcDiff($threeMonthsAgo, $thisMonth)
  251. {
  252. $list = Site::query()->join('sites_payment', 'sites.id', '=', 'sites_payment.site_id')
  253. ->selectRaw('DATE_FORMAT(bid_account_pause_time,"%Y-%m") as date,count(*) as value')
  254. ->whereBetween('bid_account_pause_time', [$threeMonthsAgo, $thisMonth])
  255. ->whereJsonContains('contract_ids', "3")
  256. ->groupBy('date')
  257. ->get()->toArray() ?? [];
  258. return [
  259. 'xAxis' => array_column($list, 'date'),
  260. 'yAxis' => array_column($list, 'value'),
  261. ];
  262. }
  263. public function grayPpcDiff($threeMonthsAgo, $thisMonth)
  264. {
  265. $list = Site::query()->selectRaw('DATE_FORMAT(online_at,"%Y-%m") as date,count(*) as value')
  266. ->whereIn('status', [2, 3, 5, 6, 7, 8, 9])
  267. ->whereBetween('online_at', [$threeMonthsAgo, $thisMonth])
  268. ->whereJsonContains('contract_ids', "3")->where('bid_status', 0)
  269. ->groupBy('date')
  270. ->get()->toArray() ?? [];
  271. return [
  272. 'xAxis' => array_column($list, 'date'),
  273. 'yAxis' => array_column($list, 'value'),
  274. ];
  275. }
  276. public function redPpcDiff($threeMonthsAgo, $thisMonth, $type = 0)
  277. {
  278. $list = Site::query()->join('sites_payment', 'sites.id', '=', 'sites_payment.site_id')
  279. ->selectRaw('DATE_FORMAT(bid_account_online_time,"%Y-%m") as date,count(*) as value')
  280. ->whereBetween('bid_account_online_time', [$threeMonthsAgo, $thisMonth])
  281. ->whereJsonContains('contract_ids', "3")->where('bid_status', 1)
  282. ->groupBy('date')
  283. ->get()->toArray() ?? [];
  284. if ($type == 1) {
  285. $list = Site::query()->join('sites_payment', 'sites.id', '=', 'sites_payment.site_id')
  286. ->selectRaw('DATE_FORMAT(bid_account_online_time,"%Y-%m") as date,count(*) as value')
  287. ->whereBetween('bid_account_online_time', [$threeMonthsAgo, $thisMonth])
  288. ->whereNull('bid_account_pause_time')
  289. ->whereNull('google_bid_fee')
  290. ->whereJsonContains('contract_ids', "3")->where('bid_status', 1)
  291. ->groupBy('date')
  292. ->get()->toArray() ?? [];
  293. return $list;
  294. }
  295. return [
  296. 'xAxis' => array_column($list, 'date'),
  297. 'yAxis' => array_column($list, 'value'),
  298. ];
  299. }
  300. public function ppcDiff($threeMonthsAgo, $thisMonth)
  301. {
  302. $list = Site::query()->selectRaw('DATE_FORMAT(order_at,"%Y-%m") as date,count(*) as value')
  303. ->whereBetween('order_at', [$threeMonthsAgo, $thisMonth])
  304. ->whereJsonContains('contract_ids', "3")
  305. ->groupBy('date')
  306. ->get()->toArray() ?? [];
  307. return [
  308. 'xAxis' => array_column($list, 'date'),
  309. 'yAxis' => array_column($list, 'value'),
  310. ];
  311. }
  312. public function ratio()
  313. {
  314. $ratio = $this->renewal2(1);
  315. //在投放个数三个月
  316. $threeMonthsAgo = date('Y-m-01 00:00:00', strtotime('first day of -2 month'));
  317. $thisMonth = date('Y-m-t 23:59:59');
  318. $res = $this->redPpcDiff($threeMonthsAgo, $thisMonth, 1);
  319. foreach ($ratio as $key => $value) {
  320. foreach ($res as $kk => $vv) {
  321. if ($value['date'] == $vv['date']) {
  322. $ratio[$key]['ratio'] = number_format($value['count'] / $vv['value'], 2);
  323. }
  324. }
  325. }
  326. sort($ratio);
  327. return [
  328. 'xAxis' => array_column($ratio, 'date'),
  329. 'yAxis' => array_column($ratio, 'ratio'),
  330. ];
  331. }
  332. //饼状图
  333. private function pie()
  334. {
  335. $no = Site::query()->selectRaw('id,contract_ids')
  336. ->whereJsonContains('contract_ids', "3")->where('bid_status', '<>', 1)->count();
  337. $in = Site::query()->selectRaw('id,contract_ids')
  338. ->whereJsonContains('contract_ids', "3")->where('bid_status', 1)->count();
  339. $pause = Site::query()->join('sites_payment', 'sites.id', '=', 'sites_payment.site_id')
  340. ->whereNotNull('bid_account_pause_time')
  341. ->where('bid_status', '=', 1)->whereJsonContains('contract_ids', "3")->count();
  342. return [
  343. 'xAxis' => ['待投放个数', '投放个数', '已暂停个数'],
  344. 'hybrid' => [
  345. ['name' => '待投放个数', 'value' => $no],
  346. ['name' => '投放个数', 'value' => $in],
  347. ['name' => '已暂停个数', 'value' => $pause],
  348. ]
  349. ];
  350. }
  351. //折线图
  352. private function line()
  353. {
  354. $monthScope = [];
  355. $monthResult = [];
  356. for ($i = 0; $i < 6; $i++) {
  357. $monthScope[] = date('Y-m', strtotime('first day of -' . $i . ' month'));
  358. $monthResult[date('Ym', strtotime('first day of -' . $i . ' month'))] = [
  359. 'begin' => strtotime(date('Y-m-d 00:00:00', strtotime('first day of -' . $i . ' month'))),
  360. 'end' => strtotime(date('Y-m-d 23:59:59', strtotime('last day of -' . $i . ' month'))),
  361. 'total_fee' => 0
  362. ];
  363. }
  364. $where = sprintf("google_bid->'$[*].google_bid_time' REGEXP '%s'", implode("|", $monthScope));
  365. $payRecords = SitePayment::query()->select(['site_id', 'google_bid'])->whereRaw($where)->get();
  366. foreach ($payRecords as $item) {
  367. $bids = $item->google_bid;
  368. foreach ($bids as $val) {
  369. if (!empty($val['google_bid_time'])) {
  370. $time = strtotime($val['google_bid_time']);
  371. foreach ($monthResult as &$v) {
  372. if ($time >= $v['begin'] && $time <= $v['end']) {
  373. $v['total_fee'] += floatval($val['google_bid_fee']);
  374. }
  375. }
  376. }
  377. }
  378. }
  379. return [
  380. 'xAxis' => array_reverse(array_keys($monthResult)),
  381. 'yAxis' => array_reverse(array_column($monthResult, 'total_fee'))
  382. ];
  383. }
  384. //续费
  385. private function renewal()
  386. {
  387. $siteIdList = Site::query()->whereJsonContains('contract_ids', "3")->pluck('id');
  388. $payRecords = SitePayment::query()->whereIn('site_id', $siteIdList)->selectRaw('site_id,google_bid')->whereNotNull('google_bid')->get();
  389. $totalRenewalNum = 0;
  390. foreach ($payRecords as $record) {
  391. $bids = $record->google_bid;
  392. $lastItem = array_pop($bids);
  393. if (!empty($lastItem['google_bid_time'])) {
  394. $totalRenewalNum++;
  395. }
  396. }
  397. return $totalRenewalNum;
  398. }
  399. //续费三个月
  400. private function renewal2($type = 0)
  401. {
  402. $siteIdList = Site::query()->whereJsonContains('contract_ids', "3")->pluck('id');
  403. $payRecords = SitePayment::query()->whereIn('site_id', $siteIdList)->select('site_id', 'google_bid')->whereNotNull('google_bid')->get();
  404. $infos = [];
  405. foreach ($payRecords as $record) {
  406. foreach ($record->google_bid as $kk => $vv) {
  407. $res = [
  408. 'site_id' => $record->site_id,
  409. 'google_bid_fee' => $vv['google_bid_fee'],
  410. 'google_bid_time' => $vv['google_bid_time'],
  411. ];
  412. $infos[] = $res;
  413. }
  414. }
  415. $array = [];
  416. foreach ($infos as $kk => $vv) {
  417. if (!empty($vv['google_bid_time'])) {
  418. $vv['google_bid_date'] = date('Y-m', strtotime($vv['google_bid_time']));
  419. $array[] = $vv;
  420. }
  421. }
  422. $threeMonthsAgo = date('Y-m-01 00:00:00', strtotime('first day of -2 month'));
  423. $argc = [];
  424. $result = $this->arrayGroupBy($array, 'google_bid_date');
  425. foreach ($result as $key => $value) {
  426. //去掉每个月相同项目id
  427. $array = $this->arrUniq($value, 'site_id');
  428. foreach ($array as $kk => $vv) {
  429. $argc[] = $vv;
  430. }
  431. }
  432. $arrayGroupBy = $this->arrayGroupBy($argc, 'google_bid_date');
  433. $list = [];
  434. foreach ($arrayGroupBy as $key => $value) {
  435. if (strtotime($key) >= strtotime($threeMonthsAgo)) {
  436. $array = [
  437. 'date' => $key,
  438. 'count' => count($value),
  439. ];
  440. $list[] = $array;
  441. }
  442. }
  443. sort($list);
  444. if ($type != 0) {
  445. return $list;
  446. }
  447. return [
  448. 'xAxis' => array_column($list, 'date'),
  449. 'yAxis' => array_column($list, 'count'),
  450. ];
  451. }
  452. //二维数组按照指定键值去重
  453. function arrUniq($arr, $key)
  454. {
  455. $key_arr = [];
  456. foreach ($arr as $k => $v) {
  457. if (in_array($v[$key], $key_arr)) {
  458. unset($arr[$k]);
  459. } else {
  460. $key_arr[] = $v[$key];
  461. }
  462. }
  463. return $arr;
  464. }
  465. //二维数组GroupBy
  466. function arrayGroupBy($arr, $key)
  467. {
  468. $grouped = array();
  469. foreach ($arr as $value) {
  470. $grouped[$value[$key]][] = $value;
  471. }
  472. if (func_num_args() > 2) {
  473. $args = func_get_args();
  474. foreach ($grouped as $key => $value) {
  475. $parameter = array_merge($value, array_slice($args, 2, func_num_args()));
  476. $grouped[$key] = call_user_func_array('array_group_by', $parameter);
  477. }
  478. }
  479. return $grouped;
  480. }
  481. private function totalFee()
  482. {
  483. $totalRecords = Site::query()
  484. ->whereJsonContains('contract_ids', "3")
  485. ->where('bid_status', '=', 1)
  486. ->with(['sitePayment' => function (\Illuminate\Database\Eloquent\Relations\HasOne $builder) {
  487. $builder->selectRaw('site_id,bid_give_fee,google_bid');
  488. }])->selectRaw('id,order_at')->get();
  489. $totalTotalFee = 0;
  490. foreach ($totalRecords as $record) {
  491. $record->bid_give_fee = $record->sitePayment->bid_give_fee ?? '';
  492. $bidGiveFee = floatval($record->bid_give_fee);
  493. $gBid = $record->sitePayment->google_bid ?? [];
  494. $totalFee = $bidGiveFee;
  495. foreach ($gBid as $v) {
  496. $tempFee = floatval($v['google_bid_fee'] ?? 0);
  497. $totalFee += $tempFee;
  498. }
  499. $totalTotalFee += $totalFee;
  500. }
  501. return $totalTotalFee;
  502. }
  503. private function totalFee2()
  504. {
  505. $totalRecords = Site::query()
  506. ->whereJsonContains('contract_ids', "3")
  507. ->where('bid_status', '=', 1)
  508. ->with(['sitePayment' => function (\Illuminate\Database\Eloquent\Relations\HasOne $builder) {
  509. $builder->selectRaw('site_id,bid_give_fee,google_bid');
  510. }])->selectRaw('id,order_at')->get();
  511. $list = [];
  512. foreach ($totalRecords as $record) {
  513. $list[] = $record->sitePayment->google_bid;
  514. }
  515. $array = [];
  516. foreach ($list as $key => $value) {
  517. if (is_array($value)) {
  518. foreach ($value as $kk => $vv) {
  519. if (!empty($vv['google_bid_time'])) {
  520. $vv['google_bid_date'] = date('Y-m', strtotime($vv['google_bid_time']));
  521. $array[] = $vv;
  522. }
  523. }
  524. }
  525. }
  526. $threeMonthsAgo = date('Y-m-01 00:00:00', strtotime('first day of -2 month'));
  527. $list = [];
  528. $result = $this->arrayGroupBy($array, 'google_bid_date');
  529. foreach ($result as $key => $value) {
  530. if (strtotime($key) >= strtotime($threeMonthsAgo)) {
  531. $num = 0;
  532. foreach ($value as $kk => $vv) {
  533. $num += (int)$vv['google_bid_fee'];
  534. }
  535. $array = [
  536. 'date' => $key,
  537. 'sum' => $num,
  538. ];
  539. $list[] = $array;
  540. }
  541. }
  542. sort($list);
  543. return [
  544. 'xAxis' => array_column($list, 'date'),
  545. 'yAxis' => array_column($list, 'sum'),
  546. ];
  547. }
  548. protected function avgScore()
  549. {
  550. $totalRecords = Site::query()->with(['sitePayment' => function (\Illuminate\Database\Eloquent\Relations\HasOne $builder) {
  551. $builder->selectRaw('site_id,bid_score');
  552. }])->selectRaw('id,order_at')->get();
  553. $totalNum = 0;
  554. $totalTotalScore = 0;
  555. foreach ($totalRecords as $record) {
  556. if (!empty($record->sitePayment->bid_score)) {
  557. $totalNum++;
  558. $totalTotalScore += floatval($record->sitePayment->bid_score);
  559. }
  560. }
  561. $totalAvg = $totalNum == 0 ? 0 : $totalTotalScore / $totalNum;
  562. return number_format($totalAvg, 2);
  563. }
  564. protected function avgScore2()
  565. {
  566. $totalRecords = Site::query()->with(['sitePayment' => function (\Illuminate\Database\Eloquent\Relations\HasOne $builder) {
  567. $builder->selectRaw('site_id,bid_score,bid_score_time');
  568. }])->selectRaw('id,order_at')->get();
  569. $array = [];
  570. foreach ($totalRecords as $record) {
  571. if (!empty($record->sitePayment->bid_score)) {
  572. $data = [
  573. 'bid_score' => $record->sitePayment->bid_score,
  574. 'bid_score_time' => $record->sitePayment->bid_score_time,
  575. 'date' => date('Y-m', strtotime($record->sitePayment->bid_score_time)),
  576. ];
  577. $array[] = $data;
  578. }
  579. }
  580. $threeMonthsAgo = date('Y-m-01 00:00:00', strtotime('first day of -2 month'));
  581. $result = $this->arrayGroupBy($array, 'date');
  582. $list = [];
  583. foreach ($result as $key => $value) {
  584. if (strtotime($key) >= strtotime($threeMonthsAgo)) {
  585. $num = 0;
  586. $sum = 0;
  587. foreach ($value as $kk => $vv) {
  588. $num++;
  589. $sum += floatval($vv['bid_score']);
  590. }
  591. $average = $sum / $num;
  592. $array = [
  593. 'date' => $key,
  594. 'average' => number_format($average, 2),
  595. ];
  596. $list[] = $array;
  597. }
  598. }
  599. sort($list);
  600. return [
  601. 'xAxis' => array_column($list, 'date'),
  602. 'yAxis' => array_column($list, 'average'),
  603. ];
  604. }
  605. //上线账户
  606. public function online(Request $request)
  607. {
  608. if (!$request->ajax()) {
  609. return view('admin.bid.online');
  610. }
  611. $keyword = $request->input('keyword');
  612. $status = $request->input('status');
  613. $payIds = SitePayment::query()->whereNotNull('bid_account_online_time')->pluck('site_id')->toArray();
  614. $records = Site::query()
  615. ->where($condition ?? [])
  616. ->where(function (Builder $builder) use ($keyword, $status) {
  617. if ($keyword) {
  618. $builder->where('domain', 'like', '%' . $keyword . '%')
  619. ->orWhere('cn_title', 'like', '%' . $keyword . '%');
  620. }
  621. if ($status) {
  622. $builder->where('status', $status);
  623. }
  624. })
  625. ->whereIn('id', $payIds)->with(['users', 'sitePayment'])
  626. ->get();
  627. foreach ($records as $record) {
  628. $record->bidder_title = implode('-', $record->users->where('role_id', Role::TYPE_BID)->pluck('nickname')->toArray());
  629. $record->bid_give_fee = $record->sitePayment->bid_give_fee ?? '';
  630. $record->bid_account_online_time = empty($record->sitePayment->bid_account_online_time) ? '' : date('Y-m-d', strtotime($record->sitePayment->bid_account_online_time));
  631. }
  632. $res = collect($records->toArray())->sortByDesc('bid_account_online_time');
  633. $perPage = $request->input('pageSize') ?? TABLE_PAGE_SIZE;
  634. $result = $this->paginateCollection($res, $perPage);
  635. return response()->json([
  636. 'rows' => $result->items(),
  637. 'total' => $result->total(),
  638. ]);
  639. }
  640. //充值账户
  641. public function recharge(Request $request)
  642. {
  643. if (!$request->ajax()) {
  644. return view('admin.bid.recharge');
  645. }
  646. $pageSize = $request->input('pageSize') ?? TABLE_PAGE_SIZE;
  647. $monthScope = [];
  648. for ($i = 0; $i < 36; $i++) {
  649. $monthScope[] = date('Y-m', strtotime('first day of -' . $i . ' month'));
  650. }
  651. $where = sprintf("google_bid->'$[*].google_bid_time' REGEXP '%s'", implode("|", $monthScope));
  652. $site = SitePayment::query();
  653. $keyword = $request->input('keyword');
  654. $status = $request->input('status');
  655. if (!empty($keyword)) {
  656. $siteIds = Site::query()->where('domain', 'like', '%' . $keyword . '%')
  657. ->orWhere('cn_title', 'like', '%' . $keyword . '%')
  658. ->pluck('id');
  659. $site->whereIn('site_id', $siteIds);
  660. }
  661. if (!empty($status)) {
  662. $siteIds = Site::query()
  663. ->where('status', $status)
  664. ->pluck('id');
  665. $site->whereIn('site_id', $siteIds);
  666. }
  667. if (!empty($keyword) && !empty($status)) {
  668. $siteIds = Site::query()->where('domain', 'like', '%' . $keyword . '%')
  669. ->orWhere('cn_title', 'like', '%' . $keyword . '%')
  670. ->where('status', $status)
  671. ->pluck('id');
  672. $site->whereIn('site_id', $siteIds);
  673. }
  674. $payRecords = $site->whereNotNull('bid_account_online_time')->select(['site_id', 'google_bid'])->whereRaw($where)->get();
  675. $results = [];
  676. foreach ($payRecords as $record) {
  677. $bids = $record->google_bid;
  678. foreach ($bids as $item) {
  679. if (strtotime($item['google_bid_time']) >= strtotime($monthScope[35])) {
  680. $results[] = [
  681. 'site_id' => $record->site_id,
  682. 'fee' => $item['google_bid_fee'],
  683. 'time' => $item['google_bid_time'],
  684. ];
  685. }
  686. }
  687. }
  688. $siteIds = array_column($results, 'site_id');
  689. $siteRecords = Site::query()
  690. ->with(['users'])
  691. ->selectRaw('id,domain,cn_title')
  692. ->whereIn('id', $siteIds)->get();
  693. foreach ($siteRecords as $item) {
  694. $item->bidder_title = implode('-', $item->users->where('role_id', Role::TYPE_BID)->pluck('nickname')->toArray());
  695. unset($item->users);
  696. }
  697. $mapRecords = $siteRecords->keyBy('id')->toArray();
  698. foreach ($results as &$record) {
  699. $record = array_merge($record, $mapRecords[$record['site_id']] ?? '');
  700. }
  701. $res = collect($results)->sortByDesc('time');
  702. $result = $this->paginateCollection($res, $pageSize);
  703. return response()->json([
  704. 'rows' => $result->items(),
  705. 'total' => $result->total(),
  706. ]);
  707. }
  708. //暂停账户
  709. public function pause(Request $request)
  710. {
  711. if (!$request->ajax()) {
  712. return view('admin.bid.pause');
  713. }
  714. $payIds = SitePayment::query()->whereNotNull('bid_account_pause_time')->orderBy('bid_account_pause_time', 'desc')->pluck('site_id')->toArray();
  715. $keyword = $request->input('keyword');
  716. $status = $request->input('status');
  717. $records = Site::query()
  718. ->where(function (Builder $builder) use ($keyword, $status) {
  719. if ($keyword) {
  720. $builder->where('domain', 'like', '%' . $keyword . '%')
  721. ->orWhere('cn_title', 'like', '%' . $keyword . '%');
  722. }
  723. if ($status) {
  724. $builder->where('status', $status);
  725. }
  726. })
  727. ->whereIn('id', $payIds)->with(['users', 'sitePayment'])->get();
  728. foreach ($records as $record) {
  729. $record->bidder_title = implode('-', $record->users->where('role_id', Role::TYPE_BID)->pluck('nickname')->toArray());
  730. $record->bid_give_fee = $record->sitePayment->bid_give_fee ?? '';
  731. $record->bid_account_pause_time = $record->sitePayment->bid_account_pause_time ?? '';
  732. $record->bid_give_fee = $record->sitePayment->bid_give_fee ?? '';
  733. $bidGiveFee = floatval($record->bid_give_fee);
  734. $gBid = $record->sitePayment->google_bid ?? [];
  735. $totalFee = $bidGiveFee;
  736. foreach ($gBid as $v) {
  737. $tempFee = floatval($v['google_bid_fee'] ?? 0);
  738. $totalFee += $tempFee;
  739. }
  740. $record->total_fee = $totalFee;
  741. }
  742. $res = collect($records->toArray())->sortByDesc('bid_account_pause_time');
  743. $perPage = $request->input('pageSize') ?? TABLE_PAGE_SIZE;
  744. $result = $this->paginateCollection($res, $perPage);
  745. return response()->json([
  746. 'rows' => $result->items(),
  747. 'total' => count($res),
  748. ]);
  749. }
  750. //客户评分
  751. public function score(Request $request)
  752. {
  753. if (!$request->ajax()) {
  754. return view('admin.bid.score');
  755. }
  756. $payIds = SitePayment::query()->whereNotNull('bid_score_time')->orderBy('bid_score_time', 'desc')->pluck('site_id');
  757. $keyword = $request->input('keyword');
  758. $status = $request->input('status');
  759. $records = Site::query()
  760. ->whereIn('id', $payIds)
  761. ->with(['users', 'sitePayment'])
  762. ->where(function (Builder $builder) use ($keyword, $status) {
  763. if ($keyword) {
  764. $builder->where('domain', 'like', '%' . $keyword . '%')
  765. ->orWhere('cn_title', 'like', '%' . $keyword . '%');
  766. }
  767. if ($status) {
  768. $builder->where('status', $status);
  769. }
  770. })->get();
  771. foreach ($records as $record) {
  772. $record->bidder_title = implode('-', $record->users->where('role_id', Role::TYPE_BID)->pluck('nickname')->toArray());
  773. $record->bid_score = $record->sitePayment->bid_score ?? '';
  774. $record->bid_score_time = $record->sitePayment->bid_score_time ?? '';
  775. }
  776. $res = collect($records->toArray())->sortByDesc('score');
  777. $perPage = $request->input('pageSize') ?? TABLE_PAGE_SIZE;
  778. $result = $this->paginateCollection($res, $perPage);
  779. return response()->json([
  780. 'rows' => $result->items(),
  781. 'total' => $result->total(),
  782. ]);
  783. }
  784. }