<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class MiniprogramController extends Controller
{
    public function actiondata(Request $request)
    {
        try {
            $this->validate($request, [
                'start_at' => 'required',
                'end_at' => 'required',
                'public_id' => 'required'
            ]);
            $public_id = $request->get('public_id') ?? 0;
            $result = array(
                'total' => [
                    'all' => [
                        'pv' => 0,
                        'uv' => 0,
                    ],
                    '1x' => [
                        'pv' => 0,
                        'uv' => 0,
                    ],
                    '2x' => [
                        'pv' => 0,
                        'uv' => 0,
                    ],
                    '3x' => [
                        'pv' => 0,
                        'uv' => 0,
                    ],
                    '4x' => [
                        'pv' => 0,
                        'uv' => 0,
                    ],
                ],
                'list' => [
                    '1x' => [],
                    '2x' => [],
                    '3x' => [],
                    '4x' => []
                ],
            );
            \DB::enableQueryLog();
            $uvall = \DB::select(
                "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
                [
                    $public_id,
                    10000,
                    49999,
                    strtotime($request->post('start_at')),
                    strtotime($request->post('end_at')) + 86400
                ]
            );
            $uv1x = \DB::select(
                "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
                [
                    $public_id,
                    10000,
                    19999,
                    strtotime($request->post('start_at')),
                    strtotime($request->post('end_at')) + 86400
                ]
            );
            $uv2x = \DB::select(
                "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
                [
                    $public_id,
                    20000,
                    29999,
                    strtotime($request->post('start_at')),
                    strtotime($request->post('end_at')) + 86400
                ]
            );
            $uv3x = \DB::select(
                "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
                [
                    $public_id,
                    30000,
                    39999,
                    strtotime($request->post('start_at')),
                    strtotime($request->post('end_at')) + 86400
                ]
            );
            $uv4x = \DB::select(
                "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
                [
                    $public_id,
                    40000,
                    49999,
                    strtotime($request->post('start_at')),
                    strtotime($request->post('end_at')) + 86400
                ]
            );

            $result['total']['all']['uv'] = $uvall[0]->c;
            $result['total']['1x']['uv'] = $uv1x[0]->c;
            $result['total']['2x']['uv'] = $uv2x[0]->c;
            $result['total']['3x']['uv'] = $uv3x[0]->c;
            $result['total']['4x']['uv'] = $uv4x[0]->c;

            $pvs = \DB::select(
                "SELECT `action_id`, COUNT(*) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `created_at` BETWEEN ? and ? GROUP BY `action_id`",
                [$public_id, strtotime($request->post('start_at')), strtotime($request->post('end_at')) + 86400]
            );
            foreach ($pvs as $v) {
                if ($v->action_id >= 10000 && $v->action_id <= 19999) {
                    $result['total']['all']['pv'] += $v->c;
                    $result['total']['1x']['pv'] += $v->c;
                    $result['list']['1x'][$v->action_id]['pv'] = $v->c;
                }
                if ($v->action_id >= 20000 && $v->action_id <= 29999) {
                    $result['total']['all']['pv'] += $v->c;
                    $result['total']['2x']['pv'] += $v->c;
                    $result['list']['2x'][$v->action_id]['pv'] = $v->c;
                }
                if ($v->action_id >= 30000 && $v->action_id <= 39999) {
                    $result['total']['all']['pv'] += $v->c;
                    $result['total']['3x']['pv'] += $v->c;
                    $result['list']['3x'][$v->action_id]['pv'] = $v->c;
                }
                if ($v->action_id >= 40000 && $v->action_id <= 49999) {
                    $result['total']['all']['pv'] += $v->c;
                    $result['total']['4x']['pv'] += $v->c;
                    $result['list']['4x'][$v->action_id]['pv'] = $v->c;
                }
            }
            $uvs = \DB::select(
                "SELECT `action_id`, COUNT(*) as c FROM( SELECT DISTINCT(`uid`), `action_id` FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `created_at` BETWEEN ? and ?) as tab GROUP BY`action_id`",
                [$public_id, strtotime($request->post('start_at')), strtotime($request->post('end_at')) + 86400]
            );
            foreach ($uvs as $v) {
                if ($v->action_id >= 10000 && $v->action_id <= 19999) {
                    $result['list']['1x'][$v->action_id]['uv'] = $v->c;
                }
                if ($v->action_id >= 20000 && $v->action_id <= 29999) {
                    $result['list']['2x'][$v->action_id]['uv'] = $v->c;
                }
                if ($v->action_id >= 30000 && $v->action_id <= 39999) {
                    $result['list']['3x'][$v->action_id]['uv'] = $v->c;
                }
                if ($v->action_id >= 40000 && $v->action_id <= 49999) {
                    $result['list']['4x'][$v->action_id]['uv'] = $v->c;
                }
            }
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => $result
            );
        } catch (\Exception $e) {
            throw new \ApiException($e->getMessage(), 501);
        }
    }
}
